8335b1
diff --git a/include/http_protocol.h b/include/http_protocol.h
8335b1
index 415270b..67fa02f 100644
8335b1
--- a/include/http_protocol.h
8335b1
+++ b/include/http_protocol.h
8335b1
@@ -502,6 +502,23 @@ AP_DECLARE(int) ap_should_client_block(request_rec *r);
8335b1
  */
8335b1
 AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
8335b1
 
8335b1
+/*
8335b1
+ * Map specific APR codes returned by the filter stack to HTTP error
8335b1
+ * codes, or the default status code provided. Use it as follows:
8335b1
+ *
8335b1
+ * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
8335b1
+ *
8335b1
+ * If the filter has already handled the error, AP_FILTER_ERROR will
8335b1
+ * be returned, which is cleanly passed through.
8335b1
+ *
8335b1
+ * These mappings imply that the filter stack is reading from the
8335b1
+ * downstream client, the proxy will map these codes differently.
8335b1
+ * @param rv APR status code
8335b1
+ * @param status Default HTTP code should the APR code not be recognised
8335b1
+ * @return Mapped HTTP status code
8335b1
+ */
8335b1
+AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status);
8335b1
+
8335b1
 /**
8335b1
  * In HTTP/1.1, any method can have a body.  However, most GET handlers
8335b1
  * wouldn't know what to do with a request body if they received one.
8335b1
diff --git a/modules/http/http_filters.c b/modules/http/http_filters.c
8335b1
index 1dde402..ed8749f 100644
8335b1
--- a/modules/http/http_filters.c
8335b1
+++ b/modules/http/http_filters.c
8335b1
@@ -57,24 +57,29 @@
8335b1
 
8335b1
 APLOG_USE_MODULE(http);
8335b1
 
8335b1
-#define INVALID_CHAR -2
8335b1
-
8335b1
-static long get_chunk_size(char *);
8335b1
-
8335b1
-typedef struct http_filter_ctx {
8335b1
+typedef struct http_filter_ctx
8335b1
+{
8335b1
     apr_off_t remaining;
8335b1
     apr_off_t limit;
8335b1
     apr_off_t limit_used;
8335b1
-    enum {
8335b1
-        BODY_NONE,
8335b1
-        BODY_LENGTH,
8335b1
-        BODY_CHUNK,
8335b1
-        BODY_CHUNK_PART
8335b1
+    apr_int32_t chunk_used;
8335b1
+    apr_int32_t chunk_bws;
8335b1
+    apr_int32_t chunkbits;
8335b1
+    enum
8335b1
+    {
8335b1
+        BODY_NONE, /* streamed data */
8335b1
+        BODY_LENGTH, /* data constrained by content length */
8335b1
+        BODY_CHUNK, /* chunk expected */
8335b1
+        BODY_CHUNK_PART, /* chunk digits */
8335b1
+        BODY_CHUNK_EXT, /* chunk extension */
8335b1
+        BODY_CHUNK_CR, /* got space(s) after digits, expect [CR]LF or ext */
8335b1
+        BODY_CHUNK_LF, /* got CR after digits or ext, expect LF */
8335b1
+        BODY_CHUNK_DATA, /* data constrained by chunked encoding */
8335b1
+        BODY_CHUNK_END, /* chunked data terminating CRLF */
8335b1
+        BODY_CHUNK_END_LF, /* got CR after data, expect LF */
8335b1
+        BODY_CHUNK_TRAILER /* trailers */
8335b1
     } state;
8335b1
-    int eos_sent;
8335b1
-    char chunk_ln[32];
8335b1
-    char *pos;
8335b1
-    apr_off_t linesize;
8335b1
+    unsigned int eos_sent :1;
8335b1
     apr_bucket_brigade *bb;
8335b1
 } http_ctx_t;
8335b1
 
