576df0
diff --git a/modules/proxy/mod_proxy_http.c b/modules/proxy/mod_proxy_http.c
576df0
index ec1e042..2c0500f 100644
576df0
--- a/modules/proxy/mod_proxy_http.c
576df0
+++ b/modules/proxy/mod_proxy_http.c
576df0
@@ -310,16 +310,18 @@ static int stream_reqbody_read(proxy_http_req_t *req, apr_bucket_brigade *bb,
576df0
     return OK;
576df0
 }
576df0
 
576df0
-static int stream_reqbody(proxy_http_req_t *req, rb_methods rb_method)
576df0
+static int stream_reqbody(proxy_http_req_t *req)
576df0
 {
576df0
     request_rec *r = req->r;
576df0
     int seen_eos = 0, rv = OK;
576df0
     apr_size_t hdr_len;
576df0
     char chunk_hdr[20];  /* must be here due to transient bucket. */
576df0
+    conn_rec *origin = req->origin;
576df0
     proxy_conn_rec *p_conn = req->backend;
576df0
     apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
576df0
     apr_bucket_brigade *header_brigade = req->header_brigade;
576df0
     apr_bucket_brigade *input_brigade = req->input_brigade;
576df0
+    rb_methods rb_method = req->rb_method;
576df0
     apr_off_t bytes, bytes_streamed = 0;
576df0
     apr_bucket *e;
576df0
 
576df0
@@ -333,7 +335,7 @@ static int stream_reqbody(proxy_http_req_t *req, rb_methods rb_method)
576df0
         }
576df0
 
576df0
         if (!APR_BRIGADE_EMPTY(input_brigade)) {
576df0
-            /* If this brigade contains EOS, either stop or remove it. */
576df0
+            /* If this brigade contains EOS, remove it and be done. */
576df0
             if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
576df0
                 seen_eos = 1;
576df0
 
576df0
@@ -375,7 +377,8 @@ static int stream_reqbody(proxy_http_req_t *req, rb_methods rb_method)
576df0
                     APR_BRIGADE_INSERT_TAIL(input_brigade, e);
576df0
                 }
576df0
             }
576df0
-            else if (bytes_streamed > req->cl_val) {
576df0
+            else if (rb_method == RB_STREAM_CL
576df0
+                     && bytes_streamed > req->cl_val) {
576df0
                 /* C-L < bytes streamed?!?
576df0
                  * We will error out after the body is completely
576df0
                  * consumed, but we can't stream more bytes at the
576df0
@@ -407,7 +410,7 @@ static int stream_reqbody(proxy_http_req_t *req, rb_methods rb_method)
576df0
         APR_BRIGADE_PREPEND(input_brigade, header_brigade);
576df0
 
576df0
         /* Flush here on EOS because we won't stream_reqbody_read() again */
576df0
-        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, req->origin,
576df0
+        rv = ap_proxy_pass_brigade(bucket_alloc, r, p_conn, origin,
576df0
                                    input_brigade, seen_eos);
576df0
         if (rv != OK) {
576df0
             return rv;
576df0
@@ -454,10 +457,6 @@ static int spool_reqbody_cl(proxy_http_req_t *req, apr_off_t *bytes_spooled)
576df0
         /* If this brigade contains EOS, either stop or remove it. */
576df0
         if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
576df0
             seen_eos = 1;
576df0
-
576df0
-            /* We can't pass this EOS to the output_filters. */
576df0
-            e = APR_BRIGADE_LAST(input_brigade);
576df0
-            apr_bucket_delete(e);
576df0
         }
576df0
 
576df0
         apr_brigade_length(input_brigade, 1, &bytes);
576df0
@@ -644,7 +643,18 @@ static int ap_proxy_http_prefetch(proxy_http_req_t *req,
576df0
      */
576df0
     temp_brigade = apr_brigade_create(p, bucket_alloc);
576df0
     block = req->prefetch_nonblocking ? APR_NONBLOCK_READ : APR_BLOCK_READ;
576df0
-    do {
576df0
+
576df0
+    /* Account for saved input, if any. */
576df0
+    apr_brigade_length(input_brigade, 0, &bytes_read);
576df0
+
576df0
+    /* Ensure we don't hit a wall where we have a buffer too small
576df0
+     * for ap_get_brigade's filters to fetch us another bucket,
576df0
+     * surrender once we hit 80 bytes less than MAX_MEM_SPOOL
576df0
+     * (an arbitrary value).
576df0
+     */
576df0
+    while (bytes_read < MAX_MEM_SPOOL - 80
576df0
+           && (APR_BRIGADE_EMPTY(input_brigade)
576df0
+               || !APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade)))) {
576df0
         status = ap_get_brigade(r->input_filters, temp_brigade,
576df0
                                 AP_MODE_READBYTES, block,
576df0
                                 MAX_MEM_SPOOL - bytes_read);
576df0
@@ -686,15 +696,7 @@ static int ap_proxy_http_prefetch(proxy_http_req_t *req,
576df0
                           c->client_ip, c->remote_host ? c->remote_host: "");
576df0
             return HTTP_INTERNAL_SERVER_ERROR;
576df0
         }
576df0
-
576df0
-    /* Ensure we don't hit a wall where we have a buffer too small
576df0
-     * for ap_get_brigade's filters to fetch us another bucket,
576df0
-     * surrender once we hit 80 bytes less than MAX_MEM_SPOOL
576df0
-     * (an arbitrary value.)
576df0
-     */
576df0
-    } while ((bytes_read < MAX_MEM_SPOOL - 80)
576df0
-              && !APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))
576df0
-              && !req->prefetch_nonblocking);
576df0
+    }
576df0
 
