fa34f0
diff --git a/modules/proxy/mod_proxy.c b/modules/proxy/mod_proxy.c
fa34f0
index de48735..d13c249 100644
fa34f0
--- a/modules/proxy/mod_proxy.c
fa34f0
+++ b/modules/proxy/mod_proxy.c
fa34f0
@@ -1574,6 +1574,8 @@ static void *create_proxy_dir_config(apr_pool_t *p, char *dummy)
fa34f0
     new->error_override_set = 0;
fa34f0
     new->add_forwarded_headers = 1;
fa34f0
     new->add_forwarded_headers_set = 0;
fa34f0
+    new->forward_100_continue = 1;
fa34f0
+    new->forward_100_continue_set = 0;
fa34f0
 
fa34f0
     return (void *) new;
fa34f0
 }
fa34f0
@@ -1610,6 +1612,11 @@ static void *merge_proxy_dir_config(apr_pool_t *p, void *basev, void *addv)
fa34f0
         : add->add_forwarded_headers;
fa34f0
     new->add_forwarded_headers_set = add->add_forwarded_headers_set
fa34f0
         || base->add_forwarded_headers_set;
fa34f0
+    new->forward_100_continue =
fa34f0
+        (add->forward_100_continue_set == 0) ? base->forward_100_continue
fa34f0
+                                             : add->forward_100_continue;
fa34f0
+    new->forward_100_continue_set = add->forward_100_continue_set
fa34f0
+                                    || base->forward_100_continue_set;
fa34f0
     
fa34f0
     return new;
fa34f0
 }
fa34f0
@@ -2110,6 +2117,14 @@ static const char *
fa34f0
     conf->preserve_host_set = 1;
fa34f0
     return NULL;
fa34f0
 }
fa34f0
+static const char *
fa34f0
+   forward_100_continue(cmd_parms *parms, void *dconf, int flag)
fa34f0
+{
fa34f0
+   proxy_dir_conf *conf = dconf;
fa34f0
+   conf->forward_100_continue = flag;
fa34f0
+   conf->forward_100_continue_set = 1;
fa34f0
+   return NULL;
fa34f0
+}
fa34f0
 
fa34f0
 static const char *
fa34f0
     set_recv_buffer_size(cmd_parms *parms, void *dummy, const char *arg)
fa34f0
@@ -2683,6 +2698,9 @@ static const command_rec proxy_cmds[] =
fa34f0
      "Configure local source IP used for request forward"),
fa34f0
     AP_INIT_FLAG("ProxyAddHeaders", add_proxy_http_headers, NULL, RSRC_CONF|ACCESS_CONF,
fa34f0
      "on if X-Forwarded-* headers should be added or completed"),
fa34f0
+    AP_INIT_FLAG("Proxy100Continue", forward_100_continue, NULL, RSRC_CONF|ACCESS_CONF,
fa34f0
+     "on if 100-Continue should be forwarded to the origin server, off if the "
fa34f0
+     "proxy should handle it by itself"),
fa34f0
     {NULL}
fa34f0
 };
fa34f0
 
fa34f0
diff --git a/modules/proxy/mod_proxy.h b/modules/proxy/mod_proxy.h
fa34f0
index 3419023..288c5d4 100644
fa34f0
--- a/modules/proxy/mod_proxy.h
fa34f0
+++ b/modules/proxy/mod_proxy.h
fa34f0
@@ -240,6 +240,8 @@ typedef struct {
fa34f0
     /** Named back references */
fa34f0
     apr_array_header_t *refs;
fa34f0
 
fa34f0
+    unsigned int forward_100_continue:1;
fa34f0
+    unsigned int forward_100_continue_set:1;
fa34f0
 } proxy_dir_conf;
fa34f0
 
fa34f0
 /* if we interpolate env vars per-request, we'll need a per-request
fa34f0
@@ -380,6 +382,12 @@ do {                             \
fa34f0
 (w)->s->io_buffer_size_set   = (c)->io_buffer_size_set;    \
fa34f0
 } while (0)
fa34f0
 
fa34f0
+#define PROXY_DO_100_CONTINUE(w, r) \
fa34f0
+((w)->s->ping_timeout_set \
fa34f0
+ && (PROXYREQ_REVERSE == (r)->proxyreq) \
fa34f0
+ && !(apr_table_get((r)->subprocess_env, "force-proxy-request-1.0")) \
fa34f0
+ && ap_request_has_body((r)))
fa34f0
+
fa34f0
 /* use 2 hashes */
fa34f0
 typedef struct {
fa34f0
     unsigned int def;
fa34f0
diff --git a/modules/proxy/mod_proxy_ftp.c b/modules/proxy/mod_proxy_ftp.c
fa34f0
index 8f6f853..8d66b4a 100644
fa34f0
--- a/modules/proxy/mod_proxy_ftp.c
fa34f0
+++ b/modules/proxy/mod_proxy_ftp.c
fa34f0
@@ -1181,12 +1181,10 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
fa34f0
         return HTTP_SERVICE_UNAVAILABLE;
fa34f0
     }
fa34f0
 
fa34f0
-    if (!backend->connection) {
fa34f0
-        status = ap_proxy_connection_create_ex("FTP", backend, r);
fa34f0
-        if (status != OK) {
fa34f0
-            proxy_ftp_cleanup(r, backend);
fa34f0
-            return status;
fa34f0
-        }
fa34f0
+    status = ap_proxy_connection_create_ex("FTP", backend, r);
fa34f0
+    if (status != OK) {
fa34f0
+        proxy_ftp_cleanup(r, backend);
fa34f0
+        return status;
fa34f0
     }
fa34f0
 
fa34f0
     /* Use old naming */
fa34f0
diff --git a/modules/proxy/mod_proxy_hcheck.c b/modules/proxy/mod_proxy_hcheck.c
fa34f0
index 2783a58..dd8e407 100644
fa34f0
--- a/modules/proxy/mod_proxy_hcheck.c
fa34f0
+++ b/modules/proxy/mod_proxy_hcheck.c
fa34f0
@@ -762,10 +762,8 @@ static apr_status_t hc_check_http(baton_t *baton)
fa34f0
     }
fa34f0
 
fa34f0
     r = create_request_rec(ptemp, ctx->s, baton->balancer, wctx->method);
fa34f0
-    if (!backend->connection) {
fa34f0
-        if ((status = ap_proxy_connection_create_ex("HCOH", backend, r)) != OK) {
fa34f0
-            return backend_cleanup("HCOH", backend, ctx->s, status);
fa34f0
-        }
fa34f0
+    if ((status = ap_proxy_connection_create_ex("HCOH", backend, r)) != OK) {
fa34f0
+        return backend_cleanup("HCOH", backend, ctx->s, status);
fa34f0
     }
fa34f0
     set_request_connection(r, backend->connection);
fa34f0
 
fa34f0
diff --git a/modules/proxy/mod_proxy_http.c b/modules/proxy/mod_proxy_http.c
fa34f0
index 56af9a8..f007ad6 100644
fa34f0
--- a/modules/proxy/mod_proxy_http.c
fa34f0
+++ b/modules/proxy/mod_proxy_http.c
fa34f0
@@ -216,8 +216,12 @@ static void add_cl(apr_pool_t *p,
fa34f0
     APR_BRIGADE_INSERT_TAIL(header_brigade, e);
fa34f0
 }
fa34f0
 
fa34f0
-#define ASCII_CRLF  "\015\012"
fa34f0
-#define ASCII_ZERO  "\060"
fa34f0
+#ifndef CRLF_ASCII
fa34f0
+#define CRLF_ASCII  "\015\012"
fa34f0
+#endif
fa34f0
+#ifndef ZERO_ASCII
fa34f0
+#define ZERO_ASCII  "\060"
fa34f0
+#endif
fa34f0
 
fa34f0
 static void terminate_headers(apr_bucket_alloc_t *bucket_alloc,
fa34f0
                               apr_bucket_brigade *header_brigade)
fa34f0
@@ -225,304 +229,228 @@ static void terminate_headers(apr_bucket_alloc_t *bucket_alloc,
fa34f0
     apr_bucket *e;
fa34f0
 
fa34f0
     /* add empty line at the end of the headers */
fa34f0
-    e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
fa34f0
+    e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
fa34f0
     APR_BRIGADE_INSERT_TAIL(header_brigade, e);
fa34f0
 }
fa34f0
 
fa34f0
 
fa34f0
 #define MAX_MEM_SPOOL 16384
fa34f0
 
fa34f0
-static int stream_reqbody_chunked(apr_pool_t *p,
fa34f0
-                                           request_rec *r,
fa34f0
-                                           proxy_conn_rec *p_conn,
fa34f0
-                                           conn_rec *origin,
fa34f0
-                                           apr_bucket_brigade *header_brigade,
fa34f0
-                                           apr_bucket_brigade *input_brigade)
fa34f0
-{
fa34f0
-    int seen_eos = 0, rv = OK;
fa34f0
-    apr_size_t hdr_len;
fa34f0
-    apr_off_t bytes;
fa34f0
-    apr_status_t status;
fa34f0
-    apr_bucket_alloc_t *bucket_alloc = r->connection->bucket_alloc;
fa34f0
-    apr_bucket_brigade *bb;
fa34f0
-    apr_bucket *e;
fa34f0
-
fa34f0
-    add_te_chunked(p, bucket_alloc, header_brigade);
fa34f0
-    terminate_headers(bucket_alloc, header_brigade);
fa34f0
+typedef enum {
fa34f0
+    RB_INIT = 0,
fa34f0
+    RB_STREAM_CL,
fa34f0
+    RB_STREAM_CHUNKED,
fa34f0
+    RB_SPOOL_CL
fa34f0
+} rb_methods;
fa34f0
 
fa34f0
-    while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
fa34f0
-    {
fa34f0
-        char chunk_hdr[20];  /* must be here due to transient bucket. */
fa34f0
+typedef struct {
fa34f0
+    apr_pool_t *p;
fa34f0
+    request_rec *r;
fa34f0
+    proxy_worker *worker;
fa34f0
+    proxy_server_conf *sconf;
fa34f0
 
fa34f0
-        /* If this brigade contains EOS, either stop or remove it. */
fa34f0
-        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa34f0
-            seen_eos = 1;
fa34f0
-
fa34f0
-            /* We can't pass this EOS to the output_filters. */
fa34f0
-            e = APR_BRIGADE_LAST(input_brigade);
fa34f0
-            apr_bucket_delete(e);
fa34f0
-        }
fa34f0
-
fa34f0
-        apr_brigade_length(input_brigade, 1, &bytes);
fa34f0
+    char server_portstr[32];
fa34f0
+    proxy_conn_rec *backend;
fa34f0
+    conn_rec *origin;
fa34f0
 
fa34f0
-        hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
fa34f0
-                               "%" APR_UINT64_T_HEX_FMT CRLF,
fa34f0
-                               (apr_uint64_t)bytes);
fa34f0
+    apr_bucket_alloc_t *bucket_alloc;
fa34f0
+    apr_bucket_brigade *header_brigade;
fa34f0
+    apr_bucket_brigade *input_brigade;
fa34f0
+    char *old_cl_val, *old_te_val;
fa34f0
+    apr_off_t cl_val;
fa34f0
 
fa34f0
-        ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
fa34f0
-        e = apr_bucket_transient_create(chunk_hdr, hdr_len,
fa34f0
-                                        bucket_alloc);
fa34f0
-        APR_BRIGADE_INSERT_HEAD(input_brigade, e);
fa34f0
+    rb_methods rb_method;
fa34f0
 
fa34f0
-        /*
fa34f0
-         * Append the end-of-chunk CRLF
fa34f0
-         */
fa34f0
-        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
fa34f0
-        APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
+    int expecting_100;
fa34f0
+    unsigned int do_100_continue:1,
fa34f0
+                 prefetch_nonblocking:1;
fa34f0
+} proxy_http_req_t;
fa34f0
 
fa34f0
-        if (header_brigade) {
fa34f0
-            /* we never sent the header brigade, so go ahead and
fa34f0
-             * take care of that now
fa34f0
-             */
fa34f0
-            bb = header_brigade;
fa34f0
-
fa34f0
-            /*
fa34f0
-             * Save input_brigade in bb brigade. (At least) in the SSL case
fa34f0
-             * input_brigade contains transient buckets whose data would get
fa34f0
-             * overwritten during the next call of ap_get_brigade in the loop.
fa34f0
-             * ap_save_brigade ensures these buckets to be set aside.
fa34f0
-             * Calling ap_save_brigade with NULL as filter is OK, because
fa34f0
-             * bb brigade already has been created and does not need to get
fa34f0
-             * created by ap_save_brigade.
fa34f0
-             */
fa34f0
-            status = ap_save_brigade(NULL, &bb, &input_brigade, p);
fa34f0
-            if (status != APR_SUCCESS) {
fa34f0
-                return HTTP_INTERNAL_SERVER_ERROR;
fa34f0
-            }
fa34f0
-
fa34f0
-            header_brigade = NULL;
fa34f0
-        }
fa34f0
-        else {
fa34f0
-            bb = input_brigade;
fa34f0
+/* Read what's in the client pipe. If nonblocking is set and read is EAGAIN,
fa34f0
+ * pass a FLUSH bucket to the backend and read again in blocking mode.
fa34f0
+ */
fa34f0
+static int stream_reqbody_read(proxy_http_req_t *req, apr_bucket_brigade *bb,
fa34f0
+                               int nonblocking)
fa34f0
+{
fa34f0
+    request_rec *r = req->r;
fa34f0
+    proxy_conn_rec *p_conn = req->backend;
fa34f0
+    apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
fa34f0
+    apr_read_type_e block = nonblocking ? APR_NONBLOCK_READ : APR_BLOCK_READ;
fa34f0
+    apr_status_t status;
fa34f0
+    int rv;
fa34f0
+
fa34f0
+    for (;;) {
fa34f0
+        status = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
fa34f0
+                                block, HUGE_STRING_LEN);
fa34f0
+        if (block == APR_BLOCK_READ
fa34f0
+                || (!APR_STATUS_IS_EAGAIN(status)
fa34f0
+                    && (status != APR_SUCCESS || !APR_BRIGADE_EMPTY(bb)))) {
fa34f0
+            break;
fa34f0
         }
fa34f0
 
fa34f0
-        /* The request is flushed below this loop with chunk EOS header */
fa34f0
-        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, 0);
fa34f0
+        /* Flush and retry (blocking) */
fa34f0
+        apr_brigade_cleanup(bb);
fa34f0
+        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, req->origin, bb, 1);
fa34f0
         if (rv != OK) {
fa34f0
             return rv;
fa34f0
         }
fa34f0
-
fa34f0
-        if (seen_eos) {
fa34f0
-            break;
fa34f0
-        }
fa34f0
-
fa34f0
-        status = ap_get_brigade(r->input_filters, input_brigade,
fa34f0
-                                AP_MODE_READBYTES, APR_BLOCK_READ,
fa34f0
-                                HUGE_STRING_LEN);
fa34f0
-
fa34f0
-        if (status != APR_SUCCESS) {
fa34f0
-            conn_rec *c = r->connection;
fa34f0
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02608)
fa34f0
-                          "read request body failed to %pI (%s)"
fa34f0
-                          " from %s (%s)", p_conn->addr,
fa34f0
-                          p_conn->hostname ? p_conn->hostname: "",
fa34f0
-                          c->client_ip, c->remote_host ? c->remote_host: "");
fa34f0
-            return ap_map_http_request_error(status, HTTP_BAD_REQUEST);
fa34f0
-        }
fa34f0
+        block = APR_BLOCK_READ;
fa34f0
     }
