]> granicus.if.org Git - apache/commitdiff
Merge r1481891, r1482075, r1482170, r1482555 from trunk:
authorJim Jagielski <jim@apache.org>
Thu, 11 Jul 2013 12:21:19 +0000 (12:21 +0000)
committerJim Jagielski <jim@apache.org>
Thu, 11 Jul 2013 12:21:19 +0000 (12:21 +0000)
mod_proxy: Ensure we don't attempt to amend a table we are iterating
through, ensuring that all headers listed by Connection are removed.

mod_proxy, mod_proxy_http: Connection headers must be stripped on the way
in and out, support an optional function to handle this.

hunk 1: C89 please;
hunk 2: optional functions are usually declared static.

mod_proxy: Make sure we skip empty tokens when parsing the Connection
header.

Submitted by: minfrin, fuankg, minfrin
Reviewed/backported by: jim

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1502201 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
STATUS
include/ap_mmn.h
modules/proxy/mod_proxy.h
modules/proxy/mod_proxy_http.c
modules/proxy/proxy_util.c

diff --git a/CHANGES b/CHANGES
index dce53279f60b23709cde7892a2b4c6093d2431b9..8fdc8a6ae9b94ff4a73481d71f8a877bc9bc629c 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -8,6 +8,10 @@ Changes with Apache 2.4.5
      URI that is not configured for DAV will trigger a segfault. [Ben Reser
      <ben reser.org>]
 
+  *) mod_proxy: Ensure we don't attempt to amend a table we are iterating
+     through, ensuring that all headers listed by Connection are removed.
+     [Graham Leggett, Co-Advisor <coad measurement-factory.com>]
+
   *) mod_proxy_http: Make the proxy-interim-response environment variable
      effective by formally overriding origin server behaviour. [Graham
      Leggett, Co-Advisor <coad measurement-factory.com>]
diff --git a/STATUS b/STATUS
index 430b32d66c702748d622571b34900b340e646166..9d9cca77def121c16782ce9ff6c95307141b5849 100644 (file)
--- a/STATUS
+++ b/STATUS
@@ -90,14 +90,6 @@ RELEASE SHOWSTOPPERS:
 PATCHES ACCEPTED TO BACKPORT FROM TRUNK:
   [ start all new proposals below, under PATCHES PROPOSED. ]
 
-  * mod_proxy: Connection header clearing issues
-    trunk patch: https://svn.apache.org/viewvc?view=revision&revision=1481891
-                 https://svn.apache.org/viewvc?view=revision&revision=1482075
-                 https://svn.apache.org/viewvc?view=revision&revision=1482170
-                 https://svn.apache.org/viewvc?view=revision&revision=1482555
-    2.4.x patch: trunk patch works modulo CHANGES and ap_mmn
-    +1: jim, druggeri, wrowe
-
   * Some easy votes to keep 2.4 in line with trunk
     - mod_slotmem_shm: Replace duplicate log tags
     - mpm/event: Check that AsyncRequestWorkerFactor is not negative (PR 54254)
index 319c72a2c76510a7469dfa2798d449114c7a3400..89c41409f3d013f19053ec4a4f5030980b3ef846 100644 (file)
  * 20120211.21 (2.4.5-dev) Add in ap_proxy_create_hdrbrgd() and
  *                         ap_proxy_pass_brigade()
  * 20120211.22 (2.4.5-dev) No longer prevent usage of strtoul()
+ * 20120211.23 (2.4.5-dev) Add ap_proxy_clear_connection()
  */
 
 #define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */
 #ifndef MODULE_MAGIC_NUMBER_MAJOR
 #define MODULE_MAGIC_NUMBER_MAJOR 20120211
 #endif
