Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_sub_filter_module.c @ 6531:59f8f2dd8b31
Variable $request_id.
The variable contains text representation based on random data, usable as
a unique request identifier.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Tue, 26 Apr 2016 19:31:46 +0300 |
parents | f01ab2dbcfdc |
children | 0e0adbbc8752 |
rev | line source |
---|---|
1172 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
1172 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
14 ngx_http_complex_value_t match; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
15 ngx_http_complex_value_t value; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
16 } ngx_http_sub_pair_t; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
17 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
18 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
19 typedef struct { |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
20 ngx_str_t match; |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
21 ngx_http_complex_value_t *value; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
22 } ngx_http_sub_match_t; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
23 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
24 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
25 typedef struct { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
26 ngx_uint_t min_match_len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
27 ngx_uint_t max_match_len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
28 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
29 u_char index[257]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
30 u_char shift[256]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
31 } ngx_http_sub_tables_t; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
32 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
33 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
34 typedef struct { |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
35 ngx_uint_t dynamic; /* unsigned dynamic:1; */ |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
36 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
37 ngx_array_t *pairs; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
38 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
39 ngx_http_sub_tables_t *tables; |
1172 | 40 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
41 ngx_hash_t types; |
1172 | 42 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
43 ngx_flag_t once; |
5229
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
44 ngx_flag_t last_modified; |
2166
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
45 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
46 ngx_array_t *types_keys; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
47 ngx_array_t *matches; |
1172 | 48 } ngx_http_sub_loc_conf_t; |
49 | |
50 | |
51 typedef struct { | |
3675
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
52 ngx_str_t saved; |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
53 ngx_str_t looked; |
1172 | 54 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
55 ngx_uint_t once; /* unsigned once:1 */ |
1172 | 56 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
57 ngx_buf_t *buf; |
1172 | 58 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
59 u_char *pos; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
60 u_char *copy_start; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
61 u_char *copy_end; |
1172 | 62 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
63 ngx_chain_t *in; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
64 ngx_chain_t *out; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
65 ngx_chain_t **last_out; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
66 ngx_chain_t *busy; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
67 ngx_chain_t *free; |
1172 | 68 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
69 ngx_str_t *sub; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
70 ngx_uint_t applied; |
1172 | 71 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
72 ngx_int_t offset; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
73 ngx_uint_t index; |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
74 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
75 ngx_http_sub_tables_t *tables; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
76 ngx_array_t *matches; |
1172 | 77 } ngx_http_sub_ctx_t; |
78 | |
79 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
80 static ngx_uint_t ngx_http_sub_cmp_index; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
81 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
82 |
1172 | 83 static ngx_int_t ngx_http_sub_output(ngx_http_request_t *r, |
84 ngx_http_sub_ctx_t *ctx); | |
85 static ngx_int_t ngx_http_sub_parse(ngx_http_request_t *r, | |
86 ngx_http_sub_ctx_t *ctx); | |
87 | |
88 static char * ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, | |
89 void *conf); | |
90 static void *ngx_http_sub_create_conf(ngx_conf_t *cf); | |
91 static char *ngx_http_sub_merge_conf(ngx_conf_t *cf, | |
92 void *parent, void *child); | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
93 static void ngx_http_sub_init_tables(ngx_http_sub_tables_t *tables, |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
94 ngx_http_sub_match_t *match, ngx_uint_t n); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
95 static ngx_int_t ngx_http_sub_cmp_matches(const void *one, const void *two); |
1172 | 96 static ngx_int_t ngx_http_sub_filter_init(ngx_conf_t *cf); |
97 | |
98 | |
99 static ngx_command_t ngx_http_sub_filter_commands[] = { | |
100 | |
101 { ngx_string("sub_filter"), | |
102 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
103 ngx_http_sub_filter, | |
104 NGX_HTTP_LOC_CONF_OFFSET, | |
105 0, | |
106 NULL }, | |
107 | |
108 { ngx_string("sub_filter_types"), | |
109 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
2166
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
110 ngx_http_types_slot, |
1172 | 111 NGX_HTTP_LOC_CONF_OFFSET, |
2166
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
112 offsetof(ngx_http_sub_loc_conf_t, types_keys), |
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
113 &ngx_http_html_default_types[0] }, |
1172 | 114 |
115 { ngx_string("sub_filter_once"), | |
116 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
117 ngx_conf_set_flag_slot, | |
118 NGX_HTTP_LOC_CONF_OFFSET, | |
119 offsetof(ngx_http_sub_loc_conf_t, once), | |
120 NULL }, | |
121 | |
5229
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
122 { ngx_string("sub_filter_last_modified"), |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
123 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
124 ngx_conf_set_flag_slot, |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
125 NGX_HTTP_LOC_CONF_OFFSET, |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
126 offsetof(ngx_http_sub_loc_conf_t, last_modified), |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
127 NULL }, |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
128 |
1172 | 129 ngx_null_command |
130 }; | |
131 | |
132 | |
133 static ngx_http_module_t ngx_http_sub_filter_module_ctx = { | |
134 NULL, /* preconfiguration */ | |
135 ngx_http_sub_filter_init, /* postconfiguration */ | |
136 | |
137 NULL, /* create main configuration */ | |
138 NULL, /* init main configuration */ | |
139 | |
140 NULL, /* create server configuration */ | |
141 NULL, /* merge server configuration */ | |
142 | |
143 ngx_http_sub_create_conf, /* create location configuration */ | |
144 ngx_http_sub_merge_conf /* merge location configuration */ | |
145 }; | |
146 | |
147 | |
148 ngx_module_t ngx_http_sub_filter_module = { | |
149 NGX_MODULE_V1, | |
150 &ngx_http_sub_filter_module_ctx, /* module context */ | |
151 ngx_http_sub_filter_commands, /* module directives */ | |
152 NGX_HTTP_MODULE, /* module type */ | |
153 NULL, /* init master */ | |
154 NULL, /* init module */ | |
155 NULL, /* init process */ | |
156 NULL, /* init thread */ | |
157 NULL, /* exit thread */ | |
158 NULL, /* exit process */ | |
159 NULL, /* exit master */ | |
160 NGX_MODULE_V1_PADDING | |
161 }; | |
162 | |
163 | |
164 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; | |
165 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; | |
166 | |
167 | |
168 static ngx_int_t | |
169 ngx_http_sub_header_filter(ngx_http_request_t *r) | |
170 { | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
171 ngx_str_t *m; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
172 ngx_uint_t i, j, n; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
173 ngx_http_sub_ctx_t *ctx; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
174 ngx_http_sub_pair_t *pairs; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
175 ngx_http_sub_match_t *matches; |
1172 | 176 ngx_http_sub_loc_conf_t *slcf; |
177 | |
178 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module); | |
179 | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
180 if (slcf->pairs == NULL |
2166
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
181 || r->headers_out.content_length_n == 0 |
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
182 || ngx_http_test_content_type(r, &slcf->types) == NULL) |
1172 | 183 { |
184 return ngx_http_next_header_filter(r); | |
185 } | |
186 | |
187 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_ctx_t)); | |
188 if (ctx == NULL) { | |
189 return NGX_ERROR; | |
190 } | |
191 | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
192 if (slcf->dynamic == 0) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
193 ctx->tables = slcf->tables; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
194 ctx->matches = slcf->matches; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
195 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
196 } else { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
197 pairs = slcf->pairs->elts; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
198 n = slcf->pairs->nelts; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
199 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
200 matches = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_match_t) * n); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
201 if (matches == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
202 return NGX_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
203 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
204 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
205 j = 0; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
206 for (i = 0; i < n; i++) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
207 matches[j].value = &pairs[i].value; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
208 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
209 if (pairs[i].match.lengths == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
210 matches[j].match = pairs[i].match.value; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
211 j++; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
212 continue; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
213 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
214 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
215 m = &matches[j].match; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
216 if (ngx_http_complex_value(r, &pairs[i].match, m) != NGX_OK) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
217 return NGX_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
218 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
219 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
220 if (m->len == 0) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
221 continue; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
222 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
223 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
224 ngx_strlow(m->data, m->data, m->len); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
225 j++; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
226 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
227 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
228 if (j == 0) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
229 return ngx_http_next_header_filter(r); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
230 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
231 |
6464
088ef087a011
Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents:
6247
diff
changeset
|
232 ctx->matches = ngx_palloc(r->pool, sizeof(ngx_array_t)); |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
233 if (ctx->matches == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
234 return NGX_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
235 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
236 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
237 ctx->matches->elts = matches; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
238 ctx->matches->nelts = j; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
239 |
6464
088ef087a011
Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents:
6247
diff
changeset
|
240 ctx->tables = ngx_palloc(r->pool, sizeof(ngx_http_sub_tables_t)); |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
241 if (ctx->tables == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
242 return NGX_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
243 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
244 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
245 ngx_http_sub_init_tables(ctx->tables, ctx->matches->elts, |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
246 ctx->matches->nelts); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
247 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
248 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
249 ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
250 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
251 ctx->saved.data = ngx_pnalloc(r->pool, ctx->tables->max_match_len - 1); |
3675
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
252 if (ctx->saved.data == NULL) { |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
253 return NGX_ERROR; |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
254 } |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
255 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
256 ctx->looked.data = ngx_pnalloc(r->pool, ctx->tables->max_match_len - 1); |
3675
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
257 if (ctx->looked.data == NULL) { |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
258 return NGX_ERROR; |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
259 } |
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
260 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
261 ctx->offset = ctx->tables->min_match_len - 1; |
1172 | 262 ctx->last_out = &ctx->out; |
263 | |
264 r->filter_need_in_memory = 1; | |
265 | |
266 if (r == r->main) { | |
267 ngx_http_clear_content_length(r); | |
5229
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
268 |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
269 if (!slcf->last_modified) { |
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
270 ngx_http_clear_last_modified(r); |
5733
e491b26fa5a1
Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5710
diff
changeset
|
271 ngx_http_clear_etag(r); |
e491b26fa5a1
Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5710
diff
changeset
|
272 |
e491b26fa5a1
Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5710
diff
changeset
|
273 } else { |
e491b26fa5a1
Entity tags: downgrade strong etags to weak ones as needed.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5710
diff
changeset
|
274 ngx_http_weak_etag(r); |
5229
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
275 } |
1172 | 276 } |
277 | |
278 return ngx_http_next_header_filter(r); | |
279 } | |
280 | |
281 | |
282 static ngx_int_t | |
283 ngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in) | |
284 { | |
285 ngx_int_t rc; | |
286 ngx_buf_t *b; | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
287 ngx_str_t *sub; |
1172 | 288 ngx_chain_t *cl; |
289 ngx_http_sub_ctx_t *ctx; | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
290 ngx_http_sub_match_t *match; |
1172 | 291 ngx_http_sub_loc_conf_t *slcf; |
292 | |
293 ctx = ngx_http_get_module_ctx(r, ngx_http_sub_filter_module); | |
294 | |
295 if (ctx == NULL) { | |
296 return ngx_http_next_body_filter(r, in); | |
297 } | |
298 | |
299 if ((in == NULL | |
300 && ctx->buf == NULL | |
301 && ctx->in == NULL | |
302 && ctx->busy == NULL)) | |
303 { | |
304 return ngx_http_next_body_filter(r, in); | |
305 } | |
306 | |
307 if (ctx->once && (ctx->buf == NULL || ctx->in == NULL)) { | |
308 | |
309 if (ctx->busy) { | |
310 if (ngx_http_sub_output(r, ctx) == NGX_ERROR) { | |
311 return NGX_ERROR; | |
312 } | |
313 } | |
314 | |
315 return ngx_http_next_body_filter(r, in); | |
316 } | |
317 | |
318 /* add the incoming chain to the chain ctx->in */ | |
319 | |
320 if (in) { | |
2536
a6d6d762c554
small optimization: " == NGX_ERROR" > " != NGX_OK"
Igor Sysoev <igor@sysoev.ru>
parents:
2414
diff
changeset
|
321 if (ngx_chain_add_copy(r->pool, &ctx->in, in) != NGX_OK) { |
1172 | 322 return NGX_ERROR; |
323 } | |
324 } | |
325 | |
326 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
327 "http sub filter \"%V\"", &r->uri); | |
328 | |
329 while (ctx->in || ctx->buf) { | |
330 | |
3642 | 331 if (ctx->buf == NULL) { |
1172 | 332 ctx->buf = ctx->in->buf; |
333 ctx->in = ctx->in->next; | |
334 ctx->pos = ctx->buf->pos; | |
335 } | |
336 | |
337 b = NULL; | |
338 | |
339 while (ctx->pos < ctx->buf->last) { | |
340 | |
341 rc = ngx_http_sub_parse(r, ctx); | |
342 | |
343 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
6480 | 344 "parse: %i, looked: \"%V\" %p-%p", |
3675
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
345 rc, &ctx->looked, ctx->copy_start, ctx->copy_end); |
1172 | 346 |
347 if (rc == NGX_ERROR) { | |
348 return rc; | |
349 } | |
350 | |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
351 if (ctx->saved.len) { |
1172 | 352 |
353 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
3675
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
354 "saved: \"%V\"", &ctx->saved); |
1172 | 355 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
356 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
357 if (cl == NULL) { |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
358 return NGX_ERROR; |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
359 } |
1172 | 360 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
361 b = cl->buf; |
1172 | 362 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
363 ngx_memzero(b, sizeof(ngx_buf_t)); |
1172 | 364 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
365 b->pos = ngx_pnalloc(r->pool, ctx->saved.len); |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
366 if (b->pos == NULL) { |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
367 return NGX_ERROR; |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
368 } |
3675
08d7165b6be1
fix case of partially matched patterns on buffer border in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
3642
diff
changeset
|
369 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
370 ngx_memcpy(b->pos, ctx->saved.data, ctx->saved.len); |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
371 b->last = b->pos + ctx->saved.len; |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
372 b->memory = 1; |
1172 | 373 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
374 *ctx->last_out = cl; |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
375 ctx->last_out = &cl->next; |
1172 | 376 |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
377 ctx->saved.len = 0; |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
378 } |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
379 |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
380 if (ctx->copy_start != ctx->copy_end) { |
1172 | 381 |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
382 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
383 if (cl == NULL) { |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
384 return NGX_ERROR; |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
385 } |
1172 | 386 |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
387 b = cl->buf; |
1172 | 388 |
389 ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t)); | |
390 | |
391 b->pos = ctx->copy_start; | |
392 b->last = ctx->copy_end; | |
393 b->shadow = NULL; | |
394 b->last_buf = 0; | |
5710
9e9c1310d17c
Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5288
diff
changeset
|
395 b->last_in_chain = 0; |
1172 | 396 b->recycled = 0; |
397 | |
398 if (b->in_file) { | |
2079
333ef9e18a59
fix error when response parsed by sub filter, then by SSI filter
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
399 b->file_last = b->file_pos + (b->last - ctx->buf->pos); |
333ef9e18a59
fix error when response parsed by sub filter, then by SSI filter
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
400 b->file_pos += b->pos - ctx->buf->pos; |
1172 | 401 } |
402 | |
403 *ctx->last_out = cl; | |
404 ctx->last_out = &cl->next; | |
405 } | |
406 | |
407 if (rc == NGX_AGAIN) { | |
408 continue; | |
409 } | |
410 | |
411 | |
412 /* rc == NGX_OK */ | |
413 | |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
414 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
415 if (cl == NULL) { |
1172 | 416 return NGX_ERROR; |
417 } | |
418 | |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
419 b = cl->buf; |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
420 |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
421 ngx_memzero(b, sizeof(ngx_buf_t)); |
1172 | 422 |
423 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module); | |
424 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
425 if (ctx->sub == NULL) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
426 ctx->sub = ngx_pcalloc(r->pool, sizeof(ngx_str_t) |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
427 * ctx->matches->nelts); |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
428 if (ctx->sub == NULL) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
429 return NGX_ERROR; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
430 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
431 } |
1172 | 432 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
433 sub = &ctx->sub[ctx->index]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
434 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
435 if (sub->data == NULL) { |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
436 match = ctx->matches->elts; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
437 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
438 if (ngx_http_complex_value(r, match[ctx->index].value, sub) |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
439 != NGX_OK) |
1172 | 440 { |
441 return NGX_ERROR; | |
442 } | |
443 } | |
444 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
445 if (sub->len) { |
1555
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
446 b->memory = 1; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
447 b->pos = sub->data; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
448 b->last = sub->data + sub->len; |
1555
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
449 |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
450 } else { |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
451 b->sync = 1; |
76fe59c6fafb
fix empty string replacement in sub_filter
Igor Sysoev <igor@sysoev.ru>
parents:
1172
diff
changeset
|
452 } |
1172 | 453 |
454 *ctx->last_out = cl; | |
455 ctx->last_out = &cl->next; | |
456 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
457 ctx->index = 0; |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
458 ctx->once = slcf->once && (++ctx->applied == ctx->matches->nelts); |
1172 | 459 |
460 continue; | |
461 } | |
462 | |
5710
9e9c1310d17c
Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5288
diff
changeset
|
463 if (ctx->looked.len |
9e9c1310d17c
Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5288
diff
changeset
|
464 && (ctx->buf->last_buf || ctx->buf->last_in_chain)) |
9e9c1310d17c
Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5288
diff
changeset
|
465 { |
5287
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
466 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
467 if (cl == NULL) { |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
468 return NGX_ERROR; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
469 } |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
470 |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
471 b = cl->buf; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
472 |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
473 ngx_memzero(b, sizeof(ngx_buf_t)); |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
474 |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
475 b->pos = ctx->looked.data; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
476 b->last = b->pos + ctx->looked.len; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
477 b->memory = 1; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
478 |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
479 *ctx->last_out = cl; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
480 ctx->last_out = &cl->next; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
481 |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
482 ctx->looked.len = 0; |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
483 } |
2dbc5e38b65d
Sub filter: fixed incomplete last buffer on partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5286
diff
changeset
|
484 |
5710
9e9c1310d17c
Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5288
diff
changeset
|
485 if (ctx->buf->last_buf || ctx->buf->flush || ctx->buf->sync |
5286
819c5b53d8b5
Sub filter: flush buffers handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5285
diff
changeset
|
486 || ngx_buf_in_memory(ctx->buf)) |
819c5b53d8b5
Sub filter: flush buffers handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5285
diff
changeset
|
487 { |
1172 | 488 if (b == NULL) { |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
489 cl = ngx_chain_get_free_buf(r->pool, &ctx->free); |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
490 if (cl == NULL) { |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
491 return NGX_ERROR; |
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
492 } |
1172 | 493 |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
494 b = cl->buf; |
1172 | 495 |
5285
d47ef93134e5
Sub filter: switched to ngx_chain_get_free_buf().
Maxim Dounin <mdounin@mdounin.ru>
parents:
5284
diff
changeset
|
496 ngx_memzero(b, sizeof(ngx_buf_t)); |
1172 | 497 |
498 b->sync = 1; | |
499 | |
500 *ctx->last_out = cl; | |
501 ctx->last_out = &cl->next; | |
502 } | |
503 | |
504 b->last_buf = ctx->buf->last_buf; | |
5710
9e9c1310d17c
Sub filter: fixed subrequests handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5288
diff
changeset
|
505 b->last_in_chain = ctx->buf->last_in_chain; |
5286
819c5b53d8b5
Sub filter: flush buffers handling.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5285
diff
changeset
|
506 b->flush = ctx->buf->flush; |
1172 | 507 b->shadow = ctx->buf; |
508 | |
509 b->recycled = ctx->buf->recycled; | |
510 } | |
511 | |
512 ctx->buf = NULL; | |
513 } | |
514 | |
515 if (ctx->out == NULL && ctx->busy == NULL) { | |
516 return NGX_OK; | |
517 } | |
518 | |
519 return ngx_http_sub_output(r, ctx); | |
520 } | |
521 | |
522 | |
523 static ngx_int_t | |
524 ngx_http_sub_output(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx) | |
525 { | |
526 ngx_int_t rc; | |
527 ngx_buf_t *b; | |
528 ngx_chain_t *cl; | |
529 | |
530 #if 1 | |
531 b = NULL; | |
532 for (cl = ctx->out; cl; cl = cl->next) { | |
533 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
534 "sub out: %p %p", cl->buf, cl->buf->pos); | |
535 if (cl->buf == b) { | |
536 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
537 "the same buf was used in sub"); | |
538 ngx_debug_point(); | |
539 return NGX_ERROR; | |
540 } | |
541 b = cl->buf; | |
542 } | |
543 #endif | |
544 | |
545 rc = ngx_http_next_body_filter(r, ctx->out); | |
546 | |
547 if (ctx->busy == NULL) { | |
548 ctx->busy = ctx->out; | |
549 | |
550 } else { | |
551 for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ } | |
552 cl->next = ctx->out; | |
553 } | |
554 | |
555 ctx->out = NULL; | |
556 ctx->last_out = &ctx->out; | |
557 | |
558 while (ctx->busy) { | |
559 | |
560 cl = ctx->busy; | |
561 b = cl->buf; | |
562 | |
563 if (ngx_buf_size(b) != 0) { | |
564 break; | |
565 } | |
566 | |
567 if (b->shadow) { | |
568 b->shadow->pos = b->shadow->last; | |
569 } | |
570 | |
571 ctx->busy = cl->next; | |
572 | |
573 if (ngx_buf_in_memory(b) || b->in_file) { | |
574 /* add data bufs only to the free buf chain */ | |
575 | |
576 cl->next = ctx->free; | |
577 ctx->free = cl; | |
578 } | |
579 } | |
580 | |
581 if (ctx->in || ctx->buf) { | |
582 r->buffered |= NGX_HTTP_SUB_BUFFERED; | |
583 | |
584 } else { | |
585 r->buffered &= ~NGX_HTTP_SUB_BUFFERED; | |
586 } | |
587 | |
588 return rc; | |
589 } | |
590 | |
591 | |
592 static ngx_int_t | |
593 ngx_http_sub_parse(ngx_http_request_t *r, ngx_http_sub_ctx_t *ctx) | |
594 { | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
595 u_char *p, *last, *pat, *pat_end, c; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
596 ngx_str_t *m; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
597 ngx_int_t offset, start, next, end, len, rc; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
598 ngx_uint_t shift, i, j; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
599 ngx_http_sub_match_t *match; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
600 ngx_http_sub_tables_t *tables; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
601 ngx_http_sub_loc_conf_t *slcf; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
602 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
603 slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module); |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
604 tables = ctx->tables; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
605 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
606 offset = ctx->offset; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
607 end = ctx->buf->last - ctx->pos; |
1172 | 608 |
609 if (ctx->once) { | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
610 /* sets start and next to end */ |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
611 offset = end + (ngx_int_t) tables->min_match_len - 1; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
612 goto again; |
1172 | 613 } |
614 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
615 while (offset < end) { |
1172 | 616 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
617 c = offset < 0 ? ctx->looked.data[ctx->looked.len + offset] |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
618 : ctx->pos[offset]; |
5810
5322be87fc02
Sub filter: fixed matching for a single character.
Valentin Bartenev <vbart@nginx.com>
parents:
5733
diff
changeset
|
619 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
620 c = ngx_tolower(c); |
1172 | 621 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
622 shift = tables->shift[c]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
623 if (shift > 0) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
624 offset += shift; |
1172 | 625 continue; |
626 } | |
627 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
628 /* a potential match */ |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
629 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
630 start = offset - (ngx_int_t) tables->min_match_len + 1; |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
631 match = ctx->matches->elts; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
632 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
633 i = ngx_max(tables->index[c], ctx->index); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
634 j = tables->index[c + 1]; |
1172 | 635 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
636 while (i != j) { |
1172 | 637 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
638 if (slcf->once && ctx->sub && ctx->sub[i].data) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
639 goto next; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
640 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
641 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
642 m = &match[i].match; |
1557
9d094e581587
*) add sub_filter parser fix similar to r1261 in SSI parser
Igor Sysoev <igor@sysoev.ru>
parents:
1555
diff
changeset
|
643 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
644 pat = m->data; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
645 pat_end = m->data + m->len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
646 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
647 if (start >= 0) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
648 p = ctx->pos + start; |
1172 | 649 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
650 } else { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
651 last = ctx->looked.data + ctx->looked.len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
652 p = last + start; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
653 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
654 while (p < last && pat < pat_end) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
655 if (ngx_tolower(*p) != *pat) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
656 goto next; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
657 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
658 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
659 p++; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
660 pat++; |
1172 | 661 } |
662 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
663 p = ctx->pos; |
1172 | 664 } |
665 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
666 while (p < ctx->buf->last && pat < pat_end) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
667 if (ngx_tolower(*p) != *pat) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
668 goto next; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
669 } |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
670 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
671 p++; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
672 pat++; |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
673 } |
1172 | 674 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
675 ctx->index = i; |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
676 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
677 if (pat != pat_end) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
678 /* partial match */ |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
679 goto again; |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
680 } |
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
681 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
682 ctx->offset = offset + (ngx_int_t) m->len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
683 next = start + (ngx_int_t) m->len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
684 end = ngx_max(next, 0); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
685 rc = NGX_OK; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
686 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
687 goto done; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
688 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
689 next: |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
690 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
691 i++; |
1172 | 692 } |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
693 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
694 offset++; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
695 ctx->index = 0; |
1172 | 696 } |
697 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
698 again: |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
699 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
700 ctx->offset = offset; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
701 start = offset - (ngx_int_t) tables->min_match_len + 1; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
702 next = start; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
703 rc = NGX_AGAIN; |
5288
102d7117ffb8
Sub filter: fixed matching after a partial match.
Maxim Dounin <mdounin@mdounin.ru>
parents:
5287
diff
changeset
|
704 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
705 done: |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
706 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
707 /* send [ - looked.len, start ] to client */ |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
708 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
709 ctx->saved.len = ctx->looked.len + ngx_min(start, 0); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
710 ngx_memcpy(ctx->saved.data, ctx->looked.data, ctx->saved.len); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
711 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
712 ctx->copy_start = ctx->pos; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
713 ctx->copy_end = ctx->pos + ngx_max(start, 0); |
1172 | 714 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
715 /* save [ next, end ] in looked */ |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
716 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
717 len = ngx_min(next, 0); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
718 p = ctx->looked.data; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
719 p = ngx_movemem(p, p + ctx->looked.len + len, - len); |
1172 | 720 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
721 len = ngx_max(next, 0); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
722 p = ngx_cpymem(p, ctx->pos + len, end - len); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
723 ctx->looked.len = p - ctx->looked.data; |
1172 | 724 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
725 /* update position */ |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
726 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
727 ctx->pos += end; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
728 ctx->offset -= end; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
729 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
730 return rc; |
1172 | 731 } |
732 | |
733 | |
734 static char * | |
735 ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
736 { | |
737 ngx_http_sub_loc_conf_t *slcf = conf; | |
738 | |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
739 ngx_str_t *value; |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
740 ngx_http_sub_pair_t *pair; |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
741 ngx_http_compile_complex_value_t ccv; |
1172 | 742 |
743 value = cf->args->elts; | |
744 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
745 if (value[1].len == 0) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
746 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "empty search pattern"); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
747 return NGX_CONF_ERROR; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
748 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
749 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
750 if (slcf->pairs == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
751 slcf->pairs = ngx_array_create(cf->pool, 1, |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
752 sizeof(ngx_http_sub_pair_t)); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
753 if (slcf->pairs == NULL) { |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
754 return NGX_CONF_ERROR; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
755 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
756 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
757 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
758 if (slcf->pairs->nelts == 255) { |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
759 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
760 "number of search patterns exceeds 255"); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
761 return NGX_CONF_ERROR; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
762 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
763 |
2135 | 764 ngx_strlow(value[1].data, value[1].data, value[1].len); |
1172 | 765 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
766 pair = ngx_array_push(slcf->pairs); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
767 if (pair == NULL) { |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
768 return NGX_CONF_ERROR; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
769 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
770 |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
771 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
772 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
773 ccv.cf = cf; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
774 ccv.value = &value[1]; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
775 ccv.complex_value = &pair->match; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
776 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
777 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
778 return NGX_CONF_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
779 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
780 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
781 if (ccv.complex_value->lengths != NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
782 slcf->dynamic = 1; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
783 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
784 } else { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
785 ngx_strlow(pair->match.value.data, pair->match.value.data, |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
786 pair->match.value.len); |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
787 } |
1172 | 788 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
789 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
1172 | 790 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
791 ccv.cf = cf; |
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
792 ccv.value = &value[2]; |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
793 ccv.complex_value = &pair->value; |
1172 | 794 |
2588
a6954ce88b80
use complex values in add_header, auth_basic_user_file,
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
795 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
1172 | 796 return NGX_CONF_ERROR; |
797 } | |
798 | |
799 return NGX_CONF_OK; | |
800 } | |
801 | |
802 | |
803 static void * | |
804 ngx_http_sub_create_conf(ngx_conf_t *cf) | |
805 { | |
806 ngx_http_sub_loc_conf_t *slcf; | |
807 | |
808 slcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sub_loc_conf_t)); | |
809 if (slcf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2588
diff
changeset
|
810 return NULL; |
1172 | 811 } |
812 | |
813 /* | |
814 * set by ngx_pcalloc(): | |
815 * | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
816 * conf->dynamic = 0; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
817 * conf->pairs = NULL; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
818 * conf->tables = NULL; |
2166
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
819 * conf->types = { NULL }; |
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
820 * conf->types_keys = NULL; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
821 * conf->matches = NULL; |
1172 | 822 */ |
823 | |
824 slcf->once = NGX_CONF_UNSET; | |
5229
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
825 slcf->last_modified = NGX_CONF_UNSET; |
1172 | 826 |
827 return slcf; | |
828 } | |
829 | |
830 | |
831 static char * | |
832 ngx_http_sub_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
833 { | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
834 ngx_uint_t i, n; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
835 ngx_http_sub_pair_t *pairs; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
836 ngx_http_sub_match_t *matches; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
837 ngx_http_sub_loc_conf_t *prev = parent; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
838 ngx_http_sub_loc_conf_t *conf = child; |
1172 | 839 |
840 ngx_conf_merge_value(conf->once, prev->once, 1); | |
5229
4c1a604b0285
Sub filter: sub_filter_last_modified directive.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4947
diff
changeset
|
841 ngx_conf_merge_value(conf->last_modified, prev->last_modified, 0); |
1172 | 842 |
3372
6b8e5c882e47
support "*" in gzip_types, ssi_types, etc
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
843 if (ngx_http_merge_types(cf, &conf->types_keys, &conf->types, |
6b8e5c882e47
support "*" in gzip_types, ssi_types, etc
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
844 &prev->types_keys, &prev->types, |
2166
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
845 ngx_http_html_default_types) |
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
846 != NGX_OK) |
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
847 { |
723df5089c05
use hash in gzip_types, ssi_types, and sub_filter_types
Igor Sysoev <igor@sysoev.ru>
parents:
2135
diff
changeset
|
848 return NGX_CONF_ERROR; |
1172 | 849 } |
850 | |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
851 if (conf->pairs == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
852 conf->dynamic = prev->dynamic; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
853 conf->pairs = prev->pairs; |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
854 conf->matches = prev->matches; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
855 conf->tables = prev->tables; |
6247
fbbb1c1ce1eb
Sub filter: fixed initialization in http{} level (ticket #791).
Roman Arutyunyan <arut@nginx.com>
parents:
6229
diff
changeset
|
856 } |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
857 |
6247
fbbb1c1ce1eb
Sub filter: fixed initialization in http{} level (ticket #791).
Roman Arutyunyan <arut@nginx.com>
parents:
6229
diff
changeset
|
858 if (conf->pairs && conf->dynamic == 0 && conf->tables == NULL) { |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
859 pairs = conf->pairs->elts; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
860 n = conf->pairs->nelts; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
861 |
6464
088ef087a011
Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents:
6247
diff
changeset
|
862 matches = ngx_palloc(cf->pool, sizeof(ngx_http_sub_match_t) * n); |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
863 if (matches == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
864 return NGX_CONF_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
865 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
866 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
867 for (i = 0; i < n; i++) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
868 matches[i].match = pairs[i].match.value; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
869 matches[i].value = &pairs[i].value; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
870 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
871 |
6464
088ef087a011
Sub filter: fixed allocation alignment.
Roman Arutyunyan <arut@nginx.com>
parents:
6247
diff
changeset
|
872 conf->matches = ngx_palloc(cf->pool, sizeof(ngx_array_t)); |
6229
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
873 if (conf->matches == NULL) { |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
874 return NGX_CONF_ERROR; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
875 } |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
876 |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
877 conf->matches->elts = matches; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
878 conf->matches->nelts = n; |
2c045e5b8291
Sub filter: support of variables in the strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
6228
diff
changeset
|
879 |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
880 conf->tables = ngx_palloc(cf->pool, sizeof(ngx_http_sub_tables_t)); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
881 if (conf->tables == NULL) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
882 return NGX_CONF_ERROR; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
883 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
884 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
885 ngx_http_sub_init_tables(conf->tables, conf->matches->elts, |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
886 conf->matches->nelts); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
887 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
888 |
1172 | 889 return NGX_CONF_OK; |
890 } | |
891 | |
892 | |
6228
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
893 static void |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
894 ngx_http_sub_init_tables(ngx_http_sub_tables_t *tables, |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
895 ngx_http_sub_match_t *match, ngx_uint_t n) |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
896 { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
897 u_char c; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
898 ngx_uint_t i, j, min, max, ch; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
899 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
900 min = match[0].match.len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
901 max = match[0].match.len; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
902 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
903 for (i = 1; i < n; i++) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
904 min = ngx_min(min, match[i].match.len); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
905 max = ngx_max(max, match[i].match.len); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
906 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
907 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
908 tables->min_match_len = min; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
909 tables->max_match_len = max; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
910 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
911 ngx_http_sub_cmp_index = tables->min_match_len - 1; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
912 ngx_sort(match, n, sizeof(ngx_http_sub_match_t), ngx_http_sub_cmp_matches); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
913 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
914 min = ngx_min(min, 255); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
915 ngx_memset(tables->shift, min, 256); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
916 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
917 ch = 0; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
918 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
919 for (i = 0; i < n; i++) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
920 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
921 for (j = 0; j < min; j++) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
922 c = match[i].match.data[tables->min_match_len - 1 - j]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
923 tables->shift[c] = ngx_min(tables->shift[c], (u_char) j); |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
924 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
925 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
926 c = match[i].match.data[tables->min_match_len - 1]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
927 while (ch <= c) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
928 tables->index[ch++] = (u_char) i; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
929 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
930 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
931 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
932 while (ch < 257) { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
933 tables->index[ch++] = (u_char) n; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
934 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
935 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
936 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
937 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
938 static ngx_int_t |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
939 ngx_http_sub_cmp_matches(const void *one, const void *two) |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
940 { |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
941 ngx_int_t c1, c2; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
942 ngx_http_sub_match_t *first, *second; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
943 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
944 first = (ngx_http_sub_match_t *) one; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
945 second = (ngx_http_sub_match_t *) two; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
946 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
947 c1 = first->match.data[ngx_http_sub_cmp_index]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
948 c2 = second->match.data[ngx_http_sub_cmp_index]; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
949 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
950 return c1 - c2; |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
951 } |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
952 |
b9447fc457b4
Sub filter: support of multiple strings to replace.
Dmitry Volyntsev <xeioex@nginx.com>
parents:
5810
diff
changeset
|
953 |
1172 | 954 static ngx_int_t |
955 ngx_http_sub_filter_init(ngx_conf_t *cf) | |
956 { | |
957 ngx_http_next_header_filter = ngx_http_top_header_filter; | |
958 ngx_http_top_header_filter = ngx_http_sub_header_filter; | |
959 | |
960 ngx_http_next_body_filter = ngx_http_top_body_filter; | |
961 ngx_http_top_body_filter = ngx_http_sub_body_filter; | |
962 | |
963 return NGX_OK; | |
964 } |