fa34f0
 
fa34f0
-    if (header_brigade) {
fa34f0
-        /* we never sent the header brigade because there was no request body;
fa34f0
-         * send it now
fa34f0
-         */
fa34f0
-        bb = header_brigade;
fa34f0
-    }
fa34f0
-    else {
fa34f0
-        if (!APR_BRIGADE_EMPTY(input_brigade)) {
fa34f0
-            /* input brigade still has an EOS which we can't pass to the output_filters. */
fa34f0
-            e = APR_BRIGADE_LAST(input_brigade);
fa34f0
-            AP_DEBUG_ASSERT(APR_BUCKET_IS_EOS(e));
fa34f0
-            apr_bucket_delete(e);
fa34f0
-        }
fa34f0
-        bb = input_brigade;
fa34f0
-    }
fa34f0
-
fa34f0
-    e = apr_bucket_immortal_create(ASCII_ZERO ASCII_CRLF
fa34f0
-                                   /* <trailers> */
fa34f0
-                                   ASCII_CRLF,
fa34f0
-                                   5, bucket_alloc);
fa34f0
-    APR_BRIGADE_INSERT_TAIL(bb, e);
fa34f0
-
fa34f0
-    if (apr_table_get(r->subprocess_env, "proxy-sendextracrlf")) {
fa34f0
-        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
fa34f0
-        APR_BRIGADE_INSERT_TAIL(bb, e);
fa34f0
+    if (status != APR_SUCCESS) {
fa34f0
+        conn_rec *c = r->connection;
fa34f0
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02608)
fa34f0
+                      "read request body failed to %pI (%s)"
fa34f0
+                      " from %s (%s)", p_conn->addr,
fa34f0
+                      p_conn->hostname ? p_conn->hostname: "",
fa34f0
+                      c->client_ip, c->remote_host ? c->remote_host: "");
fa34f0
+        return ap_map_http_request_error(status, HTTP_BAD_REQUEST);
fa34f0
     }
fa34f0
 
fa34f0
-    /* Now we have headers-only, or the chunk EOS mark; flush it */
fa34f0
-    rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, 1);
fa34f0
-    return rv;
fa34f0
+    return OK;
fa34f0
 }
fa34f0
 
fa34f0
-static int stream_reqbody_cl(apr_pool_t *p,
fa34f0
-                                      request_rec *r,
fa34f0
-                                      proxy_conn_rec *p_conn,
fa34f0
-                                      conn_rec *origin,
fa34f0
-                                      apr_bucket_brigade *header_brigade,
fa34f0
-                                      apr_bucket_brigade *input_brigade,
fa34f0
-                                      char *old_cl_val)
fa34f0
+static int stream_reqbody(proxy_http_req_t *req, rb_methods rb_method)
fa34f0
 {
fa34f0
-    int seen_eos = 0, rv = 0;
fa34f0
-    apr_status_t status = APR_SUCCESS;
fa34f0
-    apr_bucket_alloc_t *bucket_alloc = r->connection->bucket_alloc;
fa34f0
-    apr_bucket_brigade *bb;
fa34f0
+    request_rec *r = req->r;
fa34f0
+    int seen_eos = 0, rv = OK;
fa34f0
+    apr_size_t hdr_len;
fa34f0
+    char chunk_hdr[20];  /* must be here due to transient bucket. */
fa34f0
+    proxy_conn_rec *p_conn = req->backend;
fa34f0
+    apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
fa34f0
+    apr_bucket_brigade *header_brigade = req->header_brigade;
fa34f0
+    apr_bucket_brigade *input_brigade = req->input_brigade;
fa34f0
+    apr_off_t bytes, bytes_streamed = 0;
fa34f0
     apr_bucket *e;
fa34f0
-    apr_off_t cl_val = 0;
fa34f0
-    apr_off_t bytes;
fa34f0
-    apr_off_t bytes_streamed = 0;
fa34f0
-
fa34f0
-    if (old_cl_val) {
fa34f0
-        char *endstr;
fa34f0
 
fa34f0
-        add_cl(p, bucket_alloc, header_brigade, old_cl_val);
fa34f0
-        status = apr_strtoff(&cl_val, old_cl_val, &endstr, 10);
fa34f0
-
fa34f0
-        if (status || *endstr || endstr == old_cl_val || cl_val < 0) {
fa34f0
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01085)
fa34f0
-                          "could not parse request Content-Length (%s)",
fa34f0
-                          old_cl_val);
fa34f0
-            return HTTP_BAD_REQUEST;
fa34f0
+    do {
fa34f0
+        if (APR_BRIGADE_EMPTY(input_brigade)
fa34f0
+                && APR_BRIGADE_EMPTY(header_brigade)) {
fa34f0
+            rv = stream_reqbody_read(req, input_brigade, 1);
fa34f0
+            if (rv != OK) {
fa34f0
+                return rv;
fa34f0
+            }
fa34f0
         }
fa34f0
-    }
fa34f0
-    terminate_headers(bucket_alloc, header_brigade);
fa34f0
-
fa34f0
-    while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
fa34f0
-    {
fa34f0
-        apr_brigade_length(input_brigade, 1, &bytes);
fa34f0
-        bytes_streamed += bytes;
fa34f0
-
fa34f0
-        /* If this brigade contains EOS, either stop or remove it. */
fa34f0
-        if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa34f0
-            seen_eos = 1;
fa34f0
 
fa34f0
-            /* We can't pass this EOS to the output_filters. */
fa34f0
-            e = APR_BRIGADE_LAST(input_brigade);
fa34f0
-            apr_bucket_delete(e);
fa34f0
+        if (!APR_BRIGADE_EMPTY(input_brigade)) {
fa34f0
+            /* If this brigade contains EOS, either stop or remove it. */
fa34f0
+            if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa34f0
+                seen_eos = 1;
fa34f0
 
fa34f0
-            if (apr_table_get(r->subprocess_env, "proxy-sendextracrlf")) {
fa34f0
-                e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
fa34f0
-                APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
+                /* We can't pass this EOS to the output_filters. */
fa34f0
+                e = APR_BRIGADE_LAST(input_brigade);
fa34f0
+                apr_bucket_delete(e);
fa34f0
             }
fa34f0
-        }
fa34f0
 
fa34f0
-        /* C-L < bytes streamed?!?
fa34f0
-         * We will error out after the body is completely
fa34f0
-         * consumed, but we can't stream more bytes at the
fa34f0
-         * back end since they would in part be interpreted
fa34f0
-         * as another request!  If nothing is sent, then
fa34f0
-         * just send nothing.
fa34f0
-         *
fa34f0
-         * Prevents HTTP Response Splitting.
fa34f0
-         */
fa34f0
-        if (bytes_streamed > cl_val) {
fa34f0
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01086)
fa34f0
-                          "read more bytes of request body than expected "
fa34f0
-                          "(got %" APR_OFF_T_FMT ", expected %" APR_OFF_T_FMT ")",
fa34f0
-                          bytes_streamed, cl_val);
fa34f0
-            return HTTP_INTERNAL_SERVER_ERROR;
fa34f0
-        }
fa34f0
-
fa34f0
-        if (header_brigade) {
fa34f0
-            /* we never sent the header brigade, so go ahead and
fa34f0
-             * take care of that now
fa34f0
-             */
fa34f0
-            bb = header_brigade;
fa34f0
+            apr_brigade_length(input_brigade, 1, &bytes);
fa34f0
+            bytes_streamed += bytes;
fa34f0
 
fa34f0
-            /*
fa34f0
-             * Save input_brigade in bb brigade. (At least) in the SSL case
fa34f0
-             * input_brigade contains transient buckets whose data would get
fa34f0
-             * overwritten during the next call of ap_get_brigade in the loop.
fa34f0
-             * ap_save_brigade ensures these buckets to be set aside.
fa34f0
-             * Calling ap_save_brigade with NULL as filter is OK, because
fa34f0
-             * bb brigade already has been created and does not need to get
fa34f0
-             * created by ap_save_brigade.
fa34f0
-             */
fa34f0
-            status = ap_save_brigade(NULL, &bb, &input_brigade, p);
fa34f0
-            if (status != APR_SUCCESS) {
fa34f0
+            if (rb_method == RB_STREAM_CHUNKED) {
fa34f0
+                if (bytes) {
fa34f0
+                    /*
fa34f0
+                     * Prepend the size of the chunk
fa34f0
+                     */
fa34f0
+                    hdr_len = apr_snprintf(chunk_hdr, sizeof(chunk_hdr),
fa34f0
+                                           "%" APR_UINT64_T_HEX_FMT CRLF,
fa34f0
+                                           (apr_uint64_t)bytes);
fa34f0
+                    ap_xlate_proto_to_ascii(chunk_hdr, hdr_len);
fa34f0
+                    e = apr_bucket_transient_create(chunk_hdr, hdr_len,
fa34f0
+                                                    bucket_alloc);
fa34f0
+                    APR_BRIGADE_INSERT_HEAD(input_brigade, e);
fa34f0
+
fa34f0
+                    /*
fa34f0
+                     * Append the end-of-chunk CRLF
fa34f0
+                     */
fa34f0
+                    e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
fa34f0
+                    APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
+                }
fa34f0
+                if (seen_eos) {
fa34f0
+                    /*
fa34f0
+                     * Append the tailing 0-size chunk
fa34f0
+                     */
fa34f0
+                    e = apr_bucket_immortal_create(ZERO_ASCII CRLF_ASCII
fa34f0
+                                                   /* <trailers> */
fa34f0
+                                                   CRLF_ASCII,
fa34f0
+                                                   5, bucket_alloc);
fa34f0
+                    APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
+                }
fa34f0
+            }
fa34f0
+            else if (bytes_streamed > req->cl_val) {
fa34f0
+                /* C-L < bytes streamed?!?
fa34f0
+                 * We will error out after the body is completely
fa34f0
+                 * consumed, but we can't stream more bytes at the
fa34f0
+                 * back end since they would in part be interpreted
fa34f0
+                 * as another request!  If nothing is sent, then
fa34f0
+                 * just send nothing.
fa34f0
+                 *
fa34f0
+                 * Prevents HTTP Response Splitting.
fa34f0
+                 */
fa34f0
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01086)
fa34f0
+                              "read more bytes of request body than expected "
fa34f0
+                              "(got %" APR_OFF_T_FMT ", expected "
fa34f0
+                              "%" APR_OFF_T_FMT ")",
fa34f0
+                              bytes_streamed, req->cl_val);
fa34f0
                 return HTTP_INTERNAL_SERVER_ERROR;
fa34f0
             }
