Mercurial > hg > nginx
annotate src/http/modules/ngx_http_rewrite_module.c @ 2223:005fc65f7ce7
style fix: remove tabs and trailing spaces
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 01 Sep 2008 14:05:20 +0000 |
parents | 989176d3ffc5 |
children | 912ce4113f21 |
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 captures; |
16 ngx_uint_t stack_size; | |
485 | 17 |
577 | 18 ngx_flag_t log; |
635 | 19 ngx_flag_t uninitialized_variable_warn; |
485 | 20 } 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
|
21 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
22 |
485 | 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, | |
487 | 25 void *parent, void *child); |
681 | 26 static ngx_int_t ngx_http_rewrite_init(ngx_conf_t *cf); |
485 | 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, | |
487 | 29 void *conf); |
527 | 30 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, |
31 void *conf); | |
485 | 32 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, |
487 | 33 void *conf); |
501 | 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); | |
509 | 38 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, |
501 | 39 void *conf); |
577 | 40 static char * ngx_http_rewrite_value(ngx_conf_t *cf, |
41 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
|
42 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
43 |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
44 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
|
45 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
46 { ngx_string("rewrite"), |
485 | 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, | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
59 0, |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 NULL }, |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
61 |
527 | 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 | |
485 | 70 { ngx_string("if"), |
71 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE, | |
72 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
|
73 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
|
74 0, |
485 | 75 NULL }, |
76 | |
501 | 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 | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
85 { ngx_string("rewrite_log"), |
635 | 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, | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
88 ngx_conf_set_flag_slot, |
485 | 89 NGX_HTTP_LOC_CONF_OFFSET, |
90 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
|
91 NULL }, |
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
92 |
635 | 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 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
101 ngx_null_command |
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 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
104 |
667 | 105 static ngx_http_module_t ngx_http_rewrite_module_ctx = { |
509 | 106 NULL, /* preconfiguration */ |
681 | 107 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
|
108 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
109 NULL, /* create main configuration */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
110 NULL, /* init main configuration */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
111 |
485 | 112 NULL, /* create server configuration */ |
113 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
|
114 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
115 ngx_http_rewrite_create_loc_conf, /* create location configration */ |
485 | 116 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
|
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 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
120 ngx_module_t ngx_http_rewrite_module = { |
509 | 121 NGX_MODULE_V1, |
577 | 122 &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
|
123 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
|
124 NGX_HTTP_MODULE, /* module type */ |
541 | 125 NULL, /* init master */ |
681 | 126 NULL, /* init module */ |
541 | 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 | |
216
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 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
135 |
487 | 136 static ngx_int_t |
137 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
|
138 { |
509 | 139 ngx_http_script_code_pt code; |
140 ngx_http_script_engine_t *e; | |
635 | 141 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
|
142 |
635 | 143 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
|
144 |
635 | 145 if (rlcf->codes == NULL) { |
485 | 146 return NGX_DECLINED; |
147 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
148 |
509 | 149 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t)); |
501 | 150 if (e == NULL) { |
485 | 151 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
152 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
153 |
509 | 154 e->sp = ngx_pcalloc(r->pool, |
635 | 155 rlcf->stack_size * sizeof(ngx_http_variable_value_t)); |
485 | 156 if (e->sp == NULL) { |
157 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
158 } | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
159 |
635 | 160 if (rlcf->captures) { |
161 e->captures = ngx_palloc(r->pool, rlcf->captures * sizeof(int)); | |
485 | 162 if (e->captures == NULL) { |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
163 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
164 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
165 |
485 | 166 } else { |
167 e->captures = NULL; | |
168 } | |
169 | |
635 | 170 e->ip = rlcf->codes->elts; |
509 | 171 e->request = r; |
485 | 172 e->quote = 1; |
635 | 173 e->log = rlcf->log; |
485 | 174 e->status = NGX_DECLINED; |
175 | |
176 while (*(uintptr_t *) e->ip) { | |
509 | 177 code = *(ngx_http_script_code_pt *) e->ip; |
485 | 178 code(e); |
179 } | |
180 | |
739
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
181 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
|
182 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
|
183 } |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
184 |
726
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
185 if (r->err_status == 0) { |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
186 return e->status; |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
187 } |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
188 |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
189 return r->err_status; |
485 | 190 } |
191 | |
192 | |
573 | 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) | |
501 | 196 { |
635 | 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 } | |
501 | 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, | |
519 | 214 * and they should be handled by ngx_http_get_indexed_variable(), |
501 | 215 * so the handler is called only if the variable is not initialized |
216 */ | |
217 | |
635 | 218 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, |
501 | 219 "using uninitialized \"%V\" variable", &var[data].name); |
220 | |
577 | 221 *v = ngx_http_variable_null_value; |
573 | 222 |
223 return NGX_OK; | |
501 | 224 } |
225 | |
226 | |
487 | 227 static void * |
228 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
|
229 { |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
230 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
|
231 |
501 | 232 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)); |
233 if (conf == NULL) { | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
234 return NGX_CONF_ERROR; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
235 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
236 |
485 | 237 conf->stack_size = NGX_CONF_UNSET_UINT; |
238 conf->log = NGX_CONF_UNSET; | |
635 | 239 conf->uninitialized_variable_warn = NGX_CONF_UNSET; |
485 | 240 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
241 return conf; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
242 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
243 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
244 |
487 | 245 static char * |
246 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
|
247 { |
485 | 248 ngx_http_rewrite_loc_conf_t *prev = parent; |
249 ngx_http_rewrite_loc_conf_t *conf = child; | |
250 | |
509 | 251 uintptr_t *code; |
485 | 252 |
253 ngx_conf_merge_value(conf->log, prev->log, 0); | |
635 | 254 ngx_conf_merge_value(conf->uninitialized_variable_warn, |
255 prev->uninitialized_variable_warn, 1); | |
663 | 256 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10); |
485 | 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 | |
501 | 266 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t)); |
267 if (code == NULL) { | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
268 return NGX_CONF_ERROR; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
269 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
270 |
485 | 271 *code = (uintptr_t) NULL; |
272 | |
273 return NGX_CONF_OK; | |
274 } | |
275 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
276 |
509 | 277 static ngx_int_t |
681 | 278 ngx_http_rewrite_init(ngx_conf_t *cf) |
577 | 279 { |
509 | 280 ngx_http_handler_pt *h; |
281 ngx_http_core_main_conf_t *cmcf; | |
282 | |
681 | 283 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
509 | 284 |
573 | 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 | |
509 | 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; | |
577 | 300 } |
509 | 301 |
302 | |
487 | 303 static char * |
304 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 305 { |
577 | 306 ngx_http_rewrite_loc_conf_t *lcf = conf; |
307 | |
509 | 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]; | |
485 | 316 |
509 | 317 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, |
318 sizeof(ngx_http_script_regex_code_t)); | |
485 | 319 if (regex == NULL) { |
320 return NGX_CONF_ERROR; | |
321 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
322 |
509 | 323 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
324 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
325 value = cf->args->elts; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
326 |
485 | 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 | |
509 | 339 regex->code = ngx_http_script_regex_start_code; |
485 | 340 regex->uri = 1; |
341 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
|
342 |
509 | 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 | |
485 | 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 | |
2054
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
360 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
|
361 regex->status = NGX_HTTP_MOVED_TEMPORARILY; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
362 regex->redirect = 1; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
363 last = 1; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
364 } |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
365 |
485 | 366 if (cf->args->nelts == 4) { |
367 if (ngx_strcmp(value[3].data, "last") == 0) { | |
368 last = 1; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
369 |
487 | 370 } else if (ngx_strcmp(value[3].data, "break") == 0) { |
371 regex->break_cycle = 1; | |
372 last = 1; | |
373 | |
485 | 374 } else if (ngx_strcmp(value[3].data, "redirect") == 0) { |
375 regex->status = NGX_HTTP_MOVED_TEMPORARILY; | |
376 regex->redirect = 1; | |
377 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
378 |
485 | 379 } else if (ngx_strcmp(value[3].data, "permanent") == 0) { |
380 regex->status = NGX_HTTP_MOVED_PERMANENTLY; | |
381 regex->redirect = 1; | |
382 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
383 |
485 | 384 } else { |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
385 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
461 | 386 "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
|
387 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
|
388 } |
485 | 389 } |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
390 |
509 | 391 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); |
485 | 392 |
509 | 393 sc.cf = cf; |
394 sc.source = &value[2]; | |
395 sc.lengths = ®ex->lengths; | |
396 sc.values = &lcf->codes; | |
397 sc.variables = ngx_http_script_variables_count(&value[2]); | |
398 sc.main = regex; | |
399 sc.complete_lengths = 1; | |
400 sc.compile_args = !regex->redirect; | |
485 | 401 |
509 | 402 if (ngx_http_script_compile(&sc) != NGX_OK) { |
403 return NGX_CONF_ERROR; | |
404 } | |
485 | 405 |
509 | 406 regex = sc.main; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
407 |
509 | 408 regex->ncaptures = sc.ncaptures; |
409 regex->size = sc.size; | |
410 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
|
411 |
641 | 412 if (sc.variables == 0 && !sc.dup_capture) { |
509 | 413 regex->lengths = NULL; |
485 | 414 } |
415 | |
416 n = ngx_regex_capture_count(regex->regex); | |
417 | |
501 | 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) { | |
485 | 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 | |
501 | 433 if (regex->ncaptures < (ngx_uint_t) n) { |
434 regex->ncaptures = (ngx_uint_t) n; | |
485 | 435 } |
481 | 436 |
485 | 437 if (regex->ncaptures) { |
438 regex->ncaptures = (regex->ncaptures + 1) * 3; | |
439 | |
515 | 440 if (lcf->captures < regex->ncaptures) { |
441 lcf->captures = regex->ncaptures; | |
442 } | |
509 | 443 } |
444 | |
445 regex_end = ngx_http_script_add_code(lcf->codes, | |
446 sizeof(ngx_http_script_regex_end_code_t), | |
447 ®ex); | |
485 | 448 if (regex_end == NULL) { |
449 return NGX_CONF_ERROR; | |
450 } | |
451 | |
509 | 452 regex_end->code = ngx_http_script_regex_end_code; |
485 | 453 regex_end->uri = regex->uri; |
454 regex_end->args = regex->args; | |
509 | 455 regex_end->add_args = regex->add_args; |
485 | 456 regex_end->redirect = regex->redirect; |
457 | |
458 if (last) { | |
517 | 459 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex); |
485 | 460 if (code == NULL) { |
461 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
|
462 } |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
463 |
485 | 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 | |
487 | 474 static char * |
475 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 476 { |
577 | 477 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
|
478 |
509 | 479 ngx_str_t *value; |
480 ngx_http_script_return_code_t *ret; | |
485 | 481 |
509 | 482 ret = ngx_http_script_start_code(cf->pool, &lcf->codes, |
483 sizeof(ngx_http_script_return_code_t)); | |
485 | 484 if (ret == NULL) { |
485 return NGX_CONF_ERROR; | |
486 } | |
487 | |
488 value = cf->args->elts; | |
489 | |
509 | 490 ret->code = ngx_http_script_return_code; |
485 | 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; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
497 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
498 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
499 return NGX_CONF_OK; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
500 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
501 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
502 |
487 | 503 static char * |
527 | 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 * | |
487 | 522 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
|
523 { |
577 | 524 ngx_http_rewrite_loc_conf_t *lcf = conf; |
485 | 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; | |
2027 | 533 ngx_http_core_loc_conf_t *clcf, *pclcf; |
509 | 534 ngx_http_script_if_code_t *if_code; |
485 | 535 ngx_http_rewrite_loc_conf_t *nlcf; |
536 | |
501 | 537 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); |
538 if (ctx == NULL) { | |
485 | 539 return NGX_CONF_ERROR; |
540 } | |
541 | |
542 pctx = cf->ctx; | |
543 ctx->main_conf = pctx->main_conf; | |
577 | 544 ctx->srv_conf = pctx->srv_conf; |
485 | 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; | |
577 | 557 |
485 | 558 if (module->create_loc_conf) { |
559 | |
501 | 560 mconf = module->create_loc_conf(cf); |
561 if (mconf == NULL) { | |
485 | 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 | |
2027 | 576 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) { |
485 | 577 return NGX_CONF_ERROR; |
578 } | |
579 | |
501 | 580 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) { |
485 | 581 return NGX_CONF_ERROR; |
582 } | |
583 | |
509 | 584 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t)); |
485 | 585 if (if_code == NULL) { |
586 return NULL; | |
587 } | |
588 | |
509 | 589 if_code->code = ngx_http_script_if_code; |
485 | 590 |
591 elts = lcf->codes->elts; | |
592 | |
593 | |
631 | 594 /* the inner directives must be compiled to the same code array */ |
485 | 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 | |
517 | 621 if (lcf->captures < nlcf->captures) { |
622 lcf->captures = nlcf->captures; | |
623 } | |
624 | |
625 | |
485 | 626 if (elts != lcf->codes->elts) { |
509 | 627 if_code = (ngx_http_script_if_code_t *) |
485 | 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; | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
633 |
555 | 634 /* the code array belong to parent block */ |
635 | |
636 nlcf->codes = NULL; | |
637 | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
638 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
|
639 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
640 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
641 |
487 | 642 static char * |
501 | 643 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) |
644 { | |
631 | 645 u_char *p; |
646 size_t len; | |
509 | 647 ngx_str_t *value, err; |
515 | 648 ngx_uint_t cur, last, n; |
577 | 649 ngx_http_script_code_pt *code; |
631 | 650 ngx_http_script_file_code_t *fop; |
509 | 651 ngx_http_script_regex_code_t *regex; |
652 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
501 | 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 | |
631 | 686 len = value[cur].len; |
687 p = value[cur].data; | |
688 | |
689 if (len > 1 && p[0] == '$') { | |
501 | 690 |
691 if (cur != last && cur + 2 != last) { | |
692 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
631 | 693 "invalid condition \"%V\"", &value[cur]); |
501 | 694 return NGX_CONF_ERROR; |
695 } | |
696 | |
577 | 697 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) { |
501 | 698 return NGX_CONF_ERROR; |
699 } | |
700 | |
701 if (cur == last) { | |
702 return NGX_CONF_OK; | |
703 } | |
704 | |
705 cur++; | |
706 | |
631 | 707 len = value[cur].len; |
708 p = value[cur].data; | |
709 | |
710 if (len == 1 && p[0] == '=') { | |
577 | 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; | |
501 | 725 } |
726 | |
631 | 727 if (len == 2 && p[0] == '!' && p[1] == '=') { |
728 | |
577 | 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; | |
501 | 741 } |
742 | |
631 | 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] == '*')) | |
577 | 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 } | |
509 | 753 |
577 | 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], | |
631 | 760 (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0, |
761 cf->pool, &err); | |
501 | 762 |
577 | 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; | |
631 | 771 if (p[0] == '!') { |
772 regex->negative_test = 1; | |
773 } | |
577 | 774 regex->name = value[last]; |
501 | 775 |
577 | 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; | |
501 | 787 } |
788 | |
577 | 789 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
790 "unexpected \"%V\" in condition", &value[cur]); | |
791 return NGX_CONF_ERROR; | |
631 | 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 | |
669 | 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 | |
631 | 837 if (p[0] == '!') { |
838 if (p[2] == 'f') { | |
839 fop->op = ngx_http_script_file_not_plain; | |
840 return NGX_CONF_OK; | |
841 } | |
669 | 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 } | |
631 | 857 } |
858 | |
859 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
860 "invalid condition \"%V\"", &value[cur]); | |
861 return NGX_CONF_ERROR; | |
501 | 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 { | |
509 | 875 ngx_int_t index; |
876 ngx_http_script_var_code_t *var_code; | |
501 | 877 |
878 value->len--; | |
879 value->data++; | |
880 | |
577 | 881 index = ngx_http_get_variable_index(cf, value); |
501 | 882 |
577 | 883 if (index == NGX_ERROR) { |
884 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
|
885 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
886 |
577 | 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; | |
485 | 891 } |
892 | |
577 | 893 var_code->code = ngx_http_script_var_code; |
894 var_code->index = index; | |
485 | 895 |
896 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
|
897 } |
485 | 898 |
899 | |
501 | 900 static char * |
901 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
902 { | |
577 | 903 ngx_http_rewrite_loc_conf_t *lcf = conf; |
501 | 904 |
637 | 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; | |
501 | 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 | |
1565 | 922 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
501 | 923 if (v == NULL) { |
924 return NGX_CONF_ERROR; | |
925 } | |
926 | |
509 | 927 index = ngx_http_get_variable_index(cf, &value[1]); |
928 if (index == NGX_ERROR) { | |
929 return NGX_CONF_ERROR; | |
930 } | |
501 | 931 |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
932 if (v->get_handler == NULL |
1192 | 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) | |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
936 { |
637 | 937 v->get_handler = ngx_http_rewrite_var; |
555 | 938 v->data = index; |
939 } | |
509 | 940 |
577 | 941 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) { |
942 return NGX_CONF_ERROR; | |
515 | 943 } |
501 | 944 |
637 | 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) { | |
501 | 962 return NGX_CONF_ERROR; |
963 } | |
964 | |
637 | 965 vcode->code = ngx_http_script_set_var_code; |
966 vcode->index = (uintptr_t) index; | |
501 | 967 |
968 return NGX_CONF_OK; | |
969 } | |
577 | 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 } |