Mercurial > hg > nginx
comparison src/http/modules/ngx_http_gzip_filter.c @ 290:87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 16 Mar 2004 07:10:12 +0000 |
parents | 0750faf8d7e3 |
children | a472bfb778b3 |
comparison
equal
deleted
inserted
replaced
289:0750faf8d7e3 | 290:87e73f067470 |
---|---|
5 | 5 |
6 #include <zlib.h> | 6 #include <zlib.h> |
7 | 7 |
8 | 8 |
9 typedef struct { | 9 typedef struct { |
10 int enable; | 10 ngx_flag_t enable; |
11 ngx_flag_t no_buffer; | |
12 | |
11 ngx_bufs_t bufs; | 13 ngx_bufs_t bufs; |
14 | |
12 int level; | 15 int level; |
13 ssize_t wbits; | 16 int wbits; |
14 ssize_t memlevel; | 17 int memlevel; |
15 int no_buffer; | |
16 } ngx_http_gzip_conf_t; | 18 } ngx_http_gzip_conf_t; |
17 | 19 |
18 | 20 |
19 typedef struct { | 21 typedef struct { |
20 ngx_chain_t *in; | 22 ngx_chain_t *in; |
37 unsigned done:1; | 39 unsigned done:1; |
38 | 40 |
39 size_t zin; | 41 size_t zin; |
40 size_t zout; | 42 size_t zout; |
41 | 43 |
42 u_int crc32; | 44 uint32_t crc32; |
43 z_stream zstream; | 45 z_stream zstream; |
44 ngx_http_request_t *request; | 46 ngx_http_request_t *request; |
45 } ngx_http_gzip_ctx_t; | 47 } ngx_http_gzip_ctx_t; |
46 | 48 |
47 | 49 |
49 u_int size); | 51 u_int size); |
50 static void ngx_http_gzip_filter_free(void *opaque, void *address); | 52 static void ngx_http_gzip_filter_free(void *opaque, void *address); |
51 | 53 |
52 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx); | 54 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx); |
53 | 55 |
54 static char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, char *buf, | 56 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf, |
55 uintptr_t data); | 57 uintptr_t data); |
56 | 58 |
57 static int ngx_http_gzip_pre_conf(ngx_conf_t *cf); | 59 static int ngx_http_gzip_pre_conf(ngx_conf_t *cf); |
58 static int ngx_http_gzip_filter_init(ngx_cycle_t *cycle); | 60 static int ngx_http_gzip_filter_init(ngx_cycle_t *cycle); |
59 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf); | 61 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf); |
60 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf, | 62 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf, |
156 static u_char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 }; | 158 static u_char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 }; |
157 | 159 |
158 #if (HAVE_LITTLE_ENDIAN) | 160 #if (HAVE_LITTLE_ENDIAN) |
159 | 161 |
160 struct gztrailer { | 162 struct gztrailer { |
161 u_int crc32; | 163 uint32_t crc32; |
162 u_int zlen; | 164 uint32_t zlen; |
163 }; | 165 }; |
164 | 166 |
165 #else /* HAVE_BIG_ENDIAN */ | 167 #else /* HAVE_BIG_ENDIAN */ |
166 | 168 |
167 struct gztrailer { | 169 struct gztrailer { |
168 u_char crc32[4]; | 170 u_char crc32[4]; |
169 u_char zlen[4]; | 171 u_char zlen[4]; |
170 }; | 172 }; |
171 | 173 |
172 #endif | 174 #endif |
173 | 175 |
174 | 176 |
294 } | 296 } |
295 | 297 |
296 ngx_test_null(h, ngx_calloc_hunk(r->pool), ngx_http_gzip_error(ctx)); | 298 ngx_test_null(h, ngx_calloc_hunk(r->pool), ngx_http_gzip_error(ctx)); |
297 | 299 |
298 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_MEMORY; | 300 h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_MEMORY; |
299 h->pos = (char *) gzheader; | 301 h->pos = gzheader; |
300 h->last = h->pos + 10; | 302 h->last = h->pos + 10; |
301 | 303 |
302 ngx_alloc_link_and_set_hunk(cl, h, r->pool, ngx_http_gzip_error(ctx)); | 304 ngx_alloc_link_and_set_hunk(cl, h, r->pool, ngx_http_gzip_error(ctx)); |
303 ctx->out = cl; | 305 ctx->out = cl; |
304 ctx->last_out = &cl->next; | 306 ctx->last_out = &cl->next; |
394 "deflate out: ni:%X no:%X ai:%d ao:%d rc:%d", | 396 "deflate out: ni:%X no:%X ai:%d ao:%d rc:%d", |
395 ctx->zstream.next_in, ctx->zstream.next_out, | 397 ctx->zstream.next_in, ctx->zstream.next_out, |
396 ctx->zstream.avail_in, ctx->zstream.avail_out, | 398 ctx->zstream.avail_in, ctx->zstream.avail_out, |
397 rc); | 399 rc); |
398 | 400 |
399 ctx->in_hunk->pos = (char *) ctx->zstream.next_in; | 401 ctx->in_hunk->pos = ctx->zstream.next_in; |
400 ctx->out_hunk->last = (char *) ctx->zstream.next_out; | 402 ctx->out_hunk->last = ctx->zstream.next_out; |
401 | 403 |
402 if (ctx->zstream.avail_out == 0) { | 404 if (ctx->zstream.avail_out == 0) { |
403 ngx_alloc_link_and_set_hunk(cl, ctx->out_hunk, r->pool, | 405 ngx_alloc_link_and_set_hunk(cl, ctx->out_hunk, r->pool, |
404 ngx_http_gzip_error(ctx)); | 406 ngx_http_gzip_error(ctx)); |
405 *ctx->last_out = cl; | 407 *ctx->last_out = cl; |
556 "gzip free: %X", address); | 558 "gzip free: %X", address); |
557 #endif | 559 #endif |
558 } | 560 } |
559 | 561 |
560 | 562 |
561 static char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, char *buf, | 563 static u_char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, u_char *buf, |
562 uintptr_t data) | 564 uintptr_t data) |
563 { | 565 { |
564 u_int zint, zfrac; | 566 ngx_uint_t zint, zfrac; |
565 ngx_http_gzip_ctx_t *ctx; | 567 ngx_http_gzip_ctx_t *ctx; |
566 | 568 |
567 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module); | 569 ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module); |
568 | 570 |
569 if (ctx == NULL || ctx->zout == 0) { | 571 if (ctx == NULL || ctx->zout == 0) { |
570 *buf = '-'; | 572 *buf = '-'; |
571 return buf + 1; | 573 return buf + 1; |
572 } | 574 } |
573 | 575 |
574 #if 0 | 576 #if 0 |
575 return buf + ngx_snprintf(buf, NGX_INT32_LEN + 4, "%.2f", | 577 return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 4, "%.2f", |
576 (float) ctx->zin / ctx->zout); | 578 (float) ctx->zin / ctx->zout); |
577 #endif | 579 #endif |
578 | 580 |
579 /* we prefer do not use FPU */ | 581 /* we prefer do not use FPU */ |
580 | 582 |
581 zint = ctx->zin / ctx->zout; | 583 zint = (ngx_uint_t) (ctx->zin / ctx->zout); |
582 zfrac = (ctx->zin * 100 / ctx->zout) % 100; | 584 zfrac = (ngx_uint_t) ((ctx->zin * 100 / ctx->zout) % 100); |
583 | 585 |
584 if ((ctx->zin * 1000 / ctx->zout) %10 > 4) { | 586 if ((ctx->zin * 1000 / ctx->zout) %10 > 4) { |
585 if (++zfrac > 99) { | 587 if (++zfrac > 99) { |
586 zint++; | 588 zint++; |
587 zfrac = 0; | 589 zfrac = 0; |
588 } | 590 } |
589 } | 591 } |
590 | 592 |
591 return buf + ngx_snprintf(buf, NGX_INT32_LEN + 4, "%d.%02d", zint, zfrac); | 593 return buf + ngx_snprintf((char *) buf, NGX_INT32_LEN + 4, |
594 "%d.%02d", zint, zfrac); | |
592 } | 595 } |
593 | 596 |
594 | 597 |
595 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx) | 598 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx) |
596 { | 599 { |
645 ngx_test_null(conf, | 648 ngx_test_null(conf, |
646 ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t)), | 649 ngx_pcalloc(cf->pool, sizeof(ngx_http_gzip_conf_t)), |
647 NGX_CONF_ERROR); | 650 NGX_CONF_ERROR); |
648 | 651 |
649 conf->enable = NGX_CONF_UNSET; | 652 conf->enable = NGX_CONF_UNSET; |
653 conf->no_buffer = NGX_CONF_UNSET; | |
654 | |
650 /* conf->bufs.num = 0; */ | 655 /* conf->bufs.num = 0; */ |
651 conf->no_buffer = NGX_CONF_UNSET; | 656 |
652 conf->level = NGX_CONF_UNSET; | 657 conf->level = NGX_CONF_UNSET; |
653 conf->wbits = NGX_CONF_UNSET; | 658 conf->wbits = NGX_CONF_UNSET; |
654 conf->memlevel = NGX_CONF_UNSET; | 659 conf->memlevel = NGX_CONF_UNSET; |
655 | 660 |
656 return conf; | 661 return conf; |
665 | 670 |
666 ngx_conf_merge_value(conf->enable, prev->enable, 0); | 671 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
667 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, | 672 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, |
668 /* STUB: PAGE_SIZE */ 4096); | 673 /* STUB: PAGE_SIZE */ 4096); |
669 ngx_conf_merge_value(conf->level, prev->level, 1); | 674 ngx_conf_merge_value(conf->level, prev->level, 1); |
670 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS); | 675 ngx_conf_merge_value(conf->wbits, prev->wbits, MAX_WBITS); |
671 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel, | 676 ngx_conf_merge_value(conf->memlevel, prev->memlevel, MAX_MEM_LEVEL - 1); |
672 MAX_MEM_LEVEL - 1); | |
673 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0); | 677 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0); |
674 | 678 |
675 return NGX_CONF_OK; | 679 return NGX_CONF_OK; |
676 } | 680 } |
677 | 681 |