Welcome! Log In Create A New Profile

Advanced

[PATCH] Add static brotli module

Eugene Kluchnikov
February 22, 2017 08:00AM
# HG changeset patch
# User Evgenii Kliuchnikov <eustas.ru@gmail.com>
# Date 1487764873 -3600
# Wed Feb 22 13:01:13 2017 +0100
# Node ID 81eacab152efa88d1296cc04dfd110a168a6b1fb
# Parent 87cf6ddb41c216876d13cffa5e637a61b159362c
Add brotli static serving module

Brotli static serving module is a copy of gzip static serving module
with "gzip" and derivatives replaced with "brotli" and derivatives.

This module does not add any dependencies.
It allows serving offline-compressed content when browser specifies
that "br" content encoding is supported.

For lower binary overhead ngx_http_gzip_accept_encoding and
ngx_http_gzip_quantity were refactored and to be used both by gzip
and brotli modules.

diff -r 87cf6ddb41c2 -r 81eacab152ef auto/modules
--- a/auto/modules Fri Feb 17 17:01:27 2017 +0300
+++ b/auto/modules Wed Feb 22 13:01:13 2017 +0100
@@ -124,6 +124,7 @@

# the module order is important
# ngx_http_static_module
+# ngx_http_brotli_static_module
# ngx_http_gzip_static_module
# ngx_http_dav_module
# ngx_http_autoindex_module
@@ -160,6 +161,7 @@
HTTP_FILTER_MODULES=

ngx_module_order="ngx_http_static_module \
+ ngx_http_brotli_static_module \
ngx_http_gzip_static_module \
ngx_http_dav_module \
ngx_http_autoindex_module \
@@ -451,6 +453,19 @@
. auto/module
fi

+if [ $HTTP_BROTLI_STATIC = YES ]; then
+ have=NGX_HTTP_BROTLI . auto/have
+
+ ngx_module_name=ngx_http_brotli_static_module
+ ngx_module_incs=
+ ngx_module_deps=
+ ngx_module_srcs=src/http/modules/ngx_http_brotli_static_module.c
+ ngx_module_libs=
+ ngx_module_link=$HTTP_BROTLI_STATIC
+
+ . auto/module
+fi
+
if [ $HTTP_GZIP_STATIC = YES ]; then
have=NGX_HTTP_GZIP . auto/have

