Mercurial > hg > nginx-quic
annotate src/http/ngx_http_script.c @ 828:597c83dad983
fix format
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 30 Oct 2006 20:03:49 +0000 |
parents | 1be87cdd010f |
children | eb5cf273e5d2 |
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 |
509 | 426 if (e->log) { |
427 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
428 "http script copy: \"%V\"", &e->buf); | |
429 } | |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
430 } |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
431 |
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
432 |
507 | 433 size_t |
509 | 434 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
|
435 { |
507 | 436 ngx_http_variable_value_t *value; |
437 ngx_http_script_var_code_t *code; | |
438 | |
509 | 439 code = (ngx_http_script_var_code_t *) e->ip; |
507 | 440 |
509 | 441 e->ip += sizeof(ngx_http_script_var_code_t); |
507 | 442 |
573 | 443 if (e->flushed) { |
444 value = ngx_http_get_indexed_variable(e->request, code->index); | |
507 | 445 |
573 | 446 } else { |
447 value = ngx_http_get_flushed_variable(e->request, code->index); | |
448 } | |
449 | |
450 if (value && !value->not_found) { | |
451 return value->len; | |
507 | 452 } |
453 | |
553 | 454 return 0; |
99
a059e1aa65d4
nginx-0.0.1-2003-06-02-19:24:30 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
455 } |
479 | 456 |
457 | |
507 | 458 void |
509 | 459 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e) |
479 | 460 { |
507 | 461 ngx_http_variable_value_t *value; |
462 ngx_http_script_var_code_t *code; | |
463 | |
509 | 464 code = (ngx_http_script_var_code_t *) e->ip; |
465 | |
466 e->ip += sizeof(ngx_http_script_var_code_t); | |
467 | |
468 if (!e->skip) { | |
573 | 469 |
470 if (e->flushed) { | |
471 value = ngx_http_get_indexed_variable(e->request, code->index); | |
509 | 472 |
573 | 473 } else { |
474 value = ngx_http_get_flushed_variable(e->request, code->index); | |
475 } | |
476 | |
477 if (value && !value->not_found) { | |
478 e->pos = ngx_copy(e->pos, value->data, value->len); | |
509 | 479 |
553 | 480 if (e->log) { |
481 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, | |
482 e->request->connection->log, 0, | |
483 "http script var: \"%V\"", &e->buf); | |
484 } | |
509 | 485 } |
486 } | |
487 } | |
488 | |
489 | |
490 size_t | |
491 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e) | |
492 { | |
493 ngx_http_script_copy_capture_code_t *code; | |
494 | |
495 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
496 | |
497 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
498 | |
515 | 499 if (code->n < e->ncaptures) { |
500 if ((e->args || e->quote) | |
501 && (e->request->quoted_uri || e->request->plus_in_uri)) | |
502 { | |
503 return e->captures[code->n + 1] - e->captures[code->n] | |
655 | 504 + 2 * ngx_escape_uri(NULL, |
515 | 505 &e->line.data[e->captures[code->n]], |
509 | 506 e->captures[code->n + 1] - e->captures[code->n], |
507 NGX_ESCAPE_ARGS); | |
515 | 508 } else { |
509 return e->captures[code->n + 1] - e->captures[code->n]; | |
510 } | |
509 | 511 } |
515 | 512 |
513 return 0; | |
509 | 514 } |
515 | |
516 | |
517 void | |
518 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e) | |
519 { | |
520 ngx_http_script_copy_capture_code_t *code; | |
521 | |
522 code = (ngx_http_script_copy_capture_code_t *) e->ip; | |
523 | |
524 e->ip += sizeof(ngx_http_script_copy_capture_code_t); | |
507 | 525 |
515 | 526 if (code->n < e->ncaptures) { |
527 if ((e->args || e->quote) | |
528 && (e->request->quoted_uri || e->request->plus_in_uri)) | |
529 { | |
530 e->pos = (u_char *) ngx_escape_uri(e->pos, | |
531 &e->line.data[e->captures[code->n]], | |
509 | 532 e->captures[code->n + 1] - e->captures[code->n], |
533 NGX_ESCAPE_ARGS); | |
515 | 534 } else { |
573 | 535 e->pos = ngx_copy(e->pos, |
536 &e->line.data[e->captures[code->n]], | |
537 e->captures[code->n + 1] - e->captures[code->n]); | |
515 | 538 } |
509 | 539 } |
540 | |
541 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
542 "http script capture: \"%V\"", &e->buf); | |
543 } | |
544 | |
545 | |
546 void | |
547 ngx_http_script_start_args_code(ngx_http_script_engine_t *e) | |
548 { | |
549 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
550 "http script args"); | |
577 | 551 |
509 | 552 e->args = e->pos; |
553 e->ip += sizeof(uintptr_t); | |
554 } | |
555 | |
556 | |
517 | 557 |
558 #if (NGX_PCRE) | |
559 | |
509 | 560 void |
561 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e) | |
562 { | |
563 size_t len; | |
564 ngx_int_t rc; | |
565 ngx_uint_t n; | |
566 ngx_http_request_t *r; | |
567 ngx_http_script_engine_t le; | |
568 ngx_http_script_len_code_pt lcode; | |
569 ngx_http_script_regex_code_t *code; | |
570 | |
571 code = (ngx_http_script_regex_code_t *) e->ip; | |
572 | |
573 r = e->request; | |
574 | |
575 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
576 "http script regex: \"%V\"", &code->name); | |
577 | |
578 if (code->uri) { | |
515 | 579 e->line = r->uri; |
509 | 580 } else { |
581 e->sp--; | |
573 | 582 e->line.len = e->sp->len; |
583 e->line.data = e->sp->data; | |
509 | 584 } |
507 | 585 |
515 | 586 rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures); |
509 | 587 |
588 if (rc == NGX_REGEX_NO_MATCHED) { | |
589 if (e->log) { | |
590 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
515 | 591 "\"%V\" does not match \"%V\"", |
592 &code->name, &e->line); | |
509 | 593 } |
594 | |
515 | 595 e->ncaptures = 0; |
596 | |
509 | 597 if (code->test) { |
631 | 598 if (code->negative_test) { |
599 e->sp->len = 1; | |
600 e->sp->data = (u_char *) "1"; | |
601 | |
602 } else { | |
603 e->sp->len = 0; | |
604 e->sp->data = (u_char *) ""; | |
605 } | |
606 | |
509 | 607 e->sp++; |
608 | |
609 e->ip += sizeof(ngx_http_script_regex_code_t); | |
610 return; | |
611 } | |
612 | |
613 e->ip += code->next; | |
614 return; | |
615 } | |
616 | |
617 if (rc < 0) { | |
618 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
619 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"", | |
515 | 620 rc, &e->line, &code->name); |
509 | 621 |
622 e->ip = ngx_http_script_exit; | |
623 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
624 return; | |
625 } | |
626 | |
627 if (e->log) { | |
628 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
515 | 629 "\"%V\" matches \"%V\"", &code->name, &e->line); |
509 | 630 } |
631 | |
515 | 632 e->ncaptures = code->ncaptures; |
633 | |
509 | 634 if (code->test) { |
631 | 635 if (code->negative_test) { |
636 e->sp->len = 0; | |
637 e->sp->data = (u_char *) ""; | |
638 | |
639 } else { | |
640 e->sp->len = 1; | |
641 e->sp->data = (u_char *) "1"; | |
642 } | |
643 | |
509 | 644 e->sp++; |
645 | |
646 e->ip += sizeof(ngx_http_script_regex_code_t); | |
647 return; | |
648 } | |
649 | |
650 if (code->status) { | |
651 e->status = code->status; | |
507 | 652 |
509 | 653 if (!code->redirect) { |
654 e->ip = ngx_http_script_exit; | |
655 return; | |
656 } | |
657 } | |
658 | |
659 if (code->uri) { | |
660 r->internal = 1; | |
661 r->valid_unparsed_uri = 0; | |
662 | |
663 if (code->break_cycle) { | |
664 r->valid_location = 0; | |
527 | 665 r->uri_changed = 0; |
509 | 666 |
667 } else { | |
668 r->uri_changed = 1; | |
669 } | |
670 } | |
671 | |
672 if (code->lengths == NULL) { | |
673 e->buf.len = code->size; | |
674 | |
675 if (code->uri) { | |
676 if (rc && (r->quoted_uri || r->plus_in_uri)) { | |
677 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, | |
678 NGX_ESCAPE_ARGS); | |
679 } | |
680 } | |
681 | |
682 for (n = 1; n < (ngx_uint_t) rc; n++) { | |
683 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n]; | |
684 } | |
685 | |
686 } else { | |
687 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
688 | |
689 le.ip = code->lengths->elts; | |
679 | 690 le.line = e->line; |
509 | 691 le.request = r; |
692 le.captures = e->captures; | |
515 | 693 le.ncaptures = e->ncaptures; |
679 | 694 le.quote = code->redirect; |
509 | 695 |
696 len = 1; /* reserve 1 byte for possible "?" */ | |
697 | |
698 while (*(uintptr_t *) le.ip) { | |
699 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
700 len += lcode(&le); | |
701 } | |
702 | |
703 e->buf.len = len; | |
704 } | |
705 | |
515 | 706 if (code->add_args && r->args.len) { |
509 | 707 e->buf.len += r->args.len + 1; |
708 } | |
709 | |
710 e->buf.data = ngx_palloc(r->pool, e->buf.len); | |
711 if (e->buf.data == NULL) { | |
712 e->ip = ngx_http_script_exit; | |
713 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
507 | 714 return; |
715 } | |
716 | |
509 | 717 e->quote = code->redirect; |
718 | |
719 e->pos = e->buf.data; | |
720 | |
721 e->ip += sizeof(ngx_http_script_regex_code_t); | |
722 } | |
723 | |
724 | |
725 void | |
726 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e) | |
727 { | |
577 | 728 u_char *dst, *src; |
509 | 729 ngx_http_request_t *r; |
730 ngx_http_script_regex_end_code_t *code; | |
731 | |
732 code = (ngx_http_script_regex_end_code_t *) e->ip; | |
733 | |
734 r = e->request; | |
735 | |
736 e->quote = 0; | |
737 | |
738 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
739 "http script regex end"); | |
740 | |
741 if (code->redirect) { | |
742 | |
577 | 743 dst = e->buf.data; |
744 src = e->buf.data; | |
745 | |
601 | 746 ngx_unescape_uri(&dst, &src, e->pos - e->buf.data, NGX_UNESCAPE_URI); |
577 | 747 |
748 if (src < e->pos) { | |
749 dst = ngx_copy(dst, src, e->pos - src); | |
750 } | |
751 | |
752 e->pos = dst; | |
753 | |
754 if (code->add_args && r->args.len) { | |
509 | 755 *e->pos++ = (u_char) (code->args ? '&' : '?'); |
573 | 756 e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
509 | 757 } |
758 | |
759 e->buf.len = e->pos - e->buf.data; | |
760 | |
761 if (e->log) { | |
762 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
763 "rewritten redirect: \"%V\"", &e->buf); | |
764 } | |
765 | |
766 r->headers_out.location = ngx_list_push(&r->headers_out.headers); | |
767 if (r->headers_out.location == NULL) { | |
768 e->ip = ngx_http_script_exit; | |
769 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
770 return; | |
771 } | |
772 | |
577 | 773 r->headers_out.location->hash = 1; |
509 | 774 r->headers_out.location->key.len = sizeof("Location") - 1; |
775 r->headers_out.location->key.data = (u_char *) "Location"; | |
776 r->headers_out.location->value = e->buf; | |
777 | |
778 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
779 return; | |
780 } | |
781 | |
577 | 782 if (e->args) { |
509 | 783 e->buf.len = e->args - e->buf.data; |
784 | |
785 if (code->add_args && r->args.len) { | |
786 *e->pos++ = '&'; | |
573 | 787 e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
509 | 788 } |
789 | |
790 r->args.len = e->pos - e->args; | |
791 r->args.data = e->args; | |
792 | |
793 e->args = NULL; | |
794 | |
795 } else { | |
796 e->buf.len = e->pos - e->buf.data; | |
673 | 797 |
798 if (!code->add_args) { | |
799 r->args.len = 0; | |
800 } | |
509 | 801 } |
802 | |
803 if (e->log) { | |
804 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
805 "rewritten data: \"%V\", args: \"%V\"", | |
806 &e->buf, &r->args); | |
807 } | |
808 | |
809 if (code->uri) { | |
810 r->uri = e->buf; | |
811 | |
535 | 812 if (r->uri.len == 0) { |
813 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
814 "the rewritten URI has a zero length"); | |
815 e->ip = ngx_http_script_exit; | |
816 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
817 return; | |
818 } | |
819 | |
509 | 820 if (ngx_http_set_exten(r) != NGX_OK) { |
821 e->ip = ngx_http_script_exit; | |
822 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
823 return; | |
824 } | |
825 } | |
826 | |
827 e->ip += sizeof(ngx_http_script_regex_end_code_t); | |
828 } | |
829 | |
517 | 830 #endif |
831 | |
509 | 832 |
833 void | |
834 ngx_http_script_return_code(ngx_http_script_engine_t *e) | |
577 | 835 { |
509 | 836 ngx_http_script_return_code_t *code; |
577 | 837 |
509 | 838 code = (ngx_http_script_return_code_t *) e->ip; |
839 | |
840 e->status = code->status; | |
841 | |
653 | 842 if (code->status == NGX_HTTP_NO_CONTENT) { |
843 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
|
844 e->request->zero_body = 1; |
653 | 845 } |
846 | |
509 | 847 e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t); |
479 | 848 } |
509 | 849 |
850 | |
851 void | |
527 | 852 ngx_http_script_break_code(ngx_http_script_engine_t *e) |
853 { | |
854 e->request->uri_changed = 0; | |
855 | |
856 e->ip = ngx_http_script_exit; | |
857 } | |
858 | |
859 | |
860 void | |
509 | 861 ngx_http_script_if_code(ngx_http_script_engine_t *e) |
862 { | |
863 ngx_http_script_if_code_t *code; | |
864 | |
865 code = (ngx_http_script_if_code_t *) e->ip; | |
866 | |
867 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
868 "http script if"); | |
869 | |
870 e->sp--; | |
871 | |
573 | 872 if (e->sp->len && e->sp->data[0] != '0') { |
509 | 873 if (code->loc_conf) { |
874 e->request->loc_conf = code->loc_conf; | |
535 | 875 ngx_http_update_location_config(e->request); |
509 | 876 } |
877 | |
878 e->ip += sizeof(ngx_http_script_if_code_t); | |
879 return; | |
880 } | |
881 | |
882 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
577 | 883 "http script if: false"); |
509 | 884 |
885 e->ip += code->next; | |
886 } | |
887 | |
888 | |
889 void | |
577 | 890 ngx_http_script_equal_code(ngx_http_script_engine_t *e) |
891 { | |
892 ngx_http_variable_value_t *val, *res; | |
893 | |
894 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
895 "http script equal"); | |
896 | |
897 e->sp--; | |
898 val = e->sp; | |
899 res = e->sp - 1; | |
900 | |
901 e->ip += sizeof(uintptr_t); | |
902 | |
903 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len) | |
904 == 0) | |
905 { | |
906 *res = ngx_http_variable_true_value; | |
907 return; | |
908 } | |
909 | |
910 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
911 "http script equal: no"); | |
912 | |
913 *res = ngx_http_variable_null_value; | |
914 } | |
915 | |
916 | |
917 void | |
918 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e) | |
919 { | |
920 ngx_http_variable_value_t *val, *res; | |
921 | |
922 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
923 "http script not equal"); | |
924 | |
925 e->sp--; | |
926 val = e->sp; | |
927 res = e->sp - 1; | |
928 | |
929 e->ip += sizeof(uintptr_t); | |
930 | |
931 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len) | |
932 == 0) | |
933 { | |
934 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
935 "http script not equal: no"); | |
936 | |
937 *res = ngx_http_variable_null_value; | |
938 return; | |
939 } | |
940 | |
941 *res = ngx_http_variable_true_value; | |
942 } | |
943 | |
944 | |
945 void | |
631 | 946 ngx_http_script_file_code(ngx_http_script_engine_t *e) |
947 { | |
948 ngx_err_t err; | |
949 ngx_file_info_t fi; | |
950 ngx_http_variable_value_t *value; | |
951 ngx_http_script_file_code_t *code; | |
952 | |
953 value = e->sp - 1; | |
954 | |
955 code = (ngx_http_script_file_code_t *) e->ip; | |
956 e->ip += sizeof(ngx_http_script_file_code_t); | |
957 | |
958 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
959 "http script file op %p", code->op); | |
960 | |
961 if (ngx_file_info(value->data, &fi) == -1) { | |
962 err = ngx_errno; | |
963 | |
964 if (err != NGX_ENOENT && err != NGX_ENOTDIR) { | |
965 ngx_log_error(NGX_LOG_CRIT, e->request->connection->log, err, | |
966 ngx_file_info_n " \"%s\" failed", value->data); | |
967 } | |
968 | |
969 switch (code->op) { | |
671 | 970 |
631 | 971 case ngx_http_script_file_plain: |
669 | 972 case ngx_http_script_file_dir: |
973 case ngx_http_script_file_exists: | |
974 case ngx_http_script_file_exec: | |
631 | 975 goto false; |
671 | 976 |
631 | 977 case ngx_http_script_file_not_plain: |
669 | 978 case ngx_http_script_file_not_dir: |
671 | 979 case ngx_http_script_file_not_exists: |
669 | 980 case ngx_http_script_file_not_exec: |
631 | 981 goto true; |
982 } | |
983 | |
984 goto false; | |
985 } | |
986 | |
987 switch (code->op) { | |
988 case ngx_http_script_file_plain: | |
989 if (ngx_is_file(&fi)) { | |
990 goto true; | |
991 } | |
992 goto false; | |
993 | |
994 case ngx_http_script_file_not_plain: | |
995 if (ngx_is_file(&fi)) { | |
996 goto false; | |
997 } | |
998 goto true; | |
669 | 999 |
1000 case ngx_http_script_file_dir: | |
1001 if (ngx_is_dir(&fi)) { | |
1002 goto true; | |
1003 } | |
1004 goto false; | |
1005 | |
1006 case ngx_http_script_file_not_dir: | |
1007 if (ngx_is_dir(&fi)) { | |
1008 goto false; | |
1009 } | |
1010 goto true; | |
1011 | |
1012 case ngx_http_script_file_exists: | |
1013 if (ngx_is_file(&fi) || ngx_is_dir(&fi) || ngx_is_link(&fi)) { | |
1014 goto true; | |
1015 } | |
1016 goto false; | |
1017 | |
1018 case ngx_http_script_file_not_exists: | |
1019 if (ngx_is_file(&fi) || ngx_is_dir(&fi) || ngx_is_link(&fi)) { | |
1020 goto false; | |
1021 } | |
1022 goto true; | |
1023 | |
1024 #if (NGX_WIN32) | |
1025 | |
1026 case ngx_http_script_file_exec: | |
1027 goto false; | |
1028 | |
1029 case ngx_http_script_file_not_exec: | |
1030 goto true; | |
1031 | |
1032 #else | |
1033 | |
1034 case ngx_http_script_file_exec: | |
1035 if (ngx_is_exec(&fi)) { | |
1036 goto true; | |
1037 } | |
1038 goto false; | |
1039 | |
1040 case ngx_http_script_file_not_exec: | |
1041 if (ngx_is_exec(&fi)) { | |
1042 goto false; | |
1043 } | |
1044 goto true; | |
1045 | |
1046 #endif | |
631 | 1047 } |
1048 | |
1049 false: | |
1050 | |
1051 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1052 "http script file op false"); | |
1053 | |
1054 *value = ngx_http_variable_null_value; | |
1055 return; | |
1056 | |
1057 true: | |
1058 | |
1059 *value = ngx_http_variable_true_value; | |
1060 return; | |
1061 } | |
1062 | |
1063 | |
1064 void | |
515 | 1065 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e) |
1066 { | |
1067 size_t len; | |
1068 ngx_http_script_engine_t le; | |
1069 ngx_http_script_len_code_pt lcode; | |
1070 ngx_http_script_complex_value_code_t *code; | |
1071 | |
1072 code = (ngx_http_script_complex_value_code_t *) e->ip; | |
1073 | |
1074 e->ip += sizeof(ngx_http_script_complex_value_code_t); | |
1075 | |
1076 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1077 "http script complex value"); | |
1078 | |
1079 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
1080 | |
1081 le.ip = code->lengths->elts; | |
655 | 1082 le.line = e->line; |
515 | 1083 le.request = e->request; |
1084 le.captures = e->captures; | |
1085 le.ncaptures = e->ncaptures; | |
655 | 1086 le.quote = e->quote; |
515 | 1087 |
1088 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { | |
1089 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
1090 } | |
1091 | |
1092 e->buf.len = len; | |
1093 e->buf.data = ngx_palloc(e->request->pool, len); | |
1094 if (e->buf.data == NULL) { | |
1095 e->ip = ngx_http_script_exit; | |
1096 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1097 return; | |
1098 } | |
1099 | |
1100 e->pos = e->buf.data; | |
1101 | |
577 | 1102 e->sp->len = e->buf.len; |
573 | 1103 e->sp->data = e->buf.data; |
515 | 1104 e->sp++; |
1105 } | |
1106 | |
1107 | |
1108 void | |
509 | 1109 ngx_http_script_value_code(ngx_http_script_engine_t *e) |
1110 { | |
1111 ngx_http_script_value_code_t *code; | |
1112 | |
1113 code = (ngx_http_script_value_code_t *) e->ip; | |
1114 | |
1115 e->ip += sizeof(ngx_http_script_value_code_t); | |
1116 | |
573 | 1117 e->sp->len = code->text_len; |
1118 e->sp->data = (u_char *) code->text_data; | |
577 | 1119 |
1120 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1121 "http script value: \"%V\"", e->sp); | |
1122 | |
509 | 1123 e->sp++; |
1124 } | |
1125 | |
1126 | |
1127 void | |
1128 ngx_http_script_set_var_code(ngx_http_script_engine_t *e) | |
1129 { | |
1130 ngx_http_request_t *r; | |
1131 ngx_http_script_var_code_t *code; | |
1132 | |
1133 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1134 "http script set var"); | |
1135 | |
1136 code = (ngx_http_script_var_code_t *) e->ip; | |
1137 | |
1138 e->ip += sizeof(ngx_http_script_var_code_t); | |
1139 | |
1140 r = e->request; | |
1141 | |
1142 e->sp--; | |
1143 | |
573 | 1144 r->variables[code->index].len = e->sp->len; |
1145 r->variables[code->index].valid = 1; | |
1146 r->variables[code->index].no_cachable = 0; | |
1147 r->variables[code->index].not_found = 0; | |
1148 r->variables[code->index].data = e->sp->data; | |
509 | 1149 } |
1150 | |
1151 | |
1152 void | |
637 | 1153 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e) |
1154 { | |
1155 ngx_http_script_var_handler_code_t *code; | |
1156 | |
1157 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1158 "http script set var handler"); | |
1159 | |
1160 code = (ngx_http_script_var_handler_code_t *) e->ip; | |
1161 | |
1162 e->ip += sizeof(ngx_http_script_var_handler_code_t); | |
1163 | |
1164 e->sp--; | |
1165 | |
1166 code->handler(e->request, e->sp, code->data); | |
1167 } | |
1168 | |
1169 | |
1170 void | |
509 | 1171 ngx_http_script_var_code(ngx_http_script_engine_t *e) |
1172 { | |
1173 ngx_http_variable_value_t *value; | |
1174 ngx_http_script_var_code_t *code; | |
1175 | |
1176 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1177 "http script var"); | |
1178 | |
1179 code = (ngx_http_script_var_code_t *) e->ip; | |
1180 | |
1181 e->ip += sizeof(ngx_http_script_var_code_t); | |
1182 | |
573 | 1183 value = ngx_http_get_flushed_variable(e->request, code->index); |
509 | 1184 |
573 | 1185 if (value && !value->not_found) { |
1186 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, | |
1187 "http script var: \"%V\"", value); | |
1188 | |
553 | 1189 *e->sp = *value; |
509 | 1190 e->sp++; |
1191 | |
1192 return; | |
1193 } | |
1194 | |
577 | 1195 *e->sp = ngx_http_variable_null_value; |
509 | 1196 e->sp++; |
1197 } | |
1198 | |
1199 | |
1200 void | |
1201 ngx_http_script_nop_code(ngx_http_script_engine_t *e) | |
1202 { | |
1203 e->ip += sizeof(uintptr_t); | |
1204 } |