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;
|
|
762 ngx_chain_t *cl;
|
|
763 ngx_http_gzip_conf_t *conf;
|
|
764
|
|
765 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
766 "deflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d",
|
|
767 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
768 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
769 ctx->flush, ctx->redo);
|
|
770
|
|
771 rc = deflate(&ctx->zstream, ctx->flush);
|
|
772
|
|
773 if (rc != Z_OK && rc != Z_STREAM_END) {
|
|
774 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
775 "deflate() failed: %d, %d", ctx->flush, rc);
|
|
776 return NGX_ERROR;
|
|
777 }
|
|
778
|
|
779 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
780 "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
|
|
781 ctx->zstream.next_in, ctx->zstream.next_out,
|
|
782 ctx->zstream.avail_in, ctx->zstream.avail_out,
|
|
783 rc);
|
|
784
|
|
785 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
786 "gzip in_buf:%p pos:%p",
|
|
787 ctx->in_buf, ctx->in_buf->pos);
|
|
788
|
|
789 if (ctx->zstream.next_in) {
|
|
790 ctx->in_buf->pos = ctx->zstream.next_in;
|
|
791
|
|
792 if (ctx->zstream.avail_in == 0) {
|
|
793 ctx->zstream.next_in = NULL;
|
|
794 }
|
|
795 }
|
|
796
|
|
797 ctx->out_buf->last = ctx->zstream.next_out;
|
|
798
|
|
799 if (ctx->zstream.avail_out == 0) {
|
|
800
|
|
801 /* zlib wants to output some more gzipped data */
|
|
802
|
|
803 cl = ngx_alloc_chain_link(r->pool);
|
|
804 if (cl == NULL) {
|
|
805 return NGX_ERROR;
|
|
806 }
|
|
807
|
|
808 cl->buf = ctx->out_buf;
|
|
809 cl->next = NULL;
|
|
810 *ctx->last_out = cl;
|
|
811 ctx->last_out = &cl->next;
|
|
812
|
|
813 ctx->redo = 1;
|
|
814
|
|
815 return NGX_AGAIN;
|
|
816 }
|
|
817
|
|
818 ctx->redo = 0;
|
|
819
|
|
820 if (ctx->flush == Z_SYNC_FLUSH) {
|
|
821
|
|
822 ctx->zstream.avail_out = 0;
|
|
823 ctx->out_buf->flush = 1;
|
|
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
|
|
831 cl->buf = ctx->out_buf;
|
|
832 cl->next = NULL;
|
|
833 *ctx->last_out = cl;
|
|
834 ctx->last_out = &cl->next;
|
|
835
|
|
836 return NGX_OK;
|
|
837 }
|
|
838
|
|
839 if (rc == Z_STREAM_END) {
|
|
840
|
|
841 if (ngx_http_gzip_filter_deflate_end(r, ctx) != NGX_OK) {
|
|
842 return NGX_ERROR;
|
|
843 }
|
|
844
|
|
845 return NGX_OK;
|
|
846 }
|
|
847
|
|
848 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
|
|
849
|
|
850 if (conf->no_buffer && ctx->in == NULL) {
|
|
851
|
|
852 cl = ngx_alloc_chain_link(r->pool);
|
|
853 if (cl == NULL) {
|
|
854 return NGX_ERROR;
|
|
855 }
|
|
856
|
|
857 cl->buf = ctx->out_buf;
|
|
858 cl->next = NULL;
|
|
859 *ctx->last_out = cl;
|
|
860 ctx->last_out = &cl->next;
|
|
861
|
|
862 return NGX_OK;
|
|
863 }
|
|
864
|
|
865 return NGX_AGAIN;
|
|
866 }
|
|
867
|
|
868
|
|
869 static ngx_int_t
|
|
870 ngx_http_gzip_filter_deflate_end(ngx_http_request_t *r,
|
|
871 ngx_http_gzip_ctx_t *ctx)
|
|
872 {
|
|
873 int rc;
|
|
874 ngx_buf_t *b;
|
|
875 ngx_chain_t *cl;
|
|
876 struct gztrailer *trailer;
|
|
877
|
|
878 ctx->zin = ctx->zstream.total_in;
|
|
879 ctx->zout = 10 + ctx->zstream.total_out + 8;
|
|
880
|
|
881 rc = deflateEnd(&ctx->zstream);
|
|
882
|
|
883 if (rc != Z_OK) {
|
|
884 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
885 "deflateEnd() failed: %d", rc);
|
|
886 return NGX_ERROR;
|
|
887 }
|
|
888
|
|
889 ngx_pfree(r->pool, ctx->preallocated);
|
|
890
|
|
891 cl = ngx_alloc_chain_link(r->pool);
|
|
892 if (cl == NULL) {
|
|
893 return NGX_ERROR;
|
|
894 }
|
|
895
|
|
896 cl->buf = ctx->out_buf;
|
|
897 cl->next = NULL;
|
|
898 *ctx->last_out = cl;
|
|
899 ctx->last_out = &cl->next;
|
|
900
|
|
901 if (ctx->zstream.avail_out >= 8) {
|
|
902 trailer = (struct gztrailer *) ctx->out_buf->last;
|
|
903 ctx->out_buf->last += 8;
|
|
904 ctx->out_buf->last_buf = 1;
|
|
905
|
|
906 } else {
|
|
907 b = ngx_create_temp_buf(r->pool, 8);
|
|
908 if (b == NULL) {
|
|
909 return NGX_ERROR;
|
|
910 }
|
|
911
|
|
912 b->last_buf = 1;
|
|
913
|
|
914 cl = ngx_alloc_chain_link(r->pool);
|
|
915 if (cl == NULL) {
|
|
916 return NGX_ERROR;
|
|
917 }
|
|
918
|
|
919 cl->buf = b;
|
|
920 cl->next = NULL;
|
|
921 *ctx->last_out = cl;
|
|
922 ctx->last_out = &cl->next;
|
|
923 trailer = (struct gztrailer *) b->pos;
|
|
924 b->last += 8;
|
|
925 }
|
|
926
|
|
927 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
|
|
928
|
|
929 trailer->crc32 = ctx->crc32;
|
|
930 trailer->zlen = ctx->zin;
|
|
931
|
|
932 #else
|
|
933
|
|
934 trailer->crc32[0] = (u_char) (ctx->crc32 & 0xff);
|
|
935 trailer->crc32[1] = (u_char) ((ctx->crc32 >> 8) & 0xff);
|
|
936 trailer->crc32[2] = (u_char) ((ctx->crc32 >> 16) & 0xff);
|
|
937 trailer->crc32[3] = (u_char) ((ctx->crc32 >> 24) & 0xff);
|
|
938
|
|
939 trailer->zlen[0] = (u_char) (ctx->zin & 0xff);
|
|
940 trailer->zlen[1] = (u_char) ((ctx->zin >> 8) & 0xff);
|
|
941 trailer->zlen[2] = (u_char) ((ctx->zin >> 16) & 0xff);
|
|
942 trailer->zlen[3] = (u_char) ((ctx->zin >> 24) & 0xff);
|
|
943
|
|
944 #endif
|
|
945
|
|
946 ctx->zstream.avail_in = 0;
|
|
947 ctx->zstream.avail_out = 0;
|
|
948
|
|
949 ctx->done = 1;
|
|
950
|
|
951 r->connection->buffered &= ~NGX_HTTP_GZIP_BUFFERED;
|
|
952
|
|
953 return NGX_OK;
|
50
|
954 }
|
|
955
|
|
956
|
|
957 static void *
|
|
958 ngx_http_gzip_filter_alloc(void *opaque, u_int items, u_int size)
|
|
959 {
|
|
960 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
961
|
|
962 void *p;
|
|
963 ngx_uint_t alloc;
|
|
964
|
|
965 alloc = items * size;
|
|
966
|
434
|
967 if (alloc % 512 != 0 && alloc < 8192) {
|
50
|
968
|
|
969 /*
|
|
970 * The zlib deflate_state allocation, it takes about 6K,
|
|
971 * we allocate 8K. Other allocations are divisible by 512.
|
|
972 */
|
|
973
|
434
|
974 alloc = 8192;
|
50
|
975 }
|
|
976
|
|
977 if (alloc <= ctx->allocated) {
|
|
978 p = ctx->free_mem;
|
|
979 ctx->free_mem += alloc;
|
|
980 ctx->allocated -= alloc;
|
|
981
|
|
982 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
|
983 "gzip alloc: n:%ud s:%ud a:%ud p:%p",
|
|
984 items, size, alloc, p);
|
|
985
|
|
986 return p;
|
|
987 }
|
|
988
|
|
989 ngx_log_error(NGX_LOG_ALERT, ctx->request->connection->log, 0,
|
|
990 "gzip filter failed to use preallocated memory: %ud of %ud",
|
|
991 items * size, ctx->allocated);
|
|
992
|
|
993 p = ngx_palloc(ctx->request->pool, items * size);
|
|
994
|
|
995 return p;
|
|
996 }
|
|
997
|
|
998
|
|
999 static void
|
|
1000 ngx_http_gzip_filter_free(void *opaque, void *address)
|
|
1001 {
|
|
1002 #if 0
|
|
1003 ngx_http_gzip_ctx_t *ctx = opaque;
|
|
1004
|
|
1005 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0,
|
|
1006 "gzip free: %p", address);
|
|
1007 #endif
|
|
1008 }
|
|
1009
|
|
1010
|
432
|
1011 static void
|
|
1012 ngx_http_gzip_filter_free_copy_buf(ngx_http_request_t *r,
|
|
1013 ngx_http_gzip_ctx_t *ctx)
|
|
1014 {
|
|
1015 ngx_chain_t *cl;
|
|
1016
|
|
1017 for (cl = ctx->copied; cl; cl = cl->next) {
|
|
1018 ngx_pfree(r->pool, cl->buf->start);
|
|
1019 }
|
|
1020
|
|
1021 ctx->copied = NULL;
|
|
1022 }
|
|
1023
|
|
1024
|
50
|
1025 static ngx_int_t
|
122
|
1026 ngx_http_gzip_add_variables(ngx_conf_t *cf)
|
50
|
1027 {
|
260
|
1028 ngx_http_variable_t *var;
|
50
|
1029
|
132
|
1030 var = ngx_http_add_variable(cf, &ngx_http_gzip_ratio, NGX_HTTP_VAR_NOHASH);
|
122
|
1031 if (var == NULL) {
|
|
1032 return NGX_ERROR;
|
|
1033 }
|
|
1034
|
186
|
1035 var->get_handler = ngx_http_gzip_ratio_variable;
|
122
|
1036
|
50
|
1037 return NGX_OK;
|
|
1038 }
|
|
1039
|
|
1040
|
|
1041 static ngx_int_t
|
122
|
1042 ngx_http_gzip_ratio_variable(ngx_http_request_t *r,
|
|
1043 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1044 {
|
|
1045 ngx_uint_t zint, zfrac;
|
|
1046 ngx_http_gzip_ctx_t *ctx;
|
|
1047
|
126
|
1048 v->valid = 1;
|
340
|
1049 v->no_cacheable = 0;
|
122
|
1050 v->not_found = 0;
|
|
1051
|
|
1052 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
|
|
1053
|
|
1054 if (ctx == NULL || ctx->zout == 0) {
|
|
1055 v->not_found = 1;
|
|
1056 return NGX_OK;
|
|
1057 }
|
|
1058
|
382
|
1059 v->data = ngx_pnalloc(r->pool, NGX_INT32_LEN + 3);
|
122
|
1060 if (v->data == NULL) {
|
|
1061 return NGX_ERROR;
|
|
1062 }
|
|
1063
|
|
1064 zint = (ngx_uint_t) (ctx->zin / ctx->zout);
|
|
1065 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100);
|
|
1066
|
|
1067 if ((ctx->zin * 1000 / ctx->zout) % 10 > 4) {
|
|
1068
|
|
1069 /* the rounding, e.g., 2.125 to 2.13 */
|
|
1070
|
|
1071 zfrac++;
|
|
1072
|
|
1073 if (zfrac > 99) {
|
|
1074 zint++;
|
|
1075 zfrac = 0;
|
|
1076 }
|
|
1077 }
|
|
1078
|
|
1079 v->len = ngx_sprintf(v->data, "%ui.%02ui", zint, zfrac) - v->data;
|
|
1080
|
|
1081 return NGX_OK;
|
|
1082 }
|
|
1083
|
|
1084
|
50
|
1085 static void *
|
|
1086 ngx_http_gzip_create_conf(ngx_conf_t *cf)
|
|
1087 {
|
|
1088 ngx_http_gzip_conf_t *conf;
|
|
1089
|
|
1090 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t));
|
|
1091 if (conf == NULL) {
|
496
|
1092 return NULL;
|
50
|
1093 }
|
|
1094
|
|
1095 /*
|
|
1096 * set by ngx_pcalloc():
|
|
1097 *
|
|
1098 * conf->bufs.num = 0;
|
394
|
1099 * conf->types = { NULL };
|
|
1100 * conf->types_keys = NULL;
|
50
|
1101 */
|
|
1102
|
|
1103 conf->enable = NGX_CONF_UNSET;
|
|
1104 conf->no_buffer = NGX_CONF_UNSET;
|
|
1105
|
454
|
1106 conf->postpone_gzipping = NGX_CONF_UNSET_SIZE;
|
50
|
1107 conf->level = NGX_CONF_UNSET;
|
454
|
1108 conf->wbits = NGX_CONF_UNSET_SIZE;
|
|
1109 conf->memlevel = NGX_CONF_UNSET_SIZE;
|
50
|
1110 conf->min_length = NGX_CONF_UNSET;
|
|
1111
|
|
1112 return conf;
|
|
1113 }
|
|
1114
|
|
1115
|
|
1116 static char *
|
|
1117 ngx_http_gzip_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1118 {
|
|
1119 ngx_http_gzip_conf_t *prev = parent;
|
|
1120 ngx_http_gzip_conf_t *conf = child;
|
|
1121
|
|
1122 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
454
|
1123 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
|
50
|
1124
|
432
|
1125 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs,
|
|
1126 (128 * 1024) / ngx_pagesize, ngx_pagesize);
|
50
|
1127
|
454
|
1128 ngx_conf_merge_size_value(conf->postpone_gzipping, prev->postpone_gzipping,
|
|
1129 0);
|
50
|
1130 ngx_conf_merge_value(conf->level, prev->level, 1);
|
|
1131 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
|
|
1132 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
|
|
1133 MAX_MEM_LEVEL - 1);
|
126
|
1134 ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
|
50
|
1135
|
554
|
1136 if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types,
|
|
1137 &prev->types_keys, &prev->types,
|
394
|
1138 ngx_http_html_default_types)
|
|
1139 != NGX_OK)
|
|
1140 {
|
|
1141 return NGX_CONF_ERROR;
|
50
|
1142 }
|
|
1143
|
|
1144 return NGX_CONF_OK;
|
|
1145 }
|
|
1146
|
|
1147
|
230
|
1148 static ngx_int_t
|
|
1149 ngx_http_gzip_filter_init(ngx_conf_t *cf)
|
|
1150 {
|
|
1151 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
1152 ngx_http_top_header_filter = ngx_http_gzip_header_filter;
|
|
1153
|
|
1154 ngx_http_next_body_filter = ngx_http_top_body_filter;
|
|
1155 ngx_http_top_body_filter = ngx_http_gzip_body_filter;
|
|
1156
|
|
1157 return NGX_OK;
|
|
1158 }
|
|
1159
|
|
1160
|
50
|
1161 static char *
|
76
|
1162 ngx_http_gzip_window(ngx_conf_t *cf, void *post, void *data)
|
50
|
1163 {
|
416
|
1164 size_t *np = data;
|
50
|
1165
|
416
|
1166 size_t wbits, wsize;
|
50
|
1167
|
|
1168 wbits = 15;
|
|
1169
|
|
1170 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) {
|
|
1171
|
|
1172 if (wsize == *np) {
|
|
1173 *np = wbits;
|
|
1174
|
|
1175 return NGX_CONF_OK;
|
|
1176 }
|
|
1177
|
|
1178 wbits--;
|
|
1179 }
|
|
1180
|
|
1181 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k";
|
|
1182 }
|
|
1183
|
|
1184
|
|
1185 static char *
|
76
|
1186 ngx_http_gzip_hash(ngx_conf_t *cf, void *post, void *data)
|
50
|
1187 {
|
416
|
1188 size_t *np = data;
|
50
|
1189
|
416
|
1190 size_t memlevel, hsize;
|
50
|
1191
|
|
1192 memlevel = 9;
|
|
1193
|
|
1194 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) {
|
|
1195
|
|
1196 if (hsize == *np) {
|
|
1197 *np = memlevel;
|
|
1198
|
|
1199 return NGX_CONF_OK;
|
|
1200 }
|
|
1201
|
|
1202 memlevel--;
|
|
1203 }
|
|
1204
|
|
1205 return "must be 512, 1k, 2k, 4k, 8k, 16k, 32k, 64k, or 128k";
|
|
1206 }
|