Mercurial > hg > ngx_http_gunzip_filter_module
annotate ngx_http_gunzip_filter_module.c @ 21:c0301992025a draft default tip
Gunzip: tests cleanup.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 07 Sep 2012 19:38:56 +0400 |
parents | 50ec0697da32 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Maxim Dounin | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 #include <nginx.h> | |
12 | |
13 #include <zlib.h> | |
14 | |
15 | |
16 typedef struct { | |
17 ngx_flag_t enable; | |
18 ngx_bufs_t bufs; | |
19 } ngx_http_gunzip_conf_t; | |
20 | |
21 | |
22 typedef struct { | |
23 ngx_chain_t *in; | |
24 ngx_chain_t *free; | |
25 ngx_chain_t *busy; | |
26 ngx_chain_t *out; | |
27 ngx_chain_t **last_out; | |
28 | |
29 ngx_buf_t *in_buf; | |
30 ngx_buf_t *out_buf; | |
31 ngx_int_t bufs; | |
32 | |
33 unsigned started:1; | |
34 unsigned flush:4; | |
35 unsigned redo:1; | |
36 unsigned done:1; | |
37 unsigned nomem:1; | |
38 | |
39 z_stream zstream; | |
40 ngx_http_request_t *request; | |
41 } ngx_http_gunzip_ctx_t; | |
42 | |
43 | |
44 static ngx_int_t ngx_http_gunzip_filter_inflate_start(ngx_http_request_t *r, | |
45 ngx_http_gunzip_ctx_t *ctx); | |
46 static ngx_int_t ngx_http_gunzip_filter_add_data(ngx_http_request_t *r, | |
47 ngx_http_gunzip_ctx_t *ctx); | |
48 static ngx_int_t ngx_http_gunzip_filter_get_buf(ngx_http_request_t *r, | |
49 ngx_http_gunzip_ctx_t *ctx); | |
50 static ngx_int_t ngx_http_gunzip_filter_inflate(ngx_http_request_t *r, | |
51 ngx_http_gunzip_ctx_t *ctx); | |
52 static ngx_int_t ngx_http_gunzip_filter_inflate_end(ngx_http_request_t *r, | |
53 ngx_http_gunzip_ctx_t *ctx); | |
54 | |
55 static void *ngx_http_gunzip_filter_alloc(void *opaque, u_int items, | |
56 u_int size); | |
57 static void ngx_http_gunzip_filter_free(void *opaque, void *address); | |
58 | |
59 static ngx_int_t ngx_http_gunzip_filter_init(ngx_conf_t *cf); | |
60 static void *ngx_http_gunzip_create_conf(ngx_conf_t *cf); | |
61 static char *ngx_http_gunzip_merge_conf(ngx_conf_t *cf, | |
62 void *parent, void *child); | |
63 | |
64 | |
65 static ngx_command_t ngx_http_gunzip_filter_commands[] = { | |
66 | |
67 { ngx_string("gunzip"), | |
68 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
69 ngx_conf_set_flag_slot, | |
70 NGX_HTTP_LOC_CONF_OFFSET, | |
71 offsetof(ngx_http_gunzip_conf_t, enable), | |
72 NULL }, | |
73 | |
74 { ngx_string("gunzip_buffers"), | |
75 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
76 ngx_conf_set_bufs_slot, | |
77 NGX_HTTP_LOC_CONF_OFFSET, | |
78 offsetof(ngx_http_gunzip_conf_t, bufs), | |
79 NULL }, | |
80 | |
81 ngx_null_command | |
82 }; | |
83 | |
84 | |
85 static ngx_http_module_t ngx_http_gunzip_filter_module_ctx = { | |
86 NULL, /* preconfiguration */ | |
87 ngx_http_gunzip_filter_init, /* postconfiguration */ | |
88 | |
89 NULL, /* create main configuration */ | |
90 NULL, /* init main configuration */ | |
91 | |
92 NULL, /* create server configuration */ | |
93 NULL, /* merge server configuration */ | |
94 | |
95 ngx_http_gunzip_create_conf, /* create location configuration */ | |
96 ngx_http_gunzip_merge_conf /* merge location configuration */ | |
97 }; | |
98 | |
99 | |
100 ngx_module_t ngx_http_gunzip_filter_module = { | |
101 NGX_MODULE_V1, | |
102 &ngx_http_gunzip_filter_module_ctx, /* module context */ | |
103 ngx_http_gunzip_filter_commands, /* module directives */ | |
104 NGX_HTTP_MODULE, /* module type */ | |
105 NULL, /* init master */ | |
106 NULL, /* init module */ | |
107 NULL, /* init process */ | |
108 NULL, /* init thread */ | |
109 NULL, /* exit thread */ | |
110 NULL, /* exit process */ | |
111 NULL, /* exit master */ | |
112 NGX_MODULE_V1_PADDING | |
113 }; | |
114 | |
115 | |
116 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
117 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
118 | |
119 | |
120 static ngx_int_t | |
121 ngx_http_gunzip_header_filter(ngx_http_request_t *r) | |
122 { | |
123 ngx_http_gunzip_ctx_t *ctx; | |
124 ngx_http_gunzip_conf_t *conf; | |
125 | |
126 conf = ngx_http_get_module_loc_conf(r, ngx_http_gunzip_filter_module); | |
127 | |
128 /* TODO support multiple content-codings */ | |
1
0dd7d109e56b
Gunzip: add more tests and improve docs.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
129 /* TODO always gunzip - due to configuration or module request */ |
0dd7d109e56b
Gunzip: add more tests and improve docs.
Maxim Dounin <mdounin@mdounin.ru>
parents:
0
diff
changeset
|
130 /* TODO ignore content encoding? */ |
0 | 131 |
132 if (!conf->enable | |
133 || r->headers_out.content_encoding == NULL | |
134 || r->headers_out.content_encoding->value.len != 4 | |
135 || ngx_strncasecmp(r->headers_out.content_encoding->value.data, | |
136 (u_char *) "gzip", 4) != 0) | |
137 { | |
138 return ngx_http_next_header_filter(r); | |
139 } | |
140 | |
12
5b45458fb4ac
Gunzip: fix version condition 0.7.65+ to affect 0.7.* branch only.
Maxim Dounin <mdounin@mdounin.ru>
parents:
11
diff
changeset
|
141 #if (nginx_version >= 8025 || (nginx_version >= 7065 && nginx_version < 8000)) |
0 | 142 |
143 r->gzip_vary = 1; | |
144 | |
145 if (!r->gzip_tested) { | |
146 if (ngx_http_gzip_ok(r) == NGX_OK) { | |
147 return ngx_http_next_header_filter(r); | |
148 } | |
149 | |
150 } else if (!r->gzip_ok) { | |
151 return ngx_http_next_header_filter(r); | |
152 } | |
153 | |
154 #else | |
155 | |
156 if (ngx_http_gzip_ok(r) == NGX_OK) { | |
157 return ngx_http_next_header_filter(r); | |
158 } | |
159 | |
160 #endif | |
161 | |
162 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gunzip_ctx_t)); | |
163 if (ctx == NULL) { | |
164 return NGX_ERROR; | |
165 } | |
166 | |
167 ngx_http_set_ctx(r, ctx, ngx_http_gunzip_filter_module); | |
168 | |
169 ctx->request = r; | |
170 | |
171 r->filter_need_in_memory = 1; | |
172 | |
173 r->headers_out.content_encoding->hash = 0; | |
174 r->headers_out.content_encoding = NULL; | |
175 | |
176 ngx_http_clear_content_length(r); | |
177 ngx_http_clear_accept_ranges(r); | |
178 | |
179 return ngx_http_next_header_filter(r); | |
180 } | |
181 | |
182 | |
183 static ngx_int_t | |
184 ngx_http_gunzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
185 { | |
186 int rc; | |
187 ngx_chain_t *cl; | |
188 ngx_http_gunzip_ctx_t *ctx; | |
189 | |
190 ctx = ngx_http_get_module_ctx(r, ngx_http_gunzip_filter_module); | |
191 | |
192 if (ctx == NULL || ctx->done) { | |
193 return ngx_http_next_body_filter(r, in); | |
194 } | |
195 | |
196 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
197 "http gunzip filter"); | |
198 | |
199 if (!ctx->started) { | |
200 if (ngx_http_gunzip_filter_inflate_start(r, ctx) != NGX_OK) { | |
201 goto failed; | |
202 } | |
203 } | |
204 | |
205 if (in) { | |
206 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) { | |
207 goto failed; | |
208 } | |
209 } | |
210 | |
211 if (ctx->nomem) { | |
212 | |
213 /* flush busy buffers */ | |
214 | |
215 if (ngx_http_next_body_filter(r, NULL) == NGX_ERROR) { | |
216 goto failed; | |
217 } | |
218 | |
219 cl = NULL; | |
220 | |
16
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
221 #if (nginx_version >= 1001004) |
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
222 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &cl, |
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
223 (ngx_buf_tag_t) &ngx_http_gunzip_filter_module); |
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
224 #else |
0 | 225 ngx_chain_update_chains(&ctx->free, &ctx->busy, &cl, |
226 (ngx_buf_tag_t) &ngx_http_gunzip_filter_module); | |
16
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
227 #endif |
0 | 228 ctx->nomem = 0; |
229 } | |
230 | |
231 for ( ;; ) { | |
232 | |
233 /* cycle while we can write to a client */ | |
234 | |
235 for ( ;; ) { | |
236 | |
237 /* cycle while there is data to feed zlib and ... */ | |
238 | |
239 rc = ngx_http_gunzip_filter_add_data(r, ctx); | |
240 | |
241 if (rc == NGX_DECLINED) { | |
242 break; | |
243 } | |
244 | |
245 if (rc == NGX_AGAIN) { | |
246 continue; | |
247 } | |
248 | |
249 | |
250 /* ... there are buffers to write zlib output */ | |
251 | |
252 rc = ngx_http_gunzip_filter_get_buf(r, ctx); | |
253 | |
254 if (rc == NGX_DECLINED) { | |
255 break; | |
256 } | |
257 | |
258 if (rc == NGX_ERROR) { | |
259 goto failed; | |
260 } | |
261 | |
262 rc = ngx_http_gunzip_filter_inflate(r, ctx); | |
263 | |
264 if (rc == NGX_OK) { | |
265 break; | |
266 } | |
267 | |
268 if (rc == NGX_ERROR) { | |
269 goto failed; | |
270 } | |
271 | |
272 /* rc == NGX_AGAIN */ | |
273 } | |
274 | |
275 if (ctx->out == NULL) { | |
276 return ctx->busy ? NGX_AGAIN : NGX_OK; | |
277 } | |
278 | |
279 rc = ngx_http_next_body_filter(r, ctx->out); | |
280 | |
281 if (rc == NGX_ERROR) { | |
282 goto failed; | |
283 } | |
284 | |
16
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
285 #if (nginx_version >= 1001004) |
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
286 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &ctx->out, |
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
287 (ngx_buf_tag_t) &ngx_http_gunzip_filter_module); |
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
288 #else |
0 | 289 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out, |
290 (ngx_buf_tag_t) &ngx_http_gunzip_filter_module); | |
16
50ec0697da32
Catch up with nginx 1.1.4 API changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
12
diff
changeset
|
291 #endif |
0 | 292 ctx->last_out = &ctx->out; |
293 | |
294 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
295 "gunzip out: %p", ctx->out); | |
296 | |
297 ctx->nomem = 0; | |
298 | |
299 if (ctx->done) { | |
300 return rc; | |
301 } | |
302 } | |
303 | |
304 /* unreachable */ | |
305 | |
306 failed: | |
307 | |
308 ctx->done = 1; | |
309 | |
310 return NGX_ERROR; | |
311 } | |
312 | |
313 | |
314 static ngx_int_t | |
315 ngx_http_gunzip_filter_inflate_start(ngx_http_request_t *r, | |
316 ngx_http_gunzip_ctx_t *ctx) | |
317 { | |
318 int rc; | |
319 | |
320 ctx->zstream.next_in = Z_NULL; | |
321 ctx->zstream.avail_in = 0; | |
322 | |
323 ctx->zstream.zalloc = ngx_http_gunzip_filter_alloc; | |
324 ctx->zstream.zfree = ngx_http_gunzip_filter_free; | |
325 ctx->zstream.opaque = ctx; | |
326 | |
327 /* windowBits +16 to decode gzip, zlib 1.2.0.4+ */ | |
328 rc = inflateInit2(&ctx->zstream, MAX_WBITS + 16); | |
329 | |
330 if (rc != Z_OK) { | |
331 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
332 "inflateInit2() failed: %d", rc); | |
333 return NGX_ERROR; | |
334 } | |
335 | |
336 ctx->started = 1; | |
337 | |
338 ctx->last_out = &ctx->out; | |
339 ctx->flush = Z_NO_FLUSH; | |
340 | |
341 return NGX_OK; | |
342 } | |
343 | |
344 | |
345 static ngx_int_t | |
346 ngx_http_gunzip_filter_add_data(ngx_http_request_t *r, | |
347 ngx_http_gunzip_ctx_t *ctx) | |
348 { | |
349 if (ctx->zstream.avail_in || ctx->flush != Z_NO_FLUSH || ctx->redo) { | |
350 return NGX_OK; | |
351 } | |
352 | |
353 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
354 "gunzip in: %p", ctx->in); | |
355 | |
356 if (ctx->in == NULL) { | |
357 return NGX_DECLINED; | |
358 } | |
359 | |
360 ctx->in_buf = ctx->in->buf; | |
361 ctx->in = ctx->in->next; | |
362 | |
363 ctx->zstream.next_in = ctx->in_buf->pos; | |
364 ctx->zstream.avail_in = ctx->in_buf->last - ctx->in_buf->pos; | |
365 | |
366 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
367 "gunzip in_buf:%p ni:%p ai:%ud", | |
368 ctx->in_buf, | |
369 ctx->zstream.next_in, ctx->zstream.avail_in); | |
370 | |
371 if (ctx->in_buf->last_buf || ctx->in_buf->last_in_chain) { | |
372 ctx->flush = Z_FINISH; | |
373 | |
374 } else if (ctx->in_buf->flush) { | |
375 ctx->flush = Z_SYNC_FLUSH; | |
376 | |
377 } else if (ctx->zstream.avail_in == 0) { | |
378 /* ctx->flush == Z_NO_FLUSH */ | |
379 return NGX_AGAIN; | |
380 } | |
381 | |
382 return NGX_OK; | |
383 } | |
384 | |
385 | |
386 static ngx_int_t | |
387 ngx_http_gunzip_filter_get_buf(ngx_http_request_t *r, | |
388 ngx_http_gunzip_ctx_t *ctx) | |
389 { | |
390 ngx_http_gunzip_conf_t *conf; | |
391 | |
392 if (ctx->zstream.avail_out) { | |
393 return NGX_OK; | |
394 } | |
395 | |
396 conf = ngx_http_get_module_loc_conf(r, ngx_http_gunzip_filter_module); | |
397 | |
398 if (ctx->free) { | |
399 ctx->out_buf = ctx->free->buf; | |
400 ctx->free = ctx->free->next; | |
401 | |
402 ctx->out_buf->flush = 0; | |
403 | |
404 } else if (ctx->bufs < conf->bufs.num) { | |
405 | |
406 ctx->out_buf = ngx_create_temp_buf(r->pool, conf->bufs.size); | |
407 if (ctx->out_buf == NULL) { | |
408 return NGX_ERROR; | |
409 } | |
410 | |
411 ctx->out_buf->tag = (ngx_buf_tag_t) &ngx_http_gunzip_filter_module; | |
412 ctx->out_buf->recycled = 1; | |
413 ctx->bufs++; | |
414 | |
415 } else { | |
416 ctx->nomem = 1; | |
417 return NGX_DECLINED; | |
418 } | |
419 | |
420 ctx->zstream.next_out = ctx->out_buf->pos; | |
421 ctx->zstream.avail_out = conf->bufs.size; | |
422 | |
423 return NGX_OK; | |
424 } | |
425 | |
426 | |
427 static ngx_int_t | |
428 ngx_http_gunzip_filter_inflate(ngx_http_request_t *r, | |
429 ngx_http_gunzip_ctx_t *ctx) | |
430 { | |
431 int rc; | |
432 ngx_buf_t *b; | |
433 ngx_chain_t *cl; | |
434 | |
435 ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
436 "inflate in: ni:%p no:%p ai:%ud ao:%ud fl:%d redo:%d", | |
437 ctx->zstream.next_in, ctx->zstream.next_out, | |
438 ctx->zstream.avail_in, ctx->zstream.avail_out, | |
439 ctx->flush, ctx->redo); | |
440 | |
441 rc = inflate(&ctx->zstream, ctx->flush); | |
442 | |
443 if (rc != Z_OK && rc != Z_STREAM_END && rc != Z_BUF_ERROR) { | |
444 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
445 "inflate() failed: %d, %d", ctx->flush, rc); | |
446 return NGX_ERROR; | |
447 } | |
448 | |
449 ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
450 "inflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d", | |
451 ctx->zstream.next_in, ctx->zstream.next_out, | |
452 ctx->zstream.avail_in, ctx->zstream.avail_out, | |
453 rc); | |
454 | |
455 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
456 "gunzip in_buf:%p pos:%p", | |
457 ctx->in_buf, ctx->in_buf->pos); | |
458 | |
459 if (ctx->zstream.next_in) { | |
460 ctx->in_buf->pos = ctx->zstream.next_in; | |
461 | |
462 if (ctx->zstream.avail_in == 0) { | |
463 ctx->zstream.next_in = NULL; | |
464 } | |
465 } | |
466 | |
467 ctx->out_buf->last = ctx->zstream.next_out; | |
468 | |
469 if (ctx->zstream.avail_out == 0) { | |
470 | |
471 /* zlib wants to output some more data */ | |
472 | |
473 cl = ngx_alloc_chain_link(r->pool); | |
474 if (cl == NULL) { | |
475 return NGX_ERROR; | |
476 } | |
477 | |
478 cl->buf = ctx->out_buf; | |
479 cl->next = NULL; | |
480 *ctx->last_out = cl; | |
481 ctx->last_out = &cl->next; | |
482 | |
483 ctx->redo = 1; | |
484 | |
485 return NGX_AGAIN; | |
486 } | |
487 | |
488 ctx->redo = 0; | |
489 | |
490 if (ctx->flush == Z_SYNC_FLUSH) { | |
491 | |
492 ctx->flush = Z_NO_FLUSH; | |
493 | |
494 cl = ngx_alloc_chain_link(r->pool); | |
495 if (cl == NULL) { | |
496 return NGX_ERROR; | |
497 } | |
498 | |
499 b = ctx->out_buf; | |
500 | |
501 if (ngx_buf_size(b) == 0) { | |
502 | |
503 b = ngx_calloc_buf(ctx->request->pool); | |
504 if (b == NULL) { | |
505 return NGX_ERROR; | |
506 } | |
507 | |
508 } else { | |
509 ctx->zstream.avail_out = 0; | |
510 } | |
511 | |
512 b->flush = 1; | |
513 | |
514 cl->buf = b; | |
515 cl->next = NULL; | |
516 *ctx->last_out = cl; | |
517 ctx->last_out = &cl->next; | |
518 | |
519 return NGX_OK; | |
520 } | |
521 | |
522 if (rc == Z_STREAM_END && ctx->flush == Z_FINISH | |
523 && ctx->zstream.avail_in == 0) | |
524 { | |
525 | |
526 if (ngx_http_gunzip_filter_inflate_end(r, ctx) != NGX_OK) { | |
527 return NGX_ERROR; | |
528 } | |
529 | |
530 return NGX_OK; | |
531 } | |
532 | |
533 if (rc == Z_STREAM_END && ctx->zstream.avail_in > 0) { | |
534 | |
535 rc = inflateReset(&ctx->zstream); | |
536 | |
537 if (rc != Z_OK) { | |
538 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
539 "inflateReset() failed: %d", rc); | |
540 return NGX_ERROR; | |
541 } | |
542 | |
543 ctx->redo = 1; | |
544 | |
545 return NGX_AGAIN; | |
546 } | |
547 | |
548 if (ctx->in == NULL) { | |
549 | |
11
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
550 b = ctx->out_buf; |
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
551 |
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
552 if (ngx_buf_size(b) == 0) { |
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
553 return NGX_OK; |
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
554 } |
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
555 |
0 | 556 cl = ngx_alloc_chain_link(r->pool); |
557 if (cl == NULL) { | |
558 return NGX_ERROR; | |
559 } | |
560 | |
11
b12d2ee20755
Gunzip: fix gunzip of empty gzip stream.
Maxim Dounin <mdounin@mdounin.ru>
parents:
9
diff
changeset
|
561 ctx->zstream.avail_out = 0; |
0 | 562 |
563 cl->buf = b; | |
564 cl->next = NULL; | |
565 *ctx->last_out = cl; | |
566 ctx->last_out = &cl->next; | |
567 | |
568 return NGX_OK; | |
569 } | |
570 | |
571 return NGX_AGAIN; | |
572 } | |
573 | |
574 | |
575 static ngx_int_t | |
576 ngx_http_gunzip_filter_inflate_end(ngx_http_request_t *r, | |
577 ngx_http_gunzip_ctx_t *ctx) | |
578 { | |
579 int rc; | |
580 ngx_buf_t *b; | |
581 ngx_chain_t *cl; | |
582 | |
583 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
584 "gunzip inflate end"); | |
585 | |
586 rc = inflateEnd(&ctx->zstream); | |
587 | |
588 if (rc != Z_OK) { | |
589 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
590 "inflateEnd() failed: %d", rc); | |
591 return NGX_ERROR; | |
592 } | |
593 | |
594 b = ctx->out_buf; | |
595 | |
596 if (ngx_buf_size(b) == 0) { | |
597 | |
598 b = ngx_calloc_buf(ctx->request->pool); | |
599 if (b == NULL) { | |
600 return NGX_ERROR; | |
601 } | |
602 } | |
603 | |
604 cl = ngx_alloc_chain_link(r->pool); | |
605 if (cl == NULL) { | |
606 return NGX_ERROR; | |
607 } | |
608 | |
609 cl->buf = b; | |
610 cl->next = NULL; | |
611 *ctx->last_out = cl; | |
612 ctx->last_out = &cl->next; | |
613 | |
614 b->last_buf = (r == r->main) ? 1 : 0; | |
615 b->last_in_chain = 1; | |
616 b->sync = 1; | |
617 | |
618 ctx->done = 1; | |
619 | |
620 return NGX_OK; | |
621 } | |
622 | |
623 | |
624 static void * | |
625 ngx_http_gunzip_filter_alloc(void *opaque, u_int items, u_int size) | |
626 { | |
627 ngx_http_gunzip_ctx_t *ctx = opaque; | |
628 | |
629 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0, | |
630 "gunzip alloc: n:%ud s:%ud", | |
631 items, size); | |
632 | |
633 return ngx_palloc(ctx->request->pool, items * size); | |
634 } | |
635 | |
636 | |
637 static void | |
638 ngx_http_gunzip_filter_free(void *opaque, void *address) | |
639 { | |
640 #if 0 | |
641 ngx_http_gunzip_ctx_t *ctx = opaque; | |
642 | |
643 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->request->connection->log, 0, | |
644 "gunzip free: %p", address); | |
645 #endif | |
646 } | |
647 | |
648 | |
649 static void * | |
650 ngx_http_gunzip_create_conf(ngx_conf_t *cf) | |
651 { | |
652 ngx_http_gunzip_conf_t *conf; | |
653 | |
654 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_gunzip_conf_t)); | |
655 if (conf == NULL) { | |
656 return NULL; | |
657 } | |
658 | |
659 /* | |
660 * set by ngx_pcalloc(): | |
661 * | |
662 * conf->bufs.num = 0; | |
663 */ | |
664 | |
665 conf->enable = NGX_CONF_UNSET; | |
666 | |
667 return conf; | |
668 } | |
669 | |
670 | |
671 static char * | |
672 ngx_http_gunzip_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
673 { | |
674 ngx_http_gunzip_conf_t *prev = parent; | |
675 ngx_http_gunzip_conf_t *conf = child; | |
676 | |
677 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
678 | |
679 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, | |
680 (128 * 1024) / ngx_pagesize, ngx_pagesize); | |
681 | |
682 return NGX_CONF_OK; | |
683 } | |
684 | |
685 | |
686 static ngx_int_t | |
687 ngx_http_gunzip_filter_init(ngx_conf_t *cf) | |
688 { | |
689 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
690 ngx_http_top_header_filter = ngx_http_gunzip_header_filter; | |
691 | |
692 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
693 ngx_http_top_body_filter = ngx_http_gunzip_body_filter; | |
694 | |
695 return NGX_OK; | |
696 } |