[PATCH] Http gunzip: additional configuration

Alon Blayer-Gat alon.blayergat at gmail.com
Sun Nov 27 12:27:56 UTC 2016


Hi,

1) 'gunzip always' option will gunzip even if the client supports it.
2) 'gunzip types', like 'always' but only for file types specified
with 'gunzip_types <mime-types>'
3) Allow gunzip and gunzip_types directives within "if in location"
block (rewrite phase condition).

The suggested changes are needed, mainly, to allow dynamic
modification of compressed response (e.g. with the 'sub_filter'
module)
'types' and 'if in location' may allow a more selective operation.

Changes do not present any backward incompatibility.

Directives changes summary:

    gunzip (on|types|always|off)

        Context: http, server, location, if in location
        Default: off

        off
          Disable gunzip
        on
          Enable gunzip for clients that doesn't support it.
        always
          Enable gunzip even if clients support it.
        types
          Enable gunzip for clients that doesn't support it. but for
mime types specified
          by the gunzip_types directive.

    gunzip_types mime-type ...;

        Context: http, server, location, if in location
        Default: gunzip_types text/html;


# HG changeset patch
# User Alon Blayer-Gat <Alon.BlayerGat at gmail.com>
# Date 1480245650 -7200
#      Sun Nov 27 13:20:50 2016 +0200
# Node ID d7223bbfd5b3e631ee09f876150583d18bd2d092
# Parent  2c7a2d75938a31044552b0a6cd6edaebdaf0bd69
http gunzip: additional configurations

allow within 'if in location', gunzip 'always' or 'types' for mime-types

# HG changeset patch
# User Alon Blayer-Gat <Alon.BlayerGat at gmail.com>
# Date 1480245650 -7200
#      Sun Nov 27 13:20:50 2016 +0200
# Node ID d7223bbfd5b3e631ee09f876150583d18bd2d092
# Parent  2c7a2d75938a31044552b0a6cd6edaebdaf0bd69
http gunzip: additional configurations

allow within 'if in location', gunzip 'always' or 'types' for mime-types

diff -r 2c7a2d75938a -r d7223bbfd5b3
src/http/modules/ngx_http_gunzip_filter_module.c
--- a/src/http/modules/ngx_http_gunzip_filter_module.c Mon Nov 21
16:49:19 2016 +0300
+++ b/src/http/modules/ngx_http_gunzip_filter_module.c Sun Nov 27
13:20:50 2016 +0200
@@ -13,9 +13,17 @@
 #include <zlib.h>


+#define NGX_HTTP_GUNZIP_OFF     0
+#define NGX_HTTP_GUNZIP_ON      1
+#define NGX_HTTP_GUNZIP_ALWAYS  2
+#define NGX_HTTP_GUNZIP_TYPES   3
+
+
 typedef struct {
-    ngx_flag_t           enable;
+    ngx_uint_t           enable;
     ngx_bufs_t           bufs;
+    ngx_hash_t           types;
+    ngx_array_t         *types_keys;
 } ngx_http_gunzip_conf_t;


@@ -62,14 +70,24 @@
     void *parent, void *child);


+static ngx_conf_enum_t  ngx_http_gunzip[] = {
+    { ngx_string("off"), NGX_HTTP_GUNZIP_OFF },
+    { ngx_string("on"), NGX_HTTP_GUNZIP_ON },
+    { ngx_string("always"), NGX_HTTP_GUNZIP_ALWAYS },
+    { ngx_string("types"), NGX_HTTP_GUNZIP_TYPES },
+    { ngx_null_string, 0 }
+};
+
+
 static ngx_command_t  ngx_http_gunzip_filter_commands[] = {

     { ngx_string("gunzip"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
-      ngx_conf_set_flag_slot,
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
+                        |NGX_CONF_TAKE1,
+      ngx_conf_set_enum_slot,
       NGX_HTTP_LOC_CONF_OFFSET,
       offsetof(ngx_http_gunzip_conf_t, enable),
-      NULL },
+      &ngx_http_gunzip },

     { ngx_string("gunzip_buffers"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
@@ -78,6 +96,13 @@
       offsetof(ngx_http_gunzip_conf_t, bufs),
       NULL },

+    { ngx_string("gunzip_types"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
+      ngx_http_types_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_gunzip_conf_t, types_keys),
+      &ngx_http_html_default_types[0] },
+
       ngx_null_command
 };

@@ -126,10 +151,10 @@
     conf = ngx_http_get_module_loc_conf(r, ngx_http_gunzip_filter_module);

     /* TODO support multiple content-codings */
-    /* TODO always gunzip - due to configuration or module request */
+
     /* TODO ignore content encoding? */

-    if (!conf->enable
+    if (conf->enable == NGX_HTTP_GUNZIP_OFF
         || r->headers_out.content_encoding == NULL
         || r->headers_out.content_encoding->value.len != 4
         || ngx_strncasecmp(r->headers_out.content_encoding->value.data,
@@ -140,14 +165,22 @@

     r->gzip_vary = 1;

-    if (!r->gzip_tested) {
-        if (ngx_http_gzip_ok(r) == NGX_OK) {
+    if (conf->enable == NGX_HTTP_GUNZIP_ON) {
+        if (!r->gzip_tested) {
+            if (ngx_http_gzip_ok(r) == NGX_OK) {
+                return ngx_http_next_header_filter(r);
+            }
+
+        } else if (r->gzip_ok) {
             return ngx_http_next_header_filter(r);
         }
-
-    } else if (r->gzip_ok) {
-        return ngx_http_next_header_filter(r);
     }
+    else if (conf->enable == NGX_HTTP_GUNZIP_TYPES
+             && ngx_http_test_content_type(r, &conf->types) == NULL)
+     {
+             return ngx_http_next_header_filter(r);
+     }
+     /* else always gunzip - conf->enable == NGX_HTTP_GUNZIP_ALWAYS) */

     ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gunzip_ctx_t));
     if (ctx == NULL) {
@@ -651,9 +684,11 @@
      * set by ngx_pcalloc():
      *
      *     conf->bufs.num = 0;
+     *     conf->types = { NULL };
+     *     conf->types_keys = NULL;
      */

-    conf->enable = NGX_CONF_UNSET;
+    conf->enable = NGX_CONF_UNSET_UINT;

     return conf;
 }
@@ -670,6 +705,14 @@
     ngx_conf_merge_bufs_value(conf->bufs, prev->bufs,
                               (128 * 1024) / ngx_pagesize, ngx_pagesize);

+    if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
+                                 &prev->types_keys, &prev->types,
+                                 ngx_http_html_default_types)
+        != NGX_OK)
+    {
+        return NGX_CONF_ERROR;
+    }
+
     return NGX_CONF_OK;
 }



More information about the nginx-devel mailing list