Mercurial > hg > nginx-ranges
comparison src/http/modules/ngx_http_sub_filter_module.c @ 517:8fbdd980b527
Merge with current.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 13 Jul 2009 23:56:24 +0400 |
parents | f39b9e29530d |
children | 5c576ea5dbd9 |
comparison
equal
deleted
inserted
replaced
424:44a61c599bb2 | 517:8fbdd980b527 |
---|---|
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <ngx_http.h> | 9 #include <ngx_http.h> |
10 | 10 |
11 | 11 |
12 typedef struct { | 12 typedef struct { |
13 ngx_str_t match; | 13 ngx_str_t match; |
14 ngx_str_t sub; | 14 ngx_http_complex_value_t value; |
15 | 15 |
16 ngx_hash_t types; | 16 ngx_hash_t types; |
17 | 17 |
18 ngx_array_t *sub_lengths; | 18 ngx_flag_t once; |
19 ngx_array_t *sub_values; | 19 |
20 | 20 ngx_array_t *types_keys; |
21 ngx_flag_t once; | |
22 | |
23 ngx_array_t *types_keys; | |
24 } ngx_http_sub_loc_conf_t; | 21 } ngx_http_sub_loc_conf_t; |
25 | 22 |
26 | 23 |
27 typedef enum { | 24 typedef enum { |
28 sub_start_state = 0, | 25 sub_start_state = 0, |
29 sub_match_state, | 26 sub_match_state, |
30 } ngx_http_sub_state_e; | 27 } ngx_http_sub_state_e; |
31 | 28 |
32 | 29 |
33 typedef struct { | 30 typedef struct { |
34 ngx_str_t match; | 31 ngx_str_t match; |
35 | 32 |
36 ngx_uint_t once; /* unsigned once:1 */ | 33 ngx_uint_t once; /* unsigned once:1 */ |
37 | 34 |
38 ngx_buf_t *buf; | 35 ngx_buf_t *buf; |
39 | 36 |
40 u_char *pos; | 37 u_char *pos; |
41 u_char *copy_start; | 38 u_char *copy_start; |
42 u_char *copy_end; | 39 u_char *copy_end; |
43 | 40 |
44 ngx_chain_t *in; | 41 ngx_chain_t *in; |
45 ngx_chain_t *out; | 42 ngx_chain_t *out; |
46 ngx_chain_t **last_out; | 43 ngx_chain_t **last_out; |
47 ngx_chain_t *busy; | 44 ngx_chain_t *busy; |
48 ngx_chain_t *free; | 45 ngx_chain_t *free; |
49 | 46 |
50 ngx_str_t sub; | 47 ngx_str_t sub; |
51 | 48 |
52 ngx_uint_t state; | 49 ngx_uint_t state; |
53 size_t saved; | 50 size_t saved; |
54 size_t looked; | 51 size_t looked; |
55 } ngx_http_sub_ctx_t; | 52 } ngx_http_sub_ctx_t; |
56 | 53 |
57 | 54 |
58 static ngx_int_t ngx_http_sub_output(ngx_http_request_t *r, | 55 static ngx_int_t ngx_http_sub_output(ngx_http_request_t *r, |
59 ngx_http_sub_ctx_t *ctx); | 56 ngx_http_sub_ctx_t *ctx); |
152 | 149 |
153 ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module); | 150 ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module); |
154 | 151 |
155 ctx->match = slcf->match; | 152 ctx->match = slcf->match; |
156 ctx->last_out = &ctx->out; | 153 ctx->last_out = &ctx->out; |
157 ctx->sub = slcf->sub; | |
158 | 154 |
159 r->filter_need_in_memory = 1; | 155 r->filter_need_in_memory = 1; |
160 | 156 |
161 if (r == r->main) { | 157 if (r == r->main) { |
162 ngx_http_clear_content_length(r); | 158 ngx_http_clear_content_length(r); |
202 } | 198 } |
203 | 199 |
204 /* add the incoming chain to the chain ctx->in */ | 200 /* add the incoming chain to the chain ctx->in */ |
205 | 201 |
206 if (in) { | 202 if (in) { |
207 if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) { | 203 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) { |
208 return NGX_ERROR; | 204 return NGX_ERROR; |
209 } | 205 } |
210 } | 206 } |
211 | 207 |
212 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | 208 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
344 | 340 |
345 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module); | 341 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module); |
346 | 342 |
347 if (ctx->sub.data == NULL) { | 343 if (ctx->sub.data == NULL) { |
348 | 344 |
349 if (ngx_http_script_run(r, &ctx->sub, slcf->sub_lengths->elts, | 345 if (ngx_http_complex_value(r, &slcf->value, &ctx->sub) |
350 0, slcf->sub_values->elts) | 346 != NGX_OK) |
351 == NULL) | |
352 { | 347 { |
353 return NGX_ERROR; | 348 return NGX_ERROR; |
354 } | 349 } |
355 } | 350 } |
356 | 351 |
463 | 458 |
464 if (ngx_buf_size(b) != 0) { | 459 if (ngx_buf_size(b) != 0) { |
465 break; | 460 break; |
466 } | 461 } |
467 | 462 |
468 #if (NGX_HAVE_WRITE_ZEROCOPY) | |
469 if (b->zerocopy_busy) { | |
470 break; | |
471 } | |
472 #endif | |
473 | |
474 if (b->shadow) { | 463 if (b->shadow) { |
475 b->shadow->pos = b->shadow->last; | 464 b->shadow->pos = b->shadow->last; |
476 } | 465 } |
477 | 466 |
478 ctx->busy = cl->next; | 467 ctx->busy = cl->next; |
613 static char * | 602 static char * |
614 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | 603 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
615 { | 604 { |
616 ngx_http_sub_loc_conf_t *slcf = conf; | 605 ngx_http_sub_loc_conf_t *slcf = conf; |
617 | 606 |
618 ngx_str_t *value; | 607 ngx_str_t *value; |
619 ngx_int_t n; | 608 ngx_http_compile_complex_value_t ccv; |
620 ngx_http_script_compile_t sc; | |
621 | 609 |
622 if (slcf->match.len) { | 610 if (slcf->match.len) { |
623 return "is duplicate"; | 611 return "is duplicate"; |
624 } | 612 } |
625 | 613 |
627 | 615 |
628 ngx_strlow(value[1].data, value[1].data, value[1].len); | 616 ngx_strlow(value[1].data, value[1].data, value[1].len); |
629 | 617 |
630 slcf->match = value[1]; | 618 slcf->match = value[1]; |
631 | 619 |
632 n = ngx_http_script_variables_count(&value[2]); | 620 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
633 | 621 |
634 if (n == 0) { | 622 ccv.cf = cf; |
635 slcf->sub = value[2]; | 623 ccv.value = &value[2]; |
636 return NGX_CONF_OK; | 624 ccv.complex_value = &slcf->value; |
637 } | 625 |
638 | 626 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
639 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
640 | |
641 sc.cf = cf; | |
642 sc.source = &value[2]; | |
643 sc.lengths = &slcf->sub_lengths; | |
644 sc.values = &slcf->sub_values; | |
645 sc.variables = n; | |
646 sc.complete_lengths = 1; | |
647 sc.complete_values = 1; | |
648 | |
649 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
650 return NGX_CONF_ERROR; | 627 return NGX_CONF_ERROR; |
651 } | 628 } |
652 | 629 |
653 return NGX_CONF_OK; | 630 return NGX_CONF_OK; |
654 } | 631 } |
659 { | 636 { |
660 ngx_http_sub_loc_conf_t *slcf; | 637 ngx_http_sub_loc_conf_t *slcf; |
661 | 638 |
662 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t)); | 639 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t)); |
663 if (slcf == NULL) { | 640 if (slcf == NULL) { |
664 return NGX_CONF_ERROR; | 641 return NULL; |
665 } | 642 } |
666 | 643 |
667 /* | 644 /* |
668 * set by ngx_pcalloc(): | 645 * set by ngx_pcalloc(): |
669 * | 646 * |
688 ngx_http_sub_loc_conf_t *conf = child; | 665 ngx_http_sub_loc_conf_t *conf = child; |
689 | 666 |
690 ngx_conf_merge_value(conf->once, prev->once, 1); | 667 ngx_conf_merge_value(conf->once, prev->once, 1); |
691 ngx_conf_merge_str_value(conf->match, prev->match, ""); | 668 ngx_conf_merge_str_value(conf->match, prev->match, ""); |
692 | 669 |
693 if (conf->sub.data == NULL && conf->sub_lengths == NULL) { | 670 if (conf->value.value.len == 0) { |
694 conf->sub = prev->sub; | 671 conf->value = prev->value; |
695 conf->sub_lengths = prev->sub_lengths; | |
696 conf->sub_values = prev->sub_values; | |
697 } | 672 } |
698 | 673 |
699 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types, | 674 if (ngx_http_merge_types(cf, conf->types_keys, &conf->types, |
700 prev->types_keys, &prev->types, | 675 prev->types_keys, &prev->types, |
701 ngx_http_html_default_types) | 676 ngx_http_html_default_types) |