8335b1
@@ -87,6 +92,23 @@ static apr_status_t bail_out_on_error(http_ctx_t *ctx,
8335b1
     apr_bucket_brigade *bb = ctx->bb;
8335b1
 
8335b1
     apr_brigade_cleanup(bb);
8335b1
+
8335b1
+    if (f->r->proxyreq == PROXYREQ_RESPONSE) {
8335b1
+        switch (http_error) {
8335b1
+        case HTTP_REQUEST_ENTITY_TOO_LARGE:
8335b1
+            return APR_ENOSPC;
8335b1
+
8335b1
+        case HTTP_REQUEST_TIME_OUT:
8335b1
+            return APR_INCOMPLETE;
8335b1
+
8335b1
+        case HTTP_NOT_IMPLEMENTED:
8335b1
+            return APR_ENOTIMPL;
8335b1
+
8335b1
+        default:
8335b1
+            return APR_EGENERAL;
8335b1
+        }
8335b1
+    }
8335b1
+
8335b1
     e = ap_bucket_error_create(http_error,
8335b1
                                NULL, f->r->pool,
8335b1
                                f->c->bucket_alloc);
8335b1
@@ -102,117 +124,154 @@ static apr_status_t bail_out_on_error(http_ctx_t *ctx,
8335b1
     return ap_pass_brigade(f->r->output_filters, bb);
8335b1
 }
8335b1
 
8335b1
-static apr_status_t get_remaining_chunk_line(http_ctx_t *ctx,
8335b1
-                                             apr_bucket_brigade *b,
8335b1
-                                             int linelimit)
8335b1
+/**
8335b1
+ * Parse a chunk line with optional extension, detect overflow.
8335b1
+ * There are two error cases:
8335b1
+ *  1) If the conversion would require too many bits, APR_EGENERAL is returned.
8335b1
+ *  2) If the conversion used the correct number of bits, but an overflow
8335b1
+ *     caused only the sign bit to flip, then APR_ENOSPC is returned.
8335b1
+ * In general, any negative number can be considered an overflow error.
8335b1
+ */
8335b1
+static apr_status_t parse_chunk_size(http_ctx_t *ctx, const char *buffer,
8335b1
+                                     apr_size_t len, int linelimit)
8335b1
 {
8335b1
-    apr_status_t rv;
8335b1
-    apr_off_t brigade_length;
8335b1
-    apr_bucket *e;
8335b1
-    const char *lineend;
8335b1
-    apr_size_t len = 0;
8335b1
+    apr_size_t i = 0;
8335b1
 
8335b1
-    /*
8335b1
-     * As the brigade b should have been requested in mode AP_MODE_GETLINE
8335b1
-     * all buckets in this brigade are already some type of memory
8335b1
-     * buckets (due to the needed scanning for LF in mode AP_MODE_GETLINE)
8335b1
-     * or META buckets.
8335b1
-     */
8335b1
-    rv = apr_brigade_length(b, 0, &brigade_length);
8335b1
-    if (rv != APR_SUCCESS) {
8335b1
-        return rv;
8335b1
-    }
8335b1
-    /* Sanity check. Should never happen. See above. */
8335b1
-    if (brigade_length == -1) {
8335b1
-        return APR_EGENERAL;
8335b1
-    }
8335b1
-    if (!brigade_length) {
8335b1
-        return APR_EAGAIN;
8335b1
-    }
8335b1
-    ctx->linesize += brigade_length;
8335b1
-    if (ctx->linesize > linelimit) {
8335b1
-        return APR_ENOSPC;
8335b1
-    }
8335b1
-    /*
8335b1
-     * As all buckets are already some type of memory buckets or META buckets
8335b1
-     * (see above), we only need to check the last byte in the last data bucket.
8335b1
-     */
8335b1
-    for (e = APR_BRIGADE_LAST(b);
8335b1
-         e != APR_BRIGADE_SENTINEL(b);
8335b1
-         e = APR_BUCKET_PREV(e)) {
8335b1
+    while (i < len) {
8335b1
+        char c = buffer[i];
8335b1
+
8335b1
+        ap_xlate_proto_from_ascii(&c, 1);
8335b1
 
8335b1
-        if (APR_BUCKET_IS_METADATA(e)) {
8335b1
+        /* handle CRLF after the chunk */
8335b1
+        if (ctx->state == BODY_CHUNK_END
8335b1
+                || ctx->state == BODY_CHUNK_END_LF) {
8335b1
+            if (c == LF) {
8335b1
+                ctx->state = BODY_CHUNK;
8335b1
+            }
8335b1
+            else if (c == CR && ctx->state == BODY_CHUNK_END) {
8335b1
+                ctx->state = BODY_CHUNK_END_LF;
8335b1
+            }
8335b1
+            else {
8335b1
+                /*
8335b1
+                 * LF expected.
8335b1
+                 */
8335b1
+                return APR_EINVAL;
8335b1
+            }
8335b1
+            i++;
8335b1
             continue;
8335b1
         }
8335b1
-        rv = apr_bucket_read(e, &lineend, &len, APR_BLOCK_READ);
8335b1
-        if (rv != APR_SUCCESS) {
8335b1
-            return rv;
8335b1
+
8335b1
+        /* handle start of the chunk */
8335b1
+        if (ctx->state == BODY_CHUNK) {
8335b1
+            if (!apr_isxdigit(c)) {
8335b1
+                /*
8335b1
+                 * Detect invalid character at beginning. This also works for
8335b1
+                 * empty chunk size lines.
8335b1
+                 */
8335b1
+                return APR_EINVAL;
8335b1
+            }
8335b1
+            else {
8335b1
+                ctx->state = BODY_CHUNK_PART;
8335b1
+            }
8335b1
+            ctx->remaining = 0;
8335b1
+            ctx->chunkbits = sizeof(apr_off_t) * 8;
8335b1
+            ctx->chunk_used = 0;
8335b1
+            ctx->chunk_bws = 0;
8335b1
         }
8335b1
-        if (len > 0) {
8335b1
-            break;  /* we got the data we want */
8335b1
+
8335b1
+        if (c == LF) {
8335b1
+            if (ctx->remaining) {
8335b1
+                ctx->state = BODY_CHUNK_DATA;
8335b1
+            }
8335b1
+            else {
8335b1
+                ctx->state = BODY_CHUNK_TRAILER;
8335b1
+            }
8335b1
         }
8335b1
-        /* If we got a zero-length data bucket, we try the next one */
8335b1
-    }
8335b1
-    /* We had no data in this brigade */
8335b1
-    if (!len || e == APR_BRIGADE_SENTINEL(b)) {
8335b1
-        return APR_EAGAIN;
8335b1
-    }
8335b1
-    if (lineend[len - 1] != APR_ASCII_LF) {
8335b1
-        return APR_EAGAIN;
8335b1
-    }
8335b1
-    /* Line is complete. So reset ctx for next round. */
8335b1
-    ctx->linesize = 0;
8335b1
-    ctx->pos = ctx->chunk_ln;
8335b1
-    return APR_SUCCESS;
8335b1
-}
8335b1
+        else if (ctx->state == BODY_CHUNK_LF) {
8335b1
+            /*
8335b1
+             * LF expected.
8335b1
+             */
8335b1
+            return APR_EINVAL;
8335b1
+        }
8335b1
+        else if (c == CR) {
8335b1
+            ctx->state = BODY_CHUNK_LF;
8335b1
+        }
8335b1
+        else if (c == ';') {
8335b1
+            ctx->state = BODY_CHUNK_EXT;
8335b1
+        }
8335b1
+        else if (ctx->state == BODY_CHUNK_EXT) {
8335b1
+            /*
8335b1
+             * Control chars (but tabs) are invalid.
8335b1
+             */
8335b1
+            if (c != '\t' && apr_iscntrl(c)) {
8335b1
+                return APR_EINVAL;
8335b1
+            }
8335b1
+        }
8335b1
+        else if (c == ' ' || c == '\t') {
8335b1
+            /* Be lenient up to 10 BWS (term from rfc7230 - 3.2.3).
8335b1
+             */
8335b1
+            ctx->state = BODY_CHUNK_CR;
8335b1
+            if (++ctx->chunk_bws > 10) {
8335b1
+                return APR_EINVAL;
8335b1
+            }
8335b1
+        }
8335b1
+        else if (ctx->state == BODY_CHUNK_CR) {
8335b1
+            /*
8335b1
+             * ';', CR or LF expected.
8335b1
+             */
8335b1
+            return APR_EINVAL;
8335b1
+        }
8335b1
+        else if (ctx->state == BODY_CHUNK_PART) {
8335b1
+            int xvalue;
8335b1
 
8335b1
-static apr_status_t get_chunk_line(http_ctx_t *ctx, apr_bucket_brigade *b,
8335b1
-                                   int linelimit)
8335b1
-{
8335b1
-    apr_size_t len;
8335b1
-    int tmp_len;
8335b1
-    apr_status_t rv;
8335b1
+            /* ignore leading zeros */
8335b1
+            if (!ctx->remaining && c == '0') {
8335b1
+                i++;
8335b1
+                continue;
8335b1
+            }
8335b1
 
8335b1
-    tmp_len = sizeof(ctx->chunk_ln) - (ctx->pos - ctx->chunk_ln) - 1;
8335b1
-    /* Saveguard ourselves against underflows */
8335b1
-    if (tmp_len < 0) {
8335b1
-        len = 0;
8335b1
-    }
8335b1
-    else {
8335b1
-        len = (apr_size_t) tmp_len;
8335b1
-    }
8335b1
-    /*
8335b1
-     * Check if there is space left in ctx->chunk_ln. If not, then either
8335b1
-     * the chunk size is insane or we have chunk-extensions. Ignore both
8335b1
-     * by discarding the remaining part of the line via
8335b1
-     * get_remaining_chunk_line. Only bail out if the line is too long.
8335b1
-     */
8335b1
-    if (len > 0) {
8335b1
-        rv = apr_brigade_flatten(b, ctx->pos, &len;;
8335b1
-        if (rv != APR_SUCCESS) {
8335b1
-            return rv;
8335b1
+            ctx->chunkbits -= 4;
8335b1
+            if (ctx->chunkbits < 0) {
8335b1
+                /* overflow */
8335b1
+                return APR_ENOSPC;
8335b1
+            }
8335b1
+
8335b1
+            if (c >= '0' && c <= '9') {
8335b1
+                xvalue = c - '0';
8335b1
+            }
8335b1
+            else if (c >= 'A' && c <= 'F') {
8335b1
+                xvalue = c - 'A' + 0xa;
8335b1
+            }
8335b1
+            else if (c >= 'a' && c <= 'f') {
8335b1
+                xvalue = c - 'a' + 0xa;
8335b1
+            }
8335b1
+            else {
8335b1
+                /* bogus character */
8335b1
+                return APR_EINVAL;
8335b1
+            }
8335b1
+
8335b1
+            ctx->remaining = (ctx->remaining << 4) | xvalue;
8335b1
+            if (ctx->remaining < 0) {
8335b1
+                /* overflow */
8335b1
+                return APR_ENOSPC;
8335b1
+            }
8335b1
         }
8335b1
-        ctx->pos += len;
8335b1
-        ctx->linesize += len;
8335b1
-        *(ctx->pos) = '\0';
8335b1
-        /*
8335b1
-         * Check if we really got a full line. If yes the
8335b1
-         * last char in the just read buffer must be LF.
8335b1
-         * If not advance the buffer and return APR_EAGAIN.
8335b1
-         * We do not start processing until we have the
8335b1
-         * full line.
8335b1
-         */
8335b1
-        if (ctx->pos[-1] != APR_ASCII_LF) {
8335b1
-            /* Check if the remaining data in the brigade has the LF */
8335b1
-            return get_remaining_chunk_line(ctx, b, linelimit);
8335b1
+        else {
8335b1
+            /* Should not happen */
8335b1
+            return APR_EGENERAL;
8335b1
         }
8335b1
-        /* Line is complete. So reset ctx->pos for next round. */
8335b1
-        ctx->pos = ctx->chunk_ln;
8335b1
-        return APR_SUCCESS;
8335b1
+
8335b1
+        i++;
8335b1
     }
8335b1
-    return get_remaining_chunk_line(ctx, b, linelimit);
8335b1
-}
8335b1
 
8335b1
+    /* sanity check */
8335b1
+    ctx->chunk_used += len;
8335b1
+    if (ctx->chunk_used < 0 || ctx->chunk_used > linelimit) {
8335b1
+        return APR_ENOSPC;
8335b1
+    }
8335b1
+
8335b1
+    return APR_SUCCESS;
8335b1
+}
8335b1
 
8335b1
 static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
8335b1
                                           apr_bucket_brigade *b, int merge)
8335b1
@@ -226,7 +285,6 @@ static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
8335b1
     r->status = HTTP_OK;
8335b1
     r->headers_in = r->trailers_in;
8335b1
     apr_table_clear(r->headers_in);
8335b1
-    ctx->state = BODY_NONE;
8335b1
     ap_get_mime_headers(r);
8335b1
 
8335b1
     if(r->status == HTTP_OK) {
8335b1
@@ -239,7 +297,7 @@ static apr_status_t read_chunked_trailers(http_ctx_t *ctx, ap_filter_t *f,
8335b1
     else {
8335b1
         const char *error_notes = apr_table_get(r->notes,
8335b1
                                                 "error-notes");
8335b1
-        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, 
8335b1
+        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02656)
8335b1
                       "Error while reading HTTP trailer: %i%s%s",
8335b1
                       r->status, error_notes ? ": " : "",
8335b1
                       error_notes ? error_notes : "");
8335b1
@@ -270,9 +328,9 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
     apr_bucket *e;
8335b1
     http_ctx_t *ctx = f->ctx;
8335b1
     apr_status_t rv;
8335b1
-    apr_off_t totalread;
8335b1
     int http_error = HTTP_REQUEST_ENTITY_TOO_LARGE;
8335b1
     apr_bucket_brigade *bb;
8335b1
+    int again;
8335b1
 
8335b1
     conf = (core_server_config *)
8335b1
         ap_get_module_config(f->r->server->module_config, &core_module);
8335b1
@@ -286,7 +344,6 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
         const char *tenc, *lenp;
8335b1
         f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
8335b1
         ctx->state = BODY_NONE;
8335b1
-        ctx->pos = ctx->chunk_ln;
8335b1
         ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
8335b1
         bb = ctx->bb;
8335b1
 
8335b1
@@ -306,25 +363,33 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
         lenp = apr_table_get(f->r->headers_in, "Content-Length");
8335b1
 
8335b1
         if (tenc) {
8335b1
-            if (!strcasecmp(tenc, "chunked")) {
8335b1
+            if (strcasecmp(tenc, "chunked") == 0 /* fast path */
8335b1
+                    || ap_find_last_token(f->r->pool, tenc, "chunked")) {
8335b1
                 ctx->state = BODY_CHUNK;
8335b1
             }
8335b1
-            /* test lenp, because it gives another case we can handle */
8335b1
-            else if (!lenp) {
8335b1
-                /* Something that isn't in HTTP, unless some future
8335b1
+            else if (f->r->proxyreq == PROXYREQ_RESPONSE) {
8335b1
+                /* http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-23
8335b1
+                 * Section 3.3.3.3: "If a Transfer-Encoding header field is
8335b1
+                 * present in a response and the chunked transfer coding is not
8335b1
+                 * the final encoding, the message body length is determined by
8335b1
+                 * reading the connection until it is closed by the server."
8335b1
+                 */
8335b1
+                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(02555)
8335b1
+                              "Unknown Transfer-Encoding: %s; "
8335b1
+                              "using read-until-close", tenc);
8335b1
+                tenc = NULL;
8335b1
+            }
8335b1
+            else {
8335b1
+                /* Something that isn't a HTTP request, unless some future
8335b1
                  * edition defines new transfer encodings, is unsupported.
8335b1
                  */
8335b1
                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01585)
8335b1
                               "Unknown Transfer-Encoding: %s", tenc);
8335b1
-                return bail_out_on_error(ctx, f, HTTP_NOT_IMPLEMENTED);
8335b1
-            }
8335b1
-            else {
8335b1
-                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r, APLOGNO(01586)
8335b1
-                  "Unknown Transfer-Encoding: %s; using Content-Length", tenc);
8335b1
-                tenc = NULL;
8335b1
+                return bail_out_on_error(ctx, f, HTTP_BAD_REQUEST);
8335b1
             }
8335b1
+            lenp = NULL;
8335b1
         }
8335b1
-        if (lenp && !tenc) {
8335b1
+        if (lenp) {
8335b1
             char *endstr;
8335b1
 
8335b1
             ctx->state = BODY_LENGTH;
8335b1
@@ -339,7 +404,7 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
                 ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01587)
8335b1
                               "Invalid Content-Length");
8335b1
 
8335b1
-                return bail_out_on_error(ctx, f, HTTP_REQUEST_ENTITY_TOO_LARGE);
8335b1
+                return bail_out_on_error(ctx, f, HTTP_BAD_REQUEST);
8335b1
             }
8335b1
 
8335b1
             /* If we have a limit in effect and we know the C-L ahead of
8335b1
@@ -381,7 +446,8 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
             if (!ap_is_HTTP_SUCCESS(f->r->status)) {
8335b1
                 ctx->state = BODY_NONE;
8335b1
                 ctx->eos_sent = 1;
8335b1
-            } else {
8335b1
+            }
8335b1
+            else {
8335b1
                 char *tmp;
8335b1
                 int len;
8335b1
 
8335b1
@@ -389,7 +455,7 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
                  * in a state of expecting one.
8335b1
                  */
8335b1
                 f->r->expecting_100 = 0;
8335b1
-                tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL, " ",
8335b1
+                tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL " ",
8335b1
                                   ap_get_status_line(HTTP_CONTINUE), CRLF CRLF,
8335b1
                                   NULL);
8335b1
                 len = strlen(tmp);
8335b1
@@ -401,279 +467,205 @@ apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
8335b1
                 e = apr_bucket_flush_create(f->c->bucket_alloc);
8335b1
                 APR_BRIGADE_INSERT_TAIL(bb, e);
8335b1
 
8335b1
-                ap_pass_brigade(f->c->output_filters, bb);
8335b1
-            }
8335b1
-        }
8335b1
-
8335b1
-        /* We can't read the chunk until after sending 100 if required. */
8335b1
-        if (ctx->state == BODY_CHUNK) {
8335b1
-            apr_brigade_cleanup(bb);
8335b1
-
8335b1
-            rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
8335b1
-                                block, 0);
8335b1
-
8335b1
-            /* for timeout */
8335b1
-            if (block == APR_NONBLOCK_READ &&
8335b1
-                ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
8335b1
-                  (APR_STATUS_IS_EAGAIN(rv)) )) {
8335b1
-                ctx->state = BODY_CHUNK_PART;
8335b1
-                return APR_EAGAIN;
8335b1
-            }
8335b1
-
8335b1
-            if (rv == APR_SUCCESS) {
8335b1
-                rv = get_chunk_line(ctx, bb, f->r->server->limit_req_line);
8335b1
-                if (APR_STATUS_IS_EAGAIN(rv)) {
8335b1
-                    apr_brigade_cleanup(bb);
8335b1
-                    ctx->state = BODY_CHUNK_PART;
8335b1
-                    return rv;
8335b1
-                }
8335b1
-                if (rv == APR_SUCCESS) {
8335b1
-                    ctx->remaining = get_chunk_size(ctx->chunk_ln);
8335b1
-                    if (ctx->remaining == INVALID_CHAR) {
8335b1
-                        rv = APR_EGENERAL;
8335b1
-                        http_error = HTTP_BAD_REQUEST;
8335b1
-                    }
8335b1
+                rv = ap_pass_brigade(f->c->output_filters, bb);
8335b1
+                if (rv != APR_SUCCESS) {
8335b1
+                    return AP_FILTER_ERROR;
8335b1
                 }
8335b1
             }
8335b1
-            apr_brigade_cleanup(bb);
8335b1
-
8335b1
-            /* Detect chunksize error (such as overflow) */
8335b1
-            if (rv != APR_SUCCESS || ctx->remaining < 0) {
8335b1
-                ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01589) "Error reading first chunk %s ",
8335b1
-                              (ctx->remaining < 0) ? "(overflow)" : "");
8335b1
-                ctx->remaining = 0; /* Reset it in case we have to
8335b1
-                                     * come back here later */
8335b1
-                if (APR_STATUS_IS_TIMEUP(rv)) {
8335b1
-                    http_error = HTTP_REQUEST_TIME_OUT;
8335b1
-                }
8335b1
-                return bail_out_on_error(ctx, f, http_error);
8335b1
-            }
8335b1
-
8335b1
-            if (!ctx->remaining) {
8335b1
-                return read_chunked_trailers(ctx, f, b,
8335b1
-                        conf->merge_trailers == AP_MERGE_TRAILERS_ENABLE);
8335b1
-            }
8335b1
         }
