+/*********************************************************************
+ *
+ * Function : server_proxy_connection
+ *
+ * Description : Figures out whether or not we should add a
+ * Proxy-Connection header.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK.
+ *
+ *********************************************************************/
+static jb_err server_proxy_connection(struct client_state *csp, char **header)
+{
+ csp->flags |= CSP_FLAG_SERVER_PROXY_CONNECTION_HEADER_SET;
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_keep_alive
+ *
+ * Description : Stores the client's keep alive timeout.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK.
+ *
+ *********************************************************************/
+static jb_err client_keep_alive(struct client_state *csp, char **header)
+{
+ unsigned int keep_alive_timeout;
+ const char *timeout_position = strstr(*header, ": ");
+
+ if (!(csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE))
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "keep-alive support is disabled. Crunching: %s.", *header);
+ freez(*header);
+ return JB_ERR_OK;
+ }
+
+ if ((NULL == timeout_position)
+ || (1 != sscanf(timeout_position, ": %u", &keep_alive_timeout)))
+ {
+ log_error(LOG_LEVEL_ERROR, "Couldn't parse: %s", *header);
+ }
+ else
+ {
+ if (keep_alive_timeout < csp->config->keep_alive_timeout)
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Reducing keep-alive timeout from %u to %u.",
+ csp->config->keep_alive_timeout, keep_alive_timeout);
+ csp->server_connection.keep_alive_timeout = keep_alive_timeout;
+ }
+ else
+ {
+ /* XXX: Is this log worthy? */
+ log_error(LOG_LEVEL_HEADER,
+ "Client keep-alive timeout is %u. Sticking with %u.",
+ keep_alive_timeout, csp->config->keep_alive_timeout);
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_content_length
+ *
+ * Description : Gets the content length specified in a
+ * Content-Length header.
+ *
+ * Parameters :
+ * 1 : header = The Content-Length header.
+ * 2 : length = Storage to return the value.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_PARSE if no value is recognized.
+ *
+ *********************************************************************/
+static jb_err get_content_length(const char *header, unsigned long long *length)
+{
+ assert(header[14] == ':');
+
+#ifdef _WIN32
+ assert(sizeof(unsigned long long) > 4);
+ if (1 != sscanf(header+14, ": %I64u", length))
+#else
+ if (1 != sscanf(header+14, ": %llu", length))
+#endif
+ {
+ return JB_ERR_PARSE;
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_save_content_length
+ *
+ * Description : Save the Content-Length sent by the client.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_save_content_length(struct client_state *csp, char **header)
+{
+ unsigned long long content_length = 0;
+
+ assert(*(*header+14) == ':');
+
+ if (JB_ERR_OK != get_content_length(*header, &content_length))
+ {
+ log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
+ freez(*header);
+ }
+ else
+ {
+ csp->expected_client_content_length = content_length;
+ }
+
+ return JB_ERR_OK;
+}
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
+
+
+/*********************************************************************
+ *
+ * Function : client_connection
+ *
+ * Description : Makes sure a proper "Connection:" header is
+ * set and signals connection_header_adder
+ * to do nothing.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_connection(struct client_state *csp, char **header)
+{
+ static const char connection_close[] = "Connection: close";
+
+ if (!strcmpic(*header, connection_close))
+ {
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+ if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING))
+ {
+ if (!strcmpic(csp->http->ver, "HTTP/1.1"))
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Removing \'%s\' to imply keep-alive.", *header);
+ freez(*header);
+ }
+ else
+ {
+ char *old_header = *header;
+
+ *header = strdup("Connection: keep-alive");
+ if (header == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+ log_error(LOG_LEVEL_HEADER,
+ "Replaced: \'%s\' with \'%s\'", old_header, *header);
+ freez(old_header);
+ }
+ }
+ else
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Keeping the client header '%s' around. "
+ "The connection will not be kept alive.",
+ *header);
+ csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
+ }
+ }
+ else if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE))
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Keeping the client header '%s' around. "
+ "The server connection will be kept alive if possible.",
+ *header);
+ csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+ }
+ else
+ {
+ char *old_header = *header;
+
+ *header = strdup(connection_close);
+ if (header == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+ log_error(LOG_LEVEL_HEADER,
+ "Replaced: \'%s\' with \'%s\'", old_header, *header);
+ freez(old_header);
+ }
+
+ /* Signal client_connection_adder() to return early. */
+ csp->flags |= CSP_FLAG_CLIENT_CONNECTION_HEADER_SET;
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : crumble
+ *
+ * Description : This is called if a header matches a pattern to "crunch"
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err crumble(struct client_state *csp, char **header)
+{
+ (void)csp;
+ log_error(LOG_LEVEL_HEADER, "crumble crunched: %s!", *header);
+ freez(*header);
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : crunch_server_header
+ *
+ * Description : Crunch server header if it matches a string supplied by the
+ * user. Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success and always succeeds
+ *
+ *********************************************************************/
+static jb_err crunch_server_header(struct client_state *csp, char **header)
+{
+ const char *crunch_pattern;
+
+ /* Do we feel like crunching? */
+ if ((csp->action->flags & ACTION_CRUNCH_SERVER_HEADER))
+ {
+ crunch_pattern = csp->action->string[ACTION_STRING_SERVER_HEADER];
+
+ /* Is the current header the lucky one? */
+ if (strstr(*header, crunch_pattern))
+ {
+ log_error(LOG_LEVEL_HEADER, "Crunching server header: %s (contains: %s)", *header, crunch_pattern);
+ freez(*header);
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : server_content_type
+ *
+ * Description : Set the content-type for filterable types (text/.*,
+ * .*xml.*, javascript and image/gif) unless filtering has been
+ * forbidden (CT_TABOO) while parsing earlier headers.
+ * NOTE: Since text/plain is commonly used by web servers
+ * for files whose correct type is unknown, we don't
+ * set CT_TEXT for it.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_content_type(struct client_state *csp, char **header)
+{
+ /* Remove header if it isn't the first Content-Type header */
+ if ((csp->content_type & CT_DECLARED))
+ {
+ /*
+ * Another, slightly slower, way to see if
+ * we already parsed another Content-Type header.
+ */
+ assert(NULL != get_header_value(csp->headers, "Content-Type:"));
+
+ log_error(LOG_LEVEL_ERROR,
+ "Multiple Content-Type headers. Removing and ignoring: \'%s\'",
+ *header);
+ freez(*header);
+
+ return JB_ERR_OK;
+ }
+
+ /*
+ * Signal that the Content-Type has been set.
+ */
+ csp->content_type |= CT_DECLARED;
+
+ if (!(csp->content_type & CT_TABOO))
+ {
+ /*
+ * XXX: The assumption that text/plain is a sign of
+ * binary data seems to be somewhat unreasonable nowadays
+ * and should be dropped after 3.0.8 is out.
+ */
+ if ((strstr(*header, "text/") && !strstr(*header, "plain"))
+ || strstr(*header, "xml")
+ || strstr(*header, "application/x-javascript"))
+ {
+ csp->content_type |= CT_TEXT;
+ }
+ else if (strstr(*header, "image/gif"))
+ {
+ csp->content_type |= CT_GIF;
+ }
+ }
+
+ /*
+ * Are we messing with the content type?
+ */
+ if (csp->action->flags & ACTION_CONTENT_TYPE_OVERWRITE)
+ {
+ /*
+ * Make sure the user doesn't accidently
+ * change the content type of binary documents.
+ */
+ if ((csp->content_type & CT_TEXT) || (csp->action->flags & ACTION_FORCE_TEXT_MODE))
+ {
+ freez(*header);
+ *header = strdup("Content-Type: ");
+ string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
+
+ if (header == NULL)
+ {
+ log_error(LOG_LEVEL_HEADER, "Insufficient memory to replace Content-Type!");
+ return JB_ERR_MEMORY;
+ }
+ log_error(LOG_LEVEL_HEADER, "Modified: %s!", *header);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_HEADER, "%s not replaced. "
+ "It doesn't look like a content type that should be filtered. "
+ "Enable force-text-mode if you know what you're doing.", *header);
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : server_transfer_coding
+ *
+ * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
+ * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
+ * - Remove header if body was chunked but has been
+ * de-chunked for filtering.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_transfer_coding(struct client_state *csp, char **header)
+{
+ /*
+ * Turn off pcrs and gif filtering if body compressed
+ */
+ if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
+ {
+#ifdef FEATURE_ZLIB
+ /*
+ * XXX: Added to test if we could use CT_GZIP and CT_DEFLATE here.
+ */
+ log_error(LOG_LEVEL_INFO, "Marking content type for %s as CT_TABOO because of %s.",
+ csp->http->cmd, *header);
+#endif /* def FEATURE_ZLIB */
+ csp->content_type = CT_TABOO;
+ }
+
+ /*
+ * Raise flag if body chunked
+ */
+ if (strstr(*header, "chunked"))
+ {
+ csp->flags |= CSP_FLAG_CHUNKED;
+
+ /*
+ * If the body was modified, it has been de-chunked first
+ * and the header must be removed.
+ *
+ * FIXME: If there is more than one transfer encoding,
+ * only the "chunked" part should be removed here.
+ */
+ if (csp->flags & CSP_FLAG_MODIFIED)
+ {
+ log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
+ freez(*header);
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : server_content_encoding
+ *
+ * Description : This function is run twice for each request,
+ * unless FEATURE_ZLIB and filtering are disabled.
+ *
+ * The first run is used to check if the content
+ * is compressed, if FEATURE_ZLIB is disabled
+ * filtering is then disabled as well, if FEATURE_ZLIB
+ * is enabled the content is marked for decompression.
+ *
+ * The second run is used to remove the Content-Encoding
+ * header if the decompression was successful.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_content_encoding(struct client_state *csp, char **header)
+{
+#ifdef FEATURE_ZLIB
+ if ((csp->flags & CSP_FLAG_MODIFIED)
+ && (csp->content_type & (CT_GZIP | CT_DEFLATE)))
+ {
+ /*
+ * We successfully decompressed the content,
+ * and have to clean the header now, so the
+ * client no longer expects compressed data..
+ *
+ * XXX: There is a difference between cleaning
+ * and removing it completely.
+ */
+ log_error(LOG_LEVEL_HEADER, "Crunching: %s", *header);
+ freez(*header);
+ }
+ else if (strstr(*header, "gzip"))
+ {
+ /* Mark for gzip decompression */
+ csp->content_type |= CT_GZIP;
+ }
+ else if (strstr(*header, "deflate"))
+ {
+ /* Mark for zlib decompression */
+ csp->content_type |= CT_DEFLATE;
+ }
+ else if (strstr(*header, "compress"))
+ {
+ /*
+ * We can't decompress this; therefore we can't filter
+ * it either.
+ */
+ csp->content_type |= CT_TABOO;
+ }
+#else /* !defined(FEATURE_ZLIB) */
+ if (strstr(*header, "gzip") || strstr(*header, "compress") || strstr(*header, "deflate"))
+ {
+ /*
+ * Body is compressed, turn off pcrs and gif filtering.
+ */
+ csp->content_type |= CT_TABOO;
+
+ /*
+ * Log a warning if the user expects the content to be filtered.
+ */
+ if ((csp->rlist != NULL) &&
+ (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
+ {
+ log_error(LOG_LEVEL_INFO,
+ "Compressed content detected, content filtering disabled. "
+ "Consider recompiling Privoxy with zlib support or "
+ "enable the prevent-compression action.");
+ }
+ }
+#endif /* defined(FEATURE_ZLIB) */
+
+ return JB_ERR_OK;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : server_adjust_content_length
+ *
+ * Description : Adjust Content-Length header if we modified
+ * the body.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_adjust_content_length(struct client_state *csp, char **header)
+{
+ /* Regenerate header if the content was modified. */
+ if (csp->flags & CSP_FLAG_MODIFIED)
+ {
+ const size_t header_length = 50;
+ freez(*header);
+ *header = malloc(header_length);
+ if (*header == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+ create_content_length_header(csp->content_length, *header, header_length);
+ log_error(LOG_LEVEL_HEADER,
+ "Adjusted Content-Length to %llu", csp->content_length);
+ }
+
+ return JB_ERR_OK;
+}
+
+
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+/*********************************************************************
+ *
+ * Function : server_save_content_length
+ *
+ * Description : Save the Content-Length sent by the server.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_save_content_length(struct client_state *csp, char **header)
+{
+ unsigned long long content_length = 0;
+
+ assert(*(*header+14) == ':');
+
+ if (JB_ERR_OK != get_content_length(*header, &content_length))
+ {
+ log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
+ freez(*header);
+ }
+ else
+ {
+ csp->expected_content_length = content_length;
+ csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
+ csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
+ }
+
+ return JB_ERR_OK;
+}
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
+
+/*********************************************************************
+ *
+ * Function : server_content_md5
+ *
+ * Description : Crumble any Content-MD5 headers if the document was
+ * modified. FIXME: Should we re-compute instead?
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_content_md5(struct client_state *csp, char **header)
+{
+ if (csp->flags & CSP_FLAG_MODIFIED)
+ {
+ log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
+ freez(*header);
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : server_content_disposition
+ *
+ * Description : If enabled, blocks or modifies the "Content-Disposition" header.
+ * Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_content_disposition(struct client_state *csp, char **header)
+{
+ const char *newval;
+
+ /*
+ * Are we messing with the Content-Disposition header?
+ */
+ if ((csp->action->flags & ACTION_HIDE_CONTENT_DISPOSITION) == 0)
+ {
+ /* Me tinks not */
+ return JB_ERR_OK;
+ }
+
+ newval = csp->action->string[ACTION_STRING_CONTENT_DISPOSITION];
+
+ if ((newval == NULL) || (0 == strcmpic(newval, "block")))
+ {
+ /*
+ * Blocking content-disposition header
+ */
+ log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
+ freez(*header);
+ return JB_ERR_OK;
+ }
+ else
+ {
+ /*
+ * Replacing Content-Disposition header
+ */
+ freez(*header);
+ *header = strdup("Content-Disposition: ");
+ string_append(header, newval);
+
+ if (*header != NULL)
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Content-Disposition header crunched and replaced with: %s", *header);
+ }
+ }
+ return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : server_last_modified
+ *
+ * Description : Changes Last-Modified header to the actual date
+ * to help hide-if-modified-since.
+ * Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err server_last_modified(struct client_state *csp, char **header)
+{
+ const char *newval;
+ char buf[BUFFER_SIZE];
+
+ char newheader[50];
+#ifdef HAVE_GMTIME_R
+ struct tm gmt;
+#endif
+ struct tm *timeptr = NULL;
+ time_t now, last_modified;
+ long int days, hours, minutes, seconds;
+
+ /*
+ * Are we messing with the Last-Modified header?
+ */
+ if ((csp->action->flags & ACTION_OVERWRITE_LAST_MODIFIED) == 0)
+ {
+ /*Nope*/
+ return JB_ERR_OK;
+ }
+
+ newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
+
+ if (0 == strcmpic(newval, "block") )
+ {
+ /*
+ * Blocking Last-Modified header. Useless but why not.
+ */
+ log_error(LOG_LEVEL_HEADER, "Crunching %s!", *header);
+ freez(*header);
+ return JB_ERR_OK;
+ }
+ else if (0 == strcmpic(newval, "reset-to-request-time"))
+ {
+ /*
+ * Setting Last-Modified Header to now.
+ */
+ get_http_time(0, buf, sizeof(buf));
+ freez(*header);
+ *header = strdup("Last-Modified: ");
+ string_append(header, buf);
+
+ if (*header == NULL)
+ {
+ log_error(LOG_LEVEL_HEADER, "Insufficient memory. Last-Modified header got lost, boohoo.");
+ }
+ else
+ {
+ log_error(LOG_LEVEL_HEADER, "Reset to present time: %s", *header);
+ }
+ }
+ else if (0 == strcmpic(newval, "randomize"))
+ {
+ const char *header_time = *header + sizeof("Last-Modified:");
+
+ log_error(LOG_LEVEL_HEADER, "Randomizing: %s", *header);
+ now = time(NULL);
+#ifdef HAVE_GMTIME_R
+ gmtime_r(&now, &gmt);
+#elif defined(MUTEX_LOCKS_AVAILABLE)
+ privoxy_mutex_lock(&gmtime_mutex);
+ gmtime(&now);
+ privoxy_mutex_unlock(&gmtime_mutex);
+#else
+ gmtime(&now);
+#endif
+ if (JB_ERR_OK != parse_header_time(header_time, &last_modified))
+ {
+ log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
+ freez(*header);
+ }
+ else
+ {
+ long int rtime = (long int)difftime(now, last_modified);
+ if (rtime)
+ {
+ const int negative_delta = (rtime < 0);
+
+ if (negative_delta)
+ {
+ rtime *= -1;
+ log_error(LOG_LEVEL_HEADER, "Server time in the future.");
+ }
+ rtime = pick_from_range(rtime);
+ if (negative_delta)
+ {
+ rtime *= -1;
+ }
+ last_modified += rtime;
+#ifdef HAVE_GMTIME_R
+ timeptr = gmtime_r(&last_modified, &gmt);
+#elif defined(MUTEX_LOCKS_AVAILABLE)
+ privoxy_mutex_lock(&gmtime_mutex);
+ timeptr = gmtime(&last_modified);
+ privoxy_mutex_unlock(&gmtime_mutex);
+#else
+ timeptr = gmtime(&last_modified);
+#endif
+ if ((NULL == timeptr) || !strftime(newheader,
+ sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Randomizing '%s' failed. Crunching the header without replacement.",
+ *header);
+ freez(*header);
+ return JB_ERR_OK;
+ }
+
+ freez(*header);
+ *header = strdup("Last-Modified: ");
+ string_append(header, newheader);
+
+ if (*header == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "Insufficient memory, header crunched without replacement.");
+ return JB_ERR_MEMORY;
+ }
+
+ days = rtime / (3600 * 24);
+ hours = rtime / 3600 % 24;
+ minutes = rtime / 60 % 60;
+ seconds = rtime % 60;
+
+ log_error(LOG_LEVEL_HEADER,
+ "Randomized: %s (added %d da%s %d hou%s %d minut%s %d second%s",
+ *header, days, (days == 1) ? "y" : "ys", hours, (hours == 1) ? "r" : "rs",
+ minutes, (minutes == 1) ? "e" : "es", seconds, (seconds == 1) ? ")" : "s)");
+ }
+ else
+ {
+ log_error(LOG_LEVEL_HEADER, "Randomized ... or not. No time difference to work with.");
+ }
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_accept_encoding
+ *
+ * Description : Rewrite the client's Accept-Encoding header so that
+ * if doesn't allow compression, if the action applies.
+ * Note: For HTTP/1.0 the absence of the header is enough.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_accept_encoding(struct client_state *csp, char **header)
+{
+ if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
+ {
+ log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
+
+ freez(*header);
+
+ /* Temporarily disable the correct behaviour to
+ * work around a PHP bug.
+ *
+ * if (!strcmpic(csp->http->ver, "HTTP/1.1"))
+ * {
+ * *header = strdup("Accept-Encoding: identity;q=1.0, *;q=0");
+ * if (*header == NULL)
+ * {
+ * return JB_ERR_MEMORY;
+ * }
+ * }
+ *
+ */
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_te
+ *
+ * Description : Rewrite the client's TE header so that
+ * if doesn't allow compression, if the action applies.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_te(struct client_state *csp, char **header)
+{
+ if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
+ {
+ freez(*header);
+ log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_referrer
+ *
+ * Description : Handle the "referer" config setting properly.
+ * Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_referrer(struct client_state *csp, char **header)
+{
+ const char *parameter;
+ /* booleans for parameters we have to check multiple times */
+ int parameter_conditional_block;
+ int parameter_conditional_forge;
+
+#ifdef FEATURE_FORCE_LOAD
+ /*
+ * Since the referrer can include the prefix even
+ * if the request itself is non-forced, we must
+ * clean it unconditionally.
+ *
+ * XXX: strclean is too broad
+ */
+ strclean(*header, FORCE_PREFIX);
+#endif /* def FEATURE_FORCE_LOAD */
+
+ if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
+ {
+ /* Nothing left to do */
+ return JB_ERR_OK;
+ }
+
+ parameter = csp->action->string[ACTION_STRING_REFERER];
+ assert(parameter != NULL);
+ parameter_conditional_block = (0 == strcmpic(parameter, "conditional-block"));
+ parameter_conditional_forge = (0 == strcmpic(parameter, "conditional-forge"));
+
+ if (!parameter_conditional_block && !parameter_conditional_forge)
+ {
+ /*
+ * As conditional-block and conditional-forge are the only
+ * parameters that rely on the original referrer, we can
+ * remove it now for all the others.
+ */
+ freez(*header);
+ }
+
+ if (0 == strcmpic(parameter, "block"))
+ {
+ log_error(LOG_LEVEL_HEADER, "Referer crunched!");
+ return JB_ERR_OK;
+ }
+ else if (parameter_conditional_block || parameter_conditional_forge)
+ {
+ return handle_conditional_hide_referrer_parameter(header,
+ csp->http->hostport, parameter_conditional_block);
+ }
+ else if (0 == strcmpic(parameter, "forge"))
+ {
+ return create_forged_referrer(header, csp->http->hostport);
+ }
+ else
+ {
+ /* interpret parameter as user-supplied referer to fake */
+ return create_fake_referrer(header, parameter);
+ }
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_accept_language
+ *
+ * Description : Handle the "Accept-Language" config setting properly.
+ * Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_accept_language(struct client_state *csp, char **header)
+{
+ const char *newval;
+
+ /*
+ * Are we messing with the Accept-Language?
+ */
+ if ((csp->action->flags & ACTION_HIDE_ACCEPT_LANGUAGE) == 0)
+ {
+ /*I don't think so*/
+ return JB_ERR_OK;
+ }
+
+ newval = csp->action->string[ACTION_STRING_LANGUAGE];
+
+ if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
+ {
+ /*
+ * Blocking Accept-Language header
+ */
+ log_error(LOG_LEVEL_HEADER, "Crunching Accept-Language!");
+ freez(*header);
+ return JB_ERR_OK;
+ }
+ else
+ {
+ /*
+ * Replacing Accept-Language header
+ */
+ freez(*header);
+ *header = strdup("Accept-Language: ");
+ string_append(header, newval);
+
+ if (*header == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Insufficient memory. Accept-Language header crunched without replacement.");
+ }
+ else
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Accept-Language header crunched and replaced with: %s", *header);
+ }
+ }
+ return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : crunch_client_header
+ *
+ * Description : Crunch client header if it matches a string supplied by the
+ * user. Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success and always succeeds
+ *
+ *********************************************************************/
+static jb_err crunch_client_header(struct client_state *csp, char **header)
+{
+ const char *crunch_pattern;
+
+ /* Do we feel like crunching? */
+ if ((csp->action->flags & ACTION_CRUNCH_CLIENT_HEADER))
+ {
+ crunch_pattern = csp->action->string[ACTION_STRING_CLIENT_HEADER];
+
+ /* Is the current header the lucky one? */
+ if (strstr(*header, crunch_pattern))
+ {
+ log_error(LOG_LEVEL_HEADER, "Crunching client header: %s (contains: %s)", *header, crunch_pattern);
+ freez(*header);
+ }
+ }
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_uagent
+ *
+ * Description : Handle the "user-agent" config setting properly
+ * and remember its original value to enable browser
+ * bug workarounds. Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_uagent(struct client_state *csp, char **header)
+{
+ const char *newval;
+
+ if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
+ {
+ return JB_ERR_OK;
+ }
+
+ newval = csp->action->string[ACTION_STRING_USER_AGENT];
+ if (newval == NULL)
+ {
+ return JB_ERR_OK;
+ }
+
+ freez(*header);
+ *header = strdup("User-Agent: ");
+ string_append(header, newval);
+
+ log_error(LOG_LEVEL_HEADER, "Modified: %s", *header);
+
+ return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_ua
+ *
+ * Description : Handle "ua-" headers properly. Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_ua(struct client_state *csp, char **header)
+{
+ if ((csp->action->flags & ACTION_HIDE_USER_AGENT) != 0)
+ {
+ log_error(LOG_LEVEL_HEADER, "crunched User-Agent!");
+ freez(*header);
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_from
+ *
+ * Description : Handle the "from" config setting properly.
+ * Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_from(struct client_state *csp, char **header)
+{
+ const char *newval;
+
+ if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
+ {
+ return JB_ERR_OK;
+ }
+
+ freez(*header);
+
+ newval = csp->action->string[ACTION_STRING_FROM];
+
+ /*
+ * Are we blocking the e-mail address?
+ */
+ if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
+ {
+ log_error(LOG_LEVEL_HEADER, "crunched From!");
+ return JB_ERR_OK;
+ }
+
+ log_error(LOG_LEVEL_HEADER, " modified");
+
+ *header = strdup("From: ");
+ string_append(header, newval);
+
+ return (*header == NULL) ? JB_ERR_MEMORY : JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_send_cookie
+ *
+ * Description : Crunches the "cookie" header if necessary.
+ * Called from `sed'.
+ *
+ * XXX: Stupid name, doesn't send squat.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_send_cookie(struct client_state *csp, char **header)
+{
+ if (csp->action->flags & ACTION_NO_COOKIE_READ)
+ {
+ log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie: %s", *header);
+ freez(*header);
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_x_forwarded
+ *
+ * Description : Handle the "x-forwarded-for" config setting properly,
+ * also used in the add_client_headers list. Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+jb_err client_x_forwarded(struct client_state *csp, char **header)
+{
+ if (0 != (csp->action->flags & ACTION_CHANGE_X_FORWARDED_FOR))
+ {
+ const char *parameter = csp->action->string[ACTION_STRING_CHANGE_X_FORWARDED_FOR];
+
+ if (0 == strcmpic(parameter, "block"))
+ {
+ freez(*header);
+ log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
+ }
+ else if (0 == strcmpic(parameter, "add"))
+ {
+ string_append(header, ", ");
+ string_append(header, csp->ip_addr_str);
+
+ if (*header == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+ log_error(LOG_LEVEL_HEADER,
+ "Appended client IP address to %s", *header);
+ csp->flags |= CSP_FLAG_X_FORWARDED_FOR_APPENDED;
+ }
+ else
+ {
+ log_error(LOG_LEVEL_FATAL,
+ "Invalid change-x-forwarded-for parameter: '%s'", parameter);
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_max_forwards
+ *
+ * Description : If the HTTP method is OPTIONS or TRACE, subtract one
+ * from the value of the Max-Forwards header field.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_max_forwards(struct client_state *csp, char **header)
+{
+ int max_forwards;
+
+ if ((0 == strcmpic(csp->http->gpc, "trace")) ||
+ (0 == strcmpic(csp->http->gpc, "options")))
+ {
+ assert(*(*header+12) == ':');
+ if (1 == sscanf(*header+12, ": %d", &max_forwards))
+ {
+ if (max_forwards > 0)
+ {
+ snprintf(*header, strlen(*header)+1, "Max-Forwards: %d", --max_forwards);
+ log_error(LOG_LEVEL_HEADER,
+ "Max-Forwards value for %s request reduced to %d.",
+ csp->http->gpc, max_forwards);
+ }
+ else if (max_forwards < 0)
+ {
+ log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
+ freez(*header);
+ }
+ }
+ else
+ {
+ log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
+ freez(*header);
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_host
+ *
+ * Description : If the request URI did not contain host and
+ * port information, parse and evaluate the Host
+ * header field.
+ *
+ * Also, kill ill-formed HOST: headers as sent by
+ * Apple's iTunes software when used with a proxy.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_host(struct client_state *csp, char **header)
+{
+ char *p, *q;
+
+ /*
+ * If the header field name is all upper-case, chances are that it's
+ * an ill-formed one from iTunes. BTW, killing innocent headers here is
+ * not a problem -- they are regenerated later.
+ */
+ if ((*header)[1] == 'O')
+ {
+ log_error(LOG_LEVEL_HEADER, "Killed all-caps Host header line: %s", *header);
+ freez(*header);
+ return JB_ERR_OK;
+ }
+
+ if (!csp->http->hostport || (*csp->http->hostport == '*') ||
+ *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
+ {
+
+ if (NULL == (p = strdup((*header)+6)))
+ {
+ return JB_ERR_MEMORY;
+ }
+ chomp(p);
+ if (NULL == (q = strdup(p)))
+ {
+ freez(p);
+ return JB_ERR_MEMORY;
+ }
+
+ freez(csp->http->hostport);
+ csp->http->hostport = p;
+ freez(csp->http->host);
+ csp->http->host = q;
+ q = strchr(csp->http->host, ':');
+ if (q != NULL)
+ {
+ /* Terminate hostname and evaluate port string */
+ *q++ = '\0';
+ csp->http->port = atoi(q);
+ }
+ else
+ {
+ csp->http->port = csp->http->ssl ? 443 : 80;
+ }
+
+ log_error(LOG_LEVEL_HEADER, "New host and port from Host field: %s = %s:%d",
+ csp->http->hostport, csp->http->host, csp->http->port);
+ }
+
+ /* Signal client_host_adder() to return right away */
+ csp->flags |= CSP_FLAG_HOST_HEADER_IS_SET;
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_if_modified_since
+ *
+ * Description : Remove or modify the If-Modified-Since header.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_if_modified_since(struct client_state *csp, char **header)
+{
+ char newheader[50];
+#ifdef HAVE_GMTIME_R
+ struct tm gmt;
+#endif
+ struct tm *timeptr = NULL;
+ time_t tm = 0;
+ const char *newval;
+ long int hours, minutes, seconds;
+ char * endptr;
+
+ if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
+ {
+ /*
+ * The client got an error message because of a temporary problem,
+ * the problem is gone and the client now tries to revalidate our
+ * error message on the real server. The revalidation would always
+ * end with the transmission of the whole document and there is
+ * no need to expose the bogus If-Modified-Since header.
+ */
+ log_error(LOG_LEVEL_HEADER, "Crunching useless If-Modified-Since header.");
+ freez(*header);
+ }
+ else if (csp->action->flags & ACTION_HIDE_IF_MODIFIED_SINCE)
+ {
+ newval = csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE];
+
+ if ((0 == strcmpic(newval, "block")))
+ {
+ log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
+ freez(*header);
+ }
+ else /* add random value */
+ {
+ const char *header_time = *header + sizeof("If-Modified-Since:");
+
+ if (JB_ERR_OK != parse_header_time(header_time, &tm))
+ {
+ log_error(LOG_LEVEL_HEADER, "Couldn't parse: %s in %s (crunching!)", header_time, *header);
+ freez(*header);
+ }
+ else
+ {
+ long int rtime = strtol(newval, &endptr, 0);
+ const int negative_range = (rtime < 0);
+
+ if (rtime)
+ {
+ log_error(LOG_LEVEL_HEADER, "Randomizing: %s (random range: %d minut%s)",
+ *header, rtime, (rtime == 1 || rtime == -1) ? "e": "es");
+ if (negative_range)
+ {
+ rtime *= -1;
+ }
+ rtime *= 60;
+ rtime = pick_from_range(rtime);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_ERROR, "Random range is 0. Assuming time transformation test.",
+ *header);
+ }
+ tm += rtime * (negative_range ? -1 : 1);
+#ifdef HAVE_GMTIME_R
+ timeptr = gmtime_r(&tm, &gmt);
+#elif defined(MUTEX_LOCKS_AVAILABLE)
+ privoxy_mutex_lock(&gmtime_mutex);
+ timeptr = gmtime(&tm);
+ privoxy_mutex_unlock(&gmtime_mutex);
+#else
+ timeptr = gmtime(&tm);
+#endif
+ if ((NULL == timeptr) || !strftime(newheader,
+ sizeof(newheader), "%a, %d %b %Y %H:%M:%S GMT", timeptr))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Randomizing '%s' failed. Crunching the header without replacement.",
+ *header);
+ freez(*header);
+ return JB_ERR_OK;
+ }
+
+ freez(*header);
+ *header = strdup("If-Modified-Since: ");
+ string_append(header, newheader);
+
+ if (*header == NULL)
+ {
+ log_error(LOG_LEVEL_HEADER, "Insufficient memory, header crunched without replacement.");
+ return JB_ERR_MEMORY;
+ }
+
+ hours = rtime / 3600;
+ minutes = rtime / 60 % 60;
+ seconds = rtime % 60;
+
+ log_error(LOG_LEVEL_HEADER,
+ "Randomized: %s (%s %d hou%s %d minut%s %d second%s",
+ *header, (negative_range) ? "subtracted" : "added", hours,
+ (hours == 1) ? "r" : "rs", minutes, (minutes == 1) ? "e" : "es",
+ seconds, (seconds == 1) ? ")" : "s)");
+ }
+ }
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_if_none_match
+ *
+ * Description : Remove the If-None-Match header.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success, or
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+static jb_err client_if_none_match(struct client_state *csp, char **header)
+{
+ if (csp->action->flags & ACTION_CRUNCH_IF_NONE_MATCH)
+ {
+ log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
+ freez(*header);
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_x_filter
+ *
+ * Description : Disables filtering if the client set "X-Filter: No".
+ * Called from `sed'.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK on success
+ *
+ *********************************************************************/
+jb_err client_x_filter(struct client_state *csp, char **header)
+{
+ if ( 0 == strcmpic(*header, "X-Filter: No"))
+ {
+ if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
+ {
+ log_error(LOG_LEVEL_INFO, "Ignored the client's request to fetch without filtering.");
+ }
+ else
+ {
+ if (csp->action->flags & ACTION_FORCE_TEXT_MODE)
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "force-text-mode overruled the client's request to fetch without filtering!");
+ }
+ else
+ {
+ csp->content_type = CT_TABOO; /* XXX: This hack shouldn't be necessary */
+ csp->flags |= CSP_FLAG_NO_FILTERING;
+ log_error(LOG_LEVEL_HEADER, "Accepted the client's request to fetch without filtering.");
+ }
+ log_error(LOG_LEVEL_HEADER, "Crunching %s", *header);
+ freez(*header);
+ }
+ }
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : client_range
+ *
+ * Description : Removes Range, Request-Range and If-Range headers if
+ * content filtering is enabled. If the client's version
+ * of the document has been altered by Privoxy, the server
+ * could interpret the range differently than the client
+ * intended in which case the user could end up with
+ * corrupted content.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : header = On input, pointer to header to modify.
+ * On output, pointer to the modified header, or NULL
+ * to remove the header. This function frees the
+ * original string if necessary.
+ *
+ * Returns : JB_ERR_OK
+ *
+ *********************************************************************/
+static jb_err client_range(struct client_state *csp, char **header)
+{
+ if (content_filters_enabled(csp->action))
+ {
+ log_error(LOG_LEVEL_HEADER, "Content filtering is enabled."
+ " Crunching: \'%s\' to prevent range-mismatch problems.", *header);
+ freez(*header);
+ }
+
+ return JB_ERR_OK;
+}
+
+/* the following functions add headers directly to the header list */