Mercurial > hg > nginx
comparison src/http/modules/ngx_http_gzip_filter.c @ 141:656d468f4ead
nginx-0.0.1-2003-10-08-19:32:54 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 08 Oct 2003 15:32:54 +0000 |
parents | da00cde00e8a |
children | ef8c87afcfc5 |
comparison
equal
deleted
inserted
replaced
140:e32405df0e77 | 141:656d468f4ead |
---|---|
5 | 5 |
6 #include <zlib.h> | 6 #include <zlib.h> |
7 | 7 |
8 | 8 |
9 typedef struct { | 9 typedef struct { |
10 int enable; | 10 int enable; |
11 int hunk_size; | 11 ngx_bufs_t bufs; |
12 int hunks; | 12 int no_buffer; |
13 int no_buffer; | |
14 } ngx_http_gzip_conf_t; | 13 } ngx_http_gzip_conf_t; |
15 | 14 |
16 | 15 |
17 typedef struct { | 16 typedef struct { |
18 ngx_chain_t *in; | 17 ngx_chain_t *in; |
49 ngx_conf_set_flag_slot, | 48 ngx_conf_set_flag_slot, |
50 NGX_HTTP_LOC_CONF_OFFSET, | 49 NGX_HTTP_LOC_CONF_OFFSET, |
51 offsetof(ngx_http_gzip_conf_t, enable), | 50 offsetof(ngx_http_gzip_conf_t, enable), |
52 NULL}, | 51 NULL}, |
53 | 52 |
54 {ngx_string("gzip_hunk_size"), | 53 {ngx_string("gzip_buffers"), |
55 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 54 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, |
56 ngx_conf_set_size_slot, | 55 ngx_conf_set_bufs_slot, |
57 NGX_HTTP_LOC_CONF_OFFSET, | 56 NGX_HTTP_LOC_CONF_OFFSET, |
58 offsetof(ngx_http_gzip_conf_t, hunk_size), | 57 offsetof(ngx_http_gzip_conf_t, bufs), |
59 NULL}, | |
60 | |
61 {ngx_string("gzip_hunks"), | |
62 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
63 ngx_conf_set_num_slot, | |
64 NGX_HTTP_LOC_CONF_OFFSET, | |
65 offsetof(ngx_http_gzip_conf_t, hunks), | |
66 NULL}, | 58 NULL}, |
67 | 59 |
68 {ngx_string("gzip_no_buffer"), | 60 {ngx_string("gzip_no_buffer"), |
69 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | 61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
70 ngx_conf_set_flag_slot, | 62 ngx_conf_set_flag_slot, |
171 } | 163 } |
172 | 164 |
173 | 165 |
174 static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | 166 static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in) |
175 { | 167 { |
176 int rc, wbits, mem_level, zin, zout; | 168 int rc, wbits, mem_level, zin, zout, last; |
177 struct gztrailer *trailer; | 169 struct gztrailer *trailer; |
178 ngx_hunk_t *h; | 170 ngx_hunk_t *h; |
179 ngx_chain_t *ce; | 171 ngx_chain_t *ce; |
180 ngx_http_gzip_ctx_t *ctx; | 172 ngx_http_gzip_ctx_t *ctx; |
181 ngx_http_gzip_conf_t *conf; | 173 ngx_http_gzip_conf_t *conf; |
238 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) { | 230 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) { |
239 return ngx_http_gzip_error(ctx); | 231 return ngx_http_gzip_error(ctx); |
240 } | 232 } |
241 } | 233 } |
242 | 234 |
235 last = NGX_NONE; | |
236 | |
243 for ( ;; ) { | 237 for ( ;; ) { |
244 | 238 |
245 for ( ;; ) { | 239 for ( ;; ) { |
246 | 240 |
247 /* is there a data to gzip ? */ | 241 /* is there a data to gzip ? */ |
283 if (ctx->zstream.avail_out == 0) { | 277 if (ctx->zstream.avail_out == 0) { |
284 if (ctx->free) { | 278 if (ctx->free) { |
285 ctx->out_hunk = ctx->free->hunk; | 279 ctx->out_hunk = ctx->free->hunk; |
286 ctx->free = ctx->free->next; | 280 ctx->free = ctx->free->next; |
287 | 281 |
288 } else if (ctx->hunks < conf->hunks) { | 282 } else if (ctx->hunks < conf->bufs.num) { |
289 ngx_test_null(ctx->out_hunk, | 283 ngx_test_null(ctx->out_hunk, |
290 ngx_create_temp_hunk(r->pool, conf->hunk_size, | 284 ngx_create_temp_hunk(r->pool, conf->bufs.size, |
291 0, 0), | 285 0, 0), |
292 ngx_http_gzip_error(ctx)); | 286 ngx_http_gzip_error(ctx)); |
287 ctx->out_hunk->type |= NGX_HUNK_RECYCLED; | |
293 ctx->hunks++; | 288 ctx->hunks++; |
294 | 289 |
295 } else { | 290 } else { |
296 break; | 291 break; |
297 } | 292 } |
298 | 293 |
299 ctx->zstream.next_out = (unsigned char *) ctx->out_hunk->pos; | 294 ctx->zstream.next_out = (unsigned char *) ctx->out_hunk->pos; |
300 ctx->zstream.avail_out = conf->hunk_size; | 295 ctx->zstream.avail_out = conf->bufs.size; |
301 } | 296 } |
302 | 297 |
303 ngx_log_debug(r->connection->log, "deflate(): %08x %08x %d %d %d" _ | 298 ngx_log_debug(r->connection->log, "deflate(): %08x %08x %d %d %d" _ |
304 ctx->zstream.next_in _ ctx->zstream.next_out _ | 299 ctx->zstream.next_in _ ctx->zstream.next_out _ |
305 ctx->zstream.avail_in _ ctx->zstream.avail_out _ ctx->flush); | 300 ctx->zstream.avail_in _ ctx->zstream.avail_out _ ctx->flush); |
316 ctx->zstream.avail_in _ ctx->zstream.avail_out _ rc); | 311 ctx->zstream.avail_in _ ctx->zstream.avail_out _ rc); |
317 | 312 |
318 ctx->in_hunk->pos = (char *) ctx->zstream.next_in; | 313 ctx->in_hunk->pos = (char *) ctx->zstream.next_in; |
319 | 314 |
320 if (ctx->zstream.avail_out == 0) { | 315 if (ctx->zstream.avail_out == 0) { |
321 ctx->out_hunk->last += conf->hunk_size; | 316 ctx->out_hunk->last += conf->bufs.size; |
322 ngx_add_hunk_to_chain(ce, ctx->out_hunk, r->pool, | 317 ngx_add_hunk_to_chain(ce, ctx->out_hunk, r->pool, |
323 ngx_http_gzip_error(ctx)); | 318 ngx_http_gzip_error(ctx)); |
324 *ctx->last_out = ce; | 319 *ctx->last_out = ce; |
325 ctx->last_out = &ce->next; | 320 ctx->last_out = &ce->next; |
326 ctx->redo = 1; | 321 ctx->redo = 1; |
407 break; | 402 break; |
408 } | 403 } |
409 } | 404 } |
410 } | 405 } |
411 | 406 |
412 if (ctx->out == NULL) { | 407 if (ctx->out == NULL && last != NGX_NONE) { |
413 if (ctx->in || ctx->zstream.avail_in) { | 408 return last; |
414 return NGX_AGAIN; | 409 } |
415 } else { | 410 |
416 return NGX_OK; | 411 last = next_body_filter(r, ctx->out); |
417 } | 412 |
418 } | 413 if (last == NGX_ERROR) { |
419 | |
420 if (next_body_filter(r, ctx->out) == NGX_ERROR) { | |
421 return ngx_http_gzip_error(ctx); | 414 return ngx_http_gzip_error(ctx); |
422 } | 415 } |
423 | 416 |
424 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out); | 417 ngx_chain_update_chains(&ctx->free, &ctx->busy, &ctx->out); |
425 ctx->last_out = &ctx->out; | 418 ctx->last_out = &ctx->out; |
461 ngx_test_null(conf, | 454 ngx_test_null(conf, |
462 ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t)), | 455 ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t)), |
463 NGX_CONF_ERROR); | 456 NGX_CONF_ERROR); |
464 | 457 |
465 conf->enable = NGX_CONF_UNSET; | 458 conf->enable = NGX_CONF_UNSET; |
466 conf->hunk_size = NGX_CONF_UNSET; | 459 /* conf->bufs.num = 0; */ |
467 conf->hunks = NGX_CONF_UNSET; | |
468 conf->no_buffer = NGX_CONF_UNSET; | 460 conf->no_buffer = NGX_CONF_UNSET; |
469 | 461 |
470 return conf; | 462 return conf; |
471 } | 463 } |
472 | 464 |
476 { | 468 { |
477 ngx_http_gzip_conf_t *prev = parent; | 469 ngx_http_gzip_conf_t *prev = parent; |
478 ngx_http_gzip_conf_t *conf = child; | 470 ngx_http_gzip_conf_t *conf = child; |
479 | 471 |
480 ngx_conf_merge_value(conf->enable, prev->enable, 0); | 472 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
481 ngx_conf_merge_size_value(conf->hunk_size, prev->hunk_size, | 473 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, |
482 /* STUB: PAGE_SIZE */ 4096); | 474 /* STUB: PAGE_SIZE */ 4096); |
483 ngx_conf_merge_value(conf->hunks, prev->hunks, 4); | |
484 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0); | 475 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0); |
485 | 476 |
486 return NGX_CONF_OK; | 477 return NGX_CONF_OK; |
487 } | 478 } |