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