470837
diff --git a/modules/http/http_filters.c b/modules/http/http_filters.c
470837
index 9828cdf..6bedcac 100644
470837
--- a/modules/http/http_filters.c
470837
+++ b/modules/http/http_filters.c
470837
@@ -1605,9 +1605,9 @@ AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status)
470837
  */
470837
 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
470837
 {
470837
+    int rc = OK;
470837
+    conn_rec *c = r->connection;
470837
     apr_bucket_brigade *bb;
470837
-    int seen_eos;
470837
-    apr_status_t rv;
470837
 
470837
     /* Sometimes we'll get in a state where the input handling has
470837
      * detected an error where we want to drop the connection, so if
470837
@@ -1616,54 +1616,57 @@ AP_DECLARE(int) ap_discard_request_body(request_rec *r)
470837
      *
470837
      * This function is also a no-op on a subrequest.
470837
      */
470837
-    if (r->main || r->connection->keepalive == AP_CONN_CLOSE ||
470837
-        ap_status_drops_connection(r->status)) {
470837
+    if (r->main || c->keepalive == AP_CONN_CLOSE) {
470837
+        return OK;
470837
+    }
470837
+    if (ap_status_drops_connection(r->status)) {
470837
+        c->keepalive = AP_CONN_CLOSE;
470837
         return OK;
470837
     }
470837
 
470837
     bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
470837
-    seen_eos = 0;
470837
-    do {
470837
-        apr_bucket *bucket;
470837
+    for (;;) {
470837
+        apr_status_t rv;
470837
 
470837
         rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
470837
                             APR_BLOCK_READ, HUGE_STRING_LEN);
470837
-
470837
         if (rv != APR_SUCCESS) {
470837
-            apr_brigade_destroy(bb);
470837
-            return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
470837
+            rc = ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
470837
+            goto cleanup;
470837
         }
470837
 
470837
-        for (bucket = APR_BRIGADE_FIRST(bb);
470837
-             bucket != APR_BRIGADE_SENTINEL(bb);
470837
-             bucket = APR_BUCKET_NEXT(bucket))
470837
-        {
470837
-            const char *data;
470837
-            apr_size_t len;
470837
+        while (!APR_BRIGADE_EMPTY(bb)) {
470837
+            apr_bucket *b = APR_BRIGADE_FIRST(bb);
470837
 
470837
-            if (APR_BUCKET_IS_EOS(bucket)) {
470837
-                seen_eos = 1;
470837
-                break;
470837
+            if (APR_BUCKET_IS_EOS(b)) {
470837
+                goto cleanup;
470837
             }
470837
 
470837
-            /* These are metadata buckets. */
470837
-            if (bucket->length == 0) {
470837
-                continue;
470837
-            }
470837
-
470837
-            /* We MUST read because in case we have an unknown-length
470837
-             * bucket or one that morphs, we want to exhaust it.
470837
+            /* There is no need to read empty or metadata buckets or
470837
+             * buckets of known length, but we MUST read buckets of
470837
+             * unknown length in order to exhaust them.
470837
              */
470837
-            rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
470837
+            if (b->length == (apr_size_t)-1) {
470837
+                apr_size_t len;
470837
+                const char *data;
470837
+
470837
+                rv = apr_bucket_read(b, &data, &len, APR_BLOCK_READ);
470837
             if (rv != APR_SUCCESS) {
470837
-                apr_brigade_destroy(bb);
470837
-                return HTTP_BAD_REQUEST;
470837
+                    rc = HTTP_BAD_REQUEST;
470837
+                    goto cleanup;
470837
             }
470837
         }
470837
-        apr_brigade_cleanup(bb);
470837
-    } while (!seen_eos);
470837
 
470837
-    return OK;
470837
+            apr_bucket_delete(b);
470837
+        }
470837
+    }
470837
+
470837
+cleanup:
470837
+    apr_brigade_cleanup(bb);
470837
+    if (rc != OK) {
470837
+        c->keepalive = AP_CONN_CLOSE;
470837
+    }
470837
+    return rc;
470837
 }
470837
 
470837
 /* Here we deal with getting the request message body from the client.
470837
diff --git a/server/protocol.c b/server/protocol.c
470837
index a2aa081..a554970 100644
470837
--- a/server/protocol.c
470837
+++ b/server/protocol.c
470837
@@ -1666,23 +1666,29 @@ AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew,
470837
     rnew->main = (request_rec *) r;
470837
 }
470837
 
470837
-static void end_output_stream(request_rec *r)
470837
+static void end_output_stream(request_rec *r, int status)
470837
 {
470837
     conn_rec *c = r->connection;
470837
     apr_bucket_brigade *bb;
470837
     apr_bucket *b;
470837
 
470837
     bb = apr_brigade_create(r->pool, c->bucket_alloc);
470837
+    if (status != OK) {
470837
+        b = ap_bucket_error_create(status, NULL, r->pool, c->bucket_alloc);
470837
+        APR_BRIGADE_INSERT_TAIL(bb, b);
470837
+    }
470837
     b = apr_bucket_eos_create(c->bucket_alloc);
470837
     APR_BRIGADE_INSERT_TAIL(bb, b);
470837
+
470837
     ap_pass_brigade(r->output_filters, bb);
470837
+    apr_brigade_cleanup(bb);
470837
 }
470837
 
470837
 AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
470837
 {
470837
     /* tell the filter chain there is no more content coming */
470837
     if (!sub->eos_sent) {
470837
-        end_output_stream(sub);
470837
+        end_output_stream(sub, OK);
470837
     }
470837
 }
470837
 
470837
@@ -1693,11 +1699,11 @@ AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub)
470837
  */
470837
 AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r)
470837
 {
470837
-    (void) ap_discard_request_body(r);
470837
+    int status = ap_discard_request_body(r);
470837
 
470837
     /* tell the filter chain there is no more content coming */
470837
     if (!r->eos_sent) {
470837
-        end_output_stream(r);
470837
+        end_output_stream(r, status);
470837
     }
470837
 }
470837