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