fa34f0
 
fa34f0
-            header_brigade = NULL;
fa34f0
-        }
fa34f0
-        else {
fa34f0
-            bb = input_brigade;
fa34f0
-        }
fa34f0
-
fa34f0
-        /* Once we hit EOS, we are ready to flush. */
fa34f0
-        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, seen_eos);
fa34f0
-        if (rv != OK) {
fa34f0
-            return rv ;
fa34f0
-        }
fa34f0
-
fa34f0
-        if (seen_eos) {
fa34f0
-            break;
fa34f0
+            if (seen_eos && apr_table_get(r->subprocess_env,
fa34f0
+                                          "proxy-sendextracrlf")) {
fa34f0
+                e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
fa34f0
+                APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
+            }
fa34f0
         }
fa34f0
 
fa34f0
-        status = ap_get_brigade(r->input_filters, input_brigade,
fa34f0
-                                AP_MODE_READBYTES, APR_BLOCK_READ,
fa34f0
-                                HUGE_STRING_LEN);
fa34f0
+        /* If we never sent the header brigade, go ahead and take care of
fa34f0
+         * that now by prepending it (once only since header_brigade will be
fa34f0
+         * empty afterward).
fa34f0
+         */
fa34f0
+        APR_BRIGADE_PREPEND(input_brigade, header_brigade);
fa34f0
 
fa34f0
-        if (status != APR_SUCCESS) {
fa34f0
-            conn_rec *c = r->connection;
fa34f0
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02609)
fa34f0
-                          "read request body failed to %pI (%s)"
fa34f0
-                          " from %s (%s)", p_conn->addr,
fa34f0
-                          p_conn->hostname ? p_conn->hostname: "",
fa34f0
-                          c->client_ip, c->remote_host ? c->remote_host: "");
fa34f0
-            return ap_map_http_request_error(status, HTTP_BAD_REQUEST);
fa34f0
+        /* Flush here on EOS because we won't stream_reqbody_read() again */
fa34f0
+        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, req->origin,
fa34f0
+                                   input_brigade, seen_eos);
fa34f0
+        if (rv != OK) {
fa34f0
+            return rv;
fa34f0
         }
fa34f0
-    }
fa34f0
+    } while (!seen_eos);
fa34f0
 
fa34f0
-    if (bytes_streamed != cl_val) {
fa34f0
+    if (rb_method == RB_STREAM_CL && bytes_streamed != req->cl_val) {
fa34f0
         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01087)
fa34f0
                       "client %s given Content-Length did not match"
fa34f0
                       " number of body bytes read", r->connection->client_ip);
fa34f0
         return HTTP_BAD_REQUEST;
fa34f0
     }
fa34f0
 
fa34f0
-    if (header_brigade) {
fa34f0
-        /* we never sent the header brigade since there was no request
fa34f0
-         * body; send it now with the flush flag
fa34f0
-         */
fa34f0
-        bb = header_brigade;
fa34f0
-        return(ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, bb, 1));
fa34f0
-    }
fa34f0
-
fa34f0
     return OK;
fa34f0
 }
fa34f0
 
fa34f0
-static int spool_reqbody_cl(apr_pool_t *p,
fa34f0
-                                     request_rec *r,
fa34f0
-                                     proxy_conn_rec *p_conn,
fa34f0
-                                     conn_rec *origin,
fa34f0
-                                     apr_bucket_brigade *header_brigade,
fa34f0
-                                     apr_bucket_brigade *input_brigade,
fa34f0
-                                     int force_cl)
fa34f0
+static int spool_reqbody_cl(proxy_http_req_t *req, apr_off_t *bytes_spooled)
fa34f0
 {
fa34f0
-    int seen_eos = 0;
fa34f0
-    apr_status_t status;
fa34f0
-    apr_bucket_alloc_t *bucket_alloc = r->connection->bucket_alloc;
fa34f0
+    apr_pool_t *p = req->p;
fa34f0
+    request_rec *r = req->r;
fa34f0
+    int seen_eos = 0, rv = OK;
fa34f0
+    apr_status_t status = APR_SUCCESS;
fa34f0
+    apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
fa34f0
+    apr_bucket_brigade *input_brigade = req->input_brigade;
fa34f0
     apr_bucket_brigade *body_brigade;
fa34f0
     apr_bucket *e;
fa34f0
-    apr_off_t bytes, bytes_spooled = 0, fsize = 0;
fa34f0
+    apr_off_t bytes, fsize = 0;
fa34f0
     apr_file_t *tmpfile = NULL;
fa34f0
     apr_off_t limit;
fa34f0
 
fa34f0
     body_brigade = apr_brigade_create(p, bucket_alloc);
fa34f0
+    *bytes_spooled = 0;
fa34f0
 
fa34f0
     limit = ap_get_limit_req_body(r);
fa34f0
 
fa34f0
-    while (!APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(input_brigade)))
fa34f0
-    {
fa34f0
+    do {
fa34f0
+        if (APR_BRIGADE_EMPTY(input_brigade)) {
fa34f0
+            rv = stream_reqbody_read(req, input_brigade, 0);
fa34f0
+            if (rv != OK) {
fa34f0
+                return rv;
fa34f0
+            }
fa34f0
+        }
fa34f0
+
fa34f0
         /* If this brigade contains EOS, either stop or remove it. */
fa34f0
         if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa34f0
             seen_eos = 1;
fa34f0
@@ -534,13 +462,13 @@ static int spool_reqbody_cl(apr_pool_t *p,
fa34f0
 
fa34f0
         apr_brigade_length(input_brigade, 1, &bytes);
fa34f0
 
fa34f0
-        if (bytes_spooled + bytes > MAX_MEM_SPOOL) {
fa34f0
+        if (*bytes_spooled + bytes > MAX_MEM_SPOOL) {
fa34f0
             /*
fa34f0
              * LimitRequestBody does not affect Proxy requests (Should it?).
fa34f0
              * Let it take effect if we decide to store the body in a
fa34f0
              * temporary file on disk.
fa34f0
              */
fa34f0
-            if (limit && (bytes_spooled + bytes > limit)) {
fa34f0
+            if (limit && (*bytes_spooled + bytes > limit)) {
fa34f0
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01088)
fa34f0
                               "Request body is larger than the configured "
fa34f0
                               "limit of %" APR_OFF_T_FMT, limit);
fa34f0
@@ -610,69 +538,42 @@ static int spool_reqbody_cl(apr_pool_t *p,
fa34f0
 
fa34f0
         }
fa34f0
 
fa34f0
-        bytes_spooled += bytes;
fa34f0
-
fa34f0
-        if (seen_eos) {
fa34f0
-            break;
fa34f0
-        }
fa34f0
-
fa34f0
-        status = ap_get_brigade(r->input_filters, input_brigade,
fa34f0
-                                AP_MODE_READBYTES, APR_BLOCK_READ,
fa34f0
-                                HUGE_STRING_LEN);
fa34f0
-
fa34f0
-        if (status != APR_SUCCESS) {
fa34f0
-            conn_rec *c = r->connection;
fa34f0
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02610)
fa34f0
-                          "read request body failed to %pI (%s)"
fa34f0
-                          " from %s (%s)", p_conn->addr,
fa34f0
-                          p_conn->hostname ? p_conn->hostname: "",
fa34f0
-                          c->client_ip, c->remote_host ? c->remote_host: "");
fa34f0
-            return ap_map_http_request_error(status, HTTP_BAD_REQUEST);
fa34f0
-        }
fa34f0
-    }
fa34f0
+        *bytes_spooled += bytes;
fa34f0
+    } while (!seen_eos);
fa34f0
 
fa34f0
-    if (bytes_spooled || force_cl) {
fa34f0
-        add_cl(p, bucket_alloc, header_brigade, apr_off_t_toa(p, bytes_spooled));
fa34f0
-    }
fa34f0
-    terminate_headers(bucket_alloc, header_brigade);
fa34f0
-    APR_BRIGADE_CONCAT(header_brigade, body_brigade);
fa34f0
+    APR_BRIGADE_CONCAT(input_brigade, body_brigade);
fa34f0
     if (tmpfile) {
fa34f0
-        apr_brigade_insert_file(header_brigade, tmpfile, 0, fsize, p);
fa34f0
+        apr_brigade_insert_file(input_brigade, tmpfile, 0, fsize, p);
fa34f0
     }
fa34f0
     if (apr_table_get(r->subprocess_env, "proxy-sendextracrlf")) {
fa34f0
-        e = apr_bucket_immortal_create(ASCII_CRLF, 2, bucket_alloc);
fa34f0
-        APR_BRIGADE_INSERT_TAIL(header_brigade, e);
fa34f0
+        e = apr_bucket_immortal_create(CRLF_ASCII, 2, bucket_alloc);
fa34f0
+        APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
     }
fa34f0
-    /* This is all a single brigade, pass with flush flagged */
fa34f0
-    return(ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin, header_brigade, 1));
fa34f0
+    return OK;
fa34f0
 }
