Mercurial > hg > nginx
annotate src/http/modules/ngx_http_rewrite_module.c @ 4583:a1d5842064f7
Fixed buffer overflow when long URI is processed by "try_files" in
regex location with "alias" (fixes ticket #135).
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Thu, 12 Apr 2012 09:19:14 +0000 |
parents | 778ef9c3fd2d |
children | ed957e0daeb4 |
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 |
4412 | 4 * Copyright (C) Nginx, Inc. |
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
|
5 */ |
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
|
6 |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_core.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 #include <ngx_http.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
12 |
485 | 13 typedef struct { |
577 | 14 ngx_array_t *codes; /* uintptr_t */ |
485 | 15 |
577 | 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 | |
3633 | 56 |NGX_CONF_TAKE12, |
485 | 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 |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
115 ngx_http_rewrite_create_loc_conf, /* create location configuration */ |
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4412
diff
changeset
|
116 ngx_http_rewrite_merge_loc_conf /* merge location configuration */ |
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 { |
4208
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
139 ngx_int_t index; |
509 | 140 ngx_http_script_code_pt code; |
141 ngx_http_script_engine_t *e; | |
4208
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
142 ngx_http_core_srv_conf_t *cscf; |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
143 ngx_http_core_main_conf_t *cmcf; |
635 | 144 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
|
145 |
4208
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
146 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
147 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
148 index = cmcf->phase_engine.location_rewrite_index; |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
149 |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
150 if (r->phase_handler == index && r->loc_conf == cscf->ctx->loc_conf) { |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
151 /* skipping location rewrite phase for server null location */ |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
152 return NGX_DECLINED; |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
153 } |
470462cfa31b
Skipping location rewrite phase for server null location.
Igor Sysoev <igor@sysoev.ru>
parents:
4147
diff
changeset
|
154 |
635 | 155 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
|
156 |
635 | 157 if (rlcf->codes == NULL) { |
485 | 158 return NGX_DECLINED; |
159 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
160 |
509 | 161 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t)); |
501 | 162 if (e == NULL) { |
485 | 163 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
164 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
165 |
509 | 166 e->sp = ngx_pcalloc(r->pool, |
635 | 167 rlcf->stack_size * sizeof(ngx_http_variable_value_t)); |
485 | 168 if (e->sp == NULL) { |
169 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
170 } | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
171 |
635 | 172 e->ip = rlcf->codes->elts; |
509 | 173 e->request = r; |
485 | 174 e->quote = 1; |
635 | 175 e->log = rlcf->log; |
485 | 176 e->status = NGX_DECLINED; |
177 | |
178 while (*(uintptr_t *) e->ip) { | |
509 | 179 code = *(ngx_http_script_code_pt *) e->ip; |
485 | 180 code(e); |
181 } | |
182 | |
4147
7f64de1cc2c0
Fix for double content when return is used in error_page handler.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3633
diff
changeset
|
183 if (e->status < NGX_HTTP_BAD_REQUEST) { |
7f64de1cc2c0
Fix for double content when return is used in error_page handler.
Maxim Dounin <mdounin@mdounin.ru>
parents:
3633
diff
changeset
|
184 return e->status; |
739
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
185 } |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
186 |
726
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
187 if (r->err_status == 0) { |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
188 return e->status; |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
189 } |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
190 |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
191 return r->err_status; |
485 | 192 } |
193 | |
194 | |
573 | 195 static ngx_int_t |
196 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
197 uintptr_t data) | |
501 | 198 { |
635 | 199 ngx_http_variable_t *var; |
200 ngx_http_core_main_conf_t *cmcf; | |
201 ngx_http_rewrite_loc_conf_t *rlcf; | |
202 | |
203 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module); | |
204 | |
205 if (rlcf->uninitialized_variable_warn == 0) { | |
206 *v = ngx_http_variable_null_value; | |
207 return NGX_OK; | |
208 } | |
501 | 209 |
210 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
211 | |
212 var = cmcf->variables.elts; | |
213 | |
214 /* | |
215 * the ngx_http_rewrite_module sets variables directly in r->variables, | |
519 | 216 * and they should be handled by ngx_http_get_indexed_variable(), |
501 | 217 * so the handler is called only if the variable is not initialized |
218 */ | |
219 | |
635 | 220 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, |
501 | 221 "using uninitialized \"%V\" variable", &var[data].name); |
222 | |
577 | 223 *v = ngx_http_variable_null_value; |
573 | 224 |
225 return NGX_OK; | |
501 | 226 } |
227 | |
228 | |
487 | 229 static void * |
230 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
|
231 { |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
232 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
|
233 |
501 | 234 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)); |
235 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
|
236 return NULL; |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
237 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
238 |
485 | 239 conf->stack_size = NGX_CONF_UNSET_UINT; |
240 conf->log = NGX_CONF_UNSET; | |
635 | 241 conf->uninitialized_variable_warn = NGX_CONF_UNSET; |
485 | 242 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
243 return conf; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
244 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
245 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
246 |
487 | 247 static char * |
248 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
|
249 { |
485 | 250 ngx_http_rewrite_loc_conf_t *prev = parent; |
251 ngx_http_rewrite_loc_conf_t *conf = child; | |
252 | |
509 | 253 uintptr_t *code; |
485 | 254 |
255 ngx_conf_merge_value(conf->log, prev->log, 0); | |
635 | 256 ngx_conf_merge_value(conf->uninitialized_variable_warn, |
257 prev->uninitialized_variable_warn, 1); | |
663 | 258 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10); |
485 | 259 |
260 if (conf->codes == NULL) { | |
261 return NGX_CONF_OK; | |
262 } | |
263 | |
264 if (conf->codes == prev->codes) { | |
265 return NGX_CONF_OK; | |
266 } | |
267 | |
501 | 268 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t)); |
269 if (code == NULL) { | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
270 return NGX_CONF_ERROR; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
271 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
272 |
485 | 273 *code = (uintptr_t) NULL; |
274 | |
275 return NGX_CONF_OK; | |
276 } | |
277 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
278 |
509 | 279 static ngx_int_t |
681 | 280 ngx_http_rewrite_init(ngx_conf_t *cf) |
577 | 281 { |
509 | 282 ngx_http_handler_pt *h; |
283 ngx_http_core_main_conf_t *cmcf; | |
284 | |
681 | 285 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
509 | 286 |
573 | 287 h = ngx_array_push(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers); |
288 if (h == NULL) { | |
289 return NGX_ERROR; | |
290 } | |
291 | |
292 *h = ngx_http_rewrite_handler; | |
293 | |
509 | 294 h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers); |
295 if (h == NULL) { | |
296 return NGX_ERROR; | |
297 } | |
298 | |
299 *h = ngx_http_rewrite_handler; | |
300 | |
301 return NGX_OK; | |
577 | 302 } |
509 | 303 |
304 | |
487 | 305 static char * |
306 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 307 { |
577 | 308 ngx_http_rewrite_loc_conf_t *lcf = conf; |
309 | |
3325 | 310 ngx_str_t *value; |
509 | 311 ngx_uint_t last; |
3325 | 312 ngx_regex_compile_t rc; |
509 | 313 ngx_http_script_code_pt *code; |
314 ngx_http_script_compile_t sc; | |
315 ngx_http_script_regex_code_t *regex; | |
316 ngx_http_script_regex_end_code_t *regex_end; | |
317 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
485 | 318 |
509 | 319 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, |
320 sizeof(ngx_http_script_regex_code_t)); | |
485 | 321 if (regex == NULL) { |
322 return NGX_CONF_ERROR; | |
323 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
324 |
509 | 325 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
326 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
327 value = cf->args->elts; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
328 |
3325 | 329 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); |
330 | |
331 rc.pattern = value[1]; | |
332 rc.err.len = NGX_MAX_CONF_ERRSTR; | |
333 rc.err.data = errstr; | |
485 | 334 |
335 /* TODO: NGX_REGEX_CASELESS */ | |
336 | |
3325 | 337 regex->regex = ngx_http_regex_compile(cf, &rc); |
485 | 338 if (regex->regex == NULL) { |
339 return NGX_CONF_ERROR; | |
340 } | |
341 | |
509 | 342 regex->code = ngx_http_script_regex_start_code; |
485 | 343 regex->uri = 1; |
344 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
|
345 |
509 | 346 if (value[2].data[value[2].len - 1] == '?') { |
347 | |
348 /* the last "?" drops the original arguments */ | |
349 value[2].len--; | |
350 | |
351 } else { | |
352 regex->add_args = 1; | |
353 } | |
354 | |
485 | 355 last = 0; |
356 | |
3524
ca24774c16e3
autodetect redirect if URI is rewritten to a string starting with $scheme
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
357 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0 |
ca24774c16e3
autodetect redirect if URI is rewritten to a string starting with $scheme
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
358 || ngx_strncmp(value[2].data, "https://", sizeof("https://") - 1) == 0 |
ca24774c16e3
autodetect redirect if URI is rewritten to a string starting with $scheme
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
359 || ngx_strncmp(value[2].data, "$scheme", sizeof("$scheme") - 1) == 0) |
ca24774c16e3
autodetect redirect if URI is rewritten to a string starting with $scheme
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
360 { |
2054
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->size = sc.size; |
409 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
|
410 |
641 | 411 if (sc.variables == 0 && !sc.dup_capture) { |
509 | 412 regex->lengths = NULL; |
485 | 413 } |
414 | |
509 | 415 regex_end = ngx_http_script_add_code(lcf->codes, |
416 sizeof(ngx_http_script_regex_end_code_t), | |
417 ®ex); | |
485 | 418 if (regex_end == NULL) { |
419 return NGX_CONF_ERROR; | |
420 } | |
421 | |
509 | 422 regex_end->code = ngx_http_script_regex_end_code; |
485 | 423 regex_end->uri = regex->uri; |
424 regex_end->args = regex->args; | |
509 | 425 regex_end->add_args = regex->add_args; |
485 | 426 regex_end->redirect = regex->redirect; |
427 | |
428 if (last) { | |
517 | 429 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex); |
485 | 430 if (code == NULL) { |
431 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
|
432 } |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
433 |
3184 | 434 *code = NULL; |
485 | 435 } |
436 | |
437 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
438 - (u_char *) regex; | |
439 | |
440 return NGX_CONF_OK; | |
441 } | |
442 | |
443 | |
487 | 444 static char * |
445 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 446 { |
577 | 447 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
|
448 |
3633 | 449 u_char *p; |
450 ngx_str_t *value, *v; | |
451 ngx_http_script_return_code_t *ret; | |
452 ngx_http_compile_complex_value_t ccv; | |
485 | 453 |
509 | 454 ret = ngx_http_script_start_code(cf->pool, &lcf->codes, |
455 sizeof(ngx_http_script_return_code_t)); | |
485 | 456 if (ret == NULL) { |
457 return NGX_CONF_ERROR; | |
458 } | |
459 | |
460 value = cf->args->elts; | |
461 | |
3633 | 462 ngx_memzero(ret, sizeof(ngx_http_script_return_code_t)); |
463 | |
509 | 464 ret->code = ngx_http_script_return_code; |
485 | 465 |
3633 | 466 p = value[1].data; |
467 | |
468 ret->status = ngx_atoi(p, value[1].len); | |
485 | 469 |
470 if (ret->status == (uintptr_t) NGX_ERROR) { | |
3633 | 471 |
472 if (cf->args->nelts == 2 | |
473 && (ngx_strncmp(p, "http://", sizeof("http://") - 1) == 0 | |
474 || ngx_strncmp(p, "https://", sizeof("https://") - 1) == 0 | |
475 || ngx_strncmp(p, "$scheme", sizeof("$scheme") - 1) == 0)) | |
476 { | |
477 ret->status = NGX_HTTP_MOVED_TEMPORARILY; | |
478 v = &value[1]; | |
479 | |
480 } else { | |
481 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
482 "invalid return code \"%V\"", &value[1]); | |
483 return NGX_CONF_ERROR; | |
484 } | |
485 | |
486 } else { | |
487 | |
488 if (cf->args->nelts == 2) { | |
489 return NGX_CONF_OK; | |
490 } | |
491 | |
492 v = &value[2]; | |
493 } | |
494 | |
495 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); | |
496 | |
497 ccv.cf = cf; | |
498 ccv.value = v; | |
499 ccv.complex_value = &ret->text; | |
500 | |
501 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { | |
485 | 502 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
|
503 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
504 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
505 return NGX_CONF_OK; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
506 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
507 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
508 |
487 | 509 static char * |
527 | 510 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
511 { | |
512 ngx_http_rewrite_loc_conf_t *lcf = conf; | |
513 | |
514 ngx_http_script_code_pt *code; | |
515 | |
516 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t)); | |
517 if (code == NULL) { | |
518 return NGX_CONF_ERROR; | |
519 } | |
520 | |
521 *code = ngx_http_script_break_code; | |
522 | |
523 return NGX_CONF_OK; | |
524 } | |
525 | |
526 | |
527 static char * | |
487 | 528 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
|
529 { |
577 | 530 ngx_http_rewrite_loc_conf_t *lcf = conf; |
485 | 531 |
532 void *mconf; | |
533 char *rv; | |
534 u_char *elts; | |
535 ngx_uint_t i; | |
536 ngx_conf_t save; | |
537 ngx_http_module_t *module; | |
538 ngx_http_conf_ctx_t *ctx, *pctx; | |
2027 | 539 ngx_http_core_loc_conf_t *clcf, *pclcf; |
509 | 540 ngx_http_script_if_code_t *if_code; |
485 | 541 ngx_http_rewrite_loc_conf_t *nlcf; |
542 | |
501 | 543 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); |
544 if (ctx == NULL) { | |
485 | 545 return NGX_CONF_ERROR; |
546 } | |
547 | |
548 pctx = cf->ctx; | |
549 ctx->main_conf = pctx->main_conf; | |
577 | 550 ctx->srv_conf = pctx->srv_conf; |
485 | 551 |
552 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
553 if (ctx->loc_conf == NULL) { | |
554 return NGX_CONF_ERROR; | |
555 } | |
556 | |
557 for (i = 0; ngx_modules[i]; i++) { | |
558 if (ngx_modules[i]->type != NGX_HTTP_MODULE) { | |
559 continue; | |
560 } | |
561 | |
562 module = ngx_modules[i]->ctx; | |
577 | 563 |
485 | 564 if (module->create_loc_conf) { |
565 | |
501 | 566 mconf = module->create_loc_conf(cf); |
567 if (mconf == NULL) { | |
485 | 568 return NGX_CONF_ERROR; |
569 } | |
570 | |
571 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf; | |
572 } | |
573 } | |
574 | |
575 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index]; | |
576 | |
577 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index]; | |
578 clcf->loc_conf = ctx->loc_conf; | |
579 clcf->name = pclcf->name; | |
580 clcf->noname = 1; | |
581 | |
2027 | 582 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) { |
485 | 583 return NGX_CONF_ERROR; |
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) { |
3001 | 592 return NGX_CONF_ERROR; |
485 | 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 | |
627 if (elts != lcf->codes->elts) { | |
509 | 628 if_code = (ngx_http_script_if_code_t *) |
485 | 629 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts)); |
630 } | |
631 | |
632 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
633 - (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
|
634 |
555 | 635 /* the code array belong to parent block */ |
636 | |
637 nlcf->codes = NULL; | |
638 | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
639 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
|
640 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
641 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
642 |
487 | 643 static char * |
501 | 644 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) |
645 { | |
631 | 646 u_char *p; |
647 size_t len; | |
3325 | 648 ngx_str_t *value; |
649 ngx_uint_t cur, last; | |
650 ngx_regex_compile_t rc; | |
577 | 651 ngx_http_script_code_pt *code; |
631 | 652 ngx_http_script_file_code_t *fop; |
509 | 653 ngx_http_script_regex_code_t *regex; |
654 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
501 | 655 |
656 value = cf->args->elts; | |
657 last = cf->args->nelts - 1; | |
658 | |
659 if (value[1].len < 1 || value[1].data[0] != '(') { | |
660 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
661 "invalid condition \"%V\"", &value[1]); | |
662 return NGX_CONF_ERROR; | |
663 } | |
664 | |
665 if (value[1].len == 1) { | |
666 cur = 2; | |
667 | |
668 } else { | |
669 cur = 1; | |
670 value[1].len--; | |
671 value[1].data++; | |
672 } | |
673 | |
674 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') { | |
675 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
676 "invalid condition \"%V\"", &value[last]); | |
677 return NGX_CONF_ERROR; | |
678 } | |
679 | |
680 if (value[last].len == 1) { | |
681 last--; | |
682 | |
683 } else { | |
684 value[last].len--; | |
685 value[last].data[value[last].len] = '\0'; | |
686 } | |
687 | |
631 | 688 len = value[cur].len; |
689 p = value[cur].data; | |
690 | |
691 if (len > 1 && p[0] == '$') { | |
501 | 692 |
693 if (cur != last && cur + 2 != last) { | |
694 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
631 | 695 "invalid condition \"%V\"", &value[cur]); |
501 | 696 return NGX_CONF_ERROR; |
697 } | |
698 | |
577 | 699 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) { |
501 | 700 return NGX_CONF_ERROR; |
701 } | |
702 | |
703 if (cur == last) { | |
704 return NGX_CONF_OK; | |
705 } | |
706 | |
707 cur++; | |
708 | |
631 | 709 len = value[cur].len; |
710 p = value[cur].data; | |
711 | |
712 if (len == 1 && p[0] == '=') { | |
577 | 713 |
714 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
715 return NGX_CONF_ERROR; | |
716 } | |
717 | |
718 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
719 sizeof(uintptr_t)); | |
720 if (code == NULL) { | |
721 return NGX_CONF_ERROR; | |
722 } | |
723 | |
724 *code = ngx_http_script_equal_code; | |
725 | |
726 return NGX_CONF_OK; | |
501 | 727 } |
728 | |
631 | 729 if (len == 2 && p[0] == '!' && p[1] == '=') { |
730 | |
577 | 731 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { |
732 return NGX_CONF_ERROR; | |
733 } | |
734 | |
735 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
736 sizeof(uintptr_t)); | |
737 if (code == NULL) { | |
738 return NGX_CONF_ERROR; | |
739 } | |
740 | |
741 *code = ngx_http_script_not_equal_code; | |
742 return NGX_CONF_OK; | |
501 | 743 } |
744 | |
631 | 745 if ((len == 1 && p[0] == '~') |
746 || (len == 2 && p[0] == '~' && p[1] == '*') | |
747 || (len == 2 && p[0] == '!' && p[1] == '~') | |
748 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*')) | |
577 | 749 { |
750 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
751 sizeof(ngx_http_script_regex_code_t)); | |
752 if (regex == NULL) { | |
753 return NGX_CONF_ERROR; | |
754 } | |
509 | 755 |
577 | 756 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
757 | |
3325 | 758 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); |
577 | 759 |
3325 | 760 rc.pattern = value[last]; |
761 rc.options = (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0; | |
762 rc.err.len = NGX_MAX_CONF_ERRSTR; | |
763 rc.err.data = errstr; | |
501 | 764 |
3325 | 765 regex->regex = ngx_http_regex_compile(cf, &rc); |
577 | 766 if (regex->regex == NULL) { |
767 return NGX_CONF_ERROR; | |
768 } | |
769 | |
770 regex->code = ngx_http_script_regex_start_code; | |
771 regex->next = sizeof(ngx_http_script_regex_code_t); | |
772 regex->test = 1; | |
631 | 773 if (p[0] == '!') { |
774 regex->negative_test = 1; | |
775 } | |
577 | 776 regex->name = value[last]; |
501 | 777 |
577 | 778 return NGX_CONF_OK; |
501 | 779 } |
780 | |
577 | 781 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
782 "unexpected \"%V\" in condition", &value[cur]); | |
783 return NGX_CONF_ERROR; | |
631 | 784 |
785 } else if ((len == 2 && p[0] == '-') | |
786 || (len == 3 && p[0] == '!' && p[1] == '-')) | |
787 { | |
788 if (cur + 1 != last) { | |
789 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
790 "invalid condition \"%V\"", &value[cur]); | |
791 return NGX_CONF_ERROR; | |
792 } | |
793 | |
794 value[last].data[value[last].len] = '\0'; | |
795 value[last].len++; | |
796 | |
797 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
798 return NGX_CONF_ERROR; | |
799 } | |
800 | |
801 fop = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
802 sizeof(ngx_http_script_file_code_t)); | |
803 if (fop == NULL) { | |
804 return NGX_CONF_ERROR; | |
805 } | |
806 | |
807 fop->code = ngx_http_script_file_code; | |
808 | |
809 if (p[1] == 'f') { | |
810 fop->op = ngx_http_script_file_plain; | |
811 return NGX_CONF_OK; | |
812 } | |
813 | |
669 | 814 if (p[1] == 'd') { |
815 fop->op = ngx_http_script_file_dir; | |
816 return NGX_CONF_OK; | |
817 } | |
818 | |
819 if (p[1] == 'e') { | |
820 fop->op = ngx_http_script_file_exists; | |
821 return NGX_CONF_OK; | |
822 } | |
823 | |
824 if (p[1] == 'x') { | |
825 fop->op = ngx_http_script_file_exec; | |
826 return NGX_CONF_OK; | |
827 } | |
828 | |
631 | 829 if (p[0] == '!') { |
830 if (p[2] == 'f') { | |
831 fop->op = ngx_http_script_file_not_plain; | |
832 return NGX_CONF_OK; | |
833 } | |
669 | 834 |
835 if (p[2] == 'd') { | |
836 fop->op = ngx_http_script_file_not_dir; | |
837 return NGX_CONF_OK; | |
838 } | |
839 | |
840 if (p[2] == 'e') { | |
841 fop->op = ngx_http_script_file_not_exists; | |
842 return NGX_CONF_OK; | |
843 } | |
844 | |
845 if (p[2] == 'x') { | |
846 fop->op = ngx_http_script_file_not_exec; | |
847 return NGX_CONF_OK; | |
848 } | |
631 | 849 } |
850 | |
851 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
852 "invalid condition \"%V\"", &value[cur]); | |
853 return NGX_CONF_ERROR; | |
501 | 854 } |
855 | |
856 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
857 "invalid condition \"%V\"", &value[cur]); | |
858 | |
859 return NGX_CONF_ERROR; | |
860 } | |
861 | |
862 | |
863 static char * | |
864 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
865 ngx_str_t *value) | |
866 { | |
509 | 867 ngx_int_t index; |
868 ngx_http_script_var_code_t *var_code; | |
501 | 869 |
870 value->len--; | |
871 value->data++; | |
872 | |
577 | 873 index = ngx_http_get_variable_index(cf, value); |
501 | 874 |
577 | 875 if (index == NGX_ERROR) { |
876 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
|
877 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
878 |
577 | 879 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes, |
880 sizeof(ngx_http_script_var_code_t)); | |
881 if (var_code == NULL) { | |
882 return NGX_CONF_ERROR; | |
485 | 883 } |
884 | |
577 | 885 var_code->code = ngx_http_script_var_code; |
886 var_code->index = index; | |
485 | 887 |
888 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
|
889 } |
485 | 890 |
891 | |
501 | 892 static char * |
893 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
894 { | |
577 | 895 ngx_http_rewrite_loc_conf_t *lcf = conf; |
501 | 896 |
637 | 897 ngx_int_t index; |
898 ngx_str_t *value; | |
899 ngx_http_variable_t *v; | |
900 ngx_http_script_var_code_t *vcode; | |
901 ngx_http_script_var_handler_code_t *vhcode; | |
501 | 902 |
903 value = cf->args->elts; | |
904 | |
905 if (value[1].data[0] != '$') { | |
906 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
907 "invalid variable name \"%V\"", &value[1]); | |
908 return NGX_CONF_ERROR; | |
909 } | |
910 | |
911 value[1].len--; | |
912 value[1].data++; | |
913 | |
1565 | 914 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
501 | 915 if (v == NULL) { |
916 return NGX_CONF_ERROR; | |
917 } | |
918 | |
509 | 919 index = ngx_http_get_variable_index(cf, &value[1]); |
920 if (index == NGX_ERROR) { | |
921 return NGX_CONF_ERROR; | |
922 } | |
501 | 923 |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
924 if (v->get_handler == NULL |
1192 | 925 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0 |
926 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0 | |
927 && 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
|
928 { |
637 | 929 v->get_handler = ngx_http_rewrite_var; |
555 | 930 v->data = index; |
931 } | |
509 | 932 |
577 | 933 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) { |
934 return NGX_CONF_ERROR; | |
515 | 935 } |
501 | 936 |
637 | 937 if (v->set_handler) { |
938 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
939 sizeof(ngx_http_script_var_handler_code_t)); | |
940 if (vhcode == NULL) { | |
941 return NGX_CONF_ERROR; | |
942 } | |
943 | |
944 vhcode->code = ngx_http_script_var_set_handler_code; | |
945 vhcode->handler = v->set_handler; | |
946 vhcode->data = v->data; | |
947 | |
948 return NGX_CONF_OK; | |
949 } | |
950 | |
951 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
952 sizeof(ngx_http_script_var_code_t)); | |
953 if (vcode == NULL) { | |
501 | 954 return NGX_CONF_ERROR; |
955 } | |
956 | |
637 | 957 vcode->code = ngx_http_script_set_var_code; |
958 vcode->index = (uintptr_t) index; | |
501 | 959 |
960 return NGX_CONF_OK; | |
961 } | |
577 | 962 |
963 | |
964 static char * | |
965 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
966 ngx_str_t *value) | |
967 { | |
968 ngx_int_t n; | |
969 ngx_http_script_compile_t sc; | |
970 ngx_http_script_value_code_t *val; | |
971 ngx_http_script_complex_value_code_t *complex; | |
972 | |
973 n = ngx_http_script_variables_count(value); | |
974 | |
975 if (n == 0) { | |
976 val = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
977 sizeof(ngx_http_script_value_code_t)); | |
978 if (val == NULL) { | |
979 return NGX_CONF_ERROR; | |
980 } | |
981 | |
982 n = ngx_atoi(value->data, value->len); | |
983 | |
984 if (n == NGX_ERROR) { | |
985 n = 0; | |
986 } | |
987 | |
988 val->code = ngx_http_script_value_code; | |
989 val->value = (uintptr_t) n; | |
990 val->text_len = (uintptr_t) value->len; | |
991 val->text_data = (uintptr_t) value->data; | |
992 | |
993 return NGX_CONF_OK; | |
994 } | |
995 | |
996 complex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
997 sizeof(ngx_http_script_complex_value_code_t)); | |
998 if (complex == NULL) { | |
999 return NGX_CONF_ERROR; | |
1000 } | |
1001 | |
1002 complex->code = ngx_http_script_complex_value_code; | |
1003 complex->lengths = NULL; | |
1004 | |
1005 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1006 | |
1007 sc.cf = cf; | |
1008 sc.source = value; | |
1009 sc.lengths = &complex->lengths; | |
1010 sc.values = &lcf->codes; | |
1011 sc.variables = n; | |
1012 sc.complete_lengths = 1; | |
1013 | |
1014 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1015 return NGX_CONF_ERROR; | |
1016 } | |
1017 | |
1018 return NGX_CONF_OK; | |
1019 } |