Hi all,

This patch adds a module for dynamic Brotli (RFC 7932) compression in httpd.

The new compression format is supported by Mozilla Firefox since 44.0 and
by Google Chrome since 50.0 [1, 2], and both nginx and IIS have modules that
offer Brotli compression.

With the new module, existing mod_deflate installations can benefit from
better compression ratio by sending Brotli-compressed data to the clients
that support it:

    LoadModule brotli_module modules/mod_brotli.so
    LoadModule deflate_module modules/mod_deflate.so
    SetOutputFilter BROTLI_COMPRESS;DEFLATE

This module features zero-copy processing, which is only possible with the
new API from the upcoming 1.0.x series of brotli [3].

The Linux makefile works against libbrotli [4], as currently the core brotli
repository doesn't offer a way to build a library [5].  Enabling mod_brotli
can be done with:

    configure --enable-brotli=shared [--with-brotli=]

CMake build is supported as well.  Please note that the patch doesn't include
the documentation updates and other types of makefiles, but I will do it
separately.

The second patch adds a couple of tests to the test framework.

[1] https://www.mozilla.org/en-US/firefox/44.0/releasenotes/
[2] https://www.chromestatus.com/feature/5420797577396224
[3] https://github.com/google/brotli
[4] https://github.com/bagder/libbrotli
[5] https://github.com/google/brotli/pull/332


Regards,
Evgeny Kotkov
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt      (revision 1760986)
+++ CMakeLists.txt      (working copy)
@@ -58,6 +58,12 @@ ELSE()
   SET(default_nghttp2_libraries "${CMAKE_INSTALL_PREFIX}/lib/nghttp2.lib")
 ENDIF()
 
+IF(EXISTS "${CMAKE_INSTALL_PREFIX}/lib/brotli_enc.lib")
+  SET(default_brotli_libraries "${CMAKE_INSTALL_PREFIX}/lib/brotli_enc.lib" 
"${CMAKE_INSTALL_PREFIX}/lib/brotli_common.lib")
+ELSE()
+  SET(default_brotli_libraries)
+ENDIF()
+
 SET(APR_INCLUDE_DIR       "${CMAKE_INSTALL_PREFIX}/include" CACHE STRING 
"Directory with APR[-Util] include files")
 SET(APR_LIBRARIES         ${default_apr_libraries}       CACHE STRING "APR 
libraries to link with")
 SET(NGHTTP2_INCLUDE_DIR   "${CMAKE_INSTALL_PREFIX}/include" CACHE STRING 
"Directory with NGHTTP2 include files within nghttp2 subdirectory")
@@ -66,6 +72,8 @@ SET(PCRE_INCLUDE_DIR      "${CMAKE_INSTALL_PREFIX}
 SET(PCRE_LIBRARIES        ${default_pcre_libraries}      CACHE STRING "PCRE 
libraries to link with")
 SET(LIBXML2_ICONV_INCLUDE_DIR     ""                     CACHE STRING 
"Directory with iconv include files for libxml2")
 SET(LIBXML2_ICONV_LIBRARIES       ""                     CACHE STRING "iconv 
libraries to link with for libxml2")
+SET(BROTLI_INCLUDE_DIR    "${CMAKE_INSTALL_PREFIX}/include" CACHE STRING 
"Directory with include files for Brotli")
+SET(BROTLI_LIBRARIES      ${default_brotli_libraries}    CACHE STRING "Brotli 
libraries to link with")
 # end support library configuration
 
 # Misc. options
@@ -191,6 +199,18 @@ ELSE()
   SET(NGHTTP2_FOUND FALSE)
 ENDIF()
 
+# See if we have Brotli
+SET(BROTLI_FOUND TRUE)
+IF(EXISTS "${BROTLI_INCLUDE_DIR}/brotli/encode.h")
+  FOREACH(onelib ${BROTLI_LIBRARIES})
+    IF(NOT EXISTS ${onelib})
+      SET(BROTLI_FOUND FALSE)
+    ENDIF()
+  ENDFOREACH()
+ELSE()
+  SET(BROTLI_FOUND FALSE)
+ENDIF()
+
 MESSAGE(STATUS "")
 MESSAGE(STATUS "Summary of feature detection:")
 MESSAGE(STATUS "")
@@ -199,6 +219,7 @@ MESSAGE(STATUS "LUA51_FOUND .............. : ${LUA
 MESSAGE(STATUS "NGHTTP2_FOUND ............ : ${NGHTTP2_FOUND}")
 MESSAGE(STATUS "OPENSSL_FOUND ............ : ${OPENSSL_FOUND}")
 MESSAGE(STATUS "ZLIB_FOUND ............... : ${ZLIB_FOUND}")
+MESSAGE(STATUS "BROTLI_FOUND ............. : ${BROTLI_FOUND}")
 MESSAGE(STATUS "APR_HAS_LDAP ............. : ${APR_HAS_LDAP}")
 MESSAGE(STATUS "APR_HAS_XLATE ............ : ${APR_HAS_XLATE}")
 MESSAGE(STATUS "APU_HAVE_CRYPTO .......... : ${APU_HAVE_CRYPTO}")
@@ -273,6 +294,7 @@ SET(MODULE_LIST
   "modules/examples/mod_case_filter_in+O+Example uppercase conversion input 
filter"
   "modules/examples/mod_example_hooks+O+Example hook callback handler module"
   "modules/examples/mod_example_ipc+O+Example of shared memory and mutex usage"
+  "modules/filters/mod_brotli+i+Brotli compression support"
   "modules/filters/mod_buffer+I+Filter Buffering"
   "modules/filters/mod_charset_lite+i+character set translation"
   "modules/filters/mod_data+O+RFC2397 data encoder"
@@ -393,6 +415,11 @@ IF(ZLIB_FOUND)
   SET(mod_deflate_extra_includes       ${ZLIB_INCLUDE_DIR})
   SET(mod_deflate_extra_libs           ${ZLIB_LIBRARIES})
 ENDIF()
+SET(mod_brotli_requires              BROTLI_FOUND)
+IF(BROTLI_FOUND)
+  SET(mod_brotli_extra_includes        ${BROTLI_INCLUDE_DIR})
+  SET(mod_brotli_extra_libs            ${BROTLI_LIBRARIES})
+ENDIF()
 SET(mod_firehose_requires            SOMEONE_TO_MAKE_IT_COMPILE_ON_WINDOWS)
 SET(mod_heartbeat_extra_libs         mod_watchdog)
 SET(mod_http2_requires               NGHTTP2_FOUND)
@@ -961,6 +988,8 @@ MESSAGE(STATUS "  PCRE include directory .........
 MESSAGE(STATUS "  PCRE libraries .................. : ${PCRE_LIBRARIES}")
 MESSAGE(STATUS "  libxml2 iconv prereq include dir. : 
${LIBXML2_ICONV_INCLUDE_DIR}")
 MESSAGE(STATUS "  libxml2 iconv prereq libraries .. : 
${LIBXML2_ICONV_LIBRARIES}")
+MESSAGE(STATUS "  Brotli include directory......... : ${BROTLI_INCLUDE_DIR}")
+MESSAGE(STATUS "  Brotli libraries ................ : ${BROTLI_LIBRARIES}")
 MESSAGE(STATUS "  Extra include directories ....... : ${EXTRA_INCLUDES}")
 MESSAGE(STATUS "  Extra compile flags ............. : ${EXTRA_COMPILE_FLAGS}")
 MESSAGE(STATUS "  Extra libraries ................. : ${EXTRA_LIBS}")
Index: docs/log-message-tags/next-number
===================================================================
--- docs/log-message-tags/next-number   (revision 1760986)
+++ docs/log-message-tags/next-number   (working copy)
@@ -1 +1 @@
-3459
+3461
Index: modules/filters/config.m4
===================================================================
--- modules/filters/config.m4   (revision 1760986)
+++ modules/filters/config.m4   (working copy)
@@ -141,6 +141,127 @@ APACHE_MODULE(proxy_html, Fix HTML Links in a Reve
 ]
 )
 
+dnl
+dnl APACHE_CHECK_BROTLI
+dnl
+dnl Configure for Brotli, giving preference to
+dnl "--with-brotli=<path>" if it was specified.
+dnl
+AC_DEFUN([APACHE_CHECK_BROTLI],[
+  AC_CACHE_CHECK([for Brotli], [ac_cv_brotli], [
+    dnl initialise the variables we use
+    ac_cv_brotli=no
+    ac_brotli_found=""
+    ac_brotli_base=""
+    ac_brotli_libs=""
+    ac_brotli_mod_cflags=""
+    ac_brotli_mod_ldflags=""
+
+    dnl Determine the Brotli base directory, if any
+    AC_MSG_CHECKING([for user-provided Brotli base directory])
+    AC_ARG_WITH(brotli, APACHE_HELP_STRING(--with-brotli=PATH,Brotli 
installation directory), [
+      dnl If --with-brotli specifies a directory, we use that directory
+      if test "x$withval" != "xyes" -a "x$withval" != "x"; then
+        dnl This ensures $withval is actually a directory and that it is 
absolute
+        ac_brotli_base="`cd $withval ; pwd`"
+      fi
+    ])
+    if test "x$ac_brotli_base" = "x"; then
+      AC_MSG_RESULT(none)
+    else
+      AC_MSG_RESULT($ac_brotli_base)
+    fi
+
+    dnl Run header and version checks
+    saved_CPPFLAGS="$CPPFLAGS"
+    saved_LIBS="$LIBS"
+    saved_LDFLAGS="$LDFLAGS"
+
+    dnl Before doing anything else, load in pkg-config variables
+    if test -n "$PKGCONFIG"; then
+      saved_PKG_CONFIG_PATH="$PKG_CONFIG_PATH"
+      if test "x$ac_brotli_base" != "x" -a \
+              -f "${ac_brotli_base}/lib/pkgconfig/libbrotlienc.pc"; then
+        dnl Ensure that the given path is used by pkg-config too, otherwise
+        dnl the system libbrotlienc.pc might be picked up instead.
+        
PKG_CONFIG_PATH="${ac_brotli_base}/lib/pkgconfig${PKG_CONFIG_PATH+:}${PKG_CONFIG_PATH}"
+        export PKG_CONFIG_PATH
+      fi
+      ac_brotli_libs="`$PKGCONFIG --libs-only-l --silence-errors libbrotlienc`"
+      if test $? -eq 0; then
+        ac_brotli_found="yes"
+        pkglookup="`$PKGCONFIG --cflags-only-I libbrotlienc`"
+        APR_ADDTO(CPPFLAGS, [$pkglookup])
+        APR_ADDTO(MOD_CFLAGS, [$pkglookup])
+        pkglookup="`$PKGCONFIG --libs-only-L libbrotlienc`"
+        APR_ADDTO(LDFLAGS, [$pkglookup])
+        APR_ADDTO(MOD_LDFLAGS, [$pkglookup])
+        pkglookup="`$PKGCONFIG --libs-only-other libbrotlienc`"
+        APR_ADDTO(LDFLAGS, [$pkglookup])
+        APR_ADDTO(MOD_LDFLAGS, [$pkglookup])
+      fi
+      PKG_CONFIG_PATH="$saved_PKG_CONFIG_PATH"
+    fi
+
+    dnl fall back to the user-supplied directory if not found via pkg-config
+    if test "x$ac_brotli_base" != "x" -a "x$ac_brotli_found" = "x"; then
+      APR_ADDTO(CPPFLAGS, [-I$ac_brotli_base/include])
+      APR_ADDTO(MOD_CFLAGS, [-I$ac_brotli_base/include])
+      APR_ADDTO(LDFLAGS, [-L$ac_brotli_base/lib])
+      APR_ADDTO(MOD_LDFLAGS, [-L$ac_brotli_base/lib])
+      if test "x$ap_platform_runtime_link_flag" != "x"; then
+        APR_ADDTO(LDFLAGS, [$ap_platform_runtime_link_flag$ac_brotli_base/lib])
+        APR_ADDTO(MOD_LDFLAGS, 
[$ap_platform_runtime_link_flag$ac_brotli_base/lib])
+      fi
+    fi
+
+    ac_brotli_libs="${ac_brotli_libs:--lbrotlienc `$apr_config --libs`} "
+    APR_ADDTO(MOD_LDFLAGS, [$ac_brotli_libs])
+    APR_ADDTO(LIBS, [$ac_brotli_libs])
+
+    dnl Run library and function checks
+    liberrors=""
+    AC_CHECK_HEADERS([brotli/encode.h])
+    AC_MSG_CHECKING([for Brotli version >= 1.0.0])
+    AC_TRY_COMPILE([#include <brotli/encode.h>],[
+const uint8_t *o = BrotliEncoderTakeOutput((BrotliEncoderState*)0, 
(size_t*)0);],
+      [AC_MSG_RESULT(OK)
+       ac_cv_brotli="yes"],
+      [AC_MSG_RESULT(FAILED)])
+
+    dnl restore
+    CPPFLAGS="$saved_CPPFLAGS"
+    LIBS="$saved_LIBS"
+    LDFLAGS="$saved_LDFLAGS"
+
+    dnl cache MOD_LDFLAGS, MOD_CFLAGS
+    ac_brotli_mod_cflags=$MOD_CFLAGS
+    ac_brotli_mod_ldflags=$MOD_LDFLAGS
+  ])
+  if test "x$ac_cv_brotli" = "xyes"; then
+    APR_ADDTO(MOD_LDFLAGS, [$ac_brotli_mod_ldflags])
+
+    dnl Ouch!  libbrotlienc.1.so doesn't link against libm.so (-lm),
+    dnl although it should.  Workaround that in our LDFLAGS:
+
+    APR_ADDTO(MOD_LDFLAGS, ["-lm"])
+    APR_ADDTO(MOD_CFLAGS, [$ac_brotli_mod_cflags])
+  fi
+])
+
+APACHE_MODULE(brotli, Brotli compression support, , , most, [
+  APACHE_CHECK_BROTLI
+  if test "$ac_cv_brotli" = "yes" ; then
+      if test "x$enable_brotli" = "xshared"; then
+         # The only symbol which needs to be exported is the module
+         # structure, so ask libtool to hide everything else:
+         APR_ADDTO(MOD_BROTLI_LDADD, [-export-symbols-regex brotli_module])
+      fi
+  else
+      enable_brotli=no
+  fi
+])
+
 APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
 
 APACHE_MODPATH_FINISH
Index: modules/filters/mod_brotli.c
===================================================================
--- modules/filters/mod_brotli.c        (revision 0)
+++ modules/filters/mod_brotli.c        (working copy)
@@ -0,0 +1,522 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "httpd.h"
+#include "http_core.h"
+#include "http_log.h"
+#include "apr_strings.h"
+
+#include <brotli/encode.h>
+
+module AP_MODULE_DECLARE_DATA brotli_module;
+
+typedef enum {
+    ETAG_MODE_ADDSUFFIX = 0,
+    ETAG_MODE_NOCHANGE = 1,
+    ETAG_MODE_REMOVE = 2
+} etag_mode_e;
+
+typedef struct brotli_server_config_t {
+    int quality;
+    int lgwin;
+    int lgblock;
+    etag_mode_e etag_mode;
+} brotli_server_config_t;
+
+static void *create_server_config(apr_pool_t *p, server_rec *s)
+{
+    brotli_server_config_t *conf = apr_pcalloc(p, sizeof(*conf));
+
+    /* These default values allow mod_brotli to behave similarly to
+     * mod_deflate in terms of compression speed and memory usage.
+     *
+     * The idea is that since Brotli (generally) gives better compression
+     * ratio than Deflate, simply enabling mod_brotli on the server
+     * will reduce the amount of transferred data while keeping everything
+     * else unchanged.  See https://quixdb.github.io/squash-benchmark/
+     */
+    conf->quality = 5;
+    conf->lgwin = 18;
+    conf->lgblock = 0;
+    conf->etag_mode = ETAG_MODE_ADDSUFFIX;
+
+    return conf;
+}
+
+static const char *set_compression_quality(cmd_parms *cmd, void *dummy,
+                                           const char *arg)
+{
+    brotli_server_config_t *conf =
+        ap_get_module_config(cmd->server->module_config, &brotli_module);
+    int val = atoi(arg);
+
+    if (val < 0 || val > 11) {
+        return "BrotliCompressionQuality must be between 0 and 11";
+    }
+
+    conf->quality = val;
+    return NULL;
+}
+
+static const char *set_compression_lgwin(cmd_parms *cmd, void *dummy,
+                                         const char *arg)
+{
+    brotli_server_config_t *conf =
+        ap_get_module_config(cmd->server->module_config, &brotli_module);
+    int val = atoi(arg);
+
+    if (val < 10 || val > 24) {
+        return "BrotliCompressionWindow must be between 10 and 24";
+    }
+
+    conf->lgwin = val;
+    return NULL;
+}
+
+static const char *set_compression_lgblock(cmd_parms *cmd, void *dummy,
+                                           const char *arg)
+{
+    brotli_server_config_t *conf =
+        ap_get_module_config(cmd->server->module_config, &brotli_module);
+    int val = atoi(arg);
+
+    if (val < 16 || val > 24) {
+        return "BrotliCompressionMaxInputBlock must be between 16 and 24";
+    }
+
+    conf->lgblock = val;
+    return NULL;
+}
+
+static const char *set_etag_mode(cmd_parms *cmd, void *dummy,
+                                 const char *arg)
+{
+    brotli_server_config_t *conf =
+        ap_get_module_config(cmd->server->module_config, &brotli_module);
+
+    if (ap_cstr_casecmp(arg, "AddSuffix") == 0) {
+        conf->etag_mode = ETAG_MODE_ADDSUFFIX;
+    }
+    else if (ap_cstr_casecmp(arg, "NoChange") == 0) {
+        conf->etag_mode = ETAG_MODE_NOCHANGE;
+    }
+    else if (ap_cstr_casecmp(arg, "Remove") == 0) {
+        conf->etag_mode = ETAG_MODE_REMOVE;
+    }
+    else {
+        return "BrotliAlterETag accepts only 'AddSuffix', 'NoChange' and 
'Remove'";
+    }
+
+    return NULL;
+}
+
+typedef struct brotli_ctx_t {
+    BrotliEncoderState *state;
+    apr_bucket_brigade *bb;
+} brotli_ctx_t;
+
+static void *alloc_func(void *opaque, apr_size_t size)
+{
+    return apr_bucket_alloc(size, opaque);
+}
+
+static void free_func(void *opaque, void *block)
+{
+    if (block) {
+        apr_bucket_free(block);
+    }
+}
+
+static apr_status_t cleanup_ctx(void *data)
+{
+    brotli_ctx_t *ctx = data;
+
+    BrotliEncoderDestroyInstance(ctx->state);
+    ctx->state = NULL;
+    return APR_SUCCESS;
+}
+
+static brotli_ctx_t *create_ctx(int quality,
+                                int lgwin,
+                                int lgblock,
+                                apr_bucket_alloc_t *alloc,
+                                apr_pool_t *pool)
+{
+    brotli_ctx_t *ctx = apr_pcalloc(pool, sizeof(*ctx));
+
+    ctx->state = BrotliEncoderCreateInstance(alloc_func, free_func, alloc);
+    BrotliEncoderSetParameter(ctx->state, BROTLI_PARAM_QUALITY, quality);
+    BrotliEncoderSetParameter(ctx->state, BROTLI_PARAM_LGWIN, lgwin);
+    BrotliEncoderSetParameter(ctx->state, BROTLI_PARAM_LGBLOCK, lgblock);
+    apr_pool_cleanup_register(pool, ctx, cleanup_ctx, apr_pool_cleanup_null);
+
+    ctx->bb = apr_brigade_create(pool, alloc);
+
+    return ctx;
+}
+
+static apr_status_t process_chunk(brotli_ctx_t *ctx,
+                                  const void *data,
+                                  apr_size_t len,
+                                  ap_filter_t *f)
+{
+    const uint8_t *next_in = data;
+    apr_size_t avail_in = len;
+
+    while (avail_in > 0) {
+        uint8_t *next_out = NULL;
+        apr_size_t avail_out = 0;
+
+        if (!BrotliEncoderCompressStream(ctx->state,
+                                         BROTLI_OPERATION_PROCESS,
+                                         &avail_in, &next_in,
+                                         &avail_out, &next_out, NULL)) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, APLOGNO(03459)
+                          "Error while compressing data");
+            return APR_EGENERAL;
+        }
+
+        if (BrotliEncoderHasMoreOutput(ctx->state)) {
+            apr_size_t output_len = 0;
+            const uint8_t *output;
+            apr_status_t rv;
+            apr_bucket *b;
+
+            /* Drain the accumulated output.  Avoid copying the data by
+             * wrapping a pointer to the internal output buffer and passing
+             * it down to the next filter.  The pointer is only valid until
+             * the next call to BrotliEncoderCompressStream(), but we're okay
+             * with that, since the brigade is cleaned up right after the
+             * ap_pass_brigade() call.
+             */
+            output = BrotliEncoderTakeOutput(ctx->state, &output_len);
+            b = apr_bucket_transient_create(output, output_len,
+                                            ctx->bb->bucket_alloc);
+            APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
+
+            rv = ap_pass_brigade(f->next, ctx->bb);
+            apr_brigade_cleanup(ctx->bb);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+        }
+    }
+
+    return APR_SUCCESS;
+}
+
+static apr_status_t flush(brotli_ctx_t *ctx,
+                          BrotliEncoderOperation op,
+                          ap_filter_t *f)
+{
+    while (1) {
+        const uint8_t *next_in = NULL;
+        apr_size_t avail_in = 0;
+        uint8_t *next_out = NULL;
+        apr_size_t avail_out = 0;
+        apr_size_t output_len;
+        const uint8_t *output;
+        apr_bucket *b;
+
+        if (!BrotliEncoderCompressStream(ctx->state, op,
+                                         &avail_in, &next_in,
+                                         &avail_out, &next_out, NULL)) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, APLOGNO(03460)
+                          "Error while compressing data");
+            return APR_EGENERAL;
+        }
+
+        if (!BrotliEncoderHasMoreOutput(ctx->state)) {
+            break;
+        }
+
+        /* A flush can require several calls to BrotliEncoderCompressStream(),
+         * so place the data on the heap (otherwise, the pointer will become
+         * invalid after the next call to BrotliEncoderCompressStream()).
+         */
+        output_len = 0;
+        output = BrotliEncoderTakeOutput(ctx->state, &output_len);
+        b = apr_bucket_heap_create(output, output_len, NULL,
+                                   ctx->bb->bucket_alloc);
+        APR_BRIGADE_INSERT_TAIL(ctx->bb, b);
+    }
+
+    return APR_SUCCESS;
+}
+
+static const char *get_content_encoding(request_rec *r)
+{
+    const char *encoding;
+
+    encoding = apr_table_get(r->headers_out, "Content-Encoding");
+    if (encoding) {
+        const char *err_enc;
+
+        err_enc = apr_table_get(r->err_headers_out, "Content-Encoding");
+        if (err_enc) {
+            encoding = apr_pstrcat(r->pool, encoding, ",", err_enc, NULL);
+        }
+    }
+    else {
+        encoding = apr_table_get(r->err_headers_out, "Content-Encoding");
+    }
+
+    if (r->content_encoding) {
+        encoding = encoding ? apr_pstrcat(r->pool, encoding, ",",
+                                          r->content_encoding, NULL)
+                            : r->content_encoding;
+    }
+
+    return encoding;
+}
+
+static apr_status_t compress_filter(ap_filter_t *f, apr_bucket_brigade *bb)
+{
+    request_rec *r = f->r;
+    brotli_ctx_t *ctx = f->ctx;
+    apr_status_t rv;
+    brotli_server_config_t *conf;
+
+    if (APR_BRIGADE_EMPTY(bb)) {
+        return APR_SUCCESS;
+    }
+
+    conf = ap_get_module_config(r->server->module_config, &brotli_module);
+
+    if (!ctx) {
+        const char *encoding;
+        const char *token;
+        const char *accepts;
+
+        /* Only work on main request, not subrequests, that are not
+         * a 204 response with no content, and not a partial response
+         * to a Range request.
+         */
+        if (r->main || r->status == HTTP_NO_CONTENT
+            || apr_table_get(r->headers_out, "Content-Range")) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        /* Let's see what our current Content-Encoding is. */
+        encoding = get_content_encoding(r);
+
+        if (encoding) {
+            const char *tmp = encoding;
+
+            token = ap_get_token(r->pool, &tmp, 0);
+            while (token && *token) {
+                if (strcmp(token, "identity") != 0 &&
+                    strcmp(token, "7bit") != 0 &&
+                    strcmp(token, "8bit") != 0 &&
+                    strcmp(token, "binary") != 0) {
+                    /* The data is already encoded, do nothing. */
+                    ap_remove_output_filter(f);
+                    return ap_pass_brigade(f->next, bb);
+                }
+
+                if (*tmp) {
+                    ++tmp;
+                }
+                token = (*tmp) ? ap_get_token(r->pool, &tmp, 0) : NULL;
+            }
+        }
+
+        /* Even if we don't accept this request based on it not having
+         * the Accept-Encoding, we need to note that we were looking
+         * for this header and downstream proxies should be aware of
+         * that.
+         */
+        apr_table_mergen(r->headers_out, "Vary", "Accept-Encoding");
+
+        accepts = apr_table_get(r->headers_in, "Accept-Encoding");
+        if (!accepts) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        /* Do we have Accept-Encoding: br? */
+        token = ap_get_token(r->pool, &accepts, 0);
+        while (token && token[0] && ap_cstr_casecmp(token, "br") != 0) {
+            while (*accepts == ';') {
+                ++accepts;
+                ap_get_token(r->pool, &accepts, 1);
+            }
+
+            if (*accepts == ',') {
+                ++accepts;
+            }
+            token = (*accepts) ? ap_get_token(r->pool, &accepts, 0) : NULL;
+        }
+
+        if (!token || token[0] == '\0') {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        /* If the entire Content-Encoding is "identity", we can replace it. */
+        if (!encoding || ap_cstr_casecmp(encoding, "identity") == 0) {
+            apr_table_setn(r->headers_out, "Content-Encoding", "br");
+        } else {
+            apr_table_mergen(r->headers_out, "Content-Encoding", "br");
+        }
+
+        if (r->content_encoding) {
+            r->content_encoding = apr_table_get(r->headers_out,
+                                                "Content-Encoding");
+        }
+
+        apr_table_unset(r->headers_out, "Content-Length");
+        apr_table_unset(r->headers_out, "Content-MD5");
+
+        /* https://bz.apache.org/bugzilla/show_bug.cgi?id=39727
+         * https://bz.apache.org/bugzilla/show_bug.cgi?id=45023
+         *
+         * ETag must be unique among the possible representations, so a
+         * change to content-encoding requires a corresponding change to the
+         * ETag.  We make this behavior configurable, and mimic mod_deflate's
+         * DeflateAlterETag with BrotliAlterETag to keep the transition from
+         * mod_deflate seamless.
+         */
+        if (conf->etag_mode == ETAG_MODE_REMOVE) {
+            apr_table_unset(r->headers_out, "ETag");
+        }
+        else if (conf->etag_mode == ETAG_MODE_ADDSUFFIX) {
+            const char *etag = apr_table_get(r->headers_out, "ETag");
+
+            if (etag) {
+                apr_size_t len = strlen(etag);
+
+                if (len > 2 && etag[len - 1] == '"') {
+                    etag = apr_pstrndup(r->pool, etag, len - 1);
+                    etag = apr_pstrcat(r->pool, etag, "-br\"", NULL);
+                    apr_table_set(r->headers_out, "ETag", etag);
+                }
+            }
+        }
+
+        /* For 304 responses, we only need to send out the headers. */
+        if (r->status == HTTP_NOT_MODIFIED) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        ctx = create_ctx(conf->quality, conf->lgwin, conf->lgblock,
+                         f->c->bucket_alloc, r->pool);
+        f->ctx = ctx;
+    }
+
+    while (!APR_BRIGADE_EMPTY(bb)) {
+        apr_bucket *e = APR_BRIGADE_FIRST(bb);
+
+        /* Optimization: If we are a HEAD request and bytes_sent is not zero
+         * it means that we have passed the content-length filter once and
+         * have more data to send.  This means that the content-length filter
+         * could not determine our content-length for the response to the
+         * HEAD request anyway (the associated GET request would deliver the
+         * body in chunked encoding) and we can stop compressing.
+         */
+        if (r->header_only && r->bytes_sent) {
+            ap_remove_output_filter(f);
+            return ap_pass_brigade(f->next, bb);
+        }
+
+        if (APR_BUCKET_IS_EOS(e)) {
+            rv = flush(ctx, BROTLI_OPERATION_FINISH, f);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+
+            APR_BUCKET_REMOVE(e);
+            APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
+
+            rv = ap_pass_brigade(f->next, ctx->bb);
+            apr_brigade_cleanup(ctx->bb);
+            apr_pool_cleanup_run(r->pool, ctx, cleanup_ctx);
+            return rv;
+        }
+        else if (APR_BUCKET_IS_FLUSH(e)) {
+            rv = flush(ctx, BROTLI_OPERATION_FLUSH, f);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+
+            APR_BUCKET_REMOVE(e);
+            APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
+
+            rv = ap_pass_brigade(f->next, ctx->bb);
+            apr_brigade_cleanup(ctx->bb);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+        }
+        else if (APR_BUCKET_IS_METADATA(e)) {
+            APR_BUCKET_REMOVE(e);
+            APR_BRIGADE_INSERT_TAIL(ctx->bb, e);
+        }
+        else {
+            const char *data;
+            apr_size_t len;
+
+            rv = apr_bucket_read(e, &data, &len, APR_BLOCK_READ);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+            rv = process_chunk(ctx, data, len, f);
+            if (rv != APR_SUCCESS) {
+                return rv;
+            }
+            apr_bucket_delete(e);
+        }
+    }
+    return APR_SUCCESS;
+}
+
+static void register_hooks(apr_pool_t *p)
+{
+    ap_register_output_filter("BROTLI_COMPRESS", compress_filter, NULL,
+                              AP_FTYPE_CONTENT_SET);
+}
+
+static const command_rec cmds[] = {
+    AP_INIT_TAKE1("BrotliCompressionQuality", set_compression_quality,
+                  NULL, RSRC_CONF,
+                  "Compression quality between 0 and 11 (higher quality means "
+                  "slower compression)"),
+    AP_INIT_TAKE1("BrotliCompressionWindow", set_compression_lgwin,
+                  NULL, RSRC_CONF,
+                  "Sliding window size between 10 and 24 (larger windows can "
+                  "improve compression, but require more memory)"),
+    AP_INIT_TAKE1("BrotliCompressionMaxInputBlock", set_compression_lgblock,
+                  NULL, RSRC_CONF,
+                  "Maximum input block size between 16 and 24 (larger block "
+                  "sizes require more memory)"),
+    AP_INIT_TAKE1("BrotliAlterETag", set_etag_mode,
+                  NULL, RSRC_CONF,
+                  "Set how mod_brotli should modify ETag response headers: "
+                  "'AddSuffix' (default), 'NoChange' (2.2.x behavior), 
'Remove'"),
+    {NULL}
+};
+
+AP_DECLARE_MODULE(brotli) = {
+    STANDARD20_MODULE_STUFF,
+    NULL,                      /* create per-directory config structure */
+    NULL,                      /* merge per-directory config structures */
+    create_server_config,      /* create per-server config structure */
+    NULL,                      /* merge per-server config structures */
+    cmds,                      /* command apr_table_t */
+    register_hooks             /* register hooks */
+};

Property changes on: modules/filters/mod_brotli.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: os/win32/BaseAddr.ref
===================================================================
--- os/win32/BaseAddr.ref       (revision 1760986)
+++ os/win32/BaseAddr.ref       (working copy)
@@ -134,3 +134,4 @@ mod_policy.so               0x70C60000    0x000200
 mod_ssl_ct.so               0x70C80000    0x00020000
 mod_proxy_http2.so          0x70CC0000    0x00030000
 mod_http2.so                0x70D00000    0x00030000
+mod_brotli.so               0x70D30000    0x00020000
Index: t/conf/extra.conf.in
===================================================================
--- t/conf/extra.conf.in        (revision 1760986)
+++ t/conf/extra.conf.in        (working copy)
@@ -979,4 +979,24 @@ LimitRequestFields    32
   </IfVersion>
 </IfDefine>
 
+#
+# mod_brotli test config
+#
+<IfDefine APACHE2>
+  <IfModule mod_alias.c>
+    <IfModule mod_brotli.c>
+        # Reuse existing data for mod_deflate
+        Alias /only_brotli @SERVERROOT@/htdocs/modules/deflate
+        <Location /only_brotli>
+          SetOutputFilter BROTLI_COMPRESS
+        </Location>
 
+        <IfModule mod_deflate.c>
+          Alias /brotli_and_deflate @SERVERROOT@/htdocs/modules/deflate
+          <Location /brotli_and_deflate>
+            SetOutputFilter BROTLI_COMPRESS;DEFLATE
+          </Location>
+        </IfModule>
+    </IfModule>
+  </IfModule>
+</IfDefine>
Index: t/modules/brotli.t
===================================================================
--- t/modules/brotli.t  (nonexistent)
+++ t/modules/brotli.t  (working copy)
@@ -0,0 +1,67 @@
+use strict;
+use warnings FATAL => 'all';
+
+use Apache::Test;
+use Apache::TestUtil;
+use Apache::TestRequest;
+
+plan tests => 8, need_module 'brotli', need_module 'alias';
+
+my $r;
+
+# GET request against the location with Brotli.
+$r = GET("/only_brotli/index.html", "Accept-Encoding" => "br");
+ok t_cmp($r->code, 200);
+ok t_cmp($r->header("Content-Encoding"), "br", "response Content-Encoding is 
OK");
+if (!defined($r->header("Content-Length"))) {
+    t_debug "Content-Length was expected";
+    ok 0;
+}
+if (!defined($r->header("ETag"))) {
+    t_debug "ETag field was expected";
+    ok 0;
+}
+
+# GET request for a zero-length file.
+$r = GET("/only_brotli/zero.txt", "Accept-Encoding" => "br");
+ok t_cmp($r->code, 200);
+ok t_cmp($r->header("Content-Encoding"), "br", "response Content-Encoding is 
OK");
+if (!defined($r->header("Content-Length"))) {
+    t_debug "Content-Length was expected";
+    ok 0;
+}
+if (!defined($r->header("ETag"))) {
+    t_debug "ETag field was expected";
+    ok 0;
+}
+
+# HEAD request against the location with Brotli.
+$r = HEAD("/only_brotli/index.html", "Accept-Encoding" => "br");
+ok t_cmp($r->code, 200);
+ok t_cmp($r->header("Content-Encoding"), "br", "response Content-Encoding is 
OK");
+if (!defined($r->header("Content-Length"))) {
+    t_debug "Content-Length was expected";
+    ok 0;
+}
+if (!defined($r->header("ETag"))) {
+    t_debug "ETag field was expected";
+    ok 0;
+}
+
+if (have_module('deflate')) {
+    # GET request against the location with fallback to deflate (test that
+    # Brotli is chosen due to the order in SetOutputFilter).
+    $r = GET("/brotli_and_deflate/apache_pb.gif", "Accept-Encoding" => 
"deflate, br");
+    ok t_cmp($r->code, 200);
+    ok t_cmp($r->header("Content-Encoding"), "br", "response Content-Encoding 
is OK");
+    if (!defined($r->header("Content-Length"))) {
+        t_debug "Content-Length was expected";
+        ok 0;
+    }
+    if (!defined($r->header("ETag"))) {
+        t_debug "ETag field was expected";
+        ok 0;
+    }
+} else {
+    skip "skipping tests without mod_deflate" foreach (1..2);
+}

Property changes on: t/modules/brotli.t
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property

Reply via email to