Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 static ngx_int_t ngx_http_script_init_arrays(ngx_http_script_compile_t *sc);
0014 static ngx_int_t ngx_http_script_done(ngx_http_script_compile_t *sc);
0015 static ngx_int_t ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc,
0016     ngx_str_t *value, ngx_uint_t last);
0017 static ngx_int_t ngx_http_script_add_var_code(ngx_http_script_compile_t *sc,
0018     ngx_str_t *name);
0019 static ngx_int_t ngx_http_script_add_args_code(ngx_http_script_compile_t *sc);
0020 #if (NGX_PCRE)
0021 static ngx_int_t ngx_http_script_add_capture_code(ngx_http_script_compile_t *sc,
0022     ngx_uint_t n);
0023 #endif
0024 static ngx_int_t
0025     ngx_http_script_add_full_name_code(ngx_http_script_compile_t *sc);
0026 static size_t ngx_http_script_full_name_len_code(ngx_http_script_engine_t *e);
0027 static void ngx_http_script_full_name_code(ngx_http_script_engine_t *e);
0028 
0029 
0030 #define ngx_http_script_exit  (u_char *) &ngx_http_script_exit_code
0031 
0032 static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;
0033 
0034 
0035 void
0036 ngx_http_script_flush_complex_value(ngx_http_request_t *r,
0037     ngx_http_complex_value_t *val)
0038 {
0039     ngx_uint_t *index;
0040 
0041     index = val->flushes;
0042 
0043     if (index) {
0044         while (*index != (ngx_uint_t) -1) {
0045 
0046             if (r->variables[*index].no_cacheable) {
0047                 r->variables[*index].valid = 0;
0048                 r->variables[*index].not_found = 0;
0049             }
0050 
0051             index++;
0052         }
0053     }
0054 }
0055 
0056 
0057 ngx_int_t
0058 ngx_http_complex_value(ngx_http_request_t *r, ngx_http_complex_value_t *val,
0059     ngx_str_t *value)
0060 {
0061     size_t                        len;
0062     ngx_http_script_code_pt       code;
0063     ngx_http_script_len_code_pt   lcode;
0064     ngx_http_script_engine_t      e;
0065 
0066     if (val->lengths == NULL) {
0067         *value = val->value;
0068         return NGX_OK;
0069     }
0070 
0071     ngx_http_script_flush_complex_value(r, val);
0072 
0073     ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
0074 
0075     e.ip = val->lengths;
0076     e.request = r;
0077     e.flushed = 1;
0078 
0079     len = 0;
0080 
0081     while (*(uintptr_t *) e.ip) {
0082         lcode = *(ngx_http_script_len_code_pt *) e.ip;
0083         len += lcode(&e);
0084     }
0085 
0086     value->len = len;
0087     value->data = ngx_pnalloc(r->pool, len);
0088     if (value->data == NULL) {
0089         return NGX_ERROR;
0090     }
0091 
0092     e.ip = val->values;
0093     e.pos = value->data;
0094     e.buf = *value;
0095 
0096     while (*(uintptr_t *) e.ip) {
0097         code = *(ngx_http_script_code_pt *) e.ip;
0098         code((ngx_http_script_engine_t *) &e);
0099     }
0100 
0101     *value = e.buf;
0102 
0103     return NGX_OK;
0104 }
0105 
0106 
0107 ngx_int_t
0108 ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
0109 {
0110     ngx_str_t                  *v;
0111     ngx_uint_t                  i, n, nv, nc;
0112     ngx_array_t                 flushes, lengths, values, *pf, *pl, *pv;
0113     ngx_http_script_compile_t   sc;
0114 
0115     v = ccv->value;
0116 
0117     nv = 0;
0118     nc = 0;
0119 
0120     for (i = 0; i < v->len; i++) {
0121         if (v->data[i] == '$') {
0122             if (v->data[i + 1] >= '1' && v->data[i + 1] <= '9') {
0123                 nc++;
0124 
0125             } else {
0126                 nv++;
0127             }
0128         }
0129     }
0130 
0131     if ((v->len == 0 || v->data[0] != '$')
0132         && (ccv->conf_prefix || ccv->root_prefix))
0133     {
0134         if (ngx_conf_full_name(ccv->cf->cycle, v, ccv->conf_prefix) != NGX_OK) {
0135             return NGX_ERROR;
0136         }
0137 
0138         ccv->conf_prefix = 0;
0139         ccv->root_prefix = 0;
0140     }
0141 
0142     ccv->complex_value->value = *v;
0143     ccv->complex_value->flushes = NULL;
0144     ccv->complex_value->lengths = NULL;
0145     ccv->complex_value->values = NULL;
0146 
0147     if (nv == 0 && nc == 0) {
0148         return NGX_OK;
0149     }
0150 
0151     n = nv + 1;
0152 
0153     if (ngx_array_init(&flushes, ccv->cf->pool, n, sizeof(ngx_uint_t))
0154         != NGX_OK)
0155     {
0156         return NGX_ERROR;
0157     }
0158 
0159     n = nv * (2 * sizeof(ngx_http_script_copy_code_t)
0160                   + sizeof(ngx_http_script_var_code_t))
0161         + sizeof(uintptr_t);
0162 
0163     if (ngx_array_init(&lengths, ccv->cf->pool, n, 1) != NGX_OK) {
0164         return NGX_ERROR;
0165     }
0166 
0167     n = (nv * (2 * sizeof(ngx_http_script_copy_code_t)
0168                    + sizeof(ngx_http_script_var_code_t))
0169                 + sizeof(uintptr_t)
0170                 + v->len
0171                 + sizeof(uintptr_t) - 1)
0172             & ~(sizeof(uintptr_t) - 1);
0173 
0174     if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {
0175         return NGX_ERROR;
0176     }
0177 
0178     pf = &flushes;
0179     pl = &lengths;
0180     pv = &values;
0181 
0182     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
0183 
0184     sc.cf = ccv->cf;
0185     sc.source = v;
0186     sc.flushes = &pf;
0187     sc.lengths = &pl;
0188     sc.values = &pv;
0189     sc.complete_lengths = 1;
0190     sc.complete_values = 1;
0191     sc.zero = ccv->zero;
0192     sc.conf_prefix = ccv->conf_prefix;
0193     sc.root_prefix = ccv->root_prefix;
0194 
0195     if (ngx_http_script_compile(&sc) != NGX_OK) {
0196         return NGX_ERROR;
0197     }
0198 
0199     if (flushes.nelts) {
0200         ccv->complex_value->flushes = flushes.elts;
0201         ccv->complex_value->flushes[flushes.nelts] = (ngx_uint_t) -1;
0202     }
0203 
0204     ccv->complex_value->lengths = lengths.elts;
0205     ccv->complex_value->values = values.elts;
0206 
0207     return NGX_OK;
0208 }
0209 
0210 
0211 char *
0212 ngx_http_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0213 {
0214     char  *p = conf;
0215 
0216     ngx_str_t                          *value;
0217     ngx_http_complex_value_t          **cv;
0218     ngx_http_compile_complex_value_t    ccv;
0219 
0220     cv = (ngx_http_complex_value_t **) (p + cmd->offset);
0221 
0222     if (*cv != NULL) {
0223         return "is duplicate";
0224     }
0225 
0226     *cv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
0227     if (*cv == NULL) {
0228         return NGX_CONF_ERROR;
0229     }
0230 
0231     value = cf->args->elts;
0232 
0233     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
0234 
0235     ccv.cf = cf;
0236     ccv.value = &value[1];
0237     ccv.complex_value = *cv;
0238 
0239     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
0240         return NGX_CONF_ERROR;
0241     }
0242 
0243     return NGX_CONF_OK;
0244 }
0245 
0246 
0247 ngx_int_t
0248 ngx_http_test_predicates(ngx_http_request_t *r, ngx_array_t *predicates)
0249 {
0250     ngx_str_t                  val;
0251     ngx_uint_t                 i;
0252     ngx_http_complex_value_t  *cv;
0253 
0254     if (predicates == NULL) {
0255         return NGX_OK;
0256     }
0257 
0258     cv = predicates->elts;
0259 
0260     for (i = 0; i < predicates->nelts; i++) {
0261         if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
0262             return NGX_ERROR;
0263         }
0264 
0265         if (val.len && (val.len != 1 || val.data[0] != '0')) {
0266             return NGX_DECLINED;
0267         }
0268     }
0269 
0270     return NGX_OK;
0271 }
0272 
0273 
0274 ngx_int_t
0275 ngx_http_test_required_predicates(ngx_http_request_t *r,
0276     ngx_array_t *predicates)
0277 {
0278     ngx_str_t                  val;
0279     ngx_uint_t                 i;
0280     ngx_http_complex_value_t  *cv;
0281 
0282     if (predicates == NULL) {
0283         return NGX_OK;
0284     }
0285 
0286     cv = predicates->elts;
0287 
0288     for (i = 0; i < predicates->nelts; i++) {
0289         if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
0290             return NGX_ERROR;
0291         }
0292 
0293         if (val.len == 0 || (val.len == 1 && val.data[0] == '0')) {
0294             return NGX_DECLINED;
0295         }
0296     }
0297 
0298     return NGX_OK;
0299 }
0300 
0301 
0302 char *
0303 ngx_http_set_predicate_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0304 {
0305     char  *p = conf;
0306 
0307     ngx_str_t                          *value;
0308     ngx_uint_t                          i;
0309     ngx_array_t                       **a;
0310     ngx_http_complex_value_t           *cv;
0311     ngx_http_compile_complex_value_t    ccv;
0312 
0313     a = (ngx_array_t **) (p + cmd->offset);
0314 
0315     if (*a == NGX_CONF_UNSET_PTR) {
0316         *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_complex_value_t));
0317         if (*a == NULL) {
0318             return NGX_CONF_ERROR;
0319         }
0320     }
0321 
0322     value = cf->args->elts;
0323 
0324     for (i = 1; i < cf->args->nelts; i++) {
0325         cv = ngx_array_push(*a);
0326         if (cv == NULL) {
0327             return NGX_CONF_ERROR;
0328         }
0329 
0330         ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
0331 
0332         ccv.cf = cf;
0333         ccv.value = &value[i];
0334         ccv.complex_value = cv;
0335 
0336         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
0337             return NGX_CONF_ERROR;
0338         }
0339     }
0340 
0341     return NGX_CONF_OK;
0342 }
0343 
0344 
0345 ngx_uint_t
0346 ngx_http_script_variables_count(ngx_str_t *value)
0347 {
0348     ngx_uint_t  i, n;
0349 
0350     for (n = 0, i = 0; i < value->len; i++) {
0351         if (value->data[i] == '$') {
0352             n++;
0353         }
0354     }
0355 
0356     return n;
0357 }
0358 
0359 
0360 ngx_int_t
0361 ngx_http_script_compile(ngx_http_script_compile_t *sc)
0362 {
0363     u_char       ch;
0364     ngx_str_t    name;
0365     ngx_uint_t   i, bracket;
0366 
0367     if (ngx_http_script_init_arrays(sc) != NGX_OK) {
0368         return NGX_ERROR;
0369     }
0370 
0371     for (i = 0; i < sc->source->len; /* void */ ) {
0372 
0373         name.len = 0;
0374 
0375         if (sc->source->data[i] == '$') {
0376 
0377             if (++i == sc->source->len) {
0378                 goto invalid_variable;
0379             }
0380 
0381             if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') {
0382 #if (NGX_PCRE)
0383                 ngx_uint_t  n;
0384 
0385                 n = sc->source->data[i] - '0';
0386 
0387                 if (sc->captures_mask & ((ngx_uint_t) 1 << n)) {
0388                     sc->dup_capture = 1;
0389                 }
0390 
0391                 sc->captures_mask |= (ngx_uint_t) 1 << n;
0392 
0393                 if (ngx_http_script_add_capture_code(sc, n) != NGX_OK) {
0394                     return NGX_ERROR;
0395                 }
0396 
0397                 i++;
0398 
0399                 continue;
0400 #else
0401                 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
0402                                    "using variable \"$%c\" requires "
0403                                    "PCRE library", sc->source->data[i]);
0404                 return NGX_ERROR;
0405 #endif
0406             }
0407 
0408             if (sc->source->data[i] == '{') {
0409                 bracket = 1;
0410 
0411                 if (++i == sc->source->len) {
0412                     goto invalid_variable;
0413                 }
0414 
0415                 name.data = &sc->source->data[i];
0416 
0417             } else {
0418                 bracket = 0;
0419                 name.data = &sc->source->data[i];
0420             }
0421 
0422             for ( /* void */ ; i < sc->source->len; i++, name.len++) {
0423                 ch = sc->source->data[i];
0424 
0425                 if (ch == '}' && bracket) {
0426                     i++;
0427                     bracket = 0;
0428                     break;
0429                 }
0430 
0431                 if ((ch >= 'A' && ch <= 'Z')
0432                     || (ch >= 'a' && ch <= 'z')
0433                     || (ch >= '0' && ch <= '9')
0434                     || ch == '_')
0435                 {
0436                     continue;
0437                 }
0438 
0439                 break;
0440             }
0441 
0442             if (bracket) {
0443                 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
0444                                    "the closing bracket in \"%V\" "
0445                                    "variable is missing", &name);
0446                 return NGX_ERROR;
0447             }
0448 
0449             if (name.len == 0) {
0450                 goto invalid_variable;
0451             }
0452 
0453             sc->variables++;
0454 
0455             if (ngx_http_script_add_var_code(sc, &name) != NGX_OK) {
0456                 return NGX_ERROR;
0457             }
0458 
0459             continue;
0460         }
0461 
0462         if (sc->source->data[i] == '?' && sc->compile_args) {
0463             sc->args = 1;
0464             sc->compile_args = 0;
0465 
0466             if (ngx_http_script_add_args_code(sc) != NGX_OK) {
0467                 return NGX_ERROR;
0468             }
0469 
0470             i++;
0471 
0472             continue;
0473         }
0474 
0475         name.data = &sc->source->data[i];
0476 
0477         while (i < sc->source->len) {
0478 
0479             if (sc->source->data[i] == '$') {
0480                 break;
0481             }
0482 
0483             if (sc->source->data[i] == '?') {
0484 
0485                 sc->args = 1;
0486 
0487                 if (sc->compile_args) {
0488                     break;
0489                 }
0490             }
0491 
0492             i++;
0493             name.len++;
0494         }
0495 
0496         sc->size += name.len;
0497 
0498         if (ngx_http_script_add_copy_code(sc, &name, (i == sc->source->len))
0499             != NGX_OK)
0500         {
0501             return NGX_ERROR;
0502         }
0503     }
0504 
0505     return ngx_http_script_done(sc);
0506 
0507 invalid_variable:
0508 
0509     ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name");
0510 
0511     return NGX_ERROR;
0512 }
0513 
0514 
0515 u_char *
0516 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,
0517     void *code_lengths, size_t len, void *code_values)
0518 {
0519     ngx_uint_t                    i;
0520     ngx_http_script_code_pt       code;
0521     ngx_http_script_len_code_pt   lcode;
0522     ngx_http_script_engine_t      e;
0523     ngx_http_core_main_conf_t    *cmcf;
0524 
0525     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
0526 
0527     for (i = 0; i < cmcf->variables.nelts; i++) {
0528         if (r->variables[i].no_cacheable) {
0529             r->variables[i].valid = 0;
0530             r->variables[i].not_found = 0;
0531         }
0532     }
0533 
0534     ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
0535 
0536     e.ip = code_lengths;
0537     e.request = r;
0538     e.flushed = 1;
0539 
0540     while (*(uintptr_t *) e.ip) {
0541         lcode = *(ngx_http_script_len_code_pt *) e.ip;
0542         len += lcode(&e);
0543     }
0544 
0545 
0546     value->len = len;
0547     value->data = ngx_pnalloc(r->pool, len);
0548     if (value->data == NULL) {
0549         return NULL;
0550     }
0551 
0552     e.ip = code_values;
0553     e.pos = value->data;
0554 
0555     while (*(uintptr_t *) e.ip) {
0556         code = *(ngx_http_script_code_pt *) e.ip;
0557         code((ngx_http_script_engine_t *) &e);
0558     }
0559 
0560     return e.pos;
0561 }
0562 
0563 
0564 void
0565 ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r,
0566     ngx_array_t *indices)
0567 {
0568     ngx_uint_t  n, *index;
0569 
0570     if (indices) {
0571         index = indices->elts;
0572         for (n = 0; n < indices->nelts; n++) {
0573             if (r->variables[index[n]].no_cacheable) {
0574                 r->variables[index[n]].valid = 0;
0575                 r->variables[index[n]].not_found = 0;
0576             }
0577         }
0578     }
0579 }
0580 
0581 
0582 static ngx_int_t
0583 ngx_http_script_init_arrays(ngx_http_script_compile_t *sc)
0584 {
0585     ngx_uint_t   n;
0586 
0587     if (sc->flushes && *sc->flushes == NULL) {
0588         n = sc->variables ? sc->variables : 1;
0589         *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t));
0590         if (*sc->flushes == NULL) {
0591             return NGX_ERROR;
0592         }
0593     }
0594 
0595     if (*sc->lengths == NULL) {
0596         n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
0597                              + sizeof(ngx_http_script_var_code_t))
0598             + sizeof(uintptr_t);
0599 
0600         *sc->lengths = ngx_array_create(sc->cf->pool, n, 1);
0601         if (*sc->lengths == NULL) {
0602             return NGX_ERROR;
0603         }
0604     }
0605 
0606     if (*sc->values == NULL) {
0607         n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
0608                               + sizeof(ngx_http_script_var_code_t))
0609                 + sizeof(uintptr_t)
0610                 + sc->source->len
0611                 + sizeof(uintptr_t) - 1)
0612             & ~(sizeof(uintptr_t) - 1);
0613 
0614         *sc->values = ngx_array_create(sc->cf->pool, n, 1);
0615         if (*sc->values == NULL) {
0616             return NGX_ERROR;
0617         }
0618     }
0619 
0620     sc->variables = 0;
0621 
0622     return NGX_OK;
0623 }
0624 
0625 
0626 static ngx_int_t
0627 ngx_http_script_done(ngx_http_script_compile_t *sc)
0628 {
0629     ngx_str_t    zero;
0630     uintptr_t   *code;
0631 
0632     if (sc->zero) {
0633 
0634         zero.len = 1;
0635         zero.data = (u_char *) "\0";
0636 
0637         if (ngx_http_script_add_copy_code(sc, &zero, 0) != NGX_OK) {
0638             return NGX_ERROR;
0639         }
0640     }
0641 
0642     if (sc->conf_prefix || sc->root_prefix) {
0643         if (ngx_http_script_add_full_name_code(sc) != NGX_OK) {
0644             return NGX_ERROR;
0645         }
0646     }
0647 
0648     if (sc->complete_lengths) {
0649         code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
0650         if (code == NULL) {
0651             return NGX_ERROR;
0652         }
0653 
0654         *code = (uintptr_t) NULL;
0655     }
0656 
0657     if (sc->complete_values) {
0658         code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
0659                                         &sc->main);
0660         if (code == NULL) {
0661             return NGX_ERROR;
0662         }
0663 
0664         *code = (uintptr_t) NULL;
0665     }
0666 
0667     return NGX_OK;
0668 }
0669 
0670 
0671 void *
0672 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
0673 {
0674     if (*codes == NULL) {
0675         *codes = ngx_array_create(pool, 256, 1);
0676         if (*codes == NULL) {
0677             return NULL;
0678         }
0679     }
0680 
0681     return ngx_array_push_n(*codes, size);
0682 }
0683 
0684 
0685 void *
0686 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code)
0687 {
0688     u_char  *elts, **p;
0689     void    *new;
0690 
0691     elts = codes->elts;
0692 
0693     new = ngx_array_push_n(codes, size);
0694     if (new == NULL) {
0695         return NULL;
0696     }
0697 
0698     if (code) {
0699         if (elts != codes->elts) {
0700             p = code;
0701             *p += (u_char *) codes->elts - elts;
0702         }
0703     }
0704 
0705     return new;
0706 }
0707 
0708 
0709 static ngx_int_t
0710 ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc, ngx_str_t *value,
0711     ngx_uint_t last)
0712 {
0713     u_char                       *p;
0714     size_t                        size, len, zero;
0715     ngx_http_script_copy_code_t  *code;
0716 
0717     zero = (sc->zero && last);
0718     len = value->len + zero;
0719 
0720     code = ngx_http_script_add_code(*sc->lengths,
0721                                     sizeof(ngx_http_script_copy_code_t), NULL);
0722     if (code == NULL) {
0723         return NGX_ERROR;
0724     }
0725 
0726     code->code = (ngx_http_script_code_pt) (void *)
0727                                                  ngx_http_script_copy_len_code;
0728     code->len = len;
0729 
0730     size = (sizeof(ngx_http_script_copy_code_t) + len + sizeof(uintptr_t) - 1)
0731             & ~(sizeof(uintptr_t) - 1);
0732 
0733     code = ngx_http_script_add_code(*sc->values, size, &sc->main);
0734     if (code == NULL) {
0735         return NGX_ERROR;
0736     }
0737 
0738     code->code = ngx_http_script_copy_code;
0739     code->len = len;
0740 
0741     p = ngx_cpymem((u_char *) code + sizeof(ngx_http_script_copy_code_t),
0742                    value->data, value->len);
0743 
0744     if (zero) {
0745         *p = '\0';
0746         sc->zero = 0;
0747     }
0748 
0749     return NGX_OK;
0750 }
0751 
0752 
0753 size_t
0754 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e)
0755 {
0756     ngx_http_script_copy_code_t  *code;
0757 
0758     code = (ngx_http_script_copy_code_t *) e->ip;
0759 
0760     e->ip += sizeof(ngx_http_script_copy_code_t);
0761 
0762     return code->len;
0763 }
0764 
0765 
0766 void
0767 ngx_http_script_copy_code(ngx_http_script_engine_t *e)
0768 {
0769     u_char                       *p;
0770     ngx_http_script_copy_code_t  *code;
0771 
0772     code = (ngx_http_script_copy_code_t *) e->ip;
0773 
0774     p = e->pos;
0775 
0776     if (!e->skip) {
0777         e->pos = ngx_copy(p, e->ip + sizeof(ngx_http_script_copy_code_t),
0778                           code->len);
0779     }
0780 
0781     e->ip += sizeof(ngx_http_script_copy_code_t)
0782           + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
0783 
0784     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
0785                    "http script copy: \"%*s\"", e->pos - p, p);
0786 }
0787 
0788 
0789 static ngx_int_t
0790 ngx_http_script_add_var_code(ngx_http_script_compile_t *sc, ngx_str_t *name)
0791 {
0792     ngx_int_t                    index, *p;
0793     ngx_http_script_var_code_t  *code;
0794 
0795     index = ngx_http_get_variable_index(sc->cf, name);
0796 
0797     if (index == NGX_ERROR) {
0798         return NGX_ERROR;
0799     }
0800 
0801     if (sc->flushes) {
0802         p = ngx_array_push(*sc->flushes);
0803         if (p == NULL) {
0804             return NGX_ERROR;
0805         }
0806 
0807         *p = index;
0808     }
0809 
0810     code = ngx_http_script_add_code(*sc->lengths,
0811                                     sizeof(ngx_http_script_var_code_t), NULL);
0812     if (code == NULL) {
0813         return NGX_ERROR;
0814     }
0815 
0816     code->code = (ngx_http_script_code_pt) (void *)
0817                                              ngx_http_script_copy_var_len_code;
0818     code->index = (uintptr_t) index;
0819 
0820     code = ngx_http_script_add_code(*sc->values,
0821                                     sizeof(ngx_http_script_var_code_t),
0822                                     &sc->main);
0823     if (code == NULL) {
0824         return NGX_ERROR;
0825     }
0826 
0827     code->code = ngx_http_script_copy_var_code;
0828     code->index = (uintptr_t) index;
0829 
0830     return NGX_OK;
0831 }
0832 
0833 
0834 size_t
0835 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e)
0836 {
0837     ngx_http_variable_value_t   *value;
0838     ngx_http_script_var_code_t  *code;
0839 
0840     code = (ngx_http_script_var_code_t *) e->ip;
0841 
0842     e->ip += sizeof(ngx_http_script_var_code_t);
0843 
0844     if (e->flushed) {
0845         value = ngx_http_get_indexed_variable(e->request, code->index);
0846 
0847     } else {
0848         value = ngx_http_get_flushed_variable(e->request, code->index);
0849     }
0850 
0851     if (value && !value->not_found) {
0852         return value->len;
0853     }
0854 
0855     return 0;
0856 }
0857 
0858 
0859 void
0860 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e)
0861 {
0862     u_char                      *p;
0863     ngx_http_variable_value_t   *value;
0864     ngx_http_script_var_code_t  *code;
0865 
0866     code = (ngx_http_script_var_code_t *) e->ip;
0867 
0868     e->ip += sizeof(ngx_http_script_var_code_t);
0869 
0870     if (!e->skip) {
0871 
0872         if (e->flushed) {
0873             value = ngx_http_get_indexed_variable(e->request, code->index);
0874 
0875         } else {
0876             value = ngx_http_get_flushed_variable(e->request, code->index);
0877         }
0878 
0879         if (value && !value->not_found) {
0880             p = e->pos;
0881             e->pos = ngx_copy(p, value->data, value->len);
0882 
0883             ngx_log_debug2(NGX_LOG_DEBUG_HTTP,
0884                            e->request->connection->log, 0,
0885                            "http script var: \"%*s\"", e->pos - p, p);
0886         }
0887     }
0888 }
0889 
0890 
0891 static ngx_int_t
0892 ngx_http_script_add_args_code(ngx_http_script_compile_t *sc)
0893 {
0894     uintptr_t   *code;
0895 
0896     code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
0897     if (code == NULL) {
0898         return NGX_ERROR;
0899     }
0900 
0901     *code = (uintptr_t) ngx_http_script_mark_args_code;
0902 
0903     code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), &sc->main);
0904     if (code == NULL) {
0905         return NGX_ERROR;
0906     }
0907 
0908     *code = (uintptr_t) ngx_http_script_start_args_code;
0909 
0910     return NGX_OK;
0911 }
0912 
0913 
0914 size_t
0915 ngx_http_script_mark_args_code(ngx_http_script_engine_t *e)
0916 {
0917     e->is_args = 1;
0918     e->ip += sizeof(uintptr_t);
0919 
0920     return 1;
0921 }
0922 
0923 
0924 void
0925 ngx_http_script_start_args_code(ngx_http_script_engine_t *e)
0926 {
0927     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
0928                    "http script args");
0929 
0930     e->is_args = 1;
0931     e->args = e->pos;
0932     e->ip += sizeof(uintptr_t);
0933 }
0934 
0935 
0936 #if (NGX_PCRE)
0937 
0938 void
0939 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e)
0940 {
0941     size_t                         len;
0942     ngx_int_t                      rc;
0943     ngx_uint_t                     n;
0944     ngx_http_request_t            *r;
0945     ngx_http_script_engine_t       le;
0946     ngx_http_script_len_code_pt    lcode;
0947     ngx_http_script_regex_code_t  *code;
0948 
0949     code = (ngx_http_script_regex_code_t *) e->ip;
0950 
0951     r = e->request;
0952 
0953     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0954                    "http script regex: \"%V\"", &code->name);
0955 
0956     if (code->uri) {
0957         e->line = r->uri;
0958     } else {
0959         e->sp--;
0960         e->line.len = e->sp->len;
0961         e->line.data = e->sp->data;
0962     }
0963 
0964     rc = ngx_http_regex_exec(r, code->regex, &e->line);
0965 
0966     if (rc == NGX_DECLINED) {
0967         if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
0968             ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
0969                           "\"%V\" does not match \"%V\"",
0970                           &code->name, &e->line);
0971         }
0972 
0973         r->ncaptures = 0;
0974 
0975         if (code->test) {
0976             if (code->negative_test) {
0977                 e->sp->len = 1;
0978                 e->sp->data = (u_char *) "1";
0979 
0980             } else {
0981                 e->sp->len = 0;
0982                 e->sp->data = (u_char *) "";
0983             }
0984 
0985             e->sp++;
0986 
0987             e->ip += sizeof(ngx_http_script_regex_code_t);
0988             return;
0989         }
0990 
0991         e->ip += code->next;
0992         return;
0993     }
0994 
0995     if (rc == NGX_ERROR) {
0996         e->ip = ngx_http_script_exit;
0997         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
0998         return;
0999     }
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
1790 ngx_http_script_nop_code(ngx_http_script_engine_t *e)
1791 {
1792     e->ip += sizeof(uintptr_t);
1793 }