+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : NULL => trusted, else http_response.
+ *
+ *********************************************************************/
+struct http_response *trust_url(struct client_state *csp)
+{
+ struct http_response *rsp;
+ struct map * exports;
+ char buf[BUFFER_SIZE];
+ char *p;
+ struct url_spec **tl;
+ struct url_spec *t;
+ jb_err err;
+
+ /*
+ * Don't bother to work on trusted URLs
+ */
+ if (!is_untrusted_url(csp))
+ {
+ return NULL;
+ }
+
+ /*
+ * Else, prepare a response:
+ */
+ if (NULL == (rsp = alloc_http_response()))
+ {
+ return cgi_error_memory();
+ }
+
+ exports = default_exports(csp, NULL);
+ if (exports == NULL)
+ {
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ /*
+ * Export the host, port, and referrer information
+ */
+ err = map(exports, "hostport", 1, csp->http->hostport, 1)
+ || map(exports, "path", 1, csp->http->path, 1)
+ || map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0)
+ || map(exports, "path-html", 1, html_encode(csp->http->path), 0);
+
+ if (csp->referrer && strlen(csp->referrer) > 9)
+ {
+ err = err || map(exports, "referrer", 1, csp->referrer + 9, 1);
+ err = err || map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
+ }
+ else
+ {
+ err = err || map(exports, "referrer", 1, "unknown", 1);
+ err = err || map(exports, "referrer-html", 1, "unknown", 1);
+ }
+
+ if (err)
+ {
+ free_map(exports);
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ /*
+ * Export the trust list
+ */
+ p = strdup("");
+ for (tl = csp->config->trust_list; (t = *tl) ; tl++)
+ {
+ sprintf(buf, "<li>%s</li>\n", t->spec);
+ string_append(&p, buf);
+ }
+ err = map(exports, "trusted-referrers", 1, p, 0);
+
+ if (err)
+ {
+ free_map(exports);
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ /*
+ * Export the trust info, if available
+ */
+ if (csp->config->trust_info->first)
+ {
+ struct list_entry *l;
+
+ p = strdup("");
+ for (l = csp->config->trust_info->first; l ; l = l->next)
+ {
+ sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
+ string_append(&p, buf);
+ }
+ err = map(exports, "trust-info", 1, p, 0);
+ }
+ else
+ {
+ err = map_block_killer(exports, "have-trust-info");
+ }
+
+ if (err)
+ {
+ free_map(exports);
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ /*
+ * Export the force prefix or the force conditional block killer
+ */
+#ifdef FEATURE_FORCE_LOAD
+ err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
+#else /* ifndef FEATURE_FORCE_LOAD */
+ err = map_block_killer(exports, "force-support");
+#endif /* ndef FEATURE_FORCE_LOAD */
+
+ if (err)
+ {
+ free_map(exports);
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ /*
+ * Build the response
+ */
+ err = template_fill_for_cgi(csp, "untrusted", exports, rsp);
+ if (err)
+ {
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ return finish_http_response(rsp);
+}
+#endif /* def FEATURE_TRUST */
+
+
+#ifdef FEATURE_FAST_REDIRECTS
+/*********************************************************************
+ *
+ * Function : redirect_url
+ *
+ * Description : Checks for redirection URLs and returns a HTTP redirect
+ * to the destination URL, if necessary
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : NULL if URL was clean, HTTP redirect otherwise.
+ *
+ *********************************************************************/
+struct http_response *redirect_url(struct client_state *csp)
+{
+ char *p, *q;
+ struct http_response *rsp;
+
+ p = q = csp->http->path;
+ log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
+
+ /*
+ * find the last URL encoded in the request
+ */
+ while ((p = strstr(p, "http://")))
+ {
+ q = p++;
+ }
+
+ /*
+ * if there was any, generate and return a HTTP redirect
+ */
+ if (q != csp->http->path)
+ {
+ log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
+
+ if (NULL == (rsp = alloc_http_response()))
+ {
+ return cgi_error_memory();
+ }
+
+ if ( enlist_unique_header(rsp->headers, "Location", q)
+ || (NULL == (rsp->status = strdup("302 Local Redirect from Junkbuster"))) )
+ {
+ free_http_response(rsp);
+ return cgi_error_memory();
+ }
+
+ return finish_http_response(rsp);
+ }
+ else
+ {
+ return NULL;
+ }
+
+}
+#endif /* def FEATURE_FAST_REDIRECTS */
+
+
+#ifdef FEATURE_IMAGE_BLOCKING
+/*********************************************************************
+ *
+ * Function : is_imageurl