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