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