-#define MODULE_MAGIC_NUMBER_MINOR 22                   /* 0...n */
+#define MODULE_MAGIC_NUMBER_MINOR 23                   /* 0...n */
 
 /**
  * Determine if the server's current MODULE_MAGIC_NUMBER is at least a
index af6c2cbf4732f52fc3a59421f433efa68f82964a..81fd14c1737c446f6f618ce9a7837235ac161d7f 100644 (file)
@@ -960,6 +960,16 @@ PROXY_DECLARE(int) ap_proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc,
                                          conn_rec *origin, apr_bucket_brigade *bb,
                                          int flush);
 
+/**
+ * Clear the headers referenced by the Connection header from the given
+ * table, and remove the Connection header.
+ * @param r request
+ * @param headers table of headers to clear
+ * @return 1 if "close" was present, 0 otherwise.
+ */
+APR_DECLARE_OPTIONAL_FN(int, ap_proxy_clear_connection,
+        (request_rec *r, apr_table_t *headers));
+
 #define PROXY_LBMETHOD "proxylbmethod"
 
 /* The number of dynamic workers that can be added when reconfiguring.
index f2498b693f1383058d8e1b765e8a2ceb89ab9a8b..cffad2e70aabbdc2aed588c278e3415dfb0acce8 100644 (file)
@@ -21,6 +21,9 @@
 
 module AP_MODULE_DECLARE_DATA proxy_http_module;
 
+static int (*ap_proxy_clear_connection_fn)(request_rec *r, apr_table_t *headers) =
+        NULL;
+
 static apr_status_t ap_proxy_http_cleanup(const char *scheme,
                                           request_rec *r,
                                           proxy_conn_rec *backend);
@@ -178,33 +181,7 @@ static apr_table_t *ap_proxy_clean_warnings(apr_pool_t *p, apr_table_t *headers)
         return headers;
    }
 }
-static int clear_conn_headers(void *data, const char *key, const char *val)
-{
-    apr_table_t *headers = ((header_dptr*)data)->table;
-    apr_pool_t *pool = ((header_dptr*)data)->pool;
-    const char *name;
-    char *next = apr_pstrdup(pool, val);
-    while (*next) {
-        name = next;
-        while (*next && !apr_isspace(*next) && (*next != ',')) {
-            ++next;
-        }
-        while (*next && (apr_isspace(*next) || (*next == ','))) {
-            *next++ = '\0';
-        }
-        apr_table_unset(headers, name);
-    }
-    return 1;
-}
-static void ap_proxy_clear_connection(apr_pool_t *p, apr_table_t *headers)
-{
-    header_dptr x;
-    x.pool = p;
-    x.table = headers;
-    apr_table_unset(headers, "Proxy-Connection");
-    apr_table_do(clear_conn_headers, &x, headers, "Connection", NULL);
-    apr_table_unset(headers, "Connection");
-}
+
 static void add_te_chunked(apr_pool_t *p,
                            apr_bucket_alloc_t *bucket_alloc,
                            apr_bucket_brigade *header_brigade)
@@ -1491,11 +1468,10 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
              * ap_http_filter to know where to end.
              */
             te = apr_table_get(r->headers_out, "Transfer-Encoding");
+
             /* strip connection listed hop-by-hop headers from response */
-            if (ap_find_token(p, apr_table_get(r->headers_out, "Connection"),
-                              "close"))
-                backend->close = 1;
-            ap_proxy_clear_connection(p, r->headers_out);
+            backend->close = ap_proxy_clear_connection_fn(r, r->headers_out);
+
             if ((buf = apr_table_get(r->headers_out, "Content-Type"))) {
                 ap_set_content_type(r, apr_pstrdup(p, buf));
             }
@@ -1507,6 +1483,7 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
             for (i=0; hop_by_hop_hdrs[i]; ++i) {
                 apr_table_unset(r->headers_out, hop_by_hop_hdrs[i]);
             }
+
             /* Delete warnings with wrong date */
             r->headers_out = ap_proxy_clean_warnings(p, r->headers_out);
 
@@ -2035,8 +2012,34 @@ cleanup:
     }
     return status;
 }
