+
+}
+
+/*********************************************************************
+ *
+ * Function : get_destination_from_headers
+ *
+ * Description : Parse the "Host:" header to get the request's destination.
+ * Only needed if the client's request was forcefully
+ * redirected into Privoxy.
+ *
+ * Code mainly copied from client_host() which is currently
+ * run too late for this purpose.
+ *
+ * Parameters :
+ * 1 : headers = List of headers (one of them hopefully being
+ * the "Host:" header)
+ * 2 : http = storage for the result (host, port and hostport).
+ *
+ * Returns : JB_ERR_MEMORY in case of memory problems,
+ * JB_ERR_PARSE if the host header couldn't be found,
+ * JB_ERR_OK otherwise.
+ *
+ *********************************************************************/
+jb_err get_destination_from_headers(const struct list *headers, struct http_request *http)
+{
+ char *q;
+ char *p;
+ char *host;
+
+ host = get_header_value(headers, "Host:");
+
+ if (NULL == host)
+ {
+ log_error(LOG_LEVEL_ERROR, "No \"Host:\" header found.");
+ return JB_ERR_PARSE;
+ }
+
+ if (NULL == (p = strdup((host))))
+ {
+ log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
+ return JB_ERR_MEMORY;
+ }
+ chomp(p);
+ if (NULL == (q = strdup(p)))
+ {
+ freez(p);
+ log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
+ return JB_ERR_MEMORY;
+ }
+
+ freez(http->hostport);
+ http->hostport = p;
+ freez(http->host);
+ http->host = q;
+ q = strchr(http->host, ':');
+ if (q != NULL)
+ {
+ /* Terminate hostname and evaluate port string */
+ *q++ = '\0';
+ http->port = atoi(q);
+ }
+ else
+ {
+ http->port = http->ssl ? 443 : 80;
+ }
+
+ log_error(LOG_LEVEL_HEADER, "Destination extracted from \"Host:\" header: %s = %s:%d",
+ http->hostport, http->host, http->port);
+
+ return JB_ERR_OK;
+