Mercurial > hg > nginx-quic
annotate src/http/ngx_http_script.c @ 515:417a087c9c4d release-0.1.32
nginx-0.1.32-RELEASE import
*) Bugfix: the arguments were omitted in the redirects, issued by the
"rewrite" directive; the bug had appeared in 0.1.29.
*) Feature: the "if" directive supports the captures in regular
expressions.
*) Feature: the "set" directive supports the variables and the captures
of regular expressions.
*) Feature: the "X-Accel-Redirect" response header line is supported in
proxy and FastCGI mode.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 19 May 2005 13:25:22 +0000 |
parents | 9b8c906f6e63 |
children | dadfa78d2270 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
99
diff
changeset
|
5 |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_http.h> |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
509 | 12 #define ngx_http_script_exit (u_char *) &ngx_http_script_exit_code |
13 | |
14 static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL; | |
15 | |
16 | |
17 ngx_uint_t | |
18 ngx_http_script_variables_count(ngx_str_t *value) | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
19 { |
509 | 20 ngx_uint_t i, n; |
507 | 21 |
509 | 22 for (n = 0, i = 0; i < value->len; i++) { |
23 if (value->data[i] == '$') { | |
24 n++; | |
25 } | |
507 | 26 } |
27 | |
509 | 28 return n; |
29 } | |
30 | |
31 | |
32 ngx_int_t | |
33 ngx_http_script_compile(ngx_http_script_compile_t *sc) | |
34 { | |
35 u_char ch; | |
36 size_t size; | |
37 ngx_int_t index; | |
38 ngx_str_t name; | |
39 uintptr_t *code; | |
40 ngx_uint_t i, n, bracket; | |
41 ngx_http_script_var_code_t *var_code; | |
42 ngx_http_script_copy_code_t *copy; | |
43 ngx_http_script_copy_capture_code_t *copy_capture; | |
44 | |
45 if (*sc->lengths == NULL) { | |
46 n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t) | |
47 + sizeof(ngx_http_script_var_code_t)) | |
48 + sizeof(uintptr_t); | |
49 | |
50 *sc->lengths = ngx_array_create(sc->cf->pool, n, 1); | |
51 if (*sc->lengths == NULL) { | |
52 return NGX_ERROR; | |
53 } | |
54 } | |
55 | |
56 | |
57 if (*sc->values == NULL) { | |
58 n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t) | |
59 + sizeof(ngx_http_script_var_code_t)) | |
60 + sizeof(uintptr_t) | |
61 + sc->source->len | |
62 + sizeof(uintptr_t) - 1) | |
63 & ~(sizeof(uintptr_t) - 1); | |
64 | |
65 *sc->values = ngx_array_create(sc->cf->pool, n, 1); | |
66 if (*sc->values == NULL) { | |
507 | 67 return NGX_ERROR; |
68 } | |
69 } | |
70 | |
509 | 71 sc->variables = 0; |
72 | |
73 for (i = 0; i < sc->source->len; /* void */ ) { | |
74 | |
75 name.len = 0; | |
76 | |
77 if (sc->source->data[i] == '$') { | |
78 | |
79 if (++i == sc->source->len) { | |
80 goto invalid_variable; | |
81 } | |
82 | |
83 if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') { | |
84 | |
85 copy_capture = ngx_http_script_add_code(*sc->lengths, | |
86 sizeof(ngx_http_script_copy_capture_code_t), | |
87 NULL); | |
88 if (copy_capture == NULL) { | |
89 return NGX_ERROR; | |
90 } | |
91 | |
92 copy_capture->code = (ngx_http_script_code_pt) | |
93 ngx_http_script_copy_capture_len_code; | |
94 copy_capture->n = 2 * (sc->source->data[i] - '0'); | |
95 | |
96 copy_capture = ngx_http_script_add_code(*sc->values, | |
97 sizeof(ngx_http_script_copy_capture_code_t), | |
98 &sc->main); | |
99 if (copy_capture == NULL) { | |
100 return NGX_ERROR; | |
101 } | |
102 | |
103 copy_capture->code = ngx_http_script_copy_capture_code; | |
104 copy_capture->n = sc->source->data[i] - '0'; | |
105 | |
106 if (sc->ncaptures < copy_capture->n) { | |
107 sc->ncaptures = copy_capture->n; | |
108 } | |
109 | |
110 copy_capture->n *= 2; | |
507 | 111 |
509 | 112 i++; |
113 | |
114 continue; | |
115 } | |
116 | |
117 if (sc->source->data[i] == '{') { | |
118 bracket = 1; | |
119 | |
120 if (++i == sc->source->len) { | |
121 goto invalid_variable; | |
122 } | |
123 | |
124 name.data = &sc->source->data[i]; | |
125 | |
126 } else { | |
127 bracket = 0; | |
128 name.data = &sc->source->data[i]; | |
129 } | |
130 | |
131 for ( /* void */ ; i < sc->source->len; i++, name.len++) { | |
132 ch = sc->source->data[i]; | |
507 | 133 |
509 | 134 if (ch == '}' && bracket) { |
135 i++; | |
136 bracket = 0; | |
137 break; | |
138 } | |
139 | |
140 if ((ch >= 'A' && ch <= 'Z') | |
141 || (ch >= 'a' && ch <= 'z') | |
142 || (ch >= '0' && ch <= '9') | |
143 || ch == '_') | |
144 { | |
145 continue; | |
146 } | |
147 | |
148 break; | |
149 } | |
150 | |
151 if (bracket) { | |
152 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, | |
153 "the closing bracket in \"%V\" " | |
154 "variable is missing", &name); | |
507 | 155 return NGX_ERROR; |
156 } | |
157 | |
509 | 158 if (name.len == 0) { |
159 goto invalid_variable; | |
160 } | |
507 | 161 |
509 | 162 sc->variables++; |
507 | 163 |
509 | 164 index = ngx_http_get_variable_index(sc->cf, &name); |
165 | |
166 if (index == NGX_ERROR) { | |
507 | 167 return NGX_ERROR; |
168 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
169 |
509 | 170 var_code = ngx_http_script_add_code(*sc->lengths, |
171 sizeof(ngx_http_script_var_code_t), | |
172 NULL); | |
507 | 173 if (var_code == NULL) { |
174 return NGX_ERROR; | |
175 } | |
176 | |
177 var_code->code = (ngx_http_script_code_pt) | |
509 | 178 ngx_http_script_copy_var_len_code; |
179 var_code->index = (uintptr_t) index; | |
507 | 180 |
181 | |
509 | 182 var_code = ngx_http_script_add_code(*sc->values, |
183 sizeof(ngx_http_script_var_code_t), | |
184 &sc->main); | |
507 | 185 if (var_code == NULL) { |
186 return NGX_ERROR; | |
187 } | |
188 | |
509 | 189 var_code->code = ngx_http_script_copy_var_code; |
190 var_code->index = (uintptr_t) index; | |
191 | |
192 continue; | |
193 } | |
507 | 194 |
509 | 195 if (sc->source->data[i] == '?' && sc->compile_args) { |
196 sc->args = 1; | |
197 sc->compile_args = 0; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
198 |
509 | 199 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), |
200 &sc->main); | |
201 if (code == NULL) { | |
507 | 202 return NGX_ERROR; |
203 } | |
204 | |
509 | 205 *code = (uintptr_t) ngx_http_script_start_args_code; |
206 | |
207 i++; | |
208 | |
507 | 209 continue; |
210 } | |
211 | |
509 | 212 name.data = &sc->source->data[i]; |
213 | |
214 while (i < sc->source->len | |
215 && sc->source->data[i] != '$' | |
216 && !(sc->source->data[i] == '?' && sc->compile_args)) | |
217 { | |
218 i++; | |
219 name.len++; | |
220 } | |
221 | |
222 sc->size += name.len; | |
223 | |
224 copy = ngx_http_script_add_code(*sc->lengths, | |
225 sizeof(ngx_http_script_copy_code_t), | |
226 NULL); | |
227 if (copy == NULL) { | |
507 | 228 return NGX_ERROR; |
229 } | |
509 | 230 |
231 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code; | |
232 copy->len = name.len; | |
233 | |
234 size = (sizeof(ngx_http_script_copy_code_t) + name.len | |
235 + sizeof(uintptr_t) - 1) | |
236 & ~(sizeof(uintptr_t) - 1); | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
237 |
509 | 238 copy = ngx_http_script_add_code(*sc->values, size, &sc->main); |
239 if (copy == NULL) { | |
240 return NGX_ERROR; | |
241 } | |
242 | |
243 copy->code = ngx_http_script_copy_code; | |
244 copy->len = name.len; | |
245 | |
246 ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t), | |
247 name.data, name.len); | |
507 | 248 } |
249 | |
509 | 250 if (sc->complete_lengths) { |
251 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL); | |
252 if (code == NULL) { | |
253 return NGX_ERROR; | |
254 } | |
507 | 255 |
509 | 256 *code = (uintptr_t) NULL; |
507 | 257 } |
258 | |
509 | 259 if (sc->complete_values) { |
260 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), | |
261 &sc->main); | |
262 if (code == NULL) { | |
263 return NGX_ERROR; | |
264 } | |
265 | |
266 *code = (uintptr_t) NULL; | |
267 } | |
507 | 268 |
269 return NGX_OK; | |
509 | 270 |
271 invalid_variable: | |
272 | |
273 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name"); | |
274 | |
275 return NGX_ERROR; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
276 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
277 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
278 |
509 | 279 void * |
507 | 280 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size) |
479 | 281 { |
507 | 282 if (*codes == NULL) { |
283 *codes = ngx_array_create(pool, 256, 1); | |
284 if (*codes == NULL) { | |
285 return NULL; | |
286 } | |
287 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
288 |
507 | 289 return ngx_array_push_n(*codes, size); |
290 } | |
291 | |
509 | 292 |
293 void * | |
294 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code) | |
295 { | |
296 u_char *elts, **p; | |
297 void *new; | |
298 | |
299 elts = codes->elts; | |
300 | |
301 new = ngx_array_push_n(codes, size); | |
302 if (new == NULL) { | |
303 return NGX_CONF_ERROR; | |
304 } | |
305 | |
306 if (code) { | |
307 if (elts != codes->elts) { | |
308 p = code; | |
309 *p += (u_char *) codes->elts - elts; | |
310 } | |
311 } | |
312 | |
313 return new; | |
314 } | |
507 | 315 |
479 | 316 |
507 | 317 size_t |
509 | 318 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e) |
507 | 319 { |
320 ngx_http_script_copy_code_t *code; | |
479 | 321 |
509 | 322 code = (ngx_http_script_copy_code_t *) e->ip; |
507 | 323 |
509 | 324 e->ip += sizeof(ngx_http_script_copy_code_t); |
507 | 325 |
326 return code->len; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
327 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
328 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
329 |
507 | 330 void |
509 | 331 ngx_http_script_copy_code(ngx_http_script_engine_t *e) |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
332 { |
507 | 333 ngx_http_script_copy_code_t *code; |
334 | |
509 | 335 code = (ngx_http_script_copy_code_t *) e->ip; |
336 | |
337 if (!e->skip) { | |
338 e->pos = ngx_cpymem(e->pos, e->ip + sizeof(ngx_http_script_copy_code_t), | |
339 code->len); | |
340 } | |
507 | 341 |
509 | 342 e->ip += sizeof(ngx_http_script_copy_code_t) |
343 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1)); | |
507 | 344 |
509 | 345 if (e->log) { |
346 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
347 "http script copy: \"%V\"", &e->buf); | |
348 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
349 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
350 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
351 |
507 | 352 size_t |
509 | 353 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e) |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
354 { |
507 | 355 ngx_http_variable_value_t *value; |
356 ngx_http_script_var_code_t *code; | |
357 | |
509 | 358 code = (ngx_http_script_var_code_t *) e->ip; |
507 | 359 |
509 | 360 e->ip += sizeof(ngx_http_script_var_code_t); |
507 | 361 |
509 | 362 value = ngx_http_get_indexed_variable(e->request, code->index); |
507 | 363 |
509 | 364 if (value == NULL || value == NGX_HTTP_VAR_NOT_FOUND) { |
507 | 365 return 0; |
366 } | |
367 | |
368 return value->text.len; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
369 } |
479 | 370 |
371 | |
507 | 372 void |
509 | 373 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e) |
479 | 374 { |
507 | 375 ngx_http_variable_value_t *value; |
376 ngx_http_script_var_code_t *code; | |
377 | |
509 | 378 code = (ngx_http_script_var_code_t *) e->ip; |
379 | |
380 e->ip += sizeof(ngx_http_script_var_code_t); | |
381 | |
382 if (!e->skip) { | |
383 value = ngx_http_get_indexed_variable(e->request, code->index); | |
384 | |
385 if (value == NULL || value == NGX_HTTP_VAR_NOT_FOUND) { | |
386 return; | |
387 } | |
388 | |
389 e->pos = ngx_cpymem(e->pos, value->text.data, value->text.len); | |
390 | |
391 if (e->log) { | |
392 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
393 "http script var: \"%V\"", &e->buf); | |
394 } | |
395 } | |
396 } | |
397 | |
398 | |
399 size_t | |
400 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e) | |
401 { | |
402 ngx_http_script_copy_capture_code_t *code; | |
403 | |
404 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
405 | |
406 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
407 | |
515 | 408 if (code->n < e->ncaptures) { |
409 if ((e->args || e->quote) | |
410 && (e->request->quoted_uri || e->request->plus_in_uri)) | |
411 { | |
412 return e->captures[code->n + 1] - e->captures[code->n] | |
413 + ngx_escape_uri(NULL, | |
414 &e->line.data[e->captures[code->n]], | |
509 | 415 e->captures[code->n + 1] - e->captures[code->n], |
416 NGX_ESCAPE_ARGS); | |
515 | 417 } else { |
418 return e->captures[code->n + 1] - e->captures[code->n]; | |
419 } | |
509 | 420 } |
515 | 421 |
422 return 0; | |
509 | 423 } |
424 | |
425 | |
426 void | |
427 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e) | |
428 { | |
429 ngx_http_script_copy_capture_code_t *code; | |
430 | |
431 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
432 | |
433 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
507 | 434 |
515 | 435 if (code->n < e->ncaptures) { |
436 if ((e->args || e->quote) | |
437 && (e->request->quoted_uri || e->request->plus_in_uri)) | |
438 { | |
439 e->pos = (u_char *) ngx_escape_uri(e->pos, | |
440 &e->line.data[e->captures[code->n]], | |
509 | 441 e->captures[code->n + 1] - e->captures[code->n], |
442 NGX_ESCAPE_ARGS); | |
515 | 443 } else { |
444 e->pos = ngx_cpymem(e->pos, | |
445 &e->line.data[e->captures[code->n]], | |
446 e->captures[code->n + 1] - e->captures[code->n]); | |
447 } | |
509 | 448 } |
449 | |
450 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
451 "http script capture: \"%V\"", &e->buf); | |
452 } | |
453 | |
454 | |
455 void | |
456 ngx_http_script_start_args_code(ngx_http_script_engine_t *e) | |
457 { | |
458 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
459 "http script args"); | |
460 | |
461 e->args = e->pos; | |
462 e->ip += sizeof(uintptr_t); | |
463 } | |
464 | |
465 | |
466 void | |
467 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e) | |
468 { | |
469 size_t len; | |
470 ngx_int_t rc; | |
471 ngx_uint_t n; | |
472 ngx_http_request_t *r; | |
473 ngx_http_script_engine_t le; | |
474 ngx_http_script_len_code_pt lcode; | |
475 ngx_http_script_regex_code_t *code; | |
476 | |
477 code = (ngx_http_script_regex_code_t *) e->ip; | |
478 | |
479 r = e->request; | |
480 | |
481 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
482 "http script regex: \"%V\"", &code->name); | |
483 | |
484 if (code->uri) { | |
515 | 485 e->line = r->uri; |
509 | 486 } else { |
487 e->sp--; | |
515 | 488 e->line = e->sp->text; |
509 | 489 } |
507 | 490 |
515 | 491 rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures); |
509 | 492 |
493 if (rc == NGX_REGEX_NO_MATCHED) { | |
494 if (e->log) { | |
495 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
515 | 496 "\"%V\" does not match \"%V\"", |
497 &code->name, &e->line); | |
509 | 498 } |
499 | |
515 | 500 e->ncaptures = 0; |
501 | |
509 | 502 if (code->test) { |
503 e->sp->value = 0; | |
504 e->sp->text.len = 0; | |
505 e->sp->text.data = (u_char *) ""; | |
506 e->sp++; | |
507 | |
508 e->ip += sizeof(ngx_http_script_regex_code_t); | |
509 return; | |
510 } | |
511 | |
512 e->ip += code->next; | |
513 return; | |
514 } | |
515 | |
516 if (rc < 0) { | |
517 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
518 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"", | |
515 | 519 rc, &e->line, &code->name); |
509 | 520 |
521 e->ip = ngx_http_script_exit; | |
522 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
523 return; | |
524 } | |
525 | |
526 if (e->log) { | |
527 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
515 | 528 "\"%V\" matches \"%V\"", &code->name, &e->line); |
509 | 529 } |
530 | |
515 | 531 e->ncaptures = code->ncaptures; |
532 | |
509 | 533 if (code->test) { |
534 e->sp->value = 1; | |
535 e->sp->text.len = 1; | |
536 e->sp->text.data = (u_char *) "1"; | |
537 e->sp++; | |
538 | |
539 e->ip += sizeof(ngx_http_script_regex_code_t); | |
540 return; | |
541 } | |
542 | |
543 if (code->status) { | |
544 e->status = code->status; | |
507 | 545 |
509 | 546 if (!code->redirect) { |
547 e->ip = ngx_http_script_exit; | |
548 return; | |
549 } | |
550 } | |
551 | |
552 if (code->uri) { | |
553 r->internal = 1; | |
554 r->valid_unparsed_uri = 0; | |
555 | |
556 if (code->break_cycle) { | |
557 r->valid_location = 0; | |
558 | |
559 } else { | |
560 r->uri_changed = 1; | |
561 } | |
562 } | |
563 | |
564 if (code->lengths == NULL) { | |
565 e->buf.len = code->size; | |
566 | |
567 if (code->uri) { | |
568 if (rc && (r->quoted_uri || r->plus_in_uri)) { | |
569 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, | |
570 NGX_ESCAPE_ARGS); | |
571 } | |
572 } | |
573 | |
574 for (n = 1; n < (ngx_uint_t) rc; n++) { | |
575 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n]; | |
576 } | |
577 | |
578 } else { | |
579 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
580 | |
581 le.ip = code->lengths->elts; | |
582 le.request = r; | |
583 le.captures = e->captures; | |
515 | 584 le.ncaptures = e->ncaptures; |
509 | 585 |
586 len = 1; /* reserve 1 byte for possible "?" */ | |
587 | |
588 while (*(uintptr_t *) le.ip) { | |
589 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
590 len += lcode(&le); | |
591 } | |
592 | |
593 e->buf.len = len; | |
594 } | |
595 | |
515 | 596 if (code->add_args && r->args.len) { |
509 | 597 e->buf.len += r->args.len + 1; |
598 } | |
599 | |
600 e->buf.data = ngx_palloc(r->pool, e->buf.len); | |
601 if (e->buf.data == NULL) { | |
602 e->ip = ngx_http_script_exit; | |
603 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
507 | 604 return; |
605 } | |
606 | |
509 | 607 e->quote = code->redirect; |
608 | |
609 e->pos = e->buf.data; | |
610 | |
611 e->ip += sizeof(ngx_http_script_regex_code_t); | |
612 } | |
613 | |
614 | |
615 void | |
616 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e) | |
617 { | |
618 ngx_http_request_t *r; | |
619 ngx_http_script_regex_end_code_t *code; | |
620 | |
621 code = (ngx_http_script_regex_end_code_t *) e->ip; | |
622 | |
623 r = e->request; | |
624 | |
625 e->quote = 0; | |
626 | |
627 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
628 "http script regex end"); | |
629 | |
630 if (code->redirect) { | |
631 | |
632 if (code->add_args && r->args.len) { | |
633 *e->pos++ = (u_char) (code->args ? '&' : '?'); | |
634 e->pos = ngx_cpymem(e->pos, r->args.data, r->args.len); | |
635 } | |
636 | |
637 e->buf.len = e->pos - e->buf.data; | |
638 | |
639 if (e->log) { | |
640 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
641 "rewritten redirect: \"%V\"", &e->buf); | |
642 } | |
643 | |
644 r->headers_out.location = ngx_list_push(&r->headers_out.headers); | |
645 if (r->headers_out.location == NULL) { | |
646 e->ip = ngx_http_script_exit; | |
647 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
648 return; | |
649 } | |
650 | |
651 r->headers_out.location->hash = 1; | |
652 r->headers_out.location->key.len = sizeof("Location") - 1; | |
653 r->headers_out.location->key.data = (u_char *) "Location"; | |
654 r->headers_out.location->value = e->buf; | |
655 | |
656 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
657 return; | |
658 } | |
659 | |
660 if (e->args) { | |
661 e->buf.len = e->args - e->buf.data; | |
662 | |
663 if (code->add_args && r->args.len) { | |
664 *e->pos++ = '&'; | |
665 e->pos = ngx_cpymem(e->pos, r->args.data, r->args.len); | |
666 } | |
667 | |
668 r->args.len = e->pos - e->args; | |
669 r->args.data = e->args; | |
670 | |
671 e->args = NULL; | |
672 | |
673 } else { | |
674 e->buf.len = e->pos - e->buf.data; | |
675 } | |
676 | |
677 if (e->log) { | |
678 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
679 "rewritten data: \"%V\", args: \"%V\"", | |
680 &e->buf, &r->args); | |
681 } | |
682 | |
683 if (code->uri) { | |
684 r->uri = e->buf; | |
685 | |
686 if (ngx_http_set_exten(r) != NGX_OK) { | |
687 e->ip = ngx_http_script_exit; | |
688 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
689 return; | |
690 } | |
691 } | |
692 | |
693 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
694 } | |
695 | |
696 | |
697 void | |
698 ngx_http_script_return_code(ngx_http_script_engine_t *e) | |
699 { | |
700 ngx_http_script_return_code_t *code; | |
701 | |
702 code = (ngx_http_script_return_code_t *) e->ip; | |
703 | |
704 e->status = code->status; | |
705 | |
706 e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t); | |
479 | 707 } |
509 | 708 |
709 | |
710 void | |
711 ngx_http_script_if_code(ngx_http_script_engine_t *e) | |
712 { | |
713 ngx_http_script_if_code_t *code; | |
714 | |
715 code = (ngx_http_script_if_code_t *) e->ip; | |
716 | |
717 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
718 "http script if"); | |
719 | |
720 e->sp--; | |
721 | |
722 if (e->sp->value) { | |
723 if (code->loc_conf) { | |
724 e->request->loc_conf = code->loc_conf; | |
725 } | |
726 | |
727 e->ip += sizeof(ngx_http_script_if_code_t); | |
728 return; | |
729 } | |
730 | |
731 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
732 "http script if false"); | |
733 | |
734 e->ip += code->next; | |
735 } | |
736 | |
737 | |
738 void | |
515 | 739 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e) |
740 { | |
741 size_t len; | |
742 ngx_http_script_engine_t le; | |
743 ngx_http_script_len_code_pt lcode; | |
744 ngx_http_script_complex_value_code_t *code; | |
745 | |
746 code = (ngx_http_script_complex_value_code_t *) e->ip; | |
747 | |
748 e->ip += sizeof(ngx_http_script_complex_value_code_t); | |
749 | |
750 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
751 "http script complex value"); | |
752 | |
753 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
754 | |
755 le.ip = code->lengths->elts; | |
756 le.request = e->request; | |
757 le.captures = e->captures; | |
758 le.ncaptures = e->ncaptures; | |
759 | |
760 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { | |
761 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
762 } | |
763 | |
764 e->buf.len = len; | |
765 e->buf.data = ngx_palloc(e->request->pool, len); | |
766 if (e->buf.data == NULL) { | |
767 e->ip = ngx_http_script_exit; | |
768 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
769 return; | |
770 } | |
771 | |
772 e->pos = e->buf.data; | |
773 | |
774 e->sp->value = 0; | |
775 e->sp->text = e->buf; | |
776 e->sp++; | |
777 } | |
778 | |
779 | |
780 void | |
509 | 781 ngx_http_script_value_code(ngx_http_script_engine_t *e) |
782 { | |
783 ngx_http_script_value_code_t *code; | |
784 | |
785 code = (ngx_http_script_value_code_t *) e->ip; | |
786 | |
787 e->ip += sizeof(ngx_http_script_value_code_t); | |
788 | |
789 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
790 "http script value"); | |
791 | |
792 e->sp->value = (ngx_uint_t) code->value; | |
793 e->sp->text.len = (size_t) code->text_len; | |
794 e->sp->text.data = (u_char *) code->text_data; | |
795 e->sp++; | |
796 } | |
797 | |
798 | |
799 void | |
800 ngx_http_script_set_var_code(ngx_http_script_engine_t *e) | |
801 { | |
802 ngx_http_request_t *r; | |
803 ngx_http_variable_value_t *value; | |
804 ngx_http_core_main_conf_t *cmcf; | |
805 ngx_http_script_var_code_t *code; | |
806 | |
807 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
808 "http script set var"); | |
809 | |
810 code = (ngx_http_script_var_code_t *) e->ip; | |
811 | |
812 e->ip += sizeof(ngx_http_script_var_code_t); | |
813 | |
814 r = e->request; | |
815 | |
816 if (r->variables == NULL) { | |
817 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
818 | |
819 r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts | |
820 * sizeof(ngx_http_variable_value_t *)); | |
821 if (r->variables == NULL) { | |
822 e->ip = ngx_http_script_exit; | |
823 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
824 return; | |
825 } | |
826 } | |
827 | |
828 value = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); | |
829 if (value == NULL) { | |
830 e->ip = ngx_http_script_exit; | |
831 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
832 return; | |
833 } | |
834 | |
835 e->sp--; | |
836 | |
837 *value = *e->sp; | |
838 | |
839 r->variables[code->index] = value; | |
840 } | |
841 | |
842 | |
843 void | |
844 ngx_http_script_var_code(ngx_http_script_engine_t *e) | |
845 { | |
846 ngx_http_variable_value_t *value; | |
847 ngx_http_script_var_code_t *code; | |
848 | |
849 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
850 "http script var"); | |
851 | |
852 code = (ngx_http_script_var_code_t *) e->ip; | |
853 | |
854 e->ip += sizeof(ngx_http_script_var_code_t); | |
855 | |
856 value = ngx_http_get_indexed_variable(e->request, code->index); | |
857 | |
858 if (value == NULL || value == NGX_HTTP_VAR_NOT_FOUND) { | |
859 e->sp->value = 0; | |
860 e->sp->text.len = 0; | |
861 e->sp->text.data = (u_char *) ""; | |
862 e->sp++; | |
863 | |
864 return; | |
865 } | |
866 | |
867 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
868 "http script var: %ui, \"%V\"", value->value, &value->text); | |
869 | |
870 *e->sp = *value; | |
871 e->sp++; | |
872 } | |
873 | |
874 | |
875 void | |
876 ngx_http_script_nop_code(ngx_http_script_engine_t *e) | |
877 { | |
878 e->ip += sizeof(uintptr_t); | |
879 } |