3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
22 size_t postpone_gzipping;
28 ngx_array_t *types_keys;
29 } ngx_http_gzip_conf_t;
37 ngx_chain_t **last_out;
40 ngx_chain_t *copy_buf;
65 ngx_http_request_t *request;
66 } ngx_http_gzip_ctx_t;
69 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
76 #else /* NGX_HAVE_BIG_ENDIAN || !NGX_HAVE_NONALIGNED */
86 static void ngx_http_gzip_filter_memory(ngx_http_request_t *r,
87 ngx_http_gzip_ctx_t *ctx);
88 static ngx_int_t ngx_http_gzip_filter_buffer(ngx_http_gzip_ctx_t *ctx,
90 static ngx_int_t ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
91 ngx_http_gzip_ctx_t *ctx);
92 static ngx_int_t ngx_http_gzip_filter_gzheader(ngx_http_request_t *r,
93 ngx_http_gzip_ctx_t *ctx);
94 static ngx_int_t ngx_http_gzip_filter_add_data(ngx_http_request_t *r,
95 ngx_http_gzip_ctx_t *ctx);
96 static ngx_int_t ngx_http_gzip_filter_get_buf(ngx_http_request_t *r,
97 ngx_http_gzip_ctx_t *ctx);
98 static ngx_int_t ngx_http_gzip_filter_deflate(ngx_http_request_t *r,
99 ngx_http_gzip_ctx_t *ctx);
100 static ngx_int_t ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
101 ngx_http_gzip_ctx_t *ctx);
103 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
105 static void ngx_http_gzip_filter_free(void *opaque, void *address);
106 static void ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
107 ngx_http_gzip_ctx_t *ctx);
109 static ngx_int_t ngx_http_gzip_add_variables(ngx_conf_t *cf);
110 static ngx_int_t ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
111 ngx_http_variable_value_t *v, uintptr_t data);
113 static ngx_int_t ngx_http_gzip_filter_init(ngx_conf_t *cf);
114 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf);
115 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
116 void *parent, void *child);
117 static char *ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data);
118 static char *ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data);
121 static ngx_conf_num_bounds_t ngx_http_gzip_comp_level_bounds = {
122 ngx_conf_check_num_bounds, 1, 9
125 static ngx_conf_post_handler_pt ngx_http_gzip_window_p = ngx_http_gzip_window;
126 static ngx_conf_post_handler_pt ngx_http_gzip_hash_p = ngx_http_gzip_hash;
129 static ngx_command_t ngx_http_gzip_filter_commands[] = {
131 { ngx_string("gzip"),
132 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
134 ngx_conf_set_flag_slot,
135 NGX_HTTP_LOC_CONF_OFFSET,
136 offsetof(ngx_http_gzip_conf_t, enable),
139 { ngx_string("gzip_buffers"),
140 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
141 ngx_conf_set_bufs_slot,
142 NGX_HTTP_LOC_CONF_OFFSET,
143 offsetof(ngx_http_gzip_conf_t, bufs),
146 { ngx_string("gzip_types"),
147 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
149 NGX_HTTP_LOC_CONF_OFFSET,
150 offsetof(ngx_http_gzip_conf_t, types_keys),
151 &ngx_http_html_default_types[0] },
153 { ngx_string("gzip_comp_level"),
154 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
155 ngx_conf_set_num_slot,
156 NGX_HTTP_LOC_CONF_OFFSET,
157 offsetof(ngx_http_gzip_conf_t, level),
158 &ngx_http_gzip_comp_level_bounds },
160 { ngx_string("gzip_window"),
161 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
162 ngx_conf_set_size_slot,
163 NGX_HTTP_LOC_CONF_OFFSET,
164 offsetof(ngx_http_gzip_conf_t, wbits),
165 &ngx_http_gzip_window_p },
167 { ngx_string("gzip_hash"),
168 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
169 ngx_conf_set_size_slot,
170 NGX_HTTP_LOC_CONF_OFFSET,
171 offsetof(ngx_http_gzip_conf_t, memlevel),
172 &ngx_http_gzip_hash_p },
174 { ngx_string("postpone_gzipping"),
175 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
176 ngx_conf_set_size_slot,
177 NGX_HTTP_LOC_CONF_OFFSET,
178 offsetof(ngx_http_gzip_conf_t, postpone_gzipping),
181 { ngx_string("gzip_no_buffer"),
182 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
183 ngx_conf_set_flag_slot,
184 NGX_HTTP_LOC_CONF_OFFSET,
185 offsetof(ngx_http_gzip_conf_t, no_buffer),
188 { ngx_string("gzip_min_length"),
189 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
190 ngx_conf_set_size_slot,
191 NGX_HTTP_LOC_CONF_OFFSET,
192 offsetof(ngx_http_gzip_conf_t, min_length),
199 static ngx_http_module_t ngx_http_gzip_filter_module_ctx = {
200 ngx_http_gzip_add_variables, /* preconfiguration */
201 ngx_http_gzip_filter_init, /* postconfiguration */
203 NULL, /* create main configuration */
204 NULL, /* init main configuration */
206 NULL, /* create server configuration */
207 NULL, /* merge server configuration */
209 ngx_http_gzip_create_conf, /* create location configuration */
210 ngx_http_gzip_merge_conf /* merge location configuration */
214 ngx_module_t ngx_http_gzip_filter_module = {
216 &ngx_http_gzip_filter_module_ctx, /* module context */
217 ngx_http_gzip_filter_commands, /* module directives */
218 NGX_HTTP_MODULE, /* module type */
219 NULL, /* init master */
220 NULL, /* init module */
221 NULL, /* init process */
222 NULL, /* init thread */
223 NULL, /* exit thread */
224 NULL, /* exit process */
225 NULL, /* exit master */
226 NGX_MODULE_V1_PADDING
230 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");
232 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
233 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
237 ngx_http_gzip_header_filter(ngx_http_request_t *r)
240 ngx_http_gzip_ctx_t *ctx;
241 ngx_http_gzip_conf_t *conf;
243 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
246 || (r->headers_out.status != NGX_HTTP_OK
247 && r->headers_out.status != NGX_HTTP_FORBIDDEN
248 && r->headers_out.status != NGX_HTTP_NOT_FOUND)
250 || (r->headers_out.content_encoding
251 && r->headers_out.content_encoding->value.len)
252 || (r->headers_out.content_length_n != -1
253 && r->headers_out.content_length_n < conf->min_length)
254 || ngx_http_test_content_type(r, &conf->types) == NULL
255 || ngx_http_gzip_ok(r) != NGX_OK)
257 return ngx_http_next_header_filter(r);
260 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t));
265 ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module);
268 ctx->buffering = (conf->postpone_gzipping != 0);
270 ngx_http_gzip_filter_memory(r, ctx);
272 h = ngx_list_push(&r->headers_out.headers);
278 h->key.len = sizeof("Content-Encoding") - 1;
279 h->key.data = (u_char *) "Content-Encoding";
280 h->value.len = sizeof("gzip") - 1;
281 h->value.data = (u_char *) "gzip";
283 r->headers_out.content_encoding = h;
285 r->main_filter_need_in_memory = 1;
287 ngx_http_clear_content_length(r);
288 ngx_http_clear_accept_ranges(r);
290 return ngx_http_next_header_filter(r);
295 ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
299 ngx_http_gzip_ctx_t *ctx;
301 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
303 if (ctx == NULL || ctx->done) {
304 return ngx_http_next_body_filter(r, in);
307 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
310 if (ctx->buffering) {
313 * With default memory settings zlib starts to output gzipped data
314 * only after it has got about 90K, so it makes sense to allocate
315 * zlib memory (200-400K) only after we have enough data to compress.
316 * Although we copy buffers, nevertheless for not big responses
317 * this allows to allocate zlib memory, to compress and to output
318 * the response in one step using hot CPU cache.
322 switch (ngx_http_gzip_filter_buffer(ctx, in)) {
331 default: /* NGX_ERROR */
340 if (ctx->preallocated == NULL) {
341 if (ngx_http_gzip_filter_deflate_start(r, ctx) != NGX_OK) {
347 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) {
354 /* flush busy buffers */
356 if (ngx_http_next_body_filter(r, NULL) == NGX_ERROR) {
362 ngx_chain_update_chains(&ctx->free, &ctx->busy, &cl,
363 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
369 /* cycle while we can write to a client */
373 /* cycle while there is data to feed zlib and ... */
375 rc = ngx_http_gzip_filter_add_data(r, ctx);
377 if (rc == NGX_DECLINED) {
381 if (rc == NGX_AGAIN) {
386 /* ... there are buffers to write zlib output */
388 rc = ngx_http_gzip_filter_get_buf(r, ctx);
390 if (rc == NGX_DECLINED) {
394 if (rc == NGX_ERROR) {
399 rc = ngx_http_gzip_filter_deflate(r, ctx);
405 if (rc == NGX_ERROR) {
409 /* rc == NGX_AGAIN */
412 if (ctx->out == NULL) {
413 ngx_http_gzip_filter_free_copy_buf(r, ctx);
415 return ctx->busy ? NGX_AGAIN : NGX_OK;
418 if (!ctx->gzheader) {
419 if (ngx_http_gzip_filter_gzheader(r, ctx) != NGX_OK) {
424 rc = ngx_http_next_body_filter(r, ctx->out);
426 if (rc == NGX_ERROR) {
430 ngx_http_gzip_filter_free_copy_buf(r, ctx);
432 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out,
433 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
434 ctx->last_out = &ctx->out;
449 if (ctx->preallocated) {
450 deflateEnd(&ctx->zstream);
452 ngx_pfree(r->pool, ctx->preallocated);
455 ngx_http_gzip_filter_free_copy_buf(r, ctx);
462 ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
465 ngx_http_gzip_conf_t *conf;
467 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
470 memlevel = conf->memlevel;
472 if (r->headers_out.content_length_n > 0) {
474 /* the actual zlib window size is smaller by 262 bytes */
476 while (r->headers_out.content_length_n < ((1 << (wbits - 1)) - 262)) {
483 ctx->memlevel = memlevel;
486 * We preallocate a memory for zlib in one buffer (200K-400K), this
487 * decreases a number of malloc() and free() calls and also probably
488 * decreases a number of syscalls (sbrk()/mmap() and so on).
489 * Besides we free the memory as soon as a gzipping will complete
490 * and do not wait while a whole response will be sent to a client.
492 * 8K is for zlib deflate_state, it takes
493 * *) 5816 bytes on i386 and sparc64 (32-bit mode)
494 * *) 5920 bytes on amd64 and sparc64
497 ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
502 ngx_http_gzip_filter_buffer(ngx_http_gzip_ctx_t *ctx, ngx_chain_t *in)
504 size_t size, buffered;
506 ngx_chain_t *cl, **ll;
507 ngx_http_request_t *r;
508 ngx_http_gzip_conf_t *conf;
512 r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
517 for (cl = ctx->in; cl; cl = cl->next) {
518 buffered += cl->buf->last - cl->buf->pos;
522 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
525 cl = ngx_alloc_chain_link(r->pool);
532 size = b->last - b->pos;
535 if (b->flush || b->last_buf || buffered > conf->postpone_gzipping) {
539 if (ctx->buffering && size) {
541 buf = ngx_create_temp_buf(r->pool, size);
546 buf->last = ngx_cpymem(buf->pos, b->pos, size);
549 buf->last_buf = b->last_buf;
550 buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
565 return ctx->buffering ? NGX_OK : NGX_DONE;
570 ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
571 ngx_http_gzip_ctx_t *ctx)
574 ngx_http_gzip_conf_t *conf;
576 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
578 ctx->preallocated = ngx_palloc(r->pool, ctx->allocated);
579 if (ctx->preallocated == NULL) {
583 ctx->free_mem = ctx->preallocated;
585 ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
586 ctx->zstream.zfree = ngx_http_gzip_filter_free;
587 ctx->zstream.opaque = ctx;
589 rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
590 - ctx->wbits, ctx->memlevel, Z_DEFAULT_STRATEGY);
593 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
594 "deflateInit2() failed: %d", rc);
598 ctx->last_out = &ctx->out;
599 ctx->crc32 = crc32(0L, Z_NULL, 0);
600 ctx->flush = Z_NO_FLUSH;
607 ngx_http_gzip_filter_gzheader(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
611 static u_char gzheader[10] =
612 { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
614 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
621 b->last = b->pos + 10;
623 cl = ngx_alloc_chain_link(r->pool);
639 ngx_http_gzip_filter_add_data(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
641 if (ctx->zstream.avail_in || ctx->flush != Z_NO_FLUSH || ctx->redo) {
645 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
646 "gzip in: %p", ctx->in);
648 if (ctx->in == NULL) {
655 * to avoid CPU cache trashing we do not free() just quit buf,
656 * but postpone free()ing after zlib compressing and data output
659 ctx->copy_buf->next = ctx->copied;
660 ctx->copied = ctx->copy_buf;
661 ctx->copy_buf = NULL;
664 ctx->in_buf = ctx->in->buf;
666 if (ctx->in_buf->tag == (ngx_buf_tag_t) &ngx_http_gzip_filter_module) {
667 ctx->copy_buf = ctx->in;
670 ctx->in = ctx->in->next;
672 ctx->zstream.next_in = ctx->in_buf->pos;
673 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
675 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
676 "gzip in_buf:%p ni:%p ai:%ud",
678 ctx->zstream.next_in, ctx->zstream.avail_in);
680 if (ctx->in_buf->last_buf) {
681 ctx->flush = Z_FINISH;
683 } else if (ctx->in_buf->flush) {
684 ctx->flush = Z_SYNC_FLUSH;
687 if (ctx->zstream.avail_in) {
689 ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
690 ctx->zstream.avail_in);
692 } else if (ctx->flush == Z_NO_FLUSH) {
701 ngx_http_gzip_filter_get_buf(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
703 ngx_http_gzip_conf_t *conf;
705 if (ctx->zstream.avail_out) {
709 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
712 ctx->out_buf = ctx->free->buf;
713 ctx->free = ctx->free->next;
715 } else if (ctx->bufs < conf->bufs.num) {
717 ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size);
718 if (ctx->out_buf == NULL) {
722 ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
723 ctx->out_buf->recycled = 1;
731 ctx->zstream.next_out = ctx->out_buf->pos;
732 ctx->zstream.avail_out = conf->bufs.size;
739 ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
743 ngx_http_gzip_conf_t *conf;
745 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
746 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
747 ctx->zstream.next_in, ctx->zstream.next_out,
748 ctx->zstream.avail_in, ctx->zstream.avail_out,
749 ctx->flush, ctx->redo);
751 rc = deflate(&ctx->zstream, ctx->flush);
753 if (rc != Z_OK && rc != Z_STREAM_END) {
754 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
755 "deflate() failed: %d, %d", ctx->flush, rc);
759 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
760 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
761 ctx->zstream.next_in, ctx->zstream.next_out,
762 ctx->zstream.avail_in, ctx->zstream.avail_out,
765 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
766 "gzip in_buf:%p pos:%p",
767 ctx->in_buf, ctx->in_buf->pos);
769 if (ctx->zstream.next_in) {
770 ctx->in_buf->pos = ctx->zstream.next_in;
772 if (ctx->zstream.avail_in == 0) {
773 ctx->zstream.next_in = NULL;
777 ctx->out_buf->last = ctx->zstream.next_out;
779 if (ctx->zstream.avail_out == 0) {
781 /* zlib wants to output some more gzipped data */
783 cl = ngx_alloc_chain_link(r->pool);
788 cl->buf = ctx->out_buf;
791 ctx->last_out = &cl->next;
800 if (ctx->flush == Z_SYNC_FLUSH) {
802 ctx->zstream.avail_out = 0;
803 ctx->out_buf->flush = 1;
804 ctx->flush = Z_NO_FLUSH;
806 cl = ngx_alloc_chain_link(r->pool);
811 cl->buf = ctx->out_buf;
814 ctx->last_out = &cl->next;
819 if (rc == Z_STREAM_END) {
821 if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {
828 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
830 if (conf->no_buffer && ctx->in == NULL) {
832 cl = ngx_alloc_chain_link(r->pool);
837 cl->buf = ctx->out_buf;
840 ctx->last_out = &cl->next;
850 ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
851 ngx_http_gzip_ctx_t *ctx)
856 struct gztrailer *trailer;
858 ctx->zin = ctx->zstream.total_in;
859 ctx->zout = 10 + ctx->zstream.total_out + 8;
861 rc = deflateEnd(&ctx->zstream);
864 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
865 "deflateEnd() failed: %d", rc);
869 ngx_pfree(r->pool, ctx->preallocated);
871 cl = ngx_alloc_chain_link(r->pool);
876 cl->buf = ctx->out_buf;
879 ctx->last_out = &cl->next;
881 if (ctx->zstream.avail_out >= 8) {
882 trailer = (struct gztrailer *) ctx->out_buf->last;
883 ctx->out_buf->last += 8;
884 ctx->out_buf->last_buf = 1;
887 b = ngx_create_temp_buf(r->pool, 8);
894 cl = ngx_alloc_chain_link(r->pool);
902 ctx->last_out = &cl->next;
903 trailer = (struct gztrailer *) b->pos;
907 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
909 trailer->crc32 = ctx->crc32;
910 trailer->zlen = ctx->zin;
914 trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
915 trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
916 trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
917 trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
919 trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
920 trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
921 trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
922 trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
926 ctx->zstream.avail_in = 0;
927 ctx->zstream.avail_out = 0;
931 r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;
938 ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
940 ngx_http_gzip_ctx_t *ctx = opaque;
945 alloc = items * size;
947 if (alloc % 512 != 0 && alloc < 8192) {
950 * The zlib deflate_state allocation, it takes about 6K,
951 * we allocate 8K. Other allocations are divisible by 512.
957 if (alloc <= ctx->allocated) {
959 ctx->free_mem += alloc;
960 ctx->allocated -= alloc;
962 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
963 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
964 items, size, alloc, p);
969 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
970 "gzip filter failed to use preallocated memory: %ud of %ud",
971 items * size, ctx->allocated);
973 p = ngx_palloc(ctx->request->pool, items * size);
980 ngx_http_gzip_filter_free(void *opaque, void *address)
983 ngx_http_gzip_ctx_t *ctx = opaque;
985 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
986 "gzip free: %p", address);
992 ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
993 ngx_http_gzip_ctx_t *ctx)
997 for (cl = ctx->copied; cl; cl = cl->next) {
998 ngx_pfree(r->pool, cl->buf->start);
1006 ngx_http_gzip_add_variables(ngx_conf_t *cf)
1008 ngx_http_variable_t *var;
1010 var = ngx_http_add_variable(cf, &ngx_http_gzip_ratio, NGX_HTTP_VAR_NOHASH);
1015 var->get_handler = ngx_http_gzip_ratio_variable;
1022 ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
1023 ngx_http_variable_value_t *v, uintptr_t data)
1025 ngx_uint_t zint, zfrac;
1026 ngx_http_gzip_ctx_t *ctx;
1029 v->no_cacheable = 0;
1032 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
1034 if (ctx == NULL || ctx->zout == 0) {
1039 v->data = ngx_pnalloc(r->pool, NGX_INT32_LEN + 3);
1040 if (v->data == NULL) {
1044 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
1045 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
1047 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
1049 /* the rounding, e.g., 2.125 to 2.13 */
1059 v->len = ngx_sprintf(v->data, "%ui.%02ui", zint, zfrac) - v->data;
1066 ngx_http_gzip_create_conf(ngx_conf_t *cf)
1068 ngx_http_gzip_conf_t *conf;
1070 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t));
1072 return NGX_CONF_ERROR;
1076 * set by ngx_pcalloc():
1078 * conf->bufs.num = 0;
1079 * conf->types = { NULL };
1080 * conf->types_keys = NULL;
1083 conf->enable = NGX_CONF_UNSET;
1084 conf->no_buffer = NGX_CONF_UNSET;
1086 conf->postpone_gzipping = NGX_CONF_UNSET_SIZE;
1087 conf->level = NGX_CONF_UNSET;
1088 conf->wbits = NGX_CONF_UNSET_SIZE;
1089 conf->memlevel = NGX_CONF_UNSET_SIZE;
1090 conf->min_length = NGX_CONF_UNSET;
1097 ngx_http_gzip_merge_conf(ngx_conf_t *cf, void *parent, void *child)
1099 ngx_http_gzip_conf_t *prev = parent;
1100 ngx_http_gzip_conf_t *conf = child;
1102 ngx_conf_merge_value(conf->enable, prev->enable, 0);
1103 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
1105 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs,
1106 (128 * 1024) / ngx_pagesize, ngx_pagesize);
1108 ngx_conf_merge_size_value(conf->postpone_gzipping, prev->postpone_gzipping,
1110 ngx_conf_merge_value(conf->level, prev->level, 1);
1111 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
1112 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
1114 ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
1116 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,
1117 prev->types_keys, &prev->types,
1118 ngx_http_html_default_types)
1121 return NGX_CONF_ERROR;
1129 ngx_http_gzip_filter_init(ngx_conf_t *cf)
1131 ngx_http_next_header_filter = ngx_http_top_header_filter;
1132 ngx_http_top_header_filter = ngx_http_gzip_header_filter;
1134 ngx_http_next_body_filter = ngx_http_top_body_filter;
1135 ngx_http_top_body_filter = ngx_http_gzip_body_filter;
1142 ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data)
1146 size_t wbits, wsize;
1150 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
1161 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
1166 ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data)
1170 size_t memlevel, hsize;
1174 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
1185 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";