50
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
50
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12 #include <zlib.h>
|
|
13
|
|
14
|
|
15 typedef struct {
|
|
16 ngx_flag_t enable;
|
|
17 ngx_flag_t no_buffer;
|
|
18
|
394
|
19 ngx_hash_t types;
|
50
|
20
|
|
21 ngx_bufs_t bufs;
|
|
22
|
454
|
23 size_t postpone_gzipping;
|
138
|
24 ngx_int_t level;
|
50
|
25 size_t wbits;
|
|
26 size_t memlevel;
|
|
27 ssize_t min_length;
|
394
|
28
|
|
29 ngx_array_t *types_keys;
|
50
|
30 } ngx_http_gzip_conf_t;
|
|
31
|
|
32
|
|
33 typedef struct {
|
|
34 ngx_chain_t *in;
|
|
35 ngx_chain_t *free;
|
|
36 ngx_chain_t *busy;
|
|
37 ngx_chain_t *out;
|
|
38 ngx_chain_t **last_out;
|
432
|
39
|
|
40 ngx_chain_t *copied;
|
|
41 ngx_chain_t *copy_buf;
|
|
42
|
50
|
43 ngx_buf_t *in_buf;
|
|
44 ngx_buf_t *out_buf;
|
|
45 ngx_int_t bufs;
|
|
46
|
|
47 void *preallocated;
|
|
48 char *free_mem;
|
|
49 ngx_uint_t allocated;
|
|
50
|
432
|
51 int wbits;
|
|
52 int memlevel;
|
|
53
|
50
|
54 unsigned flush:4;
|
|
55 unsigned redo:1;
|
|
56 unsigned done:1;
|
416
|
57 unsigned nomem:1;
|
|
58 unsigned gzheader:1;
|
432
|
59 unsigned buffering:1;
|
50
|
60
|
|
61 size_t zin;
|
|
62 size_t zout;
|
|
63
|
|
64 uint32_t crc32;
|
|
65 z_stream zstream;
|
|
66 ngx_http_request_t *request;
|
|
67 } ngx_http_gzip_ctx_t;
|
|
68
|
|
69
|
416
|
70 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
|
|
71
|
|
72 struct gztrailer {
|
|
73 uint32_t crc32;
|
|
74 uint32_t zlen;
|
|
75 };
|
|
76
|
|
77 #else /* NGX_HAVE_BIG_ENDIAN || !NGX_HAVE_NONALIGNED */
|
|
78
|
|
79 struct gztrailer {
|
|
80 u_char crc32[4];
|
|
81 u_char zlen[4];
|
|
82 };
|
|
83
|
|
84 #endif
|
|
85
|
|
86
|
432
|
87 static void ngx_http_gzip_filter_memory(ngx_http_request_t *r,
|
|
88 ngx_http_gzip_ctx_t *ctx);
|
454
|
89 static ngx_int_t ngx_http_gzip_filter_buffer(ngx_http_gzip_ctx_t *ctx,
|
432
|
90 ngx_chain_t *in);
|
416
|
91 static ngx_int_t ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
|
|
92 ngx_http_gzip_ctx_t *ctx);
|
|
93 static ngx_int_t ngx_http_gzip_filter_gzheader(ngx_http_request_t *r,
|
|
94 ngx_http_gzip_ctx_t *ctx);
|
|
95 static ngx_int_t ngx_http_gzip_filter_add_data(ngx_http_request_t *r,
|
|
96 ngx_http_gzip_ctx_t *ctx);
|
|
97 static ngx_int_t ngx_http_gzip_filter_get_buf(ngx_http_request_t *r,
|
|
98 ngx_http_gzip_ctx_t *ctx);
|
|
99 static ngx_int_t ngx_http_gzip_filter_deflate(ngx_http_request_t *r,
|
|
100 ngx_http_gzip_ctx_t *ctx);
|
|
101 static ngx_int_t ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
|
|
102 ngx_http_gzip_ctx_t *ctx);
|
|
103
|
50
|
104 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
|
|
105 u_int size);
|
|
106 static void ngx_http_gzip_filter_free(void *opaque, void *address);
|
432
|
107 static void ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
|
|
108 ngx_http_gzip_ctx_t *ctx);
|
50
|
109
|
122
|
110 static ngx_int_t ngx_http_gzip_add_variables(ngx_conf_t *cf);
|
|
111 static ngx_int_t ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
|
|
112 ngx_http_variable_value_t *v, uintptr_t data);
|
50
|
113
|
230
|
114 static ngx_int_t ngx_http_gzip_filter_init(ngx_conf_t *cf);
|
50
|
115 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf);
|
|
116 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
|
|
117 void *parent, void *child);
|
76
|
118 static char *ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data);
|
|
119 static char *ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data);
|
50
|
120
|
|
121
|
|
122 static ngx_conf_num_bounds_t ngx_http_gzip_comp_level_bounds = {
|
|
123 ngx_conf_check_num_bounds, 1, 9
|
|
124 };
|
|
125
|
76
|
126 static ngx_conf_post_handler_pt ngx_http_gzip_window_p = ngx_http_gzip_window;
|
|
127 static ngx_conf_post_handler_pt ngx_http_gzip_hash_p = ngx_http_gzip_hash;
|
50
|
128
|
|
129
|
|
130 static ngx_command_t ngx_http_gzip_filter_commands[] = {
|
|
131
|
|
132 { ngx_string("gzip"),
|
|
133 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
134 |NGX_CONF_FLAG,
|
|
135 ngx_conf_set_flag_slot,
|
|
136 NGX_HTTP_LOC_CONF_OFFSET,
|
|
137 offsetof(ngx_http_gzip_conf_t, enable),
|
|
138 NULL },
|
|
139
|
|
140 { ngx_string("gzip_buffers"),
|
|
141 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
142 ngx_conf_set_bufs_slot,
|
|
143 NGX_HTTP_LOC_CONF_OFFSET,
|
|
144 offsetof(ngx_http_gzip_conf_t, bufs),
|
|
145 NULL },
|
|
146
|
|
147 { ngx_string("gzip_types"),
|
|
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
394
|
149 ngx_http_types_slot,
|
50
|
150 NGX_HTTP_LOC_CONF_OFFSET,
|
394
|
151 offsetof(ngx_http_gzip_conf_t, types_keys),
|
|
152 &ngx_http_html_default_types[0] },
|
50
|
153
|
|
154 { ngx_string("gzip_comp_level"),
|
|
155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
156 ngx_conf_set_num_slot,
|
|
157 NGX_HTTP_LOC_CONF_OFFSET,
|
|
158 offsetof(ngx_http_gzip_conf_t, level),
|
|
159 &ngx_http_gzip_comp_level_bounds },
|
|
160
|
|
161 { ngx_string("gzip_window"),
|
|
162 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
163 ngx_conf_set_size_slot,
|
|
164 NGX_HTTP_LOC_CONF_OFFSET,
|
|
165 offsetof(ngx_http_gzip_conf_t, wbits),
|
76
|
166 &ngx_http_gzip_window_p },
|
50
|
167
|
|
168 { ngx_string("gzip_hash"),
|
|
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
170 ngx_conf_set_size_slot,
|
|
171 NGX_HTTP_LOC_CONF_OFFSET,
|
|
172 offsetof(ngx_http_gzip_conf_t, memlevel),
|
76
|
173 &ngx_http_gzip_hash_p },
|
50
|
174
|
454
|
175 { ngx_string("postpone_gzipping"),
|
|
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
177 ngx_conf_set_size_slot,
|
|
178 NGX_HTTP_LOC_CONF_OFFSET,
|
|
179 offsetof(ngx_http_gzip_conf_t, postpone_gzipping),
|
|
180 NULL },
|
|
181
|
50
|
182 { ngx_string("gzip_no_buffer"),
|
|
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
184 ngx_conf_set_flag_slot,
|
|
185 NGX_HTTP_LOC_CONF_OFFSET,
|
|
186 offsetof(ngx_http_gzip_conf_t, no_buffer),
|
|
187 NULL },
|
|
188
|
|
189 { ngx_string("gzip_min_length"),
|
|
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
191 ngx_conf_set_size_slot,
|
|
192 NGX_HTTP_LOC_CONF_OFFSET,
|
|
193 offsetof(ngx_http_gzip_conf_t, min_length),
|
|
194 NULL },
|
|
195
|
|
196 ngx_null_command
|
|
197 };
|
|
198
|
|
199
|
|
200 static ngx_http_module_t ngx_http_gzip_filter_module_ctx = {
|
122
|
201 ngx_http_gzip_add_variables, /* preconfiguration */
|
230
|
202 ngx_http_gzip_filter_init, /* postconfiguration */
|
50
|
203
|
|
204 NULL, /* create main configuration */
|
|
205 NULL, /* init main configuration */
|
|
206
|
|
207 NULL, /* create server configuration */
|
|
208 NULL, /* merge server configuration */
|
|
209
|
|
210 ngx_http_gzip_create_conf, /* create location configuration */
|
|
211 ngx_http_gzip_merge_conf /* merge location configuration */
|
|
212 };
|
|
213
|
|
214
|
|
215 ngx_module_t ngx_http_gzip_filter_module = {
|
58
|
216 NGX_MODULE_V1,
|
50
|
217 &ngx_http_gzip_filter_module_ctx, /* module context */
|
|
218 ngx_http_gzip_filter_commands, /* module directives */
|
|
219 NGX_HTTP_MODULE, /* module type */
|
90
|
220 NULL, /* init master */
|
230
|
221 NULL, /* init module */
|
90
|
222 NULL, /* init process */
|
|
223 NULL, /* init thread */
|
|
224 NULL, /* exit thread */
|
|
225 NULL, /* exit process */
|
|
226 NULL, /* exit master */
|
|
227 NGX_MODULE_V1_PADDING
|
50
|
228 };
|
|
229
|
|
230
|
122
|
231 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");
|
58
|
232
|
50
|
233 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
234 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
|
|
235
|
|
236
|
|
237 static ngx_int_t
|
|
238 ngx_http_gzip_header_filter(ngx_http_request_t *r)
|
|
239 {
|
382
|
240 ngx_table_elt_t *h;
|
|
241 ngx_http_gzip_ctx_t *ctx;
|
|
242 ngx_http_gzip_conf_t *conf;
|
50
|
243
|
|
244 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
245
|
|
246 if (!conf->enable
|
|
247 || (r->headers_out.status != NGX_HTTP_OK
|
|
248 && r->headers_out.status != NGX_HTTP_FORBIDDEN
|
|
249 && 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)
|
556
|
254 || ngx_http_test_content_type(r, &conf->types) == NULL
|
|
255 || r->header_only)
|
50
|
256 {
|
|
257 return ngx_http_next_header_filter(r);
|
|
258 }
|
|
259
|
546
|
260 r->gzip_vary = 1;
|
|
261
|
602
|
262 #if (NGX_HTTP_DEGRADATION)
|
|
263 {
|
|
264 ngx_http_core_loc_conf_t *clcf;
|
|
265
|
|
266 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
267
|
|
268 if (clcf->gzip_disable_degradation && ngx_http_degraded(r)) {
|
|
269 return ngx_http_next_header_filter(r);
|
|
270 }
|
|
271 }
|
|
272 #endif
|
|
273
|
546
|
274 if (!r->gzip_tested) {
|
|
275 if (ngx_http_gzip_ok(r) != NGX_OK) {
|
|
276 return ngx_http_next_header_filter(r);
|
|
277 }
|
|
278
|
|
279 } else if (!r->gzip_ok) {
|
|
280 return ngx_http_next_header_filter(r);
|
|
281 }
|
|
282
|
50
|
283 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t));
|
|
284 if (ctx == NULL) {
|
|
285 return NGX_ERROR;
|
|
286 }
|
|
287
|
|
288 ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module);
|
|
289
|
|
290 ctx->request = r;
|
454
|
291 ctx->buffering = (conf->postpone_gzipping != 0);
|
432
|
292
|
|
293 ngx_http_gzip_filter_memory(r, ctx);
|
50
|
294
|
358
|
295 h = ngx_list_push(&r->headers_out.headers);
|
|
296 if (h == NULL) {
|
50
|
297 return NGX_ERROR;
|
|
298 }
|
|
299
|
358
|
300 h->hash = 1;
|
570
|
301 ngx_str_set(&h->key, "Content-Encoding");
|
|
302 ngx_str_set(&h->value, "gzip");
|
358
|
303 r->headers_out.content_encoding = h;
|
|
304
|
62
|
305 r->main_filter_need_in_memory = 1;
|
50
|
306
|
126
|
307 ngx_http_clear_content_length(r);
|
|
308 ngx_http_clear_accept_ranges(r);
|
680
|
309 ngx_http_clear_etag(r);
|
126
|
310
|
50
|
311 return ngx_http_next_header_filter(r);
|
|
312 }
|
|
313
|
|
314
|
|
315 static ngx_int_t
|
|
316 ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
317 {
|
416
|
318 int rc;
|
|
319 ngx_chain_t *cl;
|
|
320 ngx_http_gzip_ctx_t *ctx;
|
50
|
321
|
|
322 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
323
|
694
|
324 if (ctx == NULL || ctx->done || r->header_only) {
|
50
|
325 return ngx_http_next_body_filter(r, in);
|
|
326 }
|
|
327
|
432
|
328 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
329 "http gzip filter");
|
|
330
|
|
331 if (ctx->buffering) {
|
|
332
|
454
|
333 /*
|
|
334 * With default memory settings zlib starts to output gzipped data
|
|
335 * only after it has got about 90K, so it makes sense to allocate
|
|
336 * zlib memory (200-400K) only after we have enough data to compress.
|
|
337 * Although we copy buffers, nevertheless for not big responses
|
|
338 * this allows to allocate zlib memory, to compress and to output
|
|
339 * the response in one step using hot CPU cache.
|
|
340 */
|
|
341
|
432
|
342 if (in) {
|
454
|
343 switch (ngx_http_gzip_filter_buffer(ctx, in)) {
|
432
|
344
|
440
|
345 case NGX_OK:
|
|
346 return NGX_OK;
|
432
|
347
|
440
|
348 case NGX_DONE:
|
|
349 in = NULL;
|
|
350 break;
|
432
|
351
|
440
|
352 default: /* NGX_ERROR */
|
|
353 goto failed;
|
|
354 }
|
432
|
355
|
|
356 } else {
|
|
357 ctx->buffering = 0;
|
|
358 }
|
|
359 }
|
|
360
|
50
|
361 if (ctx->preallocated == NULL) {
|
416
|
362 if (ngx_http_gzip_filter_deflate_start(r, ctx) != NGX_OK) {
|
|
363 goto failed;
|
50
|
364 }
|
|
365 }
|
|
366
|
|
367 if (in) {
|
454
|
368 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) {
|
416
|
369 goto failed;
|
50
|
370 }
|
|
371 }
|
|
372
|
416
|
373 if (ctx->nomem) {
|
|
374
|
|
375 /* flush busy buffers */
|
|
376
|
|
377 if (ngx_http_next_body_filter(r, NULL) == NGX_ERROR) {
|
|
378 goto failed;
|
|
379 }
|
|
380
|
|
381 cl = NULL;
|
|
382
|
640
|
383 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &cl,
|
416
|
384 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
|
|
385 ctx->nomem = 0;
|
|
386 }
|
50
|
387
|
|
388 for ( ;; ) {
|
|
389
|
416
|
390 /* cycle while we can write to a client */
|
|
391
|
50
|
392 for ( ;; ) {
|
|
393
|
416
|
394 /* cycle while there is data to feed zlib and ... */
|
50
|
395
|
416
|
396 rc = ngx_http_gzip_filter_add_data(r, ctx);
|
50
|
397
|
416
|
398 if (rc == NGX_DECLINED) {
|
|
399 break;
|
|
400 }
|
50
|
401
|
416
|
402 if (rc == NGX_AGAIN) {
|
|
403 continue;
|
50
|
404 }
|
|
405
|
|
406
|
416
|
407 /* ... there are buffers to write zlib output */
|
50
|
408
|
416
|
409 rc = ngx_http_gzip_filter_get_buf(r, ctx);
|
50
|
410
|
416
|
411 if (rc == NGX_DECLINED) {
|
|
412 break;
|
50
|
413 }
|
|
414
|
416
|
415 if (rc == NGX_ERROR) {
|
|
416 goto failed;
|
50
|
417 }
|
|
418
|
|
419
|
416
|
420 rc = ngx_http_gzip_filter_deflate(r, ctx);
|
50
|
421
|
416
|
422 if (rc == NGX_OK) {
|
50
|
423 break;
|
|
424 }
|
|
425
|
416
|
426 if (rc == NGX_ERROR) {
|
|
427 goto failed;
|
50
|
428 }
|
|
429
|
416
|
430 /* rc == NGX_AGAIN */
|
|
431 }
|
50
|
432
|
416
|
433 if (ctx->out == NULL) {
|
432
|
434 ngx_http_gzip_filter_free_copy_buf(r, ctx);
|
|
435
|
416
|
436 return ctx->busy ? NGX_AGAIN : NGX_OK;
|
|
437 }
|
50
|
438
|
416
|
439 if (!ctx->gzheader) {
|
|
440 if (ngx_http_gzip_filter_gzheader(r, ctx) != NGX_OK) {
|
|
441 goto failed;
|
50
|
442 }
|
|
443 }
|
|
444
|
416
|
445 rc = ngx_http_next_body_filter(r, ctx->out);
|
50
|
446
|
416
|
447 if (rc == NGX_ERROR) {
|
|
448 goto failed;
|
50
|
449 }
|
|
450
|
432
|
451 ngx_http_gzip_filter_free_copy_buf(r, ctx);
|
|
452
|
640
|
453 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &ctx->out,
|
50
|
454 (ngx_buf_tag_t) &ngx_http_gzip_filter_module);
|
|
455 ctx->last_out = &ctx->out;
|
|
456
|
416
|
457 ctx->nomem = 0;
|
|
458
|
50
|
459 if (ctx->done) {
|
416
|
460 return rc;
|
|
461 }
|
|
462 }
|
|
463
|
|
464 /* unreachable */
|
|
465
|
|
466 failed:
|
|
467
|
|
468 ctx->done = 1;
|
|
469
|
|
470 if (ctx->preallocated) {
|
|
471 deflateEnd(&ctx->zstream);
|
|
472
|
|
473 ngx_pfree(r->pool, ctx->preallocated);
|
|
474 }
|
|
475
|
432
|
476 ngx_http_gzip_filter_free_copy_buf(r, ctx);
|
|
477
|
416
|
478 return NGX_ERROR;
|
|
479 }
|
|
480
|
|
481
|
432
|
482 static void
|
|
483 ngx_http_gzip_filter_memory(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
|
416
|
484 {
|
432
|
485 int wbits, memlevel;
|
416
|
486 ngx_http_gzip_conf_t *conf;
|
|
487
|
|
488 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
489
|
|
490 wbits = conf->wbits;
|
|
491 memlevel = conf->memlevel;
|
|
492
|
432
|
493 if (r->headers_out.content_length_n > 0) {
|
416
|
494
|
|
495 /* the actual zlib window size is smaller by 262 bytes */
|
|
496
|
432
|
497 while (r->headers_out.content_length_n < ((1 << (wbits - 1)) - 262)) {
|
416
|
498 wbits--;
|
|
499 memlevel--;
|
50
|
500 }
|
694
|
501
|
|
502 if (memlevel < 1) {
|
|
503 memlevel = 1;
|
|
504 }
|
50
|
505 }
|
416
|
506
|
432
|
507 ctx->wbits = wbits;
|
|
508 ctx->memlevel = memlevel;
|
|
509
|
416
|
510 /*
|
|
511 * We preallocate a memory for zlib in one buffer (200K-400K), this
|
|
512 * decreases a number of malloc() and free() calls and also probably
|
|
513 * decreases a number of syscalls (sbrk()/mmap() and so on).
|
|
514 * Besides we free the memory as soon as a gzipping will complete
|
|
515 * and do not wait while a whole response will be sent to a client.
|
|
516 *
|
|
517 * 8K is for zlib deflate_state, it takes
|
|
518 * *) 5816 bytes on i386 and sparc64 (32-bit mode)
|
|
519 * *) 5920 bytes on amd64 and sparc64
|
|
520 */
|
|
521
|
|
522 ctx->allocated = 8192 + (1 << (wbits + 2)) + (1 << (memlevel + 9));
|
432
|
523 }
|
|
524
|
|
525
|
|
526 static ngx_int_t
|
454
|
527 ngx_http_gzip_filter_buffer(ngx_http_gzip_ctx_t *ctx, ngx_chain_t *in)
|
432
|
528 {
|
454
|
529 size_t size, buffered;
|
|
530 ngx_buf_t *b, *buf;
|
|
531 ngx_chain_t *cl, **ll;
|
|
532 ngx_http_request_t *r;
|
|
533 ngx_http_gzip_conf_t *conf;
|
432
|
534
|
|
535 r = ctx->request;
|
|
536
|
|
537 r->connection->buffered |= NGX_HTTP_GZIP_BUFFERED;
|
|
538
|
|
539 buffered = 0;
|
|
540 ll = &ctx->in;
|
|
541
|
|
542 for (cl = ctx->in; cl; cl = cl->next) {
|
|
543 buffered += cl->buf->last - cl->buf->pos;
|
|
544 ll = &cl->next;
|
|
545 }
|
|
546
|
454
|
547 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
548
|
432
|
549 while (in) {
|
|
550 cl = ngx_alloc_chain_link(r->pool);
|
|
551 if (cl == NULL) {
|
|
552 return NGX_ERROR;
|
|
553 }
|
|
554
|
|
555 b = in->buf;
|
|
556
|
|
557 size = b->last - b->pos;
|
|
558 buffered += size;
|
|
559
|
454
|
560 if (b->flush || b->last_buf || buffered > conf->postpone_gzipping) {
|
432
|
561 ctx->buffering = 0;
|
|
562 }
|
|
563
|
454
|
564 if (ctx->buffering && size) {
|
432
|
565
|
|
566 buf = ngx_create_temp_buf(r->pool, size);
|
|
567 if (buf == NULL) {
|
|
568 return NGX_ERROR;
|
|
569 }
|
|
570
|
|
571 buf->last = ngx_cpymem(buf->pos, b->pos, size);
|
|
572 b->pos = b->last;
|
|
573
|
|
574 buf->last_buf = b->last_buf;
|
|
575 buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
|
|
576
|
|
577 cl->buf = buf;
|
|
578
|
|
579 } else {
|
|
580 cl->buf = b;
|
|
581 }
|
|
582
|
|
583 *ll = cl;
|
|
584 ll = &cl->next;
|
|
585 in = in->next;
|
|
586 }
|
|
587
|
|
588 *ll = NULL;
|
|
589
|
|
590 return ctx->buffering ? NGX_OK : NGX_DONE;
|
|
591 }
|
|
592
|
|
593
|
|
594 static ngx_int_t
|
|
595 ngx_http_gzip_filter_deflate_start(ngx_http_request_t *r,
|
|
596 ngx_http_gzip_ctx_t *ctx)
|
|
597 {
|
|
598 int rc;
|
|
599 ngx_http_gzip_conf_t *conf;
|
|
600
|
|
601 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
416
|
602
|
|
603 ctx->preallocated = ngx_palloc(r->pool, ctx->allocated);
|
|
604 if (ctx->preallocated == NULL) {
|
|
605 return NGX_ERROR;
|
|
606 }
|
|
607
|
|
608 ctx->free_mem = ctx->preallocated;
|
|
609
|
|
610 ctx->zstream.zalloc = ngx_http_gzip_filter_alloc;
|
|
611 ctx->zstream.zfree = ngx_http_gzip_filter_free;
|
|
612 ctx->zstream.opaque = ctx;
|
|
613
|
|
614 rc = deflateInit2(&ctx->zstream, (int) conf->level, Z_DEFLATED,
|
432
|
615 - ctx->wbits, ctx->memlevel, Z_DEFAULT_STRATEGY);
|
416
|
616
|
|
617 if (rc != Z_OK) {
|
|
618 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
619 "deflateInit2() failed: %d", rc);
|
|
620 return NGX_ERROR;
|
|
621 }
|
|
622
|
|
623 ctx->last_out = &ctx->out;
|
|
624 ctx->crc32 = crc32(0L, Z_NULL, 0);
|
|
625 ctx->flush = Z_NO_FLUSH;
|
|
626
|
|
627 return NGX_OK;
|
|
628 }
|
|
629
|
|
630
|
|
631 static ngx_int_t
|
|
632 ngx_http_gzip_filter_gzheader(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
|
|
633 {
|
|
634 ngx_buf_t *b;
|
|
635 ngx_chain_t *cl;
|
|
636 static u_char gzheader[10] =
|
|
637 { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
|
|
638
|
|
639 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
|
|
640 if (b == NULL) {
|
|
641 return NGX_ERROR;
|
|
642 }
|
|
643
|
|
644 b->memory = 1;
|
|
645 b->pos = gzheader;
|
|
646 b->last = b->pos + 10;
|
|
647
|
|
648 cl = ngx_alloc_chain_link(r->pool);
|
|
649 if (cl == NULL) {
|
|
650 return NGX_ERROR;
|
|
651 }
|
|
652
|
|
653 cl->buf = b;
|
|
654 cl->next = ctx->out;
|
|
655 ctx->out = cl;
|
|
656
|
|
657 ctx->gzheader = 1;
|
|
658
|
|
659 return NGX_OK;
|
|
660 }
|
|
661
|
|
662
|
|
663 static ngx_int_t
|
|
664 ngx_http_gzip_filter_add_data(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
|
|
665 {
|
|
666 if (ctx->zstream.avail_in || ctx->flush != Z_NO_FLUSH || ctx->redo) {
|
|
667 return NGX_OK;
|
|
668 }
|
|
669
|
|
670 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
671 "gzip in: %p", ctx->in);
|
|
672
|
|
673 if (ctx->in == NULL) {
|
|
674 return NGX_DECLINED;
|
|
675 }
|
|
676
|
432
|
677 if (ctx->copy_buf) {
|
|
678
|
|
679 /*
|
|
680 * to avoid CPU cache trashing we do not free() just quit buf,
|
|
681 * but postpone free()ing after zlib compressing and data output
|
|
682 */
|
|
683
|
|
684 ctx->copy_buf->next = ctx->copied;
|
|
685 ctx->copied = ctx->copy_buf;
|
|
686 ctx->copy_buf = NULL;
|
|
687 }
|
|
688
|
416
|
689 ctx->in_buf = ctx->in->buf;
|
432
|
690
|
|
691 if (ctx->in_buf->tag == (ngx_buf_tag_t) &ngx_http_gzip_filter_module) {
|
|
692 ctx->copy_buf = ctx->in;
|
|
693 }
|
|
694
|
416
|
695 ctx->in = ctx->in->next;
|
|
696
|
|
697 ctx->zstream.next_in = ctx->in_buf->pos;
|
|
698 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos;
|
|
699
|
|
700 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
701 "gzip in_buf:%p ni:%p ai:%ud",
|
|
702 ctx->in_buf,
|
|
703 ctx->zstream.next_in, ctx->zstream.avail_in);
|
|
704
|
|
705 if (ctx->in_buf->last_buf) {
|
|
706 ctx->flush = Z_FINISH;
|
|
707
|
|
708 } else if (ctx->in_buf->flush) {
|
|
709 ctx->flush = Z_SYNC_FLUSH;
|
|
710 }
|
|
711
|
|
712 if (ctx->zstream.avail_in) {
|
|
713
|
|
714 ctx->crc32 = crc32(ctx->crc32, ctx->zstream.next_in,
|
|
715 ctx->zstream.avail_in);
|
|
716
|
|
717 } else if (ctx->flush == Z_NO_FLUSH) {
|
|
718 return NGX_AGAIN;
|
|
719 }
|
|
720
|
|
721 return NGX_OK;
|
|
722 }
|
|
723
|
|
724
|
|
725 static ngx_int_t
|
|
726 ngx_http_gzip_filter_get_buf(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
|
|
727 {
|
|
728 ngx_http_gzip_conf_t *conf;
|
|
729
|
|
730 if (ctx->zstream.avail_out) {
|
|
731 return NGX_OK;
|
|
732 }
|
|
733
|
|
734 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
735
|
|
736 if (ctx->free) {
|
|
737 ctx->out_buf = ctx->free->buf;
|
|
738 ctx->free = ctx->free->next;
|
|
739
|
|
740 } else if (ctx->bufs < conf->bufs.num) {
|
|
741
|
|
742 ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size);
|
|
743 if (ctx->out_buf == NULL) {
|
|
744 return NGX_ERROR;
|
|
745 }
|
|
746
|
|
747 ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gzip_filter_module;
|
|
748 ctx->out_buf->recycled = 1;
|
|
749 ctx->bufs++;
|
|
750
|
|
751 } else {
|
|
752 ctx->nomem = 1;
|
|
753 return NGX_DECLINED;
|
|
754 }
|
|
755
|
|
756 ctx->zstream.next_out = ctx->out_buf->pos;
|
|
757 ctx->zstream.avail_out = conf->bufs.size;
|
|
758
|
|
759 return NGX_OK;
|
|
760 }
|
|
761
|
|
762
|
|
763 static ngx_int_t
|
|
764 ngx_http_gzip_filter_deflate(ngx_http_request_t *r, ngx_http_gzip_ctx_t *ctx)
|
|
765 {
|
|
766 int rc;
|
662
|
767 ngx_buf_t *b;
|
416
|
768 ngx_chain_t *cl;
|
|
769 ngx_http_gzip_conf_t *conf;
|
|
770
|
|
771 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
772 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
|
|
773 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
774 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
775 ctx->flush, ctx->redo);
|
|
776
|
|
777 rc = deflate(&ctx->zstream, ctx->flush);
|
|
778
|
662
|
779 if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) {
|
416
|
780 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
781 "deflate() failed: %d, %d", ctx->flush, rc);
|
|
782 return NGX_ERROR;
|
|
783 }
|
|
784
|
|
785 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
786 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
|
|
787 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
788 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
789 rc);
|
|
790
|
|
791 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
792 "gzip in_buf:%p pos:%p",
|
|
793 ctx->in_buf, ctx->in_buf->pos);
|
|
794
|
|
795 if (ctx->zstream.next_in) {
|
|
796 ctx->in_buf->pos = ctx->zstream.next_in;
|
|
797
|
|
798 if (ctx->zstream.avail_in == 0) {
|
|
799 ctx->zstream.next_in = NULL;
|
|
800 }
|
|
801 }
|
|
802
|
|
803 ctx->out_buf->last = ctx->zstream.next_out;
|
|
804
|
|
805 if (ctx->zstream.avail_out == 0) {
|
|
806
|
|
807 /* zlib wants to output some more gzipped data */
|
|
808
|
|
809 cl = ngx_alloc_chain_link(r->pool);
|
|
810 if (cl == NULL) {
|
|
811 return NGX_ERROR;
|
|
812 }
|
|
813
|
|
814 cl->buf = ctx->out_buf;
|
|
815 cl->next = NULL;
|
|
816 *ctx->last_out = cl;
|
|
817 ctx->last_out = &cl->next;
|
|
818
|
|
819 ctx->redo = 1;
|
|
820
|
|
821 return NGX_AGAIN;
|
|
822 }
|
|
823
|
|
824 ctx->redo = 0;
|
|
825
|
|
826 if (ctx->flush == Z_SYNC_FLUSH) {
|
|
827
|
|
828 ctx->flush = Z_NO_FLUSH;
|
|
829
|
|
830 cl = ngx_alloc_chain_link(r->pool);
|
|
831 if (cl == NULL) {
|
|
832 return NGX_ERROR;
|
|
833 }
|
|
834
|
662
|
835 b = ctx->out_buf;
|
|
836
|
|
837 if (ngx_buf_size(b) == 0) {
|
|
838
|
|
839 b = ngx_calloc_buf(ctx->request->pool);
|
|
840 if (b == NULL) {
|
|
841 return NGX_ERROR;
|
|
842 }
|
|
843
|
|
844 } else {
|
|
845 ctx->zstream.avail_out = 0;
|
|
846 }
|
|
847
|
|
848 b->flush = 1;
|
|
849
|
|
850 cl->buf = b;
|
416
|
851 cl->next = NULL;
|
|
852 *ctx->last_out = cl;
|
|
853 ctx->last_out = &cl->next;
|
|
854
|
|
855 return NGX_OK;
|
|
856 }
|
|
857
|
|
858 if (rc == Z_STREAM_END) {
|
|
859
|
|
860 if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {
|
|
861 return NGX_ERROR;
|
|
862 }
|
|
863
|
|
864 return NGX_OK;
|
|
865 }
|
|
866
|
|
867 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
868
|
|
869 if (conf->no_buffer && ctx->in == NULL) {
|
|
870
|
|
871 cl = ngx_alloc_chain_link(r->pool);
|
|
872 if (cl == NULL) {
|
|
873 return NGX_ERROR;
|
|
874 }
|
|
875
|
|
876 cl->buf = ctx->out_buf;
|
|
877 cl->next = NULL;
|
|
878 *ctx->last_out = cl;
|
|
879 ctx->last_out = &cl->next;
|
|
880
|
|
881 return NGX_OK;
|
|
882 }
|
|
883
|
|
884 return NGX_AGAIN;
|
|
885 }
|
|
886
|
|
887
|
|
888 static ngx_int_t
|
|
889 ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
|
|
890 ngx_http_gzip_ctx_t *ctx)
|
|
891 {
|
|
892 int rc;
|
|
893 ngx_buf_t *b;
|
|
894 ngx_chain_t *cl;
|
|
895 struct gztrailer *trailer;
|
|
896
|
|
897 ctx->zin = ctx->zstream.total_in;
|
|
898 ctx->zout = 10 + ctx->zstream.total_out + 8;
|
|
899
|
|
900 rc = deflateEnd(&ctx->zstream);
|
|
901
|
|
902 if (rc != Z_OK) {
|
|
903 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
904 "deflateEnd() failed: %d", rc);
|
|
905 return NGX_ERROR;
|
|
906 }
|
|
907
|
|
908 ngx_pfree(r->pool, ctx->preallocated);
|
|
909
|
|
910 cl = ngx_alloc_chain_link(r->pool);
|
|
911 if (cl == NULL) {
|
|
912 return NGX_ERROR;
|
|
913 }
|
|
914
|
|
915 cl->buf = ctx->out_buf;
|
|
916 cl->next = NULL;
|
|
917 *ctx->last_out = cl;
|
|
918 ctx->last_out = &cl->next;
|
|
919
|
|
920 if (ctx->zstream.avail_out >= 8) {
|
|
921 trailer = (struct gztrailer *) ctx->out_buf->last;
|
|
922 ctx->out_buf->last += 8;
|
|
923 ctx->out_buf->last_buf = 1;
|
|
924
|
|
925 } else {
|
|
926 b = ngx_create_temp_buf(r->pool, 8);
|
|
927 if (b == NULL) {
|
|
928 return NGX_ERROR;
|
|
929 }
|
|
930
|
|
931 b->last_buf = 1;
|
|
932
|
|
933 cl = ngx_alloc_chain_link(r->pool);
|
|
934 if (cl == NULL) {
|
|
935 return NGX_ERROR;
|
|
936 }
|
|
937
|
|
938 cl->buf = b;
|
|
939 cl->next = NULL;
|
|
940 *ctx->last_out = cl;
|
|
941 ctx->last_out = &cl->next;
|
|
942 trailer = (struct gztrailer *) b->pos;
|
|
943 b->last += 8;
|
|
944 }
|
|
945
|
|
946 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
|
|
947
|
|
948 trailer->crc32 = ctx->crc32;
|
|
949 trailer->zlen = ctx->zin;
|
|
950
|
|
951 #else
|
|
952
|
|
953 trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
|
|
954 trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
|
|
955 trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
|
|
956 trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
|
|
957
|
|
958 trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
|
|
959 trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
|
|
960 trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
|
|
961 trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
|
|
962
|
|
963 #endif
|
|
964
|
|
965 ctx->zstream.avail_in = 0;
|
|
966 ctx->zstream.avail_out = 0;
|
|
967
|
|
968 ctx->done = 1;
|
|
969
|
|
970 r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;
|
|
971
|
|
972 return NGX_OK;
|
50
|
973 }
|
|
974
|
|
975
|
|
976 static void *
|
|
977 ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
|
|
978 {
|
|
979 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
980
|
|
981 void *p;
|
|
982 ngx_uint_t alloc;
|
|
983
|
|
984 alloc = items * size;
|
|
985
|
434
|
986 if (alloc % 512 != 0 && alloc < 8192) {
|
50
|
987
|
|
988 /*
|
|
989 * The zlib deflate_state allocation, it takes about 6K,
|
|
990 * we allocate 8K. Other allocations are divisible by 512.
|
|
991 */
|
|
992
|
434
|
993 alloc = 8192;
|
50
|
994 }
|
|
995
|
|
996 if (alloc <= ctx->allocated) {
|
|
997 p = ctx->free_mem;
|
|
998 ctx->free_mem += alloc;
|
|
999 ctx->allocated -= alloc;
|
|
1000
|
|
1001 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
|
1002 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
|
|
1003 items, size, alloc, p);
|
|
1004
|
|
1005 return p;
|
|
1006 }
|
|
1007
|
|
1008 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
|
|
1009 "gzip filter failed to use preallocated memory: %ud of %ud",
|
|
1010 items * size, ctx->allocated);
|
|
1011
|
|
1012 p = ngx_palloc(ctx->request->pool, items * size);
|
|
1013
|
|
1014 return p;
|
|
1015 }
|
|
1016
|
|
1017
|
|
1018 static void
|
|
1019 ngx_http_gzip_filter_free(void *opaque, void *address)
|
|
1020 {
|
|
1021 #if 0
|
|
1022 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
1023
|
|
1024 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
|
1025 "gzip free: %p", address);
|
|
1026 #endif
|
|
1027 }
|
|
1028
|
|
1029
|
432
|
1030 static void
|
|
1031 ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
|
|
1032 ngx_http_gzip_ctx_t *ctx)
|
|
1033 {
|
|
1034 ngx_chain_t *cl;
|
|
1035
|
|
1036 for (cl = ctx->copied; cl; cl = cl->next) {
|
|
1037 ngx_pfree(r->pool, cl->buf->start);
|
|
1038 }
|
|
1039
|
|
1040 ctx->copied = NULL;
|
|
1041 }
|
|
1042
|
|
1043
|
50
|
1044 static ngx_int_t
|
122
|
1045 ngx_http_gzip_add_variables(ngx_conf_t *cf)
|
50
|
1046 {
|
260
|
1047 ngx_http_variable_t *var;
|
50
|
1048
|
132
|
1049 var = ngx_http_add_variable(cf, &ngx_http_gzip_ratio, NGX_HTTP_VAR_NOHASH);
|
122
|
1050 if (var == NULL) {
|
|
1051 return NGX_ERROR;
|
|
1052 }
|
|
1053
|
186
|
1054 var->get_handler = ngx_http_gzip_ratio_variable;
|
122
|
1055
|
50
|
1056 return NGX_OK;
|
|
1057 }
|
|
1058
|
|
1059
|
|
1060 static ngx_int_t
|
122
|
1061 ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
|
|
1062 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1063 {
|
|
1064 ngx_uint_t zint, zfrac;
|
|
1065 ngx_http_gzip_ctx_t *ctx;
|
|
1066
|
126
|
1067 v->valid = 1;
|
340
|
1068 v->no_cacheable = 0;
|
122
|
1069 v->not_found = 0;
|
|
1070
|
|
1071 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
1072
|
|
1073 if (ctx == NULL || ctx->zout == 0) {
|
|
1074 v->not_found = 1;
|
|
1075 return NGX_OK;
|
|
1076 }
|
|
1077
|
382
|
1078 v->data = ngx_pnalloc(r->pool, NGX_INT32_LEN + 3);
|
122
|
1079 if (v->data == NULL) {
|
|
1080 return NGX_ERROR;
|
|
1081 }
|
|
1082
|
|
1083 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
|
|
1084 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
|
|
1085
|
|
1086 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
|
|
1087
|
|
1088 /* the rounding, e.g., 2.125 to 2.13 */
|
|
1089
|
|
1090 zfrac++;
|
|
1091
|
|
1092 if (zfrac > 99) {
|
|
1093 zint++;
|
|
1094 zfrac = 0;
|
|
1095 }
|
|
1096 }
|
|
1097
|
|
1098 v->len = ngx_sprintf(v->data, "%ui.%02ui", zint, zfrac) - v->data;
|
|
1099
|
|
1100 return NGX_OK;
|
|
1101 }
|
|
1102
|
|
1103
|
50
|
1104 static void *
|
|
1105 ngx_http_gzip_create_conf(ngx_conf_t *cf)
|
|
1106 {
|
|
1107 ngx_http_gzip_conf_t *conf;
|
|
1108
|
|
1109 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t));
|
|
1110 if (conf == NULL) {
|
496
|
1111 return NULL;
|
50
|
1112 }
|
|
1113
|
|
1114 /*
|
|
1115 * set by ngx_pcalloc():
|
|
1116 *
|
|
1117 * conf->bufs.num = 0;
|
394
|
1118 * conf->types = { NULL };
|
|
1119 * conf->types_keys = NULL;
|
50
|
1120 */
|
|
1121
|
|
1122 conf->enable = NGX_CONF_UNSET;
|
|
1123 conf->no_buffer = NGX_CONF_UNSET;
|
|
1124
|
454
|
1125 conf->postpone_gzipping = NGX_CONF_UNSET_SIZE;
|
50
|
1126 conf->level = NGX_CONF_UNSET;
|
454
|
1127 conf->wbits = NGX_CONF_UNSET_SIZE;
|
|
1128 conf->memlevel = NGX_CONF_UNSET_SIZE;
|
50
|
1129 conf->min_length = NGX_CONF_UNSET;
|
|
1130
|
|
1131 return conf;
|
|
1132 }
|
|
1133
|
|
1134
|
|
1135 static char *
|
|
1136 ngx_http_gzip_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1137 {
|
|
1138 ngx_http_gzip_conf_t *prev = parent;
|
|
1139 ngx_http_gzip_conf_t *conf = child;
|
|
1140
|
|
1141 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
454
|
1142 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
|
50
|
1143
|
432
|
1144 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs,
|
|
1145 (128 * 1024) / ngx_pagesize, ngx_pagesize);
|
50
|
1146
|
454
|
1147 ngx_conf_merge_size_value(conf->postpone_gzipping, prev->postpone_gzipping,
|
|
1148 0);
|
50
|
1149 ngx_conf_merge_value(conf->level, prev->level, 1);
|
|
1150 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
|
|
1151 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
|
|
1152 MAX_MEM_LEVEL - 1);
|
126
|
1153 ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
|
50
|
1154
|
554
|
1155 if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
|
|
1156 &prev->types_keys, &prev->types,
|
394
|
1157 ngx_http_html_default_types)
|
|
1158 != NGX_OK)
|
|
1159 {
|
|
1160 return NGX_CONF_ERROR;
|
50
|
1161 }
|
|
1162
|
|
1163 return NGX_CONF_OK;
|
|
1164 }
|
|
1165
|
|
1166
|
230
|
1167 static ngx_int_t
|
|
1168 ngx_http_gzip_filter_init(ngx_conf_t *cf)
|
|
1169 {
|
|
1170 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
1171 ngx_http_top_header_filter = ngx_http_gzip_header_filter;
|
|
1172
|
|
1173 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
1174 ngx_http_top_body_filter = ngx_http_gzip_body_filter;
|
|
1175
|
|
1176 return NGX_OK;
|
|
1177 }
|
|
1178
|
|
1179
|
50
|
1180 static char *
|
76
|
1181 ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data)
|
50
|
1182 {
|
416
|
1183 size_t *np = data;
|
50
|
1184
|
416
|
1185 size_t wbits, wsize;
|
50
|
1186
|
|
1187 wbits = 15;
|
|
1188
|
|
1189 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
|
|
1190
|
|
1191 if (wsize == *np) {
|
|
1192 *np = wbits;
|
|
1193
|
|
1194 return NGX_CONF_OK;
|
|
1195 }
|
|
1196
|
|
1197 wbits--;
|
|
1198 }
|
|
1199
|
|
1200 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
|
|
1201 }
|
|
1202
|
|
1203
|
|
1204 static char *
|
76
|
1205 ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data)
|
50
|
1206 {
|
416
|
1207 size_t *np = data;
|
50
|
1208
|
416
|
1209 size_t memlevel, hsize;
|
50
|
1210
|
|
1211 memlevel = 9;
|
|
1212
|
|
1213 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
|
|
1214
|
|
1215 if (hsize == *np) {
|
|
1216 *np = memlevel;
|
|
1217
|
|
1218 return NGX_CONF_OK;
|
|
1219 }
|
|
1220
|
|
1221 memlevel--;
|
|
1222 }
|
|
1223
|
|
1224 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";
|
|
1225 }
|