Flush work-in-progress changes to keep outgoing connections
authorFabian Keil <fk@fabiankeil.de>
Thu, 9 Oct 2008 18:21:41 +0000 (18:21 +0000)
committerFabian Keil <fk@fabiankeil.de>
Thu, 9 Oct 2008 18:21:41 +0000 (18:21 +0000)
alive where possible. Incomplete and mostly #ifdef'd out.

gateway.c
gateway.h
jcc.c
jcc.h
parsers.c
project.h

index 110d0d6..69ea32b 100644 (file)
--- a/gateway.c
+++ b/gateway.c
@@ -1,4 +1,4 @@
-const char gateway_rcs[] = "$Id: gateway.c,v 1.26 2008/08/18 17:42:06 fabiankeil Exp $";
+const char gateway_rcs[] = "$Id: gateway.c,v 1.27 2008/09/27 15:05:51 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/gateway.c,v $
@@ -7,7 +7,7 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.26 2008/08/18 17:42:06 fabiankeil
  *                using a "forwarder" (i.e. HTTP proxy and/or a SOCKS4
  *                proxy).
  *
- * Copyright   :  Written by and Copyright (C) 2001-2007 the SourceForge
+ * Copyright   :  Written by and Copyright (C) 2001-2008 the SourceForge
  *                Privoxy team. http://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
@@ -34,6 +34,9 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.26 2008/08/18 17:42:06 fabiankeil
  *
  * Revisions   :
  *    $Log: gateway.c,v $
+ *    Revision 1.27  2008/09/27 15:05:51  fabiankeil
+ *    Return only once in forwarded_connect().
+ *
  *    Revision 1.26  2008/08/18 17:42:06  fabiankeil
  *    Fix typo in macro name.
  *
@@ -190,6 +193,12 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.26 2008/08/18 17:42:06 fabiankeil
 #include "jbsockets.h"
 #include "gateway.h"
 #include "miscutil.h"
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+#ifdef __GLIBC__
+#include <sys/poll.h>
+#endif /* __GLIBC__ */
+#include <poll.h>
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
 
 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
 