diff -r 87cf6ddb41c2 -r 81eacab152ef auto/options
--- a/auto/options Fri Feb 17 17:01:27 2017 +0300
+++ b/auto/options Wed Feb 22 13:01:13 2017 +0100
@@ -96,6 +96,7 @@
HTTP_FLV=NO
HTTP_MP4=NO
HTTP_GUNZIP=NO
+HTTP_BROTLI_STATIC=NO
HTTP_GZIP_STATIC=NO
HTTP_UPSTREAM_HASH=YES
HTTP_UPSTREAM_IP_HASH=YES
@@ -236,6 +237,7 @@
--with-http_flv_module) HTTP_FLV=YES ;;
--with-http_mp4_module) HTTP_MP4=YES ;;
--with-http_gunzip_module) HTTP_GUNZIP=YES ;;
+ --with-http_brotli_static_module) HTTP_BROTLI_STATIC=YES ;;
--with-http_gzip_static_module) HTTP_GZIP_STATIC=YES ;;
--with-http_auth_request_module) HTTP_AUTH_REQUEST=YES ;;
--with-http_random_index_module) HTTP_RANDOM_INDEX=YES ;;
@@ -444,6 +446,7 @@
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
+ --with-http_brotli_static_module enable ngx_http_brotli_static_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
diff -r 87cf6ddb41c2 -r 81eacab152ef contrib/vim/syntax/nginx.vim
--- a/contrib/vim/syntax/nginx.vim Fri Feb 17 17:01:27 2017 +0300
+++ b/contrib/vim/syntax/nginx.vim Wed Feb 22 13:01:13 2017 +0100
@@ -86,6 +86,7 @@
syn keyword ngxDirective autoindex
syn keyword ngxDirective autoindex_exact_size
syn keyword ngxDirective autoindex_localtime
+syn keyword ngxDirective brotli_static
syn keyword ngxDirective charset
syn keyword ngxDirective charset_types
syn keyword ngxDirective chunked_transfer_encoding
diff -r 87cf6ddb41c2 -r 81eacab152ef misc/GNUmakefile
--- a/misc/GNUmakefile Fri Feb 17 17:01:27 2017 +0300
+++ b/misc/GNUmakefile Wed Feb 22 13:01:13 2017 +0100
@@ -74,6 +74,7 @@
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
+ --with-http_brotli_static_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
diff -r 87cf6ddb41c2 -r 81eacab152ef
src/http/modules/ngx_http_brotli_static_module.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/http/modules/ngx_http_brotli_static_module.c Wed Feb 22 13:01:13
2017 +0100
@@ -0,0 +1,331 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+#define NGX_HTTP_BROTLI_STATIC_OFF 0
+#define NGX_HTTP_BROTLI_STATIC_ON 1
+#define NGX_HTTP_BROTLI_STATIC_ALWAYS 2
+
+
+typedef struct {
+ ngx_uint_t enable;
+} ngx_http_brotli_static_conf_t;
+
+
+static ngx_int_t ngx_http_brotli_static_handler(ngx_http_request_t *r);
+static void *ngx_http_brotli_static_create_conf(ngx_conf_t *cf);
+static char *ngx_http_brotli_static_merge_conf(ngx_conf_t *cf, void
*parent,
+ void *child);
+static ngx_int_t ngx_http_brotli_static_init(ngx_conf_t *cf);
+
+
+static ngx_conf_enum_t ngx_http_brotli_static[] = {
+ { ngx_string("off"), NGX_HTTP_BROTLI_STATIC_OFF },
+ { ngx_string("on"), NGX_HTTP_BROTLI_STATIC_ON },
+ { ngx_string("always"), NGX_HTTP_BROTLI_STATIC_ALWAYS },
+ { ngx_null_string, 0 }
+};
+
+
+static ngx_command_t ngx_http_brotli_static_commands[] = {
+
+ { ngx_string("brotli_static"),
+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_enum_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_brotli_static_conf_t, enable),
+ &ngx_http_brotli_static },
+
+ ngx_null_command
+};
+
+
+ngx_http_module_t ngx_http_brotli_static_module_ctx = {
+ NULL, /* preconfiguration */
+ ngx_http_brotli_static_init, /* postconfiguration */
+
+ NULL, /* create main configuration */
+ NULL, /* init main configuration */
+
+ NULL, /* create server configuration
*/
+ NULL, /* merge server configuration */
+
+ ngx_http_brotli_static_create_conf, /* create location
configuration */
+ ngx_http_brotli_static_merge_conf /* merge location configuration
*/
+};
+
+
+ngx_module_t ngx_http_brotli_static_module = {
+ NGX_MODULE_V1,
+ &ngx_http_brotli_static_module_ctx, /* module context */
+ ngx_http_brotli_static_commands, /* module directives */
+ NGX_HTTP_MODULE, /* module type */
+ NULL, /* init master */
+ NULL, /* init module */
+ NULL, /* init process */
+ NULL, /* init thread */
+ NULL, /* exit thread */
+ NULL, /* exit process */
+ NULL, /* exit master */
+ NGX_MODULE_V1_PADDING
+};
+
+
+static ngx_int_t
+ngx_http_brotli_static_handler(ngx_http_request_t *r)
+{
+ u_char *p;
+ size_t root;
+ ngx_str_t path;
+ ngx_int_t rc;
+ ngx_uint_t level;
+ ngx_log_t *log;
+ ngx_buf_t *b;
+ ngx_chain_t out;
+ ngx_table_elt_t *h;
+ ngx_open_file_info_t of;
+ ngx_http_core_loc_conf_t *clcf;
+ ngx_http_brotli_static_conf_t *bscf;
+
+ if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
+ return NGX_DECLINED;
+ }
+
+ if (r->uri.data[r->uri.len - 1] == '/') {
+ return NGX_DECLINED;
+ }
+
+ bscf = ngx_http_get_module_loc_conf(r, ngx_http_brotli_static_module);
+
+ if (bscf->enable == NGX_HTTP_BROTLI_STATIC_OFF) {
+ return NGX_DECLINED;
+ }
+
+ if (bscf->enable == NGX_HTTP_BROTLI_STATIC_ON) {
+ rc = ngx_http_brotli_ok(r);
+
+ } else {
+ /* always */
+ rc = NGX_OK;
+ }
+
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+ if (!clcf->gzip_vary && rc != NGX_OK) {
+ return NGX_DECLINED;
+ }
+
+ log = r->connection->log;
+
+ p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".br") - 1);
+ if (p == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ *p++ = '.';
+ *p++ = 'b';
+ *p++ = 'r';
+ *p = '\0';
+
+ path.len = p - path.data;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
+ "http filename: \"%s\"", path.data);
+
+ ngx_memzero(&of, sizeof(ngx_open_file_info_t));
+
+ of.read_ahead = clcf->read_ahead;
+ of.directio = clcf->directio;
+ of.valid = clcf->open_file_cache_valid;
+ of.min_uses = clcf->open_file_cache_min_uses;
+ of.errors = clcf->open_file_cache_errors;
+ of.events = clcf->open_file_cache_events;
+
+ if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
+ != NGX_OK)
+ {
+ switch (of.err) {
+
+ case 0:
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+
+ case NGX_ENOENT:
+ case NGX_ENOTDIR:
+ case NGX_ENAMETOOLONG:
+
+ return NGX_DECLINED;
+
+ case NGX_EACCES:
+#if (NGX_HAVE_OPENAT)
+ case NGX_EMLINK:
+ case NGX_ELOOP:
+#endif
+
+ level = NGX_LOG_ERR;
+ break;
+
+ default:
+
+ level = NGX_LOG_CRIT;
+ break;
+ }
+
+ ngx_log_error(level, log, of.err,
+ "%s \"%s\" failed", of.failed, path.data);
+
+ return NGX_DECLINED;
+ }
+
+ if (bscf->enable == NGX_HTTP_BROTLI_STATIC_ON) {
+ r->gzip_vary = 1;
+
+ if (rc != NGX_OK) {
+ return NGX_DECLINED;
+ }
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d",
of.fd);
+
+ if (of.is_dir) {
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");
+ return NGX_DECLINED;
+ }
+
+#if !(NGX_WIN32) /* the not regular files are probably Unix specific */
+
+ if (!of.is_file) {
+ ngx_log_error(NGX_LOG_CRIT, log, 0,
+ "\"%s\" is not a regular file", path.data);
+
+ return NGX_HTTP_NOT_FOUND;
+ }
+
+#endif
+
+ r->root_tested = !r->error_page;
+
+ rc = ngx_http_discard_request_body(r);
+
+ if (rc != NGX_OK) {
+ return rc;
+ }
+
+ log->action = "sending response to client";
+
+ r->headers_out.status = NGX_HTTP_OK;
+ r->headers_out.content_length_n = of.size;
+ r->headers_out.last_modified_time = of.mtime;
+
+ if (ngx_http_set_etag(r) != NGX_OK) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ if (ngx_http_set_content_type(r) != NGX_OK) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ h = ngx_list_push(&r->headers_out.headers);
+ if (h == NULL) {
+ return NGX_ERROR;
+ }
+
+ h->hash = 1;
+ ngx_str_set(&h->key, "Content-Encoding");
+ ngx_str_set(&h->value, "br");
+ r->headers_out.content_encoding = h;
+
+ /* we need to allocate all before the header would be sent */
+
+ b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
+ if (b == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
+ if (b->file == NULL) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ rc = ngx_http_send_header(r);
+
+ if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
+ return rc;
+ }
+
+ b->file_pos = 0;
+ b->file_last = of.size;
+
+ b->in_file = b->file_last ? 1 : 0;
+ b->last_buf = (r == r->main) ? 1 : 0;
+ b->last_in_chain = 1;
+
+ b->file->fd = of.fd;
+ b->file->name = path;
+ b->file->log = log;
+ b->file->directio = of.is_directio;
+
+ out.buf = b;
+ out.next = NULL;
+
+ return ngx_http_output_filter(r, &out);
+}
+
+
+static void *
+ngx_http_brotli_static_create_conf(ngx_conf_t *cf)
+{
+ ngx_http_brotli_static_conf_t *conf;
+
+ conf = ngx_palloc(cf->pool, sizeof(ngx_http_brotli_static_conf_t));
+ if (conf == NULL) {
+ return NULL;
+ }
+
+ conf->enable = NGX_CONF_UNSET_UINT;
+
+ return conf;
+}
+
+
+static char *
+ngx_http_brotli_static_merge_conf(ngx_conf_t *cf, void *parent, void
*child)
+{
+ ngx_http_brotli_static_conf_t *prev = parent;
+ ngx_http_brotli_static_conf_t *conf = child;
+
+ ngx_conf_merge_uint_value(conf->enable, prev->enable,
+ NGX_HTTP_BROTLI_STATIC_OFF);
+
+ return NGX_CONF_OK;
+}
+
+
+static ngx_int_t
+ngx_http_brotli_static_init(ngx_conf_t *cf)
+{
+ ngx_http_handler_pt *h;
+ ngx_http_core_main_conf_t *cmcf;
+
+ cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
+
+ h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
+ if (h == NULL) {
+ return NGX_ERROR;
+ }
+
+ *h = ngx_http_brotli_static_handler;
+
+ return NGX_OK;
+}
diff -r 87cf6ddb41c2 -r 81eacab152ef src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c Fri Feb 17 17:01:27 2017 +0300
+++ b/src/http/ngx_http_core_module.c Wed Feb 22 13:01:13 2017 +0100
@@ -73,9 +73,11 @@
void *conf);
static char *ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
+#if (NGX_HTTP_BROTLI || NGX_HTTP_GZIP)
+static ngx_int_t ngx_http_accept_encoding(ngx_str_t *ae, char *e, size_t
n);
+static ngx_uint_t ngx_http_encoding_quantity(u_char *p, u_char *last);
+#endif
#if (NGX_HTTP_GZIP)
-static ngx_int_t ngx_http_gzip_accept_encoding(ngx_str_t *ae);
-static ngx_uint_t ngx_http_gzip_quantity(u_char *p, u_char *last);
static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#endif
@@ -2170,7 +2172,7 @@
*/

