3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 ngx_array_t *codes; /* uintptr_t */
16 ngx_uint_t stack_size;
19 ngx_flag_t uninitialized_variable_warn;
20 } ngx_http_rewrite_loc_conf_t;
23 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf);
24 static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf,
25 void *parent, void *child);
26 static ngx_int_t ngx_http_rewrite_init(ngx_conf_t *cf);
27 static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
28 static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd,
30 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd,
32 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd,
34 static char * ngx_http_rewrite_if_condition(ngx_conf_t *cf,
35 ngx_http_rewrite_loc_conf_t *lcf);
36 static char *ngx_http_rewrite_variable(ngx_conf_t *cf,
37 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
38 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd,
40 static char * ngx_http_rewrite_value(ngx_conf_t *cf,
41 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
44 static ngx_command_t ngx_http_rewrite_commands[] = {
46 { ngx_string("rewrite"),
47 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
50 NGX_HTTP_LOC_CONF_OFFSET,
54 { ngx_string("return"),
55 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
57 ngx_http_rewrite_return,
58 NGX_HTTP_LOC_CONF_OFFSET,
62 { ngx_string("break"),
63 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
65 ngx_http_rewrite_break,
66 NGX_HTTP_LOC_CONF_OFFSET,
71 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
73 NGX_HTTP_LOC_CONF_OFFSET,
78 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
81 NGX_HTTP_LOC_CONF_OFFSET,
85 { ngx_string("rewrite_log"),
86 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
87 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
88 ngx_conf_set_flag_slot,
89 NGX_HTTP_LOC_CONF_OFFSET,
90 offsetof(ngx_http_rewrite_loc_conf_t, log),
93 { ngx_string("uninitialized_variable_warn"),
94 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF
95 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
96 ngx_conf_set_flag_slot,
97 NGX_HTTP_LOC_CONF_OFFSET,
98 offsetof(ngx_http_rewrite_loc_conf_t, uninitialized_variable_warn),
105 static ngx_http_module_t ngx_http_rewrite_module_ctx = {
106 NULL, /* preconfiguration */
107 ngx_http_rewrite_init, /* postconfiguration */
109 NULL, /* create main configuration */
110 NULL, /* init main configuration */
112 NULL, /* create server configuration */
113 NULL, /* merge server configuration */
115 ngx_http_rewrite_create_loc_conf, /* create location configration */
116 ngx_http_rewrite_merge_loc_conf /* merge location configration */
120 ngx_module_t ngx_http_rewrite_module = {
122 &ngx_http_rewrite_module_ctx, /* module context */
123 ngx_http_rewrite_commands, /* module directives */
124 NGX_HTTP_MODULE, /* module type */
125 NULL, /* init master */
126 NULL, /* init module */
127 NULL, /* init process */
128 NULL, /* init thread */
129 NULL, /* exit thread */
130 NULL, /* exit process */
131 NULL, /* exit master */
132 NGX_MODULE_V1_PADDING
137 ngx_http_rewrite_handler(ngx_http_request_t *r)
139 ngx_http_script_code_pt code;
140 ngx_http_script_engine_t *e;
141 ngx_http_rewrite_loc_conf_t *rlcf;
143 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
145 if (rlcf->codes == NULL) {
149 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t));
151 return NGX_HTTP_INTERNAL_SERVER_ERROR;
154 e->sp = ngx_pcalloc(r->pool,
155 rlcf->stack_size * sizeof(ngx_http_variable_value_t));
157 return NGX_HTTP_INTERNAL_SERVER_ERROR;
160 if (rlcf->captures) {
161 e->captures = ngx_palloc(r->pool, rlcf->captures * sizeof(int));
162 if (e->captures == NULL) {
163 return NGX_HTTP_INTERNAL_SERVER_ERROR;
170 e->ip = rlcf->codes->elts;
174 e->status = NGX_DECLINED;
176 while (*(uintptr_t *) e->ip) {
177 code = *(ngx_http_script_code_pt *) e->ip;
181 if (e->status == NGX_DECLINED) {
185 if (r->err_status == 0) {
189 return r->err_status;
194 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v,
197 ngx_http_variable_t *var;
198 ngx_http_core_main_conf_t *cmcf;
199 ngx_http_rewrite_loc_conf_t *rlcf;
201 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
203 if (rlcf->uninitialized_variable_warn == 0) {
204 *v = ngx_http_variable_null_value;
208 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
210 var = cmcf->variables.elts;
213 * the ngx_http_rewrite_module sets variables directly in r->variables,
214 * and they should be handled by ngx_http_get_indexed_variable(),
215 * so the handler is called only if the variable is not initialized
218 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
219 "using uninitialized \"%V\" variable", &var[data].name);
221 *v = ngx_http_variable_null_value;
228 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
230 ngx_http_rewrite_loc_conf_t *conf;
232 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t));
234 return NGX_CONF_ERROR;
237 conf->stack_size = NGX_CONF_UNSET_UINT;
238 conf->log = NGX_CONF_UNSET;
239 conf->uninitialized_variable_warn = NGX_CONF_UNSET;
246 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
248 ngx_http_rewrite_loc_conf_t *prev = parent;
249 ngx_http_rewrite_loc_conf_t *conf = child;
253 ngx_conf_merge_value(conf->log, prev->log, 0);
254 ngx_conf_merge_value(conf->uninitialized_variable_warn,
255 prev->uninitialized_variable_warn, 1);
256 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10);
258 if (conf->codes == NULL) {
262 if (conf->codes == prev->codes) {
266 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t));
268 return NGX_CONF_ERROR;
271 *code = (uintptr_t) NULL;
278 ngx_http_rewrite_init(ngx_conf_t *cf)
280 ngx_http_handler_pt *h;
281 ngx_http_core_main_conf_t *cmcf;
283 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
285 h = ngx_array_push(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers);
290 *h = ngx_http_rewrite_handler;
292 h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
297 *h = ngx_http_rewrite_handler;
304 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
306 ngx_http_rewrite_loc_conf_t *lcf = conf;
308 ngx_str_t *value, err;
311 ngx_http_script_code_pt *code;
312 ngx_http_script_compile_t sc;
313 ngx_http_script_regex_code_t *regex;
314 ngx_http_script_regex_end_code_t *regex_end;
315 u_char errstr[NGX_MAX_CONF_ERRSTR];
317 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
318 sizeof(ngx_http_script_regex_code_t));
320 return NGX_CONF_ERROR;
323 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
325 value = cf->args->elts;
327 err.len = NGX_MAX_CONF_ERRSTR;
330 /* TODO: NGX_REGEX_CASELESS */
332 regex->regex = ngx_regex_compile(&value[1], 0, cf->pool, &err);
334 if (regex->regex == NULL) {
335 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
336 return NGX_CONF_ERROR;
339 regex->code = ngx_http_script_regex_start_code;
341 regex->name = value[1];
343 if (value[2].data[value[2].len - 1] == '?') {
345 /* the last "?" drops the original arguments */
354 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0) {
355 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
360 if (ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0) {
361 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
366 if (cf->args->nelts == 4) {
367 if (ngx_strcmp(value[3].data, "last") == 0) {
370 } else if (ngx_strcmp(value[3].data, "break") == 0) {
371 regex->break_cycle = 1;
374 } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
375 regex->status = NGX_HTTP_MOVED_TEMPORARILY;
379 } else if (ngx_strcmp(value[3].data, "permanent") == 0) {
380 regex->status = NGX_HTTP_MOVED_PERMANENTLY;
385 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
386 "invalid parameter \"%V\"", &value[3]);
387 return NGX_CONF_ERROR;
391 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
394 sc.source = &value[2];
395 sc.lengths = ®ex->lengths;
396 sc.values = &lcf->codes;
397 sc.variables = ngx_http_script_variables_count(&value[2]);
399 sc.complete_lengths = 1;
400 sc.compile_args = !regex->redirect;
402 if (ngx_http_script_compile(&sc) != NGX_OK) {
403 return NGX_CONF_ERROR;
408 regex->ncaptures = sc.ncaptures;
409 regex->size = sc.size;
410 regex->args = sc.args;
412 if (sc.variables == 0 && !sc.dup_capture) {
413 regex->lengths = NULL;
416 n = ngx_regex_capture_count(regex->regex);
419 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
420 ngx_regex_capture_count_n " failed for "
421 "pattern \"%V\"", &value[1]);
422 return NGX_CONF_ERROR;
425 if (regex->ncaptures > (ngx_uint_t) n) {
426 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
427 "pattern \"%V\" has less captures "
428 "than referrenced in substitution \"%V\"",
429 &value[1], &value[2]);
430 return NGX_CONF_ERROR;
433 if (regex->ncaptures < (ngx_uint_t) n) {
434 regex->ncaptures = (ngx_uint_t) n;
437 if (regex->ncaptures) {
438 regex->ncaptures = (regex->ncaptures + 1) * 3;
440 if (lcf->captures < regex->ncaptures) {
441 lcf->captures = regex->ncaptures;
445 regex_end = ngx_http_script_add_code(lcf->codes,
446 sizeof(ngx_http_script_regex_end_code_t),
448 if (regex_end == NULL) {
449 return NGX_CONF_ERROR;
452 regex_end->code = ngx_http_script_regex_end_code;
453 regex_end->uri = regex->uri;
454 regex_end->args = regex->args;
455 regex_end->add_args = regex->add_args;
456 regex_end->redirect = regex->redirect;
459 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex);
461 return NGX_CONF_ERROR;
464 *code = (uintptr_t) NULL;
467 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
475 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
477 ngx_http_rewrite_loc_conf_t *lcf = conf;
480 ngx_http_script_return_code_t *ret;
482 ret = ngx_http_script_start_code(cf->pool, &lcf->codes,
483 sizeof(ngx_http_script_return_code_t));
485 return NGX_CONF_ERROR;
488 value = cf->args->elts;
490 ret->code = ngx_http_script_return_code;
491 ret->null = (uintptr_t) NULL;
493 ret->status = ngx_atoi(value[1].data, value[1].len);
495 if (ret->status == (uintptr_t) NGX_ERROR) {
496 return NGX_CONF_ERROR;
504 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
506 ngx_http_rewrite_loc_conf_t *lcf = conf;
508 ngx_http_script_code_pt *code;
510 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t));
512 return NGX_CONF_ERROR;
515 *code = ngx_http_script_break_code;
522 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
524 ngx_http_rewrite_loc_conf_t *lcf = conf;
531 ngx_http_module_t *module;
532 ngx_http_conf_ctx_t *ctx, *pctx;
533 ngx_http_core_loc_conf_t *clcf, *pclcf;
534 ngx_http_script_if_code_t *if_code;
535 ngx_http_rewrite_loc_conf_t *nlcf;
537 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
539 return NGX_CONF_ERROR;
543 ctx->main_conf = pctx->main_conf;
544 ctx->srv_conf = pctx->srv_conf;
546 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
547 if (ctx->loc_conf == NULL) {
548 return NGX_CONF_ERROR;
551 for (i = 0; ngx_modules[i]; i++) {
552 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
556 module = ngx_modules[i]->ctx;
558 if (module->create_loc_conf) {
560 mconf = module->create_loc_conf(cf);
562 return NGX_CONF_ERROR;
565 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
569 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
571 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
572 clcf->loc_conf = ctx->loc_conf;
573 clcf->name = pclcf->name;
576 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
577 return NGX_CONF_ERROR;
580 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) {
581 return NGX_CONF_ERROR;
584 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t));
585 if (if_code == NULL) {
589 if_code->code = ngx_http_script_if_code;
591 elts = lcf->codes->elts;
594 /* the inner directives must be compiled to the same code array */
596 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index];
597 nlcf->codes = lcf->codes;
603 if (pclcf->name.len == 0) {
604 if_code->loc_conf = NULL;
605 cf->cmd_type = NGX_HTTP_SIF_CONF;
608 if_code->loc_conf = ctx->loc_conf;
609 cf->cmd_type = NGX_HTTP_LIF_CONF;
612 rv = ngx_conf_parse(cf, NULL);
616 if (rv != NGX_CONF_OK) {
621 if (lcf->captures < nlcf->captures) {
622 lcf->captures = nlcf->captures;
626 if (elts != lcf->codes->elts) {
627 if_code = (ngx_http_script_if_code_t *)
628 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts));
631 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
632 - (u_char *) if_code;
634 /* the code array belong to parent block */
643 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf)
647 ngx_str_t *value, err;
648 ngx_uint_t cur, last, n;
649 ngx_http_script_code_pt *code;
650 ngx_http_script_file_code_t *fop;
651 ngx_http_script_regex_code_t *regex;
652 u_char errstr[NGX_MAX_CONF_ERRSTR];
654 value = cf->args->elts;
655 last = cf->args->nelts - 1;
657 if (value[1].len < 1 || value[1].data[0] != '(') {
658 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
659 "invalid condition \"%V\"", &value[1]);
660 return NGX_CONF_ERROR;
663 if (value[1].len == 1) {
672 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') {
673 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
674 "invalid condition \"%V\"", &value[last]);
675 return NGX_CONF_ERROR;
678 if (value[last].len == 1) {
683 value[last].data[value[last].len] = '\0';
686 len = value[cur].len;
689 if (len > 1 && p[0] == '$') {
691 if (cur != last && cur + 2 != last) {
692 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
693 "invalid condition \"%V\"", &value[cur]);
694 return NGX_CONF_ERROR;
697 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) {
698 return NGX_CONF_ERROR;
707 len = value[cur].len;
710 if (len == 1 && p[0] == '=') {
712 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
713 return NGX_CONF_ERROR;
716 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
719 return NGX_CONF_ERROR;
722 *code = ngx_http_script_equal_code;
727 if (len == 2 && p[0] == '!' && p[1] == '=') {
729 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
730 return NGX_CONF_ERROR;
733 code = ngx_http_script_start_code(cf->pool, &lcf->codes,
736 return NGX_CONF_ERROR;
739 *code = ngx_http_script_not_equal_code;
743 if ((len == 1 && p[0] == '~')
744 || (len == 2 && p[0] == '~' && p[1] == '*')
745 || (len == 2 && p[0] == '!' && p[1] == '~')
746 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*'))
748 regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
749 sizeof(ngx_http_script_regex_code_t));
751 return NGX_CONF_ERROR;
754 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
756 err.len = NGX_MAX_CONF_ERRSTR;
759 regex->regex = ngx_regex_compile(&value[last],
760 (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0,
763 if (regex->regex == NULL) {
764 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
765 return NGX_CONF_ERROR;
768 regex->code = ngx_http_script_regex_start_code;
769 regex->next = sizeof(ngx_http_script_regex_code_t);
772 regex->negative_test = 1;
774 regex->name = value[last];
776 n = ngx_regex_capture_count(regex->regex);
779 regex->ncaptures = (n + 1) * 3;
781 if (lcf->captures < regex->ncaptures) {
782 lcf->captures = regex->ncaptures;
789 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
790 "unexpected \"%V\" in condition", &value[cur]);
791 return NGX_CONF_ERROR;
793 } else if ((len == 2 && p[0] == '-')
794 || (len == 3 && p[0] == '!' && p[1] == '-'))
796 if (cur + 1 != last) {
797 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
798 "invalid condition \"%V\"", &value[cur]);
799 return NGX_CONF_ERROR;
802 value[last].data[value[last].len] = '\0';
805 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
806 return NGX_CONF_ERROR;
809 fop = ngx_http_script_start_code(cf->pool, &lcf->codes,
810 sizeof(ngx_http_script_file_code_t));
812 return NGX_CONF_ERROR;
815 fop->code = ngx_http_script_file_code;
818 fop->op = ngx_http_script_file_plain;
823 fop->op = ngx_http_script_file_dir;
828 fop->op = ngx_http_script_file_exists;
833 fop->op = ngx_http_script_file_exec;
839 fop->op = ngx_http_script_file_not_plain;
844 fop->op = ngx_http_script_file_not_dir;
849 fop->op = ngx_http_script_file_not_exists;
854 fop->op = ngx_http_script_file_not_exec;
859 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
860 "invalid condition \"%V\"", &value[cur]);
861 return NGX_CONF_ERROR;
864 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
865 "invalid condition \"%V\"", &value[cur]);
867 return NGX_CONF_ERROR;
872 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
876 ngx_http_script_var_code_t *var_code;
881 index = ngx_http_get_variable_index(cf, value);
883 if (index == NGX_ERROR) {
884 return NGX_CONF_ERROR;
887 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes,
888 sizeof(ngx_http_script_var_code_t));
889 if (var_code == NULL) {
890 return NGX_CONF_ERROR;
893 var_code->code = ngx_http_script_var_code;
894 var_code->index = index;
901 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
903 ngx_http_rewrite_loc_conf_t *lcf = conf;
907 ngx_http_variable_t *v;
908 ngx_http_script_var_code_t *vcode;
909 ngx_http_script_var_handler_code_t *vhcode;
911 value = cf->args->elts;
913 if (value[1].data[0] != '$') {
914 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
915 "invalid variable name \"%V\"", &value[1]);
916 return NGX_CONF_ERROR;
922 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
924 return NGX_CONF_ERROR;
927 index = ngx_http_get_variable_index(cf, &value[1]);
928 if (index == NGX_ERROR) {
929 return NGX_CONF_ERROR;
932 if (v->get_handler == NULL
933 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0
934 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0
935 && ngx_strncasecmp(value[1].data, (u_char *) "upstream_http_", 14) != 0)
937 v->get_handler = ngx_http_rewrite_var;
941 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) {
942 return NGX_CONF_ERROR;
945 if (v->set_handler) {
946 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
947 sizeof(ngx_http_script_var_handler_code_t));
948 if (vhcode == NULL) {
949 return NGX_CONF_ERROR;
952 vhcode->code = ngx_http_script_var_set_handler_code;
953 vhcode->handler = v->set_handler;
954 vhcode->data = v->data;
959 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
960 sizeof(ngx_http_script_var_code_t));
962 return NGX_CONF_ERROR;
965 vcode->code = ngx_http_script_set_var_code;
966 vcode->index = (uintptr_t) index;
973 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
977 ngx_http_script_compile_t sc;
978 ngx_http_script_value_code_t *val;
979 ngx_http_script_complex_value_code_t *complex;
981 n = ngx_http_script_variables_count(value);
984 val = ngx_http_script_start_code(cf->pool, &lcf->codes,
985 sizeof(ngx_http_script_value_code_t));
987 return NGX_CONF_ERROR;
990 n = ngx_atoi(value->data, value->len);
992 if (n == NGX_ERROR) {
996 val->code = ngx_http_script_value_code;
997 val->value = (uintptr_t) n;
998 val->text_len = (uintptr_t) value->len;
999 val->text_data = (uintptr_t) value->data;
1004 complex = ngx_http_script_start_code(cf->pool, &lcf->codes,
1005 sizeof(ngx_http_script_complex_value_code_t));
1006 if (complex == NULL) {
1007 return NGX_CONF_ERROR;
1010 complex->code = ngx_http_script_complex_value_code;
1011 complex->lengths = NULL;
1013 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1017 sc.lengths = &complex->lengths;
1018 sc.values = &lcf->codes;
1020 sc.complete_lengths = 1;
1022 if (ngx_http_script_compile(&sc) != NGX_OK) {
1023 return NGX_CONF_ERROR;