8335b1
     }
8335b1
-    else {
8335b1
-        bb = ctx->bb;
8335b1
-    }
8335b1
 
8335b1
+    /* sanity check in case we're read twice */
8335b1
     if (ctx->eos_sent) {
8335b1
         e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
         APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
         return APR_SUCCESS;
8335b1
     }
8335b1
 
8335b1
-    if (!ctx->remaining) {
8335b1
+    do {
8335b1
+        apr_brigade_cleanup(b);
8335b1
+        again = 0; /* until further notice */
8335b1
+
8335b1
+        /* read and handle the brigade */
8335b1
         switch (ctx->state) {
8335b1
-        case BODY_NONE:
8335b1
-            break;
8335b1
-        case BODY_LENGTH:
8335b1
-            e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
-            APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
-            ctx->eos_sent = 1;
8335b1
-            return APR_SUCCESS;
8335b1
         case BODY_CHUNK:
8335b1
         case BODY_CHUNK_PART:
8335b1
-            {
8335b1
-                apr_brigade_cleanup(bb);
8335b1
+        case BODY_CHUNK_EXT:
8335b1
+        case BODY_CHUNK_CR:
8335b1
+        case BODY_CHUNK_LF:
8335b1
+        case BODY_CHUNK_END:
8335b1
+        case BODY_CHUNK_END_LF: {
8335b1
 
8335b1
-                /* We need to read the CRLF after the chunk.  */
8335b1
-                if (ctx->state == BODY_CHUNK) {
8335b1
-                    rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
8335b1
-                                        block, 0);
8335b1
-                    if (block == APR_NONBLOCK_READ &&
8335b1
-                        ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
8335b1
-                          (APR_STATUS_IS_EAGAIN(rv)) )) {
8335b1
-                        return APR_EAGAIN;
8335b1
-                    }
8335b1
-                    /* If we get an error, then leave */
8335b1
-                    if (rv != APR_SUCCESS) {
8335b1
-                        return rv;
8335b1
-                    }
8335b1
-                    /*
8335b1
-                     * We really don't care whats on this line. If it is RFC
8335b1
-                     * compliant it should be only \r\n. If there is more
8335b1
-                     * before we just ignore it as long as we do not get over
8335b1
-                     * the limit for request lines.
8335b1
-                     */
8335b1
-                    rv = get_remaining_chunk_line(ctx, bb,
8335b1
-                                                  f->r->server->limit_req_line);
8335b1
-                    apr_brigade_cleanup(bb);
8335b1
-                    if (APR_STATUS_IS_EAGAIN(rv)) {
8335b1
-                        return rv;
8335b1
-                    }
8335b1
-                } else {
8335b1
-                    rv = APR_SUCCESS;
8335b1
-                }
8335b1
+            rv = ap_get_brigade(f->next, b, AP_MODE_GETLINE, block, 0);
8335b1
+
8335b1
+            /* for timeout */
8335b1
+            if (block == APR_NONBLOCK_READ
8335b1
+                    && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
8335b1
+                            || (APR_STATUS_IS_EAGAIN(rv)))) {
8335b1
+                return APR_EAGAIN;
8335b1
+            }
8335b1
+
8335b1
+            if (rv == APR_EOF) {
8335b1
+                return APR_INCOMPLETE;
8335b1
+            }
8335b1
+
8335b1
+            if (rv != APR_SUCCESS) {
8335b1
+                return rv;
8335b1
+            }
8335b1
+
8335b1
+            e = APR_BRIGADE_FIRST(b);
8335b1
+            while (e != APR_BRIGADE_SENTINEL(b)) {
8335b1
+                const char *buffer;
8335b1
+                apr_size_t len;
8335b1
+
8335b1
+                if (!APR_BUCKET_IS_METADATA(e)) {
8335b1
+                    int parsing = 0;
8335b1
+
8335b1
+                    rv = apr_bucket_read(e, &buffer, &len, APR_BLOCK_READ);
8335b1
 
8335b1
-                if (rv == APR_SUCCESS) {
8335b1
-                    /* Read the real chunk line. */
8335b1
-                    rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
8335b1
-                                        block, 0);
8335b1
-                    /* Test timeout */
8335b1
-                    if (block == APR_NONBLOCK_READ &&
8335b1
-                        ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
8335b1
-                          (APR_STATUS_IS_EAGAIN(rv)) )) {
8335b1
-                        ctx->state = BODY_CHUNK_PART;
8335b1
-                        return APR_EAGAIN;
8335b1
-                    }
8335b1
-                    ctx->state = BODY_CHUNK;
8335b1
                     if (rv == APR_SUCCESS) {
8335b1
-                        rv = get_chunk_line(ctx, bb, f->r->server->limit_req_line);
8335b1
-                        if (APR_STATUS_IS_EAGAIN(rv)) {
8335b1
-                            ctx->state = BODY_CHUNK_PART;
8335b1
-                            apr_brigade_cleanup(bb);
8335b1
-                            return rv;
8335b1
-                        }
8335b1
-                        if (rv == APR_SUCCESS) {
8335b1
-                            ctx->remaining = get_chunk_size(ctx->chunk_ln);
8335b1
-                            if (ctx->remaining == INVALID_CHAR) {
8335b1
-                                rv = APR_EGENERAL;
8335b1
+                        parsing = 1;
8335b1
+                        rv = parse_chunk_size(ctx, buffer, len,
8335b1
+                                f->r->server->limit_req_fieldsize);
8335b1
+                    }
8335b1
+                    if (rv != APR_SUCCESS) {
8335b1
+                        ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01590)
8335b1
+                                      "Error reading/parsing chunk %s ",
8335b1
+                                      (APR_ENOSPC == rv) ? "(overflow)" : "");
8335b1
+                        if (parsing) {
8335b1
+                            if (rv != APR_ENOSPC) {
8335b1
                                 http_error = HTTP_BAD_REQUEST;
8335b1
                             }
8335b1
+                            return bail_out_on_error(ctx, f, http_error);
8335b1
                         }
8335b1
+                        return rv;
8335b1
                     }
8335b1
-                    apr_brigade_cleanup(bb);
8335b1
                 }
8335b1
 
8335b1
-                /* Detect chunksize error (such as overflow) */
8335b1
-                if (rv != APR_SUCCESS || ctx->remaining < 0) {
8335b1
-                    ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01590) "Error reading chunk %s ",
8335b1
-                                  (ctx->remaining < 0) ? "(overflow)" : "");
8335b1
-                    ctx->remaining = 0; /* Reset it in case we have to
8335b1
-                                         * come back here later */
8335b1
-                    if (APR_STATUS_IS_TIMEUP(rv)) {
8335b1
-                        http_error = HTTP_REQUEST_TIME_OUT;
8335b1
-                    }
8335b1
-                    return bail_out_on_error(ctx, f, http_error);
8335b1
-                }
8335b1
+                apr_bucket_delete(e);
8335b1
+                e = APR_BRIGADE_FIRST(b);
8335b1
+            }
8335b1
+            again = 1; /* come around again */
8335b1
 
