No longer ignore SIGCHLD. Fixes bug #769381
[privoxy.git] / parsers.c
index df2ae59..97d803d 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,7 +1,7 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.55 2002/05/08 16:01:07 oes Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.56.2.6 2003/04/14 21:28:30 oes Exp $";
 /*********************************************************************
  *
- * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
+ * File        :  $Source: /cvsroot/ijbswa/current/Attic/parsers.c,v $
  *
  * Purpose     :  Declares functions to parse/crunch headers and pages.
  *                Functions declared include:
@@ -40,6 +40,35 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.55 2002/05/08 16:01:07 oes Exp $"
  *
  * Revisions   :
  *    $Log: parsers.c,v $
+ *    Revision 1.56.2.6  2003/04/14 21:28:30  oes
+ *    Completing the previous change
+ *
+ *    Revision 1.56.2.5  2003/04/14 12:08:16  oes
+ *    Added temporary workaround for bug in PHP < 4.2.3
+ *
+ *    Revision 1.56.2.4  2003/03/07 03:41:05  david__schmidt
+ *    Wrapping all *_r functions (the non-_r versions of them) with mutex semaphores for OSX.  Hopefully this will take care of all of those pesky crash reports.
+ *
+ *    Revision 1.56.2.3  2002/11/10 04:20:02  hal9
+ *    Fix typo: supressed -> suppressed
+ *
+ *    Revision 1.56.2.2  2002/09/25 14:59:53  oes
+ *    Improved cookie logging
+ *
+ *    Revision 1.56.2.1  2002/09/25 14:52:45  oes
+ *    Added basic support for OPTIONS and TRACE HTTP methods:
+ *     - New parser function client_max_forwards which decrements
+ *       the Max-Forwards HTTP header field of OPTIONS and TRACE
+ *       requests by one before forwarding
+ *     - New parser function client_host which extracts the host
+ *       and port information from the HTTP header field if the
+ *       request URI was not absolute
+ *     - Don't crumble and re-add the Host: header, but only generate
+ *       and append if missing
+ *
+ *    Revision 1.56  2002/05/12 15:34:22  jongfoster
+ *    Fixing typo in a comment
+ *
  *    Revision 1.55  2002/05/08 16:01:07  oes
  *    Optimized add_to_iob:
  *     - Use realloc instead of malloc(), memcpy(), free()
@@ -399,6 +428,11 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.55 2002/05/08 16:01:07 oes Exp $"
 #include <unistd.h>
 #endif
 
+#ifdef OSX_DARWIN
+#include <pthread.h>
+#include "jcc.h"
+/* jcc.h is for mutex semapores only */
+#endif /* def OSX_DARWIN */
 #include "project.h"
 #include "list.h"
 #include "parsers.h"
@@ -433,12 +467,13 @@ const struct parsers client_patterns[] = {
    { "cookie:",                  7,    client_send_cookie },
    { "x-forwarded-for:",         16,   client_x_forwarded },
    { "Accept-Encoding:",         16,   client_accept_encoding },
-   { "TE:",                      3,    client_te },
-   { "Host:",                     5,   crumble },
+   { "TE:",                       3,   client_te },
+   { "Host:",                     5,   client_host },
 /* { "if-modified-since:",       18,   crumble }, */
    { "Keep-Alive:",              11,   crumble },
    { "connection:",              11,   crumble },
    { "proxy-connection:",        17,   crumble },
+   { "max-forwards:",            13,   client_max_forwards },
    { NULL,                       0,    NULL }
 };
 
@@ -462,7 +497,7 @@ const add_header_func_ptr add_client_headers[] = {
    client_cookie_adder,
    client_x_forwarded_adder,
    client_xtra_adder,
-   client_accept_encoding_adder,
+   /* Temporarily disabled:    client_accept_encoding_adder, */
    connection_close_adder,
    NULL
 };
