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