8335b1
-                if (!ctx->remaining) {
8335b1
-                    return read_chunked_trailers(ctx, f, b,
8335b1
+            if (ctx->state == BODY_CHUNK_TRAILER) {
8335b1
+                /* Treat UNSET as DISABLE - trailers aren't merged by default */
8335b1
+                return read_chunked_trailers(ctx, f, b,
8335b1
                             conf->merge_trailers == AP_MERGE_TRAILERS_ENABLE);
8335b1
-                }
8335b1
             }
8335b1
+
8335b1
             break;
8335b1
         }
8335b1
-    }
8335b1
+        case BODY_NONE:
8335b1
+        case BODY_LENGTH:
8335b1
+        case BODY_CHUNK_DATA: {
8335b1
 
8335b1
-    /* Ensure that the caller can not go over our boundary point. */
8335b1
-    if (ctx->state == BODY_LENGTH || ctx->state == BODY_CHUNK) {
8335b1
-        if (ctx->remaining < readbytes) {
8335b1
-            readbytes = ctx->remaining;
8335b1
-        }
8335b1
-        AP_DEBUG_ASSERT(readbytes > 0);
8335b1
-    }
8335b1
+            /* Ensure that the caller can not go over our boundary point. */
8335b1
+            if (ctx->state != BODY_NONE && ctx->remaining < readbytes) {
8335b1
+                readbytes = ctx->remaining;
8335b1
+            }
8335b1
+            if (readbytes > 0) {
8335b1
+                apr_off_t totalread;
8335b1
 
8335b1
-    rv = ap_get_brigade(f->next, b, mode, block, readbytes);
8335b1
+                rv = ap_get_brigade(f->next, b, mode, block, readbytes);
8335b1
 
8335b1
-    if (rv != APR_SUCCESS) {
8335b1
-        return rv;
8335b1
-    }
8335b1
+                /* for timeout */
8335b1
+                if (block == APR_NONBLOCK_READ
8335b1
+                        && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
8335b1
+                                || (APR_STATUS_IS_EAGAIN(rv)))) {
8335b1
+                    return APR_EAGAIN;
8335b1
+                }
8335b1
 
8335b1
-    /* How many bytes did we just read? */
8335b1
-    apr_brigade_length(b, 0, &totalread);
8335b1
+                if (rv == APR_EOF && ctx->state != BODY_NONE
8335b1
+                        && ctx->remaining > 0) {
8335b1
+                    return APR_INCOMPLETE;
8335b1
+                }
8335b1
 
8335b1
-    /* If this happens, we have a bucket of unknown length.  Die because
8335b1
-     * it means our assumptions have changed. */
8335b1
-    AP_DEBUG_ASSERT(totalread >= 0);
8335b1
+                if (rv != APR_SUCCESS) {
8335b1
+                    return rv;
8335b1
+                }
8335b1
 
8335b1
-    if (ctx->state != BODY_NONE) {
8335b1
-        ctx->remaining -= totalread;
8335b1
-        if (ctx->remaining > 0) {
8335b1
-            e = APR_BRIGADE_LAST(b);
8335b1
-            if (APR_BUCKET_IS_EOS(e))
8335b1
-                return APR_EOF;
8335b1
-        }
8335b1
-    }
8335b1
+                /* How many bytes did we just read? */
8335b1
+                apr_brigade_length(b, 0, &totalread);
8335b1
 
8335b1
-    /* If we have no more bytes remaining on a C-L request,
8335b1
-     * save the callter a roundtrip to discover EOS.
8335b1
-     */
8335b1
-    if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
8335b1
-        e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
-        APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
-    }
8335b1
+                /* If this happens, we have a bucket of unknown length.  Die because
8335b1
+                 * it means our assumptions have changed. */
8335b1
+                AP_DEBUG_ASSERT(totalread >= 0);
8335b1
 
