56
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
644
|
4 * Copyright (C) Nginx, Inc.
|
56
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
464
|
13 static ngx_int_t ngx_http_script_init_arrays(ngx_http_script_compile_t *sc);
|
|
14 static ngx_int_t ngx_http_script_done(ngx_http_script_compile_t *sc);
|
|
15 static ngx_int_t ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc,
|
|
16 ngx_str_t *value, ngx_uint_t last);
|
|
17 static ngx_int_t ngx_http_script_add_var_code(ngx_http_script_compile_t *sc,
|
|
18 ngx_str_t *name);
|
|
19 static ngx_int_t ngx_http_script_add_args_code(ngx_http_script_compile_t *sc);
|
|
20 #if (NGX_PCRE)
|
|
21 static ngx_int_t ngx_http_script_add_capture_code(ngx_http_script_compile_t *sc,
|
|
22 ngx_uint_t n);
|
|
23 #endif
|
|
24 static ngx_int_t
|
|
25 ngx_http_script_add_full_name_code(ngx_http_script_compile_t *sc);
|
|
26 static size_t ngx_http_script_full_name_len_code(ngx_http_script_engine_t *e);
|
|
27 static void ngx_http_script_full_name_code(ngx_http_script_engine_t *e);
|
|
28
|
|
29
|
58
|
30 #define ngx_http_script_exit (u_char *) &ngx_http_script_exit_code
|
|
31
|
|
32 static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;
|
|
33
|
|
34
|
464
|
35 void
|
468
|
36 ngx_http_script_flush_complex_value(ngx_http_request_t *r,
|
464
|
37 ngx_http_complex_value_t *val)
|
|
38 {
|
|
39 ngx_uint_t *index;
|
|
40
|
|
41 index = val->flushes;
|
|
42
|
|
43 if (index) {
|
|
44 while (*index != (ngx_uint_t) -1) {
|
|
45
|
|
46 if (r->variables[*index].no_cacheable) {
|
|
47 r->variables[*index].valid = 0;
|
|
48 r->variables[*index].not_found = 0;
|
|
49 }
|
|
50
|
|
51 index++;
|
|
52 }
|
|
53 }
|
|
54 }
|
|
55
|
|
56
|
|
57 ngx_int_t
|
|
58 ngx_http_complex_value(ngx_http_request_t *r, ngx_http_complex_value_t *val,
|
|
59 ngx_str_t *value)
|
|
60 {
|
|
61 size_t len;
|
|
62 ngx_http_script_code_pt code;
|
|
63 ngx_http_script_len_code_pt lcode;
|
|
64 ngx_http_script_engine_t e;
|
|
65
|
|
66 if (val->lengths == NULL) {
|
|
67 *value = val->value;
|
|
68 return NGX_OK;
|
|
69 }
|
|
70
|
468
|
71 ngx_http_script_flush_complex_value(r, val);
|
464
|
72
|
|
73 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
|
74
|
|
75 e.ip = val->lengths;
|
|
76 e.request = r;
|
|
77 e.flushed = 1;
|
|
78
|
|
79 len = 0;
|
|
80
|
|
81 while (*(uintptr_t *) e.ip) {
|
|
82 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
83 len += lcode(&e);
|
|
84 }
|
|
85
|
|
86 value->len = len;
|
|
87 value->data = ngx_pnalloc(r->pool, len);
|
|
88 if (value->data == NULL) {
|
|
89 return NGX_ERROR;
|
|
90 }
|
|
91
|
|
92 e.ip = val->values;
|
|
93 e.pos = value->data;
|
|
94 e.buf = *value;
|
|
95
|
|
96 while (*(uintptr_t *) e.ip) {
|
|
97 code = *(ngx_http_script_code_pt *) e.ip;
|
|
98 code((ngx_http_script_engine_t *) &e);
|
|
99 }
|
|
100
|
|
101 *value = e.buf;
|
|
102
|
|
103 return NGX_OK;
|
|
104 }
|
|
105
|
|
106
|
|
107 ngx_int_t
|
|
108 ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
|
|
109 {
|
|
110 ngx_str_t *v;
|
|
111 ngx_uint_t i, n, nv, nc;
|
|
112 ngx_array_t flushes, lengths, values, *pf, *pl, *pv;
|
|
113 ngx_http_script_compile_t sc;
|
|
114
|
|
115 v = ccv->value;
|
|
116
|
|
117 if (v->len == 0) {
|
|
118 ngx_conf_log_error(NGX_LOG_EMERG, ccv->cf, 0, "empty parameter");
|
|
119 return NGX_ERROR;
|
|
120 }
|
|
121
|
|
122 nv = 0;
|
|
123 nc = 0;
|
|
124
|
|
125 for (i = 0; i < v->len; i++) {
|
|
126 if (v->data[i] == '$') {
|
|
127 if (v->data[i + 1] >= '1' && v->data[i + 1] <= '9') {
|
|
128 nc++;
|
|
129
|
|
130 } else {
|
|
131 nv++;
|
|
132 }
|
|
133 }
|
|
134 }
|
|
135
|
|
136 if (v->data[0] != '$' && (ccv->conf_prefix || ccv->root_prefix)) {
|
|
137
|
|
138 if (ngx_conf_full_name(ccv->cf->cycle, v, ccv->conf_prefix) != NGX_OK) {
|
|
139 return NGX_ERROR;
|
|
140 }
|
|
141
|
|
142 ccv->conf_prefix = 0;
|
|
143 ccv->root_prefix = 0;
|
|
144 }
|
|
145
|
468
|
146 ccv->complex_value->value = *v;
|
|
147 ccv->complex_value->flushes = NULL;
|
|
148 ccv->complex_value->lengths = NULL;
|
|
149 ccv->complex_value->values = NULL;
|
|
150
|
464
|
151 if (nv == 0 && nc == 0) {
|
|
152 return NGX_OK;
|
|
153 }
|
|
154
|
|
155 n = nv + 1;
|
|
156
|
|
157 if (ngx_array_init(&flushes, ccv->cf->pool, n, sizeof(ngx_uint_t))
|
|
158 != NGX_OK)
|
|
159 {
|
|
160 return NGX_ERROR;
|
|
161 }
|
|
162
|
|
163 n = nv * (2 * sizeof(ngx_http_script_copy_code_t)
|
|
164 + sizeof(ngx_http_script_var_code_t))
|
|
165 + sizeof(uintptr_t);
|
|
166
|
|
167 if (ngx_array_init(&lengths, ccv->cf->pool, n, 1) != NGX_OK) {
|
|
168 return NGX_ERROR;
|
|
169 }
|
|
170
|
|
171 n = (nv * (2 * sizeof(ngx_http_script_copy_code_t)
|
|
172 + sizeof(ngx_http_script_var_code_t))
|
|
173 + sizeof(uintptr_t)
|
|
174 + v->len
|
|
175 + sizeof(uintptr_t) - 1)
|
|
176 & ~(sizeof(uintptr_t) - 1);
|
|
177
|
|
178 if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {
|
|
179 return NGX_ERROR;
|
|
180 }
|
|
181
|
|
182 pf = &flushes;
|
|
183 pl = &lengths;
|
|
184 pv = &values;
|
|
185
|
|
186 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
187
|
|
188 sc.cf = ccv->cf;
|
|
189 sc.source = v;
|
|
190 sc.flushes = &pf;
|
|
191 sc.lengths = &pl;
|
|
192 sc.values = &pv;
|
|
193 sc.complete_lengths = 1;
|
|
194 sc.complete_values = 1;
|
|
195 sc.zero = ccv->zero;
|
|
196 sc.conf_prefix = ccv->conf_prefix;
|
|
197 sc.root_prefix = ccv->root_prefix;
|
|
198
|
|
199 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
200 return NGX_ERROR;
|
|
201 }
|
|
202
|
|
203 if (flushes.nelts) {
|
|
204 ccv->complex_value->flushes = flushes.elts;
|
|
205 ccv->complex_value->flushes[flushes.nelts] = (ngx_uint_t) -1;
|
|
206 }
|
|
207
|
|
208 ccv->complex_value->lengths = lengths.elts;
|
|
209 ccv->complex_value->values = values.elts;
|
|
210
|
|
211 return NGX_OK;
|
|
212 }
|
|
213
|
|
214
|
596
|
215 char *
|
598
|
216 ngx_http_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
596
|
217 {
|
|
218 char *p = conf;
|
|
219
|
|
220 ngx_str_t *value;
|
|
221 ngx_http_complex_value_t **cv;
|
|
222 ngx_http_compile_complex_value_t ccv;
|
|
223
|
|
224 cv = (ngx_http_complex_value_t **) (p + cmd->offset);
|
|
225
|
|
226 if (*cv != NULL) {
|
|
227 return "duplicate";
|
|
228 }
|
|
229
|
|
230 *cv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
|
|
231 if (*cv == NULL) {
|
|
232 return NGX_CONF_ERROR;
|
|
233 }
|
|
234
|
|
235 value = cf->args->elts;
|
|
236
|
|
237 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
238
|
|
239 ccv.cf = cf;
|
|
240 ccv.value = &value[1];
|
|
241 ccv.complex_value = *cv;
|
|
242
|
|
243 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
244 return NGX_CONF_ERROR;
|
|
245 }
|
|
246
|
|
247 return NGX_CONF_OK;
|
|
248 }
|
|
249
|
|
250
|
588
|
251 ngx_int_t
|
|
252 ngx_http_test_predicates(ngx_http_request_t *r, ngx_array_t *predicates)
|
|
253 {
|
|
254 ngx_str_t val;
|
|
255 ngx_uint_t i;
|
|
256 ngx_http_complex_value_t *cv;
|
|
257
|
|
258 if (predicates == NULL) {
|
|
259 return NGX_OK;
|
|
260 }
|
|
261
|
|
262 cv = predicates->elts;
|
|
263
|
|
264 for (i = 0; i < predicates->nelts; i++) {
|
|
265 if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
|
|
266 return NGX_ERROR;
|
|
267 }
|
|
268
|
622
|
269 if (val.len && (val.len != 1 || val.data[0] != '0')) {
|
588
|
270 return NGX_DECLINED;
|
|
271 }
|
|
272 }
|
|
273
|
|
274 return NGX_OK;
|
|
275 }
|
|
276
|
|
277
|
|
278 char *
|
|
279 ngx_http_set_predicate_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
280 {
|
|
281 char *p = conf;
|
|
282
|
|
283 ngx_str_t *value;
|
|
284 ngx_uint_t i;
|
|
285 ngx_array_t **a;
|
|
286 ngx_http_complex_value_t *cv;
|
|
287 ngx_http_compile_complex_value_t ccv;
|
|
288
|
|
289 a = (ngx_array_t **) (p + cmd->offset);
|
|
290
|
|
291 if (*a == NGX_CONF_UNSET_PTR) {
|
|
292 *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_complex_value_t));
|
|
293 if (*a == NULL) {
|
|
294 return NGX_CONF_ERROR;
|
|
295 }
|
|
296 }
|
|
297
|
|
298 value = cf->args->elts;
|
|
299
|
|
300 for (i = 1; i < cf->args->nelts; i++) {
|
|
301 cv = ngx_array_push(*a);
|
|
302 if (cv == NULL) {
|
|
303 return NGX_CONF_ERROR;
|
|
304 }
|
|
305
|
|
306 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
307
|
|
308 ccv.cf = cf;
|
|
309 ccv.value = &value[i];
|
|
310 ccv.complex_value = cv;
|
|
311
|
|
312 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
313 return NGX_CONF_ERROR;
|
|
314 }
|
|
315 }
|
|
316
|
|
317 return NGX_CONF_OK;
|
|
318 }
|
|
319
|
|
320
|
58
|
321 ngx_uint_t
|
|
322 ngx_http_script_variables_count(ngx_str_t *value)
|
56
|
323 {
|
58
|
324 ngx_uint_t i, n;
|
56
|
325
|
58
|
326 for (n = 0, i = 0; i < value->len; i++) {
|
|
327 if (value->data[i] == '$') {
|
|
328 n++;
|
|
329 }
|
56
|
330 }
|
|
331
|
58
|
332 return n;
|
|
333 }
|
|
334
|
|
335
|
|
336 ngx_int_t
|
|
337 ngx_http_script_compile(ngx_http_script_compile_t *sc)
|
|
338 {
|
464
|
339 u_char ch;
|
|
340 ngx_str_t name;
|
|
341 ngx_uint_t i, bracket;
|
122
|
342
|
464
|
343 if (ngx_http_script_init_arrays(sc) != NGX_OK) {
|
|
344 return NGX_ERROR;
|
58
|
345 }
|
|
346
|
|
347 for (i = 0; i < sc->source->len; /* void */ ) {
|
|
348
|
|
349 name.len = 0;
|
|
350
|
|
351 if (sc->source->data[i] == '$') {
|
|
352
|
|
353 if (++i == sc->source->len) {
|
|
354 goto invalid_variable;
|
|
355 }
|
|
356
|
456
|
357 #if (NGX_PCRE)
|
464
|
358 {
|
|
359 ngx_uint_t n;
|
456
|
360
|
58
|
361 if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') {
|
|
362
|
190
|
363 n = sc->source->data[i] - '0';
|
|
364
|
|
365 if (sc->captures_mask & (1 << n)) {
|
|
366 sc->dup_capture = 1;
|
|
367 }
|
|
368
|
|
369 sc->captures_mask |= 1 << n;
|
|
370
|
464
|
371 if (ngx_http_script_add_capture_code(sc, n) != NGX_OK) {
|
58
|
372 return NGX_ERROR;
|
|
373 }
|
|
374
|
|
375 i++;
|
|
376
|
|
377 continue;
|
|
378 }
|
464
|
379 }
|
456
|
380 #endif
|
|
381
|
58
|
382 if (sc->source->data[i] == '{') {
|
|
383 bracket = 1;
|
|
384
|
|
385 if (++i == sc->source->len) {
|
|
386 goto invalid_variable;
|
|
387 }
|
|
388
|
|
389 name.data = &sc->source->data[i];
|
|
390
|
|
391 } else {
|
|
392 bracket = 0;
|
|
393 name.data = &sc->source->data[i];
|
|
394 }
|
|
395
|
|
396 for ( /* void */ ; i < sc->source->len; i++, name.len++) {
|
|
397 ch = sc->source->data[i];
|
56
|
398
|
58
|
399 if (ch == '}' && bracket) {
|
|
400 i++;
|
|
401 bracket = 0;
|
|
402 break;
|
|
403 }
|
|
404
|
|
405 if ((ch >= 'A' && ch <= 'Z')
|
|
406 || (ch >= 'a' && ch <= 'z')
|
|
407 || (ch >= '0' && ch <= '9')
|
|
408 || ch == '_')
|
|
409 {
|
|
410 continue;
|
|
411 }
|
|
412
|
|
413 break;
|
|
414 }
|
|
415
|
|
416 if (bracket) {
|
|
417 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
|
|
418 "the closing bracket in \"%V\" "
|
|
419 "variable is missing", &name);
|
56
|
420 return NGX_ERROR;
|
|
421 }
|
|
422
|
58
|
423 if (name.len == 0) {
|
|
424 goto invalid_variable;
|
|
425 }
|
56
|
426
|
58
|
427 sc->variables++;
|
56
|
428
|
464
|
429 if (ngx_http_script_add_var_code(sc, &name) != NGX_OK) {
|
56
|
430 return NGX_ERROR;
|
|
431 }
|
|
432
|
58
|
433 continue;
|
|
434 }
|
56
|
435
|
58
|
436 if (sc->source->data[i] == '?' && sc->compile_args) {
|
|
437 sc->args = 1;
|
|
438 sc->compile_args = 0;
|
56
|
439
|
464
|
440 if (ngx_http_script_add_args_code(sc) != NGX_OK) {
|
366
|
441 return NGX_ERROR;
|
|
442 }
|
|
443
|
58
|
444 i++;
|
|
445
|
56
|
446 continue;
|
|
447 }
|
|
448
|
58
|
449 name.data = &sc->source->data[i];
|
|
450
|
444
|
451 while (i < sc->source->len) {
|
|
452
|
|
453 if (sc->source->data[i] == '$') {
|
|
454 break;
|
|
455 }
|
|
456
|
|
457 if (sc->source->data[i] == '?') {
|
|
458
|
|
459 sc->args = 1;
|
|
460
|
|
461 if (sc->compile_args) {
|
|
462 break;
|
|
463 }
|
|
464 }
|
|
465
|
58
|
466 i++;
|
|
467 name.len++;
|
|
468 }
|
|
469
|
|
470 sc->size += name.len;
|
|
471
|
464
|
472 if (ngx_http_script_add_copy_code(sc, &name, (i == sc->source->len))
|
|
473 != NGX_OK)
|
|
474 {
|
58
|
475 return NGX_ERROR;
|
|
476 }
|
56
|
477 }
|
|
478
|
464
|
479 return ngx_http_script_done(sc);
|
58
|
480
|
|
481 invalid_variable:
|
|
482
|
|
483 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name");
|
|
484
|
|
485 return NGX_ERROR;
|
56
|
486 }
|
|
487
|
|
488
|
172
|
489 u_char *
|
|
490 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,
|
|
491 void *code_lengths, size_t len, void *code_values)
|
|
492 {
|
294
|
493 ngx_uint_t i;
|
|
494 ngx_http_script_code_pt code;
|
|
495 ngx_http_script_len_code_pt lcode;
|
|
496 ngx_http_script_engine_t e;
|
|
497 ngx_http_core_main_conf_t *cmcf;
|
|
498
|
|
499 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
500
|
|
501 for (i = 0; i < cmcf->variables.nelts; i++) {
|
340
|
502 if (r->variables[i].no_cacheable) {
|
294
|
503 r->variables[i].valid = 0;
|
|
504 r->variables[i].not_found = 0;
|
|
505 }
|
|
506 }
|
172
|
507
|
|
508 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
|
509
|
|
510 e.ip = code_lengths;
|
|
511 e.request = r;
|
|
512 e.flushed = 1;
|
|
513
|
|
514 while (*(uintptr_t *) e.ip) {
|
|
515 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
|
516 len += lcode(&e);
|
|
517 }
|
|
518
|
|
519
|
|
520 value->len = len;
|
382
|
521 value->data = ngx_pnalloc(r->pool, len);
|
172
|
522 if (value->data == NULL) {
|
|
523 return NULL;
|
|
524 }
|
|
525
|
|
526 e.ip = code_values;
|
|
527 e.pos = value->data;
|
|
528
|
|
529 while (*(uintptr_t *) e.ip) {
|
|
530 code = *(ngx_http_script_code_pt *) e.ip;
|
|
531 code((ngx_http_script_engine_t *) &e);
|
|
532 }
|
|
533
|
|
534 return e.pos;
|
|
535 }
|
|
536
|
|
537
|
122
|
538 void
|
340
|
539 ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r,
|
122
|
540 ngx_array_t *indices)
|
|
541 {
|
|
542 ngx_uint_t n, *index;
|
|
543
|
|
544 if (indices) {
|
|
545 index = indices->elts;
|
|
546 for (n = 0; n < indices->nelts; n++) {
|
340
|
547 if (r->variables[index[n]].no_cacheable) {
|
122
|
548 r->variables[index[n]].valid = 0;
|
|
549 r->variables[index[n]].not_found = 0;
|
|
550 }
|
|
551 }
|
|
552 }
|
|
553 }
|
|
554
|
|
555
|
464
|
556 static ngx_int_t
|
|
557 ngx_http_script_init_arrays(ngx_http_script_compile_t *sc)
|
|
558 {
|
|
559 ngx_uint_t n;
|
|
560
|
|
561 if (sc->flushes && *sc->flushes == NULL) {
|
|
562 n = sc->variables ? sc->variables : 1;
|
|
563 *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t));
|
|
564 if (*sc->flushes == NULL) {
|
|
565 return NGX_ERROR;
|
|
566 }
|
|
567 }
|
|
568
|
|
569 if (*sc->lengths == NULL) {
|
|
570 n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
|
|
571 + sizeof(ngx_http_script_var_code_t))
|
|
572 + sizeof(uintptr_t);
|
|
573
|
|
574 *sc->lengths = ngx_array_create(sc->cf->pool, n, 1);
|
|
575 if (*sc->lengths == NULL) {
|
|
576 return NGX_ERROR;
|
|
577 }
|
|
578 }
|
|
579
|
|
580 if (*sc->values == NULL) {
|
|
581 n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
|
|
582 + sizeof(ngx_http_script_var_code_t))
|
|
583 + sizeof(uintptr_t)
|
|
584 + sc->source->len
|
|
585 + sizeof(uintptr_t) - 1)
|
|
586 & ~(sizeof(uintptr_t) - 1);
|
|
587
|
|
588 *sc->values = ngx_array_create(sc->cf->pool, n, 1);
|
|
589 if (*sc->values == NULL) {
|
|
590 return NGX_ERROR;
|
|
591 }
|
|
592 }
|
|
593
|
|
594 sc->variables = 0;
|
|
595
|
|
596 return NGX_OK;
|
|
597 }
|
|
598
|
|
599
|
|
600 static ngx_int_t
|
|
601 ngx_http_script_done(ngx_http_script_compile_t *sc)
|
|
602 {
|
|
603 ngx_str_t zero;
|
|
604 uintptr_t *code;
|
|
605
|
|
606 if (sc->zero) {
|
|
607
|
|
608 zero.len = 1;
|
|
609 zero.data = (u_char *) "\0";
|
|
610
|
|
611 if (ngx_http_script_add_copy_code(sc, &zero, 0) != NGX_OK) {
|
|
612 return NGX_ERROR;
|
|
613 }
|
|
614 }
|
|
615
|
|
616 if (sc->conf_prefix || sc->root_prefix) {
|
|
617 if (ngx_http_script_add_full_name_code(sc) != NGX_OK) {
|
|
618 return NGX_ERROR;
|
|
619 }
|
|
620 }
|
|
621
|
|
622 if (sc->complete_lengths) {
|
|
623 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
|
|
624 if (code == NULL) {
|
|
625 return NGX_ERROR;
|
|
626 }
|
|
627
|
|
628 *code = (uintptr_t) NULL;
|
|
629 }
|
|
630
|
|
631 if (sc->complete_values) {
|
|
632 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
|
|
633 &sc->main);
|
|
634 if (code == NULL) {
|
|
635 return NGX_ERROR;
|
|
636 }
|
|
637
|
|
638 *code = (uintptr_t) NULL;
|
|
639 }
|
|
640
|
|
641 return NGX_OK;
|
|
642 }
|
|
643
|
|
644
|
58
|
645 void *
|
56
|
646 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
|
|
647 {
|
|
648 if (*codes == NULL) {
|
|
649 *codes = ngx_array_create(pool, 256, 1);
|
|
650 if (*codes == NULL) {
|
|
651 return NULL;
|
|
652 }
|
|
653 }
|
|
654
|
|
655 return ngx_array_push_n(*codes, size);
|
|
656 }
|
|
657
|
58
|
658
|
|
659 void *
|
|
660 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code)
|
|
661 {
|
|
662 u_char *elts, **p;
|
|
663 void *new;
|
|
664
|
|
665 elts = codes->elts;
|
|
666
|
|
667 new = ngx_array_push_n(codes, size);
|
|
668 if (new == NULL) {
|
496
|
669 return NULL;
|
58
|
670 }
|
|
671
|
|
672 if (code) {
|
|
673 if (elts != codes->elts) {
|
|
674 p = code;
|
|
675 *p += (u_char *) codes->elts - elts;
|
|
676 }
|
|
677 }
|
|
678
|
|
679 return new;
|
|
680 }
|
56
|
681
|
|
682
|
464
|
683 static ngx_int_t
|
|
684 ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc, ngx_str_t *value,
|
|
685 ngx_uint_t last)
|
|
686 {
|
|
687 u_char *p;
|
|
688 size_t size, len, zero;
|
|
689 ngx_http_script_copy_code_t *code;
|
|
690
|
|
691 zero = (sc->zero && last);
|
|
692 len = value->len + zero;
|
|
693
|
|
694 code = ngx_http_script_add_code(*sc->lengths,
|
|
695 sizeof(ngx_http_script_copy_code_t), NULL);
|
|
696 if (code == NULL) {
|
|
697 return NGX_ERROR;
|
|
698 }
|
|
699
|
|
700 code->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
|
|
701 code->len = len;
|
|
702
|
|
703 size = (sizeof(ngx_http_script_copy_code_t) + len + sizeof(uintptr_t) - 1)
|
|
704 & ~(sizeof(uintptr_t) - 1);
|
|
705
|
|
706 code = ngx_http_script_add_code(*sc->values, size, &sc->main);
|
|
707 if (code == NULL) {
|
|
708 return NGX_ERROR;
|
|
709 }
|
|
710
|
|
711 code->code = ngx_http_script_copy_code;
|
|
712 code->len = len;
|
|
713
|
|
714 p = ngx_cpymem((u_char *) code + sizeof(ngx_http_script_copy_code_t),
|
|
715 value->data, value->len);
|
|
716
|
|
717 if (zero) {
|
|
718 *p = '\0';
|
|
719 sc->zero = 0;
|
|
720 }
|
|
721
|
|
722 return NGX_OK;
|
|
723 }
|
|
724
|
|
725
|
56
|
726 size_t
|
58
|
727 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e)
|
56
|
728 {
|
|
729 ngx_http_script_copy_code_t *code;
|
|
730
|
58
|
731 code = (ngx_http_script_copy_code_t *) e->ip;
|
56
|
732
|
58
|
733 e->ip += sizeof(ngx_http_script_copy_code_t);
|
56
|
734
|
|
735 return code->len;
|
|
736 }
|
|
737
|
|
738
|
|
739 void
|
58
|
740 ngx_http_script_copy_code(ngx_http_script_engine_t *e)
|
56
|
741 {
|
430
|
742 u_char *p;
|
56
|
743 ngx_http_script_copy_code_t *code;
|
|
744
|
58
|
745 code = (ngx_http_script_copy_code_t *) e->ip;
|
|
746
|
430
|
747 p = e->pos;
|
|
748
|
58
|
749 if (!e->skip) {
|
430
|
750 e->pos = ngx_copy(p, e->ip + sizeof(ngx_http_script_copy_code_t),
|
122
|
751 code->len);
|
58
|
752 }
|
56
|
753
|
58
|
754 e->ip += sizeof(ngx_http_script_copy_code_t)
|
|
755 + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
|
56
|
756
|
430
|
757 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
758 "http script copy: \"%*s\"", e->pos - p, p);
|
56
|
759 }
|
|
760
|
|
761
|
464
|
762 static ngx_int_t
|
|
763 ngx_http_script_add_var_code(ngx_http_script_compile_t *sc, ngx_str_t *name)
|
|
764 {
|
|
765 ngx_int_t index, *p;
|
|
766 ngx_http_script_var_code_t *code;
|
|
767
|
|
768 index = ngx_http_get_variable_index(sc->cf, name);
|
|
769
|
|
770 if (index == NGX_ERROR) {
|
|
771 return NGX_ERROR;
|
|
772 }
|
|
773
|
|
774 if (sc->flushes) {
|
|
775 p = ngx_array_push(*sc->flushes);
|
|
776 if (p == NULL) {
|
|
777 return NGX_ERROR;
|
|
778 }
|
|
779
|
|
780 *p = index;
|
|
781 }
|
|
782
|
|
783 code = ngx_http_script_add_code(*sc->lengths,
|
|
784 sizeof(ngx_http_script_var_code_t), NULL);
|
|
785 if (code == NULL) {
|
|
786 return NGX_ERROR;
|
|
787 }
|
|
788
|
|
789 code->code = (ngx_http_script_code_pt) ngx_http_script_copy_var_len_code;
|
|
790 code->index = (uintptr_t) index;
|
|
791
|
|
792 code = ngx_http_script_add_code(*sc->values,
|
|
793 sizeof(ngx_http_script_var_code_t),
|
|
794 &sc->main);
|
|
795 if (code == NULL) {
|
|
796 return NGX_ERROR;
|
|
797 }
|
|
798
|
|
799 code->code = ngx_http_script_copy_var_code;
|
|
800 code->index = (uintptr_t) index;
|
|
801
|
|
802 return NGX_OK;
|
|
803 }
|
|
804
|
|
805
|
56
|
806 size_t
|
58
|
807 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e)
|
56
|
808 {
|
|
809 ngx_http_variable_value_t *value;
|
|
810 ngx_http_script_var_code_t *code;
|
|
811
|
58
|
812 code = (ngx_http_script_var_code_t *) e->ip;
|
56
|
813
|
58
|
814 e->ip += sizeof(ngx_http_script_var_code_t);
|
56
|
815
|
122
|
816 if (e->flushed) {
|
|
817 value = ngx_http_get_indexed_variable(e->request, code->index);
|
56
|
818
|
122
|
819 } else {
|
|
820 value = ngx_http_get_flushed_variable(e->request, code->index);
|
|
821 }
|
|
822
|
|
823 if (value && !value->not_found) {
|
|
824 return value->len;
|
56
|
825 }
|
|
826
|
102
|
827 return 0;
|
56
|
828 }
|
|
829
|
|
830
|
|
831 void
|
58
|
832 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e)
|
56
|
833 {
|
430
|
834 u_char *p;
|
56
|
835 ngx_http_variable_value_t *value;
|
|
836 ngx_http_script_var_code_t *code;
|
|
837
|
58
|
838 code = (ngx_http_script_var_code_t *) e->ip;
|
|
839
|
|
840 e->ip += sizeof(ngx_http_script_var_code_t);
|
|
841
|
|
842 if (!e->skip) {
|
122
|
843
|
|
844 if (e->flushed) {
|
|
845 value = ngx_http_get_indexed_variable(e->request, code->index);
|
58
|
846
|
122
|
847 } else {
|
|
848 value = ngx_http_get_flushed_variable(e->request, code->index);
|
|
849 }
|
|
850
|
|
851 if (value && !value->not_found) {
|
430
|
852 p = e->pos;
|
|
853 e->pos = ngx_copy(p, value->data, value->len);
|
58
|
854
|
430
|
855 ngx_log_debug2(NGX_LOG_DEBUG_HTTP,
|
256
|
856 e->request->connection->log, 0,
|
430
|
857 "http script var: \"%*s\"", e->pos - p, p);
|
58
|
858 }
|
|
859 }
|
|
860 }
|
|
861
|
|
862
|
464
|
863 static ngx_int_t
|
|
864 ngx_http_script_add_args_code(ngx_http_script_compile_t *sc)
|
|
865 {
|
|
866 uintptr_t *code;
|
|
867
|
|
868 code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
|
|
869 if (code == NULL) {
|
|
870 return NGX_ERROR;
|
|
871 }
|
|
872
|
|
873 *code = (uintptr_t) ngx_http_script_mark_args_code;
|
|
874
|
|
875 code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), &sc->main);
|
|
876 if (code == NULL) {
|
|
877 return NGX_ERROR;
|
|
878 }
|
|
879
|
|
880 *code = (uintptr_t) ngx_http_script_start_args_code;
|
|
881
|
|
882 return NGX_OK;
|
|
883 }
|
|
884
|
|
885
|
58
|
886 size_t
|
366
|
887 ngx_http_script_mark_args_code(ngx_http_script_engine_t *e)
|
|
888 {
|
|
889 e->is_args = 1;
|
|
890 e->ip += sizeof(uintptr_t);
|
|
891
|
|
892 return 1;
|
|
893 }
|
|
894
|
|
895
|
58
|
896 void
|
|
897 ngx_http_script_start_args_code(ngx_http_script_engine_t *e)
|
|
898 {
|
|
899 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
900 "http script args");
|
126
|
901
|
404
|
902 e->is_args = 1;
|
58
|
903 e->args = e->pos;
|
|
904 e->ip += sizeof(uintptr_t);
|
|
905 }
|
|
906
|
|
907
|
66
|
908 #if (NGX_PCRE)
|
|
909
|
58
|
910 void
|
|
911 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e)
|
|
912 {
|
|
913 size_t len;
|
|
914 ngx_int_t rc;
|
|
915 ngx_uint_t n;
|
|
916 ngx_http_request_t *r;
|
|
917 ngx_http_script_engine_t le;
|
|
918 ngx_http_script_len_code_pt lcode;
|
|
919 ngx_http_script_regex_code_t *code;
|
|
920
|
|
921 code = (ngx_http_script_regex_code_t *) e->ip;
|
|
922
|
|
923 r = e->request;
|
|
924
|
|
925 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
926 "http script regex: \"%V\"", &code->name);
|
|
927
|
|
928 if (code->uri) {
|
64
|
929 e->line = r->uri;
|
58
|
930 } else {
|
|
931 e->sp--;
|
122
|
932 e->line.len = e->sp->len;
|
|
933 e->line.data = e->sp->data;
|
58
|
934 }
|
56
|
935
|
546
|
936 rc = ngx_http_regex_exec(r, code->regex, &e->line);
|
456
|
937
|
546
|
938 if (rc == NGX_DECLINED) {
|
430
|
939 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
|
58
|
940 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
|
64
|
941 "\"%V\" does not match \"%V\"",
|
|
942 &code->name, &e->line);
|
58
|
943 }
|
|
944
|
456
|
945 r->ncaptures = 0;
|
64
|
946
|
58
|
947 if (code->test) {
|
180
|
948 if (code->negative_test) {
|
|
949 e->sp->len = 1;
|
|
950 e->sp->data = (u_char *) "1";
|
|
951
|
|
952 } else {
|
|
953 e->sp->len = 0;
|
|
954 e->sp->data = (u_char *) "";
|
|
955 }
|
|
956
|
58
|
957 e->sp++;
|
|
958
|
|
959 e->ip += sizeof(ngx_http_script_regex_code_t);
|
|
960 return;
|
|
961 }
|
|
962
|
|
963 e->ip += code->next;
|
|
964 return;
|
|
965 }
|
|
966
|
546
|
967 if (rc == NGX_ERROR) {
|
58
|
968 e->ip = ngx_http_script_exit;
|
|
969 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
970 return;
|
|
971 }
|
|
972
|
430
|
973 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
|
58
|
974 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
|
64
|
975 "\"%V\" matches \"%V\"", &code->name, &e->line);
|
58
|
976 }
|
|
977
|
|
978 if (code->test) {
|
180
|
979 if (code->negative_test) {
|
|
980 e->sp->len = 0;
|
|
981 e->sp->data = (u_char *) "";
|
|
982
|
|
983 } else {
|
|
984 e->sp->len = 1;
|
|
985 e->sp->data = (u_char *) "1";
|
|
986 }
|
|
987
|
58
|
988 e->sp++;
|
|
989
|
|
990 e->ip += sizeof(ngx_http_script_regex_code_t);
|
|
991 return;
|
|
992 }
|
|
993
|
|
994 if (code->status) {
|
|
995 e->status = code->status;
|
56
|
996
|
58
|
997 if (!code->redirect) {
|
|
998 e->ip = ngx_http_script_exit;
|
|
999 return;
|
|
1000 }
|
|
1001 }
|
|
1002
|
|
1003 if (code->uri) {
|
|
1004 r->internal = 1;
|
|
1005 r->valid_unparsed_uri = 0;
|
|
1006
|
|
1007 if (code->break_cycle) {
|
|
1008 r->valid_location = 0;
|
76
|
1009 r->uri_changed = 0;
|
58
|
1010
|
|
1011 } else {
|
|
1012 r->uri_changed = 1;
|
|
1013 }
|
|
1014 }
|
|
1015
|
|
1016 if (code->lengths == NULL) {
|
|
1017 e->buf.len = code->size;
|
|
1018
|
|
1019 if (code->uri) {
|
548
|
1020 if (r->ncaptures && (r->quoted_uri || r->plus_in_uri)) {
|
58
|
1021 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
|
|
1022 NGX_ESCAPE_ARGS);
|
|
1023 }
|
|
1024 }
|
|
1025
|
548
|
1026 for (n = 2; n < r->ncaptures; n += 2) {
|
|
1027 e->buf.len += r->captures[n + 1] - r->captures[n];
|
58
|
1028 }
|
|
1029
|
|
1030 } else {
|
|
1031 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
|
|
1032
|
|
1033 le.ip = code->lengths->elts;
|
228
|
1034 le.line = e->line;
|
58
|
1035 le.request = r;
|
228
|
1036 le.quote = code->redirect;
|
58
|
1037
|
366
|
1038 len = 0;
|
58
|
1039
|
|
1040 while (*(uintptr_t *) le.ip) {
|
|
1041 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
1042 len += lcode(&le);
|
|
1043 }
|
|
1044
|
|
1045 e->buf.len = len;
|
366
|
1046 e->is_args = le.is_args;
|
58
|
1047 }
|
|
1048
|
64
|
1049 if (code->add_args && r->args.len) {
|
58
|
1050 e->buf.len += r->args.len + 1;
|
|
1051 }
|
|
1052
|
382
|
1053 e->buf.data = ngx_pnalloc(r->pool, e->buf.len);
|
58
|
1054 if (e->buf.data == NULL) {
|
|
1055 e->ip = ngx_http_script_exit;
|
|
1056 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
56
|
1057 return;
|
|
1058 }
|
|
1059
|
58
|
1060 e->quote = code->redirect;
|
|
1061
|
|
1062 e->pos = e->buf.data;
|
|
1063
|
|
1064 e->ip += sizeof(ngx_http_script_regex_code_t);
|
|
1065 }
|
|
1066
|
|
1067
|
|
1068 void
|
|
1069 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e)
|
|
1070 {
|
126
|
1071 u_char *dst, *src;
|
58
|
1072 ngx_http_request_t *r;
|
|
1073 ngx_http_script_regex_end_code_t *code;
|
|
1074
|
|
1075 code = (ngx_http_script_regex_end_code_t *) e->ip;
|
|
1076
|
|
1077 r = e->request;
|
|
1078
|
|
1079 e->quote = 0;
|
|
1080
|
|
1081 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1082 "http script regex end");
|
|
1083
|
|
1084 if (code->redirect) {
|
|
1085
|
126
|
1086 dst = e->buf.data;
|
|
1087 src = e->buf.data;
|
|
1088
|
342
|
1089 ngx_unescape_uri(&dst, &src, e->pos - e->buf.data,
|
|
1090 NGX_UNESCAPE_REDIRECT);
|
126
|
1091
|
|
1092 if (src < e->pos) {
|
620
|
1093 dst = ngx_movemem(dst, src, e->pos - src);
|
126
|
1094 }
|
|
1095
|
|
1096 e->pos = dst;
|
|
1097
|
|
1098 if (code->add_args && r->args.len) {
|
58
|
1099 *e->pos++ = (u_char) (code->args ? '&' : '?');
|
122
|
1100 e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
|
58
|
1101 }
|
|
1102
|
|
1103 e->buf.len = e->pos - e->buf.data;
|
|
1104
|
430
|
1105 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
|
58
|
1106 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
|
|
1107 "rewritten redirect: \"%V\"", &e->buf);
|
|
1108 }
|
|
1109
|
638
|
1110 ngx_http_clear_location(r);
|
|
1111
|
58
|
1112 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
|
|
1113 if (r->headers_out.location == NULL) {
|
|
1114 e->ip = ngx_http_script_exit;
|
|
1115 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
1116 return;
|
|
1117 }
|
|
1118
|
126
|
1119 r->headers_out.location->hash = 1;
|
570
|
1120 ngx_str_set(&r->headers_out.location->key, "Location");
|
58
|
1121 r->headers_out.location->value = e->buf;
|
|
1122
|
|
1123 e->ip += sizeof(ngx_http_script_regex_end_code_t);
|
|
1124 return;
|
|
1125 }
|
|
1126
|
126
|
1127 if (e->args) {
|
58
|
1128 e->buf.len = e->args - e->buf.data;
|
|
1129
|
|
1130 if (code->add_args && r->args.len) {
|
|
1131 *e->pos++ = '&';
|
122
|
1132 e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
|
58
|
1133 }
|
|
1134
|
|
1135 r->args.len = e->pos - e->args;
|
|
1136 r->args.data = e->args;
|
|
1137
|
|
1138 e->args = NULL;
|
|
1139
|
|
1140 } else {
|
|
1141 e->buf.len = e->pos - e->buf.data;
|
222
|
1142
|
|
1143 if (!code->add_args) {
|
|
1144 r->args.len = 0;
|
|
1145 }
|
58
|
1146 }
|
|
1147
|
430
|
1148 if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
|
58
|
1149 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
|
|
1150 "rewritten data: \"%V\", args: \"%V\"",
|
|
1151 &e->buf, &r->args);
|
|
1152 }
|
|
1153
|
|
1154 if (code->uri) {
|
|
1155 r->uri = e->buf;
|
|
1156
|
84
|
1157 if (r->uri.len == 0) {
|
|
1158 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
1159 "the rewritten URI has a zero length");
|
|
1160 e->ip = ngx_http_script_exit;
|
|
1161 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
1162 return;
|
|
1163 }
|
|
1164
|
508
|
1165 ngx_http_set_exten(r);
|
58
|
1166 }
|
|
1167
|
|
1168 e->ip += sizeof(ngx_http_script_regex_end_code_t);
|
|
1169 }
|
|
1170
|
456
|
1171
|
464
|
1172 static ngx_int_t
|
|
1173 ngx_http_script_add_capture_code(ngx_http_script_compile_t *sc, ngx_uint_t n)
|
|
1174 {
|
|
1175 ngx_http_script_copy_capture_code_t *code;
|
|
1176
|
|
1177 code = ngx_http_script_add_code(*sc->lengths,
|
|
1178 sizeof(ngx_http_script_copy_capture_code_t),
|
|
1179 NULL);
|
|
1180 if (code == NULL) {
|
|
1181 return NGX_ERROR;
|
|
1182 }
|
|
1183
|
|
1184 code->code = (ngx_http_script_code_pt)
|
|
1185 ngx_http_script_copy_capture_len_code;
|
|
1186 code->n = 2 * n;
|
|
1187
|
|
1188
|
|
1189 code = ngx_http_script_add_code(*sc->values,
|
|
1190 sizeof(ngx_http_script_copy_capture_code_t),
|
|
1191 &sc->main);
|
|
1192 if (code == NULL) {
|
|
1193 return NGX_ERROR;
|
|
1194 }
|
|
1195
|
|
1196 code->code = ngx_http_script_copy_capture_code;
|
|
1197 code->n = 2 * n;
|
|
1198
|
|
1199 if (sc->ncaptures < n) {
|
|
1200 sc->ncaptures = n;
|
|
1201 }
|
|
1202
|
|
1203 return NGX_OK;
|
|
1204 }
|
|
1205
|
|
1206
|
456
|
1207 size_t
|
|
1208 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e)
|
|
1209 {
|
|
1210 int *cap;
|
|
1211 u_char *p;
|
|
1212 ngx_uint_t n;
|
|
1213 ngx_http_request_t *r;
|
|
1214 ngx_http_script_copy_capture_code_t *code;
|
|
1215
|
|
1216 r = e->request;
|
|
1217
|
|
1218 code = (ngx_http_script_copy_capture_code_t *) e->ip;
|
|
1219
|
|
1220 e->ip += sizeof(ngx_http_script_copy_capture_code_t);
|
|
1221
|
|
1222 n = code->n;
|
|
1223
|
|
1224 if (n < r->ncaptures) {
|
|
1225
|
|
1226 cap = r->captures;
|
|
1227
|
|
1228 if ((e->is_args || e->quote)
|
|
1229 && (e->request->quoted_uri || e->request->plus_in_uri))
|
|
1230 {
|
|
1231 p = r->captures_data;
|
|
1232
|
|
1233 return cap[n + 1] - cap[n]
|
|
1234 + 2 * ngx_escape_uri(NULL, &p[cap[n]], cap[n + 1] - cap[n],
|
|
1235 NGX_ESCAPE_ARGS);
|
|
1236 } else {
|
|
1237 return cap[n + 1] - cap[n];
|
|
1238 }
|
|
1239 }
|
|
1240
|
|
1241 return 0;
|
|
1242 }
|
|
1243
|
|
1244
|
|
1245 void
|
|
1246 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e)
|
|
1247 {
|
|
1248 int *cap;
|
|
1249 u_char *p, *pos;
|
|
1250 ngx_uint_t n;
|
|
1251 ngx_http_request_t *r;
|
|
1252 ngx_http_script_copy_capture_code_t *code;
|
|
1253
|
|
1254 r = e->request;
|
|
1255
|
|
1256 code = (ngx_http_script_copy_capture_code_t *) e->ip;
|
|
1257
|
|
1258 e->ip += sizeof(ngx_http_script_copy_capture_code_t);
|
|
1259
|
|
1260 n = code->n;
|
|
1261
|
|
1262 pos = e->pos;
|
|
1263
|
|
1264 if (n < r->ncaptures) {
|
|
1265
|
|
1266 cap = r->captures;
|
|
1267 p = r->captures_data;
|
|
1268
|
|
1269 if ((e->is_args || e->quote)
|
|
1270 && (e->request->quoted_uri || e->request->plus_in_uri))
|
|
1271 {
|
|
1272 e->pos = (u_char *) ngx_escape_uri(pos, &p[cap[n]],
|
|
1273 cap[n + 1] - cap[n],
|
|
1274 NGX_ESCAPE_ARGS);
|
|
1275 } else {
|
|
1276 e->pos = ngx_copy(pos, &p[cap[n]], cap[n + 1] - cap[n]);
|
|
1277 }
|
|
1278 }
|
|
1279
|
|
1280 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1281 "http script capture: \"%*s\"", e->pos - pos, pos);
|
|
1282 }
|
|
1283
|
66
|
1284 #endif
|
|
1285
|
58
|
1286
|
464
|
1287 static ngx_int_t
|
|
1288 ngx_http_script_add_full_name_code(ngx_http_script_compile_t *sc)
|
|
1289 {
|
|
1290 ngx_http_script_full_name_code_t *code;
|
|
1291
|
|
1292 code = ngx_http_script_add_code(*sc->lengths,
|
|
1293 sizeof(ngx_http_script_full_name_code_t),
|
|
1294 NULL);
|
|
1295 if (code == NULL) {
|
|
1296 return NGX_ERROR;
|
|
1297 }
|
|
1298
|
|
1299 code->code = (ngx_http_script_code_pt) ngx_http_script_full_name_len_code;
|
482
|
1300 code->conf_prefix = sc->conf_prefix;
|
464
|
1301
|
|
1302 code = ngx_http_script_add_code(*sc->values,
|
|
1303 sizeof(ngx_http_script_full_name_code_t),
|
|
1304 &sc->main);
|
|
1305 if (code == NULL) {
|
|
1306 return NGX_ERROR;
|
|
1307 }
|
|
1308
|
|
1309 code->code = ngx_http_script_full_name_code;
|
482
|
1310 code->conf_prefix = sc->conf_prefix;
|
464
|
1311
|
|
1312 return NGX_OK;
|
|
1313 }
|
|
1314
|
|
1315
|
|
1316 static size_t
|
|
1317 ngx_http_script_full_name_len_code(ngx_http_script_engine_t *e)
|
|
1318 {
|
|
1319 ngx_http_script_full_name_code_t *code;
|
|
1320
|
|
1321 code = (ngx_http_script_full_name_code_t *) e->ip;
|
|
1322
|
|
1323 e->ip += sizeof(ngx_http_script_full_name_code_t);
|
|
1324
|
482
|
1325 return code->conf_prefix ? ngx_cycle->conf_prefix.len:
|
|
1326 ngx_cycle->prefix.len;
|
464
|
1327 }
|
|
1328
|
|
1329
|
|
1330 static void
|
|
1331 ngx_http_script_full_name_code(ngx_http_script_engine_t *e)
|
|
1332 {
|
|
1333 ngx_http_script_full_name_code_t *code;
|
|
1334
|
|
1335 ngx_str_t value;
|
|
1336
|
|
1337 code = (ngx_http_script_full_name_code_t *) e->ip;
|
|
1338
|
|
1339 value.data = e->buf.data;
|
|
1340 value.len = e->pos - e->buf.data;
|
|
1341
|
482
|
1342 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &value, code->conf_prefix)
|
464
|
1343 != NGX_OK)
|
|
1344 {
|
|
1345 e->ip = ngx_http_script_exit;
|
|
1346 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
1347 return;
|
|
1348 }
|
|
1349
|
|
1350 e->buf = value;
|
|
1351
|
|
1352 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1353 "http script fullname: \"%V\"", &value);
|
|
1354
|
|
1355 e->ip += sizeof(ngx_http_script_full_name_code_t);
|
|
1356 }
|
|
1357
|
|
1358
|
58
|
1359 void
|
|
1360 ngx_http_script_return_code(ngx_http_script_engine_t *e)
|
126
|
1361 {
|
58
|
1362 ngx_http_script_return_code_t *code;
|
126
|
1363
|
58
|
1364 code = (ngx_http_script_return_code_t *) e->ip;
|
|
1365
|
580
|
1366 if (code->status < NGX_HTTP_BAD_REQUEST
|
|
1367 || code->text.value.len
|
|
1368 || code->text.lengths)
|
|
1369 {
|
|
1370 e->status = ngx_http_send_response(e->request, code->status, NULL,
|
|
1371 &code->text);
|
|
1372 } else {
|
|
1373 e->status = code->status;
|
202
|
1374 }
|
|
1375
|
580
|
1376 e->ip = ngx_http_script_exit;
|
56
|
1377 }
|
58
|
1378
|
|
1379
|
|
1380 void
|
76
|
1381 ngx_http_script_break_code(ngx_http_script_engine_t *e)
|
|
1382 {
|
|
1383 e->request->uri_changed = 0;
|
|
1384
|
|
1385 e->ip = ngx_http_script_exit;
|
|
1386 }
|
|
1387
|
|
1388
|
|
1389 void
|
58
|
1390 ngx_http_script_if_code(ngx_http_script_engine_t *e)
|
|
1391 {
|
|
1392 ngx_http_script_if_code_t *code;
|
|
1393
|
|
1394 code = (ngx_http_script_if_code_t *) e->ip;
|
|
1395
|
|
1396 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1397 "http script if");
|
|
1398
|
|
1399 e->sp--;
|
|
1400
|
622
|
1401 if (e->sp->len && (e->sp->len !=1 || e->sp->data[0] != '0')) {
|
58
|
1402 if (code->loc_conf) {
|
|
1403 e->request->loc_conf = code->loc_conf;
|
84
|
1404 ngx_http_update_location_config(e->request);
|
58
|
1405 }
|
|
1406
|
|
1407 e->ip += sizeof(ngx_http_script_if_code_t);
|
|
1408 return;
|
|
1409 }
|
|
1410
|
|
1411 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
126
|
1412 "http script if: false");
|
58
|
1413
|
|
1414 e->ip += code->next;
|
|
1415 }
|
|
1416
|
|
1417
|
|
1418 void
|
126
|
1419 ngx_http_script_equal_code(ngx_http_script_engine_t *e)
|
|
1420 {
|
|
1421 ngx_http_variable_value_t *val, *res;
|
|
1422
|
|
1423 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1424 "http script equal");
|
|
1425
|
|
1426 e->sp--;
|
|
1427 val = e->sp;
|
|
1428 res = e->sp - 1;
|
|
1429
|
|
1430 e->ip += sizeof(uintptr_t);
|
|
1431
|
430
|
1432 if (val->len == res->len
|
|
1433 && ngx_strncmp(val->data, res->data, res->len) == 0)
|
126
|
1434 {
|
|
1435 *res = ngx_http_variable_true_value;
|
|
1436 return;
|
|
1437 }
|
|
1438
|
|
1439 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1440 "http script equal: no");
|
|
1441
|
|
1442 *res = ngx_http_variable_null_value;
|
|
1443 }
|
|
1444
|
|
1445
|
|
1446 void
|
|
1447 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e)
|
|
1448 {
|
|
1449 ngx_http_variable_value_t *val, *res;
|
|
1450
|
|
1451 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1452 "http script not equal");
|
|
1453
|
|
1454 e->sp--;
|
|
1455 val = e->sp;
|
|
1456 res = e->sp - 1;
|
|
1457
|
|
1458 e->ip += sizeof(uintptr_t);
|
|
1459
|
430
|
1460 if (val->len == res->len
|
|
1461 && ngx_strncmp(val->data, res->data, res->len) == 0)
|
126
|
1462 {
|
|
1463 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1464 "http script not equal: no");
|
|
1465
|
|
1466 *res = ngx_http_variable_null_value;
|
|
1467 return;
|
|
1468 }
|
|
1469
|
|
1470 *res = ngx_http_variable_true_value;
|
|
1471 }
|
|
1472
|
|
1473
|
|
1474 void
|
180
|
1475 ngx_http_script_file_code(ngx_http_script_engine_t *e)
|
|
1476 {
|
332
|
1477 ngx_str_t path;
|
|
1478 ngx_http_request_t *r;
|
|
1479 ngx_open_file_info_t of;
|
|
1480 ngx_http_core_loc_conf_t *clcf;
|
180
|
1481 ngx_http_variable_value_t *value;
|
|
1482 ngx_http_script_file_code_t *code;
|
|
1483
|
|
1484 value = e->sp - 1;
|
|
1485
|
|
1486 code = (ngx_http_script_file_code_t *) e->ip;
|
|
1487 e->ip += sizeof(ngx_http_script_file_code_t);
|
|
1488
|
332
|
1489 path.len = value->len - 1;
|
|
1490 path.data = value->data;
|
|
1491
|
|
1492 r = e->request;
|
|
1493
|
|
1494 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1495 "http script file op %p \"%V\"", code->op, &path);
|
|
1496
|
|
1497 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
180
|
1498
|
384
|
1499 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
|
|
1500
|
532
|
1501 of.read_ahead = clcf->read_ahead;
|
390
|
1502 of.directio = clcf->directio;
|
358
|
1503 of.valid = clcf->open_file_cache_valid;
|
|
1504 of.min_uses = clcf->open_file_cache_min_uses;
|
482
|
1505 of.test_only = 1;
|
332
|
1506 of.errors = clcf->open_file_cache_errors;
|
|
1507 of.events = clcf->open_file_cache_events;
|
180
|
1508
|
332
|
1509 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
|
362
|
1510 != NGX_OK)
|
332
|
1511 {
|
530
|
1512 if (of.err != NGX_ENOENT
|
|
1513 && of.err != NGX_ENOTDIR
|
|
1514 && of.err != NGX_ENAMETOOLONG)
|
|
1515 {
|
332
|
1516 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
|
482
|
1517 "%s \"%s\" failed", of.failed, value->data);
|
180
|
1518 }
|
|
1519
|
|
1520 switch (code->op) {
|
220
|
1521
|
180
|
1522 case ngx_http_script_file_plain:
|
218
|
1523 case ngx_http_script_file_dir:
|
|
1524 case ngx_http_script_file_exists:
|
|
1525 case ngx_http_script_file_exec:
|
392
|
1526 goto false_value;
|
220
|
1527
|
180
|
1528 case ngx_http_script_file_not_plain:
|
218
|
1529 case ngx_http_script_file_not_dir:
|
220
|
1530 case ngx_http_script_file_not_exists:
|
218
|
1531 case ngx_http_script_file_not_exec:
|
392
|
1532 goto true_value;
|
180
|
1533 }
|
|
1534
|
392
|
1535 goto false_value;
|
180
|
1536 }
|
|
1537
|
|
1538 switch (code->op) {
|
|
1539 case ngx_http_script_file_plain:
|
332
|
1540 if (of.is_file) {
|
392
|
1541 goto true_value;
|
180
|
1542 }
|
392
|
1543 goto false_value;
|
180
|
1544
|
|
1545 case ngx_http_script_file_not_plain:
|
332
|
1546 if (of.is_file) {
|
392
|
1547 goto false_value;
|
180
|
1548 }
|
392
|
1549 goto true_value;
|
218
|
1550
|
|
1551 case ngx_http_script_file_dir:
|
332
|
1552 if (of.is_dir) {
|
392
|
1553 goto true_value;
|
218
|
1554 }
|
392
|
1555 goto false_value;
|
218
|
1556
|
|
1557 case ngx_http_script_file_not_dir:
|
332
|
1558 if (of.is_dir) {
|
392
|
1559 goto false_value;
|
218
|
1560 }
|
392
|
1561 goto true_value;
|
218
|
1562
|
|
1563 case ngx_http_script_file_exists:
|
332
|
1564 if (of.is_file || of.is_dir || of.is_link) {
|
392
|
1565 goto true_value;
|
218
|
1566 }
|
392
|
1567 goto false_value;
|
218
|
1568
|
|
1569 case ngx_http_script_file_not_exists:
|
332
|
1570 if (of.is_file || of.is_dir || of.is_link) {
|
392
|
1571 goto false_value;
|
218
|
1572 }
|
392
|
1573 goto true_value;
|
218
|
1574
|
|
1575 case ngx_http_script_file_exec:
|
332
|
1576 if (of.is_exec) {
|
392
|
1577 goto true_value;
|
218
|
1578 }
|
392
|
1579 goto false_value;
|
218
|
1580
|
|
1581 case ngx_http_script_file_not_exec:
|
332
|
1582 if (of.is_exec) {
|
392
|
1583 goto false_value;
|
218
|
1584 }
|
392
|
1585 goto true_value;
|
180
|
1586 }
|
|
1587
|
392
|
1588 false_value:
|
180
|
1589
|
332
|
1590 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
180
|
1591 "http script file op false");
|
|
1592
|
|
1593 *value = ngx_http_variable_null_value;
|
|
1594 return;
|
|
1595
|
392
|
1596 true_value:
|
180
|
1597
|
|
1598 *value = ngx_http_variable_true_value;
|
|
1599 return;
|
|
1600 }
|
|
1601
|
|
1602
|
|
1603 void
|
64
|
1604 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e)
|
|
1605 {
|
|
1606 size_t len;
|
|
1607 ngx_http_script_engine_t le;
|
|
1608 ngx_http_script_len_code_pt lcode;
|
|
1609 ngx_http_script_complex_value_code_t *code;
|
|
1610
|
|
1611 code = (ngx_http_script_complex_value_code_t *) e->ip;
|
|
1612
|
|
1613 e->ip += sizeof(ngx_http_script_complex_value_code_t);
|
|
1614
|
|
1615 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1616 "http script complex value");
|
|
1617
|
|
1618 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
|
|
1619
|
|
1620 le.ip = code->lengths->elts;
|
204
|
1621 le.line = e->line;
|
64
|
1622 le.request = e->request;
|
204
|
1623 le.quote = e->quote;
|
64
|
1624
|
|
1625 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
|
|
1626 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
1627 }
|
|
1628
|
|
1629 e->buf.len = len;
|
382
|
1630 e->buf.data = ngx_pnalloc(e->request->pool, len);
|
64
|
1631 if (e->buf.data == NULL) {
|
|
1632 e->ip = ngx_http_script_exit;
|
|
1633 e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
1634 return;
|
|
1635 }
|
|
1636
|
|
1637 e->pos = e->buf.data;
|
|
1638
|
126
|
1639 e->sp->len = e->buf.len;
|
122
|
1640 e->sp->data = e->buf.data;
|
64
|
1641 e->sp++;
|
|
1642 }
|
|
1643
|
|
1644
|
|
1645 void
|
58
|
1646 ngx_http_script_value_code(ngx_http_script_engine_t *e)
|
|
1647 {
|
|
1648 ngx_http_script_value_code_t *code;
|
|
1649
|
|
1650 code = (ngx_http_script_value_code_t *) e->ip;
|
|
1651
|
|
1652 e->ip += sizeof(ngx_http_script_value_code_t);
|
|
1653
|
122
|
1654 e->sp->len = code->text_len;
|
|
1655 e->sp->data = (u_char *) code->text_data;
|
126
|
1656
|
|
1657 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
340
|
1658 "http script value: \"%v\"", e->sp);
|
126
|
1659
|
58
|
1660 e->sp++;
|
|
1661 }
|
|
1662
|
|
1663
|
|
1664 void
|
|
1665 ngx_http_script_set_var_code(ngx_http_script_engine_t *e)
|
|
1666 {
|
|
1667 ngx_http_request_t *r;
|
|
1668 ngx_http_script_var_code_t *code;
|
|
1669
|
|
1670 code = (ngx_http_script_var_code_t *) e->ip;
|
|
1671
|
|
1672 e->ip += sizeof(ngx_http_script_var_code_t);
|
|
1673
|
|
1674 r = e->request;
|
|
1675
|
|
1676 e->sp--;
|
|
1677
|
122
|
1678 r->variables[code->index].len = e->sp->len;
|
|
1679 r->variables[code->index].valid = 1;
|
340
|
1680 r->variables[code->index].no_cacheable = 0;
|
122
|
1681 r->variables[code->index].not_found = 0;
|
|
1682 r->variables[code->index].data = e->sp->data;
|
430
|
1683
|
|
1684 #if (NGX_DEBUG)
|
|
1685 {
|
|
1686 ngx_http_variable_t *v;
|
|
1687 ngx_http_core_main_conf_t *cmcf;
|
|
1688
|
|
1689 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
1690
|
|
1691 v = cmcf->variables.elts;
|
|
1692
|
|
1693 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1694 "http script set $%V", &v[code->index].name);
|
|
1695 }
|
|
1696 #endif
|
58
|
1697 }
|
|
1698
|
|
1699
|
|
1700 void
|
186
|
1701 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e)
|
|
1702 {
|
|
1703 ngx_http_script_var_handler_code_t *code;
|
|
1704
|
|
1705 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1706 "http script set var handler");
|
|
1707
|
|
1708 code = (ngx_http_script_var_handler_code_t *) e->ip;
|
|
1709
|
|
1710 e->ip += sizeof(ngx_http_script_var_handler_code_t);
|
|
1711
|
|
1712 e->sp--;
|
|
1713
|
|
1714 code->handler(e->request, e->sp, code->data);
|
|
1715 }
|
|
1716
|
|
1717
|
|
1718 void
|
58
|
1719 ngx_http_script_var_code(ngx_http_script_engine_t *e)
|
|
1720 {
|
|
1721 ngx_http_variable_value_t *value;
|
|
1722 ngx_http_script_var_code_t *code;
|
|
1723
|
|
1724 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
|
1725 "http script var");
|
|
1726
|
|
1727 code = (ngx_http_script_var_code_t *) e->ip;
|
|
1728
|
|
1729 e->ip += sizeof(ngx_http_script_var_code_t);
|
|
1730
|
122
|
1731 value = ngx_http_get_flushed_variable(e->request, code->index);
|
58
|
1732
|
122
|
1733 if (value && !value->not_found) {
|
|
1734 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
|
328
|
1735 "http script var: \"%v\"", value);
|
122
|
1736
|
102
|
1737 *e->sp = *value;
|
58
|
1738 e->sp++;
|
|
1739
|
|
1740 return;
|
|
1741 }
|
|
1742
|
126
|
1743 *e->sp = ngx_http_variable_null_value;
|
58
|
1744 e->sp++;
|
|
1745 }
|
|
1746
|
|
1747
|
|
1748 void
|
|
1749 ngx_http_script_nop_code(ngx_http_script_engine_t *e)
|
|
1750 {
|
|
1751 e->ip += sizeof(uintptr_t);
|
|
1752 }
|