Fix the description of create_forged_referrer()
[privoxy.git] / parsers.c
index 7dbaf02..2e2d64f 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,23 +1,11 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.228 2011/09/04 11:31:17 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.239 2011/12/31 14:49:07 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
  *
  * Purpose     :  Declares functions to parse/crunch headers and pages.
- *                Functions declared include:
- *                   `add_to_iob', `client_cookie_adder', `client_from',
- *                   `client_referrer', `client_send_cookie', `client_ua',
- *                   `client_uagent', `client_x_forwarded',
- *                   `client_x_forwarded_adder', `client_xtra_adder',
- *                   `content_type', `crumble', `destroy_list', `enlist',
- *                   `flush_socket', ``get_header', `sed', `filter_header'
- *                   `server_content_encoding', `server_content_disposition',
- *                   `server_last_modified', `client_accept_language',
- *                   `crunch_client_header', `client_if_modified_since',
- *                   `client_if_none_match', `get_destination_from_headers',
- *                   `parse_header_time', `decompress_iob' and `server_set_cookie'.
- *
- * Copyright   :  Written by and Copyright (C) 2001-2009 the
+ *
+ * Copyright   :  Written by and Copyright (C) 2001-2011 the
  *                Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -252,7 +240,6 @@ static const add_header_func_ptr add_client_headers[] = {
    client_host_adder,
    client_x_forwarded_for_adder,
    client_xtra_adder,
-   /* Temporarily disabled:    client_accept_encoding_adder, */
    client_connection_header_adder,
    NULL
 };
@@ -1850,6 +1837,11 @@ static jb_err client_connection(struct client_state *csp, char **header)
              log_error(LOG_LEVEL_HEADER,
                 "Removing \'%s\' to imply keep-alive.", *header);
              freez(*header);
+             /*
+              * While we imply keep-alive to the server,
+              * we have to remember that the client didn't.
+              */
+             csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
           }
           else
           {
@@ -2468,7 +2460,6 @@ static jb_err server_content_disposition(struct client_state *csp, char **header
 static jb_err server_last_modified(struct client_state *csp, char **header)
 {
    const char *newval;
-   char buf[BUFFER_SIZE];
    time_t last_modified;
    char newheader[50];
 
@@ -2497,6 +2488,7 @@ static jb_err server_last_modified(struct client_state *csp, char **header)
       /*
        * Setting Last-Modified Header to now.
        */
+      char buf[30];
       get_http_time(0, buf, sizeof(buf));
       freez(*header);
       *header = strdup("Last-Modified: ");
@@ -3476,36 +3468,6 @@ static jb_err client_host_adder(struct client_state *csp)
 }
 
 
-#if 0
-/*********************************************************************
- *
- * Function    :  client_accept_encoding_adder
- *
- * Description :  Add an Accept-Encoding header to the client's request
- *                that disables compression if the action applies, and
- *                the header is not already there. Called from `sed'.
- *                Note: For HTTP/1.0, the absence of the header is enough.
- *
- * Parameters  :
- *          1  :  csp = Current client state (buffers, headers, etc...)
- *
- * Returns     :  JB_ERR_OK on success, or
- *                JB_ERR_MEMORY on out-of-memory error.
- *
- *********************************************************************/
-static jb_err client_accept_encoding_adder(struct client_state *csp)
-{
-   if (   ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
-       && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
-   {
-      return enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
-   }
-
-   return JB_ERR_OK;
-}
-#endif
-
-
 /*********************************************************************
  *
  * Function    :  client_xtra_adder
@@ -4008,13 +3970,18 @@ int strclean(char *string, const char *substring)
 static jb_err parse_header_time(const char *header_time, time_t *result)
 {
    struct tm gmt;
-   static const char *time_formats[] = {
+   /*
+    * Checking for two-digit years first in an
+    * attempt to work around GNU libc's strptime()
+    * reporting negative year values when using %Y.
+    */
+   static const char * const time_formats[] = {
+      /* Tue, 02-Jun-37 20:00:00 */
+      "%a, %d-%b-%y %H:%M:%S",
       /* Tue, 02 Jun 2037 20:00:00 */
       "%a, %d %b %Y %H:%M:%S",
       /* Tue, 02-Jun-2037 20:00:00 */
       "%a, %d-%b-%Y %H:%M:%S",
-      /* Tue, 02-Jun-37 20:00:00 */
-      "%a, %d-%b-%y %H:%M:%S",
       /* Tuesday, 02-Jun-2037 20:00:00 */
       "%A, %d-%b-%Y %H:%M:%S",
       /* Tuesday Jun 02 20:00:00 2037 */
@@ -4022,16 +3989,24 @@ static jb_err parse_header_time(const char *header_time, time_t *result)
    };
    unsigned int i;
 
-   /*
-    * Zero out gmt to prevent time zone offsets.
-    * Documented to be required for GNU libc.
-    */
-   memset(&gmt, 0, sizeof(gmt));
-
    for (i = 0; i < SZ(time_formats); i++)
    {
+      /*
+       * Zero out gmt to prevent time zone offsets.
+       * Documented to be required for GNU libc.
+       */
+      memset(&gmt, 0, sizeof(gmt));
+
       if (NULL != strptime(header_time, time_formats[i], &gmt))
       {
+         /* Sanity check for GNU libc. */
+         if (gmt.tm_year < 0)
+         {
+            log_error(LOG_LEVEL_HEADER,
+               "Failed to parse '%s' using '%s'. Moving on.",
+               header_time, time_formats[i]);
+            continue;
+         }
          *result = timegm(&gmt);
          return JB_ERR_OK;
       }
@@ -4130,7 +4105,7 @@ jb_err get_destination_from_headers(const struct list *headers, struct http_requ
  * Function    :  create_forged_referrer
  *
  * Description :  Helper for client_referrer to forge a referer as
- *                'http://[hostname:port/' to fool stupid
+ *                'http://hostname[:port]/' to fool stupid
  *                checks for in-site links
  *
  * Parameters  :