upstream nginx-0.7.40
[nginx.git] / nginx / src / http / modules / ngx_http_rewrite_module.c
1
2 /*
3  * Copyright (C) Igor Sysoev
4  */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10
11
12 typedef struct {
13     ngx_array_t  *codes;        /* uintptr_t */
14
15     ngx_uint_t    stack_size;
16
17     ngx_flag_t    log;
18     ngx_flag_t    uninitialized_variable_warn;
19 } ngx_http_rewrite_loc_conf_t;
20
21
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,
28     void *conf);
29 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd,
30     void *conf);
31 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd,
32     void *conf);
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,
38     void *conf);
39 static char * ngx_http_rewrite_value(ngx_conf_t *cf,
40     ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value);
41
42
43 static ngx_command_t  ngx_http_rewrite_commands[] = {
44
45     { ngx_string("rewrite"),
46       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
47                        |NGX_CONF_TAKE23,
48       ngx_http_rewrite,
49       NGX_HTTP_LOC_CONF_OFFSET,
50       0,
51       NULL },
52
53     { ngx_string("return"),
54       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
55                        |NGX_CONF_TAKE1,
56       ngx_http_rewrite_return,
57       NGX_HTTP_LOC_CONF_OFFSET,
58       0,
59       NULL },
60
61     { ngx_string("break"),
62       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
63                        |NGX_CONF_NOARGS,
64       ngx_http_rewrite_break,
65       NGX_HTTP_LOC_CONF_OFFSET,
66       0,
67       NULL },
68
69     { ngx_string("if"),
70       NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
71       ngx_http_rewrite_if,
72       NGX_HTTP_LOC_CONF_OFFSET,
73       0,
74       NULL },
75
76     { ngx_string("set"),
77       NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
78                        |NGX_CONF_TAKE2,
79       ngx_http_rewrite_set,
80       NGX_HTTP_LOC_CONF_OFFSET,
81       0,
82       NULL },
83
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),
90       NULL },
91
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),
98       NULL },
99
100       ngx_null_command
101 };
102
103
104 static ngx_http_module_t  ngx_http_rewrite_module_ctx = {
105     NULL,                                  /* preconfiguration */
106     ngx_http_rewrite_init,                 /* postconfiguration */
107
108     NULL,                                  /* create main configuration */
109     NULL,                                  /* init main configuration */
110
111     NULL,                                  /* create server configuration */
112     NULL,                                  /* merge server configuration */
113
114     ngx_http_rewrite_create_loc_conf,      /* create location configration */
115     ngx_http_rewrite_merge_loc_conf        /* merge location configration */
116 };
117
118
119 ngx_module_t  ngx_http_rewrite_module = {
120     NGX_MODULE_V1,
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
132 };
133
134
135 static ngx_int_t
136 ngx_http_rewrite_handler(ngx_http_request_t *r)
137 {
138     ngx_http_script_code_pt       code;
139     ngx_http_script_engine_t     *e;
140     ngx_http_rewrite_loc_conf_t  *rlcf;
141
142     rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
143
144     if (rlcf->codes == NULL) {
145         return NGX_DECLINED;
146     }
147
148     e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t));
149     if (e == NULL) {
150         return NGX_HTTP_INTERNAL_SERVER_ERROR;
151     }
152
153     e->sp = ngx_pcalloc(r->pool,
154                         rlcf->stack_size * sizeof(ngx_http_variable_value_t));
155     if (e->sp == NULL) {
156         return NGX_HTTP_INTERNAL_SERVER_ERROR;
157     }
158
159     e->ip = rlcf->codes->elts;
160     e->request = r;
161     e->quote = 1;
162     e->log = rlcf->log;
163     e->status = NGX_DECLINED;
164
165     while (*(uintptr_t *) e->ip) {
166         code = *(ngx_http_script_code_pt *) e->ip;
167         code(e);
168     }
169
170     if (e->status == NGX_DECLINED) {
171         return NGX_DECLINED;
172     }
173
174     if (r->err_status == 0) {
175         return e->status;
176     }
177
178     return r->err_status;
179 }
180
181
182 static ngx_int_t
183 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v,
184     uintptr_t data)
185 {
186     ngx_http_variable_t          *var;
187     ngx_http_core_main_conf_t    *cmcf;
188     ngx_http_rewrite_loc_conf_t  *rlcf;
189
190     rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module);
191
192     if (rlcf->uninitialized_variable_warn == 0) {
193         *v = ngx_http_variable_null_value;
194         return NGX_OK;
195     }
196
197     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
198
199     var = cmcf->variables.elts;
200
201     /*
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
205      */
206
207     ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
208                   "using uninitialized \"%V\" variable", &var[data].name);
209
210     *v = ngx_http_variable_null_value;
211
212     return NGX_OK;
213 }
214
215
216 static void *
217 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
218 {
219     ngx_http_rewrite_loc_conf_t  *conf;
220
221     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t));
222     if (conf == NULL) {
223         return NGX_CONF_ERROR;
224     }
225
226     conf->stack_size = NGX_CONF_UNSET_UINT;
227     conf->log = NGX_CONF_UNSET;
228     conf->uninitialized_variable_warn = NGX_CONF_UNSET;
229
230     return conf;
231 }
232
233
234 static char *
235 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
236 {
237     ngx_http_rewrite_loc_conf_t *prev = parent;
238     ngx_http_rewrite_loc_conf_t *conf = child;
239
240     uintptr_t  *code;
241
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);
246
247     if (conf->codes == NULL) {
248         return NGX_CONF_OK;
249     }
250
251     if (conf->codes == prev->codes) {
252         return NGX_CONF_OK;
253     }
254
255     code = ngx_array_push_n(conf->codes, sizeof(uintptr_t));
256     if (code == NULL) {
257         return NGX_CONF_ERROR;
258     }
259
260     *code = (uintptr_t) NULL;
261
262     return NGX_CONF_OK;
263 }
264
265
266 static ngx_int_t
267 ngx_http_rewrite_init(ngx_conf_t *cf)
268 {
269     ngx_http_handler_pt        *h;
270     ngx_http_core_main_conf_t  *cmcf;
271
272     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
273
274     h = ngx_array_push(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers);
275     if (h == NULL) {
276         return NGX_ERROR;
277     }
278
279     *h = ngx_http_rewrite_handler;
280
281     h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
282     if (h == NULL) {
283         return NGX_ERROR;
284     }
285
286     *h = ngx_http_rewrite_handler;
287
288     return NGX_OK;
289 }
290
291
292 static char *
293 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
294 {
295     ngx_http_rewrite_loc_conf_t  *lcf = conf;
296
297     ngx_str_t                         *value, err;
298     ngx_int_t                          n;
299     ngx_uint_t                         last;
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];
305
306     regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
307                                        sizeof(ngx_http_script_regex_code_t));
308     if (regex == NULL) {
309         return NGX_CONF_ERROR;
310     }
311
312     ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
313
314     value = cf->args->elts;
315
316     err.len = NGX_MAX_CONF_ERRSTR;
317     err.data = errstr;
318
319     /* TODO: NGX_REGEX_CASELESS */
320
321     regex->regex = ngx_regex_compile(&value[1], 0, cf->pool, &err);
322
323     if (regex->regex == NULL) {
324         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
325         return NGX_CONF_ERROR;
326     }
327
328     regex->code = ngx_http_script_regex_start_code;
329     regex->uri = 1;
330     regex->name = value[1];
331
332     if (value[2].data[value[2].len - 1] == '?') {
333
334         /* the last "?" drops the original arguments */
335         value[2].len--;
336
337     } else {
338         regex->add_args = 1;
339     }
340
341     last = 0;
342
343     if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0) {
344         regex->status = NGX_HTTP_MOVED_TEMPORARILY;
345         regex->redirect = 1;
346         last = 1;
347     }
348
349     if (ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0) {
350         regex->status = NGX_HTTP_MOVED_TEMPORARILY;
351         regex->redirect = 1;
352         last = 1;
353     }
354
355     if (cf->args->nelts == 4) {
356         if (ngx_strcmp(value[3].data, "last") == 0) {
357             last = 1;
358
359         } else if (ngx_strcmp(value[3].data, "break") == 0) {
360             regex->break_cycle = 1;
361             last = 1;
362
363         } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
364             regex->status = NGX_HTTP_MOVED_TEMPORARILY;
365             regex->redirect = 1;
366             last = 1;
367
368         } else if (ngx_strcmp(value[3].data, "permanent") == 0) {
369             regex->status = NGX_HTTP_MOVED_PERMANENTLY;
370             regex->redirect = 1;
371             last = 1;
372
373         } else {
374             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
375                                "invalid parameter \"%V\"", &value[3]);
376             return NGX_CONF_ERROR;
377         }
378     }
379
380     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
381
382     sc.cf = cf;
383     sc.source = &value[2];
384     sc.lengths = &regex->lengths;
385     sc.values = &lcf->codes;
386     sc.variables = ngx_http_script_variables_count(&value[2]);
387     sc.main = regex;
388     sc.complete_lengths = 1;
389     sc.compile_args = !regex->redirect;
390
391     if (ngx_http_script_compile(&sc) != NGX_OK) {
392         return NGX_CONF_ERROR;
393     }
394
395     regex = sc.main;
396
397     regex->ncaptures = sc.ncaptures;
398     regex->size = sc.size;
399     regex->args = sc.args;
400
401     if (sc.variables == 0 && !sc.dup_capture) {
402         regex->lengths = NULL;
403     }
404
405     n = ngx_regex_capture_count(regex->regex);
406
407     if (n < 0) {
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;
412     }
413
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;
420     }
421
422     if (regex->ncaptures < (ngx_uint_t) n) {
423         regex->ncaptures = (ngx_uint_t) n;
424     }
425
426     if (regex->ncaptures) {
427         regex->ncaptures = (regex->ncaptures + 1) * 3;
428     }
429
430     regex_end = ngx_http_script_add_code(lcf->codes,
431                                       sizeof(ngx_http_script_regex_end_code_t),
432                                       &regex);
433     if (regex_end == NULL) {
434         return NGX_CONF_ERROR;
435     }
436
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;
442
443     if (last) {
444         code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), &regex);
445         if (code == NULL) {
446             return NGX_CONF_ERROR;
447         }
448
449         *code = (uintptr_t) NULL;
450     }
451
452     regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
453                                               - (u_char *) regex;
454
455     return NGX_CONF_OK;
456 }
457
458
459 static char *
460 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
461 {
462     ngx_http_rewrite_loc_conf_t  *lcf = conf;
463
464     ngx_str_t                      *value;
465     ngx_http_script_return_code_t  *ret;
466
467     ret = ngx_http_script_start_code(cf->pool, &lcf->codes,
468                                      sizeof(ngx_http_script_return_code_t));
469     if (ret == NULL) {
470         return NGX_CONF_ERROR;
471     }
472
473     value = cf->args->elts;
474
475     ret->code = ngx_http_script_return_code;
476     ret->null = (uintptr_t) NULL;
477
478     ret->status = ngx_atoi(value[1].data, value[1].len);
479
480     if (ret->status == (uintptr_t) NGX_ERROR) {
481         return NGX_CONF_ERROR;
482     }
483
484     return NGX_CONF_OK;
485 }
486
487
488 static char *
489 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
490 {
491     ngx_http_rewrite_loc_conf_t *lcf = conf;
492
493     ngx_http_script_code_pt  *code;
494
495     code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t));
496     if (code == NULL) {
497         return NGX_CONF_ERROR;
498     }
499
500     *code = ngx_http_script_break_code;
501
502     return NGX_CONF_OK;
503 }
504
505
506 static char *
507 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
508 {
509     ngx_http_rewrite_loc_conf_t  *lcf = conf;
510
511     void                         *mconf;
512     char                         *rv;
513     u_char                       *elts;
514     ngx_uint_t                    i;
515     ngx_conf_t                    save;
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;
521
522     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
523     if (ctx == NULL) {
524         return NGX_CONF_ERROR;
525     }
526
527     pctx = cf->ctx;
528     ctx->main_conf = pctx->main_conf;
529     ctx->srv_conf = pctx->srv_conf;
530
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;
534     }
535
536     for (i = 0; ngx_modules[i]; i++) {
537         if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
538             continue;
539         }
540
541         module = ngx_modules[i]->ctx;
542
543         if (module->create_loc_conf) {
544
545             mconf = module->create_loc_conf(cf);
546             if (mconf == NULL) {
547                  return NGX_CONF_ERROR;
548             }
549
550             ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
551         }
552     }
553
554     pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
555
556     clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
557     clcf->loc_conf = ctx->loc_conf;
558     clcf->name = pclcf->name;
559     clcf->noname = 1;
560
561     if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
562         return NGX_CONF_ERROR;
563     }
564
565     if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) {
566         return NGX_CONF_ERROR;
567     }
568
569     if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t));
570     if (if_code == NULL) {
571         return NULL;
572     }
573
574     if_code->code = ngx_http_script_if_code;
575
576     elts = lcf->codes->elts;
577
578
579     /* the inner directives must be compiled to the same code array */
580
581     nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index];
582     nlcf->codes = lcf->codes;
583
584
585     save = *cf;
586     cf->ctx = ctx;
587
588     if (pclcf->name.len == 0) {
589         if_code->loc_conf = NULL;
590         cf->cmd_type = NGX_HTTP_SIF_CONF;
591
592     } else {
593         if_code->loc_conf = ctx->loc_conf;
594         cf->cmd_type = NGX_HTTP_LIF_CONF;
595     }
596
597     rv = ngx_conf_parse(cf, NULL);
598
599     *cf = save;
600
601     if (rv != NGX_CONF_OK) {
602         return rv;
603     }
604
605
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));
609     }
610
611     if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts
612                                                 - (u_char *) if_code;
613
614     /* the code array belong to parent block */
615
616     nlcf->codes = NULL;
617
618     return NGX_CONF_OK;
619 }
620
621
622 static char *
623 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf)
624 {
625     u_char                        *p;
626     size_t                         len;
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];
633
634     value = cf->args->elts;
635     last = cf->args->nelts - 1;
636
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;
641     }
642
643     if (value[1].len == 1) {
644         cur = 2;
645
646     } else {
647         cur = 1;
648         value[1].len--;
649         value[1].data++;
650     }
651
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;
656     }
657
658     if (value[last].len == 1) {
659         last--;
660
661     } else {
662         value[last].len--;
663         value[last].data[value[last].len] = '\0';
664     }
665
666     len = value[cur].len;
667     p = value[cur].data;
668
669     if (len > 1 && p[0] == '$') {
670
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;
675         }
676
677         if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) {
678             return NGX_CONF_ERROR;
679         }
680
681         if (cur == last) {
682             return NGX_CONF_OK;
683         }
684
685         cur++;
686
687         len = value[cur].len;
688         p = value[cur].data;
689
690         if (len == 1 && p[0] == '=') {
691
692             if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
693                 return NGX_CONF_ERROR;
694             }
695
696             code = ngx_http_script_start_code(cf->pool, &lcf->codes,
697                                               sizeof(uintptr_t));
698             if (code == NULL) {
699                 return NGX_CONF_ERROR;
700             }
701
702             *code = ngx_http_script_equal_code;
703
704             return NGX_CONF_OK;
705         }
706
707         if (len == 2 && p[0] == '!' && p[1] == '=') {
708
709             if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
710                 return NGX_CONF_ERROR;
711             }
712
713             code = ngx_http_script_start_code(cf->pool, &lcf->codes,
714                                               sizeof(uintptr_t));
715             if (code == NULL) {
716                 return NGX_CONF_ERROR;
717             }
718
719             *code = ngx_http_script_not_equal_code;
720             return NGX_CONF_OK;
721         }
722
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] == '*'))
727         {
728             regex = ngx_http_script_start_code(cf->pool, &lcf->codes,
729                                          sizeof(ngx_http_script_regex_code_t));
730             if (regex == NULL) {
731                 return NGX_CONF_ERROR;
732             }
733
734             ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t));
735
736             err.len = NGX_MAX_CONF_ERRSTR;
737             err.data = errstr;
738
739             regex->regex = ngx_regex_compile(&value[last],
740                                   (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0,
741                                    cf->pool, &err);
742
743             if (regex->regex == NULL) {
744                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
745                 return NGX_CONF_ERROR;
746             }
747
748             regex->code = ngx_http_script_regex_start_code;
749             regex->next = sizeof(ngx_http_script_regex_code_t);
750             regex->test = 1;
751             if (p[0] == '!') {
752                 regex->negative_test = 1;
753             }
754             regex->name = value[last];
755
756             n = ngx_regex_capture_count(regex->regex);
757
758             if (n) {
759                 regex->ncaptures = (n + 1) * 3;
760             }
761
762             return NGX_CONF_OK;
763         }
764
765         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
766                            "unexpected \"%V\" in condition", &value[cur]);
767         return NGX_CONF_ERROR;
768
769     } else if ((len == 2 && p[0] == '-')
770                || (len == 3 && p[0] == '!' && p[1] == '-'))
771     {
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;
776         }
777
778         value[last].data[value[last].len] = '\0';
779         value[last].len++;
780
781         if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) {
782             return NGX_CONF_ERROR;
783         }
784
785         fop = ngx_http_script_start_code(cf->pool, &lcf->codes,
786                                           sizeof(ngx_http_script_file_code_t));
787         if (fop == NULL) {
788             return NGX_CONF_ERROR;
789         }
790
791         fop->code = ngx_http_script_file_code;
792
793         if (p[1] == 'f') {
794             fop->op = ngx_http_script_file_plain;
795             return NGX_CONF_OK;
796         }
797
798         if (p[1] == 'd') {
799             fop->op = ngx_http_script_file_dir;
800             return NGX_CONF_OK;
801         }
802
803         if (p[1] == 'e') {
804             fop->op = ngx_http_script_file_exists;
805             return NGX_CONF_OK;
806         }
807
808         if (p[1] == 'x') {
809             fop->op = ngx_http_script_file_exec;
810             return NGX_CONF_OK;
811         }
812
813         if (p[0] == '!') {
814             if (p[2] == 'f') {
815                 fop->op = ngx_http_script_file_not_plain;
816                 return NGX_CONF_OK;
817             }
818
819             if (p[2] == 'd') {
820                 fop->op = ngx_http_script_file_not_dir;
821                 return NGX_CONF_OK;
822             }
823
824             if (p[2] == 'e') {
825                 fop->op = ngx_http_script_file_not_exists;
826                 return NGX_CONF_OK;
827             }
828
829             if (p[2] == 'x') {
830                 fop->op = ngx_http_script_file_not_exec;
831                 return NGX_CONF_OK;
832             }
833         }
834
835         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
836                            "invalid condition \"%V\"", &value[cur]);
837         return NGX_CONF_ERROR;
838     }
839
840     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
841                        "invalid condition \"%V\"", &value[cur]);
842
843     return NGX_CONF_ERROR;
844 }
845
846
847 static char *
848 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
849     ngx_str_t *value)
850 {
851     ngx_int_t                    index;
852     ngx_http_script_var_code_t  *var_code;
853
854     value->len--;
855     value->data++;
856
857     index = ngx_http_get_variable_index(cf, value);
858
859     if (index == NGX_ERROR) {
860         return NGX_CONF_ERROR;
861     }
862
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;
867     }
868
869     var_code->code = ngx_http_script_var_code;
870     var_code->index = index;
871
872     return NGX_CONF_OK;
873 }
874
875
876 static char *
877 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
878 {
879     ngx_http_rewrite_loc_conf_t  *lcf = conf;
880
881     ngx_int_t                            index;
882     ngx_str_t                           *value;
883     ngx_http_variable_t                 *v;
884     ngx_http_script_var_code_t          *vcode;
885     ngx_http_script_var_handler_code_t  *vhcode;
886
887     value = cf->args->elts;
888
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;
893     }
894
895     value[1].len--;
896     value[1].data++;
897
898     v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
899     if (v == NULL) {
900         return NGX_CONF_ERROR;
901     }
902
903     index = ngx_http_get_variable_index(cf, &value[1]);
904     if (index == NGX_ERROR) {
905         return NGX_CONF_ERROR;
906     }
907
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)
912     {
913         v->get_handler = ngx_http_rewrite_var;
914         v->data = index;
915     }
916
917     if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) {
918         return NGX_CONF_ERROR;
919     }
920
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;
926         }
927
928         vhcode->code = ngx_http_script_var_set_handler_code;
929         vhcode->handler = v->set_handler;
930         vhcode->data = v->data;
931
932         return NGX_CONF_OK;
933     }
934
935     vcode = ngx_http_script_start_code(cf->pool, &lcf->codes,
936                                        sizeof(ngx_http_script_var_code_t));
937     if (vcode == NULL) {
938         return NGX_CONF_ERROR;
939     }
940
941     vcode->code = ngx_http_script_set_var_code;
942     vcode->index = (uintptr_t) index;
943
944     return NGX_CONF_OK;
945 }
946
947
948 static char *
949 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf,
950     ngx_str_t *value)
951 {
952     ngx_int_t                              n;
953     ngx_http_script_compile_t              sc;
954     ngx_http_script_value_code_t          *val;
955     ngx_http_script_complex_value_code_t  *complex;
956
957     n = ngx_http_script_variables_count(value);
958
959     if (n == 0) {
960         val = ngx_http_script_start_code(cf->pool, &lcf->codes,
961                                          sizeof(ngx_http_script_value_code_t));
962         if (val == NULL) {
963             return NGX_CONF_ERROR;
964         }
965
966         n = ngx_atoi(value->data, value->len);
967
968         if (n == NGX_ERROR) {
969             n = 0;
970         }
971
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;
976
977         return NGX_CONF_OK;
978     }
979
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;
984     }
985
986     complex->code = ngx_http_script_complex_value_code;
987     complex->lengths = NULL;
988
989     ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
990
991     sc.cf = cf;
992     sc.source = value;
993     sc.lengths = &complex->lengths;
994     sc.values = &lcf->codes;
995     sc.variables = n;
996     sc.complete_lengths = 1;
997
998     if (ngx_http_script_compile(&sc) != NGX_OK) {
999         return NGX_CONF_ERROR;
1000     }
1001
1002     return NGX_CONF_OK;
1003 }