8335b1
-    /* We have a limit in effect. */
8335b1
-    if (ctx->limit) {
8335b1
-        /* FIXME: Note that we might get slightly confused on chunked inputs
8335b1
-         * as we'd need to compensate for the chunk lengths which may not
8335b1
-         * really count.  This seems to be up for interpretation.  */
8335b1
-        ctx->limit_used += totalread;
8335b1
-        if (ctx->limit < ctx->limit_used) {
8335b1
-            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(01591)
8335b1
-                          "Read content-length of %" APR_OFF_T_FMT
8335b1
-                          " is larger than the configured limit"
8335b1
-                          " of %" APR_OFF_T_FMT, ctx->limit_used, ctx->limit);
8335b1
-            apr_brigade_cleanup(bb);
8335b1
-            e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
8335b1
-                                       f->r->pool,
8335b1
-                                       f->c->bucket_alloc);
8335b1
-            APR_BRIGADE_INSERT_TAIL(bb, e);
8335b1
-            e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
-            APR_BRIGADE_INSERT_TAIL(bb, e);
8335b1
-            ctx->eos_sent = 1;
8335b1
-            return ap_pass_brigade(f->r->output_filters, bb);
8335b1
-        }
8335b1
-    }
8335b1
+                if (ctx->state != BODY_NONE) {
8335b1
+                    ctx->remaining -= totalread;
8335b1
+                    if (ctx->remaining > 0) {
8335b1
+                        e = APR_BRIGADE_LAST(b);
8335b1
+                        if (APR_BUCKET_IS_EOS(e)) {
8335b1
+                            apr_bucket_delete(e);
8335b1
+                            return APR_INCOMPLETE;
8335b1
+                        }
8335b1
+                    }
8335b1
+                    else if (ctx->state == BODY_CHUNK_DATA) {
8335b1
+                        /* next chunk please */
8335b1
+                        ctx->state = BODY_CHUNK_END;
8335b1
+                        ctx->chunk_used = 0;
8335b1
+                    }
8335b1
+                }
8335b1
 