576df0
     /* Use chunked request body encoding or send a content-length body?
576df0
      *
576df0
@@ -838,35 +840,21 @@ static int ap_proxy_http_request(proxy_http_req_t *req)
576df0
 {
576df0
     int rv;
576df0
     request_rec *r = req->r;
576df0
-    apr_bucket_alloc_t *bucket_alloc = req->bucket_alloc;
576df0
-    apr_bucket_brigade *header_brigade = req->header_brigade;
576df0
-    apr_bucket_brigade *input_brigade = req->input_brigade;
576df0
 
576df0
     /* send the request header/body, if any. */
576df0
     switch (req->rb_method) {
576df0
+    case RB_SPOOL_CL:
576df0
     case RB_STREAM_CL:
576df0
     case RB_STREAM_CHUNKED:
576df0
         if (req->do_100_continue) {
576df0
-            rv = ap_proxy_pass_brigade(bucket_alloc, r, req->backend,
576df0
-                                       req->origin, header_brigade, 1);
576df0
+            rv = ap_proxy_pass_brigade(req->bucket_alloc, r, req->backend,
576df0
+                                       req->origin, req->header_brigade, 1);
576df0
         }
576df0
         else {
576df0
-            rv = stream_reqbody(req, req->rb_method);
576df0
+            rv = stream_reqbody(req);
576df0
         }
576df0
         break;
576df0
 
576df0
-    case RB_SPOOL_CL:
576df0
-        /* Prefetch has built the header and spooled the whole body;
576df0
-         * if we don't expect 100-continue we can flush both all at once,
576df0
-         * otherwise flush the header only.
576df0
-         */
576df0
-        if (!req->do_100_continue) {
576df0
-            APR_BRIGADE_CONCAT(header_brigade, input_brigade);
576df0
-        }
576df0
-        rv = ap_proxy_pass_brigade(bucket_alloc, r, req->backend,
576df0
-                                   req->origin, header_brigade, 1);
576df0
-        break;
576df0
-
576df0
     default:
576df0
         /* shouldn't be possible */
576df0
         rv = HTTP_INTERNAL_SERVER_ERROR;
576df0
@@ -1577,15 +1565,10 @@ int ap_proxy_http_process_response(proxy_http_req_t *req)
576df0
 
576df0
                 /* Send the request body (fully). */
576df0
                 switch(req->rb_method) {
576df0
+                case RB_SPOOL_CL:
576df0
                 case RB_STREAM_CL:
576df0
                 case RB_STREAM_CHUNKED:
576df0
-                    status = stream_reqbody(req, req->rb_method);
576df0
-                    break;
576df0
-                case RB_SPOOL_CL:
576df0
-                    /* Prefetch has spooled the whole body, flush it. */
576df0
-                    status = ap_proxy_pass_brigade(req->bucket_alloc, r,
576df0
-                                                   backend, origin,
576df0
-                                                   req->input_brigade, 1);
576df0
+                    status = stream_reqbody(req);
576df0
                     break;
576df0
                 default:
576df0
                     /* Shouldn't happen */
576df0
@@ -1940,6 +1923,7 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
576df0
     const char *u;
576df0
     proxy_http_req_t *req = NULL;
576df0
     proxy_conn_rec *backend = NULL;
576df0
+    apr_bucket_brigade *input_brigade = NULL;
576df0
     int is_ssl = 0;
576df0
     conn_rec *c = r->connection;
576df0
     proxy_dir_conf *dconf;
576df0
@@ -2005,8 +1989,20 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
576df0
 
576df0
     dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
576df0
 
576df0
+    /* We possibly reuse input data prefetched in previous call(s), e.g. for a
576df0
+     * balancer fallback scenario, and in this case the 100 continue settings
576df0
+     * should be consistent between balancer members. If not, we need to ignore
576df0
+     * Proxy100Continue on=>off once we tried to prefetch already, otherwise
576df0
+     * the HTTP_IN filter won't send 100 Continue for us anymore, and we might
576df0
+     * deadlock with the client waiting for each other. Note that off=>on is
576df0
+     * not an issue because in this case r->expecting_100 is false (the 100
576df0
+     * Continue is out already), but we make sure that prefetch will be
576df0
+     * nonblocking to avoid passing more time there. 
576df0
+     */
576df0
+    apr_pool_userdata_get((void **)&input_brigade, "proxy-req-input", p);
576df0
+
576df0
     /* Should we handle end-to-end or ping 100-continue? */
576df0
-    if ((r->expecting_100 && dconf->forward_100_continue)
576df0
+    if ((r->expecting_100 && (dconf->forward_100_continue || input_brigade))
576df0
             || PROXY_DO_100_CONTINUE(worker, r)) {
576df0
         /* We need to reset r->expecting_100 or prefetching will cause
576df0
          * ap_http_filter() to send "100 Continue" response by itself. So
576df0
@@ -2023,7 +2019,8 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
576df0
     /* Should we block while prefetching the body or try nonblocking and flush
576df0
      * data to the backend ASAP?
576df0
      */
576df0
-    else if (apr_table_get(r->subprocess_env, "proxy-prefetch-nonblocking")) {
576df0
+    else if (input_brigade || apr_table_get(r->subprocess_env,
576df0
+                                            "proxy-prefetch-nonblocking")) {
576df0
         req->prefetch_nonblocking = 1;
576df0
     }
576df0
 
576df0
@@ -2048,6 +2045,17 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
576df0
                                             sizeof(req->server_portstr))))
576df0
         goto cleanup;
576df0
 
576df0
+    /* The header is always (re-)built since it depends on worker settings,
576df0
+     * but the body can be fetched only once (even partially), so it's saved
576df0
+     * in between proxy_http_handler() calls should we come back here.
576df0
+     */
576df0
+    req->header_brigade = apr_brigade_create(p, req->bucket_alloc);
576df0
+    if (input_brigade == NULL) {
576df0
+        input_brigade = apr_brigade_create(p, req->bucket_alloc);
576df0
+        apr_pool_userdata_setn(input_brigade, "proxy-req-input", NULL, p);
576df0
+    }
576df0
+    req->input_brigade = input_brigade;
576df0
+
576df0
     /* Prefetch (nonlocking) the request body so to increase the chance to get
576df0
      * the whole (or enough) body and determine Content-Length vs chunked or
576df0
      * spooled. By doing this before connecting or reusing the backend, we want
576df0
@@ -2058,8 +2066,6 @@ static int proxy_http_handler(request_rec *r, proxy_worker *worker,
576df0
      * to reduce to the minimum the unavoidable local is_socket_connected() vs
576df0
      * remote keepalive race condition.
576df0
      */
576df0
-    req->input_brigade = apr_brigade_create(p, req->bucket_alloc);
576df0
-    req->header_brigade = apr_brigade_create(p, req->bucket_alloc);
576df0
     if ((status = ap_proxy_http_prefetch(req, uri, locurl)) != OK)
576df0
         goto cleanup;
576df0