Mercurial > hg > nginx-quic
annotate src/http/ngx_http_script.c @ 925:200bbb76204a
release-0.5.4 tag
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 14 Dec 2006 23:14:12 +0000 |
parents | eb5cf273e5d2 |
children | a9715f8f17a5 |
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 | |
641 | 94 n = sc->source->data[i] - '0'; |
95 | |
96 if (sc->captures_mask & (1 << n)) { | |
97 sc->dup_capture = 1; | |
98 } | |
99 | |
100 sc->captures_mask |= 1 << n; | |
101 | |
509 | 102 copy_capture = ngx_http_script_add_code(*sc->lengths, |
103 sizeof(ngx_http_script_copy_capture_code_t), | |
104 NULL); | |
105 if (copy_capture == NULL) { | |
106 return NGX_ERROR; | |
107 } | |
108 | |
109 copy_capture->code = (ngx_http_script_code_pt) | |
110 ngx_http_script_copy_capture_len_code; | |
641 | 111 copy_capture->n = 2 * n; |
112 | |
509 | 113 |
114 copy_capture = ngx_http_script_add_code(*sc->values, | |
115 sizeof(ngx_http_script_copy_capture_code_t), | |
116 &sc->main); | |
117 if (copy_capture == NULL) { | |
118 return NGX_ERROR; | |
119 } | |
120 | |
121 copy_capture->code = ngx_http_script_copy_capture_code; | |
641 | 122 copy_capture->n = 2 * n; |
509 | 123 |
641 | 124 if (sc->ncaptures < n) { |
125 sc->ncaptures = n; | |
509 | 126 } |
127 | |
128 i++; | |
129 | |
130 continue; | |
131 } | |
132 | |
133 if (sc->source->data[i] == '{') { | |
134 bracket = 1; | |
135 | |
136 if (++i == sc->source->len) { | |
137 goto invalid_variable; | |
138 } | |
139 | |
140 name.data = &sc->source->data[i]; | |
141 | |
142 } else { | |
143 bracket = 0; | |
144 name.data = &sc->source->data[i]; | |
145 } | |
146 | |
147 for ( /* void */ ; i < sc->source->len; i++, name.len++) { | |
148 ch = sc->source->data[i]; | |
507 | 149 |
509 | 150 if (ch == '}' && bracket) { |
151 i++; | |
152 bracket = 0; | |
153 break; | |
154 } | |
155 | |
156 if ((ch >= 'A' && ch <= 'Z') | |
157 || (ch >= 'a' && ch <= 'z') | |
158 || (ch >= '0' && ch <= '9') | |
159 || ch == '_') | |
160 { | |
161 continue; | |
162 } | |
163 | |
164 break; | |
165 } | |
166 | |
167 if (bracket) { | |
168 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, | |
169 "the closing bracket in \"%V\" " | |
170 "variable is missing", &name); | |
507 | 171 return NGX_ERROR; |
172 } | |
173 | |
509 | 174 if (name.len == 0) { |
175 goto invalid_variable; | |
176 } | |
507 | 177 |
509 | 178 sc->variables++; |
507 | 179 |
509 | 180 index = ngx_http_get_variable_index(sc->cf, &name); |
181 | |
182 if (index == NGX_ERROR) { | |
507 | 183 return NGX_ERROR; |
184 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
185 |
573 | 186 if (sc->flushes) { |
187 p = ngx_array_push(*sc->flushes); | |
188 if (p == NULL) { | |
189 return NGX_ERROR; | |
190 } | |
191 | |
192 *p = index; | |
193 } | |
194 | |
509 | 195 var_code = ngx_http_script_add_code(*sc->lengths, |
196 sizeof(ngx_http_script_var_code_t), | |
197 NULL); | |
507 | 198 if (var_code == NULL) { |
199 return NGX_ERROR; | |
200 } | |
201 | |
202 var_code->code = (ngx_http_script_code_pt) | |
509 | 203 ngx_http_script_copy_var_len_code; |
204 var_code->index = (uintptr_t) index; | |
507 | 205 |
206 | |
509 | 207 var_code = ngx_http_script_add_code(*sc->values, |
208 sizeof(ngx_http_script_var_code_t), | |
209 &sc->main); | |
507 | 210 if (var_code == NULL) { |
211 return NGX_ERROR; | |
212 } | |
213 | |
509 | 214 var_code->code = ngx_http_script_copy_var_code; |
215 var_code->index = (uintptr_t) index; | |
216 | |
217 continue; | |
218 } | |
507 | 219 |
509 | 220 if (sc->source->data[i] == '?' && sc->compile_args) { |
221 sc->args = 1; | |
222 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
|
223 |
509 | 224 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), |
225 &sc->main); | |
226 if (code == NULL) { | |
507 | 227 return NGX_ERROR; |
228 } | |
229 | |
509 | 230 *code = (uintptr_t) ngx_http_script_start_args_code; |
231 | |
232 i++; | |
233 | |
507 | 234 continue; |
235 } | |
236 | |
509 | 237 name.data = &sc->source->data[i]; |
238 | |
239 while (i < sc->source->len | |
240 && sc->source->data[i] != '$' | |
241 && !(sc->source->data[i] == '?' && sc->compile_args)) | |
242 { | |
243 i++; | |
244 name.len++; | |
245 } | |
246 | |
247 sc->size += name.len; | |
248 | |
249 copy = ngx_http_script_add_code(*sc->lengths, | |
250 sizeof(ngx_http_script_copy_code_t), | |
251 NULL); | |
252 if (copy == NULL) { | |
507 | 253 return NGX_ERROR; |
254 } | |
509 | 255 |
256 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code; | |
257 copy->len = name.len; | |
258 | |
259 size = (sizeof(ngx_http_script_copy_code_t) + name.len | |
260 + sizeof(uintptr_t) - 1) | |
261 & ~(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
|
262 |
509 | 263 copy = ngx_http_script_add_code(*sc->values, size, &sc->main); |
264 if (copy == NULL) { | |
265 return NGX_ERROR; | |
266 } | |
267 | |
268 copy->code = ngx_http_script_copy_code; | |
269 copy->len = name.len; | |
270 | |
271 ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t), | |
272 name.data, name.len); | |
507 | 273 } |
274 | |
509 | 275 if (sc->complete_lengths) { |
276 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL); | |
277 if (code == NULL) { | |
278 return NGX_ERROR; | |
279 } | |
507 | 280 |
509 | 281 *code = (uintptr_t) NULL; |
507 | 282 } |
283 | |
509 | 284 if (sc->complete_values) { |
285 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), | |
286 &sc->main); | |
287 if (code == NULL) { | |
288 return NGX_ERROR; | |
289 } | |
290 | |
291 *code = (uintptr_t) NULL; | |
292 } | |
507 | 293 |
294 return NGX_OK; | |
509 | 295 |
296 invalid_variable: | |
297 | |
298 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name"); | |
299 | |
300 return NGX_ERROR; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
301 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
302 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
303 |
623 | 304 u_char * |
305 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value, | |
306 void *code_lengths, size_t len, void *code_values) | |
307 { | |
308 ngx_http_script_code_pt code; | |
309 ngx_http_script_len_code_pt lcode; | |
310 ngx_http_script_engine_t e; | |
311 | |
312 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
313 | |
314 e.ip = code_lengths; | |
315 e.request = r; | |
316 e.flushed = 1; | |
317 | |
318 while (*(uintptr_t *) e.ip) { | |
319 lcode = *(ngx_http_script_len_code_pt *) e.ip; | |
320 len += lcode(&e); | |
321 } | |
322 | |
323 | |
324 value->len = len; | |
325 value->data = ngx_palloc(r->pool, len); | |
326 if (value->data == NULL) { | |
327 return NULL; | |
328 } | |
329 | |
330 e.ip = code_values; | |
331 e.pos = value->data; | |
332 | |
333 while (*(uintptr_t *) e.ip) { | |
334 code = *(ngx_http_script_code_pt *) e.ip; | |
335 code((ngx_http_script_engine_t *) &e); | |
336 } | |
337 | |
338 return e.pos; | |
339 } | |
340 | |
341 | |
573 | 342 void |
343 ngx_http_script_flush_no_cachable_variables(ngx_http_request_t *r, | |
344 ngx_array_t *indices) | |
345 { | |
346 ngx_uint_t n, *index; | |
347 | |
348 if (indices) { | |
349 index = indices->elts; | |
350 for (n = 0; n < indices->nelts; n++) { | |
351 if (r->variables[index[n]].no_cachable) { | |
352 r->variables[index[n]].valid = 0; | |
353 r->variables[index[n]].not_found = 0; | |
354 } | |
355 } | |
356 } | |
357 } | |
358 | |
359 | |
509 | 360 void * |
507 | 361 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size) |
479 | 362 { |
507 | 363 if (*codes == NULL) { |
364 *codes = ngx_array_create(pool, 256, 1); | |
365 if (*codes == NULL) { | |
366 return NULL; | |
367 } | |
368 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
369 |
507 | 370 return ngx_array_push_n(*codes, size); |
371 } | |
372 | |
509 | 373 |
374 void * | |
375 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code) | |
376 { | |
377 u_char *elts, **p; | |
378 void *new; | |
379 | |
380 elts = codes->elts; | |
381 | |
382 new = ngx_array_push_n(codes, size); | |
383 if (new == NULL) { | |
384 return NGX_CONF_ERROR; | |
385 } | |
386 | |
387 if (code) { | |
388 if (elts != codes->elts) { | |
389 p = code; | |
390 *p += (u_char *) codes->elts - elts; | |
391 } | |
392 } | |
393 | |
394 return new; | |
395 } | |
507 | 396 |
479 | 397 |
507 | 398 size_t |
509 | 399 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e) |
507 | 400 { |
401 ngx_http_script_copy_code_t *code; | |
479 | 402 |
509 | 403 code = (ngx_http_script_copy_code_t *) e->ip; |
507 | 404 |
509 | 405 e->ip += sizeof(ngx_http_script_copy_code_t); |
507 | 406 |
407 return code->len; | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
408 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
409 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
410 |
507 | 411 void |
509 | 412 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
|
413 { |
507 | 414 ngx_http_script_copy_code_t *code; |
415 | |
509 | 416 code = (ngx_http_script_copy_code_t *) e->ip; |
417 | |
418 if (!e->skip) { | |
573 | 419 e->pos = ngx_copy(e->pos, e->ip + sizeof(ngx_http_script_copy_code_t), |
420 code->len); | |
509 | 421 } |
507 | 422 |
509 | 423 e->ip += sizeof(ngx_http_script_copy_code_t) |
424 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1)); | |
507 | 425 |
845
eb5cf273e5d2
debug log should not be under rewrite_log control
Igor Sysoev <igor@sysoev.ru>
parents:
730
diff
changeset
|
426 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
eb5cf273e5d2
debug log should not be under rewrite_log control
Igor Sysoev <igor@sysoev.ru>
parents:
730
diff
changeset
|
427 "http script copy: \"%V\"", &e->buf); |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
428 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
429 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
430 |
507 | 431 size_t |
509 | 432 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
|
433 { |
507 | 434 ngx_http_variable_value_t *value; |
435 ngx_http_script_var_code_t *code; | |
436 | |
509 | 437 code = (ngx_http_script_var_code_t *) e->ip; |
507 | 438 |
509 | 439 e->ip += sizeof(ngx_http_script_var_code_t); |
507 | 440 |
573 | 441 if (e->flushed) { |
442 value = ngx_http_get_indexed_variable(e->request, code->index); | |
507 | 443 |
573 | 444 } else { |
445 value = ngx_http_get_flushed_variable(e->request, code->index); | |
446 } | |
447 | |
448 if (value && !value->not_found) { | |
449 return value->len; | |
507 | 450 } |
451 | |
553 | 452 return 0; |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
453 } |
479 | 454 |
455 | |
507 | 456 void |
509 | 457 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e) |
479 | 458 { |
507 | 459 ngx_http_variable_value_t *value; |
460 ngx_http_script_var_code_t *code; | |
461 | |
509 | 462 code = (ngx_http_script_var_code_t *) e->ip; |
463 | |
464 e->ip += sizeof(ngx_http_script_var_code_t); | |
465 | |
466 if (!e->skip) { | |
573 | 467 |
468 if (e->flushed) { | |
469 value = ngx_http_get_indexed_variable(e->request, code->index); | |
509 | 470 |
573 | 471 } else { |
472 value = ngx_http_get_flushed_variable(e->request, code->index); | |
473 } | |
474 | |
475 if (value && !value->not_found) { | |
476 e->pos = ngx_copy(e->pos, value->data, value->len); | |
509 | 477 |
845
eb5cf273e5d2
debug log should not be under rewrite_log control
Igor Sysoev <igor@sysoev.ru>
parents:
730
diff
changeset
|
478 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, |
eb5cf273e5d2
debug log should not be under rewrite_log control
Igor Sysoev <igor@sysoev.ru>
parents:
730
diff
changeset
|
479 e->request->connection->log, 0, |
eb5cf273e5d2
debug log should not be under rewrite_log control
Igor Sysoev <igor@sysoev.ru>
parents:
730
diff
changeset
|
480 "http script var: \"%V\"", &e->buf); |
509 | 481 } |
482 } | |
483 } | |
484 | |
485 | |
486 size_t | |
487 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e) | |
488 { | |
489 ngx_http_script_copy_capture_code_t *code; | |
490 | |
491 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
492 | |
493 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
494 | |
515 | 495 if (code->n < e->ncaptures) { |
496 if ((e->args || e->quote) | |
497 && (e->request->quoted_uri || e->request->plus_in_uri)) | |
498 { | |
499 return e->captures[code->n + 1] - e->captures[code->n] | |
655 | 500 + 2 * ngx_escape_uri(NULL, |
515 | 501 &e->line.data[e->captures[code->n]], |
509 | 502 e->captures[code->n + 1] - e->captures[code->n], |
503 NGX_ESCAPE_ARGS); | |
515 | 504 } else { |
505 return e->captures[code->n + 1] - e->captures[code->n]; | |
506 } | |
509 | 507 } |
515 | 508 |
509 return 0; | |
509 | 510 } |
511 | |
512 | |
513 void | |
514 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e) | |
515 { | |
516 ngx_http_script_copy_capture_code_t *code; | |
517 | |
518 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
519 | |
520 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
507 | 521 |
515 | 522 if (code->n < e->ncaptures) { |
523 if ((e->args || e->quote) | |
524 && (e->request->quoted_uri || e->request->plus_in_uri)) | |
525 { | |
526 e->pos = (u_char *) ngx_escape_uri(e->pos, | |
527 &e->line.data[e->captures[code->n]], | |
509 | 528 e->captures[code->n + 1] - e->captures[code->n], |
529 NGX_ESCAPE_ARGS); | |
515 | 530 } else { |
573 | 531 e->pos = ngx_copy(e->pos, |
532 &e->line.data[e->captures[code->n]], | |
533 e->captures[code->n + 1] - e->captures[code->n]); | |
515 | 534 } |
509 | 535 } |
536 | |
537 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
538 "http script capture: \"%V\"", &e->buf); | |
539 } | |
540 | |
541 | |
542 void | |
543 ngx_http_script_start_args_code(ngx_http_script_engine_t *e) | |
544 { | |
545 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
546 "http script args"); | |
577 | 547 |
509 | 548 e->args = e->pos; |
549 e->ip += sizeof(uintptr_t); | |
550 } | |
551 | |
552 | |
517 | 553 |
554 #if (NGX_PCRE) | |
555 | |
509 | 556 void |
557 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e) | |
558 { | |
559 size_t len; | |
560 ngx_int_t rc; | |
561 ngx_uint_t n; | |
562 ngx_http_request_t *r; | |
563 ngx_http_script_engine_t le; | |
564 ngx_http_script_len_code_pt lcode; | |
565 ngx_http_script_regex_code_t *code; | |
566 | |
567 code = (ngx_http_script_regex_code_t *) e->ip; | |
568 | |
569 r = e->request; | |
570 | |
571 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
572 "http script regex: \"%V\"", &code->name); | |
573 | |
574 if (code->uri) { | |
515 | 575 e->line = r->uri; |
509 | 576 } else { |
577 e->sp--; | |
573 | 578 e->line.len = e->sp->len; |
579 e->line.data = e->sp->data; | |
509 | 580 } |
507 | 581 |
515 | 582 rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures); |
509 | 583 |
584 if (rc == NGX_REGEX_NO_MATCHED) { | |
585 if (e->log) { | |
586 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
515 | 587 "\"%V\" does not match \"%V\"", |
588 &code->name, &e->line); | |
509 | 589 } |
590 | |
515 | 591 e->ncaptures = 0; |
592 | |
509 | 593 if (code->test) { |
631 | 594 if (code->negative_test) { |
595 e->sp->len = 1; | |
596 e->sp->data = (u_char *) "1"; | |
597 | |
598 } else { | |
599 e->sp->len = 0; | |
600 e->sp->data = (u_char *) ""; | |
601 } | |
602 | |
509 | 603 e->sp++; |
604 | |
605 e->ip += sizeof(ngx_http_script_regex_code_t); | |
606 return; | |
607 } | |
608 | |
609 e->ip += code->next; | |
610 return; | |
611 } | |
612 | |
613 if (rc < 0) { | |
614 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
615 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"", | |
515 | 616 rc, &e->line, &code->name); |
509 | 617 |
618 e->ip = ngx_http_script_exit; | |
619 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
620 return; | |
621 } | |
622 | |
623 if (e->log) { | |
624 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
515 | 625 "\"%V\" matches \"%V\"", &code->name, &e->line); |
509 | 626 } |
627 | |
515 | 628 e->ncaptures = code->ncaptures; |
629 | |
509 | 630 if (code->test) { |
631 | 631 if (code->negative_test) { |
632 e->sp->len = 0; | |
633 e->sp->data = (u_char *) ""; | |
634 | |
635 } else { | |
636 e->sp->len = 1; | |
637 e->sp->data = (u_char *) "1"; | |
638 } | |
639 | |
509 | 640 e->sp++; |
641 | |
642 e->ip += sizeof(ngx_http_script_regex_code_t); | |
643 return; | |
644 } | |
645 | |
646 if (code->status) { | |
647 e->status = code->status; | |
507 | 648 |
509 | 649 if (!code->redirect) { |
650 e->ip = ngx_http_script_exit; | |
651 return; | |
652 } | |
653 } | |
654 | |
655 if (code->uri) { | |
656 r->internal = 1; | |
657 r->valid_unparsed_uri = 0; | |
658 | |
659 if (code->break_cycle) { | |
660 r->valid_location = 0; | |
527 | 661 r->uri_changed = 0; |
509 | 662 |
663 } else { | |
664 r->uri_changed = 1; | |
665 } | |
666 } | |
667 | |
668 if (code->lengths == NULL) { | |
669 e->buf.len = code->size; | |
670 | |
671 if (code->uri) { | |
672 if (rc && (r->quoted_uri || r->plus_in_uri)) { | |
673 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, | |
674 NGX_ESCAPE_ARGS); | |
675 } | |
676 } | |
677 | |
678 for (n = 1; n < (ngx_uint_t) rc; n++) { | |
679 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n]; | |
680 } | |
681 | |
682 } else { | |
683 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
684 | |
685 le.ip = code->lengths->elts; | |
679 | 686 le.line = e->line; |
509 | 687 le.request = r; |
688 le.captures = e->captures; | |
515 | 689 le.ncaptures = e->ncaptures; |
679 | 690 le.quote = code->redirect; |
509 | 691 |
692 len = 1; /* reserve 1 byte for possible "?" */ | |
693 | |
694 while (*(uintptr_t *) le.ip) { | |
695 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
696 len += lcode(&le); | |
697 } | |
698 | |
699 e->buf.len = len; | |
700 } | |
701 | |
515 | 702 if (code->add_args && r->args.len) { |
509 | 703 e->buf.len += r->args.len + 1; |
704 } | |
705 | |
706 e->buf.data = ngx_palloc(r->pool, e->buf.len); | |
707 if (e->buf.data == NULL) { | |
708 e->ip = ngx_http_script_exit; | |
709 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
507 | 710 return; |
711 } | |
712 | |
509 | 713 e->quote = code->redirect; |
714 | |
715 e->pos = e->buf.data; | |
716 | |
717 e->ip += sizeof(ngx_http_script_regex_code_t); | |
718 } | |
719 | |
720 | |
721 void | |
722 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e) | |
723 { | |
577 | 724 u_char *dst, *src; |
509 | 725 ngx_http_request_t *r; |
726 ngx_http_script_regex_end_code_t *code; | |
727 | |
728 code = (ngx_http_script_regex_end_code_t *) e->ip; | |
729 | |
730 r = e->request; | |
731 | |
732 e->quote = 0; | |
733 | |
734 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
735 "http script regex end"); | |
736 | |
737 if (code->redirect) { | |
738 | |
577 | 739 dst = e->buf.data; |
740 src = e->buf.data; | |
741 | |
601 | 742 ngx_unescape_uri(&dst, &src, e->pos - e->buf.data, NGX_UNESCAPE_URI); |
577 | 743 |
744 if (src < e->pos) { | |
745 dst = ngx_copy(dst, src, e->pos - src); | |
746 } | |
747 | |
748 e->pos = dst; | |
749 | |
750 if (code->add_args && r->args.len) { | |
509 | 751 *e->pos++ = (u_char) (code->args ? '&' : '?'); |
573 | 752 e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
509 | 753 } |
754 | |
755 e->buf.len = e->pos - e->buf.data; | |
756 | |
757 if (e->log) { | |
758 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
759 "rewritten redirect: \"%V\"", &e->buf); | |
760 } | |
761 | |
762 r->headers_out.location = ngx_list_push(&r->headers_out.headers); | |
763 if (r->headers_out.location == NULL) { | |
764 e->ip = ngx_http_script_exit; | |
765 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
766 return; | |
767 } | |
768 | |
577 | 769 r->headers_out.location->hash = 1; |
509 | 770 r->headers_out.location->key.len = sizeof("Location") - 1; |
771 r->headers_out.location->key.data = (u_char *) "Location"; | |
772 r->headers_out.location->value = e->buf; | |
773 | |
774 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
775 return; | |
776 } | |
777 | |
577 | 778 if (e->args) { |
509 | 779 e->buf.len = e->args - e->buf.data; |
780 | |
781 if (code->add_args && r->args.len) { | |
782 *e->pos++ = '&'; | |
573 | 783 e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
509 | 784 } |
785 | |
786 r->args.len = e->pos - e->args; | |
787 r->args.data = e->args; | |
788 | |
789 e->args = NULL; | |
790 | |
791 } else { | |
792 e->buf.len = e->pos - e->buf.data; | |
673 | 793 |
794 if (!code->add_args) { | |
795 r->args.len = 0; | |
796 } | |
509 | 797 } |
798 | |
799 if (e->log) { | |
800 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
801 "rewritten data: \"%V\", args: \"%V\"", | |
802 &e->buf, &r->args); | |
803 } | |
804 | |
805 if (code->uri) { | |
806 r->uri = e->buf; | |
807 | |
535 | 808 if (r->uri.len == 0) { |
809 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
810 "the rewritten URI has a zero length"); | |
811 e->ip = ngx_http_script_exit; | |
812 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
813 return; | |
814 } | |
815 | |
509 | 816 if (ngx_http_set_exten(r) != NGX_OK) { |
817 e->ip = ngx_http_script_exit; | |
818 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
819 return; | |
820 } | |
821 } | |
822 | |
823 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
824 } | |
825 | |
517 | 826 #endif |
827 | |
509 | 828 |
829 void | |
830 ngx_http_script_return_code(ngx_http_script_engine_t *e) | |
577 | 831 { |
509 | 832 ngx_http_script_return_code_t *code; |
577 | 833 |
509 | 834 code = (ngx_http_script_return_code_t *) e->ip; |
835 | |
836 e->status = code->status; | |
837 | |
653 | 838 if (code->status == NGX_HTTP_NO_CONTENT) { |
839 e->request->header_only = 1; | |
730
1be87cdd010f
set "Content-Length: 0" for errors handled by "return 204"
Igor Sysoev <igor@sysoev.ru>
parents:
679
diff
changeset
|
840 e->request->zero_body = 1; |
653 | 841 } |
842 | |
509 | 843 e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t); |
479 | 844 } |
509 | 845 |
846 | |
847 void | |
527 | 848 ngx_http_script_break_code(ngx_http_script_engine_t *e) |
849 { | |
850 e->request->uri_changed = 0; | |
851 | |
852 e->ip = ngx_http_script_exit; | |
853 } | |
854 | |
855 | |
856 void | |
509 | 857 ngx_http_script_if_code(ngx_http_script_engine_t *e) |
858 { | |
859 ngx_http_script_if_code_t *code; | |
860 | |
861 code = (ngx_http_script_if_code_t *) e->ip; | |
862 | |
863 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
864 "http script if"); | |
865 | |
866 e->sp--; | |
867 | |
573 | 868 if (e->sp->len && e->sp->data[0] != '0') { |
509 | 869 if (code->loc_conf) { |
870 e->request->loc_conf = code->loc_conf; | |
535 | 871 ngx_http_update_location_config(e->request); |
509 | 872 } |
873 | |
874 e->ip += sizeof(ngx_http_script_if_code_t); | |
875 return; | |
876 } | |
877 | |
878 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
577 | 879 "http script if: false"); |
509 | 880 |
881 e->ip += code->next; | |
882 } | |
883 | |
884 | |
885 void | |
577 | 886 ngx_http_script_equal_code(ngx_http_script_engine_t *e) |
887 { | |
888 ngx_http_variable_value_t *val, *res; | |
889 | |
890 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
891 "http script equal"); | |
892 | |
893 e->sp--; | |
894 val = e->sp; | |
895 res = e->sp - 1; | |
896 | |
897 e->ip += sizeof(uintptr_t); | |
898 | |
899 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len) | |
900 == 0) | |
901 { | |
902 *res = ngx_http_variable_true_value; | |
903 return; | |
904 } | |
905 | |
906 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
907 "http script equal: no"); | |
908 | |
909 *res = ngx_http_variable_null_value; | |
910 } | |
911 | |
912 | |
913 void | |
914 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e) | |
915 { | |
916 ngx_http_variable_value_t *val, *res; | |
917 | |
918 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
919 "http script not equal"); | |
920 | |
921 e->sp--; | |
922 val = e->sp; | |
923 res = e->sp - 1; | |
924 | |
925 e->ip += sizeof(uintptr_t); | |
926 | |
927 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len) | |
928 == 0) | |
929 { | |
930 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
931 "http script not equal: no"); | |
932 | |
933 *res = ngx_http_variable_null_value; | |
934 return; | |
935 } | |
936 | |
937 *res = ngx_http_variable_true_value; | |
938 } | |
939 | |
940 | |
941 void | |
631 | 942 ngx_http_script_file_code(ngx_http_script_engine_t *e) |
943 { | |
944 ngx_err_t err; | |
945 ngx_file_info_t fi; | |
946 ngx_http_variable_value_t *value; | |
947 ngx_http_script_file_code_t *code; | |
948 | |
949 value = e->sp - 1; | |
950 | |
951 code = (ngx_http_script_file_code_t *) e->ip; | |
952 e->ip += sizeof(ngx_http_script_file_code_t); | |
953 | |
954 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
955 "http script file op %p", code->op); | |
956 | |
957 if (ngx_file_info(value->data, &fi) == -1) { | |
958 err = ngx_errno; | |
959 | |
960 if (err != NGX_ENOENT && err != NGX_ENOTDIR) { | |
961 ngx_log_error(NGX_LOG_CRIT, e->request->connection->log, err, | |
962 ngx_file_info_n " \"%s\" failed", value->data); | |
963 } | |
964 | |
965 switch (code->op) { | |
671 | 966 |
631 | 967 case ngx_http_script_file_plain: |
669 | 968 case ngx_http_script_file_dir: |
969 case ngx_http_script_file_exists: | |
970 case ngx_http_script_file_exec: | |
631 | 971 goto false; |
671 | 972 |
631 | 973 case ngx_http_script_file_not_plain: |
669 | 974 case ngx_http_script_file_not_dir: |
671 | 975 case ngx_http_script_file_not_exists: |
669 | 976 case ngx_http_script_file_not_exec: |
631 | 977 goto true; |
978 } | |
979 | |
980 goto false; | |
981 } | |
982 | |
983 switch (code->op) { | |
984 case ngx_http_script_file_plain: | |
985 if (ngx_is_file(&fi)) { | |
986 goto true; | |
987 } | |
988 goto false; | |
989 | |
990 case ngx_http_script_file_not_plain: | |
991 if (ngx_is_file(&fi)) { | |
992 goto false; | |
993 } | |
994 goto true; | |
669 | 995 |
996 case ngx_http_script_file_dir: | |
997 if (ngx_is_dir(&fi)) { | |
998 goto true; | |
999 } | |
1000 goto false; | |
1001 | |
1002 case ngx_http_script_file_not_dir: | |
1003 if (ngx_is_dir(&fi)) { | |
1004 goto false; | |
1005 } | |
1006 goto true; | |
1007 | |
1008 case ngx_http_script_file_exists: | |
1009 if (ngx_is_file(&fi) || ngx_is_dir(&fi) || ngx_is_link(&fi)) { | |
1010 goto true; | |
1011 } | |
1012 goto false; | |
1013 | |
1014 case ngx_http_script_file_not_exists: | |
1015 if (ngx_is_file(&fi) || ngx_is_dir(&fi) || ngx_is_link(&fi)) { | |
1016 goto false; | |
1017 } | |
1018 goto true; | |
1019 | |
1020 #if (NGX_WIN32) | |
1021 | |
1022 case ngx_http_script_file_exec: | |
1023 goto false; | |
1024 | |
1025 case ngx_http_script_file_not_exec: | |
1026 goto true; | |
1027 | |
1028 #else | |
1029 | |
1030 case ngx_http_script_file_exec: | |
1031 if (ngx_is_exec(&fi)) { | |
1032 goto true; | |
1033 } | |
1034 goto false; | |
1035 | |
1036 case ngx_http_script_file_not_exec: | |
1037 if (ngx_is_exec(&fi)) { | |
1038 goto false; | |
1039 } | |
1040 goto true; | |
1041 | |
1042 #endif | |
631 | 1043 } |
1044 | |
1045 false: | |
1046 | |
1047 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1048 "http script file op false"); | |
1049 | |
1050 *value = ngx_http_variable_null_value; | |
1051 return; | |
1052 | |
1053 true: | |
1054 | |
1055 *value = ngx_http_variable_true_value; | |
1056 return; | |
1057 } | |
1058 | |
1059 | |
1060 void | |
515 | 1061 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e) |
1062 { | |
1063 size_t len; | |
1064 ngx_http_script_engine_t le; | |
1065 ngx_http_script_len_code_pt lcode; | |
1066 ngx_http_script_complex_value_code_t *code; | |
1067 | |
1068 code = (ngx_http_script_complex_value_code_t *) e->ip; | |
1069 | |
1070 e->ip += sizeof(ngx_http_script_complex_value_code_t); | |
1071 | |
1072 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1073 "http script complex value"); | |
1074 | |
1075 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
1076 | |
1077 le.ip = code->lengths->elts; | |
655 | 1078 le.line = e->line; |
515 | 1079 le.request = e->request; |
1080 le.captures = e->captures; | |
1081 le.ncaptures = e->ncaptures; | |
655 | 1082 le.quote = e->quote; |
515 | 1083 |
1084 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { | |
1085 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
1086 } | |
1087 | |
1088 e->buf.len = len; | |
1089 e->buf.data = ngx_palloc(e->request->pool, len); | |
1090 if (e->buf.data == NULL) { | |
1091 e->ip = ngx_http_script_exit; | |
1092 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1093 return; | |
1094 } | |
1095 | |
1096 e->pos = e->buf.data; | |
1097 | |
577 | 1098 e->sp->len = e->buf.len; |
573 | 1099 e->sp->data = e->buf.data; |
515 | 1100 e->sp++; |
1101 } | |
1102 | |
1103 | |
1104 void | |
509 | 1105 ngx_http_script_value_code(ngx_http_script_engine_t *e) |
1106 { | |
1107 ngx_http_script_value_code_t *code; | |
1108 | |
1109 code = (ngx_http_script_value_code_t *) e->ip; | |
1110 | |
1111 e->ip += sizeof(ngx_http_script_value_code_t); | |
1112 | |
573 | 1113 e->sp->len = code->text_len; |
1114 e->sp->data = (u_char *) code->text_data; | |
577 | 1115 |
1116 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1117 "http script value: \"%V\"", e->sp); | |
1118 | |
509 | 1119 e->sp++; |
1120 } | |
1121 | |
1122 | |
1123 void | |
1124 ngx_http_script_set_var_code(ngx_http_script_engine_t *e) | |
1125 { | |
1126 ngx_http_request_t *r; | |
1127 ngx_http_script_var_code_t *code; | |
1128 | |
1129 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1130 "http script set var"); | |
1131 | |
1132 code = (ngx_http_script_var_code_t *) e->ip; | |
1133 | |
1134 e->ip += sizeof(ngx_http_script_var_code_t); | |
1135 | |
1136 r = e->request; | |
1137 | |
1138 e->sp--; | |
1139 | |
573 | 1140 r->variables[code->index].len = e->sp->len; |
1141 r->variables[code->index].valid = 1; | |
1142 r->variables[code->index].no_cachable = 0; | |
1143 r->variables[code->index].not_found = 0; | |
1144 r->variables[code->index].data = e->sp->data; | |
509 | 1145 } |
1146 | |
1147 | |
1148 void | |
637 | 1149 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e) |
1150 { | |
1151 ngx_http_script_var_handler_code_t *code; | |
1152 | |
1153 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1154 "http script set var handler"); | |
1155 | |
1156 code = (ngx_http_script_var_handler_code_t *) e->ip; | |
1157 | |
1158 e->ip += sizeof(ngx_http_script_var_handler_code_t); | |
1159 | |
1160 e->sp--; | |
1161 | |
1162 code->handler(e->request, e->sp, code->data); | |
1163 } | |
1164 | |
1165 | |
1166 void | |
509 | 1167 ngx_http_script_var_code(ngx_http_script_engine_t *e) |
1168 { | |
1169 ngx_http_variable_value_t *value; | |
1170 ngx_http_script_var_code_t *code; | |
1171 | |
1172 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1173 "http script var"); | |
1174 | |
1175 code = (ngx_http_script_var_code_t *) e->ip; | |
1176 | |
1177 e->ip += sizeof(ngx_http_script_var_code_t); | |
1178 | |
573 | 1179 value = ngx_http_get_flushed_variable(e->request, code->index); |
509 | 1180 |
573 | 1181 if (value && !value->not_found) { |
1182 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1183 "http script var: \"%V\"", value); | |
1184 | |
553 | 1185 *e->sp = *value; |
509 | 1186 e->sp++; |
1187 | |
1188 return; | |
1189 } | |
1190 | |
577 | 1191 *e->sp = ngx_http_variable_null_value; |
509 | 1192 e->sp++; |
1193 } | |
1194 | |
1195 | |
1196 void | |
1197 ngx_http_script_nop_code(ngx_http_script_engine_t *e) | |
1198 { | |
1199 e->ip += sizeof(uintptr_t); | |
1200 } |