Mercurial > hg > nginx
comparison src/http/modules/ngx_http_gzip_filter.c @ 158:d377ee423603
nginx-0.0.1-2003-10-24-10:53:41 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 24 Oct 2003 06:53:41 +0000 |
parents | 70b36c805682 |
children | 981e4af2a425 |
comparison
equal
deleted
inserted
replaced
157:70b36c805682 | 158:d377ee423603 |
---|---|
48 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx); | 48 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx); |
49 static int ngx_http_gzip_filter_init(ngx_cycle_t *cycle); | 49 static int ngx_http_gzip_filter_init(ngx_cycle_t *cycle); |
50 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf); | 50 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf); |
51 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf, | 51 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf, |
52 void *parent, void *child); | 52 void *parent, void *child); |
53 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, ngx_command_t *cmd, | 53 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data); |
54 void *conf); | 54 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data); |
55 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, ngx_command_t *cmd, | 55 |
56 void *conf); | 56 |
57 | 57 static ngx_conf_num_bounds_t ngx_http_gzip_comp_level_bounds = { |
58 static ngx_conf_bounds_t ngx_http_gzip_comp_level_bounds; | 58 ngx_conf_check_num_bounds, 1, 9 |
59 }; | |
60 | |
61 static ngx_conf_post_handler_pt ngx_http_gzip_set_window_p = | |
62 ngx_http_gzip_set_window; | |
63 static ngx_conf_post_handler_pt ngx_http_gzip_set_hash_p = | |
64 ngx_http_gzip_set_hash; | |
65 | |
59 | 66 |
60 | 67 |
61 static ngx_command_t ngx_http_gzip_filter_commands[] = { | 68 static ngx_command_t ngx_http_gzip_filter_commands[] = { |
62 | 69 |
63 {ngx_string("gzip"), | 70 {ngx_string("gzip"), |
81 offsetof(ngx_http_gzip_conf_t, level), | 88 offsetof(ngx_http_gzip_conf_t, level), |
82 &ngx_http_gzip_comp_level_bounds}, | 89 &ngx_http_gzip_comp_level_bounds}, |
83 | 90 |
84 {ngx_string("gzip_window"), | 91 {ngx_string("gzip_window"), |
85 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 92 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
86 ngx_http_gzip_set_window, | 93 ngx_conf_set_size_slot, |
87 NGX_HTTP_LOC_CONF_OFFSET, | 94 NGX_HTTP_LOC_CONF_OFFSET, |
88 offsetof(ngx_http_gzip_conf_t, wbits), | 95 offsetof(ngx_http_gzip_conf_t, wbits), |
89 NULL}, | 96 &ngx_http_gzip_set_window_p}, |
90 | 97 |
91 {ngx_string("gzip_hash"), | 98 {ngx_string("gzip_hash"), |
92 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 99 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
93 ngx_http_gzip_set_hash, | 100 ngx_conf_set_size_slot, |
94 NGX_HTTP_LOC_CONF_OFFSET, | 101 NGX_HTTP_LOC_CONF_OFFSET, |
95 offsetof(ngx_http_gzip_conf_t, memlevel), | 102 offsetof(ngx_http_gzip_conf_t, memlevel), |
96 NULL}, | 103 &ngx_http_gzip_set_hash_p}, |
97 | 104 |
98 {ngx_string("gzip_no_buffer"), | 105 {ngx_string("gzip_no_buffer"), |
99 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | 106 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
100 ngx_conf_set_flag_slot, | 107 ngx_conf_set_flag_slot, |
101 NGX_HTTP_LOC_CONF_OFFSET, | 108 NGX_HTTP_LOC_CONF_OFFSET, |
126 ngx_http_gzip_filter_init, /* init module */ | 133 ngx_http_gzip_filter_init, /* init module */ |
127 NULL /* init child */ | 134 NULL /* init child */ |
128 }; | 135 }; |
129 | 136 |
130 | 137 |
131 static ngx_conf_bounds_t ngx_http_gzip_comp_level_bounds = { | 138 static u_char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 }; |
132 ngx_conf_check_num_bounds, { { 1, 9 } } | |
133 }; | |
134 | |
135 | |
136 static u_char gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 }; | |
137 | 139 |
138 #if (HAVE_LITTLE_ENDIAN) | 140 #if (HAVE_LITTLE_ENDIAN) |
139 | 141 |
140 struct gztrailer { | 142 struct gztrailer { |
141 u_int crc32; | 143 u_int crc32; |
238 } | 240 } |
239 } | 241 } |
240 | 242 |
241 /* | 243 /* |
242 * We preallocate a memory for zlib in one hunk (200K-400K), this | 244 * We preallocate a memory for zlib in one hunk (200K-400K), this |
243 * dicreases number of malloc() and free() calls and probably | 245 * dicreases a number of malloc() and free() calls and also probably |
244 * syscalls. | 246 * dicreases a number of syscalls. |
245 * Besides we free() this memory as soon as the gzipping will complete | 247 * Besides we free() this memory as soon as the gzipping will complete |
246 * and do not wait while a whole response will be sent to a client. | 248 * and do not wait while a whole response will be sent to a client. |
247 * | 249 * |
248 * 8K is for zlib deflate_state (~6K). | 250 * 8K is for zlib deflate_state (~6K). |
249 * | 251 * |
589 | 591 |
590 return NGX_CONF_OK; | 592 return NGX_CONF_OK; |
591 } | 593 } |
592 | 594 |
593 | 595 |
594 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, ngx_command_t *cmd, | 596 static char *ngx_http_gzip_set_window(ngx_conf_t *cf, void *post, void *data) |
595 void *conf) | 597 { |
596 { | 598 int *np = data; |
597 ngx_http_gzip_conf_t *lcf = conf; | 599 |
598 | 600 int wbits, wsize; |
599 int wbits, wsize; | 601 |
600 char *rv; | 602 |
601 | 603 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "WBITS: %d", *np); |
602 | |
603 rv = ngx_conf_set_size_slot(cf, cmd, conf); | |
604 if (rv) { | |
605 return rv; | |
606 } | |
607 | |
608 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "WBITS: %d", lcf->wbits); | |
609 | 604 |
610 wbits = 15; | 605 wbits = 15; |
611 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) { | 606 for (wsize = 32 * 1024; wsize > 256; wsize >>= 1) { |
612 | 607 |
613 if (wsize == lcf->wbits) { | 608 if (wsize == *np) { |
614 lcf->wbits = wbits; | 609 *np = wbits; |
615 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "WBITS: %d", lcf->wbits); | 610 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "WBITS: %d", *np); |
616 return NULL; | 611 return NULL; |
617 } | 612 } |
618 | 613 |
619 wbits--; | 614 wbits--; |
620 } | 615 } |
621 | 616 |
622 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k"; | 617 return "must be 512, 1k, 2k, 4k, 8k, 16k, or 32k"; |
623 } | 618 } |
624 | 619 |
625 | 620 |
626 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, ngx_command_t *cmd, | 621 static char *ngx_http_gzip_set_hash(ngx_conf_t *cf, void *post, void *data) |
627 void *conf) | 622 { |
628 { | 623 int *np = data; |
629 ngx_http_gzip_conf_t *lcf = conf; | 624 |
630 | 625 int memlevel, hsize; |
631 int memlevel, hsize; | 626 |
632 char *rv; | 627 |
633 | 628 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "MEMLEVEL: %d", *np); |
634 | |
635 rv = ngx_conf_set_size_slot(cf, cmd, conf); | |
636 if (rv) { | |
637 return rv; | |
638 } | |
639 | |
640 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "MEMLEVEL: %d", lcf->memlevel); | |
641 | 629 |
642 memlevel = 9; | 630 memlevel = 9; |
643 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) { | 631 for (hsize = 128 * 1024; hsize > 256; hsize >>= 1) { |
644 | 632 |
645 if (hsize == lcf->memlevel) { | 633 if (hsize == *np) { |
646 lcf->memlevel = memlevel; | 634 *np = memlevel; |
647 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "MEMLEVEL: %d", lcf->memlevel); | 635 ngx_conf_log_error(NGX_LOG_INFO, cf, 0, "MEMLEVEL: %d", *np); |
648 return NULL; | 636 return NULL; |
649 } | 637 } |
650 | 638 |
651 memlevel--; | 639 memlevel--; |
652 } | 640 } |