@@ -240,6 +249,441 @@ struct socks_reply {
 
 static const char socks_userid[] = "anonymous";
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+
+#define MAX_REUSABLE_CONNECTIONS 100
+
+struct reusable_connection
+{
+   jb_socket sfd;
+   int       in_use;
+   char      *host;
+   int       port;
+
+   int       forwarder_type;
+   char      *gateway_host;
+   int       gateway_port;
+   char      *forward_host;
+   int       forward_port;
+};
+
+static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
+
+static int mark_connection_unused(jb_socket sfd);
+static void mark_connection_closed(struct reusable_connection *closed_connection);
+
+/*********************************************************************
+ *
+ * Function    :  initialize_reusable_connections
+ *
+ * Description :  Initializes the reusable_connection structures.
+ *                Must be called with connection_reuse_mutex locked.
+ *
+ * Parameters  : N/A
+ *
+ * Returns     : void
+ *
+ *********************************************************************/
+extern void initialize_reusable_connections(void)
+{
+   unsigned int slot = 0;
+
+   log_error(LOG_LEVEL_INFO,
+      "Support for 'Connection: keep-alive' is experimental, "
+      "incomplete and known not to work properly in some situations.");
+
+   for (slot = 0; slot < SZ(reusable_connection); slot++)
+   {
+      mark_connection_closed(&reusable_connection[slot]);
+   }
+
+   log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  remember_connection
+ *
+ * Description :  Remembers a connection for reuse later on.
+ *
+ * Parameters  :
+ *          1  :  sfd  = Open socket to remember.
+ *          2  :  http = The destination for the connection.
+ *          3  :  fwd  = The forwarder settings used.
+ *
+ * Returns     : void
+ *
+ *********************************************************************/
+void remember_connection(jb_socket sfd, const struct http_request *http,
+                                        const struct forward_spec *fwd)
+{
+   unsigned int slot = 0;
+   int free_slot_found = FALSE;
+
+   assert(sfd != JB_INVALID_SOCKET);
+
+   if (mark_connection_unused(sfd))
+   {
+      return;
+   }
+
+   privoxy_mutex_lock(&connection_reuse_mutex);
+
+   /* Find free socket slot. */
+   for (slot = 0; slot < SZ(reusable_connection); slot++)
+   {
+      if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
+      {
+         assert(reusable_connection[slot].in_use == 0);
+         log_error(LOG_LEVEL_CONNECT,
+            "Remembering socket %d for %s:%d in slot %d.",
+            sfd, http->host, http->port, slot);
+         free_slot_found = TRUE;
+         break;
+      }
+   }
+
+   if (!free_slot_found)
+   {
+      log_error(LOG_LEVEL_CONNECT,
+        "No free slots found to remembering socket for %s:%d. Last slot %d.",
+        http->host, http->port, slot);
+      privoxy_mutex_unlock(&connection_reuse_mutex);
+      close_socket(sfd);
+      return;
+   }
+
+   assert(NULL != http->host);
+   reusable_connection[slot].host = strdup(http->host);
+   if (NULL == reusable_connection[slot].host)
+   {
+      log_error(LOG_LEVEL_FATAL, "Out of memory saving socket.");
+   }
+   reusable_connection[slot].sfd = sfd;
+   reusable_connection[slot].port = http->port;
+   reusable_connection[slot].in_use = 0;
+
+   assert(NULL != fwd);
+   assert(reusable_connection[slot].gateway_host == NULL);
+   assert(reusable_connection[slot].gateway_port == 0);
+   assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
+   assert(reusable_connection[slot].forward_host == NULL);
+   assert(reusable_connection[slot].forward_port == 0);
+
+   reusable_connection[slot].forwarder_type = fwd->type;
+   if (NULL != fwd->gateway_host)
+   {
+      reusable_connection[slot].gateway_host = strdup(fwd->gateway_host);
+      if (NULL == reusable_connection[slot].gateway_host)
+      {
+         log_error(LOG_LEVEL_FATAL, "Out of memory saving gateway_host.");
+      }
+   }
+   else
+   {
+      reusable_connection[slot].gateway_host = NULL;
+   }
+   reusable_connection[slot].gateway_port = fwd->gateway_port;
+
+   if (NULL != fwd->forward_host)
+   {
+      reusable_connection[slot].forward_host = strdup(fwd->forward_host);
+      if (NULL == reusable_connection[slot].forward_host)
+      {
+         log_error(LOG_LEVEL_FATAL, "Out of memory saving forward_host.");
+      }
+   }
+   else
+   {
+      reusable_connection[slot].forward_host = NULL;
+   }
+   reusable_connection[slot].forward_port = fwd->forward_port;
+
+   privoxy_mutex_unlock(&connection_reuse_mutex);
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  mark_connection_closed
+ *
+ * Description : Marks a reused connection closed.
+ *               Must be called with connection_reuse_mutex locked.
+ *
+ * Parameters  :
+ *          1  :  closed_connection = The connection to mark as closed.
+ *
+ * Returns     : void
+ *
+ *********************************************************************/
+static void mark_connection_closed(struct reusable_connection *closed_connection)
+{
+   closed_connection->in_use = FALSE;
+   closed_connection->sfd = JB_INVALID_SOCKET;
+   freez(closed_connection->host);
+   closed_connection->port = 0;
+   closed_connection->forwarder_type = SOCKS_NONE;
+   freez(closed_connection->gateway_host);
+   closed_connection->gateway_port = 0;
+   freez(closed_connection->forward_host);
+   closed_connection->forward_port = 0;
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  forget_connection
+ *
+ * Description :  Removes a previously remembered connection from
+ *                the list of reusable connections.
+ *
+ * Parameters  :
+ *          1  :  sfd = The socket belonging to the connection in question.
+ *
+ * Returns     : void
+ *
+ *********************************************************************/
+void forget_connection(jb_socket sfd)
+{
+   unsigned int slot = 0;
+
+   assert(sfd != JB_INVALID_SOCKET);
+
+   privoxy_mutex_lock(&connection_reuse_mutex);
+
+   for (slot = 0; slot < SZ(reusable_connection); slot++)
+   {
+      if (reusable_connection[slot].sfd == sfd)
+      {
+         assert(reusable_connection[slot].in_use);
+         break;
+      }
+   }
+
+   if (reusable_connection[slot].sfd != sfd)
+   {
+      log_error(LOG_LEVEL_CONNECT,
+        "Socket %d already forgotten or never remembered.", sfd);
+      privoxy_mutex_unlock(&connection_reuse_mutex);
+      return;
+   }
+
+   log_error(LOG_LEVEL_CONNECT,
+      "Forgetting socket %d for %s:%d in slot %d.",
+      sfd, reusable_connection[slot].host, reusable_connection[slot].port, slot);
+
+   mark_connection_closed(&reusable_connection[slot]);
+
+   privoxy_mutex_unlock(&connection_reuse_mutex);
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  connection_destination_matches
+ *
+ * Description :  Determines whether a remembered connection can
+ *                be reused. That is whether the destination and
+ *                the forwarding settings match.
+ *
+ * Parameters  :
+ *          1  :  connection = The connection to check.
+ *          2  :  http = The destination for the connection.
+ *          3  :  fwd  = The forwarder settings.
+ *
+ * Returns     :  TRUE for yes, FALSE otherwise.
+ *
+ *********************************************************************/
+static int connection_destination_matches(const struct reusable_connection *connection,
+                                          const struct http_request *http,
+                                          const struct forward_spec *fwd)
+{
+   /* XXX: Start of duplicated checks for debugging purposes. */
+   if (strcmpic(connection->host, http->host))
+   {
+      return FALSE;
+   }
+
+   if (connection->forwarder_type != fwd->type)
+   {
+      log_error(LOG_LEVEL_CONNECT, "Type mismatch: %d %d (%s)",
+         connection->forwarder_type, fwd->type, http->host);
+      return FALSE;
+   }
+   if (connection->gateway_port   != fwd->gateway_port)
+   {
+      log_error(LOG_LEVEL_CONNECT, "Gateway port mismatch: %d %d (%s)",
+         connection->gateway_port, fwd->gateway_port, http->host);
+      return FALSE;
+   }
+   if (connection->forward_port   != fwd->forward_port)
+   {
+      log_error(LOG_LEVEL_CONNECT, "Forward port mismatch: %d %d (%s)",
+         connection->forward_port, fwd->forward_port, http->host);
+      return FALSE;
+   }
+   if (connection->forward_port   != fwd->forward_port)
+   {
+      log_error(LOG_LEVEL_CONNECT, "Server port mismatch: %d %d (%s)",
+         connection->forward_port, fwd->forward_port, http->host);
+      return FALSE;
+   }
+
+   /* XXX: End of duplicated checks for debugging purposes. */
+
+   if ((connection->forwarder_type != fwd->type)
+    || (connection->gateway_port   != fwd->gateway_port)
+    || (connection->forward_port   != fwd->forward_port)
+    || (connection->port           != http->port))
+   {
+      return FALSE;
+   }
+
+   if ((    (NULL != connection->gateway_host)
+         && (NULL != fwd->gateway_host)
+         && strcmpic(connection->gateway_host, fwd->gateway_host))
+       && (connection->gateway_host != fwd->gateway_host))
+   {
+      log_error(LOG_LEVEL_CONNECT, "Gateway mismatch.");
+      return FALSE;
+   }
+
+   if ((    (NULL != connection->forward_host)
+         && (NULL != fwd->forward_host)
+         && strcmpic(connection->forward_host, fwd->forward_host))
+      && (connection->forward_host != fwd->forward_host))
+   {
+      log_error(LOG_LEVEL_CONNECT, "Forwarding proxy mismatch.");
+      return FALSE;
+   }
+
+   return (!strcmpic(connection->host, http->host));
+
+}
+
+/*********************************************************************
+ *
+ * Function    :  get_reusable_connection
+ *
+ * Description :  Returns an open socket to a previously remembered
+ *                open connection (if there is one).
+ *
+ * Parameters  :
+ *          1  :  http = The destination for the connection.
+ *          2  :  fwd  = The forwarder settings.
+ *
+ * Returns     :  JB_INVALID_SOCKET => No reusable connection found,
+ *                otherwise a usable socket.
+ *
+ *********************************************************************/
+static jb_socket get_reusable_connection(const struct http_request *http,
+                                         const struct forward_spec *fwd)
+{
+   jb_socket sfd = JB_INVALID_SOCKET;
+   unsigned int slot = 0;
+
+   privoxy_mutex_lock(&connection_reuse_mutex);
+
+   for (slot = 0; slot < SZ(reusable_connection); slot++)
+   {
+      if (!reusable_connection[slot].in_use
+         && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
+      {
+         int poll_result;
+         struct pollfd poll_fd[1];
+         memset(poll_fd, 0, sizeof(poll_fd));
+         poll_fd[0].fd = reusable_connection[slot].sfd;
+         poll_fd[0].events = POLLIN;
+
+         poll_result = poll(poll_fd, 1, 0);
+
+         if (-1 != poll_result)
+         {
+            if ((poll_fd[0].revents & POLLIN))
+            {
+               log_error(LOG_LEVEL_CONNECT,
+                  "Socket %d for %s:%d in slot %d is no longer usable. Closing.",
+                  reusable_connection[slot].sfd, reusable_connection[slot].host,
+                  reusable_connection[slot].port, slot);
+               mark_connection_closed(&reusable_connection[slot]);
+            }
+         }
+         else
+         {
+            log_error(LOG_LEVEL_CONNECT,
+               "Failed to poll socket %d for %s:%d in slot %d",
+               reusable_connection[slot].sfd, reusable_connection[slot].host,
+               reusable_connection[slot].port, slot);
+         }
+
+
+         if (connection_destination_matches(&reusable_connection[slot], http, fwd))
+         {
+            reusable_connection[slot].in_use = TRUE;
+            sfd = reusable_connection[slot].sfd;
+            log_error(LOG_LEVEL_CONNECT,
+               "Found reusable socket %d for %s:%d in slot %d",
+               sfd, reusable_connection[slot].host, reusable_connection[slot].port, slot);
+            break;
+         }
+      }
+   }
+
+   privoxy_mutex_unlock(&connection_reuse_mutex);
+
+   return sfd;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function    :  mark_connection_unused
+ *
+ * Description :  Gives a remembered connection free for reuse.
+ *
+ * Parameters  :
+ *          1  :  sfd = The socket belonging to the connection in question.
+ *
+ * Returns     :  TRUE => Socket found and marked as unused.
+ *                FALSE => Socket not found.
+ *
+ *********************************************************************/
+static int mark_connection_unused(jb_socket sfd)
+{
+   unsigned int slot = 0;
+   unsigned int socket_found = FALSE;
+
+   assert(sfd != JB_INVALID_SOCKET);
+
+   privoxy_mutex_lock(&connection_reuse_mutex);
+
+   for (slot = 0; slot < SZ(reusable_connection); slot++)
+   {
+      if (reusable_connection[slot].sfd == sfd)
+      {
+         assert(reusable_connection[slot].in_use);
+         break;
+      }
+   }
+
+   if (reusable_connection[slot].sfd == sfd)
+   {
+      socket_found = TRUE;
+      log_error(LOG_LEVEL_CONNECT,
+         "Marking open socket %d for %s:%d in slot %d as unused.",
+         sfd, reusable_connection[slot].host, reusable_connection[slot].port, slot);
+      reusable_connection[slot].in_use = 0;
+   }
+
+   privoxy_mutex_unlock(&connection_reuse_mutex);
+
+   return socket_found;
+
+}
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
 
 /*********************************************************************
  *
@@ -264,6 +708,20 @@ jb_socket forwarded_connect(const struct forward_spec * fwd,
    int dest_port;
    jb_socket sfd = JB_INVALID_SOCKET;
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+   sfd = get_reusable_connection(http, fwd);
+   if (JB_INVALID_SOCKET == sfd)
+   {
+      log_error(LOG_LEVEL_CONNECT,
+         "No reusable socket for %s:%d found. Opening a new one.",
+         http->host, http->port);
+   }
+   else
+   {
+      return sfd;
+   }
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
    /* Figure out if we need to connect to the web server or a HTTP proxy. */
    if (fwd->forward_host)
    {
index b887da8..c5f83a2 100644 (file)
--- a/gateway.h
+++ b/gateway.h
@@ -1,9 +1,9 @@
 #ifndef GATEWAY_H_INCLUDED
 #define GATEWAY_H_INCLUDED
-#define GATEWAY_H_VERSION "$Id: gateway.h,v 1.7 2002/03/26 22:29:54 swa Exp $"
+#define GATEWAY_H_VERSION "$Id: gateway.h,v 1.9 2006/07/18 14:48:46 david__schmidt Exp $"
 /*********************************************************************
  *
- * File        :  $Source: /cvsroot/ijbswa/current/Attic/gateway.h,v $
+ * File        :  $Source: /cvsroot/ijbswa/current/gateway.h,v $
  *
  * Purpose     :  Contains functions to connect to a server, possibly
  *                using a "gateway" (i.e. HTTP proxy and/or SOCKS4
  *
  * Revisions   :
  *    $Log: gateway.h,v $
+ *    Revision 1.9  2006/07/18 14:48:46  david__schmidt
+ *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
+ *    with what was really the latest development (the v_3_0_branch branch)
+ *
  *    Revision 1.7  2002/03/26 22:29:54  swa
  *    we have a new homepage!
  *
@@ -99,6 +103,14 @@ struct client_state;
 extern jb_socket forwarded_connect(const struct forward_spec * fwd, 
                                    struct http_request *http, 
                                    struct client_state *csp);
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+extern void initialize_reusable_connections(void);
+extern void forget_connection(jb_socket sfd);
+extern void remember_connection(jb_socket sfd,
+                                const struct http_request *http,
+                                const struct forward_spec *fwd);
+#endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+
 
 /*
  * Solaris fix
diff --git a/jcc.c b/jcc.c
index 2d96ac9..4b3e4eb 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.186 2008/09/04 08:13:58 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.187 2008/09/07 12:35:05 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -33,6 +33,9 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.186 2008/09/04 08:13:58 fabiankeil Exp $"
  *
  * Revisions   :
  *    $Log: jcc.c,v $
+ *    Revision 1.187  2008/09/07 12:35:05  fabiankeil
+ *    Add mutex lock support for _WIN32.
+ *
  *    Revision 1.186  2008/09/04 08:13:58  fabiankeil
  *    Prepare for critical sections on Windows by adding a
  *    layer of indirection before the pthread mutex functions.
@@ -1180,6 +1183,7 @@ static int32 server_thread(void *data);
  */
 privoxy_mutex_t log_mutex;
 privoxy_mutex_t log_init_mutex;
+privoxy_mutex_t connection_reuse_mutex;
 
 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R)
 privoxy_mutex_t resolver_mutex;
@@ -1970,6 +1974,52 @@ static jb_err change_request_destination(struct client_state *csp)
 }
 
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+/*********************************************************************
+ *
+ * Function    :  server_response_is_complete
+ *
+ * Description :  Determines whether we should stop reading
+ *                from the server socket.
+ *
+ * Parameters  :
+ *          1  :  csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns     :  TRUE if the response is complete,
+ *                FALSE otherwise.
+ *
+ *********************************************************************/
+static int server_response_is_complete(struct client_state *csp)
+{
+   int content_length_known = (csp->flags & CSP_FLAG_CONTENT_LENGTH_SET);
+
+   if (!strcmpic(csp->http->gpc, "HEAD"))
+   {
+      /*
+       * "HEAD" implies no body, we are thus expecting
+       * no content. XXX: incomplete "list" of methods?
+       */
+      log_error(LOG_LEVEL_INFO, "Method %s implies no body.", csp->http->gpc);
+      csp->expected_content_length = 0;
+      content_length_known = TRUE;
+   }
+
+   if (csp->http->status == 304)
+   {
+      /*
+       * Expect no body. XXX: incomplete "list" of status codes?
+       */
+      log_error(LOG_LEVEL_INFO, "Status code %d implies no body.", csp->http->status);
+      csp->expected_content_length = 0;
+      content_length_known = TRUE;
+   }
+
+   return (content_length_known && ((0 == csp->expected_content_length)
+            || (csp->expected_content_length <= (csp->iob->eod - csp->iob->cur))));
+}
+#endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+
+
 /*********************************************************************
  *
  * Function    :  chat
@@ -2460,6 +2510,22 @@ static void chat(struct client_state *csp)
       FD_SET(csp->cfd, &rfds);
       FD_SET(csp->sfd, &rfds);
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+      if (server_body && server_response_is_complete(csp))
+      {
+         log_error(LOG_LEVEL_CONNECT,
+            "Stopped reading from server. Expected content length: %d. "
+            "Actual content length: %d. Most recently received: %d.",
+            csp->expected_content_length, (csp->iob->eod - csp->iob->cur), len);
+         len = 0;
+         /*
+          * XXX: should not jump around,
+          * chat() is complicated enough already.
+          */
+         goto reading_done;
+      }
+#endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
+
       n = select((int)maxfd+1, &rfds, NULL, NULL, NULL);
 
       if (n < 0)
@@ -2540,6 +2606,10 @@ static void chat(struct client_state *csp)
             return;
          }
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+         reading_done:
+#endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
+
          /* Add a trailing zero.  This lets filter_popups
           * use string operations.
           */
@@ -2900,7 +2970,19 @@ 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))
+      {
+         remember_connection(csp->sfd, csp->http, forward_url(csp, csp->http));
+      }
+      else
+      {
+         forget_connection(csp->sfd);
+         close_socket(csp->sfd);
+      }
+#else
       close_socket(csp->sfd);
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
    }
 
    csp->flags &= ~CSP_FLAG_ACTIVE;
@@ -3071,8 +3153,8 @@ static void initialize_mutexes(void)
     * Prepare global mutex semaphores
     */
    privoxy_mutex_init(&log_mutex);
-
    privoxy_mutex_init(&log_init_mutex);
+   privoxy_mutex_init(&connection_reuse_mutex);
 
    /*
     * XXX: The assumptions below are a bit naive
@@ -3629,6 +3711,14 @@ static void listen_loop(void)
 
    config = load_config();
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+   /*
+    * XXX: Should be relocated once it no
+    * longer needs to emit log messages.
+    */
+   initialize_reusable_connections();
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
    bfd = bind_port_helper(config);
 
 #ifdef FEATURE_GRACEFUL_TERMINATION
diff --git a/jcc.h b/jcc.h
index 86e7334..7fbc483 100644 (file)
--- a/jcc.h
+++ b/jcc.h
@@ -1,6 +1,6 @@
 #ifndef JCC_H_INCLUDED
 #define JCC_H_INCLUDED
-#define JCC_H_VERSION "$Id: jcc.h,v 1.23 2008/09/04 08:13:58 fabiankeil Exp $"
+#define JCC_H_VERSION "$Id: jcc.h,v 1.24 2008/09/07 12:35:05 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.h,v $
@@ -35,6 +35,9 @@
  *
  * Revisions   :
  *    $Log: jcc.h,v $
+ *    Revision 1.24  2008/09/07 12:35:05  fabiankeil
+ *    Add mutex lock support for _WIN32.
+ *
  *    Revision 1.23  2008/09/04 08:13:58  fabiankeil
  *    Prepare for critical sections on Windows by adding a
  *    layer of indirection before the pthread mutex functions.
@@ -209,6 +212,7 @@ extern void privoxy_mutex_unlock(privoxy_mutex_t *mutex);
 
 extern privoxy_mutex_t log_mutex;
 extern privoxy_mutex_t log_init_mutex;
+extern privoxy_mutex_t connection_reuse_mutex;
 
 #ifndef HAVE_GMTIME_R
 extern privoxy_mutex_t gmtime_mutex;
index b841ca6..48f53fe 100644 (file)
--- a/parsers.c
+++ b/parsers.c
@@ -1,4 +1,4 @@
-const char parsers_rcs[] = "$Id: parsers.c,v 1.143 2008/09/21 13:36:52 fabiankeil Exp $";
+const char parsers_rcs[] = "$Id: parsers.c,v 1.144 2008/09/21 13:59:33 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/parsers.c,v $
@@ -44,6 +44,9 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.143 2008/09/21 13:36:52 fabiankei
  *
  * Revisions   :
  *    $Log: parsers.c,v $
+ *    Revision 1.144  2008/09/21 13:59:33  fabiankeil
+ *    Treat unknown change-x-forwarded-for parameters as fatal errors.
+ *
  *    Revision 1.143  2008/09/21 13:36:52  fabiankeil
  *    If change-x-forwarded-for{add} is used and the client
  *    sends multiple X-Forwarded-For headers, append the client's
@@ -910,8 +913,8 @@ static jb_err header_tagger(struct client_state *csp, char *header);
 static jb_err parse_header_time(const char *header_time, time_t *result);
 
 static jb_err crumble                   (struct client_state *csp, char **header);
-static jb_err connection                (struct client_state *csp, char **header);
 static jb_err filter_header             (struct client_state *csp, char **header);
+static jb_err client_connection         (struct client_state *csp, char **header);
 static jb_err client_referrer           (struct client_state *csp, char **header);
 static jb_err client_uagent             (struct client_state *csp, char **header);
 static jb_err client_ua                 (struct client_state *csp, char **header);
@@ -929,8 +932,9 @@ 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_connection         (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);
+static jb_err server_adjust_content_length(struct client_state *csp, char **header);
 static jb_err server_content_md5        (struct client_state *csp, char **header);
 static jb_err server_content_encoding   (struct client_state *csp, char **header);
 static jb_err server_transfer_coding    (struct client_state *csp, char **header);
@@ -939,10 +943,15 @@ static jb_err crunch_server_header      (struct client_state *csp, char **header
 static jb_err server_last_modified      (struct client_state *csp, char **header);
 static jb_err server_content_disposition(struct client_state *csp, char **header);
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+static jb_err server_save_content_length(struct client_state *csp, char **header);
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
 static jb_err client_host_adder       (struct client_state *csp);
 static jb_err client_xtra_adder       (struct client_state *csp);
 static jb_err client_x_forwarded_for_adder(struct client_state *csp);
-static jb_err connection_close_adder  (struct client_state *csp); 
+static jb_err client_connection_header_adder(struct client_state *csp);
+static jb_err server_connection_close_adder(struct client_state *csp);
 
 static jb_err create_forged_referrer(char **header, const char *hostport);
 static jb_err create_fake_referrer(char **header, const char *fake_referrer);
@@ -976,7 +985,7 @@ static const struct parsers client_patterns[] = {
    { "Host:",                     5,   client_host },
    { "if-modified-since:",       18,   client_if_modified_since },
    { "Keep-Alive:",              11,   crumble },
-   { "connection:",              11,   connection },
+   { "connection:",              11,   client_connection },
    { "proxy-connection:",        17,   crumble },
    { "max-forwards:",            13,   client_max_forwards },
    { "Accept-Language:",         16,   client_accept_language },
@@ -993,10 +1002,13 @@ static const struct parsers client_patterns[] = {
 static const struct parsers server_patterns[] = {
    { "HTTP/",                     5, server_http },
    { "set-cookie:",              11, server_set_cookie },
-   { "connection:",              11, connection },
+   { "connection:",              11, server_connection },
    { "Content-Type:",            13, server_content_type },
    { "Content-MD5:",             12, server_content_md5 },
    { "Content-Encoding:",        17, server_content_encoding },
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+   { "Content-Length:",          15, server_save_content_length },
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
    { "Transfer-Encoding:",       18, server_transfer_coding },
    { "Keep-Alive:",              11, crumble },
    { "content-disposition:",     20, server_content_disposition },
@@ -1011,12 +1023,12 @@ static const add_header_func_ptr add_client_headers[] = {
    client_x_forwarded_for_adder,
    client_xtra_adder,
    /* Temporarily disabled:    client_accept_encoding_adder, */
-   connection_close_adder,
+   client_connection_header_adder,
    NULL
 };
 
 static const add_header_func_ptr add_server_headers[] = {
-   connection_close_adder,
+   server_connection_close_adder,
    NULL
 };
 
@@ -1091,7 +1103,9 @@ jb_err add_to_iob(struct client_state *csp, char *buf, int n)
     */
    if (need > csp->config->buffer_limit)
    {
-      log_error(LOG_LEVEL_INFO, "Buffer limit reached while extending the buffer (iob)");
+      log_error(LOG_LEVEL_INFO,
+         "Buffer limit reached while extending the buffer (iob). Needed: %d. Limit: %d",
+         need, csp->config->buffer_limit);
       return JB_ERR_MEMORY;
    }
 
@@ -1897,7 +1911,7 @@ jb_err update_server_headers(struct client_state *csp)
    jb_err err = JB_ERR_OK;
 
    static const struct parsers server_patterns_light[] = {
-      { "Content-Length:",    15, server_content_length },
+      { "Content-Length:",    15, server_adjust_content_length },
       { "Transfer-Encoding:", 18, server_transfer_coding },
 #ifdef FEATURE_ZLIB
       { "Content-Encoding:",  17, server_content_encoding },
@@ -2314,10 +2328,10 @@ static jb_err filter_header(struct client_state *csp, char **header)
 
 /*********************************************************************
  *
- * Function    :  connection
+ * Function    :  server_connection
  *
  * Description :  Makes sure that the value of the Connection: header
- *                is "close" and signals connection_close_adder 
+ *                is "close" and signals server_connection_close_adder 
  *                to do nothing.
  *
  * Parameters  :
@@ -2331,14 +2345,21 @@ static jb_err filter_header(struct client_state *csp, char **header)
  *                JB_ERR_MEMORY on out-of-memory error.
  *
  *********************************************************************/
-static jb_err connection(struct client_state *csp, char **header)
+static jb_err server_connection(struct client_state *csp, char **header)
 {
    char *old_header = *header;
 
    /* Do we have a 'Connection: close' header? */
    if (strcmpic(*header, "Connection: close"))
    {
-      /* No, create one */
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+      if (!strcmpic(*header, "Connection: keep-alive"))
+      {
+         /* Remember to keep the connection alive. */
+         csp->flags |= CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE;
+      }
+#endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+
       *header = strdup("Connection: close");
       if (header == NULL)
       { 
@@ -2348,16 +2369,55 @@ static jb_err connection(struct client_state *csp, char **header)
       freez(old_header);
    }
 
-   /* Signal connection_close_adder() to return early. */
-   if (csp->flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
-   {
-      csp->flags |= CSP_FLAG_SERVER_CONNECTION_CLOSE_SET;
-   }
-   else
+   /* Signal server_connection_close_adder() to return early. */
+   csp->flags |= CSP_FLAG_SERVER_CONNECTION_CLOSE_SET;
+
+   return JB_ERR_OK;
+}
+
+/*********************************************************************
+ *
+ * Function    :  client_connection
+ *
+ * Description :  Makes sure a proper "Connection:" header is
+ *                set and signals connection_header_adder 
+ *                to do nothing.
+ *
+ * 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.
+ *
+ *********************************************************************/
+static jb_err client_connection(struct client_state *csp, char **header)
+{
+   char *old_header = *header;
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+   static const char wanted_header[] = "Connection: keep-alive";
+#else
+   static const char wanted_header[] = "Connection: close";
+#endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+
+   if (strcmpic(*header, wanted_header))
    {
-      csp->flags |= CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET;
+      *header = strdup(wanted_header);
+      if (header == NULL)
+      { 
+         return JB_ERR_MEMORY;
+      }
+      log_error(LOG_LEVEL_HEADER,
+         "Replaced: \'%s\' with \'%s\'", old_header, *header);
+      freez(old_header);
    }
 
+   /* Signal client_connection_close_adder() to return early. */
+   csp->flags |= CSP_FLAG_CLIENT_CONNECTION_HEADER_SET;
+
    return JB_ERR_OK;
 }
 
@@ -2580,6 +2640,13 @@ static jb_err server_transfer_coding(struct client_state *csp, char **header)
          log_error(LOG_LEVEL_HEADER, "Removing: %s", *header);
          freez(*header);
       }
+
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+      log_error(LOG_LEVEL_ERROR,
+         "Chunked transfer encoding detected with experimental "
+         "keep-alive support enabled. Expect delayed delivery.");
+#endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
+
    }
 
    return JB_ERR_OK;
@@ -2676,7 +2743,7 @@ static jb_err server_content_encoding(struct client_state *csp, char **header)
 
 /*********************************************************************
  *
- * Function    :  server_content_length
+ * Function    :  server_adjust_content_length
  *
  * Description :  Adjust Content-Length header if we modified
  *                the body.
@@ -2692,7 +2759,7 @@ static jb_err server_content_encoding(struct client_state *csp, char **header)
  *                JB_ERR_MEMORY on out-of-memory error.
  *
  *********************************************************************/
-static jb_err server_content_length(struct client_state *csp, char **header)
+static jb_err server_adjust_content_length(struct client_state *csp, char **header)
 {
    const size_t max_header_length = 80;
 
@@ -2716,6 +2783,46 @@ static jb_err server_content_length(struct client_state *csp, char **header)
 }
 
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+/*********************************************************************
+ *
+ * Function    :  server_save_content_length
+ *
+ * Description :  Save the Content-Length sent by the server.
+ *
+ * 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.
+ *
+ *********************************************************************/
+static jb_err server_save_content_length(struct client_state *csp, char **header)
+{
+   unsigned int content_length = 0;
+
+   assert(*(*header+14) == ':');
+
+   if (1 != sscanf(*header+14, ": %u", &content_length))
+   {
+      log_error(LOG_LEVEL_ERROR, "Crunching invalid header: %s", *header);
+      freez(*header);
+   }
+   else
+   {
+      csp->expected_content_length = content_length;
+      csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
+   }
+
+   return JB_ERR_OK;
+}
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
+
 /*********************************************************************
  *
  * Function    :  server_content_md5
@@ -3946,7 +4053,7 @@ static jb_err client_x_forwarded_for_adder(struct client_state *csp)
 
 /*********************************************************************
  *
- * Function    :  connection_close_adder
+ * Function    :  server_connection_close_adder
  *
  * Description :  "Temporary" fix for the needed but missing HTTP/1.1
  *                support. Adds a "Connection: close" header to csp->headers
@@ -3961,23 +4068,12 @@ static jb_err client_x_forwarded_for_adder(struct client_state *csp)
  *                JB_ERR_MEMORY on out-of-memory error.
  *
  *********************************************************************/
-static jb_err connection_close_adder(struct client_state *csp)
+static jb_err server_connection_close_adder(struct client_state *csp)
 {
    const unsigned int flags = csp->flags;
 
-   /*
-    * Return right away if
-    *
-    * - we're parsing server headers and the server header
-    *   "Connection: close" is already set, or if
-    *
-    * - we're parsing client headers and the client header 
-    *   "Connection: close" is already set.
-    */
-   if ((flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE
-     && flags & CSP_FLAG_SERVER_CONNECTION_CLOSE_SET)
-   ||(!(flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
-     && flags & CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET))
+   if ((flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
+    && (flags & CSP_FLAG_SERVER_CONNECTION_CLOSE_SET))
    {
       return JB_ERR_OK;
    }
@@ -3988,6 +4084,41 @@ static jb_err connection_close_adder(struct client_state *csp)
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  client_connection_header_adder
+ *
+ * Description :  Adds a proper "Connection:" header to csp->headers
+ *                unless the header was already present. 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_connection_header_adder(struct client_state *csp)
+{
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+   static const char wanted_header[] = "Connection: keep-alive";
+#else
+   static const char wanted_header[] = "Connection: close";
+#endif /* FEATURE_CONNECTION_KEEP_ALIVE */
+   const unsigned int flags = csp->flags;
+
+   if (!(flags & CSP_FLAG_CLIENT_HEADER_PARSING_DONE)
+     && (flags & CSP_FLAG_CLIENT_CONNECTION_HEADER_SET))
+   {
+      return JB_ERR_OK;
+   }
+
+   log_error(LOG_LEVEL_HEADER, "Adding: %s", wanted_header);
+
+   return enlist(csp->headers, wanted_header);
+}
+
+
 /*********************************************************************
  *
  * Function    :  server_http
index d3c33cf..2305a2d 100644 (file)
--- a/project.h
+++ b/project.h
@@ -1,7 +1,7 @@
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
-#define PROJECT_H_VERSION "$Id: project.h,v 1.119 2008/09/20 10:04:33 fabiankeil Exp $"
+#define PROJECT_H_VERSION "$Id: project.h,v 1.120 2008/09/21 13:36:52 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
  *
  * Revisions   :
  *    $Log: project.h,v $
+ *    Revision 1.120  2008/09/21 13:36:52  fabiankeil
+ *    If change-x-forwarded-for{add} is used and the client
+ *    sends multiple X-Forwarded-For headers, append the client's
+ *    IP address to each one of them. "Traditionally" we would
+ *    lose all but the last one.
+ *
  *    Revision 1.119  2008/09/20 10:04:33  fabiankeil
  *    Remove hide-forwarded-for-headers action which has
  *    been obsoleted by change-x-forwarded-for{block}.
@@ -1288,10 +1294,10 @@ struct url_actions
 #define CSP_FLAG_TOGGLED_ON 0x20
 
 /**
- * Flag for csp->flags: Set if adding the 'Connection: close' header
- * for the client isn't necessary.
+ * Flag for csp->flags: Set if an acceptable Connection header
+ * is already set.
  */
-#define CSP_FLAG_CLIENT_CONNECTION_CLOSE_SET   0x00000040UL
+#define CSP_FLAG_CLIENT_CONNECTION_HEADER_SET   0x00000040UL
 
 /**
  * Flag for csp->flags: Set if adding the 'Connection: close' header
@@ -1324,6 +1330,23 @@ struct url_actions
  */
 #define CSP_FLAG_X_FORWARDED_FOR_APPENDED      0x00000800UL
 
+/**
+ * Flag for csp->flags: Set if the server wants to keep
+ * the connection alive.
+ *
+ * XXX: Incomplete implementation, we currently only
+ * look for "Connection: keep-alive".
+ */
+#define CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE  0x00001000UL
+
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+/**
+ * Flag for csp->flags: Set if the server specified the
+ * content length.
+ */
+#define CSP_FLAG_CONTENT_LENGTH_SET            0x00002000UL
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
 /*
  * Flags for use in return codes of child processes
  */
@@ -1404,6 +1427,14 @@ struct client_state
    /** Length after content modification. */
    size_t content_length;
 
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+   /** Expected length of content after which we
+    * should stop reading from the server socket.
+    */
+   /* XXX: is this the right location? */
+   size_t expected_content_length;
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+
 #ifdef FEATURE_TRUST
 
    /** Trust file. */