Downgrade "Received x bytes while expecting y." message to
[privoxy.git] / jcc.c
diff --git a/jcc.c b/jcc.c
index c80e91a..d6bbf85 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.203 2008/11/06 18:34:35 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.210 2008/12/02 22:03:18 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -33,6 +33,37 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.203 2008/11/06 18:34:35 fabiankeil Exp $"
  *
  * Revisions   :
  *    $Log: jcc.c,v $
+ *    Revision 1.210  2008/12/02 22:03:18  fabiankeil
+ *    Don't miscalculate byte_count if we don't get all the
+ *    server headers with one read_socket() call. With keep-alive
+ *    support enabled, this caused delays until the server closed
+ *    the connection.
+ *
+ *    Revision 1.209  2008/11/27 09:44:04  fabiankeil
+ *    Cosmetics for the last commit: Don't watch out for
+ *    the last chunk if the content isn't chunk-encoded or
+ *    if we already determined the content length previously.
+ *
+ *    Revision 1.208  2008/11/26 18:24:17  fabiankeil
+ *    Recognize that the server response is complete if the
+ *    last chunk is read together with the server headers.
+ *    Reported by Lee.
+ *
+ *    Revision 1.207  2008/11/25 17:25:16  fabiankeil
+ *    Don't convert the client-header list to text until we need to.
+ *
+ *    Revision 1.206  2008/11/23 17:00:11  fabiankeil
+ *    Some more chat() cosmetics.
+ *
+ *    Revision 1.205  2008/11/16 12:43:49  fabiankeil
+ *    Turn keep-alive support into a runtime feature
+ *    that is disabled by setting keep-alive-timeout
+ *    to a negative value.
+ *
+ *    Revision 1.204  2008/11/06 19:42:17  fabiankeil
+ *    Fix last-chunk detection hack to also apply
+ *    if buf[] contains nothing but the last-chunk.
+ *
  *    Revision 1.203  2008/11/06 18:34:35  fabiankeil
  *    Factor receive_client_request() and
  *    parse_client_request() out of chat().
@@ -2514,13 +2545,6 @@ static void chat(struct client_state *csp)
       build_request_line(csp, fwd, &csp->headers->first->str);
    }
 
-   hdr = list_to_text(csp->headers);
-   if (hdr == NULL)
-   {
-      /* FIXME Should handle error properly */
-      log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
-   }
-
    /*
     * We have a request. Check if one of the crunchers wants it.
     */
@@ -2530,28 +2554,18 @@ static void chat(struct client_state *csp)
        * Yes. The client got the crunch response
        * and we are done here after cleaning up.
        */
-      freez(hdr);
+      /* XXX: why list_remove_all()? */
       list_remove_all(csp->headers);
 
       return;
    }
 
-   /*
-    * The headers can't be removed earlier because
-    * they were still needed for the referrer check
-    * in case of CGI crunches.
-    *
-    * XXX: Would it be worth to move the referrer check
-    * into client_referrer() and set a flag if it's trusted?
-    */
-   list_remove_all(csp->headers);
-
    log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
 
    if (fwd->forward_host)
    {
       log_error(LOG_LEVEL_CONNECT, "via %s:%d to: %s",
-               fwd->forward_host, fwd->forward_port, http->hostport);
+         fwd->forward_host, fwd->forward_port, http->hostport);
    }
    else
    {
@@ -2560,11 +2574,13 @@ static void chat(struct client_state *csp)
 
    /* here we connect to the server, gateway, or the forwarder */
 
-   while ( (csp->sfd = forwarded_connect(fwd, http, csp))
-         && (errno == EINVAL) && (forwarded_connect_retries++ < max_forwarded_connect_retries))
+   while ((csp->sfd = forwarded_connect(fwd, http, csp))
+      && (errno == EINVAL)
+      && (forwarded_connect_retries++ < max_forwarded_connect_retries))
    {
-      log_error(LOG_LEVEL_ERROR, "failed request #%u to connect to %s. Trying again.",
-                forwarded_connect_retries, http->hostport);
+      log_error(LOG_LEVEL_ERROR,
+         "failed request #%u to connect to %s. Trying again.",
+         forwarded_connect_retries, http->hostport);
    }
 
    if (csp->sfd == JB_INVALID_SOCKET)
@@ -2582,7 +2598,7 @@ static void chat(struct client_state *csp)
       {
          rsp = error_response(csp, "connect-failed", errno);
          log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E",
-                http->hostport);
+            http->hostport);
       }
 
       /* Write the answer to the client */
@@ -2591,10 +2607,17 @@ static void chat(struct client_state *csp)
          send_crunch_response(csp, rsp);
       }
 
-      freez(hdr);
       return;
    }
 
+   hdr = list_to_text(csp->headers);
+   if (hdr == NULL)
+   {
+      /* FIXME Should handle error properly */
+      log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
+   }
+   list_remove_all(csp->headers);
+
    if (fwd->forward_host || (http->ssl == 0))
    {
       /*
@@ -2660,6 +2683,18 @@ static void chat(struct client_state *csp)
       FD_SET(csp->sfd, &rfds);
 
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
+      if ((csp->flags & CSP_FLAG_CHUNKED)
+         && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
+         && ((csp->iob->eod - csp->iob->cur) >= 5)
+         && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
+      {
+         log_error(LOG_LEVEL_CONNECT,
+            "Looks like we read the last chunk together with "
+            "the server headers. We better stop reading.");
+         byte_count = (size_t)(csp->iob->eod - csp->iob->cur);
+         csp->expected_content_length = byte_count;
+         csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
+      }
       if (server_body && server_response_is_complete(csp, byte_count))
       {
          log_error(LOG_LEVEL_CONNECT,
@@ -2976,6 +3011,12 @@ static void chat(struct client_state *csp)
                    * Since we have to wait for more from the server before
                    * we can parse the headers we just continue here.
                    */
+                  int header_offset = csp->iob->cur - header_start;
+                  assert(csp->iob->cur >= header_start);
+                  byte_count += (size_t)(len - header_offset);
+                  log_error(LOG_LEVEL_CONNECT, "Continuing buffering headers. "
+                     "byte_count: %d. header_offset: %d. len: %d.",
+                     byte_count, header_offset, len);
                   continue;
                }
             }
@@ -3122,7 +3163,7 @@ static void chat(struct client_state *csp)
    if ((csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
       && (csp->expected_content_length != byte_count))
    {
-      log_error(LOG_LEVEL_ERROR,
+      log_error(LOG_LEVEL_CONNECT,
          "Received %d bytes while expecting %d.",
          byte_count, csp->expected_content_length);
       mark_server_socket_tainted(csp);
@@ -3158,7 +3199,8 @@ static void serve(struct client_state *csp)
    if (csp->sfd != JB_INVALID_SOCKET)
    {
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
-      if ((csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE))
+      if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
+       && (csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE))
       {
          remember_connection(csp->sfd, csp->http, forward_url(csp, csp->http));
       }