Blame SOURCES/httpd-2.4.6-r1811746.patch

008793
--- a/server/protocol.c  2017/10/10 17:47:25 1811745
008793
+++ b/server/protocol.c  2017/10/10 17:51:13 1811746
008793
@@ -1674,62 +1674,88 @@
008793
         ctx->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
008793
     }
008793
 
008793
-    /* Loop through this set of buckets to compute their length
008793
-     */
008793
+    /* Loop through the brigade to count the length. To avoid
008793
+     * arbitrary memory consumption with morphing bucket types, this
008793
+     * loop will stop and pass on the brigade when necessary. */
008793
     e = APR_BRIGADE_FIRST(b);
008793
     while (e != APR_BRIGADE_SENTINEL(b)) {
008793
+        apr_status_t rv;
008793
+
008793
         if (APR_BUCKET_IS_EOS(e)) {
008793
             eos = 1;
008793
             break;
008793
         }
008793
-        if (e->length == (apr_size_t)-1) {
008793
+        /* For a flush bucket, fall through to pass the brigade and
008793
+         * flush now. */
008793
+        else if (APR_BUCKET_IS_FLUSH(e)) {
008793
+            e = APR_BUCKET_NEXT(e);
008793
+        }
008793
+        /* For metadata bucket types other than FLUSH, loop. */
008793
+        else if (APR_BUCKET_IS_METADATA(e)) {
008793
+            e = APR_BUCKET_NEXT(e);
008793
+            continue;
008793
+        }
008793
+        /* For determinate length data buckets, count the length and
008793
+         * continue. */
008793
+        else if (e->length != (apr_size_t)-1) {
008793
+            r->bytes_sent += e->length;
008793
+            e = APR_BUCKET_NEXT(e);
008793
+            continue;
008793
+        }
008793
+        /* For indeterminate length data buckets, perform one read. */
008793
+        else /* e->length == (apr_size_t)-1 */ {
008793
             apr_size_t len;
008793
             const char *ignored;
008793
-            apr_status_t rv;
008793
-
008793
-            /* This is probably a pipe bucket.  Send everything
008793
-             * prior to this, and then read the data for this bucket.
008793
-             */
008793
+        
008793
             rv = apr_bucket_read(e, &ignored, &len, eblock);
008793
+            if ((rv != APR_SUCCESS) && !APR_STATUS_IS_EAGAIN(rv)) {
008793
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00574)
008793
+                              "ap_content_length_filter: "
008793
+                              "apr_bucket_read() failed");
008793
+                return rv;
008793
+            }
008793
             if (rv == APR_SUCCESS) {
008793
-                /* Attempt a nonblocking read next time through */
008793
                 eblock = APR_NONBLOCK_READ;
008793
+                e = APR_BUCKET_NEXT(e);
008793
                 r->bytes_sent += len;
008793
             }
008793
             else if (APR_STATUS_IS_EAGAIN(rv)) {
008793
-                /* Output everything prior to this bucket, and then
008793
-                 * do a blocking read on the next batch.
008793
-                 */
008793
-                if (e != APR_BRIGADE_FIRST(b)) {
008793
-                    apr_bucket *flush;
008793
-                    apr_brigade_split_ex(b, e, ctx->tmpbb);
008793
-                    flush = apr_bucket_flush_create(r->connection->bucket_alloc);
008793
-
008793
-                    APR_BRIGADE_INSERT_TAIL(b, flush);
008793
-                    rv = ap_pass_brigade(f->next, b);
008793
-                    if (rv != APR_SUCCESS || f->c->aborted) {
008793
-                        return rv;
008793
-                    }
008793
-                    apr_brigade_cleanup(b);
008793
-                    APR_BRIGADE_CONCAT(b, ctx->tmpbb);
008793
-                    e = APR_BRIGADE_FIRST(b);
008793
+                apr_bucket *flush;
008793
 
008793
-                    ctx->data_sent = 1;
008793
-                }
008793
+                /* Next read must block. */
008793
                 eblock = APR_BLOCK_READ;
008793
-                continue;
008793
-            }
008793
-            else {
008793
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00574)
008793
-                              "ap_content_length_filter: "
008793
-                              "apr_bucket_read() failed");
008793
-                return rv;
008793
+
008793
+                /* Ensure the last bucket to pass down is a flush if
008793
+                 * the next read will block. */
008793
+                flush = apr_bucket_flush_create(f->c->bucket_alloc);
008793
+                APR_BUCKET_INSERT_BEFORE(e, flush);
008793
             }
008793
         }
008793
-        else {
008793
-            r->bytes_sent += e->length;
008793
+
008793
+        /* Optimization: if the next bucket is EOS (directly after a
008793
+         * bucket morphed to the heap, or a flush), short-cut to
008793
+         * handle EOS straight away - allowing C-L to be determined
008793
+         * for content which is already entirely in memory. */
008793
+        if (e != APR_BRIGADE_SENTINEL(b) && APR_BUCKET_IS_EOS(e)) {
008793
+            continue;
008793
+        }
008793
+
008793
+        /* On reaching here, pass on everything in the brigade up to
008793
+         * this point. */
008793
+        apr_brigade_split_ex(b, e, ctx->tmpbb);
008793
+        
008793
+        rv = ap_pass_brigade(f->next, b);
008793
+        if (rv != APR_SUCCESS) {
008793
+            return rv;
008793
+        }
008793
+        else if (f->c->aborted) {
008793
+            return APR_ECONNABORTED;
008793
         }
008793
-        e = APR_BUCKET_NEXT(e);
008793
+        apr_brigade_cleanup(b);
008793
+        APR_BRIGADE_CONCAT(b, ctx->tmpbb);
008793
+        e = APR_BRIGADE_FIRST(b);
008793
+        
008793
+        ctx->data_sent = 1;
008793
     }
008793
 
008793
     /* If we've now seen the entire response and it's otherwise