8335b1
-    return APR_SUCCESS;
8335b1
-}
8335b1
+                /* We have a limit in effect. */
8335b1
+                if (ctx->limit) {
8335b1
+                    /* FIXME: Note that we might get slightly confused on
8335b1
+                     * chunked inputs as we'd need to compensate for the chunk
8335b1
+                     * lengths which may not really count.  This seems to be up
8335b1
+                     * for interpretation.
8335b1
+                     */
8335b1
+                    ctx->limit_used += totalread;
8335b1
+                    if (ctx->limit < ctx->limit_used) {
8335b1
+                        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r,
8335b1
+                                      APLOGNO(01591) "Read content length of "
8335b1
+                                      "%" APR_OFF_T_FMT " is larger than the "
8335b1
+                                      "configured limit of %" APR_OFF_T_FMT,
8335b1
+                                      ctx->limit_used, ctx->limit);
8335b1
+                        return bail_out_on_error(ctx, f,
8335b1
+                                                 HTTP_REQUEST_ENTITY_TOO_LARGE);
8335b1
+                    }
8335b1
+                }
8335b1
+            }
8335b1
 
8335b1
-/**
8335b1
- * Parse a chunk extension, detect overflow.
8335b1
- * There are two error cases:
8335b1
- *  1) If the conversion would require too many bits, a -1 is returned.
8335b1
- *  2) If the conversion used the correct number of bits, but an overflow
8335b1
- *     caused only the sign bit to flip, then that negative number is
8335b1
- *     returned.
8335b1
- * In general, any negative number can be considered an overflow error.
8335b1
- */
8335b1
-static long get_chunk_size(char *b)
8335b1
-{
8335b1
-    long chunksize = 0;
8335b1
-    size_t chunkbits = sizeof(long) * 8;
8335b1
+            /* If we have no more bytes remaining on a C-L request,
8335b1
+             * save the caller a round trip to discover EOS.
8335b1
+             */
8335b1
+            if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
8335b1
+                e = apr_bucket_eos_create(f->c->bucket_alloc);
8335b1
+                APR_BRIGADE_INSERT_TAIL(b, e);
8335b1
+                ctx->eos_sent = 1;
8335b1
+            }
8335b1
 
