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