This modification to socket structures within Flood provides an in-place
solution to make check_socket() work properly for ssl (could not have
worked properly in the past for keepalive mode because check_socket() was
ignoring the ssl_socket_t level. By putting all the transactions with
sockets above the apr in a single structure, (a) I have eliminated this
and similar errors stemming from confusion regarding sublayers of socket
transactions, (b) paved the way for future additions to this socket
structure which may be needed at various sublevels, and (c) eliminated
multiple allocations of memory for a single transaction. An example of (b)
which has been already added to this structure is a linkage with the
request which is going out of the socket. This provides such information
as the profile's pool and the request's method, and we use it as well to
provide finer timing and time out capabilities at the socket level. This
eliminated the need to explicitly add the method field to the socket
structure (as it is already done in the request) which had been done to
support the "HEAD" requests in the socket level, and also eliminates the
need for a pool parameter to be passed to either the (ssl_) open_socket or
check_socket functions since it is available through the request which is
linked to the socket structure which is now passed to those "open"
functions.

flood_net.h now will import the structures from flood_net_ssl.h which
contains the unified socket structure flood_socket_t. The files
flood_net.c, flood_net_ssl.c, flood_socket_keepalive.c, and
flood_socket_generic.c now utilize this new socket structure and contain
the changes to implement it correctly. The linkage between the request and
socket is set up in the open_socket() function (and now in the
keepalive_begin_conn() function as well because it may not always call the
open_socket() function but is has to link the request).

Please examine the 6 attached diffs which outline these changes.

-Norman Tuttle, Developer, OpenDemand Systems [EMAIL PROTECTED]
--- flood_net.h 2003-10-15 15:28:32.000000000 -0400
+++ \flood-1.1\flood_net.h      2003-09-06 00:27:38.000000000 -0400
@@ -57,6 +57,22 @@
 #ifndef __flood_socket_h

 #define __flood_socket_h

 

-#include "flood_net_ssl.h"

+#include <apr_network_io.h> /* apr_socket_t */

+#include <apr_poll.h>       /* apr_pollfd_t */

+#include <apr_pools.h>      /* apr_pool_t */

+

+#include "flood_profile.h"

+

+typedef struct flood_socket_t {

+    apr_socket_t *socket;

+    apr_pollfd_t read_pollset;

+} flood_socket_t;

+

+flood_socket_t* open_socket(apr_pool_t *pool, request_t *r,

+                            apr_status_t *status);

+void close_socket(flood_socket_t *s);

+apr_status_t write_socket(flood_socket_t *s, request_t *r);

+apr_status_t read_socket(flood_socket_t *s, char *buf, int *buflen);

+apr_status_t check_socket(flood_socket_t *s, apr_pool_t *pool);

 

 #endif  /* __flood_socket_h */

--- flood_net_ssl.h     2003-10-15 16:34:41.000000000 -0400
+++ \flood-1.1\flood_net_ssl.h  2003-09-06 00:27:38.000000000 -0400
@@ -59,44 +59,14 @@
 

 #include <apr_network_io.h> /* apr_socket_t */

 #include <apr_pools.h>      /* apr_pool_t */

-#include <apr_poll.h>       /* apr_pollfd_t */

-#include "flood_profile.h"

 

-#if FLOOD_HAS_OPENSSL

-

-#define OPENSSL_THREAD_DEFINES

-#include <openssl/ssl.h>

-#include <openssl/err.h>

-#include <openssl/rand.h>

-

-#endif

-

-typedef struct flood_socket_t {

-    apr_socket_t *socket;

-    int wantresponse;  /* A boolean */

-    int ssl;           /* A boolean */

-    int available;     /* was boolean, now an enum */

-    apr_port_t port;   /* 16-bit uint, compare for keepalive */

-    char *hostname;    /* Needed to compare for keepalive */

-    apr_pollfd_t read_pollset;

-    request_t *request; /* Points to the request which was used to create the 
socket. */

-#if FLOOD_HAS_OPENSSL

-    SSL_CTX *ssl_context;

-    SSL *ssl_connection;

-#endif

-} flood_socket_t;

+typedef struct ssl_socket_t ssl_socket_t;

 

 apr_status_t ssl_init_socket(apr_pool_t *pool);

-apr_socket_t *ssl_open_socket(flood_socket_t *s, request_t *r,

+ssl_socket_t* ssl_open_socket(apr_pool_t *pool, request_t *r,

                               apr_status_t *status);

-void ssl_close_socket(flood_socket_t *s);

-apr_status_t ssl_write_socket(flood_socket_t *s, request_t *r);

-apr_status_t ssl_read_socket(flood_socket_t *s, char *buf, int *buflen);

-apr_socket_t *open_socket(flood_socket_t *s, request_t *r,

-                          apr_status_t *status);

-void close_socket(flood_socket_t *s);

-apr_status_t write_socket(flood_socket_t *s, request_t *r);

-apr_status_t read_socket(flood_socket_t *s, char *buf, int *buflen);

-apr_status_t check_socket(flood_socket_t *s);

+void ssl_close_socket(ssl_socket_t *s);

+apr_status_t ssl_write_socket(ssl_socket_t *s, request_t *r);

+apr_status_t ssl_read_socket(ssl_socket_t *s, char *buf, int *buflen);

 

 #endif  /* __flood_net_socket_h */

--- flood_net.c 2003-10-15 19:15:30.000000000 -0400
+++ \flood-1.1\flood_net.c      2003-09-09 05:49:50.000000000 -0400
@@ -59,16 +59,17 @@
 #include "flood_net.h"

 

 /* Open the TCP connection to the server */

-apr_socket_t* open_socket(flood_socket_t *s, request_t *r,

-                          apr_status_t *status)

+flood_socket_t* open_socket(apr_pool_t *pool, request_t *r,

+                            apr_status_t *status)

 {

-    apr_pool_t *pool = r->pool;

     apr_status_t rv = 0;

     apr_sockaddr_t *destsa;

+    flood_socket_t* fs;

+    

+    fs = apr_palloc(pool, sizeof(flood_socket_t));

 

-    s->request = r; /* Link the request structure to the socket. */

-    if ((rv = apr_sockaddr_info_get(&destsa, r->parsed_uri->hostname, APR_INET,

-                                    r->parsed_uri->port, 0, pool))

+    if ((rv = apr_sockaddr_info_get(&destsa, r->parsed_uri->hostname, 
APR_INET, 

+                                    r->parsed_uri->port, 0, pool)) 

                                     != APR_SUCCESS) {

         if (status) {

             *status = rv;

@@ -76,7 +77,7 @@
         return NULL;

     }

 

-    if ((rv = apr_socket_create(&s->socket, APR_INET, SOCK_STREAM,

+    if ((rv = apr_socket_create(&fs->socket, APR_INET, SOCK_STREAM,

                                 APR_PROTO_TCP, pool)) != APR_SUCCESS) {

         if (status) {

             *status = rv;

@@ -84,10 +85,10 @@
         return NULL;

     }

 

-    if ((rv = apr_socket_connect(s->socket, destsa)) != APR_SUCCESS) {

+    if ((rv = apr_socket_connect(fs->socket, destsa)) != APR_SUCCESS) {

         if (APR_STATUS_IS_EINPROGRESS(rv)) {

             /* FIXME: Handle better */

-            close_socket(s);

+            close_socket(fs);

             if (status) {

                 *status = rv;

             }

@@ -96,17 +97,17 @@
         else if (APR_STATUS_IS_EAGAIN(rv))

         {

             /* We have run out of ports available due to TIME_WAIT exhaustion.

-             * Sleep for four minutes, and try again.

+             * Sleep for four minutes, and try again. 

              * Note: Solaris returns EADDRNOTAVAIL, Linux returns EAGAIN.

              * XXX: Then APR'IZE THIS ALREADY

              */

             apr_sleep(4 * 60 * APR_USEC_PER_SEC);

-            return open_socket(s, r, status);

+            return open_socket(pool, r, status);

         }

         else

         {

             /* FIXME: Handle */

-            close_socket(s);

+            close_socket(fs);

             if (status) {

                 *status = rv;

             }

@@ -114,13 +115,13 @@
         }

     }

 

-    apr_socket_opt_set(s->socket, APR_SO_TIMEOUT, LOCAL_SOCKET_TIMEOUT);

-    s->read_pollset.desc_type = APR_POLL_SOCKET;

-    s->read_pollset.desc.s = s->socket;

-    s->read_pollset.reqevents = APR_POLLIN;

-    s->read_pollset.p = pool;

-

-    return s->socket;

+    apr_socket_opt_set(fs->socket, APR_SO_TIMEOUT, LOCAL_SOCKET_TIMEOUT);

+    fs->read_pollset.desc_type = APR_POLL_SOCKET;

+    fs->read_pollset.desc.s = fs->socket;

+    fs->read_pollset.reqevents = APR_POLLIN;

+    fs->read_pollset.p = pool;

+    

+    return fs;

 }

 

 /* close down TCP socket */

@@ -157,20 +158,22 @@
     return e;

 }

 

-apr_status_t check_socket(flood_socket_t *s)

+apr_status_t check_socket(flood_socket_t *s, apr_pool_t *pool)

 {

+    apr_status_t e;

     apr_int32_t socketsRead;

     apr_pollfd_t pout;

+    apr_int16_t event;

 

     pout.desc_type = APR_POLL_SOCKET;

     pout.desc.s = s->socket;

     pout.reqevents = APR_POLLIN | APR_POLLPRI | APR_POLLERR | APR_POLLHUP | 
APR_POLLNVAL;

-    pout.p = s->request->pool;

-

-    apr_poll(&pout, 1, &socketsRead, 1000);

+    pout.p = pool;

+    

+    e = apr_poll(&pout, 1, &socketsRead, 1000);

     if (socketsRead && pout.rtnevents) {

         return APR_EGENERAL;

     }

-

+    

     return APR_SUCCESS;

 }

--- flood_net_ssl.c     2003-10-15 17:06:52.000000000 -0400
+++ \flood-1.1\flood_net_ssl.c  2003-10-08 19:25:02.000000000 -0400
@@ -68,13 +68,24 @@
 

 #if FLOOD_HAS_OPENSSL

 

+#define OPENSSL_THREAD_DEFINES

+#include <openssl/ssl.h>

+#include <openssl/err.h>

+#include <openssl/rand.h>

+

+struct ssl_socket_t {

+    SSL_CTX *ssl_context;

+    SSL *ssl_connection;

+    flood_socket_t *socket;

+};

+

 apr_pool_t *ssl_pool;

 

 #if APR_HAS_THREADS

 apr_thread_mutex_t **ssl_locks;

 

-typedef struct CRYPTO_dynlock_value {

-    apr_thread_mutex_t *lock;

+typedef struct CRYPTO_dynlock_value { 

+    apr_thread_mutex_t *lock; 

 } CRYPTO_dynlock_value;

 

 static CRYPTO_dynlock_value *ssl_dyn_create(const char* file, int line)

@@ -90,7 +101,7 @@
     return l;

 }

 

-static void ssl_dyn_lock(int mode, CRYPTO_dynlock_value *l, const char *file,

+static void ssl_dyn_lock(int mode, CRYPTO_dynlock_value *l, const char *file, 

                          int line)

 {

     if (mode & CRYPTO_LOCK) {

@@ -120,7 +131,7 @@
 static unsigned long ssl_id(void)

 {

     /* FIXME: This is lame and not portable. -aaron */

-    return (unsigned long) apr_os_thread_current();

+    return (unsigned long) apr_os_thread_current(); 

 }

 #endif

 

@@ -199,42 +210,46 @@
     return APR_SUCCESS;

 }

 

-void ssl_read_socket_handshake(flood_socket_t *s);

+void ssl_read_socket_handshake(ssl_socket_t *s);

 

-apr_socket_t* ssl_open_socket(flood_socket_t *s, request_t *r,

-                              apr_status_t *status)

+ssl_socket_t* ssl_open_socket(apr_pool_t *pool, request_t *r,

+                              apr_status_t *status) 

 {

     apr_os_sock_t ossock;

     int e, sslError;

 

+    ssl_socket_t *ssl_socket = apr_pcalloc(pool, sizeof(ssl_socket_t));

+

     /* Open our TCP-based connection */

-    if (!open_socket(s, r, status))

+    ssl_socket->socket = open_socket(pool, r, status);

+    

+    if (!ssl_socket->socket)

         return NULL;

 

     /* Get the native OS socket. */

-    apr_os_sock_get(&ossock, s->socket);

+    apr_os_sock_get(&ossock, ssl_socket->socket->socket);

 

     /* Create a local context */

-    s->ssl_context = SSL_CTX_new(SSLv23_client_method());

-    SSL_CTX_set_options(s->ssl_context, SSL_OP_ALL);

+    ssl_socket->ssl_context = SSL_CTX_new(SSLv23_client_method());

+    SSL_CTX_set_options(ssl_socket->ssl_context, SSL_OP_ALL);

 #ifdef SSL_MODE_AUTO_RETRY

     /* Not all OpenSSL versions support this. */

-    SSL_CTX_set_options(s->ssl_context, SSL_MODE_AUTO_RETRY);

+    SSL_CTX_set_options(ssl_socket->ssl_context, SSL_MODE_AUTO_RETRY);

 #endif

-    /*SSL_CTX_set_default_verify_paths(s->ssl_context);*/

-    SSL_CTX_load_verify_locations(s->ssl_context, NULL, CAPATH);

+    /*SSL_CTX_set_default_verify_paths(ssl_socket->ssl_context);*/

+    SSL_CTX_load_verify_locations(ssl_socket->ssl_context, NULL, CAPATH);

 

     /* Initialize the SSL connection */

-    ssl_socket->ssl_connection = SSL_new(s->ssl_context);

-    SSL_set_connect_state(s->ssl_connection);

+    ssl_socket->ssl_connection = SSL_new(ssl_socket->ssl_context);

+    SSL_set_connect_state(ssl_socket->ssl_connection);

 

     /* Set the descriptors */

-    SSL_set_fd(s->ssl_connection, ossock);

-    e = SSL_connect(s->ssl_connection);

+    SSL_set_fd(ssl_socket->ssl_connection, ossock);

+    e = SSL_connect(ssl_socket->ssl_connection);

 

     if (e)

     {

-        sslError = SSL_get_error(s->ssl_connection, e);

+        sslError = SSL_get_error(ssl_socket->ssl_connection, e);

 

         switch (sslError)

         {

@@ -245,22 +260,22 @@
             break;

         default:

             ERR_print_errors_fp(stderr);

-            return NULL;

+            return NULL; 

         }

     }

 

-    return s->socket;

+    return ssl_socket;

 }

 

 /* close down TCP socket */

-void ssl_close_socket(flood_socket_t *s)

+void ssl_close_socket(ssl_socket_t *s)

 {

     SSL_free(s->ssl_connection);

     SSL_CTX_free(s->ssl_context);

-    close_socket(s);

+    close_socket(s->socket);

 }

 

-apr_status_t ssl_read_socket(flood_socket_t *s, char *buf, int *buflen)

+apr_status_t ssl_read_socket(ssl_socket_t *s, char *buf, int *buflen)

 {

     apr_status_t e;

     int sslError;

@@ -268,7 +283,7 @@
 

     /* Wait until there is something to read. */

     if (SSL_pending(s->ssl_connection) < *buflen) {

-        e = apr_poll(&s->read_pollset, 1, &socketsRead,

+        e = apr_poll(&s->socket->read_pollset, 1, &socketsRead,

                      LOCAL_SOCKET_TIMEOUT);

 

         if (socketsRead != 1)

@@ -287,34 +302,34 @@
         ssl_read_socket(s, buf, buflen);

         break;

     case SSL_ERROR_ZERO_RETURN: /* Peer closed connection. */

-        return APR_EOF;

+        return APR_EOF; 

     case SSL_ERROR_SYSCALL: /* Look at errno. */

         if (errno == 0)

             return APR_EOF;

-        /* Continue through with the error case. */

+        /* Continue through with the error case. */   

     case SSL_ERROR_WANT_WRITE:  /* Technically, not an error. */

     default:

         ERR_print_errors_fp(stderr);

-        return APR_EGENERAL;

+        return APR_EGENERAL; 

     }

 

     return APR_SUCCESS;

 }

 

-void ssl_read_socket_handshake(flood_socket_t *s)

+void ssl_read_socket_handshake(ssl_socket_t *s)

 {

     char buf[1];

-    int buflen = 1;

+    int buflen = 1; 

     /* Wait until there is something to read. */

     apr_int32_t socketsRead;

     apr_status_t e;

-    e = apr_poll(&s->read_pollset, 1, &socketsRead,

+    e = apr_poll(&s->socket->read_pollset, 1, &socketsRead,

                  LOCAL_SOCKET_TIMEOUT);

     e = SSL_read(s->ssl_connection, buf, buflen);

 }

 

 /* Write to the socket */

-apr_status_t ssl_write_socket(flood_socket_t *s, request_t *r)

+apr_status_t ssl_write_socket(ssl_socket_t *s, request_t *r)

 {

     apr_status_t e;

     int sslError;

@@ -335,10 +350,10 @@
         break;

     default:

         ERR_print_errors_fp(stderr);

-        return APR_EGENERAL;

+        return APR_EGENERAL; 

     }

 

-    return APR_SUCCESS;

+    return APR_SUCCESS;     

 }

 

 #else /* FLOOD_HAS_OPENSSL */

@@ -348,22 +363,22 @@
     return APR_ENOTIMPL;

 }

 

-apr_socket_t* ssl_open_socket(apr_pool_t *pool, request_t *r,

+ssl_socket_t* ssl_open_socket(apr_pool_t *pool, request_t *r,

                               apr_status_t *status)

 {

     return NULL;

 }

 

-void ssl_close_socket(flood_socket_t *s)

+void ssl_close_socket(ssl_socket_t *s)

 {

 }

 

-apr_status_t ssl_write_socket(flood_socket_t *s, request_t *r)

+apr_status_t ssl_write_socket(ssl_socket_t *s, request_t *r)

 {

     return APR_ENOTIMPL;

 }

 

-apr_status_t ssl_read_socket(flood_socket_t *s, char *buf, int *buflen)

+apr_status_t ssl_read_socket(ssl_socket_t *s, char *buf, int *buflen)

 {

     return APR_ENOTIMPL;

 }

--- flood_socket_generic.c      2003-10-15 19:20:16.000000000 -0400
+++ \flood-1.1\flood_socket_generic.c   2003-09-06 00:27:38.000000000 -0400
@@ -66,14 +66,20 @@
 #include "flood_net_ssl.h"

 #include "flood_socket_generic.h"

 

+typedef struct {

+    void *s;

+    int wantresponse;   /* A boolean */

+    int ssl;            /* A boolean */

+} generic_socket_t;

+

 apr_status_t generic_socket_init(socket_t **sock, apr_pool_t *pool)

 {

-    flood_socket_t *new_gsock;

+    generic_socket_t *new_gsock;

 

-    new_gsock = (flood_socket_t *)apr_palloc(pool, sizeof(flood_socket_t));

+    new_gsock = (generic_socket_t *)apr_palloc(pool, sizeof(generic_socket_t));

     if (new_gsock == NULL)

         return APR_ENOMEM;

-    new_gsock->socket = NULL;

+    new_gsock->s = NULL;

 

     *sock = new_gsock;

     return APR_SUCCESS;

@@ -85,7 +91,7 @@
 apr_status_t generic_begin_conn(socket_t *sock, request_t *req, apr_pool_t 
*pool)

 {

     apr_status_t rv;

-    flood_socket_t *gsock = (flood_socket_t *)sock;

+    generic_socket_t *gsock = (generic_socket_t *)sock;

 

     if (strcasecmp(req->parsed_uri->scheme, "https") == 0) {

         /* If we don't have SSL, error out. */

@@ -102,11 +108,11 @@
     /* The return types are not identical, so it can't be a ternary

      * operation. */

     if (gsock->ssl)

-        ssl_open_socket(gsock, req, &rv);

+        gsock->s = ssl_open_socket(pool, req, &rv);

     else

-        open_socket(gsock, req, &rv);

+        gsock->s = open_socket(pool, req, &rv);

 

-    if (gsock->socket == NULL)

+    if (gsock->s == NULL)

         return rv;

 

     req->keepalive = 0; /* FIXME: Maybe move this into flood_socket_t */

@@ -118,10 +124,10 @@
  */

 apr_status_t generic_send_req(socket_t *sock, request_t *req, apr_pool_t *pool)

 {

-    flood_socket_t *gsock = (flood_socket_t *)sock;

+    generic_socket_t *gsock = (generic_socket_t *)sock;

     gsock->wantresponse = req->wantresponse;

-    return gsock->ssl ? ssl_write_socket(gsock, req) :

-                        write_socket(gsock, req);

+    return gsock->ssl ? ssl_write_socket(gsock->s, req) :

+                        write_socket(gsock->s, req);

 }

 

 /**

@@ -134,7 +140,7 @@
     response_t *new_resp;

     apr_status_t status;

 

-    flood_socket_t *gsock = (flood_socket_t *)sock;

+    generic_socket_t *gsock = (generic_socket_t *)sock;

 

     new_resp = apr_pcalloc(pool, sizeof(response_t));

     new_resp->rbuftype = POOL;

@@ -152,8 +158,8 @@
         do

         {

             i = MAX_DOC_LENGTH - 1;

-            status = gsock->ssl ? ssl_read_socket(gsock, b, &i)

-                                : read_socket(gsock, b, &i);

+            status = gsock->ssl ? ssl_read_socket(gsock->s, b, &i)

+                                : read_socket(gsock->s, b, &i);

             if (new_resp->rbufsize + i > currentalloc)

             {

                 /* You can think why this always work. */

@@ -163,7 +169,7 @@
                 memcpy(new_resp->rbuf, op, cp - op);

                 cp = new_resp->rbuf + (cp - op);

             }

-

+            

             memcpy(cp, b, i);

             new_resp->rbufsize += i;

             cp += i;

@@ -175,15 +181,15 @@
         /* We just want to store the first chunk read. */

         new_resp->rbufsize = MAX_DOC_LENGTH - 1;

         new_resp->rbuf = apr_palloc(pool, new_resp->rbufsize);

-        status = gsock->ssl ? ssl_read_socket(gsock, new_resp->rbuf,

+        status = gsock->ssl ? ssl_read_socket(gsock->s, new_resp->rbuf, 

                                               &new_resp->rbufsize) :

-                              read_socket(gsock, new_resp->rbuf,

+                              read_socket(gsock->s, new_resp->rbuf, 

                                           &new_resp->rbufsize);

 

         while (status != APR_EOF && status != APR_TIMEUP) {

             i = MAX_DOC_LENGTH - 1;

-            status = gsock->ssl ? ssl_read_socket(gsock, b, &i) :

-                                  read_socket(gsock, b, &i);

+            status = gsock->ssl ? ssl_read_socket(gsock->s, b, &i) :

+                                  read_socket(gsock->s, b, &i);

         }

         if (status != APR_SUCCESS && status != APR_EOF) {

             return status;

@@ -205,7 +211,7 @@
                                         socket_t *sock,

                                         apr_pool_t *pool)

 {

-    flood_socket_t *gsock = (flood_socket_t *)sock;

+    generic_socket_t *gsock = (generic_socket_t *)sock;

     int orig_wantresponse   = gsock->wantresponse;

     apr_status_t status;

 

@@ -221,8 +227,8 @@
  */

 apr_status_t generic_end_conn(socket_t *sock, request_t *req, response_t *resp)

 {

-    flood_socket_t *gsock = (flood_socket_t *)sock;

-    gsock->ssl ? ssl_close_socket(gsock) : close_socket(gsock);

+    generic_socket_t *gsock = (generic_socket_t *)sock;

+    gsock->ssl ? ssl_close_socket(gsock->s) : close_socket(gsock->s);

     return APR_SUCCESS;

 }

 

--- flood_socket_keepalive.c    2003-10-15 19:06:44.000000000 -0400
+++ \flood-1.1\flood_socket_keepalive.c 2003-09-06 00:27:38.000000000 -0400
@@ -69,25 +69,35 @@
 #include "flood_socket_keepalive.h"

 

 #define ksock_read_socket(ksock, buf, lenaddr) \

-    ksock->ssl ? ssl_read_socket(ksock, buf, lenaddr) : \

-                 read_socket(ksock, buf, lenaddr)

+    ksock->ssl ? ssl_read_socket(ksock->s, buf, lenaddr) : \

+                 read_socket(ksock->s, buf, lenaddr)

 

 #define ksock_write_socket(ksock, req) \

-    ksock->ssl ? ssl_write_socket(ksock, req) : \

-                 write_socket(ksock, req)

+    ksock->ssl ? ssl_write_socket(ksock->s, req) : \

+                 write_socket(ksock->s, req)

+

+typedef struct {

+    void *s;

+    apr_pollfd_t *p;

+    int reopen_socket; /* A boolean */

+    int wantresponse;  /* A boolean */

+    int ssl;           /* A boolean */

+    method_e method;   /* The method of the request. */

+} keepalive_socket_t;

 

 /**

  * Keep-alive implementation for socket_init.

  */

 apr_status_t keepalive_socket_init(socket_t **sock, apr_pool_t *pool)

 {

-    flood_socket_t *new_ksock;

+    keepalive_socket_t *new_ksock;

 

-    new_ksock = (flood_socket_t *)apr_palloc(pool, sizeof(flood_socket_t));

+    new_ksock = (keepalive_socket_t *)apr_palloc(pool, 
sizeof(keepalive_socket_t));

     if (new_ksock == NULL)

         return APR_ENOMEM;

-    new_ksock->socket = NULL;

-    new_ksock->available = 1;

+    new_ksock->s = NULL;

+    new_ksock->p = NULL;

+    new_ksock->reopen_socket = 1;

     new_ksock->wantresponse = 1;

     new_ksock->ssl = 0;

 

@@ -100,16 +110,16 @@
  */

 apr_status_t keepalive_begin_conn(socket_t *sock, request_t *req, apr_pool_t 
*pool)

 {

-    flood_socket_t *ksock = (flood_socket_t *)sock;

+    keepalive_socket_t *ksock = (keepalive_socket_t *)sock;

 

-    if (!ksock->available && ksock->socket) {

+    if (!ksock->reopen_socket && ksock->s) {

         apr_status_t e;

-        e = check_socket(ksock);

+        e = check_socket(ksock->s, pool);

         if (e != APR_SUCCESS) {

-            ksock->available = 1;

+            ksock->reopen_socket = 1;

         }

     }

-    if (ksock->available || ksock->socket == NULL) {

+    if (ksock->reopen_socket || ksock->s == NULL) {

         apr_status_t rv;

         if (strcasecmp(req->parsed_uri->scheme, "https") == 0) {

         /* If we don't have SSL, error out. */

@@ -126,16 +136,15 @@
         /* The return types are not identical, so it can't be a ternary

          * operation. */

         if (ksock->ssl)

-            ssl_open_socket(ksock, req, &rv);

+            ksock->s = ssl_open_socket(pool, req, &rv);

         else

-            open_socket(ksock, req, &rv);

+            ksock->s = open_socket(pool, req, &rv);

 

-        if (ksock->socket == NULL)

+        if (ksock->s == NULL)

             return rv;

 

-        ksock->available = 0; /* we just opened it */

+        ksock->reopen_socket = 0; /* we just opened it */

     }

-    ksock->request = req;

     req->keepalive = 1;

     return APR_SUCCESS;

 }

@@ -145,10 +154,11 @@
  */

 apr_status_t keepalive_send_req(socket_t *sock, request_t *req, apr_pool_t 
*pool)

 {

-    flood_socket_t *ksock = (flood_socket_t *)sock;

+    keepalive_socket_t *ksock = (keepalive_socket_t *)sock;

     ksock->wantresponse = req->wantresponse;

-    return ksock->ssl ? ssl_write_socket(ksock, req) :

-                        write_socket(ksock, req);

+    ksock->method = req->method;

+    return ksock->ssl ? ssl_write_socket(ksock->s, req) :

+                        write_socket(ksock->s, req);

 }

 

 static long keepalive_read_chunk_size(char *begin_chunk)

@@ -173,7 +183,7 @@
 }

 

 static apr_status_t keepalive_read_chunk(response_t *resp,

-                                         flood_socket_t *sock,

+                                         keepalive_socket_t *sock,

                                          int chunk_length,

                                          char **bp, int bplen)

 {

@@ -231,7 +241,7 @@
                     return status;

                 }

 

-                /* We got caught in the middle of a chunk last time. */

+                /* We got caught in the middle of a chunk last time. */ 

                 if (old_length < 0) {

                     b -= old_length;

                     blen += old_length;

@@ -297,8 +307,8 @@
     return APR_SUCCESS;

 }

 

-static apr_status_t keepalive_load_resp(response_t *resp,

-                                        flood_socket_t *sock,

+static apr_status_t keepalive_load_resp(response_t *resp, 

+                                        keepalive_socket_t *sock,

                                         apr_size_t remaining, apr_pool_t *pool)

 {

     /* Ugh, we want everything. */

@@ -350,7 +360,7 @@
         cp += i;

         remaining -= i;

     }

-    while (status != APR_EGENERAL && status != APR_EOF &&

+    while (status != APR_EGENERAL && status != APR_EOF && 

            status != APR_TIMEUP && (!remain || remaining));

 

     return status;

@@ -361,7 +371,7 @@
  */

 apr_status_t keepalive_recv_resp(response_t **resp, socket_t *sock, apr_pool_t 
*pool)

 {

-    flood_socket_t *ksock = (flood_socket_t *)sock;

+    keepalive_socket_t *ksock = (keepalive_socket_t *)sock;

     char *cl, *ecl, cls[17];

     char *current_line;

     int i;

@@ -397,7 +407,7 @@
         header_end = memchr(current_line, ':', line_length);

         if (header_end) {

             key_length = header_end - current_line;

-

+ 

             header_key = apr_pstrmemdup(pool, current_line, key_length);

             header_val = apr_pstrmemdup(pool, current_line + key_length + 2,

                                         line_length - key_length - 2);

@@ -410,14 +420,14 @@
     /* If this exists, we aren't keepalive anymore. */

     header = apr_table_get(new_resp->headers, "Connection");

     if (header && !strcasecmp(header, "Close")) {

-        new_resp->keepalive = 0;

+        new_resp->keepalive = 0; 

     }

     else {

-        new_resp->keepalive = 1;

+        new_resp->keepalive = 1; 

     }

 

     /* If we have a HEAD request, we shouldn't be receiving a body. */

-    if (ksock->request->method == HEAD) {

+    if (ksock->method == HEAD) {

         *resp = new_resp;

 

         return APR_SUCCESS;

@@ -439,7 +449,7 @@
         /* We have a partial chunk and we aren't at the end. */

         if (cl && *cl && (cl - (char*)new_resp->rbuf) < new_resp->rbufsize) {

             int remaining;

-

+    

             do {

                 if (new_resp->chunk) {

                     /* If we have enough space to skip over the ending CRLF,

@@ -466,8 +476,8 @@
                     foo = strstr(new_resp->chunk, CRLF);

                     assert(foo);

                     new_resp->chunk = foo + 2;

-                    remaining = new_resp->rbufsize -

-                                    (int)(new_resp->chunk -

+                    remaining = new_resp->rbufsize - 

+                                    (int)(new_resp->chunk - 

                                           (char*)new_resp->rbuf);

                 }

                 else {

@@ -485,7 +495,7 @@
         header = apr_table_get(new_resp->headers, "Content-Length");

         if (!header)

         {

-            new_resp->keepalive = 0;

+            new_resp->keepalive = 0; 

         }

 

         if (header)

@@ -498,7 +508,7 @@
                 cls[ecl-cl] = '\0';

                 content_length = strtol(cls, &ecl, 10);

                 if (*ecl != '\0')

-                    new_resp->keepalive = 0;

+                    new_resp->keepalive = 0; 

             }

         }

 

@@ -509,14 +519,14 @@
 

             /* We didn't get full headers.  Crap. */

             if (!ecl)

-                new_resp->keepalive = 0;

+                new_resp->keepalive = 0; 

             {

                 ecl += sizeof(CRLF) - 1;

                 content_length -= new_resp->rbufsize - (ecl - 
(char*)new_resp->rbuf);

-            }

+            } 

         }

     }

-

+   

     if (ksock->wantresponse)

     {

         if (new_resp->keepalive)

@@ -548,7 +558,7 @@
         }

         else

         {

-            while (status != APR_EGENERAL && status != APR_EOF &&

+            while (status != APR_EGENERAL && status != APR_EOF && 

                    status != APR_TIMEUP) {

                 i = MAX_DOC_LENGTH - 1;

                 status = ksock_read_socket(ksock, b, &i);

@@ -566,13 +576,13 @@
  */

 apr_status_t keepalive_end_conn(socket_t *sock, request_t *req, response_t 
*resp)

 {

-    flood_socket_t *ksock = (flood_socket_t *)sock;

+    keepalive_socket_t *ksock = (keepalive_socket_t *)sock;

 

     if (resp->keepalive == 0) {

-        ksock->ssl ? ssl_close_socket(ksock) : close_socket(ksock);

-        ksock->available = 1; /* we just closed it */

+        ksock->ssl ? ssl_close_socket(ksock->s) : close_socket(ksock->s);

+        ksock->reopen_socket = 1; /* we just closed it */

     }

-

+        

     return APR_SUCCESS;

 }

 

Reply via email to