8335b1
-    ap_xlate_proto_from_ascii(b, strlen(b));
8335b1
+            break;
8335b1
+        }
8335b1
+        case BODY_CHUNK_TRAILER: {
8335b1
 
8335b1
-    if (!apr_isxdigit(*b)) {
8335b1
-        /*
8335b1
-         * Detect invalid character at beginning. This also works for empty
8335b1
-         * chunk size lines.
8335b1
-         */
8335b1
-        return INVALID_CHAR;
8335b1
-    }
8335b1
-    /* Skip leading zeros */
8335b1
-    while (*b == '0') {
8335b1
-        ++b;
8335b1
-    }
8335b1
+            rv = ap_get_brigade(f->next, b, mode, block, readbytes);
8335b1
 
8335b1
-    while (apr_isxdigit(*b) && (chunkbits > 0)) {
8335b1
-        int xvalue = 0;
8335b1
+            /* for timeout */
8335b1
+            if (block == APR_NONBLOCK_READ
8335b1
+                    && ((rv == APR_SUCCESS && APR_BRIGADE_EMPTY(b))
8335b1
+                            || (APR_STATUS_IS_EAGAIN(rv)))) {
8335b1
+                return APR_EAGAIN;
8335b1
+            }
8335b1
+
8335b1
+            if (rv != APR_SUCCESS) {
8335b1
+                return rv;
8335b1
+            }
8335b1
 
8335b1
-        if (*b >= '0' && *b <= '9') {
8335b1
-            xvalue = *b - '0';
8335b1
+            break;
8335b1
         }
8335b1
-        else if (*b >= 'A' && *b <= 'F') {
8335b1
-            xvalue = *b - 'A' + 0xa;
8335b1
+        default: {
8335b1
+            /* Should not happen */
8335b1
+            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, f->r, APLOGNO(02901)
8335b1
+                          "Unexpected body state (%i)", (int)ctx->state);
8335b1
+            return APR_EGENERAL;
8335b1
         }
8335b1
-        else if (*b >= 'a' && *b <= 'f') {
8335b1
-            xvalue = *b - 'a' + 0xa;
8335b1
         }
8335b1
 
8335b1
-        chunksize = (chunksize << 4) | xvalue;
8335b1
-        chunkbits -= 4;
8335b1
-        ++b;
8335b1
-    }
8335b1
-    if (apr_isxdigit(*b)) {
8335b1
-        /* overflow */
8335b1
-        return -1;
8335b1
-    }
8335b1
+    } while (again);
8335b1
 