fa34f0
 
fa34f0
-static
fa34f0
-int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
-                                   proxy_conn_rec *p_conn, proxy_worker *worker,
fa34f0
-                                   proxy_server_conf *conf,
fa34f0
-                                   apr_uri_t *uri,
fa34f0
-                                   char *url, char *server_portstr)
fa34f0
+static int ap_proxy_http_prefetch(proxy_http_req_t *req,
fa34f0
+                                  apr_uri_t *uri, char *url)
fa34f0
 {
fa34f0
+    apr_pool_t *p = req->p;
fa34f0
+    request_rec *r = req->r;
fa34f0
     conn_rec *c = r->connection;
fa34f0
-    apr_bucket_alloc_t *bucket_alloc = c->bucket_alloc;
fa34f0
-    apr_bucket_brigade *header_brigade;
fa34f0
-    apr_bucket_brigade *input_brigade;
fa34f0
+    proxy_conn_rec *p_conn = req->backend;
fa34f0
+    apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
fa34f0
+    apr_bucket_brigade *header_brigade = req->header_brigade;
fa34f0
+    apr_bucket_brigade *input_brigade = req->input_brigade;
fa34f0
     apr_bucket_brigade *temp_brigade;
fa34f0
     apr_bucket *e;
fa34f0
     char *buf;
fa34f0
     apr_status_t status;
fa34f0
-    enum rb_methods {RB_INIT, RB_STREAM_CL, RB_STREAM_CHUNKED, RB_SPOOL_CL};
fa34f0
-    enum rb_methods rb_method = RB_INIT;
fa34f0
-    char *old_cl_val = NULL;
fa34f0
-    char *old_te_val = NULL;
fa34f0
     apr_off_t bytes_read = 0;
fa34f0
     apr_off_t bytes;
fa34f0
     int force10, rv;
fa34f0
+    apr_read_type_e block;
fa34f0
     conn_rec *origin = p_conn->connection;
fa34f0
 
fa34f0
     if (apr_table_get(r->subprocess_env, "force-proxy-request-1.0")) {
fa34f0
-        if (r->expecting_100) {
fa34f0
+        if (req->expecting_100) {
fa34f0
             return HTTP_EXPECTATION_FAILED;
fa34f0
         }
fa34f0
         force10 = 1;
fa34f0
@@ -680,17 +581,14 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
         force10 = 0;
fa34f0
     }
fa34f0
 
fa34f0
-    header_brigade = apr_brigade_create(p, bucket_alloc);
fa34f0
     rv = ap_proxy_create_hdrbrgd(p, header_brigade, r, p_conn,
fa34f0
-                                 worker, conf, uri, url, server_portstr,
fa34f0
-                                 &old_cl_val, &old_te_val);
fa34f0
+                                 req->worker, req->sconf,
fa34f0
+                                 uri, url, req->server_portstr,
fa34f0
+                                 &req->old_cl_val, &req->old_te_val);
fa34f0
     if (rv != OK) {
fa34f0
         return rv;
fa34f0
     }
fa34f0
 
fa34f0
-    /* We have headers, let's figure out our request body... */
fa34f0
-    input_brigade = apr_brigade_create(p, bucket_alloc);
fa34f0
-
fa34f0
     /* sub-requests never use keepalives, and mustn't pass request bodies.
fa34f0
      * Because the new logic looks at input_brigade, we will self-terminate
fa34f0
      * input_brigade and jump past all of the request body logic...
fa34f0
@@ -703,9 +601,9 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
     if (!r->kept_body && r->main) {
fa34f0
         /* XXX: Why DON'T sub-requests use keepalives? */
fa34f0
         p_conn->close = 1;
fa34f0
-        old_cl_val = NULL;
fa34f0
-        old_te_val = NULL;
fa34f0
-        rb_method = RB_STREAM_CL;
fa34f0
+        req->old_te_val = NULL;
fa34f0
+        req->old_cl_val = NULL;
fa34f0
+        req->rb_method = RB_STREAM_CL;
fa34f0
         e = apr_bucket_eos_create(input_brigade->bucket_alloc);
fa34f0
         APR_BRIGADE_INSERT_TAIL(input_brigade, e);
fa34f0
         goto skip_body;
fa34f0
@@ -719,18 +617,19 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
      * encoding has been done by the extensions' handler, and
fa34f0
      * do not modify add_te_chunked's logic
fa34f0
      */
fa34f0
-    if (old_te_val && strcasecmp(old_te_val, "chunked") != 0) {
fa34f0
+    if (req->old_te_val && ap_cstr_casecmp(req->old_te_val, "chunked") != 0) {
fa34f0
         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01093)
fa34f0
-                      "%s Transfer-Encoding is not supported", old_te_val);
fa34f0
+                      "%s Transfer-Encoding is not supported",
fa34f0
+                      req->old_te_val);
fa34f0
         return HTTP_INTERNAL_SERVER_ERROR;
fa34f0
     }
fa34f0
 
fa34f0
-    if (old_cl_val && old_te_val) {
fa34f0
+    if (req->old_cl_val && req->old_te_val) {
fa34f0
         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01094)
fa34f0
                       "client %s (%s) requested Transfer-Encoding "
fa34f0
                       "chunked body with Content-Length (C-L ignored)",
fa34f0
                       c->client_ip, c->remote_host ? c->remote_host: "");
fa34f0
-        old_cl_val = NULL;
fa34f0
+        req->old_cl_val = NULL;
fa34f0
         origin->keepalive = AP_CONN_CLOSE;
fa34f0
         p_conn->close = 1;
fa34f0
     }
fa34f0
@@ -744,10 +643,19 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
      * reasonable size.
fa34f0
      */
fa34f0
     temp_brigade = apr_brigade_create(p, bucket_alloc);
fa34f0
+    block = req->prefetch_nonblocking ? APR_NONBLOCK_READ : APR_BLOCK_READ;
fa34f0
     do {
fa34f0
         status = ap_get_brigade(r->input_filters, temp_brigade,
fa34f0
-                                AP_MODE_READBYTES, APR_BLOCK_READ,
fa34f0
+                                AP_MODE_READBYTES, block,
fa34f0
                                 MAX_MEM_SPOOL - bytes_read);
fa34f0
+        /* ap_get_brigade may return success with an empty brigade
fa34f0
+         * for a non-blocking read which would block
fa34f0
+         */
fa34f0
+        if (block == APR_NONBLOCK_READ
fa34f0
+            && ((status == APR_SUCCESS && APR_BRIGADE_EMPTY(temp_brigade))
fa34f0
+                || APR_STATUS_IS_EAGAIN(status))) {
fa34f0
+            break;
fa34f0
+        }
fa34f0
         if (status != APR_SUCCESS) {
fa34f0
             ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01095)
fa34f0
                           "prefetch request body failed to %pI (%s)"
fa34f0
@@ -785,7 +693,8 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
      * (an arbitrary value.)
fa34f0
      */
fa34f0
     } while ((bytes_read < MAX_MEM_SPOOL - 80)
fa34f0
-              && !APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade)));
fa34f0
+              && !APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))
fa34f0
+              && !req->prefetch_nonblocking);
fa34f0
 
fa34f0
     /* Use chunked request body encoding or send a content-length body?
fa34f0
      *
fa34f0
@@ -822,7 +731,8 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
      * is absent, and the filters are unchanged (the body won't
fa34f0
      * be resized by another content filter).
fa34f0
      */
