blob: 95c1cc1051c27249dc9b0a096c0350a83e44e123 [file] [log] [blame]
Igor Sysoev7578ec92003-06-02 15:24:30 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev7578ec92003-06-02 15:24:30 +00007#include <ngx_config.h>
8#include <ngx_core.h>
9#include <ngx_http.h>
10
11
Igor Sysoev899b44e2005-05-12 14:58:06 +000012#define ngx_http_script_exit (u_char *) &ngx_http_script_exit_code
13
14static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;
15
16
17ngx_uint_t
18ngx_http_script_variables_count(ngx_str_t *value)
Igor Sysoev7578ec92003-06-02 15:24:30 +000019{
Igor Sysoev899b44e2005-05-12 14:58:06 +000020 ngx_uint_t i, n;
Igor Sysoev02025fd2005-01-18 13:03:58 +000021
Igor Sysoev899b44e2005-05-12 14:58:06 +000022 for (n = 0, i = 0; i < value->len; i++) {
23 if (value->data[i] == '$') {
24 n++;
25 }
Igor Sysoev02f742b2005-04-08 15:18:55 +000026 }
Igor Sysoev02025fd2005-01-18 13:03:58 +000027
Igor Sysoev899b44e2005-05-12 14:58:06 +000028 return n;
29}
30
31
32ngx_int_t
33ngx_http_script_compile(ngx_http_script_compile_t *sc)
34{
35 u_char ch;
36 size_t size;
Igor Sysoev09c684b2005-11-09 17:25:55 +000037 ngx_int_t index, *p;
Igor Sysoev899b44e2005-05-12 14:58:06 +000038 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
Igor Sysoev09c684b2005-11-09 17:25:55 +000045 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
Igor Sysoev899b44e2005-05-12 14:58:06 +000054 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) {
Igor Sysoev02f742b2005-04-08 15:18:55 +000061 return NGX_ERROR;
62 }
63 }
Igor Sysoev02025fd2005-01-18 13:03:58 +000064
Igor Sysoev899b44e2005-05-12 14:58:06 +000065
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) {
Igor Sysoev02f742b2005-04-08 15:18:55 +000076 return NGX_ERROR;
77 }
78 }
79
Igor Sysoev899b44e2005-05-12 14:58:06 +000080 sc->variables = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +000081
Igor Sysoev899b44e2005-05-12 14:58:06 +000082 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
Igor Sysoev7f7846d2006-04-26 09:52:47 +000094 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
Igor Sysoev899b44e2005-05-12 14:58:06 +0000102 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;
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000111 copy_capture->n = 2 * n;
112
Igor Sysoev899b44e2005-05-12 14:58:06 +0000113
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;
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000122 copy_capture->n = 2 * n;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000123
Igor Sysoev7f7846d2006-04-26 09:52:47 +0000124 if (sc->ncaptures < n) {
125 sc->ncaptures = n;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000126 }
127
Igor Sysoev899b44e2005-05-12 14:58:06 +0000128 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];
149
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);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000171 return NGX_ERROR;
172 }
173
Igor Sysoev899b44e2005-05-12 14:58:06 +0000174 if (name.len == 0) {
175 goto invalid_variable;
176 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000177
Igor Sysoev899b44e2005-05-12 14:58:06 +0000178 sc->variables++;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000179
Igor Sysoev899b44e2005-05-12 14:58:06 +0000180 index = ngx_http_get_variable_index(sc->cf, &name);
181
182 if (index == NGX_ERROR) {
Igor Sysoev02f742b2005-04-08 15:18:55 +0000183 return NGX_ERROR;
184 }
185
Igor Sysoev09c684b2005-11-09 17:25:55 +0000186 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
Igor Sysoev899b44e2005-05-12 14:58:06 +0000195 var_code = ngx_http_script_add_code(*sc->lengths,
196 sizeof(ngx_http_script_var_code_t),
197 NULL);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000198 if (var_code == NULL) {
199 return NGX_ERROR;
200 }
201
202 var_code->code = (ngx_http_script_code_pt)
Igor Sysoev899b44e2005-05-12 14:58:06 +0000203 ngx_http_script_copy_var_len_code;
204 var_code->index = (uintptr_t) index;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000205
206
Igor Sysoev899b44e2005-05-12 14:58:06 +0000207 var_code = ngx_http_script_add_code(*sc->values,
208 sizeof(ngx_http_script_var_code_t),
209 &sc->main);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000210 if (var_code == NULL) {
211 return NGX_ERROR;
212 }
213
Igor Sysoev899b44e2005-05-12 14:58:06 +0000214 var_code->code = ngx_http_script_copy_var_code;
215 var_code->index = (uintptr_t) index;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000216
217 continue;
218 }
219
Igor Sysoev899b44e2005-05-12 14:58:06 +0000220 if (sc->source->data[i] == '?' && sc->compile_args) {
221 sc->args = 1;
222 sc->compile_args = 0;
223
Igor Sysoev04610ea2008-02-12 18:05:32 +0000224 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t),
225 NULL);
226 if (code == NULL) {
227 return NGX_ERROR;
228 }
229
230 *code = (uintptr_t) ngx_http_script_mark_args_code;
231
Igor Sysoev899b44e2005-05-12 14:58:06 +0000232 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
233 &sc->main);
234 if (code == NULL) {
235 return NGX_ERROR;
236 }
237
238 *code = (uintptr_t) ngx_http_script_start_args_code;
239
240 i++;
241
242 continue;
243 }
244
245 name.data = &sc->source->data[i];
246
247 while (i < sc->source->len
248 && sc->source->data[i] != '$'
249 && !(sc->source->data[i] == '?' && sc->compile_args))
250 {
251 i++;
252 name.len++;
253 }
254
255 sc->size += name.len;
256
257 copy = ngx_http_script_add_code(*sc->lengths,
258 sizeof(ngx_http_script_copy_code_t),
259 NULL);
260 if (copy == NULL) {
Igor Sysoev02f742b2005-04-08 15:18:55 +0000261 return NGX_ERROR;
262 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000263
264 copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
265 copy->len = name.len;
266
267 size = (sizeof(ngx_http_script_copy_code_t) + name.len
268 + sizeof(uintptr_t) - 1)
269 & ~(sizeof(uintptr_t) - 1);
270
271 copy = ngx_http_script_add_code(*sc->values, size, &sc->main);
272 if (copy == NULL) {
273 return NGX_ERROR;
274 }
275
276 copy->code = ngx_http_script_copy_code;
277 copy->len = name.len;
278
279 ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t),
280 name.data, name.len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000281 }
282
Igor Sysoev899b44e2005-05-12 14:58:06 +0000283 if (sc->complete_lengths) {
284 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
285 if (code == NULL) {
286 return NGX_ERROR;
287 }
288
289 *code = (uintptr_t) NULL;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000290 }
291
Igor Sysoev899b44e2005-05-12 14:58:06 +0000292 if (sc->complete_values) {
293 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
294 &sc->main);
295 if (code == NULL) {
296 return NGX_ERROR;
297 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000298
Igor Sysoev899b44e2005-05-12 14:58:06 +0000299 *code = (uintptr_t) NULL;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000300 }
301
Igor Sysoev02f742b2005-04-08 15:18:55 +0000302 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000303
304invalid_variable:
305
306 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name");
307
308 return NGX_ERROR;
Igor Sysoev7578ec92003-06-02 15:24:30 +0000309}
310
311
Igor Sysoev8fea8852006-03-15 09:53:04 +0000312u_char *
313ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,
314 void *code_lengths, size_t len, void *code_values)
315{
Igor Sysoev2c8f0572007-03-30 19:00:34 +0000316 ngx_uint_t i;
317 ngx_http_script_code_pt code;
318 ngx_http_script_len_code_pt lcode;
319 ngx_http_script_engine_t e;
320 ngx_http_core_main_conf_t *cmcf;
321
322 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
323
324 for (i = 0; i < cmcf->variables.nelts; i++) {
Igor Sysoev2d3f3f62007-10-14 18:56:15 +0000325 if (r->variables[i].no_cacheable) {
Igor Sysoev2c8f0572007-03-30 19:00:34 +0000326 r->variables[i].valid = 0;
327 r->variables[i].not_found = 0;
328 }
329 }
Igor Sysoev8fea8852006-03-15 09:53:04 +0000330
331 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
332
333 e.ip = code_lengths;
334 e.request = r;
335 e.flushed = 1;
336
337 while (*(uintptr_t *) e.ip) {
338 lcode = *(ngx_http_script_len_code_pt *) e.ip;
339 len += lcode(&e);
340 }
341
342
343 value->len = len;
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000344 value->data = ngx_pnalloc(r->pool, len);
Igor Sysoev8fea8852006-03-15 09:53:04 +0000345 if (value->data == NULL) {
346 return NULL;
347 }
348
349 e.ip = code_values;
350 e.pos = value->data;
351
352 while (*(uintptr_t *) e.ip) {
353 code = *(ngx_http_script_code_pt *) e.ip;
354 code((ngx_http_script_engine_t *) &e);
355 }
356
357 return e.pos;
358}
359
360
Igor Sysoev09c684b2005-11-09 17:25:55 +0000361void
Igor Sysoev2d3f3f62007-10-14 18:56:15 +0000362ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +0000363 ngx_array_t *indices)
364{
365 ngx_uint_t n, *index;
366
367 if (indices) {
368 index = indices->elts;
369 for (n = 0; n < indices->nelts; n++) {
Igor Sysoev2d3f3f62007-10-14 18:56:15 +0000370 if (r->variables[index[n]].no_cacheable) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000371 r->variables[index[n]].valid = 0;
372 r->variables[index[n]].not_found = 0;
373 }
374 }
375 }
376}
377
378
Igor Sysoev899b44e2005-05-12 14:58:06 +0000379void *
Igor Sysoev02f742b2005-04-08 15:18:55 +0000380ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
Igor Sysoev7578ec92003-06-02 15:24:30 +0000381{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000382 if (*codes == NULL) {
383 *codes = ngx_array_create(pool, 256, 1);
384 if (*codes == NULL) {
385 return NULL;
386 }
387 }
Igor Sysoev7578ec92003-06-02 15:24:30 +0000388
Igor Sysoev02f742b2005-04-08 15:18:55 +0000389 return ngx_array_push_n(*codes, size);
390}
Igor Sysoev7578ec92003-06-02 15:24:30 +0000391
Igor Sysoev899b44e2005-05-12 14:58:06 +0000392
393void *
394ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code)
395{
396 u_char *elts, **p;
397 void *new;
398
399 elts = codes->elts;
400
401 new = ngx_array_push_n(codes, size);
402 if (new == NULL) {
403 return NGX_CONF_ERROR;
404 }
405
406 if (code) {
407 if (elts != codes->elts) {
408 p = code;
409 *p += (u_char *) codes->elts - elts;
410 }
411 }
412
413 return new;
414}
Igor Sysoev02025fd2005-01-18 13:03:58 +0000415
Igor Sysoev02f742b2005-04-08 15:18:55 +0000416
417size_t
Igor Sysoev899b44e2005-05-12 14:58:06 +0000418ngx_http_script_copy_len_code(ngx_http_script_engine_t *e)
Igor Sysoev02f742b2005-04-08 15:18:55 +0000419{
420 ngx_http_script_copy_code_t *code;
421
Igor Sysoev899b44e2005-05-12 14:58:06 +0000422 code = (ngx_http_script_copy_code_t *) e->ip;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000423
Igor Sysoev899b44e2005-05-12 14:58:06 +0000424 e->ip += sizeof(ngx_http_script_copy_code_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000425
426 return code->len;
Igor Sysoev7578ec92003-06-02 15:24:30 +0000427}
428
429
Igor Sysoev02f742b2005-04-08 15:18:55 +0000430void
Igor Sysoev899b44e2005-05-12 14:58:06 +0000431ngx_http_script_copy_code(ngx_http_script_engine_t *e)
Igor Sysoev7578ec92003-06-02 15:24:30 +0000432{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000433 ngx_http_script_copy_code_t *code;
434
Igor Sysoev899b44e2005-05-12 14:58:06 +0000435 code = (ngx_http_script_copy_code_t *) e->ip;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000436
Igor Sysoev899b44e2005-05-12 14:58:06 +0000437 if (!e->skip) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000438 e->pos = ngx_copy(e->pos, e->ip + sizeof(ngx_http_script_copy_code_t),
439 code->len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000440 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000441
Igor Sysoev899b44e2005-05-12 14:58:06 +0000442 e->ip += sizeof(ngx_http_script_copy_code_t)
443 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
444
Igor Sysoev58364232006-11-14 12:45:03 +0000445 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
446 "http script copy: \"%V\"", &e->buf);
Igor Sysoev7578ec92003-06-02 15:24:30 +0000447}
448
449
Igor Sysoev02f742b2005-04-08 15:18:55 +0000450size_t
Igor Sysoev899b44e2005-05-12 14:58:06 +0000451ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e)
Igor Sysoev7578ec92003-06-02 15:24:30 +0000452{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000453 ngx_http_variable_value_t *value;
454 ngx_http_script_var_code_t *code;
455
Igor Sysoev899b44e2005-05-12 14:58:06 +0000456 code = (ngx_http_script_var_code_t *) e->ip;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000457
Igor Sysoev899b44e2005-05-12 14:58:06 +0000458 e->ip += sizeof(ngx_http_script_var_code_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000459
Igor Sysoev09c684b2005-11-09 17:25:55 +0000460 if (e->flushed) {
461 value = ngx_http_get_indexed_variable(e->request, code->index);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000462
Igor Sysoev09c684b2005-11-09 17:25:55 +0000463 } else {
464 value = ngx_http_get_flushed_variable(e->request, code->index);
465 }
466
467 if (value && !value->not_found) {
468 return value->len;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000469 }
470
Igor Sysoevf6e1fe32005-10-04 10:38:53 +0000471 return 0;
Igor Sysoev7578ec92003-06-02 15:24:30 +0000472}
473
474
Igor Sysoev02f742b2005-04-08 15:18:55 +0000475void
Igor Sysoev899b44e2005-05-12 14:58:06 +0000476ngx_http_script_copy_var_code(ngx_http_script_engine_t *e)
Igor Sysoev7578ec92003-06-02 15:24:30 +0000477{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000478 ngx_http_variable_value_t *value;
479 ngx_http_script_var_code_t *code;
480
Igor Sysoev899b44e2005-05-12 14:58:06 +0000481 code = (ngx_http_script_var_code_t *) e->ip;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000482
Igor Sysoev899b44e2005-05-12 14:58:06 +0000483 e->ip += sizeof(ngx_http_script_var_code_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000484
Igor Sysoev899b44e2005-05-12 14:58:06 +0000485 if (!e->skip) {
Igor Sysoev02f742b2005-04-08 15:18:55 +0000486
Igor Sysoev09c684b2005-11-09 17:25:55 +0000487 if (e->flushed) {
488 value = ngx_http_get_indexed_variable(e->request, code->index);
489
490 } else {
491 value = ngx_http_get_flushed_variable(e->request, code->index);
492 }
493
494 if (value && !value->not_found) {
495 e->pos = ngx_copy(e->pos, value->data, value->len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000496
Igor Sysoev58364232006-11-14 12:45:03 +0000497 ngx_log_debug1(NGX_LOG_DEBUG_HTTP,
498 e->request->connection->log, 0,
499 "http script var: \"%V\"", &e->buf);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000500 }
501 }
502}
503
504
505size_t
506ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e)
507{
508 ngx_http_script_copy_capture_code_t *code;
509
510 code = (ngx_http_script_copy_capture_code_t *) e->ip;
511
512 e->ip += sizeof(ngx_http_script_copy_capture_code_t);
513
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000514 if (code->n < e->ncaptures) {
Igor Sysoev04610ea2008-02-12 18:05:32 +0000515 if ((e->is_args || e->quote)
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000516 && (e->request->quoted_uri || e->request->plus_in_uri))
517 {
518 return e->captures[code->n + 1] - e->captures[code->n]
Igor Sysoevaf3b7ea2006-05-31 14:11:45 +0000519 + 2 * ngx_escape_uri(NULL,
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000520 &e->line.data[e->captures[code->n]],
Igor Sysoev899b44e2005-05-12 14:58:06 +0000521 e->captures[code->n + 1] - e->captures[code->n],
522 NGX_ESCAPE_ARGS);
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000523 } else {
524 return e->captures[code->n + 1] - e->captures[code->n];
525 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000526 }
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000527
528 return 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000529}
530
531
532void
533ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e)
534{
535 ngx_http_script_copy_capture_code_t *code;
536
537 code = (ngx_http_script_copy_capture_code_t *) e->ip;
538
539 e->ip += sizeof(ngx_http_script_copy_capture_code_t);
540
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000541 if (code->n < e->ncaptures) {
Igor Sysoev04610ea2008-02-12 18:05:32 +0000542 if ((e->is_args || e->quote)
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000543 && (e->request->quoted_uri || e->request->plus_in_uri))
544 {
545 e->pos = (u_char *) ngx_escape_uri(e->pos,
546 &e->line.data[e->captures[code->n]],
Igor Sysoev899b44e2005-05-12 14:58:06 +0000547 e->captures[code->n + 1] - e->captures[code->n],
548 NGX_ESCAPE_ARGS);
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000549 } else {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000550 e->pos = ngx_copy(e->pos,
551 &e->line.data[e->captures[code->n]],
552 e->captures[code->n + 1] - e->captures[code->n]);
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000553 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000554 }
555
556 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
557 "http script capture: \"%V\"", &e->buf);
558}
559
560
Igor Sysoev04610ea2008-02-12 18:05:32 +0000561size_t
562ngx_http_script_mark_args_code(ngx_http_script_engine_t *e)
563{
564 e->is_args = 1;
565 e->ip += sizeof(uintptr_t);
566
567 return 1;
568}
569
570
Igor Sysoev899b44e2005-05-12 14:58:06 +0000571void
572ngx_http_script_start_args_code(ngx_http_script_engine_t *e)
573{
574 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
575 "http script args");
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000576
Igor Sysoev899b44e2005-05-12 14:58:06 +0000577 e->args = e->pos;
578 e->ip += sizeof(uintptr_t);
579}
580
581
Igor Sysoev4959ec42005-05-23 12:07:45 +0000582
583#if (NGX_PCRE)
584
Igor Sysoev899b44e2005-05-12 14:58:06 +0000585void
586ngx_http_script_regex_start_code(ngx_http_script_engine_t *e)
587{
588 size_t len;
589 ngx_int_t rc;
590 ngx_uint_t n;
591 ngx_http_request_t *r;
592 ngx_http_script_engine_t le;
593 ngx_http_script_len_code_pt lcode;
594 ngx_http_script_regex_code_t *code;
595
596 code = (ngx_http_script_regex_code_t *) e->ip;
597
598 r = e->request;
599
600 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
601 "http script regex: \"%V\"", &code->name);
602
603 if (code->uri) {
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000604 e->line = r->uri;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000605 } else {
606 e->sp--;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000607 e->line.len = e->sp->len;
608 e->line.data = e->sp->data;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000609 }
610
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000611 rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000612
613 if (rc == NGX_REGEX_NO_MATCHED) {
614 if (e->log) {
615 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000616 "\"%V\" does not match \"%V\"",
617 &code->name, &e->line);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000618 }
619
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000620 e->ncaptures = 0;
621
Igor Sysoev899b44e2005-05-12 14:58:06 +0000622 if (code->test) {
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000623 if (code->negative_test) {
624 e->sp->len = 1;
625 e->sp->data = (u_char *) "1";
626
627 } else {
628 e->sp->len = 0;
629 e->sp->data = (u_char *) "";
630 }
631
Igor Sysoev899b44e2005-05-12 14:58:06 +0000632 e->sp++;
633
634 e->ip += sizeof(ngx_http_script_regex_code_t);
635 return;
636 }
637
638 e->ip += code->next;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000639 return;
640 }
641
Igor Sysoev899b44e2005-05-12 14:58:06 +0000642 if (rc < 0) {
643 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
644 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000645 rc, &e->line, &code->name);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000646
647 e->ip = ngx_http_script_exit;
648 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
649 return;
650 }
651
652 if (e->log) {
653 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000654 "\"%V\" matches \"%V\"", &code->name, &e->line);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000655 }
656
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000657 e->ncaptures = code->ncaptures;
658
Igor Sysoev899b44e2005-05-12 14:58:06 +0000659 if (code->test) {
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000660 if (code->negative_test) {
661 e->sp->len = 0;
662 e->sp->data = (u_char *) "";
663
664 } else {
665 e->sp->len = 1;
666 e->sp->data = (u_char *) "1";
667 }
668
Igor Sysoev899b44e2005-05-12 14:58:06 +0000669 e->sp++;
670
671 e->ip += sizeof(ngx_http_script_regex_code_t);
672 return;
673 }
674
675 if (code->status) {
676 e->status = code->status;
677
678 if (!code->redirect) {
679 e->ip = ngx_http_script_exit;
680 return;
681 }
682 }
683
684 if (code->uri) {
685 r->internal = 1;
686 r->valid_unparsed_uri = 0;
687
688 if (code->break_cycle) {
689 r->valid_location = 0;
Igor Sysoev5192b362005-07-08 14:34:20 +0000690 r->uri_changed = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000691
692 } else {
693 r->uri_changed = 1;
694 }
695 }
696
697 if (code->lengths == NULL) {
698 e->buf.len = code->size;
699
700 if (code->uri) {
701 if (rc && (r->quoted_uri || r->plus_in_uri)) {
702 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
703 NGX_ESCAPE_ARGS);
704 }
705 }
706
707 for (n = 1; n < (ngx_uint_t) rc; n++) {
708 e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n];
709 }
710
711 } else {
712 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
713
714 le.ip = code->lengths->elts;
Igor Sysoev3f8dc592006-08-28 16:57:48 +0000715 le.line = e->line;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000716 le.request = r;
717 le.captures = e->captures;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000718 le.ncaptures = e->ncaptures;
Igor Sysoev3f8dc592006-08-28 16:57:48 +0000719 le.quote = code->redirect;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000720
Igor Sysoev04610ea2008-02-12 18:05:32 +0000721 len = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000722
723 while (*(uintptr_t *) le.ip) {
724 lcode = *(ngx_http_script_len_code_pt *) le.ip;
725 len += lcode(&le);
726 }
727
728 e->buf.len = len;
Igor Sysoev04610ea2008-02-12 18:05:32 +0000729 e->is_args = le.is_args;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000730 }
731
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000732 if (code->add_args && r->args.len) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000733 e->buf.len += r->args.len + 1;
734 }
735
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +0000736 e->buf.data = ngx_pnalloc(r->pool, e->buf.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000737 if (e->buf.data == NULL) {
738 e->ip = ngx_http_script_exit;
739 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
740 return;
741 }
742
743 e->quote = code->redirect;
744
745 e->pos = e->buf.data;
746
747 e->ip += sizeof(ngx_http_script_regex_code_t);
748}
749
750
751void
752ngx_http_script_regex_end_code(ngx_http_script_engine_t *e)
753{
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000754 u_char *dst, *src;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000755 ngx_http_request_t *r;
756 ngx_http_script_regex_end_code_t *code;
757
758 code = (ngx_http_script_regex_end_code_t *) e->ip;
759
760 r = e->request;
761
762 e->quote = 0;
763
764 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
765 "http script regex end");
766
767 if (code->redirect) {
768
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000769 dst = e->buf.data;
770 src = e->buf.data;
771
Igor Sysoevf0a51cf2007-10-22 10:19:17 +0000772 ngx_unescape_uri(&dst, &src, e->pos - e->buf.data,
773 NGX_UNESCAPE_REDIRECT);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000774
775 if (src < e->pos) {
776 dst = ngx_copy(dst, src, e->pos - src);
777 }
778
779 e->pos = dst;
780
781 if (code->add_args && r->args.len) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000782 *e->pos++ = (u_char) (code->args ? '&' : '?');
Igor Sysoev09c684b2005-11-09 17:25:55 +0000783 e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000784 }
785
786 e->buf.len = e->pos - e->buf.data;
787
788 if (e->log) {
789 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
790 "rewritten redirect: \"%V\"", &e->buf);
791 }
792
793 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
794 if (r->headers_out.location == NULL) {
795 e->ip = ngx_http_script_exit;
796 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
797 return;
798 }
799
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000800 r->headers_out.location->hash = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000801 r->headers_out.location->key.len = sizeof("Location") - 1;
802 r->headers_out.location->key.data = (u_char *) "Location";
803 r->headers_out.location->value = e->buf;
804
805 e->ip += sizeof(ngx_http_script_regex_end_code_t);
806 return;
807 }
808
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000809 if (e->args) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000810 e->buf.len = e->args - e->buf.data;
811
812 if (code->add_args && r->args.len) {
813 *e->pos++ = '&';
Igor Sysoev09c684b2005-11-09 17:25:55 +0000814 e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000815 }
816
817 r->args.len = e->pos - e->args;
818 r->args.data = e->args;
819
820 e->args = NULL;
821
822 } else {
823 e->buf.len = e->pos - e->buf.data;
Igor Sysoev08e63d42006-08-14 15:09:38 +0000824
825 if (!code->add_args) {
826 r->args.len = 0;
827 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000828 }
829
830 if (e->log) {
831 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
832 "rewritten data: \"%V\", args: \"%V\"",
833 &e->buf, &r->args);
834 }
835
836 if (code->uri) {
837 r->uri = e->buf;
838
Igor Sysoevb85fd592005-08-23 15:36:54 +0000839 if (r->uri.len == 0) {
840 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
841 "the rewritten URI has a zero length");
842 e->ip = ngx_http_script_exit;
843 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
844 return;
845 }
846
Igor Sysoev899b44e2005-05-12 14:58:06 +0000847 if (ngx_http_set_exten(r) != NGX_OK) {
848 e->ip = ngx_http_script_exit;
849 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
850 return;
851 }
852 }
853
854 e->ip += sizeof(ngx_http_script_regex_end_code_t);
855}
856
Igor Sysoev4959ec42005-05-23 12:07:45 +0000857#endif
858
Igor Sysoev899b44e2005-05-12 14:58:06 +0000859
860void
861ngx_http_script_return_code(ngx_http_script_engine_t *e)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000862{
Igor Sysoev899b44e2005-05-12 14:58:06 +0000863 ngx_http_script_return_code_t *code;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000864
Igor Sysoev899b44e2005-05-12 14:58:06 +0000865 code = (ngx_http_script_return_code_t *) e->ip;
866
867 e->status = code->status;
868
Igor Sysoevafd7ec52006-05-29 17:28:12 +0000869 if (code->status == NGX_HTTP_NO_CONTENT) {
870 e->request->header_only = 1;
Igor Sysoev8fd830a2006-10-02 10:22:51 +0000871 e->request->zero_body = 1;
Igor Sysoevafd7ec52006-05-29 17:28:12 +0000872 }
873
Igor Sysoev899b44e2005-05-12 14:58:06 +0000874 e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t);
875}
876
877
878void
Igor Sysoev5192b362005-07-08 14:34:20 +0000879ngx_http_script_break_code(ngx_http_script_engine_t *e)
880{
881 e->request->uri_changed = 0;
882
883 e->ip = ngx_http_script_exit;
884}
885
886
887void
Igor Sysoev899b44e2005-05-12 14:58:06 +0000888ngx_http_script_if_code(ngx_http_script_engine_t *e)
889{
890 ngx_http_script_if_code_t *code;
891
892 code = (ngx_http_script_if_code_t *) e->ip;
893
894 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
895 "http script if");
896
897 e->sp--;
898
Igor Sysoev09c684b2005-11-09 17:25:55 +0000899 if (e->sp->len && e->sp->data[0] != '0') {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000900 if (code->loc_conf) {
901 e->request->loc_conf = code->loc_conf;
Igor Sysoevb85fd592005-08-23 15:36:54 +0000902 ngx_http_update_location_config(e->request);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000903 }
904
905 e->ip += sizeof(ngx_http_script_if_code_t);
906 return;
907 }
908
909 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000910 "http script if: false");
Igor Sysoev899b44e2005-05-12 14:58:06 +0000911
912 e->ip += code->next;
913}
914
915
916void
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000917ngx_http_script_equal_code(ngx_http_script_engine_t *e)
918{
919 ngx_http_variable_value_t *val, *res;
920
921 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
922 "http script equal");
923
924 e->sp--;
925 val = e->sp;
926 res = e->sp - 1;
927
928 e->ip += sizeof(uintptr_t);
929
930 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len)
931 == 0)
932 {
933 *res = ngx_http_variable_true_value;
934 return;
935 }
936
937 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
938 "http script equal: no");
939
940 *res = ngx_http_variable_null_value;
941}
942
943
944void
945ngx_http_script_not_equal_code(ngx_http_script_engine_t *e)
946{
947 ngx_http_variable_value_t *val, *res;
948
949 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
950 "http script not equal");
951
952 e->sp--;
953 val = e->sp;
954 res = e->sp - 1;
955
956 e->ip += sizeof(uintptr_t);
957
958 if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len)
959 == 0)
960 {
961 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
962 "http script not equal: no");
963
964 *res = ngx_http_variable_null_value;
965 return;
966 }
967
968 *res = ngx_http_variable_true_value;
969}
970
971
972void
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000973ngx_http_script_file_code(ngx_http_script_engine_t *e)
974{
Igor Sysoev140c7552007-09-01 12:12:48 +0000975 ngx_str_t path;
976 ngx_http_request_t *r;
977 ngx_open_file_info_t of;
978 ngx_http_core_loc_conf_t *clcf;
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000979 ngx_http_variable_value_t *value;
980 ngx_http_script_file_code_t *code;
981
982 value = e->sp - 1;
983
984 code = (ngx_http_script_file_code_t *) e->ip;
985 e->ip += sizeof(ngx_http_script_file_code_t);
986
Igor Sysoev140c7552007-09-01 12:12:48 +0000987 path.len = value->len - 1;
988 path.data = value->data;
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000989
Igor Sysoev140c7552007-09-01 12:12:48 +0000990 r = e->request;
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000991
Igor Sysoev140c7552007-09-01 12:12:48 +0000992 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
993 "http script file op %p \"%V\"", code->op, &path);
994
995 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
996
Igor Sysoev5a0eac82008-06-26 14:07:59 +0000997 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
998
Igor Sysoev385af282008-07-30 12:34:04 +0000999 of.directio = clcf->directio;
Igor Sysoev9b9616e2007-12-21 16:19:48 +00001000 of.valid = clcf->open_file_cache_valid;
Igor Sysoevf3b0e492007-12-22 13:19:39 +00001001 of.min_uses = clcf->open_file_cache_min_uses;
Igor Sysoev140c7552007-09-01 12:12:48 +00001002 of.errors = clcf->open_file_cache_errors;
Igor Sysoev9afd58f2007-09-03 08:41:42 +00001003 of.events = clcf->open_file_cache_events;
Igor Sysoev140c7552007-09-01 12:12:48 +00001004
1005 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
Igor Sysoev86b91592007-12-27 20:32:43 +00001006 != NGX_OK)
Igor Sysoev140c7552007-09-01 12:12:48 +00001007 {
1008 if (of.err != NGX_ENOENT && of.err != NGX_ENOTDIR) {
1009 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001010 ngx_file_info_n " \"%s\" failed", value->data);
1011 }
1012
1013 switch (code->op) {
Igor Sysoevc55a1042006-08-09 19:59:45 +00001014
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001015 case ngx_http_script_file_plain:
Igor Sysoevb71c6902006-08-04 16:04:04 +00001016 case ngx_http_script_file_dir:
1017 case ngx_http_script_file_exists:
1018 case ngx_http_script_file_exec:
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001019 goto false_value;
Igor Sysoevc55a1042006-08-09 19:59:45 +00001020
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001021 case ngx_http_script_file_not_plain:
Igor Sysoevb71c6902006-08-04 16:04:04 +00001022 case ngx_http_script_file_not_dir:
Igor Sysoevc55a1042006-08-09 19:59:45 +00001023 case ngx_http_script_file_not_exists:
Igor Sysoevb71c6902006-08-04 16:04:04 +00001024 case ngx_http_script_file_not_exec:
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001025 goto true_value;
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001026 }
1027
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001028 goto false_value;
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001029 }
1030
1031 switch (code->op) {
1032 case ngx_http_script_file_plain:
Igor Sysoev140c7552007-09-01 12:12:48 +00001033 if (of.is_file) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001034 goto true_value;
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001035 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001036 goto false_value;
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001037
1038 case ngx_http_script_file_not_plain:
Igor Sysoev140c7552007-09-01 12:12:48 +00001039 if (of.is_file) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001040 goto false_value;
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001041 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001042 goto true_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001043
1044 case ngx_http_script_file_dir:
Igor Sysoev140c7552007-09-01 12:12:48 +00001045 if (of.is_dir) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001046 goto true_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001047 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001048 goto false_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001049
1050 case ngx_http_script_file_not_dir:
Igor Sysoev140c7552007-09-01 12:12:48 +00001051 if (of.is_dir) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001052 goto false_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001053 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001054 goto true_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001055
1056 case ngx_http_script_file_exists:
Igor Sysoev140c7552007-09-01 12:12:48 +00001057 if (of.is_file || of.is_dir || of.is_link) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001058 goto true_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001059 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001060 goto false_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001061
1062 case ngx_http_script_file_not_exists:
Igor Sysoev140c7552007-09-01 12:12:48 +00001063 if (of.is_file || of.is_dir || of.is_link) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001064 goto false_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001065 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001066 goto true_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001067
Igor Sysoevb71c6902006-08-04 16:04:04 +00001068 case ngx_http_script_file_exec:
Igor Sysoev140c7552007-09-01 12:12:48 +00001069 if (of.is_exec) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001070 goto true_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001071 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001072 goto false_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001073
1074 case ngx_http_script_file_not_exec:
Igor Sysoev140c7552007-09-01 12:12:48 +00001075 if (of.is_exec) {
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001076 goto false_value;
Igor Sysoevb71c6902006-08-04 16:04:04 +00001077 }
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001078 goto true_value;
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001079 }
1080
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001081false_value:
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001082
Igor Sysoev140c7552007-09-01 12:12:48 +00001083 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001084 "http script file op false");
1085
1086 *value = ngx_http_variable_null_value;
1087 return;
1088
Igor Sysoeva3e9f7d2008-07-31 07:55:46 +00001089true_value:
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001090
1091 *value = ngx_http_variable_true_value;
1092 return;
1093}
1094
1095
1096void
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001097ngx_http_script_complex_value_code(ngx_http_script_engine_t *e)
1098{
1099 size_t len;
1100 ngx_http_script_engine_t le;
1101 ngx_http_script_len_code_pt lcode;
1102 ngx_http_script_complex_value_code_t *code;
1103
1104 code = (ngx_http_script_complex_value_code_t *) e->ip;
1105
1106 e->ip += sizeof(ngx_http_script_complex_value_code_t);
1107
1108 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1109 "http script complex value");
1110
1111 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
1112
1113 le.ip = code->lengths->elts;
Igor Sysoevaf3b7ea2006-05-31 14:11:45 +00001114 le.line = e->line;
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001115 le.request = e->request;
1116 le.captures = e->captures;
1117 le.ncaptures = e->ncaptures;
Igor Sysoevaf3b7ea2006-05-31 14:11:45 +00001118 le.quote = e->quote;
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001119
1120 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
1121 lcode = *(ngx_http_script_len_code_pt *) le.ip;
1122 }
1123
1124 e->buf.len = len;
Igor Sysoev7f6b2ff2008-06-17 15:00:30 +00001125 e->buf.data = ngx_pnalloc(e->request->pool, len);
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001126 if (e->buf.data == NULL) {
1127 e->ip = ngx_http_script_exit;
1128 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1129 return;
1130 }
1131
1132 e->pos = e->buf.data;
1133
Igor Sysoev09c684b2005-11-09 17:25:55 +00001134 e->sp->len = e->buf.len;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001135 e->sp->data = e->buf.data;
Igor Sysoeve31e90b2005-05-19 13:25:22 +00001136 e->sp++;
1137}
1138
1139
1140void
Igor Sysoev899b44e2005-05-12 14:58:06 +00001141ngx_http_script_value_code(ngx_http_script_engine_t *e)
1142{
1143 ngx_http_script_value_code_t *code;
1144
1145 code = (ngx_http_script_value_code_t *) e->ip;
1146
1147 e->ip += sizeof(ngx_http_script_value_code_t);
1148
Igor Sysoev09c684b2005-11-09 17:25:55 +00001149 e->sp->len = code->text_len;
1150 e->sp->data = (u_char *) code->text_data;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001151
1152 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
Igor Sysoev5ba67392007-10-09 18:44:59 +00001153 "http script value: \"%v\"", e->sp);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001154
Igor Sysoev899b44e2005-05-12 14:58:06 +00001155 e->sp++;
1156}
1157
1158
1159void
1160ngx_http_script_set_var_code(ngx_http_script_engine_t *e)
1161{
1162 ngx_http_request_t *r;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001163 ngx_http_script_var_code_t *code;
1164
1165 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1166 "http script set var");
1167
1168 code = (ngx_http_script_var_code_t *) e->ip;
1169
1170 e->ip += sizeof(ngx_http_script_var_code_t);
1171
1172 r = e->request;
1173
Igor Sysoev899b44e2005-05-12 14:58:06 +00001174 e->sp--;
1175
Igor Sysoev09c684b2005-11-09 17:25:55 +00001176 r->variables[code->index].len = e->sp->len;
1177 r->variables[code->index].valid = 1;
Igor Sysoev2d3f3f62007-10-14 18:56:15 +00001178 r->variables[code->index].no_cacheable = 0;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001179 r->variables[code->index].not_found = 0;
1180 r->variables[code->index].data = e->sp->data;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001181}
1182
1183
1184void
Igor Sysoev7bdb7202006-04-19 15:30:56 +00001185ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e)
1186{
1187 ngx_http_script_var_handler_code_t *code;
1188
1189 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1190 "http script set var handler");
1191
1192 code = (ngx_http_script_var_handler_code_t *) e->ip;
1193
1194 e->ip += sizeof(ngx_http_script_var_handler_code_t);
1195
1196 e->sp--;
1197
1198 code->handler(e->request, e->sp, code->data);
1199}
1200
1201
1202void
Igor Sysoev899b44e2005-05-12 14:58:06 +00001203ngx_http_script_var_code(ngx_http_script_engine_t *e)
1204{
1205 ngx_http_variable_value_t *value;
1206 ngx_http_script_var_code_t *code;
1207
1208 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1209 "http script var");
1210
1211 code = (ngx_http_script_var_code_t *) e->ip;
1212
1213 e->ip += sizeof(ngx_http_script_var_code_t);
1214
Igor Sysoev09c684b2005-11-09 17:25:55 +00001215 value = ngx_http_get_flushed_variable(e->request, code->index);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001216
Igor Sysoev09c684b2005-11-09 17:25:55 +00001217 if (value && !value->not_found) {
1218 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
Igor Sysoev0d4b3722007-08-20 09:57:19 +00001219 "http script var: \"%v\"", value);
Igor Sysoev09c684b2005-11-09 17:25:55 +00001220
Igor Sysoevf6e1fe32005-10-04 10:38:53 +00001221 *e->sp = *value;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001222 e->sp++;
1223
1224 return;
1225 }
1226
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001227 *e->sp = ngx_http_variable_null_value;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001228 e->sp++;
1229}
1230
1231
1232void
1233ngx_http_script_nop_code(ngx_http_script_engine_t *e)
1234{
1235 e->ip += sizeof(uintptr_t);
Igor Sysoev7578ec92003-06-02 15:24:30 +00001236}