3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
27 ngx_array_t *types_keys;
28 } ngx_http_gzip_conf_t;
36 ngx_chain_t **last_out;
39 ngx_chain_t *copy_buf;
64 ngx_http_request_t *request;
65 } ngx_http_gzip_ctx_t;
68 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
75 #else /* NGX_HAVE_BIG_ENDIAN || !NGX_HAVE_NONALIGNED */
85 static void ngx_http_gzip_filter_memory(ngx_http_request_t *r,
86 ngx_http_gzip_ctx_t *ctx);
87 static ngx_int_t ngx_http_gzip_filter_copy_recycled(ngx_http_gzip_ctx_t *ctx,
89 static ngx_int_t ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
90 ngx_http_gzip_ctx_t *ctx);
91 static ngx_int_t ngx_http_gzip_filter_gzheader(ngx_http_request_t *r,
92 ngx_http_gzip_ctx_t *ctx);
93 static ngx_int_t ngx_http_gzip_filter_add_data(ngx_http_request_t *r,
94 ngx_http_gzip_ctx_t *ctx);
95 static ngx_int_t ngx_http_gzip_filter_get_buf(ngx_http_request_t *r,
96 ngx_http_gzip_ctx_t *ctx);
97 static ngx_int_t ngx_http_gzip_filter_deflate(ngx_http_request_t *r,
98 ngx_http_gzip_ctx_t *ctx);
99 static ngx_int_t ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
100 ngx_http_gzip_ctx_t *ctx);
102 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
104 static void ngx_http_gzip_filter_free(void *opaque, void *address);
105 static void ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
106 ngx_http_gzip_ctx_t *ctx);
108 static ngx_int_t ngx_http_gzip_add_variables(ngx_conf_t *cf);
109 static ngx_int_t ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
110 ngx_http_variable_value_t *v, uintptr_t data);
112 static ngx_int_t ngx_http_gzip_filter_init(ngx_conf_t *cf);
113 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf);
114 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
115 void *parent, void *child);
116 static char *ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data);
117 static char *ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data);
120 static ngx_conf_num_bounds_t ngx_http_gzip_comp_level_bounds = {
121 ngx_conf_check_num_bounds, 1, 9
124 static ngx_conf_post_handler_pt ngx_http_gzip_window_p = ngx_http_gzip_window;
125 static ngx_conf_post_handler_pt ngx_http_gzip_hash_p = ngx_http_gzip_hash;
128 static ngx_command_t ngx_http_gzip_filter_commands[] = {
130 { ngx_string("gzip"),
131 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
133 ngx_conf_set_flag_slot,
134 NGX_HTTP_LOC_CONF_OFFSET,
135 offsetof(ngx_http_gzip_conf_t, enable),
138 { ngx_string("gzip_buffers"),
139 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
140 ngx_conf_set_bufs_slot,
141 NGX_HTTP_LOC_CONF_OFFSET,
142 offsetof(ngx_http_gzip_conf_t, bufs),
145 { ngx_string("gzip_types"),
146 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
148 NGX_HTTP_LOC_CONF_OFFSET,
149 offsetof(ngx_http_gzip_conf_t, types_keys),
150 &ngx_http_html_default_types[0] },
152 { ngx_string("gzip_comp_level"),
153 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
154 ngx_conf_set_num_slot,
155 NGX_HTTP_LOC_CONF_OFFSET,
156 offsetof(ngx_http_gzip_conf_t, level),
157 &ngx_http_gzip_comp_level_bounds },
159 { ngx_string("gzip_window"),
160 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
161 ngx_conf_set_size_slot,
162 NGX_HTTP_LOC_CONF_OFFSET,
163 offsetof(ngx_http_gzip_conf_t, wbits),
164 &ngx_http_gzip_window_p },
166 { ngx_string("gzip_hash"),
167 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
168 ngx_conf_set_size_slot,
169 NGX_HTTP_LOC_CONF_OFFSET,
170 offsetof(ngx_http_gzip_conf_t, memlevel),
171 &ngx_http_gzip_hash_p },
173 { ngx_string("gzip_no_buffer"),
174 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
175 ngx_conf_set_flag_slot,
176 NGX_HTTP_LOC_CONF_OFFSET,
177 offsetof(ngx_http_gzip_conf_t, no_buffer),
180 { ngx_string("gzip_min_length"),
181 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
182 ngx_conf_set_size_slot,
183 NGX_HTTP_LOC_CONF_OFFSET,
184 offsetof(ngx_http_gzip_conf_t, min_length),
191 static ngx_http_module_t ngx_http_gzip_filter_module_ctx = {
192 ngx_http_gzip_add_variables, /* preconfiguration */
193 ngx_http_gzip_filter_init, /* postconfiguration */
195 NULL, /* create main configuration */
196 NULL, /* init main configuration */
198 NULL, /* create server configuration */
199 NULL, /* merge server configuration */
201 ngx_http_gzip_create_conf, /* create location configuration */
202 ngx_http_gzip_merge_conf /* merge location configuration */
206 ngx_module_t ngx_http_gzip_filter_module = {
208 &ngx_http_gzip_filter_module_ctx, /* module context */
209 ngx_http_gzip_filter_commands, /* module directives */
210 NGX_HTTP_MODULE, /* module type */
211 NULL, /* init master */
212 NULL, /* init module */
213 NULL, /* init process */
214 NULL, /* init thread */
215 NULL, /* exit thread */
216 NULL, /* exit process */
217 NULL, /* exit master */
218 NGX_MODULE_V1_PADDING
222 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");
224 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
225 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
229 ngx_http_gzip_header_filter(ngx_http_request_t *r)
232 ngx_http_gzip_ctx_t *ctx;
233 ngx_http_gzip_conf_t *conf;
235 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
238 || (r->headers_out.status != NGX_HTTP_OK
239 && r->headers_out.status != NGX_HTTP_FORBIDDEN
240 && r->headers_out.status != NGX_HTTP_NOT_FOUND)
242 || (r->headers_out.content_encoding
243 && r->headers_out.content_encoding->value.len)
244 || (r->headers_out.content_length_n != -1
245 && r->headers_out.content_length_n < conf->min_length)
246 || ngx_http_test_content_type(r, &conf->types) == NULL
247 || ngx_http_gzip_ok(r) != NGX_OK)
249 return ngx_http_next_header_filter(r);
252 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t));
257 ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module);
262 ngx_http_gzip_filter_memory(r, ctx);
264 h = ngx_list_push(&r->headers_out.headers);
270 h->key.len = sizeof("Content-Encoding") - 1;
271 h->key.data = (u_char *) "Content-Encoding";
272 h->value.len = sizeof("gzip") - 1;
273 h->value.data = (u_char *) "gzip";
275 r->headers_out.content_encoding = h;
277 r->main_filter_need_in_memory = 1;
279 ngx_http_clear_content_length(r);
280 ngx_http_clear_accept_ranges(r);
282 return ngx_http_next_header_filter(r);
287 ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
291 ngx_http_gzip_ctx_t *ctx;
293 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
295 if (ctx == NULL || ctx->done) {
296 return ngx_http_next_body_filter(r, in);
299 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
302 if (ctx->buffering) {
305 switch (ngx_http_gzip_filter_copy_recycled(ctx, in)) {
314 default: /* NGX_ERROR */
323 if (ctx->preallocated == NULL) {
324 if (ngx_http_gzip_filter_deflate_start(r, ctx) != NGX_OK) {
330 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {
337 /* flush busy buffers */
339 if (ngx_http_next_body_filter(r, NULL) == NGX_ERROR) {
345 ngx_chain_update_chains(&ctx->free, &ctx->busy, &cl,
346 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
352 /* cycle while we can write to a client */
356 /* cycle while there is data to feed zlib and ... */
358 rc = ngx_http_gzip_filter_add_data(r, ctx);
360 if (rc == NGX_DECLINED) {
364 if (rc == NGX_AGAIN) {
369 /* ... there are buffers to write zlib output */
371 rc = ngx_http_gzip_filter_get_buf(r, ctx);
373 if (rc == NGX_DECLINED) {
377 if (rc == NGX_ERROR) {
382 rc = ngx_http_gzip_filter_deflate(r, ctx);
388 if (rc == NGX_ERROR) {
392 /* rc == NGX_AGAIN */
395 if (ctx->out == NULL) {
396 ngx_http_gzip_filter_free_copy_buf(r, ctx);
398 return ctx->busy ? NGX_AGAIN : NGX_OK;
401 if (!ctx->gzheader) {
402 if (ngx_http_gzip_filter_gzheader(r, ctx) != NGX_OK) {
407 rc = ngx_http_next_body_filter(r, ctx->out);
409 if (rc == NGX_ERROR) {
413 ngx_http_gzip_filter_free_copy_buf(r, ctx);
415 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
416 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
417 ctx->last_out = &ctx->out;
432 if (ctx->preallocated) {
433 deflateEnd(&ctx->zstream);
435 ngx_pfree(r->pool, ctx->preallocated);
438 ngx_http_gzip_filter_free_copy_buf(r, ctx);
445 ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
448 ngx_http_gzip_conf_t *conf;
450 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
453 memlevel = conf->memlevel;
455 if (r->headers_out.content_length_n > 0) {
457 /* the actual zlib window size is smaller by 262 bytes */
459 while (r->headers_out.content_length_n < ((1 << (wbits - 1)) - 262)) {
466 ctx->memlevel = memlevel;
469 * We preallocate a memory for zlib in one buffer (200K-400K), this
470 * decreases a number of malloc() and free() calls and also probably
471 * decreases a number of syscalls (sbrk()/mmap() and so on).
472 * Besides we free the memory as soon as a gzipping will complete
473 * and do not wait while a whole response will be sent to a client.
475 * 8K is for zlib deflate_state, it takes
476 * *) 5816 bytes on i386 and sparc64 (32-bit mode)
477 * *) 5920 bytes on amd64 and sparc64
480 ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
485 ngx_http_gzip_filter_copy_recycled(ngx_http_gzip_ctx_t *ctx, ngx_chain_t *in)
487 size_t size, buffered;
489 ngx_chain_t *cl, **ll;
490 ngx_http_request_t *r;
494 r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
499 for (cl = ctx->in; cl; cl = cl->next) {
500 buffered += cl->buf->last - cl->buf->pos;
505 cl = ngx_alloc_chain_link(r->pool);
512 size = b->last - b->pos;
515 if (b->flush || b->last_buf) {
518 } else if (buffered > ctx->allocated / 2) {
521 * With default memory settings zlib starts to output gzipped data
522 * only after it has got about 90K, so it makes sense to allocate
523 * zlib memory (200-400K) only after we have enough data
524 * to compress. Although we copy recycled buffers, nevertheless
525 * for responses up to 120K this allows to allocate zlib memory,
526 * to compress and to output the response in one step
527 * using hot CPU cache.
533 if (ctx->buffering && size && b->recycled) {
535 buf = ngx_create_temp_buf(r->pool, size);
540 buf->last = ngx_cpymem(buf->pos, b->pos, size);
543 buf->last_buf = b->last_buf;
544 buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
559 return ctx->buffering ? NGX_OK : NGX_DONE;
564 ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
565 ngx_http_gzip_ctx_t *ctx)
568 ngx_http_gzip_conf_t *conf;
570 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
572 ctx->preallocated = ngx_palloc(r->pool, ctx->allocated);
573 if (ctx->preallocated == NULL) {
577 ctx->free_mem = ctx->preallocated;
579 ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
580 ctx->zstream.zfree = ngx_http_gzip_filter_free;
581 ctx->zstream.opaque = ctx;
583 rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
584 - ctx->wbits, ctx->memlevel, Z_DEFAULT_STRATEGY);
587 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
588 "deflateInit2() failed: %d", rc);
592 ctx->last_out = &ctx->out;
593 ctx->crc32 = crc32(0L, Z_NULL, 0);
594 ctx->flush = Z_NO_FLUSH;
601 ngx_http_gzip_filter_gzheader(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
605 static u_char gzheader[10] =
606 { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
608 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
615 b->last = b->pos + 10;
617 cl = ngx_alloc_chain_link(r->pool);
633 ngx_http_gzip_filter_add_data(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
635 if (ctx->zstream.avail_in || ctx->flush != Z_NO_FLUSH || ctx->redo) {
639 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
640 "gzip in: %p", ctx->in);
642 if (ctx->in == NULL) {
649 * to avoid CPU cache trashing we do not free() just quit buf,
650 * but postpone free()ing after zlib compressing and data output
653 ctx->copy_buf->next = ctx->copied;
654 ctx->copied = ctx->copy_buf;
655 ctx->copy_buf = NULL;
658 ctx->in_buf = ctx->in->buf;
660 if (ctx->in_buf->tag == (ngx_buf_tag_t) &ngx_http_gzip_filter_module) {
661 ctx->copy_buf = ctx->in;
664 ctx->in = ctx->in->next;
666 ctx->zstream.next_in = ctx->in_buf->pos;
667 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
669 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
670 "gzip in_buf:%p ni:%p ai:%ud",
672 ctx->zstream.next_in, ctx->zstream.avail_in);
674 if (ctx->in_buf->last_buf) {
675 ctx->flush = Z_FINISH;
677 } else if (ctx->in_buf->flush) {
678 ctx->flush = Z_SYNC_FLUSH;
681 if (ctx->zstream.avail_in) {
683 ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
684 ctx->zstream.avail_in);
686 } else if (ctx->flush == Z_NO_FLUSH) {
695 ngx_http_gzip_filter_get_buf(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
697 ngx_http_gzip_conf_t *conf;
699 if (ctx->zstream.avail_out) {
703 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
706 ctx->out_buf = ctx->free->buf;
707 ctx->free = ctx->free->next;
709 } else if (ctx->bufs < conf->bufs.num) {
711 ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size);
712 if (ctx->out_buf == NULL) {
716 ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
717 ctx->out_buf->recycled = 1;
725 ctx->zstream.next_out = ctx->out_buf->pos;
726 ctx->zstream.avail_out = conf->bufs.size;
733 ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
737 ngx_http_gzip_conf_t *conf;
739 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
740 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
741 ctx->zstream.next_in, ctx->zstream.next_out,
742 ctx->zstream.avail_in, ctx->zstream.avail_out,
743 ctx->flush, ctx->redo);
745 rc = deflate(&ctx->zstream, ctx->flush);
747 if (rc != Z_OK && rc != Z_STREAM_END) {
748 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
749 "deflate() failed: %d, %d", ctx->flush, rc);
753 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
754 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
755 ctx->zstream.next_in, ctx->zstream.next_out,
756 ctx->zstream.avail_in, ctx->zstream.avail_out,
759 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
760 "gzip in_buf:%p pos:%p",
761 ctx->in_buf, ctx->in_buf->pos);
763 if (ctx->zstream.next_in) {
764 ctx->in_buf->pos = ctx->zstream.next_in;
766 if (ctx->zstream.avail_in == 0) {
767 ctx->zstream.next_in = NULL;
771 ctx->out_buf->last = ctx->zstream.next_out;
773 if (ctx->zstream.avail_out == 0) {
775 /* zlib wants to output some more gzipped data */
777 cl = ngx_alloc_chain_link(r->pool);
782 cl->buf = ctx->out_buf;
785 ctx->last_out = &cl->next;
794 if (ctx->flush == Z_SYNC_FLUSH) {
796 ctx->zstream.avail_out = 0;
797 ctx->out_buf->flush = 1;
798 ctx->flush = Z_NO_FLUSH;
800 cl = ngx_alloc_chain_link(r->pool);
805 cl->buf = ctx->out_buf;
808 ctx->last_out = &cl->next;
813 if (rc == Z_STREAM_END) {
815 if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {
822 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
824 if (conf->no_buffer && ctx->in == NULL) {
826 cl = ngx_alloc_chain_link(r->pool);
831 cl->buf = ctx->out_buf;
834 ctx->last_out = &cl->next;
844 ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
845 ngx_http_gzip_ctx_t *ctx)
850 struct gztrailer *trailer;
852 ctx->zin = ctx->zstream.total_in;
853 ctx->zout = 10 + ctx->zstream.total_out + 8;
855 rc = deflateEnd(&ctx->zstream);
858 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
859 "deflateEnd() failed: %d", rc);
863 ngx_pfree(r->pool, ctx->preallocated);
865 cl = ngx_alloc_chain_link(r->pool);
870 cl->buf = ctx->out_buf;
873 ctx->last_out = &cl->next;
875 if (ctx->zstream.avail_out >= 8) {
876 trailer = (struct gztrailer *) ctx->out_buf->last;
877 ctx->out_buf->last += 8;
878 ctx->out_buf->last_buf = 1;
881 b = ngx_create_temp_buf(r->pool, 8);
888 cl = ngx_alloc_chain_link(r->pool);
896 ctx->last_out = &cl->next;
897 trailer = (struct gztrailer *) b->pos;
901 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
903 trailer->crc32 = ctx->crc32;
904 trailer->zlen = ctx->zin;
908 trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
909 trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
910 trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
911 trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
913 trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
914 trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
915 trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
916 trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
920 ctx->zstream.avail_in = 0;
921 ctx->zstream.avail_out = 0;
925 r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;
932 ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
934 ngx_http_gzip_ctx_t *ctx = opaque;
939 alloc = items * size;
941 if (alloc % 512 != 0 && alloc < 8192) {
944 * The zlib deflate_state allocation, it takes about 6K,
945 * we allocate 8K. Other allocations are divisible by 512.
951 if (alloc <= ctx->allocated) {
953 ctx->free_mem += alloc;
954 ctx->allocated -= alloc;
956 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
957 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
958 items, size, alloc, p);
963 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
964 "gzip filter failed to use preallocated memory: %ud of %ud",
965 items * size, ctx->allocated);
967 p = ngx_palloc(ctx->request->pool, items * size);
974 ngx_http_gzip_filter_free(void *opaque, void *address)
977 ngx_http_gzip_ctx_t *ctx = opaque;
979 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
980 "gzip free: %p", address);
986 ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
987 ngx_http_gzip_ctx_t *ctx)
991 for (cl = ctx->copied; cl; cl = cl->next) {
992 ngx_pfree(r->pool, cl->buf->start);
1000 ngx_http_gzip_add_variables(ngx_conf_t *cf)
1002 ngx_http_variable_t *var;
1004 var = ngx_http_add_variable(cf, &ngx_http_gzip_ratio, NGX_HTTP_VAR_NOHASH);
1009 var->get_handler = ngx_http_gzip_ratio_variable;
1016 ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
1017 ngx_http_variable_value_t *v, uintptr_t data)
1019 ngx_uint_t zint, zfrac;
1020 ngx_http_gzip_ctx_t *ctx;
1023 v->no_cacheable = 0;
1026 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
1028 if (ctx == NULL || ctx->zout == 0) {
1033 v->data = ngx_pnalloc(r->pool, NGX_INT32_LEN + 3);
1034 if (v->data == NULL) {
1038 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
1039 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
1041 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
1043 /* the rounding, e.g., 2.125 to 2.13 */
1053 v->len = ngx_sprintf(v->data, "%ui.%02ui", zint, zfrac) - v->data;
1060 ngx_http_gzip_create_conf(ngx_conf_t *cf)
1062 ngx_http_gzip_conf_t *conf;
1064 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t));
1066 return NGX_CONF_ERROR;
1070 * set by ngx_pcalloc():
1072 * conf->bufs.num = 0;
1073 * conf->types = { NULL };
1074 * conf->types_keys = NULL;
1077 conf->enable = NGX_CONF_UNSET;
1078 conf->no_buffer = NGX_CONF_UNSET;
1080 conf->level = NGX_CONF_UNSET;
1081 conf->wbits = (size_t) NGX_CONF_UNSET;
1082 conf->memlevel = (size_t) NGX_CONF_UNSET;
1083 conf->min_length = NGX_CONF_UNSET;
1090 ngx_http_gzip_merge_conf(ngx_conf_t *cf, void *parent, void *child)
1092 ngx_http_gzip_conf_t *prev = parent;
1093 ngx_http_gzip_conf_t *conf = child;
1095 ngx_conf_merge_value(conf->enable, prev->enable, 0);
1097 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs,
1098 (128 * 1024) / ngx_pagesize, ngx_pagesize);
1100 ngx_conf_merge_value(conf->level, prev->level, 1);
1101 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
1102 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
1104 ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
1105 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
1107 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,
1108 prev->types_keys, &prev->types,
1109 ngx_http_html_default_types)
1112 return NGX_CONF_ERROR;
1120 ngx_http_gzip_filter_init(ngx_conf_t *cf)
1122 ngx_http_next_header_filter = ngx_http_top_header_filter;
1123 ngx_http_top_header_filter = ngx_http_gzip_header_filter;
1125 ngx_http_next_body_filter = ngx_http_top_body_filter;
1126 ngx_http_top_body_filter = ngx_http_gzip_body_filter;
1133 ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data)
1137 size_t wbits, wsize;
1141 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
1152 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
1157 ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data)
1161 size_t memlevel, hsize;
1165 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
1176 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";