fa34f0
-    if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa34f0
+    if (!APR_BRIGADE_EMPTY(input_brigade)
fa34f0
+        && APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa34f0
         /* The whole thing fit, so our decision is trivial, use
fa34f0
          * the filtered bytes read from the client for the request
fa34f0
          * body Content-Length.
fa34f0
@@ -830,34 +740,43 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
          * If we expected no body, and read no body, do not set
fa34f0
          * the Content-Length.
fa34f0
          */
fa34f0
-        if (old_cl_val || old_te_val || bytes_read) {
fa34f0
-            old_cl_val = apr_off_t_toa(r->pool, bytes_read);
fa34f0
+        if (req->old_cl_val || req->old_te_val || bytes_read) {
fa34f0
+            req->old_cl_val = apr_off_t_toa(r->pool, bytes_read);
fa34f0
+            req->cl_val = bytes_read;
fa34f0
         }
fa34f0
-        rb_method = RB_STREAM_CL;
fa34f0
+        req->rb_method = RB_STREAM_CL;
fa34f0
     }
fa34f0
-    else if (old_te_val) {
fa34f0
+    else if (req->old_te_val) {
fa34f0
         if (force10
fa34f0
              || (apr_table_get(r->subprocess_env, "proxy-sendcl")
fa34f0
                   && !apr_table_get(r->subprocess_env, "proxy-sendchunks")
fa34f0
                   && !apr_table_get(r->subprocess_env, "proxy-sendchunked"))) {
fa34f0
-            rb_method = RB_SPOOL_CL;
fa34f0
+            req->rb_method = RB_SPOOL_CL;
fa34f0
         }
fa34f0
         else {
fa34f0
-            rb_method = RB_STREAM_CHUNKED;
fa34f0
+            req->rb_method = RB_STREAM_CHUNKED;
fa34f0
         }
fa34f0
     }
fa34f0
-    else if (old_cl_val) {
fa34f0
+    else if (req->old_cl_val) {
fa34f0
         if (r->input_filters == r->proto_input_filters) {
fa34f0
-            rb_method = RB_STREAM_CL;
fa34f0
+            char *endstr;
fa34f0
+            status = apr_strtoff(&req->cl_val, req->old_cl_val, &endstr, 10);
fa34f0
+            if (status != APR_SUCCESS || *endstr || req->cl_val < 0) {
fa34f0
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01085)
fa34f0
+                              "could not parse request Content-Length (%s)",
fa34f0
+                              req->old_cl_val);
fa34f0
+                return HTTP_BAD_REQUEST;
fa34f0
+            }
fa34f0
+            req->rb_method = RB_STREAM_CL;
fa34f0
         }
fa34f0
         else if (!force10
fa34f0
                   && (apr_table_get(r->subprocess_env, "proxy-sendchunks")
fa34f0
                       || apr_table_get(r->subprocess_env, "proxy-sendchunked"))
fa34f0
                   && !apr_table_get(r->subprocess_env, "proxy-sendcl")) {
fa34f0
-            rb_method = RB_STREAM_CHUNKED;
fa34f0
+            req->rb_method = RB_STREAM_CHUNKED;
fa34f0
         }
fa34f0
         else {
fa34f0
-            rb_method = RB_SPOOL_CL;
fa34f0
+            req->rb_method = RB_SPOOL_CL;
fa34f0
         }
fa34f0
     }
fa34f0
     else {
fa34f0
@@ -865,7 +784,31 @@ int ap_proxy_http_request(apr_pool_t *p, request_rec *r,
fa34f0
          * requests, and has the behavior that it will not add any C-L
fa34f0
          * when the old_cl_val is NULL.
fa34f0
          */
fa34f0
-        rb_method = RB_SPOOL_CL;
fa34f0
+        req->rb_method = RB_SPOOL_CL;
fa34f0
+    }
fa34f0
+
fa34f0
+    switch (req->rb_method) {
fa34f0
+    case RB_STREAM_CHUNKED:
fa34f0
+        add_te_chunked(req->p, bucket_alloc, header_brigade);
fa34f0
+        break;
fa34f0
+
fa34f0
+    case RB_STREAM_CL:
fa34f0
+        if (req->old_cl_val) {
fa34f0
+            add_cl(req->p, bucket_alloc, header_brigade, req->old_cl_val);
fa34f0
+        }
fa34f0
+        break;
fa34f0
+
fa34f0
+    default: /* => RB_SPOOL_CL */
fa34f0
+        /* If we have to spool the body, do it now, before connecting or
fa34f0
+         * reusing the backend connection.
fa34f0
+         */
fa34f0
+        rv = spool_reqbody_cl(req, &bytes);
fa34f0
+        if (rv != OK) {
fa34f0
+            return rv;
fa34f0
+        }
fa34f0
+        if (bytes || req->old_te_val || req->old_cl_val) {
fa34f0
+            add_cl(p, bucket_alloc, header_brigade, apr_off_t_toa(p, bytes));
fa34f0
+        }
fa34f0
     }
fa34f0
 
fa34f0
 /* Yes I hate gotos.  This is the subrequest shortcut */
fa34f0
@@ -886,23 +829,44 @@ skip_body:
fa34f0
         e = apr_bucket_pool_create(buf, strlen(buf), p, c->bucket_alloc);
fa34f0
         APR_BRIGADE_INSERT_TAIL(header_brigade, e);
fa34f0
     }
fa34f0
+    terminate_headers(bucket_alloc, header_brigade);
fa34f0
 
fa34f0
-    /* send the request body, if any. */
fa34f0
-    switch(rb_method) {
fa34f0
-    case RB_STREAM_CHUNKED:
fa34f0
-        rv = stream_reqbody_chunked(p, r, p_conn, origin, header_brigade,
fa34f0
-                                        input_brigade);
fa34f0
-        break;
fa34f0
+    return OK;
fa34f0
+}
fa34f0
+
fa34f0
+static int ap_proxy_http_request(proxy_http_req_t *req)
fa34f0
+{
fa34f0
+    int rv;
fa34f0
+    request_rec *r = req->r;
fa34f0
+    apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
fa34f0
+    apr_bucket_brigade *header_brigade = req->header_brigade;
fa34f0
+    apr_bucket_brigade *input_brigade = req->input_brigade;
fa34f0
+
fa34f0
+    /* send the request header/body, if any. */
fa34f0
+    switch (req->rb_method) {
fa34f0
     case RB_STREAM_CL:
fa34f0
-        rv = stream_reqbody_cl(p, r, p_conn, origin, header_brigade,
fa34f0
-                                   input_brigade, old_cl_val);
fa34f0
+    case RB_STREAM_CHUNKED:
fa34f0
+        if (req->do_100_continue) {
fa34f0
+            rv = ap_proxy_pass_brigade(bucket_alloc, r, req->backend,
fa34f0
+                                       req->origin, header_brigade, 1);
fa34f0
+        }
fa34f0
+        else {
fa34f0
+            rv = stream_reqbody(req, req->rb_method);
fa34f0
+        }
fa34f0
         break;
fa34f0
+
fa34f0
     case RB_SPOOL_CL:
fa34f0
-        rv = spool_reqbody_cl(p, r, p_conn, origin, header_brigade,
fa34f0
-                                  input_brigade, (old_cl_val != NULL)
fa34f0
-                                              || (old_te_val != NULL)
fa34f0
-                                              || (bytes_read > 0));
fa34f0
+        /* Prefetch has built the header and spooled the whole body;
fa34f0
+         * if we don't expect 100-continue we can flush both all at once,
fa34f0
+         * otherwise flush the header only.
fa34f0
+         */
fa34f0
+        if (!req->do_100_continue) {
fa34f0
+            APR_BRIGADE_CONCAT(header_brigade, input_brigade);
fa34f0
+        }
fa34f0
+        rv = ap_proxy_pass_brigade(bucket_alloc, r, req->backend,
fa34f0
+                                   req->origin, header_brigade, 1);
fa34f0
         break;
fa34f0
+
fa34f0
     default:
fa34f0
         /* shouldn't be possible */
fa34f0
         rv = HTTP_INTERNAL_SERVER_ERROR;
fa34f0
@@ -910,10 +874,12 @@ skip_body:
fa34f0
     }
fa34f0
 
fa34f0
     if (rv != OK) {
fa34f0
+        conn_rec *c = r->connection;
fa34f0
         /* apr_status_t value has been logged in lower level method */
fa34f0
         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01097)
fa34f0
                       "pass request body failed to %pI (%s) from %s (%s)",
fa34f0
-                      p_conn->addr, p_conn->hostname ? p_conn->hostname: "",
fa34f0
+                      req->backend->addr,
fa34f0
+                      req->backend->hostname ? req->backend->hostname: "",
fa34f0
                       c->client_ip, c->remote_host ? c->remote_host: "");
fa34f0
         return rv;
fa34f0
     }
fa34f0
@@ -1189,12 +1155,16 @@ static int add_trailers(void *data, const char *key, const char *val)
fa34f0
 }
fa34f0
 
fa34f0
 static
