295152
diff --git a/modules/ssl/ssl_engine_io.c b/modules/ssl/ssl_engine_io.c
295152
index 018b667..4e3875a 100644
295152
--- a/modules/ssl/ssl_engine_io.c
295152
+++ b/modules/ssl/ssl_engine_io.c
295152
@@ -1598,18 +1598,32 @@ static apr_status_t ssl_io_filter_input(ap_filter_t *f,
295152
 }
295152
 
295152
 
295152
-/* ssl_io_filter_output() produces one SSL/TLS message per bucket
295152
+/* ssl_io_filter_output() produces one SSL/TLS record per bucket
295152
  * passed down the output filter stack.  This results in a high
295152
- * overhead (network packets) for any output comprising many small
295152
- * buckets.  SSI page applied through the HTTP chunk filter, for
295152
- * example, may produce many brigades containing small buckets -
295152
- * [chunk-size CRLF] [chunk-data] [CRLF].
295152
+ * overhead (more network packets & TLS processing) for any output
295152
+ * comprising many small buckets.  SSI output passed through the HTTP
295152
+ * chunk filter, for example, may produce many brigades containing
295152
+ * small buckets - [chunk-size CRLF] [chunk-data] [CRLF].
295152
  *
295152
- * The coalescing filter merges many small buckets into larger buckets
295152
- * where possible, allowing the SSL I/O output filter to handle them
295152
- * more efficiently. */
295152
+ * Sending HTTP response headers as a separate TLS record to the
295152
+ * response body also reveals information to a network observer (the
295152
+ * size of headers) which can be significant.
295152
+ *
295152
+ * The coalescing filter merges data buckets with the aim of producing
295152
+ * fewer, larger TLS records - without copying/buffering all content
295152
+ * and introducing unnecessary overhead.
295152
+ *
295152
+ * ### This buffering could be probably be done more comprehensively
295152
+ * ### in ssl_io_filter_output itself. 
295152
+ * 
295152
+ * ### Another possible performance optimisation in particular for the
295152
+ * ### [HEAP] [FILE] HTTP response case is using a brigade rather than
295152
+ * ### a char array to buffer; using apr_brigade_write() to append
295152
+ * ### will use already-allocated memory from the HEAP, reducing # of
295152
+ * ### copies.
295152
+ */
295152
 
295152
-#define COALESCE_BYTES (2048)
295152
+#define COALESCE_BYTES (AP_IOBUFSIZE)
295152
 
295152
 struct coalesce_ctx {
295152
     char buffer[COALESCE_BYTES];
295152
@@ -1622,11 +1636,12 @@ static apr_status_t ssl_io_filter_coalesce(ap_filter_t *f,
295152
     apr_bucket *e, *upto;
295152
     apr_size_t bytes = 0;
295152
     struct coalesce_ctx *ctx = f->ctx;
295152
+    apr_size_t buffered = ctx ? ctx->bytes : 0; /* space used on entry */
295152
     unsigned count = 0;
295152
 
295152
     /* The brigade consists of zero-or-more small data buckets which
295152
-     * can be coalesced (the prefix), followed by the remainder of the
295152
-     * brigade.
295152
+     * can be coalesced (referred to as the "prefix"), followed by the
295152
+     * remainder of the brigade.
295152
      *
295152
      * Find the last bucket - if any - of that prefix.  count gives
295152
      * the number of buckets in the prefix.  The "prefix" must contain
295152
@@ -1641,24 +1656,97 @@ static apr_status_t ssl_io_filter_coalesce(ap_filter_t *f,
295152
          e != APR_BRIGADE_SENTINEL(bb)
295152
              && !APR_BUCKET_IS_METADATA(e)
295152
              && e->length != (apr_size_t)-1
295152
-             && e->length < COALESCE_BYTES
295152
-             && (bytes + e->length) < COALESCE_BYTES
295152
-             && (ctx == NULL
295152
-                 || bytes + ctx->bytes + e->length < COALESCE_BYTES);
295152
+             && e->length <= COALESCE_BYTES
295152
+             && (buffered + bytes + e->length) <= COALESCE_BYTES;
295152
          e = APR_BUCKET_NEXT(e)) {
295152
         if (e->length) count++; /* don't count zero-length buckets */
295152
         bytes += e->length;
295152
     }
295152
+
295152
+    /* If there is room remaining and the next bucket is a data
295152
+     * bucket, try to include it in the prefix to coalesce.  For a
295152
+     * typical [HEAP] [FILE] HTTP response brigade, this handles
295152
+     * merging the headers and the start of the body into a single TLS
295152
+     * record. */
295152
+    if (bytes + buffered > 0
295152
+        && bytes + buffered < COALESCE_BYTES
295152
+        && e != APR_BRIGADE_SENTINEL(bb)
295152
+        && !APR_BUCKET_IS_METADATA(e)) {
295152
+        apr_status_t rv = APR_SUCCESS;
295152
+
295152
+        /* For an indeterminate length bucket (PIPE/CGI/...), try a
295152
+         * non-blocking read to have it morph into a HEAP.  If the
295152
+         * read fails with EAGAIN, it is harmless to try a split
295152
+         * anyway, split is ENOTIMPL for most PIPE-like buckets. */
295152
+        if (e->length == (apr_size_t)-1) {
295152
+            const char *discard;
295152
+            apr_size_t ignore;
295152
+
295152
+            rv = apr_bucket_read(e, &discard, &ignore, APR_NONBLOCK_READ);
295152
+            if (rv != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(rv)) {
295152
+                ap_log_cerror(APLOG_MARK, APLOG_ERR, rv, f->c, APLOGNO(10232)
295152
+                              "coalesce failed to read from %s bucket",
295152
+                              e->type->name);
295152
+                return AP_FILTER_ERROR;
295152
+            }
295152
+        }
295152
+
295152
+        if (rv == APR_SUCCESS) {
295152
+            /* If the read above made the bucket morph, it may now fit
295152
+             * entirely within the buffer.  Otherwise, split it so it does
295152
+             * fit. */
295152
+            if (e->length > COALESCE_BYTES
295152
+                || e->length + buffered + bytes > COALESCE_BYTES) {
295152
+                rv = apr_bucket_split(e, COALESCE_BYTES - (buffered + bytes));
295152
+            }
295152
+
295152
+            if (rv == APR_SUCCESS && e->length == 0) {
295152
+                /* As above, don't count in the prefix if the bucket is
295152
+                 * now zero-length. */
295152
+            }
295152
+            else if (rv == APR_SUCCESS) {
295152
+                ap_log_cerror(APLOG_MARK, APLOG_TRACE4, 0, f->c,
295152
+                              "coalesce: adding %" APR_SIZE_T_FMT " bytes "
295152
+                              "from split %s bucket, total %" APR_SIZE_T_FMT,
295152
+                              e->length, e->type->name, bytes + buffered);
295152
+
295152
+                count++;
295152
+                bytes += e->length;
295152
+                e = APR_BUCKET_NEXT(e);
295152
+            }
295152
+            else if (rv != APR_ENOTIMPL) {
295152
+                ap_log_cerror(APLOG_MARK, APLOG_ERR, rv, f->c, APLOGNO(10233)
295152
+                              "coalesce: failed to split data bucket");
295152
+                return AP_FILTER_ERROR;
295152
+            }
295152
+        }
295152
+    }
295152
+
295152
+    /* The prefix is zero or more buckets.  upto now points to the
295152
+     * bucket AFTER the end of the prefix, which may be the brigade
295152
+     * sentinel. */
295152
     upto = e;
295152
 
295152
-    /* Coalesce the prefix, if:
295152
-     * a) more than one bucket is found to coalesce, or
295152
-     * b) the brigade contains only a single data bucket, or
295152
-     * c) the data bucket is not last but we have buffered data already.
295152
+    /* Coalesce the prefix, if any of the following are true:
295152
+     * 
295152
+     * a) the prefix is more than one bucket
295152
+     * OR
295152
+     * b) the prefix is the entire brigade, which is a single bucket
295152
+     *    AND the prefix length is smaller than the buffer size,
295152
+     * OR
295152
+     * c) the prefix is a single bucket
295152
+     *    AND there is buffered data from a previous pass.
295152
+     * 
295152
+     * The aim with (b) is to buffer a small bucket so it can be
295152
+     * coalesced with future invocations of this filter.  e.g.  three
295152
+     * calls each with a single 100 byte HEAP bucket should get
295152
+     * coalesced together.  But an invocation with a 8192 byte HEAP
295152
+     * should pass through untouched.
295152
      */
295152
     if (bytes > 0
295152
         && (count > 1
295152
-            || (upto == APR_BRIGADE_SENTINEL(bb))
295152
+            || (upto == APR_BRIGADE_SENTINEL(bb)
295152
+                && bytes < COALESCE_BYTES)
295152
             || (ctx && ctx->bytes > 0))) {
295152
         /* If coalescing some bytes, ensure a context has been
295152
          * created. */
295152
@@ -1669,7 +1757,8 @@ static apr_status_t ssl_io_filter_coalesce(ap_filter_t *f,
295152
 
295152
         ap_log_cerror(APLOG_MARK, APLOG_TRACE4, 0, f->c,
295152
                       "coalesce: have %" APR_SIZE_T_FMT " bytes, "
295152
-                      "adding %" APR_SIZE_T_FMT " more", ctx->bytes, bytes);
295152
+                      "adding %" APR_SIZE_T_FMT " more (buckets=%u)",
295152
+                      ctx->bytes, bytes, count);
295152
 
295152
         /* Iterate through the prefix segment.  For non-fatal errors
295152
          * in this loop it is safe to break out and fall back to the
295152
@@ -1684,7 +1773,8 @@ static apr_status_t ssl_io_filter_coalesce(ap_filter_t *f,
295152
             if (APR_BUCKET_IS_METADATA(e)
295152
                 || e->length == (apr_size_t)-1) {
295152
                 ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, f->c, APLOGNO(02012)
295152
-                              "unexpected bucket type during coalesce");
295152
+                              "unexpected %s bucket during coalesce",
295152
+                              e->type->name);
295152
                 break; /* non-fatal error; break out */
295152
             }
295152