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