comparison src/http/modules/ngx_http_gzip_filter_module.c @ 358:9121a0a91f47 NGINX_0_6_23

nginx 0.6.23 *) Change: the "off" parameter in the "ssl_session_cache" directive; now this is default parameter. *) Change: the "open_file_cache_retest" directive was renamed to the "open_file_cache_valid". *) Feature: the "open_file_cache_min_uses" directive. *) Feature: the ngx_http_gzip_static_module. *) Feature: the "gzip_disable" directive. *) Feature: the "memcached_pass" directive may be used inside the "if" block. *) Bugfix: a segmentation fault occurred in worker process, if the "memcached_pass" and "if" directives were used in the same location. *) Bugfix: if a "satisfy_any on" directive was used and not all access and auth modules directives were set, then other given access and auth directives were not tested; *) Bugfix: regex parameters in a "valid_referers" directive were not inherited from previous level. *) Bugfix: a "post_action" directive did run if a request was completed with 499 status code. *) Bugfix: optimization of 16K buffer usage in a SSL connection. Thanks to Ben Maurer. *) Bugfix: the STARTTLS in SMTP mode did not work. Thanks to Oleg Motienko. *) Bugfix: in HTTPS mode requests might fail with the "bad write retry" error; bug appeared in 0.5.13.
author Igor Sysoev <http://sysoev.ru>
date Thu, 27 Dec 2007 00:00:00 +0300
parents 05693816539c
children 3ce4580ae286
comparison
equal deleted inserted replaced
357:16d557a75356 358:9121a0a91f47
12 12
13 13
14 typedef struct { 14 typedef struct {
15 ngx_flag_t enable; 15 ngx_flag_t enable;
16 ngx_flag_t no_buffer; 16 ngx_flag_t no_buffer;
17 ngx_flag_t vary;
18 17
19 ngx_array_t *types; /* array of ngx_str_t */ 18 ngx_array_t *types; /* array of ngx_str_t */
20 19
21 ngx_bufs_t bufs; 20 ngx_bufs_t bufs;
22
23 ngx_uint_t http_version;
24 ngx_uint_t proxied;
25 21
26 ngx_int_t level; 22 ngx_int_t level;
27 size_t wbits; 23 size_t wbits;
28 size_t memlevel; 24 size_t memlevel;
29 ssize_t min_length; 25 ssize_t min_length;
30 } ngx_http_gzip_conf_t; 26 } ngx_http_gzip_conf_t;
31
32
33 #define NGX_HTTP_GZIP_PROXIED_OFF 0x0002
34 #define NGX_HTTP_GZIP_PROXIED_EXPIRED 0x0004
35 #define NGX_HTTP_GZIP_PROXIED_NO_CACHE 0x0008
36 #define NGX_HTTP_GZIP_PROXIED_NO_STORE 0x0010
37 #define NGX_HTTP_GZIP_PROXIED_PRIVATE 0x0020
38 #define NGX_HTTP_GZIP_PROXIED_NO_LM 0x0040
39 #define NGX_HTTP_GZIP_PROXIED_NO_ETAG 0x0080
40 #define NGX_HTTP_GZIP_PROXIED_AUTH 0x0100
41 #define NGX_HTTP_GZIP_PROXIED_ANY 0x0200
42 27
43 28
44 typedef struct { 29 typedef struct {
45 ngx_chain_t *in; 30 ngx_chain_t *in;
46 ngx_chain_t *free; 31 ngx_chain_t *free;
68 z_stream zstream; 53 z_stream zstream;
69 ngx_http_request_t *request; 54 ngx_http_request_t *request;
70 } ngx_http_gzip_ctx_t; 55 } ngx_http_gzip_ctx_t;
71 56
72 57
73 static ngx_int_t ngx_http_gzip_proxied(ngx_http_request_t *r,
74 ngx_http_gzip_conf_t *conf);
75 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items, 58 static void *ngx_http_gzip_filter_alloc(void *opaque, u_int items,
76 u_int size); 59 u_int size);
77 static void ngx_http_gzip_filter_free(void *opaque, void *address); 60 static void ngx_http_gzip_filter_free(void *opaque, void *address);
78 static void ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx); 61 static void ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx);
79 62
97 80
98 static ngx_conf_post_handler_pt ngx_http_gzip_window_p = ngx_http_gzip_window; 81 static ngx_conf_post_handler_pt ngx_http_gzip_window_p = ngx_http_gzip_window;
99 static ngx_conf_post_handler_pt ngx_http_gzip_hash_p = ngx_http_gzip_hash; 82 static ngx_conf_post_handler_pt ngx_http_gzip_hash_p = ngx_http_gzip_hash;
100 83
101 84
102 static ngx_conf_enum_t ngx_http_gzip_http_version[] = {
103 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
104 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
105 { ngx_null_string, 0 }
106 };
107
108
109 static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {
110 { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
111 { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
112 { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
113 { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
114 { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
115 { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
116 { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
117 { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
118 { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
119 { ngx_null_string, 0 }
120 };
121
122
123 static ngx_command_t ngx_http_gzip_filter_commands[] = { 85 static ngx_command_t ngx_http_gzip_filter_commands[] = {
124 86
125 { ngx_string("gzip"), 87 { ngx_string("gzip"),
126 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF 88 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
127 |NGX_CONF_FLAG, 89 |NGX_CONF_FLAG,
170 ngx_conf_set_flag_slot, 132 ngx_conf_set_flag_slot,
171 NGX_HTTP_LOC_CONF_OFFSET, 133 NGX_HTTP_LOC_CONF_OFFSET,
172 offsetof(ngx_http_gzip_conf_t, no_buffer), 134 offsetof(ngx_http_gzip_conf_t, no_buffer),
173 NULL }, 135 NULL },
174 136
175 { ngx_string("gzip_http_version"),
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
177 ngx_conf_set_enum_slot,
178 NGX_HTTP_LOC_CONF_OFFSET,
179 offsetof(ngx_http_gzip_conf_t, http_version),
180 &ngx_http_gzip_http_version },
181
182 { ngx_string("gzip_proxied"),
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
184 ngx_conf_set_bitmask_slot,
185 NGX_HTTP_LOC_CONF_OFFSET,
186 offsetof(ngx_http_gzip_conf_t, proxied),
187 &ngx_http_gzip_proxied_mask },
188
189 { ngx_string("gzip_min_length"), 137 { ngx_string("gzip_min_length"),
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 138 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
191 ngx_conf_set_size_slot, 139 ngx_conf_set_size_slot,
192 NGX_HTTP_LOC_CONF_OFFSET, 140 NGX_HTTP_LOC_CONF_OFFSET,
193 offsetof(ngx_http_gzip_conf_t, min_length), 141 offsetof(ngx_http_gzip_conf_t, min_length),
194 NULL },
195
196 { ngx_string("gzip_vary"),
197 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
198 ngx_conf_set_flag_slot,
199 NGX_HTTP_LOC_CONF_OFFSET,
200 offsetof(ngx_http_gzip_conf_t, vary),
201 NULL }, 142 NULL },
202 143
203 ngx_null_command 144 ngx_null_command
204 }; 145 };
205 146
253 194
254 #endif 195 #endif
255 196
256 197
257 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio"); 198 static ngx_str_t ngx_http_gzip_ratio = ngx_string("gzip_ratio");
258 static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache");
259 static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store");
260 static ngx_str_t ngx_http_gzip_private = ngx_string("private");
261
262 199
263 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; 200 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
264 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; 201 static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
265 202
266 203
267 static ngx_int_t 204 static ngx_int_t
268 ngx_http_gzip_header_filter(ngx_http_request_t *r) 205 ngx_http_gzip_header_filter(ngx_http_request_t *r)
269 { 206 {
270 ngx_str_t *type; 207 ngx_str_t *type;
271 ngx_uint_t i; 208 ngx_uint_t i;
272 ngx_table_elt_t *header; 209 ngx_table_elt_t *h;
273 ngx_http_gzip_ctx_t *ctx; 210 ngx_http_gzip_ctx_t *ctx;
274 ngx_http_gzip_conf_t *conf; 211 ngx_http_gzip_conf_t *conf;
212 ngx_http_core_loc_conf_t *clcf;
275 213
276 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module); 214 conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
277 215
278 if (!conf->enable 216 if (!conf->enable
279 || (r->headers_out.status != NGX_HTTP_OK 217 || (r->headers_out.status != NGX_HTTP_OK
280 && r->headers_out.status != NGX_HTTP_FORBIDDEN 218 && r->headers_out.status != NGX_HTTP_FORBIDDEN
281 && r->headers_out.status != NGX_HTTP_NOT_FOUND) 219 && r->headers_out.status != NGX_HTTP_NOT_FOUND)
282 || r->header_only 220 || r->header_only
283 || r != r->main
284 || r->http_version < conf->http_version
285 || r->headers_out.content_type.len == 0 221 || r->headers_out.content_type.len == 0
286 || (r->headers_out.content_encoding 222 || (r->headers_out.content_encoding
287 && r->headers_out.content_encoding->value.len) 223 && r->headers_out.content_encoding->value.len)
288 || r->headers_in.accept_encoding == NULL
289 || (r->headers_out.content_length_n != -1 224 || (r->headers_out.content_length_n != -1
290 && r->headers_out.content_length_n < conf->min_length) 225 && r->headers_out.content_length_n < conf->min_length)
291 || ngx_strcasestrn(r->headers_in.accept_encoding->value.data, 226 || ngx_http_gzip_ok(r) != NGX_OK)
292 "gzip", 4 - 1)
293 == NULL
294 )
295 { 227 {
296 return ngx_http_next_header_filter(r); 228 return ngx_http_next_header_filter(r);
297 } 229 }
298
299 230
300 type = conf->types->elts; 231 type = conf->types->elts;
301 for (i = 0; i < conf->types->nelts; i++) { 232 for (i = 0; i < conf->types->nelts; i++) {
302 if (r->headers_out.content_type.len >= type[i].len 233 if (r->headers_out.content_type.len >= type[i].len
303 && ngx_strncasecmp(r->headers_out.content_type.data, 234 && ngx_strncasecmp(r->headers_out.content_type.data,
307 } 238 }
308 } 239 }
309 240
310 return ngx_http_next_header_filter(r); 241 return ngx_http_next_header_filter(r);
311 242
312
313 found: 243 found:
314
315 if (r->headers_in.via) {
316 if (conf->proxied & NGX_HTTP_GZIP_PROXIED_OFF) {
317 return ngx_http_next_header_filter(r);
318 }
319
320 if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_ANY)
321 && ngx_http_gzip_proxied(r, conf) == NGX_DECLINED)
322 {
323 return ngx_http_next_header_filter(r);
324 }
325 }
326
327
328 /*
329 * if the URL (without the "http://" prefix) is longer than 253 bytes
330 * then MSIE 4.x can not handle the compressed stream - it waits too long,
331 * hangs up or crashes
332 */
333
334 if (r->headers_in.msie4 && r->unparsed_uri.len > 200) {
335 return ngx_http_next_header_filter(r);
336 }
337 244
338 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t)); 245 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_gzip_ctx_t));
339 if (ctx == NULL) { 246 if (ctx == NULL) {
340 return NGX_ERROR; 247 return NGX_ERROR;
341 } 248 }
342 249
343 ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module); 250 ngx_http_set_ctx(r, ctx, ngx_http_gzip_filter_module);
344 251
345
346 ctx->request = r; 252 ctx->request = r;
347 253
348 header = ngx_list_push(&r->headers_out.headers); 254 h = ngx_list_push(&r->headers_out.headers);
349 if (header == NULL) { 255 if (h == NULL) {
350 return NGX_ERROR; 256 return NGX_ERROR;
351 } 257 }
352 258
353 header->hash = 1; 259 h->hash = 1;
354 header->key.len = sizeof("Content-Encoding") - 1; 260 h->key.len = sizeof("Content-Encoding") - 1;
355 header->key.data = (u_char *) "Content-Encoding"; 261 h->key.data = (u_char *) "Content-Encoding";
356 header->value.len = sizeof("gzip") - 1; 262 h->value.len = sizeof("gzip") - 1;
357 header->value.data = (u_char *) "gzip"; 263 h->value.data = (u_char *) "gzip";
358 264
359 r->headers_out.content_encoding = header; 265 r->headers_out.content_encoding = h;
360 266
361 if (conf->vary) { 267 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
362 header = ngx_list_push(&r->headers_out.headers); 268
363 if (header == NULL) { 269 if (clcf->gzip_vary) {
270 h = ngx_list_push(&r->headers_out.headers);
271 if (h == NULL) {
364 return NGX_ERROR; 272 return NGX_ERROR;
365 } 273 }
366 274
367 header->hash = 1; 275 h->hash = 1;
368 header->key.len = sizeof("Vary") - 1; 276 h->key.len = sizeof("Vary") - 1;
369 header->key.data = (u_char *) "Vary"; 277 h->key.data = (u_char *) "Vary";
370 header->value.len = sizeof("Accept-Encoding") - 1; 278 h->value.len = sizeof("Accept-Encoding") - 1;
371 header->value.data = (u_char *) "Accept-Encoding"; 279 h->value.data = (u_char *) "Accept-Encoding";
372 } 280 }
373 281
374 ctx->length = r->headers_out.content_length_n; 282 ctx->length = r->headers_out.content_length_n;
375 283
376 r->main_filter_need_in_memory = 1; 284 r->main_filter_need_in_memory = 1;
377 285
378 ngx_http_clear_content_length(r); 286 ngx_http_clear_content_length(r);
379 ngx_http_clear_accept_ranges(r); 287 ngx_http_clear_accept_ranges(r);
380 288
381 return ngx_http_next_header_filter(r); 289 return ngx_http_next_header_filter(r);
382 }
383
384
385 static ngx_int_t
386 ngx_http_gzip_proxied(ngx_http_request_t *r, ngx_http_gzip_conf_t *conf)
387 {
388 time_t date, expires;
389
390 if (r->headers_in.authorization
391 && (conf->proxied & NGX_HTTP_GZIP_PROXIED_AUTH))
392 {
393 return NGX_OK;
394 }
395
396 if (r->headers_out.expires) {
397
398 if (!(conf->proxied & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
399 return NGX_DECLINED;
400 }
401
402 expires = ngx_http_parse_time(r->headers_out.expires->value.data,
403 r->headers_out.expires->value.len);
404 if (expires == NGX_ERROR) {
405 return NGX_DECLINED;
406 }
407
408 if (r->headers_out.date) {
409 date = ngx_http_parse_time(r->headers_out.date->value.data,
410 r->headers_out.date->value.len);
411 if (date == NGX_ERROR) {
412 return NGX_DECLINED;
413 }
414
415 } else {
416 date = ngx_time();
417 }
418
419 if (expires < date) {
420 return NGX_OK;
421 }
422
423 return NGX_DECLINED;
424 }
425
426 if (r->headers_out.cache_control.elts) {
427
428 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
429 && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,
430 &ngx_http_gzip_no_cache, NULL) >= 0)
431 {
432 return NGX_OK;
433 }
434
435 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_STORE)
436 && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,
437 &ngx_http_gzip_no_store, NULL) >= 0)
438 {
439 return NGX_OK;
440 }
441
442 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_PRIVATE)
443 && ngx_http_parse_multi_header_lines(&r->headers_out.cache_control,
444 &ngx_http_gzip_private, NULL) >= 0)
445 {
446 return NGX_OK;
447 }
448
449 return NGX_DECLINED;
450 }
451
452 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_LM)
453 && r->headers_out.last_modified)
454 {
455 return NGX_DECLINED;
456 }
457
458 if ((conf->proxied & NGX_HTTP_GZIP_PROXIED_NO_ETAG)
459 && r->headers_out.etag)
460 {
461 return NGX_DECLINED;
462 }
463
464 return NGX_OK;
465 } 290 }
466 291
467 292
468 static ngx_int_t 293 static ngx_int_t
469 ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in) 294 ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
1015 840
1016 /* 841 /*
1017 * set by ngx_pcalloc(): 842 * set by ngx_pcalloc():
1018 * 843 *
1019 * conf->bufs.num = 0; 844 * conf->bufs.num = 0;
1020 * conf->proxied = 0;
1021 * conf->types = NULL; 845 * conf->types = NULL;
1022 */ 846 */
1023 847
1024 conf->enable = NGX_CONF_UNSET; 848 conf->enable = NGX_CONF_UNSET;
1025 conf->no_buffer = NGX_CONF_UNSET; 849 conf->no_buffer = NGX_CONF_UNSET;
1026 conf->vary = NGX_CONF_UNSET;
1027
1028 conf->http_version = NGX_CONF_UNSET_UINT;
1029 850
1030 conf->level = NGX_CONF_UNSET; 851 conf->level = NGX_CONF_UNSET;
1031 conf->wbits = (size_t) NGX_CONF_UNSET; 852 conf->wbits = (size_t) NGX_CONF_UNSET;
1032 conf->memlevel = (size_t) NGX_CONF_UNSET; 853 conf->memlevel = (size_t) NGX_CONF_UNSET;
1033 conf->min_length = NGX_CONF_UNSET; 854 conf->min_length = NGX_CONF_UNSET;
1045 ngx_str_t *type; 866 ngx_str_t *type;
1046 867
1047 ngx_conf_merge_value(conf->enable, prev->enable, 0); 868 ngx_conf_merge_value(conf->enable, prev->enable, 0);
1048 869
1049 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize); 870 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 4, ngx_pagesize);
1050
1051 ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
1052 NGX_HTTP_VERSION_11);
1053 ngx_conf_merge_bitmask_value(conf->proxied, prev->proxied,
1054 (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
1055 871
1056 ngx_conf_merge_value(conf->level, prev->level, 1); 872 ngx_conf_merge_value(conf->level, prev->level, 1);
1057 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS); 873 ngx_conf_merge_size_value(conf->wbits, prev->wbits, MAX_WBITS);
1058 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel, 874 ngx_conf_merge_size_value(conf->memlevel, prev->memlevel,
1059 MAX_MEM_LEVEL - 1); 875 MAX_MEM_LEVEL - 1);
1060 ngx_conf_merge_value(conf->min_length, prev->min_length, 20); 876 ngx_conf_merge_value(conf->min_length, prev->min_length, 20);
1061 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0); 877 ngx_conf_merge_value(conf->no_buffer, prev->no_buffer, 0);
1062 ngx_conf_merge_value(conf->vary, prev->vary, 0);
1063 878
1064 if (conf->types == NULL) { 879 if (conf->types == NULL) {
1065 if (prev->types == NULL) { 880 if (prev->types == NULL) {
1066 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t)); 881 conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
1067 if (conf->types == NULL) { 882 if (conf->types == NULL) {