+
+/* post_config hook: */
+static int proxy_http_post_config(apr_pool_t *pconf, apr_pool_t *plog,
+        apr_pool_t *ptemp, server_rec *s)
+{
+
+    /* proxy_http_post_config() will be called twice during startup.  So, don't
+     * set up the static data the 1st time through. */
+    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) {
+        return OK;
+    }
+
+    if (!ap_proxy_clear_connection_fn) {
+        ap_proxy_clear_connection_fn =
+                APR_RETRIEVE_OPTIONAL_FN(ap_proxy_clear_connection);
+        if (!ap_proxy_clear_connection_fn) {
+            ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02477)
+                         "mod_proxy must be loaded for mod_proxy_http");
+            return !OK;
+        }
+    }
+
+    return OK;
+}
+
 static void ap_proxy_http_register_hook(apr_pool_t *p)
 {
+    ap_hook_post_config(proxy_http_post_config, NULL, NULL, APR_HOOK_MIDDLE);
     proxy_hook_scheme_handler(proxy_http_handler, NULL, NULL, APR_HOOK_FIRST);
     proxy_hook_canon_handler(proxy_http_canon, NULL, NULL, APR_HOOK_FIRST);
     warn_rx = ap_pregcomp(p, "[0-9]{3}[ \t]+[^ \t]+[ \t]+\"[^\"]*\"([ \t]+\"([^\"]+)\")?", 0);
index 1a174cc5b824e5e64fb6bd388aa719c0826a9e57..67dc93943bd8ee1e7e63a0d72389ca1721523498 100644 (file)
@@ -2844,45 +2844,71 @@ PROXY_DECLARE(proxy_balancer_shared *) ap_proxy_find_balancershm(ap_slotmem_prov
     return NULL;
 }
 
-void proxy_util_register_hooks(apr_pool_t *p)
-{
-    APR_REGISTER_OPTIONAL_FN(ap_proxy_retry_worker);
-}
-
-/* Clear all connection-based headers from the incoming headers table */
-typedef struct header_dptr {
+typedef struct header_connection {
     apr_pool_t *pool;
-    apr_table_t *table;
-    apr_time_t time;
-} header_dptr;
+    apr_array_header_t *array;
+    const char *first;
+    unsigned int closed:1;
+} header_connection;
 
-static int clear_conn_headers(void *data, const char *key, const char *val)
+static int find_conn_headers(void *data, const char *key, const char *val)
 {
-    apr_table_t *headers = ((header_dptr*)data)->table;
-    apr_pool_t *pool = ((header_dptr*)data)->pool;
+    header_connection *x = data;
     const char *name;
-    char *next = apr_pstrdup(pool, val);
-    while (*next) {
-        name = next;
-        while (*next && !apr_isspace(*next) && (*next != ',')) {
-            ++next;
+
+    do {
+        while (*val == ',') {
+            val++;
         }
-        while (*next && (apr_isspace(*next) || (*next == ','))) {
-            *next++ = '\0';
+        name = ap_get_token(x->pool, &val, 0);
+        if (!strcasecmp(name, "close")) {
+            x->closed = 1;
         }
-        apr_table_unset(headers, name);
-    }
+        if (!x->first) {
+            x->first = name;
+        }
+        else {
+            const char **elt;
+            if (!x->array) {
+                x->array = apr_array_make(x->pool, 4, sizeof(char *));
+            }
+            elt = apr_array_push(x->array);
+            *elt = name;
+        }
+    } while (*val);
+
     return 1;
 }
 
-static void proxy_clear_connection(apr_pool_t *p, apr_table_t *headers)
+/**
+ * Remove all headers referred to by the Connection header.
+ */
+static int ap_proxy_clear_connection(request_rec *r, apr_table_t *headers)
 {
-    header_dptr x;
-    x.pool = p;
-    x.table = headers;
+    const char **name;
+    header_connection x;
+
+    x.pool = r->pool;
+    x.array = NULL;
+    x.first = NULL;
+    x.closed = 0;
+
     apr_table_unset(headers, "Proxy-Connection");
-    apr_table_do(clear_conn_headers, &x, headers, "Connection", NULL);
-    apr_table_unset(headers, "Connection");
+
+    apr_table_do(find_conn_headers, &x, headers, "Connection", NULL);
+    if (x.first) {
+        /* fast path - no memory allocated for one header */
+        apr_table_unset(headers, "Connection");
+        apr_table_unset(headers, x.first);
+    }
+    if (x.array) {
+        /* two or more headers */
+        while ((name = apr_array_pop(x.array))) {
+            apr_table_unset(headers, *name);
+        }
+    }
+
+    return x.closed;
 }
 
 PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
@@ -3069,7 +3095,7 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
      * apr is compiled with APR_POOL_DEBUG.
      */
     headers_in_copy = apr_table_copy(r->pool, r->headers_in);
-    proxy_clear_connection(p, headers_in_copy);
+    ap_proxy_clear_connection(r, headers_in_copy);
     /* send request headers */
     headers_in_array = apr_table_elts(headers_in_copy);
     headers_in = (const apr_table_entry_t *) headers_in_array->elts;
@@ -3174,3 +3200,9 @@ PROXY_DECLARE(int) ap_proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc,
     apr_brigade_cleanup(bb);
     return OK;
 }
+
+void proxy_util_register_hooks(apr_pool_t *p)
+{
+    APR_REGISTER_OPTIONAL_FN(ap_proxy_retry_worker);
+    APR_REGISTER_OPTIONAL_FN(ap_proxy_clear_connection);
+}