@@ -994,17 +1029,23 @@ jb_err client_accept_encoding(struct client_state *csp, char **header)
 {
    if ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
    {
-      log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
+      log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress content");
 
       freez(*header);
-      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;
-         }
-      }
+
+      /* 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;
@@ -1034,7 +1075,7 @@ 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, "Supressed offer to compress transfer");
+      log_error(LOG_LEVEL_HEADER, "Suppressed offer to compress transfer");
    }
 
    return JB_ERR_OK;
@@ -1278,7 +1319,7 @@ jb_err client_send_cookie(struct client_state *csp, char **header)
    }
    else
    {
-      log_error(LOG_LEVEL_HEADER, " crunch!");
+      log_error(LOG_LEVEL_HEADER, "Crunched outgoing cookie -- yum!");
    }
 
    /*
@@ -1332,13 +1373,135 @@ jb_err client_x_forwarded(struct client_state *csp, char **header)
    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.
+ *
+ *********************************************************************/
+jb_err client_max_forwards(struct client_state *csp, char **header)
+{
+   unsigned int max_forwards;
+
+   if ((0 == strcmpic(csp->http->gpc, "trace"))
+      || (0 == strcmpic(csp->http->gpc, "options")))
+   {
+      if (1 == sscanf(*header, "Max-Forwards: %u", &max_forwards))
+      {
+         if (max_forwards-- >= 1)
+         {
+            sprintf(*header, "Max-Forwards: %u", max_forwards);
+            log_error(LOG_LEVEL_HEADER, "Max forwards of %s request now %d", csp->http->gpc, max_forwards);
+         }
+         else
+         {
+            log_error(LOG_LEVEL_ERROR, "Non-intercepted %s request with Max-Forwards zero!", csp->http->gpc);
+         }
+      }
+   }
+
+   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.
+ *
+ *********************************************************************/
+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);
+   }
+
+   return JB_ERR_OK;
+}
+
+
 /* the following functions add headers directly to the header list */
 
 /*********************************************************************
  *
  * Function    :  client_host_adder
  *
- * Description :  (re)adds the host header. Called from `sed'.
+ * Description :  Adds the Host: header field if it is missing.
+ *                Called from `sed'.
  *
  * Parameters  :
  *          1  :  csp = Current client state (buffers, headers, etc...)
@@ -1350,7 +1513,6 @@ jb_err client_x_forwarded(struct client_state *csp, char **header)
 jb_err client_host_adder(struct client_state *csp)
 {
    char *p;
-   char *pos;
    jb_err err;
 
    if ( !csp->http->hostport || !*(csp->http->hostport))
@@ -1358,31 +1520,22 @@ jb_err client_host_adder(struct client_state *csp)
       return JB_ERR_OK;
    }
 
-   p = strdup("Host: ");
    /*
-   ** remove 'user:pass@' from 'proto://user:pass@host'
-   */
-   if ( (pos = strchr( csp->http->hostport, '@')) != NULL )
+    * remove 'user:pass@' from 'proto://user:pass@host'
+    */
+   if ( (p = strchr( csp->http->hostport, '@')) != NULL )
    {
-       string_append(&p, pos+1);
+      p++;
    }
    else
    {
-      string_append(&p, csp->http->hostport);
+      p = csp->http->hostport;
    }
 
-   if (p == NULL)
-   {
-      return JB_ERR_MEMORY;
-   }
-
-   log_error(LOG_LEVEL_HEADER, "addh: %s", p);
-
-   err = enlist(csp->headers, p);
-
-   freez(p);
-
+   log_error(LOG_LEVEL_HEADER, "addh-unique: Host: %s", p);
+   err = enlist_unique_header(csp->headers, "Host", p);
    return err;
+
 }
 
 
@@ -1660,6 +1813,10 @@ jb_err server_set_cookie(struct client_state *csp, char **header)
       time (&now); 
 #ifdef HAVE_LOCALTIME_R
       tm_now = *localtime_r(&now, &tm_now);
+#elif OSX_DARWIN
+      pthread_mutex_lock(&localtime_mutex);
+      tm_now = *localtime (&now); 
+      pthread_mutex_unlock(&localtime_mutex);
 #else
       tm_now = *localtime (&now); 
 #endif
@@ -1672,6 +1829,7 @@ jb_err server_set_cookie(struct client_state *csp, char **header)
 
    if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
    {
+      log_error(LOG_LEVEL_HEADER, "Crunched incoming cookie -- yum!");
       return crumble(csp, header);
    }
    else if ((csp->action->flags & ACTION_NO_COOKIE_KEEP) != 0)