Remove an unreasonable parameter limit in parse_cgi_parameters()
[privoxy.git] / parsers.c
index 4dd2a5f..867e480 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.241 2012/03/04 11:47:54 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.244 2012/03/09 16:23:50 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -1480,14 +1480,14 @@ static jb_err filter_header(struct client_state *csp, char **header)
                for (job = joblist; NULL != job; job = job->next)
                {
                   matches = pcrs_execute(job, *header, size, &newheader, &size);
-                  if ( 0 < matches )
+                  if (0 < matches)
                   {
                      current_hits += matches;
                      log_error(LOG_LEVEL_HEADER, "Transforming \"%s\" to \"%s\"", *header, newheader);
                      freez(*header);
                      *header = newheader;
                   }
-                  else if ( 0 == matches )
+                  else if (0 == matches)
                   {
                      /* Filter doesn't change header */
                      freez(newheader);
@@ -1544,8 +1544,7 @@ static jb_err filter_header(struct client_state *csp, char **header)
  *                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.
+ * Returns     :  JB_ERR_OK on success.
  *
  *********************************************************************/
 static jb_err server_connection(struct client_state *csp, char **header)
@@ -1554,7 +1553,7 @@ static jb_err server_connection(struct client_state *csp, char **header)
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
     && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
 #endif
-      )
+     )
    {
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
       if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE))
@@ -1572,11 +1571,7 @@ static jb_err server_connection(struct client_state *csp, char **header)
       {
          char *old_header = *header;
 
-         *header = strdup("Connection: close");
-         if (header == NULL)
-         {
-            return JB_ERR_MEMORY;
-         }
+         *header = strdup_or_die("Connection: close");
          log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
          freez(old_header);
       }
