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