Let the CGI editor suggest a forward-override
[privoxy.git] / parsers.c
index 2bcb19d..9b0fd4b 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.109 2007/09/08 14:25:48 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.116 2007/12/01 13:04:22 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -44,6 +44,32 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.109 2007/09/08 14:25:48 fabiankei
  *
  * Revisions   :
  *    $Log: parsers.c,v $
+ *    Revision 1.116  2007/12/01 13:04:22  fabiankeil
+ *    Fix a crash on mingw32 with some Last Modified times in the future.
+ *
+ *    Revision 1.115  2007/11/02 16:52:50  fabiankeil
+ *    Remove a "can't happen" error block which, over
+ *    time, mutated into a "guaranteed to happen" block.
+ *
+ *    Revision 1.114  2007/10/19 16:56:26  fabiankeil
+ *    - Downgrade "Buffer limit reached" message to LOG_LEVEL_INFO.
+ *    - Use shiny new content_filters_enabled() in client_range().
+ *
+ *    Revision 1.113  2007/10/10 17:29:57  fabiankeil
+ *    I forgot about Poland.
+ *
+ *    Revision 1.112  2007/10/09 16:38:40  fabiankeil
+ *    Remove Range and If-Range headers if content filtering is enabled.
+ *
+ *    Revision 1.111  2007/10/04 18:07:00  fabiankeil
+ *    Move ACTION_VANILLA_WAFER handling from jcc's chat() into
+ *    client_cookie_adder() to make sure send-vanilla-wafer can be
+ *    controlled through tags (and thus regression-tested).
+ *
+ *    Revision 1.110  2007/09/29 10:42:37  fabiankeil
+ *    - Remove "scanning headers for" log message again.
+ *    - Some more whitespace fixes.
+ *
  *    Revision 1.109  2007/09/08 14:25:48  fabiankeil
  *    Refactor client_referrer() and add conditional-forge parameter.
  *
