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