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