In server_last_modified(), rename negative to negative_delta.
[privoxy.git] / parsers.c
index 48845d7..b2b7085 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.158 2009/05/19 17:27:05 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.167 2009/05/28 21:13:34 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -471,7 +471,7 @@ jb_err decompress_iob(struct client_state *csp)
              */
             int skip_bytes;
             skip_bytes = *cur++;
-            skip_bytes = *cur++ << 8;
+            skip_bytes += *cur++ << 8;
 
             assert(skip_bytes == *csp->iob->cur - 2 + ((*csp->iob->cur - 1) << 8));
 
@@ -1562,30 +1562,32 @@ static jb_err filter_header(struct client_state *csp, char **header)
  *********************************************************************/
 static jb_err server_connection(struct client_state *csp, char **header)
 {
-   /* Do we have a 'Connection: close' header? */
-   if (strcmpic(*header, "Connection: close"))
+   if (!strcmpic(*header, "Connection: keep-alive"))
    {
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
-      if ((csp->config->feature_flags &
-           RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
-         && !strcmpic(*header, "Connection: keep-alive"))
+      if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE))
       {
-         /* Remember to keep the connection alive. */
          csp->flags |= CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE;
       }
-      log_error(LOG_LEVEL_HEADER,
-         "Keeping the server header '%s' around.", *header);
-#else
-      char *old_header = *header;
 
-      *header = strdup("Connection: close");
-      if (header == NULL)
-      { 
-         return JB_ERR_MEMORY;
+      if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
+      {
+         log_error(LOG_LEVEL_HEADER,
+            "Keeping the server header '%s' around.", *header);
       }
-      log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
-      freez(old_header);
+      else
 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+      {
+         char *old_header = *header;
+
+         *header = strdup("Connection: close");
+         if (header == NULL)
+         {
+            return JB_ERR_MEMORY;
+         }
+         log_error(LOG_LEVEL_HEADER, "Replaced: \'%s\' with \'%s\'", old_header, *header);
+         freez(old_header);
+      }
    }
 
    /* Signal server_connection_adder() to return early. */
@@ -1617,8 +1619,8 @@ static jb_err server_keep_alive(struct client_state *csp, char **header)
    unsigned int keep_alive_timeout;
    const char *timeout_position = strstr(*header, "timeout=");
 
-   if ((NULL != timeout_position)
-    && (1 != sscanf(timeout_position, "timeout=%u", &keep_alive_timeout)))
+   if ((NULL == timeout_position)
+    || (1 != sscanf(timeout_position, "timeout=%u", &keep_alive_timeout)))
    {
       log_error(LOG_LEVEL_ERROR, "Couldn't parse: %s", *header);
    }
@@ -2228,7 +2230,6 @@ static jb_err server_last_modified(struct client_state *csp, char **header)
 #endif
    struct tm *timeptr = NULL;
    time_t now, last_modified;                  
-   long int rtime;
    long int days, hours, minutes, seconds;
    
    /*
@@ -2278,7 +2279,7 @@ static jb_err server_last_modified(struct client_state *csp, char **header)
       now = time(NULL);
 #ifdef HAVE_GMTIME_R
       gmtime_r(&now, &gmt);
-#elif FEATURE_PTHREAD
+#elif defined(MUTEX_LOCKS_AVAILABLE)
       privoxy_mutex_lock(&gmtime_mutex);
       gmtime(&now);
       privoxy_mutex_unlock(&gmtime_mutex);
@@ -2292,23 +2293,25 @@ static jb_err server_last_modified(struct client_state *csp, char **header)
       }
       else
       {
-         rtime = (long int)difftime(now, last_modified);
+         long int rtime = (long int)difftime(now, last_modified);
          if (rtime)
          {
-            int negative = 0;
+            const int negative_delta = (rtime < 0);
 
-            if (rtime < 0)
+            if (negative_delta)
             {
                rtime *= -1; 
-               negative = 1;
                log_error(LOG_LEVEL_HEADER, "Server time in the future.");
             }
             rtime = pick_from_range(rtime);
-            if (negative) rtime *= -1;
+            if (negative_delta)
+            {
+               rtime *= -1;
+            }
             last_modified += rtime;
 #ifdef HAVE_GMTIME_R
             timeptr = gmtime_r(&last_modified, &gmt);
-#elif FEATURE_PTHREAD
+#elif defined(MUTEX_LOCKS_AVAILABLE)
             privoxy_mutex_lock(&gmtime_mutex);
             timeptr = gmtime(&last_modified);
             privoxy_mutex_unlock(&gmtime_mutex);
@@ -3028,7 +3031,7 @@ static jb_err client_if_modified_since(struct client_state *csp, char **header)
             tm += rtime * (negative ? -1 : 1);
 #ifdef HAVE_GMTIME_R
             timeptr = gmtime_r(&tm, &gmt);
-#elif FEATURE_PTHREAD
+#elif defined(MUTEX_LOCKS_AVAILABLE)
             privoxy_mutex_lock(&gmtime_mutex);
             timeptr = gmtime(&tm);
             privoxy_mutex_unlock(&gmtime_mutex);
@@ -3387,7 +3390,8 @@ static jb_err server_connection_adder(struct client_state *csp)
  * Function    :  server_proxy_connection_adder
  *
  * Description :  Adds a "Proxy-Connection: keep-alive" header to
- *                csp->headers. XXX: We should reuse existant ones.
+ *                csp->headers if the client asked for keep-alive.
+ *                XXX: We should reuse existant ones.
  *
  * Parameters  :
  *          1  :  csp = Current client state (buffers, headers, etc...)
@@ -3399,8 +3403,15 @@ static jb_err server_connection_adder(struct client_state *csp)
 static jb_err server_proxy_connection_adder(struct client_state *csp)
 {
    static const char proxy_connection_header[] = "Proxy-Connection: keep-alive";
-   log_error(LOG_LEVEL_HEADER, "Adding: %s", proxy_connection_header);
-   return enlist(csp->headers, proxy_connection_header);
+   jb_err err = JB_ERR_OK;
+
+   if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
+   {
+      log_error(LOG_LEVEL_HEADER, "Adding: %s", proxy_connection_header);
+      err = enlist(csp->headers, proxy_connection_header);
+   }
+
+   return err;
 }
 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
 
@@ -3421,15 +3432,20 @@ static jb_err server_proxy_connection_adder(struct client_state *csp)
  *********************************************************************/
 static jb_err client_connection_header_adder(struct client_state *csp)
 {
-   const unsigned int flags = csp->flags;
    const char *wanted_header = get_appropiate_connection_header(csp);
 
-   if (!(flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
-     && (flags & CSP_FLAG_CLIENT_CONNECTION_HEADER_SET))
+   if (!(csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
+     && (csp->flags & CSP_FLAG_CLIENT_CONNECTION_HEADER_SET))
    {
       return JB_ERR_OK;
    }
 
+   if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
+      && (csp->http->ssl == 0))
+   {
+      csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
+   }
+
    log_error(LOG_LEVEL_HEADER, "Adding: %s", wanted_header);
 
    return enlist(csp->headers, wanted_header);