if (ngx_memcmp(ae->value.data, "gzip,", 5) != 0
- && ngx_http_gzip_accept_encoding(&ae->value) != NGX_OK)
+ && ngx_http_accept_encoding(&ae->value, "gzip", 4) != NGX_OK)
{
return NGX_DECLINED;
}
@@ -2296,16 +2298,20 @@
return NGX_OK;
}

+#endif
+
+
+#if (NGX_HTTP_BROTLI || NGX_HTTP_GZIP)

/*
- * gzip is enabled for the following quantities:
+ * encoding is enabled for the following quantities:
* "gzip; q=0.001" ... "gzip; q=1.000"
- * gzip is disabled for the following quantities:
- * "gzip; q=0" ... "gzip; q=0.000", and for any invalid cases
+ * encoding is disabled for the following quantities:
+ * "br; q=0" ... "br; q=0.000", and for any invalid cases
*/

static ngx_int_t
-ngx_http_gzip_accept_encoding(ngx_str_t *ae)
+ngx_http_accept_encoding(ngx_str_t *ae, char *e, size_t n)
{
u_char *p, *start, *last;

@@ -2313,7 +2319,7 @@
last = start + ae->len;

for ( ;; ) {
- p = ngx_strcasestrn(start, "gzip", 4 - 1);
+ p = ngx_strcasestrn(start, e, n - 1);
if (p == NULL) {
return NGX_DECLINED;
}
@@ -2322,10 +2328,10 @@
break;
}

- start = p + 4;
- }
-
- p += 4;
+ start = p + n;
+ }
+
+ p += n;

