17bfed
diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c
17bfed
index 3d5b220..ec9a414 100644
17bfed
--- a/modules/proxy/proxy_util.c
17bfed
+++ b/modules/proxy/proxy_util.c
17bfed
@@ -3621,12 +3621,14 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
                                             char **old_cl_val,
17bfed
                                             char **old_te_val)
17bfed
 {
17bfed
+    int rc = OK;
17bfed
     conn_rec *c = r->connection;
17bfed
     int counter;
17bfed
     char *buf;
17bfed
+    apr_table_t *saved_headers_in = r->headers_in;
17bfed
+    const char *saved_host = apr_table_get(saved_headers_in, "Host");
17bfed
     const apr_array_header_t *headers_in_array;
17bfed
     const apr_table_entry_t *headers_in;
17bfed
-    apr_table_t *saved_headers_in;
17bfed
     apr_bucket *e;
17bfed
     int do_100_continue;
17bfed
     conn_rec *origin = p_conn->connection;
17bfed
@@ -3662,6 +3664,52 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
     ap_xlate_proto_to_ascii(buf, strlen(buf));
17bfed
     e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
17bfed
     APR_BRIGADE_INSERT_TAIL(header_brigade, e);
17bfed
+
17bfed
+    /*
17bfed
+     * Make a copy on r->headers_in for the request we make to the backend,
17bfed
+     * modify the copy in place according to our configuration and connection
17bfed
+     * handling, use it to fill in the forwarded headers' brigade, and finally
17bfed
+     * restore the saved/original ones in r->headers_in.
17bfed
+     *
17bfed
+     * Note: We need to take r->pool for apr_table_copy as the key / value
17bfed
+     * pairs in r->headers_in have been created out of r->pool and
17bfed
+     * p might be (and actually is) a longer living pool.
17bfed
+     * This would trigger the bad pool ancestry abort in apr_table_copy if
17bfed
+     * apr is compiled with APR_POOL_DEBUG.
17bfed
+     *
17bfed
+     * icing: if p indeed lives longer than r->pool, we should allocate
17bfed
+     * all new header values from r->pool as well and avoid leakage.
17bfed
+     */
17bfed
+    r->headers_in = apr_table_copy(r->pool, saved_headers_in);
17bfed
+
17bfed
+    /* Return the original Transfer-Encoding and/or Content-Length values
17bfed
+     * then drop the headers, they must be set by the proxy handler based
17bfed
+     * on the actual body being forwarded.
17bfed
+     */
17bfed
+    if ((*old_te_val = (char *)apr_table_get(r->headers_in,
17bfed
+                                             "Transfer-Encoding"))) {
17bfed
+        apr_table_unset(r->headers_in, "Transfer-Encoding");
17bfed
+    }
17bfed
+    if ((*old_cl_val = (char *)apr_table_get(r->headers_in,
17bfed
+                                             "Content-Length"))) {
17bfed
+        apr_table_unset(r->headers_in, "Content-Length");
17bfed
+    }
17bfed
+
17bfed
+    /* Clear out hop-by-hop request headers not to forward */
17bfed
+    if (ap_proxy_clear_connection(r, r->headers_in) < 0) {
17bfed
+        rc = HTTP_BAD_REQUEST;
17bfed
+        goto cleanup;
17bfed
+    }
17bfed
+
17bfed
+    /* RFC2616 13.5.1 says we should strip these */
17bfed
+    apr_table_unset(r->headers_in, "Keep-Alive");
17bfed
+    apr_table_unset(r->headers_in, "Upgrade");
17bfed
+    apr_table_unset(r->headers_in, "Trailer");
17bfed
+    apr_table_unset(r->headers_in, "TE");
17bfed
+
17bfed
+    /* We used to send `Host: ` always first, so let's keep it that
17bfed
+     * way. No telling which legacy backend is relying no this.
17bfed
+     */
17bfed
     if (dconf->preserve_host == 0) {
17bfed
         if (ap_strchr_c(uri->hostname, ':')) { /* if literal IPv6 address */
17bfed
             if (uri->port_str && uri->port != DEFAULT_HTTP_PORT) {
17bfed
@@ -3683,7 +3731,7 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
         /* don't want to use r->hostname, as the incoming header might have a
17bfed
          * port attached
17bfed
          */
17bfed
-        const char* hostname = apr_table_get(r->headers_in,"Host");
17bfed
+        const char* hostname = saved_host;
17bfed
         if (!hostname) {
17bfed
             hostname =  r->server->server_hostname;
17bfed
             ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01092)
17bfed
@@ -3697,21 +3745,7 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
     ap_xlate_proto_to_ascii(buf, strlen(buf));
17bfed
     e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
17bfed
     APR_BRIGADE_INSERT_TAIL(header_brigade, e);
17bfed
-
17bfed
-    /*
17bfed
-     * Save the original headers in here and restore them when leaving, since
17bfed
-     * we will apply proxy purpose only modifications (eg. clearing hop-by-hop
17bfed
-     * headers, add Via or X-Forwarded-* or Expect...), whereas the originals
17bfed
-     * will be needed later to prepare the correct response and logging.
17bfed
-     *
17bfed
-     * Note: We need to take r->pool for apr_table_copy as the key / value
17bfed
-     * pairs in r->headers_in have been created out of r->pool and
17bfed
-     * p might be (and actually is) a longer living pool.
17bfed
-     * This would trigger the bad pool ancestry abort in apr_table_copy if
17bfed
-     * apr is compiled with APR_POOL_DEBUG.
17bfed
-     */
17bfed
-    saved_headers_in = r->headers_in;
17bfed
-    r->headers_in = apr_table_copy(r->pool, saved_headers_in);
17bfed
+    apr_table_unset(r->headers_in, "Host");
17bfed
 
17bfed
     /* handle Via */
17bfed
     if (conf->viaopt == via_block) {
17bfed
@@ -3778,8 +3812,6 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
      */
17bfed
     if (dconf->add_forwarded_headers) {
17bfed
         if (PROXYREQ_REVERSE == r->proxyreq) {
17bfed
-            const char *buf;
17bfed
-
17bfed
             /* Add X-Forwarded-For: so that the upstream has a chance to
17bfed
              * determine, where the original request came from.
17bfed
              */
17bfed
@@ -3789,8 +3821,9 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
             /* Add X-Forwarded-Host: so that upstream knows what the
17bfed
              * original request hostname was.
17bfed
              */
17bfed
-            if ((buf = apr_table_get(r->headers_in, "Host"))) {
17bfed
-                apr_table_mergen(r->headers_in, "X-Forwarded-Host", buf);
17bfed
+            if (saved_host) {
17bfed
+                apr_table_mergen(r->headers_in, "X-Forwarded-Host",
17bfed
+                                 saved_host);
17bfed
             }
17bfed
 
17bfed
             /* Add X-Forwarded-Server: so that upstream knows what the
17bfed
@@ -3802,10 +3835,27 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
         }
17bfed
     }
17bfed
 
17bfed
+    /* Do we want to strip Proxy-Authorization ?
17bfed
+     * If we haven't used it, then NO
17bfed
+     * If we have used it then MAYBE: RFC2616 says we MAY propagate it.
17bfed
+     * So let's make it configurable by env.
17bfed
+     */
17bfed
+    if (r->user != NULL /* we've authenticated */
17bfed
+        && !apr_table_get(r->subprocess_env, "Proxy-Chain-Auth")) {
17bfed
+        apr_table_unset(r->headers_in, "Proxy-Authorization");
17bfed
+    }
17bfed
+
17bfed
+    /* for sub-requests, ignore freshness/expiry headers */
17bfed
+    if (r->main) {
17bfed
+        apr_table_unset(r->headers_in, "If-Match");
17bfed
+        apr_table_unset(r->headers_in, "If-Modified-Since");
17bfed
+        apr_table_unset(r->headers_in, "If-Range");
17bfed
+        apr_table_unset(r->headers_in, "If-Unmodified-Since");
17bfed
+        apr_table_unset(r->headers_in, "If-None-Match");
17bfed
+     }
17bfed
+
17bfed
+    /* run hook to fixup the request we are about to send */
17bfed
     proxy_run_fixups(r);
17bfed
-    if (ap_proxy_clear_connection(r, r->headers_in) < 0) {
17bfed
-        return HTTP_BAD_REQUEST;
17bfed
-    }
17bfed
 
17bfed
     creds = apr_table_get(r->notes, "proxy-basic-creds");
17bfed
     if (creds) {
17bfed
@@ -3817,55 +3867,8 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
     headers_in = (const apr_table_entry_t *) headers_in_array->elts;
17bfed
     for (counter = 0; counter < headers_in_array->nelts; counter++) {
17bfed
         if (headers_in[counter].key == NULL
17bfed
-            || headers_in[counter].val == NULL
17bfed
-
17bfed
-            /* Already sent */
17bfed
-            || !strcasecmp(headers_in[counter].key, "Host")
17bfed
-
17bfed
-            /* Clear out hop-by-hop request headers not to send
17bfed
-             * RFC2616 13.5.1 says we should strip these headers
17bfed
-             */
17bfed
-            || !strcasecmp(headers_in[counter].key, "Keep-Alive")
17bfed
-            || !strcasecmp(headers_in[counter].key, "TE")
17bfed
-            || !strcasecmp(headers_in[counter].key, "Trailer")
17bfed
-            || !strcasecmp(headers_in[counter].key, "Upgrade")
17bfed
-
17bfed
-            ) {
17bfed
-            continue;
17bfed
-        }
17bfed
-        /* Do we want to strip Proxy-Authorization ?
17bfed
-         * If we haven't used it, then NO
17bfed
-         * If we have used it then MAYBE: RFC2616 says we MAY propagate it.
17bfed
-         * So let's make it configurable by env.
17bfed
-         */
17bfed
-        if (!strcasecmp(headers_in[counter].key,"Proxy-Authorization")) {
17bfed
-            if (r->user != NULL) { /* we've authenticated */
17bfed
-                if (!apr_table_get(r->subprocess_env, "Proxy-Chain-Auth")) {
17bfed
-                    continue;
17bfed
-                }
17bfed
-            }
17bfed
-        }
17bfed
-
17bfed
-        /* Skip Transfer-Encoding and Content-Length for now.
17bfed
-         */
17bfed
-        if (!strcasecmp(headers_in[counter].key, "Transfer-Encoding")) {
17bfed
-            *old_te_val = headers_in[counter].val;
17bfed
-            continue;
17bfed
-        }
17bfed
-        if (!strcasecmp(headers_in[counter].key, "Content-Length")) {
17bfed
-            *old_cl_val = headers_in[counter].val;
17bfed
-            continue;
17bfed
-        }
17bfed
-
17bfed
-        /* for sub-requests, ignore freshness/expiry headers */
17bfed
-        if (r->main) {
17bfed
-            if (   !strcasecmp(headers_in[counter].key, "If-Match")
17bfed
-                || !strcasecmp(headers_in[counter].key, "If-Modified-Since")
17bfed
-                || !strcasecmp(headers_in[counter].key, "If-Range")
17bfed
-                || !strcasecmp(headers_in[counter].key, "If-Unmodified-Since")
17bfed
-                || !strcasecmp(headers_in[counter].key, "If-None-Match")) {
17bfed
-                continue;
17bfed
-            }
17bfed
+            || headers_in[counter].val == NULL) {
17bfed
+             continue;
17bfed
         }
17bfed
 
17bfed
         buf = apr_pstrcat(p, headers_in[counter].key, ": ",
17bfed
@@ -3876,11 +3879,9 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
17bfed
         APR_BRIGADE_INSERT_TAIL(header_brigade, e);
17bfed
     }
17bfed
 
17bfed
-    /* Restore the original headers in (see comment above),
17bfed
-     * we won't modify them anymore.
17bfed
-     */
17bfed
+cleanup:
17bfed
     r->headers_in = saved_headers_in;
17bfed
-    return OK;
17bfed
+    return rc;
17bfed
 }
17bfed
 
17bfed
 PROXY_DECLARE(int) ap_proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc,