Ticket #798: brotli_1_11_10.patch

File brotli_1_11_10.patch, 9.6 KB (added by eustas@…, 10 years ago)
  • contrib/vim/syntax/nginx.vim

    # HG changeset patch
    # User Evgenii Kliuchnikov <eustas.ru@gmail.com>
    # Date 1487932524 -3600
    #      Fri Feb 24 11:35:24 2017 +0100
    # Node ID 662115ad250f48cfdd7963de40bfe7c7c8ae8ba3
    # Parent  1ad0999a7ded3d4fb01c7acf8ff57c80b643da7e
    Add brotli static serving support (ontoZZ 1.11.10)
    
    Both .gz and .br static content is served by
    ngx_http_gzip_static_modile, but have separate configuration.
    
    diff -r 1ad0999a7ded -r 662115ad250f contrib/vim/syntax/nginx.vim
    a b  
    8686syn keyword ngxDirective autoindex
    8787syn keyword ngxDirective autoindex_exact_size
    8888syn keyword ngxDirective autoindex_localtime
     89syn keyword ngxDirective brotli_static
    8990syn keyword ngxDirective charset
    9091syn keyword ngxDirective charset_types
    9192syn keyword ngxDirective chunked_transfer_encoding
  • src/http/modules/ngx_http_gzip_static_module.c

    diff -r 1ad0999a7ded -r 662115ad250f src/http/modules/ngx_http_gzip_static_module.c
    a b  
    1616
    1717
    1818typedef struct {
    19     ngx_uint_t  enable;
     19    ngx_uint_t  enable_gzip;
     20    ngx_uint_t  enable_brotli;
    2021} ngx_http_gzip_static_conf_t;
    2122
    2223
     24static ngx_int_t ngx_http_gzip_static_serve_file(ngx_http_request_t *r,
     25    ngx_uint_t enable, ngx_uint_t ok, ngx_http_core_loc_conf_t *clcf,
     26    ngx_str_t *encoding);
    2327static ngx_int_t ngx_http_gzip_static_handler(ngx_http_request_t *r);
    2428static void *ngx_http_gzip_static_create_conf(ngx_conf_t *cf);
    2529static char *ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent,
     
    4145      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
    4246      ngx_conf_set_enum_slot,
    4347      NGX_HTTP_LOC_CONF_OFFSET,
    44       offsetof(ngx_http_gzip_static_conf_t, enable),
     48      offsetof(ngx_http_gzip_static_conf_t, enable_gzip),
     49      &ngx_http_gzip_static },
     50
     51    { ngx_string("brotli_static"),
     52      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
     53      ngx_conf_set_enum_slot,
     54      NGX_HTTP_LOC_CONF_OFFSET,
     55      offsetof(ngx_http_gzip_static_conf_t, enable_brotli),
    4556      &ngx_http_gzip_static },
    4657
    4758      ngx_null_command
     
    7990};
    8091
    8192
     93static ngx_str_t  encoding_gzip = ngx_string("gzip");
     94static ngx_str_t  encoding_brotli = ngx_string("br");
     95
     96
    8297static ngx_int_t
    8398ngx_http_gzip_static_handler(ngx_http_request_t *r)
    8499{
    85     u_char                       *p;
    86     size_t                        root;
    87     ngx_str_t                     path;
    88100    ngx_int_t                     rc;
    89     ngx_uint_t                    level;
    90     ngx_log_t                    *log;
    91     ngx_buf_t                    *b;
    92     ngx_chain_t                   out;
    93     ngx_table_elt_t              *h;
    94     ngx_open_file_info_t          of;
    95101    ngx_http_core_loc_conf_t     *clcf;
    96102    ngx_http_gzip_static_conf_t  *gzcf;
    97103
     
    105111
    106112    gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
    107113
    108     if (gzcf->enable == NGX_HTTP_GZIP_STATIC_OFF) {
     114    if (gzcf->enable_gzip == NGX_HTTP_GZIP_STATIC_OFF
     115        && gzcf->enable_brotli == NGX_HTTP_GZIP_STATIC_OFF) {
    109116        return NGX_DECLINED;
    110117    }
    111118
    112     if (gzcf->enable == NGX_HTTP_GZIP_STATIC_ON) {
    113         rc = ngx_http_gzip_ok(r);
     119    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
    114120
    115     } else {
    116         /* always */
    117         rc = NGX_OK;
     121    rc = NGX_DECLINED;
     122    if (gzcf->enable_gzip != NGX_HTTP_GZIP_STATIC_OFF) {
     123        if (gzcf->enable_gzip == NGX_HTTP_GZIP_STATIC_ON) {
     124            rc = ngx_http_gzip_ok(r);
     125        } else {
     126            /* always */
     127            rc = NGX_OK;
     128        }
     129        rc = ngx_http_gzip_static_serve_file(r, gzcf->enable_gzip, rc, clcf,
     130            &encoding_gzip);
    118131    }
    119132
    120     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
     133    if (gzcf->enable_brotli != NGX_HTTP_GZIP_STATIC_OFF && rc == NGX_DECLINED) {
     134        if (gzcf->enable_gzip == NGX_HTTP_GZIP_STATIC_ON) {
     135            rc = ngx_http_gzip_ok(r);
     136        } else {
     137            /* always */
     138            rc = NGX_OK;
     139        }
     140        rc = ngx_http_gzip_static_serve_file(r, gzcf->enable_brotli, rc, clcf,
     141            &encoding_brotli);
     142    }
     143
     144    return rc;
     145}
     146
     147
     148static ngx_int_t
     149ngx_http_gzip_static_serve_file(ngx_http_request_t *r, ngx_uint_t enable,
     150    ngx_uint_t ok, ngx_http_core_loc_conf_t *clcf, ngx_str_t *encoding)
     151{
     152    u_char               *p;
     153    size_t                root;
     154    ngx_str_t             path;
     155    ngx_int_t             rc;
     156    ngx_uint_t            level;
     157    ngx_log_t            *log;
     158    ngx_buf_t            *b;
     159    ngx_chain_t           out;
     160    ngx_table_elt_t      *h;
     161    ngx_open_file_info_t  of;
     162
     163    rc = ok;
    121164
    122165    if (!clcf->gzip_vary && rc != NGX_OK) {
    123166        return NGX_DECLINED;
     
    125168
    126169    log = r->connection->log;
    127170
    128     p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
     171    p = ngx_http_map_uri_to_path(r, &path, &root, 4 - 1);
    129172    if (p == NULL) {
    130173        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    131174    }
    132175
    133176    *p++ = '.';
    134     *p++ = 'g';
    135     *p++ = 'z';
     177    *p++ = encoding->data[0];
     178    *p++ = encoding->data[1];
    136179    *p = '\0';
    137180
    138181    path.len = p - path.data;
     
    188231        return NGX_DECLINED;
    189232    }
    190233
    191     if (gzcf->enable == NGX_HTTP_GZIP_STATIC_ON) {
     234    if (enable == NGX_HTTP_GZIP_STATIC_ON) {
    192235        r->gzip_vary = 1;
    193236
    194237        if (rc != NGX_OK) {
     
    243286
    244287    h->hash = 1;
    245288    ngx_str_set(&h->key, "Content-Encoding");
    246     ngx_str_set(&h->value, "gzip");
     289    h->value = *encoding;
    247290    r->headers_out.content_encoding = h;
    248291
    249292    /* we need to allocate all before the header would be sent */
     
    293336        return NULL;
    294337    }
    295338
    296     conf->enable = NGX_CONF_UNSET_UINT;
     339    conf->enable_gzip = NGX_CONF_UNSET_UINT;
     340    conf->enable_brotli = NGX_CONF_UNSET_UINT;
    297341
    298342    return conf;
    299343}
     
    305349    ngx_http_gzip_static_conf_t *prev = parent;
    306350    ngx_http_gzip_static_conf_t *conf = child;
    307351
    308     ngx_conf_merge_uint_value(conf->enable, prev->enable,
     352    ngx_conf_merge_uint_value(conf->enable_gzip, prev->enable_gzip,
     353                              NGX_HTTP_GZIP_STATIC_OFF);
     354    ngx_conf_merge_uint_value(conf->enable_brotli, prev->enable_brotli,
    309355                              NGX_HTTP_GZIP_STATIC_OFF);
    310356
    311357    return NGX_CONF_OK;
  • src/http/ngx_http_core_module.c

    diff -r 1ad0999a7ded -r 662115ad250f src/http/ngx_http_core_module.c
    a b  
    7474static char *ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
    7575    void *conf);
    7676#if (NGX_HTTP_GZIP)
    77 static ngx_int_t ngx_http_gzip_accept_encoding(ngx_str_t *ae);
    78 static ngx_uint_t ngx_http_gzip_quantity(u_char *p, u_char *last);
     77static ngx_int_t ngx_http_accept_encoding(ngx_str_t *ae, char *e, size_t n);
     78static ngx_uint_t ngx_http_encoding_quantity(u_char *p, u_char *last);
    7979static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd,
    8080    void *conf);
    8181#endif
     
    21702170     */
    21712171
    21722172    if (ngx_memcmp(ae->value.data, "gzip,", 5) != 0
    2173         && ngx_http_gzip_accept_encoding(&ae->value) != NGX_OK)
     2173        && ngx_http_accept_encoding(&ae->value, "gzip", 4) != NGX_OK)
    21742174    {
    21752175        return NGX_DECLINED;
    21762176    }
     
    22972297}
    22982298
    22992299
     2300ngx_int_t
     2301ngx_http_brotli_ok(ngx_http_request_t *r)
     2302{
     2303    ngx_table_elt_t  *ae;
     2304
     2305    if (r != r->main) {
     2306        return NGX_DECLINED;
     2307    }
     2308
     2309    ae = r->headers_in.accept_encoding;
     2310    if (ae == NULL) {
     2311        return NGX_DECLINED;
     2312    }
     2313
     2314    if (ae->value.len < sizeof("br") - 1) {
     2315        return NGX_DECLINED;
     2316    }
     2317
     2318    if (ngx_http_accept_encoding(&ae->value, "br", 2) != NGX_OK)
     2319    {
     2320        return NGX_DECLINED;
     2321    }
     2322
     2323    return NGX_OK;
     2324}
     2325
     2326
    23002327/*
    2301  * gzip is enabled for the following quantities:
     2328 * encoding is enabled for the following quantities:
    23022329 *     "gzip; q=0.001" ... "gzip; q=1.000"
    2303  * gzip is disabled for the following quantities:
    2304  *     "gzip; q=0" ... "gzip; q=0.000", and for any invalid cases
     2330 * encoding is disabled for the following quantities:
     2331 *     "br; q=0" ... "br; q=0.000", and for any invalid cases
    23052332 */
    23062333
    23072334static ngx_int_t
    2308 ngx_http_gzip_accept_encoding(ngx_str_t *ae)
     2335ngx_http_accept_encoding(ngx_str_t *ae, char *e, size_t n)
    23092336{
    23102337    u_char  *p, *start, *last;
    23112338
     
    23132340    last = start + ae->len;
    23142341
    23152342    for ( ;; ) {
    2316         p = ngx_strcasestrn(start, "gzip", 4 - 1);
     2343        p = ngx_strcasestrn(start, e, n - 1);
    23172344        if (p == NULL) {
    23182345            return NGX_DECLINED;
    23192346        }
     
    23222349            break;
    23232350        }
    23242351
    2325         start = p + 4;
    2326     }
    2327 
    2328     p += 4;
     2352        start = p + n;
     2353    }
     2354
     2355    p += n;
    23292356
    23302357    while (p < last) {
    23312358        switch (*p++) {
     
    23642391        return NGX_DECLINED;
    23652392    }
    23662393
    2367     if (ngx_http_gzip_quantity(p, last) == 0) {
     2394    if (ngx_http_encoding_quantity(p, last) == 0) {
    23682395        return NGX_DECLINED;
    23692396    }
    23702397
     
    23732400
    23742401
    23752402static ngx_uint_t
    2376 ngx_http_gzip_quantity(u_char *p, u_char *last)
     2403ngx_http_encoding_quantity(u_char *p, u_char *last)
    23772404{
    23782405    u_char      c;
    23792406    ngx_uint_t  n, q;
  • src/http/ngx_http_core_module.h

    diff -r 1ad0999a7ded -r 662115ad250f src/http/ngx_http_core_module.h
    a b  
    504504ngx_int_t ngx_http_auth_basic_user(ngx_http_request_t *r);
    505505#if (NGX_HTTP_GZIP)
    506506ngx_int_t ngx_http_gzip_ok(ngx_http_request_t *r);
     507ngx_int_t ngx_http_brotli_ok(ngx_http_request_t *r);
    507508#endif
    508509
    509510