while (p < last) {
switch (*p++) {
@@ -2364,7 +2370,7 @@
return NGX_DECLINED;
}

- if (ngx_http_gzip_quantity(p, last) == 0) {
+ if (ngx_http_encoding_quantity(p, last) == 0) {
return NGX_DECLINED;
}

@@ -2373,7 +2379,7 @@


static ngx_uint_t
-ngx_http_gzip_quantity(u_char *p, u_char *last)
+ngx_http_encoding_quantity(u_char *p, u_char *last)
{
u_char c;
ngx_uint_t n, q;
@@ -2428,6 +2434,37 @@
#endif


+#if (NGX_HTTP_BROTLI)
+
+ngx_int_t
+ngx_http_brotli_ok(ngx_http_request_t *r)
+{
+ ngx_table_elt_t *ae;
+
+ if (r != r->main) {
+ return NGX_DECLINED;
+ }
+
+ ae = r->headers_in.accept_encoding;
+ if (ae == NULL) {
+ return NGX_DECLINED;
+ }
+
+ if (ae->value.len < sizeof("br") - 1) {
+ return NGX_DECLINED;
+ }
+
+ if (ngx_http_accept_encoding(&ae->value, "br", 2) != NGX_OK)
+ {
+ return NGX_DECLINED;
+ }
+
+ return NGX_OK;
+}
+
+#endif
+
+
ngx_int_t
ngx_http_subrequest(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
diff -r 87cf6ddb41c2 -r 81eacab152ef src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h Fri Feb 17 17:01:27 2017 +0300
+++ b/src/http/ngx_http_core_module.h Wed Feb 22 13:01:13 2017 +0100
@@ -506,6 +506,9 @@
ngx_int_t ngx_http_gzip_ok(ngx_http_request_t *r);
#endif

+#if (NGX_HTTP_BROTLI)
+ngx_int_t ngx_http_brotli_ok(ngx_http_request_t *r);
+#endif

ngx_int_t ngx_http_subrequest(ngx_http_request_t *r,
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **sr,


--
С наилучшими пожеланиями, Евгений Ключников
WBR, Eugene Kluchnikov
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[PATCH] Add static brotli module

Eugene Kluchnikov 539 February 22, 2017 08:00AM

Re: [PATCH] Add static brotli module

Valentin V. Bartenev 310 February 22, 2017 09:42AM

Re: [PATCH] Add static brotli module

Eugene Kluchnikov 331 February 22, 2017 10:52AM

Re: [PATCH] Add static brotli module

Eugene Kluchnikov 361 February 23, 2017 07:14AM

Re: [PATCH] Add static brotli module

Eugene Kluchnikov 266 February 23, 2017 02:04PM

Re: [PATCH] Add static brotli module

fanboy 402 February 24, 2017 03:06AM

Re: [PATCH] Add static brotli module

Eugene Kluchnikov 293 February 24, 2017 06:04AM

Re: [PATCH] Add static brotli module

Eugene Kluchnikov 318 February 24, 2017 06:10AM

Re: [PATCH] Add static brotli module

fanboy 556 February 24, 2017 06:36AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 312
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready