3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 ngx_array_t *codes; /* uintptr_t */
15 ngx_uint_t stack_size;
18 ngx_flag_t uninitialized_variable_warn;
19 } ngx_http_rewrite_loc_conf_t;
22 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf);
23 static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf,
24 void *parent, void *child);
25 static ngx_int_t ngx_http_rewrite_init(ngx_conf_t *cf);
26 static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
27 static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd,
29 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd,
31 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd,
33 static char * ngx_http_rewrite_if_condition(ngx_conf_t *cf,
34 ngx_http_rewrite_loc_conf_t *lcf);
35 static char *ngx_http_rewrite_variable(ngx_conf_t *cf,
36 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
37 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd,
39 static char * ngx_http_rewrite_value(ngx_conf_t *cf,
40 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
43 static ngx_command_t ngx_http_rewrite_commands[] = {
45 { ngx_string("rewrite"),
46 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
49 NGX_HTTP_LOC_CONF_OFFSET,
53 { ngx_string("return"),
54 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
56 ngx_http_rewrite_return,
57 NGX_HTTP_LOC_CONF_OFFSET,
61 { ngx_string("break"),
62 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
64 ngx_http_rewrite_break,
65 NGX_HTTP_LOC_CONF_OFFSET,
70 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
72 NGX_HTTP_LOC_CONF_OFFSET,
77 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
80 NGX_HTTP_LOC_CONF_OFFSET,
84 { ngx_string("rewrite_log"),
85 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
86 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
87 ngx_conf_set_flag_slot,
88 NGX_HTTP_LOC_CONF_OFFSET,
89 offsetof(ngx_http_rewrite_loc_conf_t, log),
92 { ngx_string("uninitialized_variable_warn"),
93 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
94 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
95 ngx_conf_set_flag_slot,
96 NGX_HTTP_LOC_CONF_OFFSET,
97 offsetof(ngx_http_rewrite_loc_conf_t, uninitialized_variable_warn),
104 static ngx_http_module_t ngx_http_rewrite_module_ctx = {
105 NULL, /* preconfiguration */
106 ngx_http_rewrite_init, /* postconfiguration */
108 NULL, /* create main configuration */
109 NULL, /* init main configuration */
111 NULL, /* create server configuration */
112 NULL, /* merge server configuration */
114 ngx_http_rewrite_create_loc_conf, /* create location configration */
115 ngx_http_rewrite_merge_loc_conf /* merge location configration */
119 ngx_module_t ngx_http_rewrite_module = {
121 &ngx_http_rewrite_module_ctx, /* module context */
122 ngx_http_rewrite_commands, /* module directives */
123 NGX_HTTP_MODULE, /* module type */
124 NULL, /* init master */
125 NULL, /* init module */
126 NULL, /* init process */
127 NULL, /* init thread */
128 NULL, /* exit thread */
129 NULL, /* exit process */
130 NULL, /* exit master */
131 NGX_MODULE_V1_PADDING
136 ngx_http_rewrite_handler(ngx_http_request_t *r)
138 ngx_http_script_code_pt code;
139 ngx_http_script_engine_t *e;
140 ngx_http_rewrite_loc_conf_t *rlcf;
142 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
144 if (rlcf->codes == NULL) {
148 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t));
150 return NGX_HTTP_INTERNAL_SERVER_ERROR;
153 e->sp = ngx_pcalloc(r->pool,
154 rlcf->stack_size * sizeof(ngx_http_variable_value_t));
156 return NGX_HTTP_INTERNAL_SERVER_ERROR;
159 e->ip = rlcf->codes->elts;
163 e->status = NGX_DECLINED;
165 while (*(uintptr_t *) e->ip) {
166 code = *(ngx_http_script_code_pt *) e->ip;
170 if (e->status == NGX_DECLINED) {
174 if (r->err_status == 0) {
178 return r->err_status;
183 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v,
186 ngx_http_variable_t *var;
187 ngx_http_core_main_conf_t *cmcf;
188 ngx_http_rewrite_loc_conf_t *rlcf;
190 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
192 if (rlcf->uninitialized_variable_warn == 0) {
193 *v = ngx_http_variable_null_value;
197 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
199 var = cmcf->variables.elts;
202 * the ngx_http_rewrite_module sets variables directly in r->variables,
203 * and they should be handled by ngx_http_get_indexed_variable(),
204 * so the handler is called only if the variable is not initialized
207 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
208 "using uninitialized \"%V\" variable", &var[data].name);
210 *v = ngx_http_variable_null_value;
217 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
219 ngx_http_rewrite_loc_conf_t *conf;
221 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t));
223 return NGX_CONF_ERROR;
226 conf->stack_size = NGX_CONF_UNSET_UINT;
227 conf->log = NGX_CONF_UNSET;
228 conf->uninitialized_variable_warn = NGX_CONF_UNSET;
235 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
237 ngx_http_rewrite_loc_conf_t *prev = parent;
238 ngx_http_rewrite_loc_conf_t *conf = child;
242 ngx_conf_merge_value(conf->log, prev->log, 0);
243 ngx_conf_merge_value(conf->uninitialized_variable_warn,
244 prev->uninitialized_variable_warn, 1);
245 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10);
247 if (conf->codes == NULL) {
251 if (conf->codes == prev->codes) {
255 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t));
257 return NGX_CONF_ERROR;
260 *code = (uintptr_t) NULL;
267 ngx_http_rewrite_init(ngx_conf_t *cf)
269 ngx_http_handler_pt *h;
270 ngx_http_core_main_conf_t *cmcf;
272 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
274 h = ngx_array_push(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers);
279 *h = ngx_http_rewrite_handler;
281 h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
286 *h = ngx_http_rewrite_handler;
293 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
295 ngx_http_rewrite_loc_conf_t *lcf = conf;
297 ngx_str_t *value, err;
300 ngx_http_script_code_pt *code;
301 ngx_http_script_compile_t sc;
302 ngx_http_script_regex_code_t *regex;
303 ngx_http_script_regex_end_code_t *regex_end;
304 u_char errstr[NGX_MAX_CONF_ERRSTR];
306 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
307 sizeof(ngx_http_script_regex_code_t));
309 return NGX_CONF_ERROR;
312 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
314 value = cf->args->elts;
316 err.len = NGX_MAX_CONF_ERRSTR;
319 /* TODO: NGX_REGEX_CASELESS */
321 regex->regex = ngx_regex_compile(&value[1], 0, cf->pool, &err);
323 if (regex->regex == NULL) {
324 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
325 return NGX_CONF_ERROR;
328 regex->code = ngx_http_script_regex_start_code;
330 regex->name = value[1];
332 if (value[2].data[value[2].len - 1] == '?') {
334 /* the last "?" drops the original arguments */
343 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0) {
344 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
349 if (ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0) {
350 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
355 if (cf->args->nelts == 4) {
356 if (ngx_strcmp(value[3].data, "last") == 0) {
359 } else if (ngx_strcmp(value[3].data, "break") == 0) {
360 regex->break_cycle = 1;
363 } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
364 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
368 } else if (ngx_strcmp(value[3].data, "permanent") == 0) {
369 regex->status = NGX_HTTP_MOVED_PERMANENTLY;
374 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
375 "invalid parameter \"%V\"", &value[3]);
376 return NGX_CONF_ERROR;
380 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
383 sc.source = &value[2];
384 sc.lengths = ®ex->lengths;
385 sc.values = &lcf->codes;
386 sc.variables = ngx_http_script_variables_count(&value[2]);
388 sc.complete_lengths = 1;
389 sc.compile_args = !regex->redirect;
391 if (ngx_http_script_compile(&sc) != NGX_OK) {
392 return NGX_CONF_ERROR;
397 regex->ncaptures = sc.ncaptures;
398 regex->size = sc.size;
399 regex->args = sc.args;
401 if (sc.variables == 0 && !sc.dup_capture) {
402 regex->lengths = NULL;
405 n = ngx_regex_capture_count(regex->regex);
408 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
409 ngx_regex_capture_count_n " failed for "
410 "pattern \"%V\"", &value[1]);
411 return NGX_CONF_ERROR;
414 if (regex->ncaptures > (ngx_uint_t) n) {
415 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
416 "pattern \"%V\" has less captures "
417 "than referrenced in substitution \"%V\"",
418 &value[1], &value[2]);
419 return NGX_CONF_ERROR;
422 if (regex->ncaptures < (ngx_uint_t) n) {
423 regex->ncaptures = (ngx_uint_t) n;
426 if (regex->ncaptures) {
427 regex->ncaptures = (regex->ncaptures + 1) * 3;
430 regex_end = ngx_http_script_add_code(lcf->codes,
431 sizeof(ngx_http_script_regex_end_code_t),
433 if (regex_end == NULL) {
434 return NGX_CONF_ERROR;
437 regex_end->code = ngx_http_script_regex_end_code;
438 regex_end->uri = regex->uri;
439 regex_end->args = regex->args;
440 regex_end->add_args = regex->add_args;
441 regex_end->redirect = regex->redirect;
444 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex);
446 return NGX_CONF_ERROR;
449 *code = (uintptr_t) NULL;
452 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
460 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
462 ngx_http_rewrite_loc_conf_t *lcf = conf;
465 ngx_http_script_return_code_t *ret;
467 ret = ngx_http_script_start_code(cf->pool, &lcf->codes,
468 sizeof(ngx_http_script_return_code_t));
470 return NGX_CONF_ERROR;
473 value = cf->args->elts;
475 ret->code = ngx_http_script_return_code;
476 ret->null = (uintptr_t) NULL;
478 ret->status = ngx_atoi(value[1].data, value[1].len);
480 if (ret->status == (uintptr_t) NGX_ERROR) {
481 return NGX_CONF_ERROR;
489 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
491 ngx_http_rewrite_loc_conf_t *lcf = conf;
493 ngx_http_script_code_pt *code;
495 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t));
497 return NGX_CONF_ERROR;
500 *code = ngx_http_script_break_code;
507 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
509 ngx_http_rewrite_loc_conf_t *lcf = conf;
516 ngx_http_module_t *module;
517 ngx_http_conf_ctx_t *ctx, *pctx;
518 ngx_http_core_loc_conf_t *clcf, *pclcf;
519 ngx_http_script_if_code_t *if_code;
520 ngx_http_rewrite_loc_conf_t *nlcf;
522 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
524 return NGX_CONF_ERROR;
528 ctx->main_conf = pctx->main_conf;
529 ctx->srv_conf = pctx->srv_conf;
531 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
532 if (ctx->loc_conf == NULL) {
533 return NGX_CONF_ERROR;
536 for (i = 0; ngx_modules[i]; i++) {
537 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
541 module = ngx_modules[i]->ctx;
543 if (module->create_loc_conf) {
545 mconf = module->create_loc_conf(cf);
547 return NGX_CONF_ERROR;
550 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
554 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
556 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
557 clcf->loc_conf = ctx->loc_conf;
558 clcf->name = pclcf->name;
561 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
562 return NGX_CONF_ERROR;
565 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) {
566 return NGX_CONF_ERROR;
569 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t));
570 if (if_code == NULL) {
574 if_code->code = ngx_http_script_if_code;
576 elts = lcf->codes->elts;
579 /* the inner directives must be compiled to the same code array */
581 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index];
582 nlcf->codes = lcf->codes;
588 if (pclcf->name.len == 0) {
589 if_code->loc_conf = NULL;
590 cf->cmd_type = NGX_HTTP_SIF_CONF;
593 if_code->loc_conf = ctx->loc_conf;
594 cf->cmd_type = NGX_HTTP_LIF_CONF;
597 rv = ngx_conf_parse(cf, NULL);
601 if (rv != NGX_CONF_OK) {
606 if (elts != lcf->codes->elts) {
607 if_code = (ngx_http_script_if_code_t *)
608 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts));
611 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
612 - (u_char *) if_code;
614 /* the code array belong to parent block */
623 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf)
627 ngx_str_t *value, err;
628 ngx_uint_t cur, last, n;
629 ngx_http_script_code_pt *code;
630 ngx_http_script_file_code_t *fop;
631 ngx_http_script_regex_code_t *regex;
632 u_char errstr[NGX_MAX_CONF_ERRSTR];
634 value = cf->args->elts;
635 last = cf->args->nelts - 1;
637 if (value[1].len < 1 || value[1].data[0] != '(') {
638 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
639 "invalid condition \"%V\"", &value[1]);
640 return NGX_CONF_ERROR;
643 if (value[1].len == 1) {
652 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') {
653 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
654 "invalid condition \"%V\"", &value[last]);
655 return NGX_CONF_ERROR;
658 if (value[last].len == 1) {
663 value[last].data[value[last].len] = '\0';
666 len = value[cur].len;
669 if (len > 1 && p[0] == '$') {
671 if (cur != last && cur + 2 != last) {
672 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
673 "invalid condition \"%V\"", &value[cur]);
674 return NGX_CONF_ERROR;
677 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) {
678 return NGX_CONF_ERROR;
687 len = value[cur].len;
690 if (len == 1 && p[0] == '=') {
692 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
693 return NGX_CONF_ERROR;
696 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
699 return NGX_CONF_ERROR;
702 *code = ngx_http_script_equal_code;
707 if (len == 2 && p[0] == '!' && p[1] == '=') {
709 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
710 return NGX_CONF_ERROR;
713 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
716 return NGX_CONF_ERROR;
719 *code = ngx_http_script_not_equal_code;
723 if ((len == 1 && p[0] == '~')
724 || (len == 2 && p[0] == '~' && p[1] == '*')
725 || (len == 2 && p[0] == '!' && p[1] == '~')
726 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*'))
728 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
729 sizeof(ngx_http_script_regex_code_t));
731 return NGX_CONF_ERROR;
734 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
736 err.len = NGX_MAX_CONF_ERRSTR;
739 regex->regex = ngx_regex_compile(&value[last],
740 (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0,
743 if (regex->regex == NULL) {
744 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
745 return NGX_CONF_ERROR;
748 regex->code = ngx_http_script_regex_start_code;
749 regex->next = sizeof(ngx_http_script_regex_code_t);
752 regex->negative_test = 1;
754 regex->name = value[last];
756 n = ngx_regex_capture_count(regex->regex);
759 regex->ncaptures = (n + 1) * 3;
765 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
766 "unexpected \"%V\" in condition", &value[cur]);
767 return NGX_CONF_ERROR;
769 } else if ((len == 2 && p[0] == '-')
770 || (len == 3 && p[0] == '!' && p[1] == '-'))
772 if (cur + 1 != last) {
773 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
774 "invalid condition \"%V\"", &value[cur]);
775 return NGX_CONF_ERROR;
778 value[last].data[value[last].len] = '\0';
781 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
782 return NGX_CONF_ERROR;
785 fop = ngx_http_script_start_code(cf->pool, &lcf->codes,
786 sizeof(ngx_http_script_file_code_t));
788 return NGX_CONF_ERROR;
791 fop->code = ngx_http_script_file_code;
794 fop->op = ngx_http_script_file_plain;
799 fop->op = ngx_http_script_file_dir;
804 fop->op = ngx_http_script_file_exists;
809 fop->op = ngx_http_script_file_exec;
815 fop->op = ngx_http_script_file_not_plain;
820 fop->op = ngx_http_script_file_not_dir;
825 fop->op = ngx_http_script_file_not_exists;
830 fop->op = ngx_http_script_file_not_exec;
835 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
836 "invalid condition \"%V\"", &value[cur]);
837 return NGX_CONF_ERROR;
840 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
841 "invalid condition \"%V\"", &value[cur]);
843 return NGX_CONF_ERROR;
848 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
852 ngx_http_script_var_code_t *var_code;
857 index = ngx_http_get_variable_index(cf, value);
859 if (index == NGX_ERROR) {
860 return NGX_CONF_ERROR;
863 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes,
864 sizeof(ngx_http_script_var_code_t));
865 if (var_code == NULL) {
866 return NGX_CONF_ERROR;
869 var_code->code = ngx_http_script_var_code;
870 var_code->index = index;
877 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
879 ngx_http_rewrite_loc_conf_t *lcf = conf;
883 ngx_http_variable_t *v;
884 ngx_http_script_var_code_t *vcode;
885 ngx_http_script_var_handler_code_t *vhcode;
887 value = cf->args->elts;
889 if (value[1].data[0] != '$') {
890 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
891 "invalid variable name \"%V\"", &value[1]);
892 return NGX_CONF_ERROR;
898 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
900 return NGX_CONF_ERROR;
903 index = ngx_http_get_variable_index(cf, &value[1]);
904 if (index == NGX_ERROR) {
905 return NGX_CONF_ERROR;
908 if (v->get_handler == NULL
909 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0
910 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0
911 && ngx_strncasecmp(value[1].data, (u_char *) "upstream_http_", 14) != 0)
913 v->get_handler = ngx_http_rewrite_var;
917 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) {
918 return NGX_CONF_ERROR;
921 if (v->set_handler) {
922 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
923 sizeof(ngx_http_script_var_handler_code_t));
924 if (vhcode == NULL) {
925 return NGX_CONF_ERROR;
928 vhcode->code = ngx_http_script_var_set_handler_code;
929 vhcode->handler = v->set_handler;
930 vhcode->data = v->data;
935 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
936 sizeof(ngx_http_script_var_code_t));
938 return NGX_CONF_ERROR;
941 vcode->code = ngx_http_script_set_var_code;
942 vcode->index = (uintptr_t) index;
949 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
953 ngx_http_script_compile_t sc;
954 ngx_http_script_value_code_t *val;
955 ngx_http_script_complex_value_code_t *complex;
957 n = ngx_http_script_variables_count(value);
960 val = ngx_http_script_start_code(cf->pool, &lcf->codes,
961 sizeof(ngx_http_script_value_code_t));
963 return NGX_CONF_ERROR;
966 n = ngx_atoi(value->data, value->len);
968 if (n == NGX_ERROR) {
972 val->code = ngx_http_script_value_code;
973 val->value = (uintptr_t) n;
974 val->text_len = (uintptr_t) value->len;
975 val->text_data = (uintptr_t) value->data;
980 complex = ngx_http_script_start_code(cf->pool, &lcf->codes,
981 sizeof(ngx_http_script_complex_value_code_t));
982 if (complex == NULL) {
983 return NGX_CONF_ERROR;
986 complex->code = ngx_http_script_complex_value_code;
987 complex->lengths = NULL;
989 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
993 sc.lengths = &complex->lengths;
994 sc.values = &lcf->codes;
996 sc.complete_lengths = 1;
998 if (ngx_http_script_compile(&sc) != NGX_OK) {
999 return NGX_CONF_ERROR;