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