Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_rewrite_module.c @ 879:804514cb0758
bump version to 0.5.0
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 27 Nov 2006 14:34:42 +0000 |
parents | 9f983de19a1d |
children | 5e1a8fe79c4b |
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 | |
360 if (cf->args->nelts == 4) { | |
361 if (ngx_strcmp(value[3].data, "last") == 0) { | |
362 last = 1; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
363 |
487 | 364 } else if (ngx_strcmp(value[3].data, "break") == 0) { |
365 regex->break_cycle = 1; | |
366 last = 1; | |
367 | |
485 | 368 } else if (ngx_strcmp(value[3].data, "redirect") == 0) { |
369 regex->status = NGX_HTTP_MOVED_TEMPORARILY; | |
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 if (ngx_strcmp(value[3].data, "permanent") == 0) { |
374 regex->status = NGX_HTTP_MOVED_PERMANENTLY; | |
375 regex->redirect = 1; | |
376 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
377 |
485 | 378 } else { |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
379 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
461 | 380 "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
|
381 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
|
382 } |
485 | 383 } |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
384 |
509 | 385 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); |
485 | 386 |
509 | 387 sc.cf = cf; |
388 sc.source = &value[2]; | |
389 sc.lengths = ®ex->lengths; | |
390 sc.values = &lcf->codes; | |
391 sc.variables = ngx_http_script_variables_count(&value[2]); | |
392 sc.main = regex; | |
393 sc.complete_lengths = 1; | |
394 sc.compile_args = !regex->redirect; | |
485 | 395 |
509 | 396 if (ngx_http_script_compile(&sc) != NGX_OK) { |
397 return NGX_CONF_ERROR; | |
398 } | |
485 | 399 |
509 | 400 regex = sc.main; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
401 |
509 | 402 regex->ncaptures = sc.ncaptures; |
403 regex->size = sc.size; | |
404 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
|
405 |
641 | 406 if (sc.variables == 0 && !sc.dup_capture) { |
509 | 407 regex->lengths = NULL; |
485 | 408 } |
409 | |
410 n = ngx_regex_capture_count(regex->regex); | |
411 | |
501 | 412 if (n < 0) { |
413 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
414 ngx_regex_capture_count_n " failed for " | |
415 "pattern \"%V\"", &value[1]); | |
416 return NGX_CONF_ERROR; | |
417 } | |
418 | |
419 if (regex->ncaptures > (ngx_uint_t) n) { | |
485 | 420 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
421 "pattern \"%V\" has less captures " | |
422 "than referrenced in substitution \"%V\"", | |
423 &value[1], &value[2]); | |
424 return NGX_CONF_ERROR; | |
425 } | |
426 | |
501 | 427 if (regex->ncaptures < (ngx_uint_t) n) { |
428 regex->ncaptures = (ngx_uint_t) n; | |
485 | 429 } |
481 | 430 |
485 | 431 if (regex->ncaptures) { |
432 regex->ncaptures = (regex->ncaptures + 1) * 3; | |
433 | |
515 | 434 if (lcf->captures < regex->ncaptures) { |
435 lcf->captures = regex->ncaptures; | |
436 } | |
509 | 437 } |
438 | |
439 regex_end = ngx_http_script_add_code(lcf->codes, | |
440 sizeof(ngx_http_script_regex_end_code_t), | |
441 ®ex); | |
485 | 442 if (regex_end == NULL) { |
443 return NGX_CONF_ERROR; | |
444 } | |
445 | |
509 | 446 regex_end->code = ngx_http_script_regex_end_code; |
485 | 447 regex_end->uri = regex->uri; |
448 regex_end->args = regex->args; | |
509 | 449 regex_end->add_args = regex->add_args; |
485 | 450 regex_end->redirect = regex->redirect; |
451 | |
452 if (last) { | |
517 | 453 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex); |
485 | 454 if (code == NULL) { |
455 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
|
456 } |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
457 |
485 | 458 *code = (uintptr_t) NULL; |
459 } | |
460 | |
461 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
462 - (u_char *) regex; | |
463 | |
464 return NGX_CONF_OK; | |
465 } | |
466 | |
467 | |
487 | 468 static char * |
469 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 470 { |
577 | 471 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
|
472 |
509 | 473 ngx_str_t *value; |
474 ngx_http_script_return_code_t *ret; | |
485 | 475 |
509 | 476 ret = ngx_http_script_start_code(cf->pool, &lcf->codes, |
477 sizeof(ngx_http_script_return_code_t)); | |
485 | 478 if (ret == NULL) { |
479 return NGX_CONF_ERROR; | |
480 } | |
481 | |
482 value = cf->args->elts; | |
483 | |
509 | 484 ret->code = ngx_http_script_return_code; |
485 | 485 ret->null = (uintptr_t) NULL; |
486 | |
487 ret->status = ngx_atoi(value[1].data, value[1].len); | |
488 | |
489 if (ret->status == (uintptr_t) NGX_ERROR) { | |
490 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
|
491 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
492 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
493 return NGX_CONF_OK; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
494 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
495 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
496 |
487 | 497 static char * |
527 | 498 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
499 { | |
500 ngx_http_rewrite_loc_conf_t *lcf = conf; | |
501 | |
502 ngx_http_script_code_pt *code; | |
503 | |
504 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t)); | |
505 if (code == NULL) { | |
506 return NGX_CONF_ERROR; | |
507 } | |
508 | |
509 *code = ngx_http_script_break_code; | |
510 | |
511 return NGX_CONF_OK; | |
512 } | |
513 | |
514 | |
515 static char * | |
487 | 516 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
|
517 { |
577 | 518 ngx_http_rewrite_loc_conf_t *lcf = conf; |
485 | 519 |
520 void *mconf; | |
521 char *rv; | |
522 u_char *elts; | |
523 ngx_uint_t i; | |
524 ngx_conf_t save; | |
525 ngx_http_module_t *module; | |
526 ngx_http_conf_ctx_t *ctx, *pctx; | |
527 ngx_http_core_loc_conf_t *clcf, *pclcf, **clcfp; | |
509 | 528 ngx_http_script_if_code_t *if_code; |
485 | 529 ngx_http_rewrite_loc_conf_t *nlcf; |
530 | |
501 | 531 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); |
532 if (ctx == NULL) { | |
485 | 533 return NGX_CONF_ERROR; |
534 } | |
535 | |
536 pctx = cf->ctx; | |
537 ctx->main_conf = pctx->main_conf; | |
577 | 538 ctx->srv_conf = pctx->srv_conf; |
485 | 539 |
540 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
541 if (ctx->loc_conf == NULL) { | |
542 return NGX_CONF_ERROR; | |
543 } | |
544 | |
545 for (i = 0; ngx_modules[i]; i++) { | |
546 if (ngx_modules[i]->type != NGX_HTTP_MODULE) { | |
547 continue; | |
548 } | |
549 | |
550 module = ngx_modules[i]->ctx; | |
577 | 551 |
485 | 552 if (module->create_loc_conf) { |
553 | |
501 | 554 mconf = module->create_loc_conf(cf); |
555 if (mconf == NULL) { | |
485 | 556 return NGX_CONF_ERROR; |
557 } | |
558 | |
559 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf; | |
560 } | |
561 } | |
562 | |
563 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index]; | |
564 | |
565 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index]; | |
566 clcf->loc_conf = ctx->loc_conf; | |
567 clcf->name = pclcf->name; | |
568 clcf->noname = 1; | |
569 | |
570 if (pclcf->locations.elts == NULL) { | |
571 if (ngx_array_init(&pclcf->locations, cf->pool, 4, sizeof(void *)) | |
555 | 572 == NGX_ERROR) |
485 | 573 { |
574 return NGX_CONF_ERROR; | |
575 } | |
576 } | |
577 | |
501 | 578 clcfp = ngx_array_push(&pclcf->locations); |
579 if (clcfp == NULL) { | |
485 | 580 return NGX_CONF_ERROR; |
581 } | |
582 | |
583 *clcfp = clcf; | |
584 | |
585 | |
501 | 586 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) { |
485 | 587 return NGX_CONF_ERROR; |
588 } | |
589 | |
509 | 590 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t)); |
485 | 591 if (if_code == NULL) { |
592 return NULL; | |
593 } | |
594 | |
509 | 595 if_code->code = ngx_http_script_if_code; |
485 | 596 |
597 elts = lcf->codes->elts; | |
598 | |
599 | |
631 | 600 /* the inner directives must be compiled to the same code array */ |
485 | 601 |
602 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index]; | |
603 nlcf->codes = lcf->codes; | |
604 | |
605 | |
606 save = *cf; | |
607 cf->ctx = ctx; | |
608 | |
609 if (pclcf->name.len == 0) { | |
610 if_code->loc_conf = NULL; | |
611 cf->cmd_type = NGX_HTTP_SIF_CONF; | |
612 | |
613 } else { | |
614 if_code->loc_conf = ctx->loc_conf; | |
615 cf->cmd_type = NGX_HTTP_LIF_CONF; | |
616 } | |
617 | |
618 rv = ngx_conf_parse(cf, NULL); | |
619 | |
620 *cf = save; | |
621 | |
622 if (rv != NGX_CONF_OK) { | |
623 return rv; | |
624 } | |
625 | |
626 | |
517 | 627 if (lcf->captures < nlcf->captures) { |
628 lcf->captures = nlcf->captures; | |
629 } | |
630 | |
631 | |
485 | 632 if (elts != lcf->codes->elts) { |
509 | 633 if_code = (ngx_http_script_if_code_t *) |
485 | 634 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts)); |
635 } | |
636 | |
637 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
638 - (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
|
639 |
555 | 640 /* the code array belong to parent block */ |
641 | |
642 nlcf->codes = NULL; | |
643 | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
644 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
|
645 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
646 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
647 |
487 | 648 static char * |
501 | 649 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) |
650 { | |
631 | 651 u_char *p; |
652 size_t len; | |
509 | 653 ngx_str_t *value, err; |
515 | 654 ngx_uint_t cur, last, n; |
577 | 655 ngx_http_script_code_pt *code; |
631 | 656 ngx_http_script_file_code_t *fop; |
509 | 657 ngx_http_script_regex_code_t *regex; |
658 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
501 | 659 |
660 value = cf->args->elts; | |
661 last = cf->args->nelts - 1; | |
662 | |
663 if (value[1].len < 1 || value[1].data[0] != '(') { | |
664 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
665 "invalid condition \"%V\"", &value[1]); | |
666 return NGX_CONF_ERROR; | |
667 } | |
668 | |
669 if (value[1].len == 1) { | |
670 cur = 2; | |
671 | |
672 } else { | |
673 cur = 1; | |
674 value[1].len--; | |
675 value[1].data++; | |
676 } | |
677 | |
678 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') { | |
679 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
680 "invalid condition \"%V\"", &value[last]); | |
681 return NGX_CONF_ERROR; | |
682 } | |
683 | |
684 if (value[last].len == 1) { | |
685 last--; | |
686 | |
687 } else { | |
688 value[last].len--; | |
689 value[last].data[value[last].len] = '\0'; | |
690 } | |
691 | |
631 | 692 len = value[cur].len; |
693 p = value[cur].data; | |
694 | |
695 if (len > 1 && p[0] == '$') { | |
501 | 696 |
697 if (cur != last && cur + 2 != last) { | |
698 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
631 | 699 "invalid condition \"%V\"", &value[cur]); |
501 | 700 return NGX_CONF_ERROR; |
701 } | |
702 | |
577 | 703 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) { |
501 | 704 return NGX_CONF_ERROR; |
705 } | |
706 | |
707 if (cur == last) { | |
708 return NGX_CONF_OK; | |
709 } | |
710 | |
711 cur++; | |
712 | |
631 | 713 len = value[cur].len; |
714 p = value[cur].data; | |
715 | |
716 if (len == 1 && p[0] == '=') { | |
577 | 717 |
718 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
719 return NGX_CONF_ERROR; | |
720 } | |
721 | |
722 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
723 sizeof(uintptr_t)); | |
724 if (code == NULL) { | |
725 return NGX_CONF_ERROR; | |
726 } | |
727 | |
728 *code = ngx_http_script_equal_code; | |
729 | |
730 return NGX_CONF_OK; | |
501 | 731 } |
732 | |
631 | 733 if (len == 2 && p[0] == '!' && p[1] == '=') { |
734 | |
577 | 735 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { |
736 return NGX_CONF_ERROR; | |
737 } | |
738 | |
739 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
740 sizeof(uintptr_t)); | |
741 if (code == NULL) { | |
742 return NGX_CONF_ERROR; | |
743 } | |
744 | |
745 *code = ngx_http_script_not_equal_code; | |
746 return NGX_CONF_OK; | |
501 | 747 } |
748 | |
631 | 749 if ((len == 1 && p[0] == '~') |
750 || (len == 2 && p[0] == '~' && p[1] == '*') | |
751 || (len == 2 && p[0] == '!' && p[1] == '~') | |
752 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*')) | |
577 | 753 { |
754 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
755 sizeof(ngx_http_script_regex_code_t)); | |
756 if (regex == NULL) { | |
757 return NGX_CONF_ERROR; | |
758 } | |
509 | 759 |
577 | 760 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
761 | |
762 err.len = NGX_MAX_CONF_ERRSTR; | |
763 err.data = errstr; | |
764 | |
765 regex->regex = ngx_regex_compile(&value[last], | |
631 | 766 (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0, |
767 cf->pool, &err); | |
501 | 768 |
577 | 769 if (regex->regex == NULL) { |
770 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data); | |
771 return NGX_CONF_ERROR; | |
772 } | |
773 | |
774 regex->code = ngx_http_script_regex_start_code; | |
775 regex->next = sizeof(ngx_http_script_regex_code_t); | |
776 regex->test = 1; | |
631 | 777 if (p[0] == '!') { |
778 regex->negative_test = 1; | |
779 } | |
577 | 780 regex->name = value[last]; |
501 | 781 |
577 | 782 n = ngx_regex_capture_count(regex->regex); |
783 | |
784 if (n) { | |
785 regex->ncaptures = (n + 1) * 3; | |
786 | |
787 if (lcf->captures < regex->ncaptures) { | |
788 lcf->captures = regex->ncaptures; | |
789 } | |
790 } | |
791 | |
792 return NGX_CONF_OK; | |
501 | 793 } |
794 | |
577 | 795 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
796 "unexpected \"%V\" in condition", &value[cur]); | |
797 return NGX_CONF_ERROR; | |
631 | 798 |
799 } else if ((len == 2 && p[0] == '-') | |
800 || (len == 3 && p[0] == '!' && p[1] == '-')) | |
801 { | |
802 if (cur + 1 != last) { | |
803 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
804 "invalid condition \"%V\"", &value[cur]); | |
805 return NGX_CONF_ERROR; | |
806 } | |
807 | |
808 value[last].data[value[last].len] = '\0'; | |
809 value[last].len++; | |
810 | |
811 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
812 return NGX_CONF_ERROR; | |
813 } | |
814 | |
815 fop = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
816 sizeof(ngx_http_script_file_code_t)); | |
817 if (fop == NULL) { | |
818 return NGX_CONF_ERROR; | |
819 } | |
820 | |
821 fop->code = ngx_http_script_file_code; | |
822 | |
823 if (p[1] == 'f') { | |
824 fop->op = ngx_http_script_file_plain; | |
825 return NGX_CONF_OK; | |
826 } | |
827 | |
669 | 828 if (p[1] == 'd') { |
829 fop->op = ngx_http_script_file_dir; | |
830 return NGX_CONF_OK; | |
831 } | |
832 | |
833 if (p[1] == 'e') { | |
834 fop->op = ngx_http_script_file_exists; | |
835 return NGX_CONF_OK; | |
836 } | |
837 | |
838 if (p[1] == 'x') { | |
839 fop->op = ngx_http_script_file_exec; | |
840 return NGX_CONF_OK; | |
841 } | |
842 | |
631 | 843 if (p[0] == '!') { |
844 if (p[2] == 'f') { | |
845 fop->op = ngx_http_script_file_not_plain; | |
846 return NGX_CONF_OK; | |
847 } | |
669 | 848 |
849 if (p[2] == 'd') { | |
850 fop->op = ngx_http_script_file_not_dir; | |
851 return NGX_CONF_OK; | |
852 } | |
853 | |
854 if (p[2] == 'e') { | |
855 fop->op = ngx_http_script_file_not_exists; | |
856 return NGX_CONF_OK; | |
857 } | |
858 | |
859 if (p[2] == 'x') { | |
860 fop->op = ngx_http_script_file_not_exec; | |
861 return NGX_CONF_OK; | |
862 } | |
631 | 863 } |
864 | |
865 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
866 "invalid condition \"%V\"", &value[cur]); | |
867 return NGX_CONF_ERROR; | |
501 | 868 } |
869 | |
870 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
871 "invalid condition \"%V\"", &value[cur]); | |
872 | |
873 return NGX_CONF_ERROR; | |
874 } | |
875 | |
876 | |
877 static char * | |
878 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
879 ngx_str_t *value) | |
880 { | |
509 | 881 ngx_int_t index; |
882 ngx_http_script_var_code_t *var_code; | |
501 | 883 |
884 value->len--; | |
885 value->data++; | |
886 | |
577 | 887 index = ngx_http_get_variable_index(cf, value); |
501 | 888 |
577 | 889 if (index == NGX_ERROR) { |
890 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
|
891 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
892 |
577 | 893 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes, |
894 sizeof(ngx_http_script_var_code_t)); | |
895 if (var_code == NULL) { | |
896 return NGX_CONF_ERROR; | |
485 | 897 } |
898 | |
577 | 899 var_code->code = ngx_http_script_var_code; |
900 var_code->index = index; | |
485 | 901 |
902 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
|
903 } |
485 | 904 |
905 | |
501 | 906 static char * |
907 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
908 { | |
577 | 909 ngx_http_rewrite_loc_conf_t *lcf = conf; |
501 | 910 |
637 | 911 ngx_int_t index; |
912 ngx_str_t *value; | |
913 ngx_http_variable_t *v; | |
914 ngx_http_script_var_code_t *vcode; | |
915 ngx_http_script_var_handler_code_t *vhcode; | |
501 | 916 |
917 value = cf->args->elts; | |
918 | |
919 if (value[1].data[0] != '$') { | |
920 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
921 "invalid variable name \"%V\"", &value[1]); | |
922 return NGX_CONF_ERROR; | |
923 } | |
924 | |
925 value[1].len--; | |
926 value[1].data++; | |
927 | |
509 | 928 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGABLE); |
501 | 929 if (v == NULL) { |
930 return NGX_CONF_ERROR; | |
931 } | |
932 | |
509 | 933 index = ngx_http_get_variable_index(cf, &value[1]); |
934 if (index == NGX_ERROR) { | |
935 return NGX_CONF_ERROR; | |
936 } | |
501 | 937 |
637 | 938 if (v->get_handler == NULL) { |
939 v->get_handler = ngx_http_rewrite_var; | |
555 | 940 v->data = index; |
941 } | |
509 | 942 |
577 | 943 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) { |
944 return NGX_CONF_ERROR; | |
515 | 945 } |
501 | 946 |
637 | 947 if (v->set_handler) { |
948 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
949 sizeof(ngx_http_script_var_handler_code_t)); | |
950 if (vhcode == NULL) { | |
951 return NGX_CONF_ERROR; | |
952 } | |
953 | |
954 vhcode->code = ngx_http_script_var_set_handler_code; | |
955 vhcode->handler = v->set_handler; | |
956 vhcode->data = v->data; | |
957 | |
958 return NGX_CONF_OK; | |
959 } | |
960 | |
961 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
962 sizeof(ngx_http_script_var_code_t)); | |
963 if (vcode == NULL) { | |
501 | 964 return NGX_CONF_ERROR; |
965 } | |
966 | |
637 | 967 vcode->code = ngx_http_script_set_var_code; |
968 vcode->index = (uintptr_t) index; | |
501 | 969 |
970 return NGX_CONF_OK; | |
971 } | |
577 | 972 |
973 | |
974 static char * | |
975 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
976 ngx_str_t *value) | |
977 { | |
978 ngx_int_t n; | |
979 ngx_http_script_compile_t sc; | |
980 ngx_http_script_value_code_t *val; | |
981 ngx_http_script_complex_value_code_t *complex; | |
982 | |
983 n = ngx_http_script_variables_count(value); | |
984 | |
985 if (n == 0) { | |
986 val = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
987 sizeof(ngx_http_script_value_code_t)); | |
988 if (val == NULL) { | |
989 return NGX_CONF_ERROR; | |
990 } | |
991 | |
992 n = ngx_atoi(value->data, value->len); | |
993 | |
994 if (n == NGX_ERROR) { | |
995 n = 0; | |
996 } | |
997 | |
998 val->code = ngx_http_script_value_code; | |
999 val->value = (uintptr_t) n; | |
1000 val->text_len = (uintptr_t) value->len; | |
1001 val->text_data = (uintptr_t) value->data; | |
1002 | |
1003 return NGX_CONF_OK; | |
1004 } | |
1005 | |
1006 complex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
1007 sizeof(ngx_http_script_complex_value_code_t)); | |
1008 if (complex == NULL) { | |
1009 return NGX_CONF_ERROR; | |
1010 } | |
1011 | |
1012 complex->code = ngx_http_script_complex_value_code; | |
1013 complex->lengths = NULL; | |
1014 | |
1015 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1016 | |
1017 sc.cf = cf; | |
1018 sc.source = value; | |
1019 sc.lengths = &complex->lengths; | |
1020 sc.values = &lcf->codes; | |
1021 sc.variables = n; | |
1022 sc.complete_lengths = 1; | |
1023 | |
1024 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1025 return NGX_CONF_ERROR; | |
1026 } | |
1027 | |
1028 return NGX_CONF_OK; | |
1029 } |