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