Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_rewrite_module.c @ 2975:2c0f09aa8574
allow underscore in request method
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 13 Jul 2009 09:33:34 +0000 |
parents | c7d57b539248 |
children | cf21d35248bc |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
5 |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_http.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
485 | 12 typedef struct { |
577 | 13 ngx_array_t *codes; /* uintptr_t */ |
485 | 14 |
577 | 15 ngx_uint_t stack_size; |
485 | 16 |
577 | 17 ngx_flag_t log; |
635 | 18 ngx_flag_t uninitialized_variable_warn; |
485 | 19 } ngx_http_rewrite_loc_conf_t; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
20 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
21 |
485 | 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, | |
487 | 24 void *parent, void *child); |
681 | 25 static ngx_int_t ngx_http_rewrite_init(ngx_conf_t *cf); |
485 | 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, | |
487 | 28 void *conf); |
527 | 29 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, |
30 void *conf); | |
485 | 31 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, |
487 | 32 void *conf); |
501 | 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); | |
509 | 37 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, |
501 | 38 void *conf); |
577 | 39 static char * ngx_http_rewrite_value(ngx_conf_t *cf, |
40 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value); | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
41 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
42 |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
43 static ngx_command_t ngx_http_rewrite_commands[] = { |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
44 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
45 { ngx_string("rewrite"), |
485 | 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, | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
58 0, |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
59 NULL }, |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 |
527 | 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 | |
485 | 69 { ngx_string("if"), |
70 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE, | |
71 ngx_http_rewrite_if, | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
72 NGX_HTTP_LOC_CONF_OFFSET, |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
73 0, |
485 | 74 NULL }, |
75 | |
501 | 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 | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
84 { ngx_string("rewrite_log"), |
635 | 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, | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
87 ngx_conf_set_flag_slot, |
485 | 88 NGX_HTTP_LOC_CONF_OFFSET, |
89 offsetof(ngx_http_rewrite_loc_conf_t, log), | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
90 NULL }, |
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
91 |
635 | 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 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
100 ngx_null_command |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
101 }; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
102 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
103 |
667 | 104 static ngx_http_module_t ngx_http_rewrite_module_ctx = { |
509 | 105 NULL, /* preconfiguration */ |
681 | 106 ngx_http_rewrite_init, /* postconfiguration */ |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
107 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
108 NULL, /* create main configuration */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
109 NULL, /* init main configuration */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
110 |
485 | 111 NULL, /* create server configuration */ |
112 NULL, /* merge server configuration */ | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
113 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
114 ngx_http_rewrite_create_loc_conf, /* create location configration */ |
485 | 115 ngx_http_rewrite_merge_loc_conf /* merge location configration */ |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
116 }; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
117 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
118 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
119 ngx_module_t ngx_http_rewrite_module = { |
509 | 120 NGX_MODULE_V1, |
577 | 121 &ngx_http_rewrite_module_ctx, /* module context */ |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
122 ngx_http_rewrite_commands, /* module directives */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
123 NGX_HTTP_MODULE, /* module type */ |
541 | 124 NULL, /* init master */ |
681 | 125 NULL, /* init module */ |
541 | 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 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
132 }; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
133 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
134 |
487 | 135 static ngx_int_t |
136 ngx_http_rewrite_handler(ngx_http_request_t *r) | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
137 { |
509 | 138 ngx_http_script_code_pt code; |
139 ngx_http_script_engine_t *e; | |
635 | 140 ngx_http_rewrite_loc_conf_t *rlcf; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
141 |
635 | 142 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module); |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
143 |
635 | 144 if (rlcf->codes == NULL) { |
485 | 145 return NGX_DECLINED; |
146 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
147 |
509 | 148 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t)); |
501 | 149 if (e == NULL) { |
485 | 150 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
151 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
152 |
509 | 153 e->sp = ngx_pcalloc(r->pool, |
635 | 154 rlcf->stack_size * sizeof(ngx_http_variable_value_t)); |
485 | 155 if (e->sp == NULL) { |
156 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
157 } | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
158 |
635 | 159 e->ip = rlcf->codes->elts; |
509 | 160 e->request = r; |
485 | 161 e->quote = 1; |
635 | 162 e->log = rlcf->log; |
485 | 163 e->status = NGX_DECLINED; |
164 | |
165 while (*(uintptr_t *) e->ip) { | |
509 | 166 code = *(ngx_http_script_code_pt *) e->ip; |
485 | 167 code(e); |
168 } | |
169 | |
739
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
170 if (e->status == NGX_DECLINED) { |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
171 return NGX_DECLINED; |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
172 } |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
173 |
726
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
174 if (r->err_status == 0) { |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
175 return e->status; |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
176 } |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
177 |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
178 return r->err_status; |
485 | 179 } |
180 | |
181 | |
573 | 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) | |
501 | 185 { |
635 | 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 } | |
501 | 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, | |
519 | 203 * and they should be handled by ngx_http_get_indexed_variable(), |
501 | 204 * so the handler is called only if the variable is not initialized |
205 */ | |
206 | |
635 | 207 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, |
501 | 208 "using uninitialized \"%V\" variable", &var[data].name); |
209 | |
577 | 210 *v = ngx_http_variable_null_value; |
573 | 211 |
212 return NGX_OK; | |
501 | 213 } |
214 | |
215 | |
487 | 216 static void * |
217 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf) | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
218 { |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
219 ngx_http_rewrite_loc_conf_t *conf; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
220 |
501 | 221 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)); |
222 if (conf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2547
diff
changeset
|
223 return NULL; |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
224 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
225 |
485 | 226 conf->stack_size = NGX_CONF_UNSET_UINT; |
227 conf->log = NGX_CONF_UNSET; | |
635 | 228 conf->uninitialized_variable_warn = NGX_CONF_UNSET; |
485 | 229 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
230 return conf; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
231 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
232 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
233 |
487 | 234 static char * |
235 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
236 { |
485 | 237 ngx_http_rewrite_loc_conf_t *prev = parent; |
238 ngx_http_rewrite_loc_conf_t *conf = child; | |
239 | |
509 | 240 uintptr_t *code; |
485 | 241 |
242 ngx_conf_merge_value(conf->log, prev->log, 0); | |
635 | 243 ngx_conf_merge_value(conf->uninitialized_variable_warn, |
244 prev->uninitialized_variable_warn, 1); | |
663 | 245 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10); |
485 | 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 | |
501 | 255 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t)); |
256 if (code == NULL) { | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
257 return NGX_CONF_ERROR; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
258 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
259 |
485 | 260 *code = (uintptr_t) NULL; |
261 | |
262 return NGX_CONF_OK; | |
263 } | |
264 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
265 |
509 | 266 static ngx_int_t |
681 | 267 ngx_http_rewrite_init(ngx_conf_t *cf) |
577 | 268 { |
509 | 269 ngx_http_handler_pt *h; |
270 ngx_http_core_main_conf_t *cmcf; | |
271 | |
681 | 272 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
509 | 273 |
573 | 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 | |
509 | 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; | |
577 | 289 } |
509 | 290 |
291 | |
487 | 292 static char * |
293 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 294 { |
577 | 295 ngx_http_rewrite_loc_conf_t *lcf = conf; |
296 | |
509 | 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]; | |
485 | 305 |
509 | 306 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, |
307 sizeof(ngx_http_script_regex_code_t)); | |
485 | 308 if (regex == NULL) { |
309 return NGX_CONF_ERROR; | |
310 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
311 |
509 | 312 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
313 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
314 value = cf->args->elts; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
315 |
485 | 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 | |
509 | 328 regex->code = ngx_http_script_regex_start_code; |
485 | 329 regex->uri = 1; |
330 regex->name = value[1]; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
331 |
509 | 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 | |
485 | 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 | |
2054
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
349 if (ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0) { |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
350 regex->status = NGX_HTTP_MOVED_TEMPORARILY; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
351 regex->redirect = 1; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
352 last = 1; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
353 } |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
354 |
485 | 355 if (cf->args->nelts == 4) { |
356 if (ngx_strcmp(value[3].data, "last") == 0) { | |
357 last = 1; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
358 |
487 | 359 } else if (ngx_strcmp(value[3].data, "break") == 0) { |
360 regex->break_cycle = 1; | |
361 last = 1; | |
362 | |
485 | 363 } else if (ngx_strcmp(value[3].data, "redirect") == 0) { |
364 regex->status = NGX_HTTP_MOVED_TEMPORARILY; | |
365 regex->redirect = 1; | |
366 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
367 |
485 | 368 } else if (ngx_strcmp(value[3].data, "permanent") == 0) { |
369 regex->status = NGX_HTTP_MOVED_PERMANENTLY; | |
370 regex->redirect = 1; | |
371 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
372 |
485 | 373 } else { |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
374 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
461 | 375 "invalid parameter \"%V\"", &value[3]); |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
376 return NGX_CONF_ERROR; |
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
377 } |
485 | 378 } |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
379 |
509 | 380 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); |
485 | 381 |
509 | 382 sc.cf = cf; |
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]); | |
387 sc.main = regex; | |
388 sc.complete_lengths = 1; | |
389 sc.compile_args = !regex->redirect; | |
485 | 390 |
509 | 391 if (ngx_http_script_compile(&sc) != NGX_OK) { |
392 return NGX_CONF_ERROR; | |
393 } | |
485 | 394 |
509 | 395 regex = sc.main; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
396 |
509 | 397 regex->ncaptures = sc.ncaptures; |
398 regex->size = sc.size; | |
399 regex->args = sc.args; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
400 |
641 | 401 if (sc.variables == 0 && !sc.dup_capture) { |
509 | 402 regex->lengths = NULL; |
485 | 403 } |
404 | |
405 n = ngx_regex_capture_count(regex->regex); | |
406 | |
501 | 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) { | |
485 | 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 | |
501 | 422 if (regex->ncaptures < (ngx_uint_t) n) { |
423 regex->ncaptures = (ngx_uint_t) n; | |
485 | 424 } |
481 | 425 |
485 | 426 if (regex->ncaptures) { |
427 regex->ncaptures = (regex->ncaptures + 1) * 3; | |
509 | 428 } |
429 | |
430 regex_end = ngx_http_script_add_code(lcf->codes, | |
431 sizeof(ngx_http_script_regex_end_code_t), | |
432 ®ex); | |
485 | 433 if (regex_end == NULL) { |
434 return NGX_CONF_ERROR; | |
435 } | |
436 | |
509 | 437 regex_end->code = ngx_http_script_regex_end_code; |
485 | 438 regex_end->uri = regex->uri; |
439 regex_end->args = regex->args; | |
509 | 440 regex_end->add_args = regex->add_args; |
485 | 441 regex_end->redirect = regex->redirect; |
442 | |
443 if (last) { | |
517 | 444 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex); |
485 | 445 if (code == NULL) { |
446 return NGX_CONF_ERROR; | |
218
05592fd7a436
nginx-0.0.1-2004-01-05-23:55:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
216
diff
changeset
|
447 } |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
448 |
485 | 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 | |
487 | 459 static char * |
460 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 461 { |
577 | 462 ngx_http_rewrite_loc_conf_t *lcf = conf; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
463 |
509 | 464 ngx_str_t *value; |
465 ngx_http_script_return_code_t *ret; | |
485 | 466 |
509 | 467 ret = ngx_http_script_start_code(cf->pool, &lcf->codes, |
468 sizeof(ngx_http_script_return_code_t)); | |
485 | 469 if (ret == NULL) { |
470 return NGX_CONF_ERROR; | |
471 } | |
472 | |
473 value = cf->args->elts; | |
474 | |
509 | 475 ret->code = ngx_http_script_return_code; |
485 | 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; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
482 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
483 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
484 return NGX_CONF_OK; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
485 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
486 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
487 |
487 | 488 static char * |
527 | 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 * | |
487 | 507 ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
508 { |
577 | 509 ngx_http_rewrite_loc_conf_t *lcf = conf; |
485 | 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; | |
2027 | 518 ngx_http_core_loc_conf_t *clcf, *pclcf; |
509 | 519 ngx_http_script_if_code_t *if_code; |
485 | 520 ngx_http_rewrite_loc_conf_t *nlcf; |
521 | |
501 | 522 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); |
523 if (ctx == NULL) { | |
485 | 524 return NGX_CONF_ERROR; |
525 } | |
526 | |
527 pctx = cf->ctx; | |
528 ctx->main_conf = pctx->main_conf; | |
577 | 529 ctx->srv_conf = pctx->srv_conf; |
485 | 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; | |
577 | 542 |
485 | 543 if (module->create_loc_conf) { |
544 | |
501 | 545 mconf = module->create_loc_conf(cf); |
546 if (mconf == NULL) { | |
485 | 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 | |
2027 | 561 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) { |
485 | 562 return NGX_CONF_ERROR; |
563 } | |
564 | |
501 | 565 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) { |
485 | 566 return NGX_CONF_ERROR; |
567 } | |
568 | |
509 | 569 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t)); |
485 | 570 if (if_code == NULL) { |
571 return NULL; | |
572 } | |
573 | |
509 | 574 if_code->code = ngx_http_script_if_code; |
485 | 575 |
576 elts = lcf->codes->elts; | |
577 | |
578 | |
631 | 579 /* the inner directives must be compiled to the same code array */ |
485 | 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) { | |
509 | 607 if_code = (ngx_http_script_if_code_t *) |
485 | 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; | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
613 |
555 | 614 /* the code array belong to parent block */ |
615 | |
616 nlcf->codes = NULL; | |
617 | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
618 return NGX_CONF_OK; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
619 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
620 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
621 |
487 | 622 static char * |
501 | 623 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) |
624 { | |
631 | 625 u_char *p; |
626 size_t len; | |
509 | 627 ngx_str_t *value, err; |
515 | 628 ngx_uint_t cur, last, n; |
577 | 629 ngx_http_script_code_pt *code; |
631 | 630 ngx_http_script_file_code_t *fop; |
509 | 631 ngx_http_script_regex_code_t *regex; |
632 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
501 | 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 | |
631 | 666 len = value[cur].len; |
667 p = value[cur].data; | |
668 | |
669 if (len > 1 && p[0] == '$') { | |
501 | 670 |
671 if (cur != last && cur + 2 != last) { | |
672 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
631 | 673 "invalid condition \"%V\"", &value[cur]); |
501 | 674 return NGX_CONF_ERROR; |
675 } | |
676 | |
577 | 677 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) { |
501 | 678 return NGX_CONF_ERROR; |
679 } | |
680 | |
681 if (cur == last) { | |
682 return NGX_CONF_OK; | |
683 } | |
684 | |
685 cur++; | |
686 | |
631 | 687 len = value[cur].len; |
688 p = value[cur].data; | |
689 | |
690 if (len == 1 && p[0] == '=') { | |
577 | 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; | |
501 | 705 } |
706 | |
631 | 707 if (len == 2 && p[0] == '!' && p[1] == '=') { |
708 | |
577 | 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; | |
501 | 721 } |
722 | |
631 | 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] == '*')) | |
577 | 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 } | |
509 | 733 |
577 | 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], | |
631 | 740 (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0, |
741 cf->pool, &err); | |
501 | 742 |
577 | 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; | |
631 | 751 if (p[0] == '!') { |
752 regex->negative_test = 1; | |
753 } | |
577 | 754 regex->name = value[last]; |
501 | 755 |
577 | 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; | |
501 | 763 } |
764 | |
577 | 765 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
766 "unexpected \"%V\" in condition", &value[cur]); | |
767 return NGX_CONF_ERROR; | |
631 | 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 | |
669 | 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 | |
631 | 813 if (p[0] == '!') { |
814 if (p[2] == 'f') { | |
815 fop->op = ngx_http_script_file_not_plain; | |
816 return NGX_CONF_OK; | |
817 } | |
669 | 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 } | |
631 | 833 } |
834 | |
835 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
836 "invalid condition \"%V\"", &value[cur]); | |
837 return NGX_CONF_ERROR; | |
501 | 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 { | |
509 | 851 ngx_int_t index; |
852 ngx_http_script_var_code_t *var_code; | |
501 | 853 |
854 value->len--; | |
855 value->data++; | |
856 | |
577 | 857 index = ngx_http_get_variable_index(cf, value); |
501 | 858 |
577 | 859 if (index == NGX_ERROR) { |
860 return NGX_CONF_ERROR; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
861 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
862 |
577 | 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; | |
485 | 867 } |
868 | |
577 | 869 var_code->code = ngx_http_script_var_code; |
870 var_code->index = index; | |
485 | 871 |
872 return NGX_CONF_OK; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
873 } |
485 | 874 |
875 | |
501 | 876 static char * |
877 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
878 { | |
577 | 879 ngx_http_rewrite_loc_conf_t *lcf = conf; |
501 | 880 |
637 | 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; | |
501 | 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 | |
1565 | 898 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
501 | 899 if (v == NULL) { |
900 return NGX_CONF_ERROR; | |
901 } | |
902 | |
509 | 903 index = ngx_http_get_variable_index(cf, &value[1]); |
904 if (index == NGX_ERROR) { | |
905 return NGX_CONF_ERROR; | |
906 } | |
501 | 907 |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
908 if (v->get_handler == NULL |
1192 | 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) | |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
912 { |
637 | 913 v->get_handler = ngx_http_rewrite_var; |
555 | 914 v->data = index; |
915 } | |
509 | 916 |
577 | 917 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) { |
918 return NGX_CONF_ERROR; | |
515 | 919 } |
501 | 920 |
637 | 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) { | |
501 | 938 return NGX_CONF_ERROR; |
939 } | |
940 | |
637 | 941 vcode->code = ngx_http_script_set_var_code; |
942 vcode->index = (uintptr_t) index; | |
501 | 943 |
944 return NGX_CONF_OK; | |
945 } | |
577 | 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 } |