ngx_http_gzip_static_module and gzip_disable

This commit is contained in:
Igor Sysoev 2007-12-27 14:21:59 +00:00
parent 3d70581d45
commit c16b005f30
8 changed files with 669 additions and 210 deletions

View File

@ -265,6 +265,12 @@ if [ $HTTP_FLV = YES ]; then
HTTP_SRCS="$HTTP_SRCS $HTTP_FLV_SRCS"
fi
if [ $HTTP_GZIP_STATIC = YES ]; then
have=NGX_HTTP_GZIP . auto/have
HTTP_MODULES="$HTTP_MODULES $HTTP_GZIP_STATIC_MODULE"
HTTP_SRCS="$HTTP_SRCS $HTTP_GZIP_STATIC_SRCS"
fi
if [ $HTTP_UPSTREAM_IP_HASH = YES ]; then
HTTP_MODULES="$HTTP_MODULES $HTTP_UPSTREAM_IP_HASH_MODULE"
HTTP_SRCS="$HTTP_SRCS $HTTP_UPSTREAM_IP_HASH_SRCS"

View File

@ -76,6 +76,7 @@ HTTP_LIMIT_ZONE=YES
HTTP_EMPTY_GIF=YES
HTTP_BROWSER=YES
HTTP_FLV=NO
HTTP_GZIP_STATIC=NO
HTTP_UPSTREAM_IP_HASH=YES
# STUB
@ -162,6 +163,7 @@ do
--with-http_sub_module) HTTP_SUB=YES ;;
--with-http_dav_module) HTTP_DAV=YES ;;
--with-http_flv_module) HTTP_FLV=YES ;;
--with-http_gzip_static_module) HTTP_GZIP_STATIC=YES ;;
--without-http_charset_module) HTTP_CHARSET=NO ;;
--without-http_gzip_module) HTTP_GZIP=NO ;;
@ -273,6 +275,7 @@ cat << END
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_stub_status_module enable ngx_http_stub_status_module
--without-http_charset_module disable ngx_http_charset_module

View File

@ -417,6 +417,10 @@ HTTP_FLV_MODULE=ngx_http_flv_module
HTTP_FLV_SRCS=src/http/modules/ngx_http_flv_module.c
HTTP_GZIP_STATIC_MODULE=ngx_http_gzip_static_module
HTTP_GZIP_STATIC_SRCS=src/http/modules/ngx_http_gzip_static_module.c
HTTP_UPSTREAM_IP_HASH_MODULE=ngx_http_upstream_ip_hash_module
HTTP_UPSTREAM_IP_HASH_SRCS=src/http/modules/ngx_http_upstream_ip_hash_module.c

View File