8335b1
-    return chunksize;
8335b1
+    return APR_SUCCESS;
8335b1
 }
8335b1
 
8335b1
 typedef struct header_struct {
8335b1
@@ -1385,6 +1377,39 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
8335b1
     return ap_pass_brigade(f->next, b);
8335b1
 }
8335b1
 
8335b1
+/*
8335b1
+ * Map specific APR codes returned by the filter stack to HTTP error
8335b1
+ * codes, or the default status code provided. Use it as follows:
8335b1
+ *
8335b1
+ * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
8335b1
+ *
8335b1
+ * If the filter has already handled the error, AP_FILTER_ERROR will
8335b1
+ * be returned, which is cleanly passed through.
8335b1
+ *
8335b1
+ * These mappings imply that the filter stack is reading from the
8335b1
+ * downstream client, the proxy will map these codes differently.
8335b1
+ */
8335b1
+AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status)
8335b1
+{
8335b1
+    switch (rv) {
8335b1
+    case AP_FILTER_ERROR: {
8335b1
+        return AP_FILTER_ERROR;
8335b1
+    }
8335b1
+    case APR_ENOSPC: {
8335b1
+        return HTTP_REQUEST_ENTITY_TOO_LARGE;
8335b1
+    }
8335b1
+    case APR_ENOTIMPL: {
8335b1
+        return HTTP_NOT_IMPLEMENTED;
8335b1
+    }
8335b1
+    case APR_ETIMEDOUT: {
8335b1
+        return HTTP_REQUEST_TIME_OUT;
8335b1
+    }
8335b1
+    default: {
8335b1
+        return status;
8335b1
+    }
8335b1
+    }
8335b1
+}
8335b1
+
8335b1
 /* In HTTP/1.1, any method can have a body.  However, most GET handlers
8335b1
  * wouldn't know what to do with a request body if they received one.
8335b1
  * This helper routine tests for and reads any message body in the request,
8335b1
@@ -1402,7 +1427,8 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
8335b1
 AP_DECLARE(int) ap_discard_request_body(request_rec *r)
8335b1
 {
8335b1
     apr_bucket_brigade *bb;
8335b1
-    int rv, seen_eos;
8335b1
+    int seen_eos;
8335b1
+    apr_status_t rv;
8335b1
 
8335b1
     /* Sometimes we'll get in a state where the input handling has
8335b1
      * detected an error where we want to drop the connection, so if
8335b1
@@ -1425,21 +1451,8 @@ AP_DECLARE(int) ap_discard_request_body(request_rec *r)
8335b1
                             APR_BLOCK_READ, HUGE_STRING_LEN);
8335b1
 
8335b1
         if (rv != APR_SUCCESS) {
8335b1
-            /* FIXME: If we ever have a mapping from filters (apr_status_t)
8335b1
-             * to HTTP error codes, this would be a good place for them.
8335b1
-             *
8335b1
-             * If we received the special case AP_FILTER_ERROR, it means
8335b1
-             * that the filters have already handled this error.
8335b1
-             * Otherwise, we should assume we have a bad request.
8335b1
-             */
8335b1
-            if (rv == AP_FILTER_ERROR) {
8335b1
-                apr_brigade_destroy(bb);
8335b1
-                return rv;
8335b1
-            }
8335b1
-            else {
8335b1
-                apr_brigade_destroy(bb);
8335b1
-                return HTTP_BAD_REQUEST;
8335b1
-            }
8335b1
+            apr_brigade_destroy(bb);
8335b1
+            return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
8335b1
         }
8335b1
 
8335b1
         for (bucket = APR_BRIGADE_FIRST(bb);
8335b1
@@ -1608,6 +1621,13 @@ AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer,
8335b1
     /* We lose the failure code here.  This is why ap_get_client_block should
8335b1
      * not be used.
8335b1
      */
8335b1
+    if (rv == AP_FILTER_ERROR) {
8335b1
+        /* AP_FILTER_ERROR means a filter has responded already,
8335b1
+         * we are DONE.
8335b1
+         */
8335b1
+        apr_brigade_destroy(bb);
8335b1
+        return -1;
8335b1
+    }
8335b1
     if (rv != APR_SUCCESS) {
8335b1
         /* if we actually fail here, we want to just return and
8335b1
          * stop trying to read data from the client.