@@ -788,6 +814,7 @@ static jb_err client_accept_language    (struct client_state *csp, char **header
 static jb_err client_if_none_match      (struct client_state *csp, char **header);
 static jb_err crunch_client_header      (struct client_state *csp, char **header);
 static jb_err client_x_filter           (struct client_state *csp, char **header);
+static jb_err client_range              (struct client_state *csp, char **header);
 static jb_err server_set_cookie         (struct client_state *csp, char **header);
 static jb_err server_content_type       (struct client_state *csp, char **header);
 static jb_err server_content_length     (struct client_state *csp, char **header);
@@ -802,7 +829,6 @@ static jb_err server_content_disposition(struct client_state *csp, char **header
 static jb_err client_host_adder       (struct client_state *csp);
 static jb_err client_cookie_adder     (struct client_state *csp);
 static jb_err client_xtra_adder       (struct client_state *csp);
-static jb_err client_x_forwarded_adder(struct client_state *csp);
 static jb_err connection_close_adder  (struct client_state *csp); 
 
 static jb_err create_forged_referrer(char **header, const char *hostport);
@@ -827,6 +853,8 @@ const struct parsers client_patterns[] = {
    { "max-forwards:",            13,   client_max_forwards },
    { "Accept-Language:",         16,   client_accept_language },
    { "if-none-match:",           14,   client_if_none_match },
+   { "Range:",                    6,   client_range },
+   { "If-Range:",                 9,   client_range },
    { "X-Filter:",                 9,   client_x_filter },
    { "*",                         0,   crunch_client_header },
    { "*",                         0,   filter_header },
@@ -861,7 +889,6 @@ const struct parsers server_patterns_light[] = {
 const add_header_func_ptr add_client_headers[] = {
    client_host_adder,
    client_cookie_adder,
-   client_x_forwarded_adder,
    client_xtra_adder,
    /* Temporarily disabled:    client_accept_encoding_adder, */
    connection_close_adder,
@@ -873,6 +900,16 @@ const add_header_func_ptr add_server_headers[] = {
    NULL
 };
 
+/* The vanilla wafer. */
+static const char VANILLA_WAFER[] =
+   "NOTICE=TO_WHOM_IT_MAY_CONCERN_"
+   "Do_not_send_me_any_copyrighted_information_other_than_the_"
+   "document_that_I_am_requesting_or_any_of_its_necessary_components._"
+   "In_particular_do_not_send_me_any_cookies_that_"
+   "are_subject_to_a_claim_of_copyright_by_anybody._"
+   "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_"
+   "(copyright_or_otherwise)_applying_to_any_cookie._";
+
 /*********************************************************************
  *
  * Function    :  flush_socket
@@ -945,7 +982,7 @@ jb_err add_to_iob(struct client_state *csp, char *buf, int n)
     */
    if (need > csp->config->buffer_limit)
    {
-      log_error(LOG_LEVEL_ERROR, "Buffer limit reached while extending the buffer (iob)");
+      log_error(LOG_LEVEL_INFO, "Buffer limit reached while extending the buffer (iob)");
       return JB_ERR_MEMORY;
    }
 
@@ -2563,7 +2600,16 @@ static jb_err server_last_modified(struct client_state *csp, char **header)
          rtime = (long int)difftime(now, last_modified);
          if (rtime)
          {
+            int negative = 0;
+
+            if (rtime < 0)
+            {
+               rtime *= -1; 
+               negative = 1;
+               log_error(LOG_LEVEL_HEADER, "Server time in the future.");
+            }
             rtime = pick_from_range(rtime);
+            if (negative) rtime *= -1;
             last_modified += rtime;
 #ifdef HAVE_GMTIME_R
             timeptr = gmtime_r(&last_modified, &gmt);
@@ -3040,19 +3086,7 @@ static jb_err client_send_cookie(struct client_state *csp, char **header)
  *********************************************************************/
 jb_err client_x_forwarded(struct client_state *csp, char **header)
 {
-   if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
-   {
-      /* Save it so we can re-add it later */
-      freez(csp->x_forwarded);
-      csp->x_forwarded = *header;
-
-      /*
-       * Always set *header = NULL, since this information
-       * will be sent at the end of the header.
-       */
-      *header = NULL;
-   }
-   else
+   if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
    {
       freez(*header);
       log_error(LOG_LEVEL_HEADER, "crunched x-forwarded-for!");
@@ -3101,17 +3135,6 @@ static jb_err client_max_forwards(struct client_state *csp, char **header)
             log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
             freez(*header);
          }
-         else
-         {
-            /*
-             * Not supposed to be reached. direct_response() which
-             * was already called earlier in chat() should have
-             * intercepted the request.
-             */
-            log_error(LOG_LEVEL_ERROR,
-               "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
-            assert(max_forwards != 0);
-         }
       }
       else
       {
@@ -3399,6 +3422,40 @@ jb_err client_x_filter(struct client_state *csp, char **header)
    return JB_ERR_OK; 
 }
 
+
+/*********************************************************************
+ *
+ * Function    :  client_range
+ *
+ * Description :  Removes 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))
+   {
+      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 */
 
 /*********************************************************************
@@ -3471,9 +3528,21 @@ jb_err client_cookie_adder(struct client_state *csp)
 {
    char *tmp;
    struct list_entry *wafer;
-   struct list_entry *wafer_list = csp->action->multi[ACTION_MULTI_WAFER]->first;
+   struct list_entry *wafer_list;
    jb_err err;
 
+   /*
+    * If the user has not supplied any wafers, and the user has not
+    * told us to suppress the vanilla wafer, then send the vanilla wafer.
+    */
+   if ((0 != (csp->action->flags & ACTION_VANILLA_WAFER))
+      && list_is_empty(csp->action->multi[ACTION_MULTI_WAFER]))
+   {
+      enlist(csp->action->multi[ACTION_MULTI_WAFER], VANILLA_WAFER);
+   }
+
+   wafer_list = csp->action->multi[ACTION_MULTI_WAFER]->first;
+
    if (NULL == wafer_list)
    {
       /* Nothing to do */
@@ -3568,53 +3637,6 @@ static jb_err client_xtra_adder(struct client_state *csp)
 }
 
 
-/*********************************************************************
- *
- * Function    :  client_x_forwarded_adder
- *
- * Description :  Used in the add_client_headers list.  Called from `sed'.
- *
- * 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_x_forwarded_adder(struct client_state *csp)
-{
-   char *p = NULL;
-   jb_err err;
-
-   if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
-   {
-      return JB_ERR_OK;
-   }
-
-   if (csp->x_forwarded)
-   {
-      p = strdup(csp->x_forwarded);
-      string_append(&p, ", ");
-   }
-   else
-   {
-      p = strdup("X-Forwarded-For: ");
-   }
-   string_append(&p, csp->ip_addr_str);
-
-   if (p == NULL)
-   {
-      return JB_ERR_MEMORY;
-   }
-
-   log_error(LOG_LEVEL_HEADER, "addh: %s", p);
-   err = enlist(csp->headers, p);
-   free(p);
-
-   return err;
-}
-
-
 /*********************************************************************
  *
  * Function    :  connection_close_adder