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