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