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