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