@ -14,15 +14,11 @@
typedef struct {
ngx_flag_t enable;
ngx_flag_t no_buffer;
ngx_flag_t vary;
ngx_array_t *types; /* array of ngx_str_t */
ngx_bufs_t bufs;
ngx_uint_t http_version;
ngx_uint_t proxied;
ngx_int_t level;
size_t wbits;
size_t memlevel;
@ -30,17 +26,6 @@ typedef struct {
} ngx_http_gzip_conf_t;
#define NGX_HTTP_GZIP_PROXIED_OFF 0x0002
#define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x0004
#define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x0008
#define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x0010
#define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x0020
#define NGX_HTTP_GZIP_PROXIED_NO_LM 0x0040
#define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x0080
#define NGX_HTTP_GZIP_PROXIED_AUTH 0x0100
#define NGX_HTTP_GZIP_PROXIED_ANY 0x0200
typedef struct {
ngx_chain_t *in;
ngx_chain_t *free;
@ -70,8 +55,6 @@ typedef struct {
} ngx_http_gzip_ctx_t;
static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,
ngx_http_gzip_conf_t *conf);
static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
u_int size);
static void ngx_http_gzip_filter_free(void *opaque, void *address);
@ -99,27 +82,6 @@ static ngx_conf_post_handler_pt ngx_http_gzip_window_p = ngx_http_gzip_window;
static ngx_conf_post_handler_pt ngx_http_gzip_hash_p = ngx_http_gzip_hash;
static ngx_conf_enum_t ngx_http_gzip_http_version[] = {
{ ngx_string("1.0"), NGX_HTTP_VERSION_10 },
{ ngx_string("1.1"), NGX_HTTP_VERSION_11 },
{ ngx_null_string, 0 }
};
static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {
{ ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
{ ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
{ ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
{ ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
{ ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
{ ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
{ ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
{ ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
{ ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_http_gzip_filter_commands[] = {
{ ngx_string("gzip"),
@ -172,20 +134,6 @@ static ngx_command_t ngx_http_gzip_filter_commands[] = {
offsetof(ngx_http_gzip_conf_t, no_buffer),
NULL },
{ ngx_string("gzip_http_version"),
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_gzip_conf_t, http_version),
&ngx_http_gzip_http_version },
{ ngx_string("gzip_proxied"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_gzip_conf_t, proxied),
&ngx_http_gzip_proxied_mask },
{ ngx_string("gzip_min_length"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
@ -193,13 +141,6 @@ static ngx_command_t ngx_http_gzip_filter_commands[] = {
offsetof(ngx_http_gzip_conf_t, min_length),
NULL },
{ ngx_string("gzip_vary"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_gzip_conf_t, vary),
NULL },
ngx_null_command
};
@ -255,10 +196,6 @@ struct gztrailer {
static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");
static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache");
static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store");
static ngx_str_t ngx_http_gzip_private = ngx_string("private");
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
@ -267,11 +204,12 @@ static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static ngx_int_t
ngx_http_gzip_header_filter(ngx_http_request_t *r)
{
ngx_str_t *type;
ngx_uint_t i;
ngx_table_elt_t *header;
ngx_http_gzip_ctx_t *ctx;
ngx_http_gzip_conf_t *conf;
ngx_str_t *type;
ngx_uint_t i;
ngx_table_elt_t *h;
ngx_http_gzip_ctx_t *ctx;
ngx_http_gzip_conf_t *conf;
ngx_http_core_loc_conf_t *clcf;
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
@ -280,23 +218,16 @@ ngx_http_gzip_header_filter(ngx_http_request_t *r)
&& r->headers_out.status != NGX_HTTP_FORBIDDEN
&& r->headers_out.status != NGX_HTTP_NOT_FOUND)
|| r->header_only
|| r != r->main
|| r->http_version < conf->http_version
|| r->headers_out.content_type.len == 0
|| (r->headers_out.content_encoding
&& r->headers_out.content_encoding->value.len)
|| r->headers_in.accept_encoding == NULL
|| (r->headers_out.content_length_n != -1
&& r->headers_out.content_length_n < conf->min_length)
|| ngx_strcasestrn(r->headers_in.accept_encoding->value.data,
"gzip", 4 - 1)
== NULL
)
|| ngx_http_gzip_ok(r) != NGX_OK)
{
return ngx_http_next_header_filter(r);
}
type = conf->types->elts;
for (i = 0; i < conf->types->nelts; i++) {
if (r->headers_out.content_type.len >= type[i].len
@ -309,32 +240,8 @@ ngx_http_gzip_header_filter(ngx_http_request_t *r)
return ngx_http_next_header_filter(r);
found:
if (r->headers_in.via) {
if (conf->proxied & NGX_HTTP_GZIP_PROXIED_OFF) {
return ngx_http_next_header_filter(r);
}
if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_ANY)
&& ngx_http_gzip_proxied(r, conf) == NGX_DECLINED)
{
return ngx_http_next_header_filter(r);
}
}
/*
* if the URL (without the "http://" prefix) is longer than 253 bytes
* then MSIE 4.x can not handle the compressed stream - it waits too long,
* hangs up or crashes
*/
if (r->headers_in.msie4 && r->unparsed_uri.len > 200) {
return ngx_http_next_header_filter(r);
}
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
@ -342,33 +249,34 @@ found:
ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module);
ctx->request = r;
header = ngx_list_push(&r->headers_out.headers);
if (header == NULL) {
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_ERROR;
}
header->hash = 1;
header->key.len = sizeof("Content-Encoding") - 1;
header->key.data = (u_char *) "Content-Encoding";
header->value.len = sizeof("gzip") - 1;
header->value.data = (u_char *) "gzip";
h->hash = 1;
h->key.len = sizeof("Content-Encoding") - 1;
h->key.data = (u_char *) "Content-Encoding";
h->value.len = sizeof("gzip") - 1;
h->value.data = (u_char *) "gzip";
r->headers_out.content_encoding = header;
r->headers_out.content_encoding = h;
if (conf->vary) {
header = ngx_list_push(&r->headers_out.headers);
if (header == NULL) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (clcf->gzip_vary) {
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_ERROR;
}
header->hash = 1;
header->key.len = sizeof("Vary") - 1;
header->key.data = (u_char *) "Vary";
header->value.len = sizeof("Accept-Encoding") - 1;
header->value.data = (u_char *) "Accept-Encoding";
h->hash = 1;
h->key.len = sizeof("Vary") - 1;
h->key.data = (u_char *) "Vary";
h->value.len = sizeof("Accept-Encoding") - 1;
h->value.data = (u_char *) "Accept-Encoding";
}
ctx->length = r->headers_out.content_length_n;
@ -382,89 +290,6 @@ found:
}
static ngx_int_t
ngx_http_gzip_proxied(ngx_http_request_t *r, ngx_http_gzip_conf_t *conf)
{
time_t date, expires;
if (r->headers_in.authorization
&& (conf->proxied & NGX_HTTP_GZIP_PROXIED_AUTH))
{
return NGX_OK;
}
if (r->headers_out.expires) {
if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
return NGX_DECLINED;
}
expires = ngx_http_parse_time(r->headers_out.expires->value.data,
r->headers_out.expires->value.len);
if (expires == NGX_ERROR) {
return NGX_DECLINED;
}
if (r->headers_out.date) {
date = ngx_http_parse_time(r->headers_out.date->value.data,
r->headers_out.date->value.len);
if (date == NGX_ERROR) {
return NGX_DECLINED;
}
} else {
date = ngx_time();
}
if (expires < date) {
return NGX_OK;
}
return NGX_DECLINED;
}
if (r->headers_out.cache_control.elts) {
if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
&& ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,
&ngx_http_gzip_no_cache, NULL) >= 0)
{
return NGX_OK;
}
if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_STORE)
&& ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,
&ngx_http_gzip_no_store, NULL) >= 0)
{
return NGX_OK;
}
if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_PRIVATE)
&& ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,
&ngx_http_gzip_private, NULL) >= 0)
{
return NGX_OK;
}
return NGX_DECLINED;
}
if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_LM)
&& r->headers_out.last_modified)
{
return NGX_DECLINED;
}
if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_ETAG)
&& r->headers_out.etag)
{
return NGX_DECLINED;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
@ -1017,15 +842,11 @@ ngx_http_gzip_create_conf(ngx_conf_t *cf)
* set by ngx_pcalloc():
*
* conf->bufs.num = 0;
* conf->proxied = 0;
* conf->types = NULL;
*/
conf->enable = NGX_CONF_UNSET;
conf->no_buffer = NGX_CONF_UNSET;
conf->vary = NGX_CONF_UNSET;
conf->http_version = NGX_CONF_UNSET_UINT;
conf->level = NGX_CONF_UNSET;
conf->wbits = (size_t) NGX_CONF_UNSET;
@ -1048,18 +869,12 @@ ngx_http_gzip_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize);
ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
NGX_HTTP_VERSION_11);
ngx_conf_merge_bitmask_value(conf->proxied, prev->proxied,
(NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
ngx_conf_merge_value(conf->level, prev->level, 1);
ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
MAX_MEM_LEVEL - 1);
ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
ngx_conf_merge_value(conf->vary, prev->vary, 0);
if (conf->types == NULL) {
if (prev->types == NULL) {

View File

@ -0,0 +1,302 @@
/*
* Copyright (C) Igor Sysoev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_flag_t enable;
} ngx_http_gzip_static_conf_t;
static ngx_int_t ngx_http_gzip_static_handler(ngx_http_request_t *r);
static void *ngx_http_gzip_static_create_conf(ngx_conf_t *cf);
static char *ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_gzip_static_init(ngx_conf_t *cf);
static ngx_command_t ngx_http_gzip_static_commands[] = {
{ ngx_string("gzip_static"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_gzip_static_conf_t, enable),
NULL },
ngx_null_command
};
ngx_http_module_t ngx_http_gzip_static_module_ctx = {
NULL, /* preconfiguration */
ngx_http_gzip_static_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_gzip_static_create_conf, /* create location configuration */
ngx_http_gzip_static_merge_conf /* merge location configuration */
};
ngx_module_t ngx_http_gzip_static_module = {
NGX_MODULE_V1,
&ngx_http_gzip_static_module_ctx, /* module context */
ngx_http_gzip_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_gzip_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_gzip_static_conf_t *gzcf;
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
if (r->uri.data[r->uri.len - 1] == '/') {
return NGX_DECLINED;
}
/* TODO: Win32 */
if (r->zero_in_uri) {
return NGX_DECLINED;
}
gzcf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_static_module);
if (!gzcf->enable || ngx_http_gzip_ok(r) != NGX_OK) {
return NGX_DECLINED;
}
log = r->connection->log;
p = ngx_http_map_uri_to_path(r, &path, &root, sizeof(".gz") - 1);
if (p == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
*p++ = '.';
*p++ = 'g';
*p++ = 'z';
*p = '\0';
path.len = p - path.data;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
"http filename: \"%s\"", path.data);
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
of.test_dir = 0;
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;
rc = ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool);
if (rc == NGX_ERROR) {
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:
level = NGX_LOG_ERR;
rc = NGX_DECLINED;
break;
default:
level = NGX_LOG_CRIT;
rc = NGX_DECLINED;
break;
}
ngx_log_error(level, log, of.err,
ngx_open_file_n " \"%s\" failed", path.data);
return rc;
}
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, ngx_errno,
"\"%s\" is not a regular file", path.data);
return NGX_HTTP_NOT_FOUND;
}
#endif
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_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;
h->key.len = sizeof("Content-Encoding") - 1;
h->key.data = (u_char *) "Content-Encoding";
h->value.len = sizeof("gzip") - 1;
h->value.data = (u_char *) "gzip";
r->headers_out.content_encoding = h;
if (clcf->gzip_vary) {
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_ERROR;
}
h->hash = 1;
h->key.len = sizeof("Vary") - 1;
h->key.data = (u_char *) "Vary";
h->value.len = sizeof("Accept-Encoding") - 1;
h->value.data = (u_char *) "Accept-Encoding";
}
/* 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 = 1;
b->last_in_chain = 1;
b->file->fd = of.fd;
b->file->name = path;
b->file->log = log;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static void *
ngx_http_gzip_static_create_conf(ngx_conf_t *cf)
{
ngx_http_gzip_static_conf_t *conf;
conf = ngx_palloc(cf->pool, sizeof(ngx_http_gzip_static_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_gzip_static_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_gzip_static_conf_t *prev = parent;
ngx_http_gzip_static_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 0);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_gzip_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_gzip_static_handler;
return NGX_OK;
}

View File

@ -71,6 +71,10 @@ static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#if (NGX_HTTP_GZIP)
static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#endif
static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data);
static char *ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data);
@ -98,6 +102,36 @@ static ngx_conf_enum_t ngx_http_core_request_body_in_file[] = {
};
#if (NGX_HTTP_GZIP)
static ngx_conf_enum_t ngx_http_gzip_http_version[] = {
{ ngx_string("1.0"), NGX_HTTP_VERSION_10 },
{ ngx_string("1.1"), NGX_HTTP_VERSION_11 },
{ ngx_null_string, 0 }
};
static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {
{ ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
{ ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
{ ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
{ ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
{ ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
{ ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
{ ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
{ ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
{ ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
{ ngx_null_string, 0 }
};
static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache");
static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store");
static ngx_str_t ngx_http_gzip_private = ngx_string("private");
#endif
static ngx_command_t ngx_http_core_commands[] = {
{ ngx_string("variables_hash_max_size"),
@ -526,6 +560,38 @@ static ngx_command_t ngx_http_core_commands[] = {
offsetof(ngx_http_core_loc_conf_t, resolver_timeout),
NULL },
#if (NGX_HTTP_GZIP)
{ ngx_string("gzip_vary"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, gzip_vary),
NULL },
{ ngx_string("gzip_http_version"),
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_core_loc_conf_t, gzip_http_version),
&ngx_http_gzip_http_version },
{ ngx_string("gzip_proxied"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, gzip_proxied),
&ngx_http_gzip_proxied_mask },
{ ngx_string("gzip_disable"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_gzip_disable,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
#endif
ngx_null_command
};
@ -618,6 +684,7 @@ ngx_http_handler(ngx_http_request_t *r)
}
r->valid_location = 1;
r->gzip = 0;
r->write_event_handler = ngx_http_core_run_phases;
ngx_http_core_run_phases(r);
@ -1404,6 +1471,164 @@ ngx_http_auth_basic_user(ngx_http_request_t *r)
}
#if (NGX_HTTP_GZIP)
ngx_int_t
ngx_http_gzip_ok(ngx_http_request_t *r)
{
time_t date, expires;
ngx_uint_t p;
ngx_array_t *cc;
ngx_table_elt_t *e, *d;
ngx_http_core_loc_conf_t *clcf;
if (r->gzip == 1) {
return NGX_OK;
}
if (r->gzip == 2) {
return NGX_DECLINED;
}
r->gzip = 2;
if (r != r->main
|| r->headers_in.accept_encoding == NULL
|| ngx_strcasestrn(r->headers_in.accept_encoding->value.data,
"gzip", 4 - 1)
== NULL
/*
* if the URL (without the "http://" prefix) is longer than 253 bytes,
* then MSIE 4.x can not handle the compressed stream - it waits
* too long, hangs up or crashes
*/
|| (r->headers_in.msie4 && r->unparsed_uri.len > 200))
{
return NGX_DECLINED;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->http_version < clcf->gzip_http_version) {
return NGX_DECLINED;
}
if (r->headers_in.via == NULL) {
goto ok;
}
p = clcf->gzip_proxied;
if (p & NGX_HTTP_GZIP_PROXIED_OFF) {
return NGX_DECLINED;
}
if (p & NGX_HTTP_GZIP_PROXIED_ANY) {
goto ok;
}
if (r->headers_in.authorization && (p & NGX_HTTP_GZIP_PROXIED_AUTH)) {
goto ok;
}
e = r->headers_out.expires;
if (e) {
if (!(p & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
return NGX_DECLINED;
}
expires = ngx_http_parse_time(e->value.data, e->value.len);
if (expires == NGX_ERROR) {
return NGX_DECLINED;
}
d = r->headers_out.date;
if (d) {
date = ngx_http_parse_time(d->value.data, d->value.len);
if (date == NGX_ERROR) {
return NGX_DECLINED;
}
} else {
date = ngx_time();
}
if (expires < date) {
goto ok;
}
return NGX_DECLINED;
}
cc = &r->headers_out.cache_control;
if (cc->elts) {
if ((p & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
&& ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_cache,
NULL)
>= 0)
{
goto ok;
}
if ((p & NGX_HTTP_GZIP_PROXIED_NO_STORE)
&& ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_store,
NULL)
>= 0)
{
goto ok;
}
if ((p & NGX_HTTP_GZIP_PROXIED_PRIVATE)
&& ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_private,
NULL)
>= 0)
{
goto ok;
}
return NGX_DECLINED;
}
if ((p & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) {
return NGX_DECLINED;
}
if ((p & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) {
return NGX_DECLINED;
}
ok:
#if (NGX_PCRE)
if (clcf->gzip_disable && r->headers_in.user_agent) {
if (ngx_regex_exec_array(clcf->gzip_disable,
&r->headers_in.user_agent->value,
r->connection->log)
!= NGX_DECLINED)
{
return NGX_DECLINED;
}
}
#endif
r->gzip = 1;
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,
@ -2404,6 +2629,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf)
* lcf->exact_match = 0;
* lcf->auto_redirect = 0;
* lcf->alias = 0;
* lcf->gzip_proxied = 0;
*/
lcf->client_max_body_size = NGX_CONF_UNSET;
@ -2434,12 +2660,19 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf)
lcf->server_tokens = NGX_CONF_UNSET;
lcf->types_hash_max_size = NGX_CONF_UNSET_UINT;
lcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT;
lcf->open_file_cache = NGX_CONF_UNSET_PTR;
lcf->open_file_cache_valid = NGX_CONF_UNSET;
lcf->open_file_cache_min_uses = NGX_CONF_UNSET_UINT;
lcf->open_file_cache_errors = NGX_CONF_UNSET;
lcf->open_file_cache_events = NGX_CONF_UNSET;
#if (NGX_HTTP_GZIP)
lcf->gzip_vary = NGX_CONF_UNSET;
lcf->gzip_http_version = NGX_CONF_UNSET_UINT;
lcf->gzip_disable = NGX_CONF_UNSET_PTR;
#endif
return lcf;
}
@ -2650,6 +2883,17 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_sec_value(conf->open_file_cache_events,
prev->open_file_cache_events, 0);
#if (NGX_HTTP_GZIP)
ngx_conf_merge_value(conf->gzip_vary, prev->gzip_vary, 0);
ngx_conf_merge_uint_value(conf->gzip_http_version, prev->gzip_http_version,
NGX_HTTP_VERSION_11);
ngx_conf_merge_bitmask_value(conf->gzip_proxied, prev->gzip_proxied,
(NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
ngx_conf_merge_ptr_value(conf->gzip_disable, prev->gzip_disable, NULL);
#endif
return NGX_CONF_OK;
}
@ -3438,6 +3682,63 @@ ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
#if (NGX_HTTP_GZIP)
static char *
ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
#if (NGX_PCRE)
ngx_http_core_loc_conf_t *clcf = conf;
ngx_str_t err, *value;
ngx_uint_t i;
ngx_regex_elt_t *re;
u_char errstr[NGX_MAX_CONF_ERRSTR];
if (clcf->gzip_disable == NGX_CONF_UNSET_PTR) {
clcf->gzip_disable = ngx_array_create(cf->pool, 2,
sizeof(ngx_regex_elt_t));
if (clcf->gzip_disable == NULL) {
return NGX_CONF_ERROR;
}
}
value = cf->args->elts;
err.len = NGX_MAX_CONF_ERRSTR;
err.data = errstr;
for (i = 1; i < cf->args->nelts; i++) {
re = ngx_array_push(clcf->gzip_disable);
if (re == NULL) {
return NGX_CONF_ERROR;
}
re->regex = ngx_regex_compile(&value[i], NGX_REGEX_CASELESS, cf->pool,
&err);
if (re->regex == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
return NGX_CONF_ERROR;
}
re->name = value[i].data;
}
return NGX_CONF_OK;
#else
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"\"gzip_disable\" requires PCRE library");
return NGX_CONF_ERROR;
#endif
}
#endif
static char *
ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data)
{

View File

@ -13,6 +13,17 @@
#include <ngx_http.h>
#define NGX_HTTP_GZIP_PROXIED_OFF 0x0002
#define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x0004
#define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x0008
#define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x0010
#define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x0020
#define NGX_HTTP_GZIP_PROXIED_NO_LM 0x0040
#define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x0080
#define NGX_HTTP_GZIP_PROXIED_AUTH 0x0100
#define NGX_HTTP_GZIP_PROXIED_ANY 0x0200
typedef struct {
unsigned default_server:1;
unsigned bind:1;
@ -290,6 +301,17 @@ struct ngx_http_core_loc_conf_s {
ngx_flag_t recursive_error_pages; /* recursive_error_pages */
ngx_flag_t server_tokens; /* server_tokens */
#if (NGX_HTTP_GZIP)
ngx_flag_t gzip_vary; /* gzip_vary */
ngx_uint_t gzip_http_version; /* gzip_http_version */
ngx_uint_t gzip_proxied; /* gzip_proxied */
#if (NGX_PCRE)
ngx_array_t *gzip_disable; /* gzip_disable */
#endif
#endif
ngx_array_t *error_pages; /* error_page */
ngx_path_t *client_body_temp_path; /* client_body_temp_path */
@ -330,6 +352,10 @@ ngx_int_t ngx_http_set_exten(ngx_http_request_t *r);
u_char *ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *name,
size_t *root_length, size_t reserved);
ngx_int_t ngx_http_auth_basic_user(ngx_http_request_t *r);
#if (NGX_HTTP_GZIP)
ngx_int_t ngx_http_gzip_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,

View File

@ -430,6 +430,8 @@ struct ngx_http_request_s {
unsigned header_timeout_set:1;
unsigned gzip:2;
unsigned proxy:1;
unsigned bypass_cache:1;
unsigned no_cache:1;