fa34f0
-apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
-                                            proxy_conn_rec **backend_ptr,
fa34f0
-                                            proxy_worker *worker,
fa34f0
-                                            proxy_server_conf *conf,
fa34f0
-                                            char *server_portstr) {
fa34f0
+int ap_proxy_http_process_response(proxy_http_req_t *req)
fa34f0
+{
fa34f0
+    apr_pool_t *p = req->p;
fa34f0
+    request_rec *r = req->r;
fa34f0
     conn_rec *c = r->connection;
fa34f0
+    proxy_worker *worker = req->worker;
fa34f0
+    proxy_conn_rec *backend = req->backend;
fa34f0
+    conn_rec *origin = req->origin;
fa34f0
+    int do_100_continue = req->do_100_continue;
fa34f0
+
fa34f0
     char *buffer;
fa34f0
     char fixed_buffer[HUGE_STRING_LEN];
fa34f0
     const char *buf;
fa34f0
@@ -1217,19 +1187,11 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
     int proxy_status = OK;
fa34f0
     const char *original_status_line = r->status_line;
fa34f0
     const char *proxy_status_line = NULL;
fa34f0
-    proxy_conn_rec *backend = *backend_ptr;
fa34f0
-    conn_rec *origin = backend->connection;
fa34f0
     apr_interval_time_t old_timeout = 0;
fa34f0
     proxy_dir_conf *dconf;
fa34f0
-    int do_100_continue;
fa34f0
 
fa34f0
     dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
fa34f0
 
fa34f0
-    do_100_continue = (worker->s->ping_timeout_set
fa34f0
-                       && ap_request_has_body(r)
fa34f0
-                       && (PROXYREQ_REVERSE == r->proxyreq)
fa34f0
-                       && !(apr_table_get(r->subprocess_env, "force-proxy-request-1.0")));
fa34f0
-
fa34f0
     bb = apr_brigade_create(p, c->bucket_alloc);
fa34f0
     pass_bb = apr_brigade_create(p, c->bucket_alloc);
fa34f0
 
fa34f0
@@ -1248,7 +1210,7 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
     }
fa34f0
 
fa34f0
     /* Setup for 100-Continue timeout if appropriate */
fa34f0
-    if (do_100_continue) {
fa34f0
+    if (do_100_continue && worker->s->ping_timeout_set) {
fa34f0
         apr_socket_timeout_get(backend->sock, &old_timeout);
fa34f0
         if (worker->s->ping_timeout != old_timeout) {
fa34f0
             apr_status_t rc;
fa34f0
@@ -1273,6 +1235,8 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
                    origin->local_addr->port));
fa34f0
     do {
fa34f0
         apr_status_t rc;
fa34f0
+        int major = 0, minor = 0;
fa34f0
+        int toclose = 0;
fa34f0
 
fa34f0
         apr_brigade_cleanup(bb);
fa34f0
 
fa34f0
@@ -1360,9 +1324,6 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
          * This is buggy if we ever see an HTTP/1.10
fa34f0
          */
fa34f0
         if (apr_date_checkmask(buffer, "HTTP/#.# ###*")) {
fa34f0
-            int major, minor;
fa34f0
-            int toclose;
fa34f0
-
fa34f0
             major = buffer[5] - '0';
fa34f0
             minor = buffer[7] - '0';
fa34f0
 
fa34f0
@@ -1412,8 +1373,8 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
                          "Set-Cookie", NULL);
fa34f0
 
fa34f0
             /* shove the headers direct into r->headers_out */
fa34f0
-            ap_proxy_read_headers(r, backend->r, buffer, response_field_size, origin,
fa34f0
-                                  &pread_len);
fa34f0
+            ap_proxy_read_headers(r, backend->r, buffer, response_field_size,
fa34f0
+                                  origin, &pread_len);
fa34f0
 
fa34f0
             if (r->headers_out == NULL) {
fa34f0
                 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01106)
fa34f0
@@ -1491,7 +1452,8 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
             r->headers_out = ap_proxy_clean_warnings(p, r->headers_out);
fa34f0
 
fa34f0
             /* handle Via header in response */
fa34f0
-            if (conf->viaopt != via_off && conf->viaopt != via_block) {
fa34f0
+            if (req->sconf->viaopt != via_off
fa34f0
+                    && req->sconf->viaopt != via_block) {
fa34f0
                 const char *server_name = ap_get_server_name(r);
fa34f0
                 /* If USE_CANONICAL_NAME_OFF was configured for the proxy virtual host,
fa34f0
                  * then the server name returned by ap_get_server_name() is the
fa34f0
@@ -1502,18 +1464,18 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
                     server_name = r->server->server_hostname;
fa34f0
                 /* create a "Via:" response header entry and merge it */
fa34f0
                 apr_table_addn(r->headers_out, "Via",
fa34f0
-                               (conf->viaopt == via_full)
fa34f0
+                               (req->sconf->viaopt == via_full)
fa34f0
                                      ? apr_psprintf(p, "%d.%d %s%s (%s)",
fa34f0
                                            HTTP_VERSION_MAJOR(r->proto_num),
fa34f0
                                            HTTP_VERSION_MINOR(r->proto_num),
fa34f0
                                            server_name,
fa34f0
-                                           server_portstr,
fa34f0
+                                           req->server_portstr,
fa34f0
                                            AP_SERVER_BASEVERSION)
fa34f0
                                      : apr_psprintf(p, "%d.%d %s%s",
fa34f0
                                            HTTP_VERSION_MAJOR(r->proto_num),
fa34f0
                                            HTTP_VERSION_MINOR(r->proto_num),
fa34f0
                                            server_name,
fa34f0
-                                           server_portstr)
fa34f0
+                                           req->server_portstr)
fa34f0
                 );
fa34f0
             }
fa34f0
 
fa34f0
@@ -1531,18 +1493,6 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
         }
fa34f0
 
fa34f0
         if (ap_is_HTTP_INFO(proxy_status)) {
fa34f0
-            interim_response++;
fa34f0
-            /* Reset to old timeout iff we've adjusted it */
fa34f0
-            if (do_100_continue
fa34f0
-                && (r->status == HTTP_CONTINUE)
fa34f0
-                && (worker->s->ping_timeout != old_timeout)) {
fa34f0
-                    apr_socket_timeout_set(backend->sock, old_timeout);
fa34f0
-            }
fa34f0
-        }
fa34f0
-        else {
fa34f0
-            interim_response = 0;
fa34f0
-        }
fa34f0
-        if (interim_response) {
fa34f0
             /* RFC2616 tells us to forward this.
fa34f0
              *
fa34f0
              * OTOH, an interim response here may mean the backend
fa34f0
@@ -1563,7 +1513,13 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
             ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r,
fa34f0
                           "HTTP: received interim %d response", r->status);
fa34f0
             if (!policy
fa34f0
-                    || (!strcasecmp(policy, "RFC") && ((r->expecting_100 = 1)))) {
fa34f0
+                    || (!strcasecmp(policy, "RFC")
fa34f0
+                        && (proxy_status != HTTP_CONTINUE
fa34f0
+                            || (req->expecting_100 = 1)))) {
fa34f0
+                if (proxy_status == HTTP_CONTINUE) {
fa34f0
+                    r->expecting_100 = req->expecting_100;
fa34f0
+                    req->expecting_100 = 0;
fa34f0
+                }
fa34f0
                 ap_send_interim_response(r, 1);
fa34f0
             }
fa34f0
             /* FIXME: refine this to be able to specify per-response-status
fa34f0
@@ -1573,7 +1529,106 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
                 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01108)
fa34f0
                               "undefined proxy interim response policy");
fa34f0
             }
fa34f0
+            interim_response++;
fa34f0
         }
fa34f0
+        else {
fa34f0
+            interim_response = 0;
fa34f0
+        }
fa34f0
+
fa34f0
+        /* If we still do 100-continue (end-to-end or ping), either the
fa34f0
+         * current response is the expected "100 Continue" and we are done
fa34f0
+         * with this mode, or this is another interim response and we'll wait
fa34f0
+         * for the next one, or this is a final response and hence the backend
fa34f0
+         * did not honor our expectation.
fa34f0
+         */
fa34f0
+        if (do_100_continue && (!interim_response
fa34f0
+                                || proxy_status == HTTP_CONTINUE)) {
fa34f0
+            /* RFC 7231 - Section 5.1.1 - Expect - Requirement for servers
fa34f0
+             *   A server that responds with a final status code before
fa34f0
+             *   reading the entire message body SHOULD indicate in that
fa34f0
+             *   response whether it intends to close the connection or
fa34f0
+             *   continue reading and discarding the request message.
fa34f0
+             *
fa34f0
+             * So, if this response is not an interim 100 Continue, we can
fa34f0
+             * avoid sending the request body if the backend responded with
fa34f0
+             * "Connection: close" or HTTP < 1.1, and either let the core
fa34f0
+             * discard it or the caller try another balancer member with the
fa34f0
+             * same body (given status 503, though not implemented yet).
fa34f0
+             */
fa34f0
+            int do_send_body = (proxy_status == HTTP_CONTINUE
fa34f0
+                                || (!toclose && major > 0 && minor > 0));
fa34f0
+
fa34f0
+            /* Reset to old timeout iff we've adjusted it. */
fa34f0
+            if (worker->s->ping_timeout_set) {
fa34f0
+                apr_socket_timeout_set(backend->sock, old_timeout);
fa34f0
+            }
fa34f0
+
fa34f0
+            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(10153)
fa34f0
+                          "HTTP: %s100 continue sent by %pI (%s): "
fa34f0
+                          "%ssending body (response: HTTP/%i.%i %s)",
fa34f0
+                          proxy_status != HTTP_CONTINUE ? "no " : "",
fa34f0
+                          backend->addr,
fa34f0
+                          backend->hostname ? backend->hostname : "",
fa34f0
+                          do_send_body ? "" : "not ",
fa34f0
+                          major, minor, proxy_status_line);
fa34f0
+
fa34f0
+            if (do_send_body) {
fa34f0
+                int status;
fa34f0
+
fa34f0
+                /* Send the request body (fully). */
fa34f0
+                switch(req->rb_method) {
fa34f0
+                case RB_STREAM_CL:
fa34f0
+                case RB_STREAM_CHUNKED:
fa34f0
+                    status = stream_reqbody(req, req->rb_method);
fa34f0
+                    break;
fa34f0
+                case RB_SPOOL_CL:
fa34f0
+                    /* Prefetch has spooled the whole body, flush it. */
fa34f0
+                    status = ap_proxy_pass_brigade(req->bucket_alloc, r,
fa34f0
+                                                   backend, origin,
fa34f0
+                                                   req->input_brigade, 1);
fa34f0
+                    break;
fa34f0
+                default:
fa34f0
+                    /* Shouldn't happen */
fa34f0
+                    status = HTTP_INTERNAL_SERVER_ERROR;
fa34f0
+                    break;
fa34f0
+                }
fa34f0
+                if (status != OK) {
fa34f0
+                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
fa34f0
+                            APLOGNO(10154) "pass request body failed "
fa34f0
+                            "to %pI (%s) from %s (%s) with status %i",
fa34f0
+                            backend->addr,
fa34f0
+                            backend->hostname ? backend->hostname : "",
fa34f0
+                            c->client_ip,
fa34f0
+                            c->remote_host ? c->remote_host : "",
fa34f0
+                            status);
fa34f0
+                    backend->close = 1;
fa34f0
+                    return status;
fa34f0
+                }
fa34f0
+            }
fa34f0
+            else {
fa34f0
+                /* If we don't read the client connection any further, since
fa34f0
+                 * there are pending data it should be "Connection: close"d to
fa34f0
+                 * prevent reuse. We don't exactly c->keepalive = AP_CONN_CLOSE
fa34f0
+                 * here though, because error_override or a potential retry on
fa34f0
+                 * another backend could finally read that data and finalize
fa34f0
+                 * the request processing, making keep-alive possible. So what
fa34f0
+                 * we do is restoring r->expecting_100 for ap_set_keepalive()
fa34f0
+                 * to do the right thing according to the final response and
fa34f0
+                 * any later update of r->expecting_100.
fa34f0
+                 */
fa34f0
+                r->expecting_100 = req->expecting_100;
fa34f0
+                req->expecting_100 = 0;
fa34f0
+            }
fa34f0
+
fa34f0
+            /* Once only! */
fa34f0
+            do_100_continue = 0;
fa34f0
+        }
fa34f0
+
fa34f0
+        if (interim_response) {
fa34f0
+            /* Already forwarded above, read next response */
fa34f0
+            continue;
fa34f0
+        }
fa34f0
+
fa34f0
         /* Moved the fixups of Date headers and those affected by
fa34f0
          * ProxyPassReverse/etc from here to ap_proxy_read_headers
fa34f0
          */
fa34f0
@@ -1648,7 +1703,6 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
 
fa34f0
         /* send body - but only if a body is expected */
fa34f0
         if ((!r->header_only) &&                   /* not HEAD request */
fa34f0
-            !interim_response &&                   /* not any 1xx response */
fa34f0
             (proxy_status != HTTP_NO_CONTENT) &&      /* not 204 */
fa34f0
             (proxy_status != HTTP_NOT_MODIFIED)) {    /* not 304 */
fa34f0
 
fa34f0
@@ -1697,7 +1751,7 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
 
fa34f0
                     rv = ap_get_brigade(backend->r->input_filters, bb,
fa34f0
                                         AP_MODE_READBYTES, mode,
fa34f0
-                                        conf->io_buffer_size);
fa34f0
+                                        req->sconf->io_buffer_size);
fa34f0
 
fa34f0
                     /* ap_get_brigade will return success with an empty brigade
fa34f0
                      * for a non-blocking read which would block: */
fa34f0
@@ -1789,7 +1843,7 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
                         ap_proxy_release_connection(backend->worker->s->scheme,
fa34f0
                                 backend, r->server);
fa34f0
                         /* Ensure that the backend is not reused */
fa34f0
-                        *backend_ptr = NULL;
fa34f0
+                        req->backend = NULL;
fa34f0
 
fa34f0
                     }
fa34f0
 
fa34f0
@@ -1798,12 +1852,13 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
                         || c->aborted) {
fa34f0
                         /* Ack! Phbtt! Die! User aborted! */
fa34f0
                         /* Only close backend if we haven't got all from the
fa34f0
-                         * backend. Furthermore if *backend_ptr is NULL it is no
fa34f0
+                         * backend. Furthermore if req->backend is NULL it is no
fa34f0
                          * longer safe to fiddle around with backend as it might
fa34f0
                          * be already in use by another thread.
fa34f0
                          */
fa34f0
-                        if (*backend_ptr) {
fa34f0
-                            backend->close = 1;  /* this causes socket close below */
fa34f0
+                        if (req->backend) {
fa34f0
+                            /* this causes socket close below */
fa34f0
+                            req->backend->close = 1;
fa34f0
                         }
fa34f0
                         finish = TRUE;
fa34f0
                     }
fa34f0
@@ -1816,7 +1871,7 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
             }
fa34f0
             ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, "end body send");
fa34f0
         }
fa34f0
-        else if (!interim_response) {
fa34f0
+        else {
fa34f0
             ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, "header only");
fa34f0
 
fa34f0
             /* make sure we release the backend connection as soon
fa34f0
@@ -1826,7 +1881,7 @@ apr_status_t ap_proxy_http_process_response(apr_pool_t * p, request_rec *r,
fa34f0
              */
fa34f0
             ap_proxy_release_connection(backend->worker->s->scheme,
fa34f0
                     backend, r->server);
fa34f0
-            *backend_ptr = NULL;
fa34f0
+            req->backend = NULL;
fa34f0
 
fa34f0
             /* Pass EOS bucket down the filter chain. */
fa34f0
             e = apr_bucket_eos_create(c->bucket_alloc);
fa34f0
@@ -1880,14 +1935,17 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
fa34f0
                               apr_port_t proxyport)
fa34f0
 {
fa34f0
     int status;
fa34f0
-    char server_portstr[32];
fa34f0
     char *scheme;
fa34f0
     const char *proxy_function;
fa34f0
     const char *u;
fa34f0
+    proxy_http_req_t *req = NULL;
fa34f0
     proxy_conn_rec *backend = NULL;
fa34f0
     int is_ssl = 0;
fa34f0
     conn_rec *c = r->connection;
fa34f0
+    proxy_dir_conf *dconf;
fa34f0
     int retry = 0;
fa34f0
+    char *locurl = url;
fa34f0
+    int toclose = 0;
fa34f0
     /*
fa34f0
      * Use a shorter-lived pool to reduce memory usage
fa34f0
      * and avoid a memory leak
fa34f0
@@ -1928,14 +1986,47 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
fa34f0
     }
fa34f0
     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "HTTP: serving URL %s", url);
fa34f0
 
fa34f0
-
fa34f0
     /* create space for state information */
fa34f0
     if ((status = ap_proxy_acquire_connection(proxy_function, &backend,
fa34f0
-                                              worker, r->server)) != OK)
fa34f0
-        goto cleanup;
fa34f0
+                                              worker, r->server)) != OK) {
fa34f0
+        return status;
fa34f0
+    }
fa34f0
 
fa34f0
     backend->is_ssl = is_ssl;
fa34f0
 
fa34f0
+    req = apr_pcalloc(p, sizeof(*req));
fa34f0
+    req->p = p;
fa34f0
+    req->r = r;
fa34f0
+    req->sconf = conf;
fa34f0
+    req->worker = worker;
fa34f0
+    req->backend = backend;
fa34f0
+    req->bucket_alloc = c->bucket_alloc;
fa34f0
+    req->rb_method = RB_INIT;
fa34f0
+
fa34f0
+    dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
fa34f0
+
fa34f0
+    /* Should we handle end-to-end or ping 100-continue? */
fa34f0
+    if ((r->expecting_100 && dconf->forward_100_continue)
fa34f0
+            || PROXY_DO_100_CONTINUE(worker, r)) {
fa34f0
+        /* We need to reset r->expecting_100 or prefetching will cause
fa34f0
+         * ap_http_filter() to send "100 Continue" response by itself. So
fa34f0
+         * we'll use req->expecting_100 in mod_proxy_http to determine whether
fa34f0
+         * the client should be forwarded "100 continue", and r->expecting_100
fa34f0
+         * will be restored at the end of the function with the actual value of
fa34f0
+         * req->expecting_100 (i.e. cleared only if mod_proxy_http sent the
fa34f0
+         * "100 Continue" according to its policy).
fa34f0
+         */
fa34f0
+        req->do_100_continue = req->prefetch_nonblocking = 1;
fa34f0
+        req->expecting_100 = r->expecting_100;
fa34f0
+        r->expecting_100 = 0;
fa34f0
+    }
fa34f0
+    /* Should we block while prefetching the body or try nonblocking and flush
fa34f0
+     * data to the backend ASAP?
fa34f0
+     */
fa34f0
+    else if (apr_table_get(r->subprocess_env, "proxy-prefetch-nonblocking")) {
fa34f0
+        req->prefetch_nonblocking = 1;
fa34f0
+    }
fa34f0
+
fa34f0
     /*
fa34f0
      * In the case that we are handling a reverse proxy connection and this
fa34f0
      * is not a request that is coming over an already kept alive connection
fa34f0
@@ -1949,15 +2040,53 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
fa34f0
         backend->close = 1;
fa34f0
     }
fa34f0
 
fa34f0
+    /* Step One: Determine Who To Connect To */
fa34f0
+    if ((status = ap_proxy_determine_connection(p, r, conf, worker, backend,
fa34f0
+                                            uri, &locurl, proxyname,
fa34f0
+                                            proxyport, req->server_portstr,
fa34f0
+                                            sizeof(req->server_portstr))))
fa34f0
+        goto cleanup;
fa34f0
+
fa34f0
+    /* Prefetch (nonlocking) the request body so to increase the chance to get
fa34f0
+     * the whole (or enough) body and determine Content-Length vs chunked or
fa34f0
+     * spooled. By doing this before connecting or reusing the backend, we want
fa34f0
+     * to minimize the delay between this connection is considered alive and
fa34f0
+     * the first bytes sent (should the client's link be slow or some input
fa34f0
+     * filter retain the data). This is a best effort to prevent the backend
fa34f0
+     * from closing (from under us) what it thinks is an idle connection, hence
fa34f0
+     * to reduce to the minimum the unavoidable local is_socket_connected() vs
fa34f0
+     * remote keepalive race condition.
fa34f0
+     */
fa34f0
+    req->input_brigade = apr_brigade_create(p, req->bucket_alloc);
fa34f0
+    req->header_brigade = apr_brigade_create(p, req->bucket_alloc);
fa34f0
+    if ((status = ap_proxy_http_prefetch(req, uri, locurl)) != OK)
fa34f0
+        goto cleanup;
fa34f0
+
fa34f0
+    /* We need to reset backend->close now, since ap_proxy_http_prefetch() set
fa34f0
+     * it to disable the reuse of the connection *after* this request (no keep-
fa34f0
+     * alive), not to close any reusable connection before this request. However
fa34f0
+     * assure what is expected later by using a local flag and do the right thing
fa34f0
+     * when ap_proxy_connect_backend() below provides the connection to close.
fa34f0
+     */
fa34f0
+    toclose = backend->close;
fa34f0
+    backend->close = 0;
fa34f0
+
fa34f0
     while (retry < 2) {
fa34f0
-        char *locurl = url;
fa34f0
+        if (retry) {
fa34f0
+            char *newurl = url;
fa34f0
 
fa34f0
-        /* Step One: Determine Who To Connect To */
fa34f0
-        if ((status = ap_proxy_determine_connection(p, r, conf, worker, backend,
fa34f0
-                                                uri, &locurl, proxyname,
fa34f0
-                                                proxyport, server_portstr,
fa34f0
-                                                sizeof(server_portstr))) != OK)
fa34f0
-            break;
fa34f0
+            /* Step One (again): (Re)Determine Who To Connect To */
fa34f0
+            if ((status = ap_proxy_determine_connection(p, r, conf, worker,
fa34f0
+                            backend, uri, &newurl, proxyname, proxyport,
fa34f0
+                            req->server_portstr, sizeof(req->server_portstr))))
fa34f0
+                break;
fa34f0
+
fa34f0
+            /* The code assumes locurl is not changed during the loop, or
fa34f0
+             * ap_proxy_http_prefetch() would have to be called every time,
fa34f0
+             * and header_brigade be changed accordingly...
fa34f0
+             */
fa34f0
+            AP_DEBUG_ASSERT(strcmp(newurl, locurl) == 0);
fa34f0
+        }
fa34f0
 
fa34f0
         /* Step Two: Make the Connection */
fa34f0
         if (ap_proxy_check_connection(proxy_function, backend, r->server, 1,
fa34f0
@@ -1972,54 +2101,64 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
fa34f0
         }
fa34f0
 
fa34f0
         /* Step Three: Create conn_rec */
fa34f0
-        if (!backend->connection) {
fa34f0
-            if ((status = ap_proxy_connection_create_ex(proxy_function,
fa34f0
-                                                        backend, r)) != OK)
fa34f0
-                break;
fa34f0
-            /*
fa34f0
-             * On SSL connections set a note on the connection what CN is
fa34f0
-             * requested, such that mod_ssl can check if it is requested to do
fa34f0
-             * so.
fa34f0
-             */
fa34f0
-            if (backend->ssl_hostname) {
fa34f0
-                apr_table_setn(backend->connection->notes,
fa34f0
-                               "proxy-request-hostname",
fa34f0
-                               backend->ssl_hostname);
fa34f0
-            }
fa34f0
+        if ((status = ap_proxy_connection_create_ex(proxy_function,
fa34f0
+                                                    backend, r)) != OK)
fa34f0
+            break;
fa34f0
+        req->origin = backend->connection;
fa34f0
+
fa34f0
+        /* Don't recycle the connection if prefetch (above) told not to do so */
fa34f0
+        if (toclose) {
fa34f0
+            backend->close = 1;
fa34f0
+            req->origin->keepalive = AP_CONN_CLOSE;
fa34f0
+        }
fa34f0
+
fa34f0
+        /*
fa34f0
+         * On SSL connections set a note on the connection what CN is
fa34f0
+         * requested, such that mod_ssl can check if it is requested to do
fa34f0
+         * so.
fa34f0
+         *
fa34f0
+         * https://github.com/apache/httpd/commit/7d272e2628b4ae05f68cdc74b070707250896a34
fa34f0
+         */
fa34f0
+        if (backend->ssl_hostname) {
fa34f0
+            apr_table_setn(backend->connection->notes,
fa34f0
+                           "proxy-request-hostname",
fa34f0
+                           backend->ssl_hostname);
fa34f0
         }
fa34f0
 
fa34f0
         /* Step Four: Send the Request
fa34f0
          * On the off-chance that we forced a 100-Continue as a
fa34f0
          * kinda HTTP ping test, allow for retries
fa34f0
          */
fa34f0
-        if ((status = ap_proxy_http_request(p, r, backend, worker,
fa34f0
-                                        conf, uri, locurl, server_portstr)) != OK) {
fa34f0
-            if ((status == HTTP_SERVICE_UNAVAILABLE) && worker->s->ping_timeout_set) {
fa34f0
-                backend->close = 1;
fa34f0
+        status = ap_proxy_http_request(req);
fa34f0
+        if (status != OK) {
fa34f0
+            if (req->do_100_continue && status == HTTP_SERVICE_UNAVAILABLE) {
fa34f0
                 ap_log_rerror(APLOG_MARK, APLOG_INFO, status, r, APLOGNO(01115)
fa34f0
                               "HTTP: 100-Continue failed to %pI (%s)",
fa34f0
                               worker->cp->addr, worker->s->hostname_ex);
fa34f0
+                backend->close = 1;
fa34f0
                 retry++;
fa34f0
                 continue;
fa34f0
-            } else {
fa34f0
-                break;
fa34f0
-            }
fa34f0
+            } 
fa34f0
 
fa34f0
+            break;
fa34f0
         }
fa34f0
 
fa34f0
         /* Step Five: Receive the Response... Fall thru to cleanup */
fa34f0
-        status = ap_proxy_http_process_response(p, r, &backend, worker,
fa34f0
-                                                conf, server_portstr);
fa34f0
+        status = ap_proxy_http_process_response(req);
fa34f0
 
fa34f0
         break;
fa34f0
     }
fa34f0
 
fa34f0
     /* Step Six: Clean Up */
fa34f0
 cleanup:
fa34f0
-    if (backend) {
fa34f0
+    if (req->backend) {
fa34f0
         if (status != OK)
fa34f0
-            backend->close = 1;
fa34f0
-        ap_proxy_http_cleanup(proxy_function, r, backend);
fa34f0
+            req->backend->close = 1;
fa34f0
+        ap_proxy_http_cleanup(proxy_function, r, req->backend);
fa34f0
+    }
fa34f0
+    if (req->expecting_100) {
fa34f0
+        /* Restore r->expecting_100 if we didn't touch it */
fa34f0
+        r->expecting_100 = req->expecting_100;
fa34f0
     }
fa34f0
     return status;
fa34f0
 }
fa34f0
diff --git a/modules/proxy/mod_proxy_uwsgi.c b/modules/proxy/mod_proxy_uwsgi.c
fa34f0
index c5d4f8e..e1253e4 100644
fa34f0
--- a/modules/proxy/mod_proxy_uwsgi.c
fa34f0
+++ b/modules/proxy/mod_proxy_uwsgi.c
fa34f0
@@ -509,12 +509,11 @@ static int uwsgi_handler(request_rec *r, proxy_worker * worker,
fa34f0
     }
fa34f0
 
fa34f0
     /* Step Three: Create conn_rec */
fa34f0
-    if (!backend->connection) {
fa34f0
-        if ((status = ap_proxy_connection_create(UWSGI_SCHEME, backend,
fa34f0
-                                                 r->connection,
fa34f0
-                                                 r->server)) != OK)
fa34f0
-            goto cleanup;
fa34f0
-    }
fa34f0
+    if ((status = ap_proxy_connection_create(UWSGI_SCHEME, backend,
fa34f0
+                                             r->connection,
fa34f0
+                                             r->server)) != OK)
fa34f0
+        goto cleanup;
fa34f0
+
fa34f0
 
fa34f0
     /* Step Four: Process the Request */
fa34f0
     if (((status = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK)
fa34f0
diff --git a/modules/proxy/mod_proxy_wstunnel.c b/modules/proxy/mod_proxy_wstunnel.c
fa34f0
index 9dda010..4aadbab 100644
fa34f0
--- a/modules/proxy/mod_proxy_wstunnel.c
fa34f0
+++ b/modules/proxy/mod_proxy_wstunnel.c
fa34f0
@@ -284,8 +284,8 @@ static int proxy_wstunnel_handler(request_rec *r, proxy_worker *worker,
fa34f0
     char server_portstr[32];
fa34f0
     proxy_conn_rec *backend = NULL;
fa34f0
     char *scheme;
fa34f0
-    int retry;
fa34f0
     apr_pool_t *p = r->pool;
fa34f0
+    char *locurl = url;
fa34f0
     apr_uri_t *uri;
fa34f0
     int is_ssl = 0;
fa34f0
     const char *upgrade_method = *worker->s->upgrade ? worker->s->upgrade : "WebSocket";
fa34f0
@@ -318,59 +318,51 @@ static int proxy_wstunnel_handler(request_rec *r, proxy_worker *worker,
fa34f0
     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02451) "serving URL %s", url);
fa34f0
 
fa34f0
     /* create space for state information */
fa34f0
-    status = ap_proxy_acquire_connection(scheme, &backend, worker,
fa34f0
-                                         r->server);
fa34f0
+    status = ap_proxy_acquire_connection(scheme, &backend, worker, r->server);
fa34f0
     if (status != OK) {
fa34f0
-        if (backend) {
fa34f0
-            backend->close = 1;
fa34f0
-            ap_proxy_release_connection(scheme, backend, r->server);
fa34f0
-        }
fa34f0
-        return status;
fa34f0
+        goto cleanup;
fa34f0
     }
fa34f0
 
fa34f0
     backend->is_ssl = is_ssl;
fa34f0
     backend->close = 0;
fa34f0
 
fa34f0
-    retry = 0;
fa34f0
-    while (retry < 2) {
fa34f0
-        char *locurl = url;
fa34f0
-        /* Step One: Determine Who To Connect To */
fa34f0
-        status = ap_proxy_determine_connection(p, r, conf, worker, backend,
fa34f0
-                                               uri, &locurl, proxyname, proxyport,
fa34f0
-                                               server_portstr,
fa34f0
-                                               sizeof(server_portstr));
fa34f0
-
fa34f0
-        if (status != OK)
fa34f0
-            break;
fa34f0
-
fa34f0
-        /* Step Two: Make the Connection */
fa34f0
-        if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
fa34f0
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02452)
fa34f0
-                          "failed to make connection to backend: %s",
fa34f0
-                          backend->hostname);
fa34f0
-            status = HTTP_SERVICE_UNAVAILABLE;
fa34f0
-            break;
fa34f0
-        }
fa34f0
-
fa34f0
-        /* Step Three: Create conn_rec */
fa34f0
-        if (!backend->connection) {
fa34f0
-            status = ap_proxy_connection_create_ex(scheme, backend, r);
fa34f0
-            if (status  != OK) {
fa34f0
-                break;
fa34f0
-            }
fa34f0
-        }
fa34f0
-
fa34f0
-        backend->close = 1; /* must be after ap_proxy_determine_connection */
fa34f0
-
fa34f0
+    /* Step One: Determine Who To Connect To */
fa34f0
+    status = ap_proxy_determine_connection(p, r, conf, worker, backend,
fa34f0
+                                           uri, &locurl, proxyname, proxyport,
fa34f0
+                                           server_portstr,
fa34f0
+                                           sizeof(server_portstr));
fa34f0
+    
fa34f0
+    if (status != OK) {
fa34f0
+        goto cleanup;
fa34f0
+    }
fa34f0
+    
fa34f0
+    /* Step Two: Make the Connection */
fa34f0
+    if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
fa34f0
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02452)
fa34f0
+                      "failed to make connection to backend: %s",
fa34f0
+                      backend->hostname);
fa34f0
+        status = HTTP_SERVICE_UNAVAILABLE;
fa34f0
+        goto cleanup;
fa34f0
+    }
fa34f0
 
fa34f0
-        /* Step Three: Process the Request */
fa34f0
-        status = proxy_wstunnel_request(p, r, backend, worker, conf, uri, locurl,
fa34f0
-                                      server_portstr);
fa34f0
-        break;
fa34f0
+    /* Step Three: Create conn_rec */
fa34f0
+    /* keep it because of */
fa34f0
+    /* https://github.com/apache/httpd/commit/313d5ee40f390da1a6ee2c2752864ad3aad0a1c3 */
fa34f0
+    status = ap_proxy_connection_create_ex(scheme, backend, r);
fa34f0
+    if (status != OK) {
fa34f0
+        goto cleanup;
fa34f0
     }
fa34f0
+    
fa34f0
+    /* Step Four: Process the Request */
fa34f0
+    status = proxy_wstunnel_request(p, r, backend, worker, conf, uri, locurl,
fa34f0
+                                  server_portstr);
fa34f0
 
fa34f0
+cleanup:
fa34f0
     /* Do not close the socket */
fa34f0
-    ap_proxy_release_connection(scheme, backend, r->server);
fa34f0
+    if (backend) {
fa34f0
+        backend->close = 1;
fa34f0
+        ap_proxy_release_connection(scheme, backend, r->server);
fa34f0
+    }
fa34f0
     return status;
fa34f0
 }
fa34f0
 
fa34f0
diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c
fa34f0
index 0bbfa59..0759dac 100644
fa34f0
--- a/modules/proxy/proxy_util.c
fa34f0
+++ b/modules/proxy/proxy_util.c
fa34f0
@@ -3573,10 +3573,7 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
fa34f0
      * To be compliant, we only use 100-Continue for requests with bodies.
fa34f0
      * We also make sure we won't be talking HTTP/1.0 as well.
fa34f0
      */
fa34f0
-    do_100_continue = (worker->s->ping_timeout_set
fa34f0
-                       && ap_request_has_body(r)
fa34f0
-                       && (PROXYREQ_REVERSE == r->proxyreq)
fa34f0
-                       && !(apr_table_get(r->subprocess_env, "force-proxy-request-1.0")));
fa34f0
+    do_100_continue = PROXY_DO_100_CONTINUE(worker, r);
fa34f0
 
fa34f0
     if (apr_table_get(r->subprocess_env, "force-proxy-request-1.0")) {
fa34f0
         /*
fa34f0
@@ -3593,7 +3590,9 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
fa34f0
         buf = apr_pstrcat(p, r->method, " ", url, " HTTP/1.1" CRLF, NULL);
fa34f0
     }
fa34f0
     if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {
fa34f0
-        origin->keepalive = AP_CONN_CLOSE;
fa34f0
+        if (origin) {
fa34f0
+            origin->keepalive = AP_CONN_CLOSE;
fa34f0
+        }
fa34f0
         p_conn->close = 1;
fa34f0
     }
fa34f0
     ap_xlate_proto_to_ascii(buf, strlen(buf));
fa34f0
@@ -3685,14 +3684,6 @@ PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
fa34f0
     if (do_100_continue) {
fa34f0
         const char *val;
fa34f0
 
fa34f0
-        if (!r->expecting_100) {
fa34f0
-            /* Don't forward any "100 Continue" response if the client is
fa34f0
-             * not expecting it.
fa34f0
-             */
fa34f0
-            apr_table_setn(r->subprocess_env, "proxy-interim-response",
fa34f0
-                                              "Suppress");
fa34f0
-        }
fa34f0
-
fa34f0
         /* Add the Expect header if not already there. */
fa34f0
         if (((val = apr_table_get(r->headers_in, "Expect")) == NULL)
fa34f0
                 || (strcasecmp(val, "100-Continue") != 0 /* fast path */
fa34f0
diff --git a/server/protocol.c b/server/protocol.c
fa34f0
index 8d90055..8d1fdd2 100644
fa34f0
--- a/server/protocol.c
fa34f0
+++ b/server/protocol.c
fa34f0
@@ -2188,21 +2188,23 @@ AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers)
fa34f0
                       "Status is %d - not sending interim response", r->status);
fa34f0
         return;
fa34f0
     }
fa34f0
-    if ((r->status == HTTP_CONTINUE) && !r->expecting_100) {
fa34f0
-        /*
fa34f0
-         * Don't send 100-Continue when there was no Expect: 100-continue
fa34f0
-         * in the request headers. For origin servers this is a SHOULD NOT
fa34f0
-         * for proxies it is a MUST NOT according to RFC 2616 8.2.3
fa34f0
-         */
fa34f0
-        return;
fa34f0
-    }
fa34f0
+    if (r->status == HTTP_CONTINUE) {
fa34f0
+        if (!r->expecting_100) {
fa34f0
+            /*
fa34f0
+             * Don't send 100-Continue when there was no Expect: 100-continue
fa34f0
+             * in the request headers. For origin servers this is a SHOULD NOT
fa34f0
+             * for proxies it is a MUST NOT according to RFC 2616 8.2.3
fa34f0
+             */
fa34f0
+            return;
fa34f0
+        }
fa34f0
 
fa34f0
-    /* if we send an interim response, we're no longer in a state of
fa34f0
-     * expecting one.  Also, this could feasibly be in a subrequest,
fa34f0
-     * so we need to propagate the fact that we responded.
fa34f0
-     */
fa34f0
-    for (rr = r; rr != NULL; rr = rr->main) {
fa34f0
-        rr->expecting_100 = 0;
fa34f0
+        /* if we send an interim response, we're no longer in a state of
fa34f0
+         * expecting one.  Also, this could feasibly be in a subrequest,
fa34f0
+         * so we need to propagate the fact that we responded.
fa34f0
+         */
fa34f0
+        for (rr = r; rr != NULL; rr = rr->main) {
fa34f0
+            rr->expecting_100 = 0;
fa34f0
+        }
fa34f0
     }
fa34f0
 
fa34f0
     status_line = apr_pstrcat(r->pool, AP_SERVER_PROTOCOL, " ", r->status_line, CRLF, NULL);