Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.19.2 ]​[ nginx-1.18.0 ]​

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 size_t
0108 ngx_http_complex_value_size(ngx_http_request_t *r,
0109     ngx_http_complex_value_t *val, size_t default_value)
0110 {
0111     size_t     size;
0112     ngx_str_t  value;
0113 
0114     if (val == NULL) {
0115         return default_value;
0116     }
0117 
0118     if (val->lengths == NULL) {
0119         return val->u.size;
0120     }
0121 
0122     if (ngx_http_complex_value(r, val, &value) != NGX_OK) {
0123         return default_value;
0124     }
0125 
0126     size = ngx_parse_size(&value);
0127 
0128     if (size == (size_t) NGX_ERROR) {
0129         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
0130                       "invalid size \"%V\"", &value);
0131         return default_value;
0132     }
0133 
0134     return size;
0135 }
0136 
0137 
0138 ngx_int_t
0139 ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
0140 {
0141     ngx_str_t                  *v;
0142     ngx_uint_t                  i, n, nv, nc;
0143     ngx_array_t                 flushes, lengths, values, *pf, *pl, *pv;
0144     ngx_http_script_compile_t   sc;
0145 
0146     v = ccv->value;
0147 
0148     nv = 0;
0149     nc = 0;
0150 
0151     for (i = 0; i < v->len; i++) {
0152         if (v->data[i] == '$') {
0153             if (v->data[i + 1] >= '1' && v->data[i + 1] <= '9') {
0154                 nc++;
0155 
0156             } else {
0157                 nv++;
0158             }
0159         }
0160     }
0161 
0162     if ((v->len == 0 || v->data[0] != '$')
0163         && (ccv->conf_prefix || ccv->root_prefix))
0164     {
0165         if (ngx_conf_full_name(ccv->cf->cycle, v, ccv->conf_prefix) != NGX_OK) {
0166             return NGX_ERROR;
0167         }
0168 
0169         ccv->conf_prefix = 0;
0170         ccv->root_prefix = 0;
0171     }
0172 
0173     ccv->complex_value->value = *v;
0174     ccv->complex_value->flushes = NULL;
0175     ccv->complex_value->lengths = NULL;
0176     ccv->complex_value->values = NULL;
0177 
0178     if (nv == 0 && nc == 0) {
0179         return NGX_OK;
0180     }
0181 
0182     n = nv + 1;
0183 
0184     if (ngx_array_init(&flushes, ccv->cf->pool, n, sizeof(ngx_uint_t))
0185         != NGX_OK)
0186     {
0187         return NGX_ERROR;
0188     }
0189 
0190     n = nv * (2 * sizeof(ngx_http_script_copy_code_t)
0191                   + sizeof(ngx_http_script_var_code_t))
0192         + sizeof(uintptr_t);
0193 
0194     if (ngx_array_init(&lengths, ccv->cf->pool, n, 1) != NGX_OK) {
0195         return NGX_ERROR;
0196     }
0197 
0198     n = (nv * (2 * sizeof(ngx_http_script_copy_code_t)
0199                    + sizeof(ngx_http_script_var_code_t))
0200                 + sizeof(uintptr_t)
0201                 + v->len
0202                 + sizeof(uintptr_t) - 1)
0203             & ~(sizeof(uintptr_t) - 1);
0204 
0205     if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {
0206         return NGX_ERROR;
0207     }
0208 
0209     pf = &flushes;
0210     pl = &lengths;
0211     pv = &values;
0212 
0213     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
0214 
0215     sc.cf = ccv->cf;
0216     sc.source = v;
0217     sc.flushes = &pf;
0218     sc.lengths = &pl;
0219     sc.values = &pv;
0220     sc.complete_lengths = 1;
0221     sc.complete_values = 1;
0222     sc.zero = ccv->zero;
0223     sc.conf_prefix = ccv->conf_prefix;
0224     sc.root_prefix = ccv->root_prefix;
0225 
0226     if (ngx_http_script_compile(&sc) != NGX_OK) {
0227         return NGX_ERROR;
0228     }
0229 
0230     if (flushes.nelts) {
0231         ccv->complex_value->flushes = flushes.elts;
0232         ccv->complex_value->flushes[flushes.nelts] = (ngx_uint_t) -1;
0233     }
0234 
0235     ccv->complex_value->lengths = lengths.elts;
0236     ccv->complex_value->values = values.elts;
0237 
0238     return NGX_OK;
0239 }
0240 
0241 
0242 char *
0243 ngx_http_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0244 {
0245     char  *p = conf;
0246 
0247     ngx_str_t                          *value;
0248     ngx_http_complex_value_t          **cv;
0249     ngx_http_compile_complex_value_t    ccv;
0250 
0251     cv = (ngx_http_complex_value_t **) (p + cmd->offset);
0252 
0253     if (*cv != NULL) {
0254         return "is duplicate";
0255     }
0256 
0257     *cv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
0258     if (*cv == NULL) {
0259         return NGX_CONF_ERROR;
0260     }
0261 
0262     value = cf->args->elts;
0263 
0264     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
0265 
0266     ccv.cf = cf;
0267     ccv.value = &value[1];
0268     ccv.complex_value = *cv;
0269 
0270     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
0271         return NGX_CONF_ERROR;
0272     }
0273 
0274     return NGX_CONF_OK;
0275 }
0276 
0277 
0278 char *
0279 ngx_http_set_complex_value_size_slot(ngx_conf_t *cf, ngx_command_t *cmd,
0280     void *conf)
0281 {
0282     char  *p = conf;
0283 
0284     char                      *rv;
0285     ngx_http_complex_value_t  *cv;
0286 
0287     rv = ngx_http_set_complex_value_slot(cf, cmd, conf);
0288 
0289     if (rv != NGX_CONF_OK) {
0290         return rv;
0291     }
0292 
0293     cv = *(ngx_http_complex_value_t **) (p + cmd->offset);
0294 
0295     if (cv->lengths) {
0296         return NGX_CONF_OK;
0297     }
0298 
0299     cv->u.size = ngx_parse_size(&cv->value);
0300     if (cv->u.size == (size_t) NGX_ERROR) {
0301         return "invalid value";
0302     }
0303 
0304     return NGX_CONF_OK;
0305 }
0306 
0307 
0308 ngx_int_t
0309 ngx_http_test_predicates(ngx_http_request_t *r, ngx_array_t *predicates)
0310 {
0311     ngx_str_t                  val;
0312     ngx_uint_t                 i;
0313     ngx_http_complex_value_t  *cv;
0314 
0315     if (predicates == NULL) {
0316         return NGX_OK;
0317     }
0318 
0319     cv = predicates->elts;
0320 
0321     for (i = 0; i < predicates->nelts; i++) {
0322         if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
0323             return NGX_ERROR;
0324         }
0325 
0326         if (val.len && (val.len != 1 || val.data[0] != '0')) {
0327             return NGX_DECLINED;
0328         }
0329     }
0330 
0331     return NGX_OK;
0332 }
0333 
0334 
0335 ngx_int_t
0336 ngx_http_test_required_predicates(ngx_http_request_t *r,
0337     ngx_array_t *predicates)
0338 {
0339     ngx_str_t                  val;
0340     ngx_uint_t                 i;
0341     ngx_http_complex_value_t  *cv;
0342 
0343     if (predicates == NULL) {
0344         return NGX_OK;
0345     }
0346 
0347     cv = predicates->elts;
0348 
0349     for (i = 0; i < predicates->nelts; i++) {
0350         if (ngx_http_complex_value(r, &cv[i], &val) != NGX_OK) {
0351             return NGX_ERROR;
0352         }
0353 
0354         if (val.len == 0 || (val.len == 1 && val.data[0] == '0')) {
0355             return NGX_DECLINED;
0356         }
0357     }
0358 
0359     return NGX_OK;
0360 }
0361 
0362 
0363 char *
0364 ngx_http_set_predicate_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0365 {
0366     char  *p = conf;
0367 
0368     ngx_str_t                          *value;
0369     ngx_uint_t                          i;
0370     ngx_array_t                       **a;
0371     ngx_http_complex_value_t           *cv;
0372     ngx_http_compile_complex_value_t    ccv;
0373 
0374     a = (ngx_array_t **) (p + cmd->offset);
0375 
0376     if (*a == NGX_CONF_UNSET_PTR) {
0377         *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_complex_value_t));
0378         if (*a == NULL) {
0379             return NGX_CONF_ERROR;
0380         }
0381     }
0382 
0383     value = cf->args->elts;
0384 
0385     for (i = 1; i < cf->args->nelts; i++) {
0386         cv = ngx_array_push(*a);
0387         if (cv == NULL) {
0388             return NGX_CONF_ERROR;
0389         }
0390 
0391         ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
0392 
0393         ccv.cf = cf;
0394         ccv.value = &value[i];
0395         ccv.complex_value = cv;
0396 
0397         if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
0398             return NGX_CONF_ERROR;
0399         }
0400     }
0401 
0402     return NGX_CONF_OK;
0403 }
0404 
0405 
0406 ngx_uint_t
0407 ngx_http_script_variables_count(ngx_str_t *value)
0408 {
0409     ngx_uint_t  i, n;
0410 
0411     for (n = 0, i = 0; i < value->len; i++) {
0412         if (value->data[i] == '$') {
0413             n++;
0414         }
0415     }
0416 
0417     return n;
0418 }
0419 
0420 
0421 ngx_int_t
0422 ngx_http_script_compile(ngx_http_script_compile_t *sc)
0423 {
0424     u_char       ch;
0425     ngx_str_t    name;
0426     ngx_uint_t   i, bracket;
0427 
0428     if (ngx_http_script_init_arrays(sc) != NGX_OK) {
0429         return NGX_ERROR;
0430     }
0431 
0432     for (i = 0; i < sc->source->len; /* void */ ) {
0433 
0434         name.len = 0;
0435 
0436         if (sc->source->data[i] == '$') {
0437 
0438             if (++i == sc->source->len) {
0439                 goto invalid_variable;
0440             }
0441 
0442             if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') {
0443 #if (NGX_PCRE)
0444                 ngx_uint_t  n;
0445 
0446                 n = sc->source->data[i] - '0';
0447 
0448                 if (sc->captures_mask & ((ngx_uint_t) 1 << n)) {
0449                     sc->dup_capture = 1;
0450                 }
0451 
0452                 sc->captures_mask |= (ngx_uint_t) 1 << n;
0453 
0454                 if (ngx_http_script_add_capture_code(sc, n) != NGX_OK) {
0455                     return NGX_ERROR;
0456                 }
0457 
0458                 i++;
0459 
0460                 continue;
0461 #else
0462                 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
0463                                    "using variable \"$%c\" requires "
0464                                    "PCRE library", sc->source->data[i]);
0465                 return NGX_ERROR;
0466 #endif
0467             }
0468 
0469             if (sc->source->data[i] == '{') {
0470                 bracket = 1;
0471 
0472                 if (++i == sc->source->len) {
0473                     goto invalid_variable;
0474                 }
0475 
0476                 name.data = &sc->source->data[i];
0477 
0478             } else {
0479                 bracket = 0;
0480                 name.data = &sc->source->data[i];
0481             }
0482 
0483             for ( /* void */ ; i < sc->source->len; i++, name.len++) {
0484                 ch = sc->source->data[i];
0485 
0486                 if (ch == '}' && bracket) {
0487                     i++;
0488                     bracket = 0;
0489                     break;
0490                 }
0491 
0492                 if ((ch >= 'A' && ch <= 'Z')
0493                     || (ch >= 'a' && ch <= 'z')
0494                     || (ch >= '0' && ch <= '9')
0495                     || ch == '_')
0496                 {
0497                     continue;
0498                 }
0499 
0500                 break;
0501             }
0502 
0503             if (bracket) {
0504                 ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0,
0505                                    "the closing bracket in \"%V\" "
0506                                    "variable is missing", &name);
0507                 return NGX_ERROR;
0508             }
0509 
0510             if (name.len == 0) {
0511                 goto invalid_variable;
0512             }
0513 
0514             sc->variables++;
0515 
0516             if (ngx_http_script_add_var_code(sc, &name) != NGX_OK) {
0517                 return NGX_ERROR;
0518             }
0519 
0520             continue;
0521         }
0522 
0523         if (sc->source->data[i] == '?' && sc->compile_args) {
0524             sc->args = 1;
0525             sc->compile_args = 0;
0526 
0527             if (ngx_http_script_add_args_code(sc) != NGX_OK) {
0528                 return NGX_ERROR;
0529             }
0530 
0531             i++;
0532 
0533             continue;
0534         }
0535 
0536         name.data = &sc->source->data[i];
0537 
0538         while (i < sc->source->len) {
0539 
0540             if (sc->source->data[i] == '$') {
0541                 break;
0542             }
0543 
0544             if (sc->source->data[i] == '?') {
0545 
0546                 sc->args = 1;
0547 
0548                 if (sc->compile_args) {
0549                     break;
0550                 }
0551             }
0552 
0553             i++;
0554             name.len++;
0555         }
0556 
0557         sc->size += name.len;
0558 
0559         if (ngx_http_script_add_copy_code(sc, &name, (i == sc->source->len))
0560             != NGX_OK)
0561         {
0562             return NGX_ERROR;
0563         }
0564     }
0565 
0566     return ngx_http_script_done(sc);
0567 
0568 invalid_variable:
0569 
0570     ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name");
0571 
0572     return NGX_ERROR;
0573 }
0574 
0575 
0576 u_char *
0577 ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,
0578     void *code_lengths, size_t len, void *code_values)
0579 {
0580     ngx_uint_t                    i;
0581     ngx_http_script_code_pt       code;
0582     ngx_http_script_len_code_pt   lcode;
0583     ngx_http_script_engine_t      e;
0584     ngx_http_core_main_conf_t    *cmcf;
0585 
0586     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
0587 
0588     for (i = 0; i < cmcf->variables.nelts; i++) {
0589         if (r->variables[i].no_cacheable) {
0590             r->variables[i].valid = 0;
0591             r->variables[i].not_found = 0;
0592         }
0593     }
0594 
0595     ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
0596 
0597     e.ip = code_lengths;
0598     e.request = r;
0599     e.flushed = 1;
0600 
0601     while (*(uintptr_t *) e.ip) {
0602         lcode = *(ngx_http_script_len_code_pt *) e.ip;
0603         len += lcode(&e);
0604     }
0605 
0606 
0607     value->len = len;
0608     value->data = ngx_pnalloc(r->pool, len);
0609     if (value->data == NULL) {
0610         return NULL;
0611     }
0612 
0613     e.ip = code_values;
0614     e.pos = value->data;
0615 
0616     while (*(uintptr_t *) e.ip) {
0617         code = *(ngx_http_script_code_pt *) e.ip;
0618         code((ngx_http_script_engine_t *) &e);
0619     }
0620 
0621     return e.pos;
0622 }
0623 
0624 
0625 void
0626 ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r,
0627     ngx_array_t *indices)
0628 {
0629     ngx_uint_t  n, *index;
0630 
0631     if (indices) {
0632         index = indices->elts;
0633         for (n = 0; n < indices->nelts; n++) {
0634             if (r->variables[index[n]].no_cacheable) {
0635                 r->variables[index[n]].valid = 0;
0636                 r->variables[index[n]].not_found = 0;
0637             }
0638         }
0639     }
0640 }
0641 
0642 
0643 static ngx_int_t
0644 ngx_http_script_init_arrays(ngx_http_script_compile_t *sc)
0645 {
0646     ngx_uint_t   n;
0647 
0648     if (sc->flushes && *sc->flushes == NULL) {
0649         n = sc->variables ? sc->variables : 1;
0650         *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t));
0651         if (*sc->flushes == NULL) {
0652             return NGX_ERROR;
0653         }
0654     }
0655 
0656     if (*sc->lengths == NULL) {
0657         n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
0658                              + sizeof(ngx_http_script_var_code_t))
0659             + sizeof(uintptr_t);
0660 
0661         *sc->lengths = ngx_array_create(sc->cf->pool, n, 1);
0662         if (*sc->lengths == NULL) {
0663             return NGX_ERROR;
0664         }
0665     }
0666 
0667     if (*sc->values == NULL) {
0668         n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t)
0669                               + sizeof(ngx_http_script_var_code_t))
0670                 + sizeof(uintptr_t)
0671                 + sc->source->len
0672                 + sizeof(uintptr_t) - 1)
0673             & ~(sizeof(uintptr_t) - 1);
0674 
0675         *sc->values = ngx_array_create(sc->cf->pool, n, 1);
0676         if (*sc->values == NULL) {
0677             return NGX_ERROR;
0678         }
0679     }
0680 
0681     sc->variables = 0;
0682 
0683     return NGX_OK;
0684 }
0685 
0686 
0687 static ngx_int_t
0688 ngx_http_script_done(ngx_http_script_compile_t *sc)
0689 {
0690     ngx_str_t    zero;
0691     uintptr_t   *code;
0692 
0693     if (sc->zero) {
0694 
0695         zero.len = 1;
0696         zero.data = (u_char *) "\0";
0697 
0698         if (ngx_http_script_add_copy_code(sc, &zero, 0) != NGX_OK) {
0699             return NGX_ERROR;
0700         }
0701     }
0702 
0703     if (sc->conf_prefix || sc->root_prefix) {
0704         if (ngx_http_script_add_full_name_code(sc) != NGX_OK) {
0705             return NGX_ERROR;
0706         }
0707     }
0708 
0709     if (sc->complete_lengths) {
0710         code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
0711         if (code == NULL) {
0712             return NGX_ERROR;
0713         }
0714 
0715         *code = (uintptr_t) NULL;
0716     }
0717 
0718     if (sc->complete_values) {
0719         code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t),
0720                                         &sc->main);
0721         if (code == NULL) {
0722             return NGX_ERROR;
0723         }
0724 
0725         *code = (uintptr_t) NULL;
0726     }
0727 
0728     return NGX_OK;
0729 }
0730 
0731 
0732 void *
0733 ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
0734 {
0735     if (*codes == NULL) {
0736         *codes = ngx_array_create(pool, 256, 1);
0737         if (*codes == NULL) {
0738             return NULL;
0739         }
0740     }
0741 
0742     return ngx_array_push_n(*codes, size);
0743 }
0744 
0745 
0746 void *
0747 ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code)
0748 {
0749     u_char  *elts, **p;
0750     void    *new;
0751 
0752     elts = codes->elts;
0753 
0754     new = ngx_array_push_n(codes, size);
0755     if (new == NULL) {
0756         return NULL;
0757     }
0758 
0759     if (code) {
0760         if (elts != codes->elts) {
0761             p = code;
0762             *p += (u_char *) codes->elts - elts;
0763         }
0764     }
0765 
0766     return new;
0767 }
0768 
0769 
0770 static ngx_int_t
0771 ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc, ngx_str_t *value,
0772     ngx_uint_t last)
0773 {
0774     u_char                       *p;
0775     size_t                        size, len, zero;
0776     ngx_http_script_copy_code_t  *code;
0777 
0778     zero = (sc->zero && last);
0779     len = value->len + zero;
0780 
0781     code = ngx_http_script_add_code(*sc->lengths,
0782                                     sizeof(ngx_http_script_copy_code_t), NULL);
0783     if (code == NULL) {
0784         return NGX_ERROR;
0785     }
0786 
0787     code->code = (ngx_http_script_code_pt) (void *)
0788                                                  ngx_http_script_copy_len_code;
0789     code->len = len;
0790 
0791     size = (sizeof(ngx_http_script_copy_code_t) + len + sizeof(uintptr_t) - 1)
0792             & ~(sizeof(uintptr_t) - 1);
0793 
0794     code = ngx_http_script_add_code(*sc->values, size, &sc->main);
0795     if (code == NULL) {
0796         return NGX_ERROR;
0797     }
0798 
0799     code->code = ngx_http_script_copy_code;
0800     code->len = len;
0801 
0802     p = ngx_cpymem((u_char *) code + sizeof(ngx_http_script_copy_code_t),
0803                    value->data, value->len);
0804 
0805     if (zero) {
0806         *p = '\0';
0807         sc->zero = 0;
0808     }
0809 
0810     return NGX_OK;
0811 }
0812 
0813 
0814 size_t
0815 ngx_http_script_copy_len_code(ngx_http_script_engine_t *e)
0816 {
0817     ngx_http_script_copy_code_t  *code;
0818 
0819     code = (ngx_http_script_copy_code_t *) e->ip;
0820 
0821     e->ip += sizeof(ngx_http_script_copy_code_t);
0822 
0823     return code->len;
0824 }
0825 
0826 
0827 void
0828 ngx_http_script_copy_code(ngx_http_script_engine_t *e)
0829 {
0830     u_char                       *p;
0831     ngx_http_script_copy_code_t  *code;
0832 
0833     code = (ngx_http_script_copy_code_t *) e->ip;
0834 
0835     p = e->pos;
0836 
0837     if (!e->skip) {
0838         e->pos = ngx_copy(p, e->ip + sizeof(ngx_http_script_copy_code_t),
0839                           code->len);
0840     }
0841 
0842     e->ip += sizeof(ngx_http_script_copy_code_t)
0843           + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1));
0844 
0845     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
0846                    "http script copy: \"%*s\"", e->pos - p, p);
0847 }
0848 
0849 
0850 static ngx_int_t
0851 ngx_http_script_add_var_code(ngx_http_script_compile_t *sc, ngx_str_t *name)
0852 {
0853     ngx_int_t                    index, *p;
0854     ngx_http_script_var_code_t  *code;
0855 
0856     index = ngx_http_get_variable_index(sc->cf, name);
0857 
0858     if (index == NGX_ERROR) {
0859         return NGX_ERROR;
0860     }
0861 
0862     if (sc->flushes) {
0863         p = ngx_array_push(*sc->flushes);
0864         if (p == NULL) {
0865             return NGX_ERROR;
0866         }
0867 
0868         *p = index;
0869     }
0870 
0871     code = ngx_http_script_add_code(*sc->lengths,
0872                                     sizeof(ngx_http_script_var_code_t), NULL);
0873     if (code == NULL) {
0874         return NGX_ERROR;
0875     }
0876 
0877     code->code = (ngx_http_script_code_pt) (void *)
0878                                              ngx_http_script_copy_var_len_code;
0879     code->index = (uintptr_t) index;
0880 
0881     code = ngx_http_script_add_code(*sc->values,
0882                                     sizeof(ngx_http_script_var_code_t),
0883                                     &sc->main);
0884     if (code == NULL) {
0885         return NGX_ERROR;
0886     }
0887 
0888     code->code = ngx_http_script_copy_var_code;
0889     code->index = (uintptr_t) index;
0890 
0891     return NGX_OK;
0892 }
0893 
0894 
0895 size_t
0896 ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e)
0897 {
0898     ngx_http_variable_value_t   *value;
0899     ngx_http_script_var_code_t  *code;
0900 
0901     code = (ngx_http_script_var_code_t *) e->ip;
0902 
0903     e->ip += sizeof(ngx_http_script_var_code_t);
0904 
0905     if (e->flushed) {
0906         value = ngx_http_get_indexed_variable(e->request, code->index);
0907 
0908     } else {
0909         value = ngx_http_get_flushed_variable(e->request, code->index);
0910     }
0911 
0912     if (value && !value->not_found) {
0913         return value->len;
0914     }
0915 
0916     return 0;
0917 }
0918 
0919 
0920 void
0921 ngx_http_script_copy_var_code(ngx_http_script_engine_t *e)
0922 {
0923     u_char                      *p;
0924     ngx_http_variable_value_t   *value;
0925     ngx_http_script_var_code_t  *code;
0926 
0927     code = (ngx_http_script_var_code_t *) e->ip;
0928 
0929     e->ip += sizeof(ngx_http_script_var_code_t);
0930 
0931     if (!e->skip) {
0932 
0933         if (e->flushed) {
0934             value = ngx_http_get_indexed_variable(e->request, code->index);
0935 
0936         } else {
0937             value = ngx_http_get_flushed_variable(e->request, code->index);
0938         }
0939 
0940         if (value && !value->not_found) {
0941             p = e->pos;
0942             e->pos = ngx_copy(p, value->data, value->len);
0943 
0944             ngx_log_debug2(NGX_LOG_DEBUG_HTTP,
0945                            e->request->connection->log, 0,
0946                            "http script var: \"%*s\"", e->pos - p, p);
0947         }
0948     }
0949 }
0950 
0951 
0952 static ngx_int_t
0953 ngx_http_script_add_args_code(ngx_http_script_compile_t *sc)
0954 {
0955     uintptr_t   *code;
0956 
0957     code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL);
0958     if (code == NULL) {
0959         return NGX_ERROR;
0960     }
0961 
0962     *code = (uintptr_t) ngx_http_script_mark_args_code;
0963 
0964     code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), &sc->main);
0965     if (code == NULL) {
0966         return NGX_ERROR;
0967     }
0968 
0969     *code = (uintptr_t) ngx_http_script_start_args_code;
0970 
0971     return NGX_OK;
0972 }
0973 
0974 
0975 size_t
0976 ngx_http_script_mark_args_code(ngx_http_script_engine_t *e)
0977 {
0978     e->is_args = 1;
0979     e->ip += sizeof(uintptr_t);
0980 
0981     return 1;
0982 }
0983 
0984 
0985 void
0986 ngx_http_script_start_args_code(ngx_http_script_engine_t *e)
0987 {
0988     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
0989                    "http script args");
0990 
0991     e->is_args = 1;
0992     e->args = e->pos;
0993     e->ip += sizeof(uintptr_t);
0994 }
0995 
0996 
0997 #if (NGX_PCRE)
0998 
0999 void
1000 ngx_http_script_regex_start_code(ngx_http_script_engine_t *e)
1001 {
1002     size_t                         len;
1003     ngx_int_t                      rc;
1004     ngx_uint_t                     n;
1005     ngx_http_request_t            *r;
1006     ngx_http_script_engine_t       le;
1007     ngx_http_script_len_code_pt    lcode;
1008     ngx_http_script_regex_code_t  *code;
1009 
1010     code = (ngx_http_script_regex_code_t *) e->ip;
1011 
1012     r = e->request;
1013 
1014     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1015                    "http script regex: \"%V\"", &code->name);
1016 
1017     if (code->uri) {
1018         e->line = r->uri;
1019     } else {
1020         e->sp--;
1021         e->line.len = e->sp->len;
1022         e->line.data = e->sp->data;
1023     }
1024 
1025     rc = ngx_http_regex_exec(r, code->regex, &e->line);
1026 
1027     if (rc == NGX_DECLINED) {
1028         if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
1029             ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
1030                           "\"%V\" does not match \"%V\"",
1031                           &code->name, &e->line);
1032         }
1033 
1034         r->ncaptures = 0;
1035 
1036         if (code->test) {
1037             if (code->negative_test) {
1038                 e->sp->len = 1;
1039                 e->sp->data = (u_char *) "1";
1040 
1041             } else {
1042                 e->sp->len = 0;
1043                 e->sp->data = (u_char *) "";
1044             }
1045 
1046             e->sp++;
1047 
1048             e->ip += sizeof(ngx_http_script_regex_code_t);
1049             return;
1050         }
1051 
1052         e->ip += code->next;
1053         return;
1054     }
1055 
1056     if (rc == NGX_ERROR) {
1057         e->ip = ngx_http_script_exit;
1058         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1059         return;
1060     }
1061 
1062     if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
1063         ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
1064                       "\"%V\" matches \"%V\"", &code->name, &e->line);
1065     }
1066 
1067     if (code->test) {
1068         if (code->negative_test) {
1069             e->sp->len = 0;
1070             e->sp->data = (u_char *) "";
1071 
1072         } else {
1073             e->sp->len = 1;
1074             e->sp->data = (u_char *) "1";
1075         }
1076 
1077         e->sp++;
1078 
1079         e->ip += sizeof(ngx_http_script_regex_code_t);
1080         return;
1081     }
1082 
1083     if (code->status) {
1084         e->status = code->status;
1085 
1086         if (!code->redirect) {
1087             e->ip = ngx_http_script_exit;
1088             return;
1089         }
1090     }
1091 
1092     if (code->uri) {
1093         r->internal = 1;
1094         r->valid_unparsed_uri = 0;
1095 
1096         if (code->break_cycle) {
1097             r->valid_location = 0;
1098             r->uri_changed = 0;
1099 
1100         } else {
1101             r->uri_changed = 1;
1102         }
1103     }
1104 
1105     if (code->lengths == NULL) {
1106         e->buf.len = code->size;
1107 
1108         if (code->uri) {
1109             if (r->ncaptures && (r->quoted_uri || r->plus_in_uri)) {
1110                 e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
1111                                                  NGX_ESCAPE_ARGS);
1112             }
1113         }
1114 
1115         for (n = 2; n < r->ncaptures; n += 2) {
1116             e->buf.len += r->captures[n + 1] - r->captures[n];
1117         }
1118 
1119     } else {
1120         ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
1121 
1122         le.ip = code->lengths->elts;
1123         le.line = e->line;
1124         le.request = r;
1125         le.quote = code->redirect;
1126 
1127         len = 0;
1128 
1129         while (*(uintptr_t *) le.ip) {
1130             lcode = *(ngx_http_script_len_code_pt *) le.ip;
1131             len += lcode(&le);
1132         }
1133 
1134         e->buf.len = len;
1135     }
1136 
1137     if (code->add_args && r->args.len) {
1138         e->buf.len += r->args.len + 1;
1139     }
1140 
1141     e->buf.data = ngx_pnalloc(r->pool, e->buf.len);
1142     if (e->buf.data == NULL) {
1143         e->ip = ngx_http_script_exit;
1144         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1145         return;
1146     }
1147 
1148     e->quote = code->redirect;
1149 
1150     e->pos = e->buf.data;
1151 
1152     e->ip += sizeof(ngx_http_script_regex_code_t);
1153 }
1154 
1155 
1156 void
1157 ngx_http_script_regex_end_code(ngx_http_script_engine_t *e)
1158 {
1159     u_char                            *dst, *src;
1160     ngx_http_request_t                *r;
1161     ngx_http_script_regex_end_code_t  *code;
1162 
1163     code = (ngx_http_script_regex_end_code_t *) e->ip;
1164 
1165     r = e->request;
1166 
1167     e->quote = 0;
1168 
1169     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1170                    "http script regex end");
1171 
1172     if (code->redirect) {
1173 
1174         dst = e->buf.data;
1175         src = e->buf.data;
1176 
1177         ngx_unescape_uri(&dst, &src, e->pos - e->buf.data,
1178                          NGX_UNESCAPE_REDIRECT);
1179 
1180         if (src < e->pos) {
1181             dst = ngx_movemem(dst, src, e->pos - src);
1182         }
1183 
1184         e->pos = dst;
1185 
1186         if (code->add_args && r->args.len) {
1187             *e->pos++ = (u_char) (code->args ? '&' : '?');
1188             e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
1189         }
1190 
1191         e->buf.len = e->pos - e->buf.data;
1192 
1193         if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
1194             ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
1195                           "rewritten redirect: \"%V\"", &e->buf);
1196         }
1197 
1198         ngx_http_clear_location(r);
1199 
1200         r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1201         if (r->headers_out.location == NULL) {
1202             e->ip = ngx_http_script_exit;
1203             e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1204             return;
1205         }
1206 
1207         r->headers_out.location->hash = 1;
1208         ngx_str_set(&r->headers_out.location->key, "Location");
1209         r->headers_out.location->value = e->buf;
1210 
1211         e->ip += sizeof(ngx_http_script_regex_end_code_t);
1212         return;
1213     }
1214 
1215     if (e->args) {
1216         e->buf.len = e->args - e->buf.data;
1217 
1218         if (code->add_args && r->args.len) {
1219             *e->pos++ = '&';
1220             e->pos = ngx_copy(e->pos, r->args.data, r->args.len);
1221         }
1222 
1223         r->args.len = e->pos - e->args;
1224         r->args.data = e->args;
1225 
1226         e->args = NULL;
1227 
1228     } else {
1229         e->buf.len = e->pos - e->buf.data;
1230 
1231         if (!code->add_args) {
1232             r->args.len = 0;
1233         }
1234     }
1235 
1236     if (e->log || (r->connection->log->log_level & NGX_LOG_DEBUG_HTTP)) {
1237         ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
1238                       "rewritten data: \"%V\", args: \"%V\"",
1239                       &e->buf, &r->args);
1240     }
1241 
1242     if (code->uri) {
1243         r->uri = e->buf;
1244 
1245         if (r->uri.len == 0) {
1246             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1247                           "the rewritten URI has a zero length");
1248             e->ip = ngx_http_script_exit;
1249             e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1250             return;
1251         }
1252 
1253         ngx_http_set_exten(r);
1254     }
1255 
1256     e->ip += sizeof(ngx_http_script_regex_end_code_t);
1257 }
1258 
1259 
1260 static ngx_int_t
1261 ngx_http_script_add_capture_code(ngx_http_script_compile_t *sc, ngx_uint_t n)
1262 {
1263     ngx_http_script_copy_capture_code_t  *code;
1264 
1265     code = ngx_http_script_add_code(*sc->lengths,
1266                                     sizeof(ngx_http_script_copy_capture_code_t),
1267                                     NULL);
1268     if (code == NULL) {
1269         return NGX_ERROR;
1270     }
1271 
1272     code->code = (ngx_http_script_code_pt) (void *)
1273                                          ngx_http_script_copy_capture_len_code;
1274     code->n = 2 * n;
1275 
1276 
1277     code = ngx_http_script_add_code(*sc->values,
1278                                     sizeof(ngx_http_script_copy_capture_code_t),
1279                                     &sc->main);
1280     if (code == NULL) {
1281         return NGX_ERROR;
1282     }
1283 
1284     code->code = ngx_http_script_copy_capture_code;
1285     code->n = 2 * n;
1286 
1287     if (sc->ncaptures < n) {
1288         sc->ncaptures = n;
1289     }
1290 
1291     return NGX_OK;
1292 }
1293 
1294 
1295 size_t
1296 ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e)
1297 {
1298     int                                  *cap;
1299     u_char                               *p;
1300     ngx_uint_t                            n;
1301     ngx_http_request_t                   *r;
1302     ngx_http_script_copy_capture_code_t  *code;
1303 
1304     r = e->request;
1305 
1306     code = (ngx_http_script_copy_capture_code_t *) e->ip;
1307 
1308     e->ip += sizeof(ngx_http_script_copy_capture_code_t);
1309 
1310     n = code->n;
1311 
1312     if (n < r->ncaptures) {
1313 
1314         cap = r->captures;
1315 
1316         if ((e->is_args || e->quote)
1317             && (e->request->quoted_uri || e->request->plus_in_uri))
1318         {
1319             p = r->captures_data;
1320 
1321             return cap[n + 1] - cap[n]
1322                    + 2 * ngx_escape_uri(NULL, &p[cap[n]], cap[n + 1] - cap[n],
1323                                         NGX_ESCAPE_ARGS);
1324         } else {
1325             return cap[n + 1] - cap[n];
1326         }
1327     }
1328 
1329     return 0;
1330 }
1331 
1332 
1333 void
1334 ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e)
1335 {
1336     int                                  *cap;
1337     u_char                               *p, *pos;
1338     ngx_uint_t                            n;
1339     ngx_http_request_t                   *r;
1340     ngx_http_script_copy_capture_code_t  *code;
1341 
1342     r = e->request;
1343 
1344     code = (ngx_http_script_copy_capture_code_t *) e->ip;
1345 
1346     e->ip += sizeof(ngx_http_script_copy_capture_code_t);
1347 
1348     n = code->n;
1349 
1350     pos = e->pos;
1351 
1352     if (n < r->ncaptures) {
1353 
1354         cap = r->captures;
1355         p = r->captures_data;
1356 
1357         if ((e->is_args || e->quote)
1358             && (e->request->quoted_uri || e->request->plus_in_uri))
1359         {
1360             e->pos = (u_char *) ngx_escape_uri(pos, &p[cap[n]],
1361                                                cap[n + 1] - cap[n],
1362                                                NGX_ESCAPE_ARGS);
1363         } else {
1364             e->pos = ngx_copy(pos, &p[cap[n]], cap[n + 1] - cap[n]);
1365         }
1366     }
1367 
1368     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1369                    "http script capture: \"%*s\"", e->pos - pos, pos);
1370 }
1371 
1372 #endif
1373 
1374 
1375 static ngx_int_t
1376 ngx_http_script_add_full_name_code(ngx_http_script_compile_t *sc)
1377 {
1378     ngx_http_script_full_name_code_t  *code;
1379 
1380     code = ngx_http_script_add_code(*sc->lengths,
1381                                     sizeof(ngx_http_script_full_name_code_t),
1382                                     NULL);
1383     if (code == NULL) {
1384         return NGX_ERROR;
1385     }
1386 
1387     code->code = (ngx_http_script_code_pt) (void *)
1388                                             ngx_http_script_full_name_len_code;
1389     code->conf_prefix = sc->conf_prefix;
1390 
1391     code = ngx_http_script_add_code(*sc->values,
1392                                     sizeof(ngx_http_script_full_name_code_t),
1393                                     &sc->main);
1394     if (code == NULL) {
1395         return NGX_ERROR;
1396     }
1397 
1398     code->code = ngx_http_script_full_name_code;
1399     code->conf_prefix = sc->conf_prefix;
1400 
1401     return NGX_OK;
1402 }
1403 
1404 
1405 static size_t
1406 ngx_http_script_full_name_len_code(ngx_http_script_engine_t *e)
1407 {
1408     ngx_http_script_full_name_code_t  *code;
1409 
1410     code = (ngx_http_script_full_name_code_t *) e->ip;
1411 
1412     e->ip += sizeof(ngx_http_script_full_name_code_t);
1413 
1414     return code->conf_prefix ? ngx_cycle->conf_prefix.len:
1415                                ngx_cycle->prefix.len;
1416 }
1417 
1418 
1419 static void
1420 ngx_http_script_full_name_code(ngx_http_script_engine_t *e)
1421 {
1422     ngx_http_script_full_name_code_t  *code;
1423 
1424     ngx_str_t  value, *prefix;
1425 
1426     code = (ngx_http_script_full_name_code_t *) e->ip;
1427 
1428     value.data = e->buf.data;
1429     value.len = e->pos - e->buf.data;
1430 
1431     prefix = code->conf_prefix ? (ngx_str_t *) &ngx_cycle->conf_prefix:
1432                                  (ngx_str_t *) &ngx_cycle->prefix;
1433 
1434     if (ngx_get_full_name(e->request->pool, prefix, &value) != NGX_OK) {
1435         e->ip = ngx_http_script_exit;
1436         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1437         return;
1438     }
1439 
1440     e->buf = value;
1441 
1442     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1443                    "http script fullname: \"%V\"", &value);
1444 
1445     e->ip += sizeof(ngx_http_script_full_name_code_t);
1446 }
1447 
1448 
1449 void
1450 ngx_http_script_return_code(ngx_http_script_engine_t *e)
1451 {
1452     ngx_http_script_return_code_t  *code;
1453 
1454     code = (ngx_http_script_return_code_t *) e->ip;
1455 
1456     if (code->status < NGX_HTTP_BAD_REQUEST
1457         || code->text.value.len
1458         || code->text.lengths)
1459     {
1460         e->status = ngx_http_send_response(e->request, code->status, NULL,
1461                                            &code->text);
1462     } else {
1463         e->status = code->status;
1464     }
1465 
1466     e->ip = ngx_http_script_exit;
1467 }
1468 
1469 
1470 void
1471 ngx_http_script_break_code(ngx_http_script_engine_t *e)
1472 {
1473     ngx_http_request_t  *r;
1474 
1475     r = e->request;
1476 
1477     if (r->uri_changed) {
1478         r->valid_location = 0;
1479         r->uri_changed = 0;
1480     }
1481 
1482     e->ip = ngx_http_script_exit;
1483 }
1484 
1485 
1486 void
1487 ngx_http_script_if_code(ngx_http_script_engine_t *e)
1488 {
1489     ngx_http_script_if_code_t  *code;
1490 
1491     code = (ngx_http_script_if_code_t *) e->ip;
1492 
1493     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1494                    "http script if");
1495 
1496     e->sp--;
1497 
1498     if (e->sp->len && (e->sp->len != 1 || e->sp->data[0] != '0')) {
1499         if (code->loc_conf) {
1500             e->request->loc_conf = code->loc_conf;
1501             ngx_http_update_location_config(e->request);
1502         }
1503 
1504         e->ip += sizeof(ngx_http_script_if_code_t);
1505         return;
1506     }
1507 
1508     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1509                    "http script if: false");
1510 
1511     e->ip += code->next;
1512 }
1513 
1514 
1515 void
1516 ngx_http_script_equal_code(ngx_http_script_engine_t *e)
1517 {
1518     ngx_http_variable_value_t  *val, *res;
1519 
1520     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1521                    "http script equal");
1522 
1523     e->sp--;
1524     val = e->sp;
1525     res = e->sp - 1;
1526 
1527     e->ip += sizeof(uintptr_t);
1528 
1529     if (val->len == res->len
1530         && ngx_strncmp(val->data, res->data, res->len) == 0)
1531     {
1532         *res = ngx_http_variable_true_value;
1533         return;
1534     }
1535 
1536     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1537                    "http script equal: no");
1538 
1539     *res = ngx_http_variable_null_value;
1540 }
1541 
1542 
1543 void
1544 ngx_http_script_not_equal_code(ngx_http_script_engine_t *e)
1545 {
1546     ngx_http_variable_value_t  *val, *res;
1547 
1548     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1549                    "http script not equal");
1550 
1551     e->sp--;
1552     val = e->sp;
1553     res = e->sp - 1;
1554 
1555     e->ip += sizeof(uintptr_t);
1556 
1557     if (val->len == res->len
1558         && ngx_strncmp(val->data, res->data, res->len) == 0)
1559     {
1560         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1561                        "http script not equal: no");
1562 
1563         *res = ngx_http_variable_null_value;
1564         return;
1565     }
1566 
1567     *res = ngx_http_variable_true_value;
1568 }
1569 
1570 
1571 void
1572 ngx_http_script_file_code(ngx_http_script_engine_t *e)
1573 {
1574     ngx_str_t                     path;
1575     ngx_http_request_t           *r;
1576     ngx_open_file_info_t          of;
1577     ngx_http_core_loc_conf_t     *clcf;
1578     ngx_http_variable_value_t    *value;
1579     ngx_http_script_file_code_t  *code;
1580 
1581     value = e->sp - 1;
1582 
1583     code = (ngx_http_script_file_code_t *) e->ip;
1584     e->ip += sizeof(ngx_http_script_file_code_t);
1585 
1586     path.len = value->len - 1;
1587     path.data = value->data;
1588 
1589     r = e->request;
1590 
1591     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1592                    "http script file op %p \"%V\"", (void *) code->op, &path);
1593 
1594     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1595 
1596     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
1597 
1598     of.read_ahead = clcf->read_ahead;
1599     of.directio = clcf->directio;
1600     of.valid = clcf->open_file_cache_valid;
1601     of.min_uses = clcf->open_file_cache_min_uses;
1602     of.test_only = 1;
1603     of.errors = clcf->open_file_cache_errors;
1604     of.events = clcf->open_file_cache_events;
1605 
1606     if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
1607         e->ip = ngx_http_script_exit;
1608         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1609         return;
1610     }
1611 
1612     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
1613         != NGX_OK)
1614     {
1615         if (of.err == 0) {
1616             e->ip = ngx_http_script_exit;
1617             e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1618             return;
1619         }
1620 
1621         if (of.err != NGX_ENOENT
1622             && of.err != NGX_ENOTDIR
1623             && of.err != NGX_ENAMETOOLONG)
1624         {
1625             ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
1626                           "%s \"%s\" failed", of.failed, value->data);
1627         }
1628 
1629         switch (code->op) {
1630 
1631         case ngx_http_script_file_plain:
1632         case ngx_http_script_file_dir:
1633         case ngx_http_script_file_exists:
1634         case ngx_http_script_file_exec:
1635              goto false_value;
1636 
1637         case ngx_http_script_file_not_plain:
1638         case ngx_http_script_file_not_dir:
1639         case ngx_http_script_file_not_exists:
1640         case ngx_http_script_file_not_exec:
1641              goto true_value;
1642         }
1643 
1644         goto false_value;
1645     }
1646 
1647     switch (code->op) {
1648     case ngx_http_script_file_plain:
1649         if (of.is_file) {
1650              goto true_value;
1651         }
1652         goto false_value;
1653 
1654     case ngx_http_script_file_not_plain:
1655         if (of.is_file) {
1656             goto false_value;
1657         }
1658         goto true_value;
1659 
1660     case ngx_http_script_file_dir:
1661         if (of.is_dir) {
1662              goto true_value;
1663         }
1664         goto false_value;
1665 
1666     case ngx_http_script_file_not_dir:
1667         if (of.is_dir) {
1668             goto false_value;
1669         }
1670         goto true_value;
1671 
1672     case ngx_http_script_file_exists:
1673         if (of.is_file || of.is_dir || of.is_link) {
1674              goto true_value;
1675         }
1676         goto false_value;
1677 
1678     case ngx_http_script_file_not_exists:
1679         if (of.is_file || of.is_dir || of.is_link) {
1680             goto false_value;
1681         }
1682         goto true_value;
1683 
1684     case ngx_http_script_file_exec:
1685         if (of.is_exec) {
1686              goto true_value;
1687         }
1688         goto false_value;
1689 
1690     case ngx_http_script_file_not_exec:
1691         if (of.is_exec) {
1692             goto false_value;
1693         }
1694         goto true_value;
1695     }
1696 
1697 false_value:
1698 
1699     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1700                    "http script file op false");
1701 
1702     *value = ngx_http_variable_null_value;
1703     return;
1704 
1705 true_value:
1706 
1707     *value = ngx_http_variable_true_value;
1708     return;
1709 }
1710 
1711 
1712 void
1713 ngx_http_script_complex_value_code(ngx_http_script_engine_t *e)
1714 {
1715     size_t                                 len;
1716     ngx_http_script_engine_t               le;
1717     ngx_http_script_len_code_pt            lcode;
1718     ngx_http_script_complex_value_code_t  *code;
1719 
1720     code = (ngx_http_script_complex_value_code_t *) e->ip;
1721 
1722     e->ip += sizeof(ngx_http_script_complex_value_code_t);
1723 
1724     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1725                    "http script complex value");
1726 
1727     ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
1728 
1729     le.ip = code->lengths->elts;
1730     le.line = e->line;
1731     le.request = e->request;
1732     le.quote = e->quote;
1733 
1734     for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
1735         lcode = *(ngx_http_script_len_code_pt *) le.ip;
1736     }
1737 
1738     e->buf.len = len;
1739     e->buf.data = ngx_pnalloc(e->request->pool, len);
1740     if (e->buf.data == NULL) {
1741         e->ip = ngx_http_script_exit;
1742         e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
1743         return;
1744     }
1745 
1746     e->pos = e->buf.data;
1747 
1748     e->sp->len = e->buf.len;
1749     e->sp->data = e->buf.data;
1750     e->sp++;
1751 }
1752 
1753 
1754 void
1755 ngx_http_script_value_code(ngx_http_script_engine_t *e)
1756 {
1757     ngx_http_script_value_code_t  *code;
1758 
1759     code = (ngx_http_script_value_code_t *) e->ip;
1760 
1761     e->ip += sizeof(ngx_http_script_value_code_t);
1762 
1763     e->sp->len = code->text_len;
1764     e->sp->data = (u_char *) code->text_data;
1765 
1766     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1767                    "http script value: \"%v\"", e->sp);
1768 
1769     e->sp++;
1770 }
1771 
1772 
1773 void
1774 ngx_http_script_set_var_code(ngx_http_script_engine_t *e)
1775 {
1776     ngx_http_request_t          *r;
1777     ngx_http_script_var_code_t  *code;
1778 
1779     code = (ngx_http_script_var_code_t *) e->ip;
1780 
1781     e->ip += sizeof(ngx_http_script_var_code_t);
1782 
1783     r = e->request;
1784 
1785     e->sp--;
1786 
1787     r->variables[code->index].len = e->sp->len;
1788     r->variables[code->index].valid = 1;
1789     r->variables[code->index].no_cacheable = 0;
1790     r->variables[code->index].not_found = 0;
1791     r->variables[code->index].data = e->sp->data;
1792 
1793 #if (NGX_DEBUG)
1794     {
1795     ngx_http_variable_t        *v;
1796     ngx_http_core_main_conf_t  *cmcf;
1797 
1798     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
1799 
1800     v = cmcf->variables.elts;
1801 
1802     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1803                    "http script set $%V", &v[code->index].name);
1804     }
1805 #endif
1806 }
1807 
1808 
1809 void
1810 ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e)
1811 {
1812     ngx_http_script_var_handler_code_t  *code;
1813 
1814     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1815                    "http script set var handler");
1816 
1817     code = (ngx_http_script_var_handler_code_t *) e->ip;
1818 
1819     e->ip += sizeof(ngx_http_script_var_handler_code_t);
1820 
1821     e->sp--;
1822 
1823     code->handler(e->request, e->sp, code->data);
1824 }
1825 
1826 
1827 void
1828 ngx_http_script_var_code(ngx_http_script_engine_t *e)
1829 {
1830     ngx_http_variable_value_t   *value;
1831     ngx_http_script_var_code_t  *code;
1832 
1833     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1834                    "http script var");
1835 
1836     code = (ngx_http_script_var_code_t *) e->ip;
1837 
1838     e->ip += sizeof(ngx_http_script_var_code_t);
1839 
1840     value = ngx_http_get_flushed_variable(e->request, code->index);
1841 
1842     if (value && !value->not_found) {
1843         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
1844                        "http script var: \"%v\"", value);
1845 
1846         *e->sp = *value;
1847         e->sp++;
1848 
1849         return;
1850     }
1851 
1852     *e->sp = ngx_http_variable_null_value;
1853     e->sp++;
1854 }
1855 
1856 
1857 void
1858 ngx_http_script_nop_code(ngx_http_script_engine_t *e)
1859 {
1860     e->ip += sizeof(uintptr_t);
1861 }