@@ -1806,8 +1801,7 @@ static jb_err client_save_content_length(struct client_state *csp, char **header
  *                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.
+ * Returns     :  JB_ERR_OK on success.
  *
  *********************************************************************/
 static jb_err client_connection(struct client_state *csp, char **header)
@@ -1834,11 +1828,7 @@ static jb_err client_connection(struct client_state *csp, char **header)
           {
              char *old_header = *header;
 
-             *header = strdup("Connection: keep-alive");
-             if (header == NULL)
-             {
-                return JB_ERR_MEMORY;
-             }
+             *header = strdup_or_die("Connection: keep-alive");
              log_error(LOG_LEVEL_HEADER,
                 "Replaced: \'%s\' with \'%s\'", old_header, *header);
              freez(old_header);
@@ -1866,11 +1856,7 @@ static jb_err client_connection(struct client_state *csp, char **header)
    {
       char *old_header = *header;
 
-      *header = strdup(connection_close);
-      if (header == NULL)
-      {
-         return JB_ERR_MEMORY;
-      }
+      *header = strdup_or_die(connection_close);
       log_error(LOG_LEVEL_HEADER,
          "Replaced: \'%s\' with \'%s\'", old_header, *header);
       freez(old_header);
@@ -2024,7 +2010,7 @@ static jb_err server_content_type(struct client_state *csp, char **header)
       if ((csp->content_type & CT_TEXT) || (csp->action->flags & ACTION_FORCE_TEXT_MODE))
       {
          freez(*header);
-         *header = strdup("Content-Type: ");
+         *header = strdup_or_die("Content-Type: ");
          string_append(header, csp->action->string[ACTION_STRING_CONTENT_TYPE]);
 
          if (header == NULL)
@@ -2461,7 +2447,7 @@ static jb_err server_last_modified(struct client_state *csp, char **header)
 
    newval = csp->action->string[ACTION_STRING_LAST_MODIFIED];
 
-   if (0 == strcmpic(newval, "block") )
+   if (0 == strcmpic(newval, "block"))
    {
       /*
        * Blocking Last-Modified header. Useless but why not.
@@ -2757,7 +2743,7 @@ static jb_err client_accept_language(struct client_state *csp, char **header)
 
    newval = csp->action->string[ACTION_STRING_LANGUAGE];
 
-   if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
+   if ((newval == NULL) || (0 == strcmpic(newval, "block")))
    {
       /*
        * Blocking Accept-Language header
@@ -2934,7 +2920,7 @@ static jb_err client_from(struct client_state *csp, char **header)
    /*
     * Are we blocking the e-mail address?
     */
-   if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
+   if ((newval == NULL) || (0 == strcmpic(newval, "block")))
    {
       log_error(LOG_LEVEL_HEADER, "crunched From!");
       return JB_ERR_OK;
@@ -3128,16 +3114,9 @@ static jb_err client_host(struct client_state *csp, char **header)
        *csp->http->hostport == ' ' || *csp->http->hostport == '\0')
    {
 
-      if (NULL == (p = strdup((*header)+6)))
-      {
-         return JB_ERR_MEMORY;
-      }
+      p = strdup_or_die((*header)+6);
       chomp(p);
-      if (NULL == (q = strdup(p)))
-      {
-         freez(p);
-         return JB_ERR_MEMORY;
-      }
+      q = strdup_or_die(p);
 
       freez(csp->http->hostport);
       csp->http->hostport = p;
@@ -3194,7 +3173,7 @@ static jb_err client_if_modified_since(struct client_state *csp, char **header)
    const char *newval;
    char * endptr;
 
-   if ( 0 == strcmpic(*header, "If-Modified-Since: Wed, 08 Jun 1955 12:00:00 GMT"))
+   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,
@@ -3341,7 +3320,7 @@ static jb_err client_if_none_match(struct client_state *csp, char **header)
  *********************************************************************/
 jb_err client_x_filter(struct client_state *csp, char **header)
 {
-   if ( 0 == strcmpic(*header, "X-Filter: No"))
+   if (0 == strcmpic(*header, "X-Filter: No"))
    {
       if (!(csp->config->feature_flags & RUNTIME_FEATURE_HTTP_TOGGLE))
       {
@@ -3373,11 +3352,25 @@ jb_err client_x_filter(struct client_state *csp, char **header)
  * 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.
+ *                content filtering is enabled and the range doesn't
+ *                start at byte 0.
+ *
+ *                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.
+ *
+ *                If the range starts at byte 0 this isn't an issue
+ *                so the header can pass. Partial requests like this
+ *                are used to render preview images for videos without
+ *                downloading the whole video.
+ *
+ *                While HTTP doesn't require that range requests are
+ *                honoured and the client could simply abort the download
+ *                after receiving a sufficient amount of data, various
+ *                clients don't handle complete responses to range
+ *                requests gracefully and emit misleading error messages
+ *                instead.
  *
  * Parameters  :
  *          1  :  csp = Current client state (buffers, headers, etc...)
@@ -3391,7 +3384,8 @@ jb_err client_x_filter(struct client_state *csp, char **header)
  *********************************************************************/
 static jb_err client_range(struct client_state *csp, char **header)
 {
-   if (content_filters_enabled(csp->action))
+   if (content_filters_enabled(csp->action)
+      && (0 != strncmpic(strstr(*header, ":"), ": bytes=0-", 10)))
    {
       log_error(LOG_LEVEL_HEADER, "Content filtering is enabled."
          " Crunching: \'%s\' to prevent range-mismatch problems.", *header);
@@ -3428,7 +3422,7 @@ static jb_err client_host_adder(struct client_state *csp)
       return JB_ERR_OK;
    }
 
-   if ( !csp->http->hostport || !*(csp->http->hostport))
+   if (!csp->http->hostport || !*(csp->http->hostport))
    {
       /* XXX: When does this happen and why is it OK? */
       log_error(LOG_LEVEL_INFO, "Weirdness in client_host_adder detected and ignored.");
@@ -3438,7 +3432,7 @@ static jb_err client_host_adder(struct client_state *csp)
    /*
     * remove 'user:pass@' from 'proto://user:pass@host'
     */
-   if ( (p = strchr( csp->http->hostport, '@')) != NULL )
+   if ((p = strchr( csp->http->hostport, '@')) != NULL)
    {
       p++;
    }
@@ -3571,7 +3565,7 @@ static jb_err server_connection_adder(struct client_state *csp)
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
     && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
 #endif
-       )
+      )
    {
       log_error(LOG_LEVEL_HEADER, "A HTTP/1.1 response "
          "without Connection header implies keep-alive.");
@@ -4020,7 +4014,7 @@ static jb_err parse_header_time(const char *header_time, time_t *result)
  *                the "Host:" header)
  *          2  :  http = storage for the result (host, port and hostport).
  *
- * Returns     :  JB_ERR_MEMORY in case of memory problems,
+ * Returns     :  JB_ERR_MEMORY (or terminates) in case of memory problems,
  *                JB_ERR_PARSE if the host header couldn't be found,
  *                JB_ERR_OK otherwise.
  *
@@ -4039,19 +4033,9 @@ jb_err get_destination_from_headers(const struct list *headers, struct http_requ
       return JB_ERR_PARSE;
    }
 
-   p = strdup(host);
-   if (NULL == p)
-   {
-      log_error(LOG_LEVEL_ERROR, "Out of memory while parsing \"Host:\" header");
-      return JB_ERR_MEMORY;
-   }
+   p = strdup_or_die(host);
    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;
-   }
+   q = strdup_or_die(p);
 
    freez(http->hostport);
    http->hostport = p;
@@ -4183,16 +4167,10 @@ static jb_err create_fake_referrer(char **header, const char *fake_referrer)
 static jb_err handle_conditional_hide_referrer_parameter(char **header,
    const char *host, const int parameter_conditional_block)
 {
-   char *referer = strdup(*header);
+   char *referer = strdup_or_die(*header);
    const size_t hostlength = strlen(host);
    const char *referer_url = NULL;
 
-   if (NULL == referer)
-   {
-      freez(*header);
-      return JB_ERR_MEMORY;
-   }
-
    /* referer begins with 'Referer: http[s]://' */
    if ((hostlength+17) < strlen(referer))
    {