Mercurial > hg > nginx
annotate src/http/modules/ngx_http_rewrite_module.c @ 3496:fb88801564de
build nginx/Windows against OpenSSL-0.9.8n, since nginx-0.8.35
built against OpenSSL-1.0.0 crashed on Windows XP somewhere
in the NTDLL.DLL on the master process exit
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 21 Apr 2010 15:09:17 +0000 |
parents | 42c16d8bddbe |
children | ca24774c16e3 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
437
diff
changeset
|
5 |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_http.h> |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
485 | 12 typedef struct { |
577 | 13 ngx_array_t *codes; /* uintptr_t */ |
485 | 14 |
577 | 15 ngx_uint_t stack_size; |
485 | 16 |
577 | 17 ngx_flag_t log; |
635 | 18 ngx_flag_t uninitialized_variable_warn; |
485 | 19 } ngx_http_rewrite_loc_conf_t; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
20 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
21 |
485 | 22 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf); |
23 static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, | |
487 | 24 void *parent, void *child); |
681 | 25 static ngx_int_t ngx_http_rewrite_init(ngx_conf_t *cf); |
485 | 26 static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
27 static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, | |
487 | 28 void *conf); |
527 | 29 static char *ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, |
30 void *conf); | |
485 | 31 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, |
487 | 32 void *conf); |
501 | 33 static char * ngx_http_rewrite_if_condition(ngx_conf_t *cf, |
34 ngx_http_rewrite_loc_conf_t *lcf); | |
35 static char *ngx_http_rewrite_variable(ngx_conf_t *cf, | |
36 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value); | |
509 | 37 static char *ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, |
501 | 38 void *conf); |
577 | 39 static char * ngx_http_rewrite_value(ngx_conf_t *cf, |
40 ngx_http_rewrite_loc_conf_t *lcf, ngx_str_t *value); | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
41 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
42 |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
43 static ngx_command_t ngx_http_rewrite_commands[] = { |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
44 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
45 { ngx_string("rewrite"), |
485 | 46 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF |
47 |NGX_CONF_TAKE23, | |
48 ngx_http_rewrite, | |
49 NGX_HTTP_LOC_CONF_OFFSET, | |
50 0, | |
51 NULL }, | |
52 | |
53 { ngx_string("return"), | |
54 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF | |
55 |NGX_CONF_TAKE1, | |
56 ngx_http_rewrite_return, | |
57 NGX_HTTP_LOC_CONF_OFFSET, | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
58 0, |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
59 NULL }, |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 |
527 | 61 { ngx_string("break"), |
62 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF | |
63 |NGX_CONF_NOARGS, | |
64 ngx_http_rewrite_break, | |
65 NGX_HTTP_LOC_CONF_OFFSET, | |
66 0, | |
67 NULL }, | |
68 | |
485 | 69 { ngx_string("if"), |
70 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE, | |
71 ngx_http_rewrite_if, | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
72 NGX_HTTP_LOC_CONF_OFFSET, |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
73 0, |
485 | 74 NULL }, |
75 | |
501 | 76 { ngx_string("set"), |
77 NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF | |
78 |NGX_CONF_TAKE2, | |
79 ngx_http_rewrite_set, | |
80 NGX_HTTP_LOC_CONF_OFFSET, | |
81 0, | |
82 NULL }, | |
83 | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
84 { ngx_string("rewrite_log"), |
635 | 85 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF |
86 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG, | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
87 ngx_conf_set_flag_slot, |
485 | 88 NGX_HTTP_LOC_CONF_OFFSET, |
89 offsetof(ngx_http_rewrite_loc_conf_t, log), | |
325
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
90 NULL }, |
7c3323909107
nginx-0.0.3-2004-04-23-20:50:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
91 |
635 | 92 { ngx_string("uninitialized_variable_warn"), |
93 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF | |
94 |NGX_HTTP_LIF_CONF|NGX_CONF_FLAG, | |
95 ngx_conf_set_flag_slot, | |
96 NGX_HTTP_LOC_CONF_OFFSET, | |
97 offsetof(ngx_http_rewrite_loc_conf_t, uninitialized_variable_warn), | |
98 NULL }, | |
99 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
100 ngx_null_command |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
101 }; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
102 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
103 |
667 | 104 static ngx_http_module_t ngx_http_rewrite_module_ctx = { |
509 | 105 NULL, /* preconfiguration */ |
681 | 106 ngx_http_rewrite_init, /* postconfiguration */ |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
107 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
108 NULL, /* create main configuration */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
109 NULL, /* init main configuration */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
110 |
485 | 111 NULL, /* create server configuration */ |
112 NULL, /* merge server configuration */ | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
113 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
114 ngx_http_rewrite_create_loc_conf, /* create location configration */ |
485 | 115 ngx_http_rewrite_merge_loc_conf /* merge location configration */ |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
116 }; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
117 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
118 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
119 ngx_module_t ngx_http_rewrite_module = { |
509 | 120 NGX_MODULE_V1, |
577 | 121 &ngx_http_rewrite_module_ctx, /* module context */ |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
122 ngx_http_rewrite_commands, /* module directives */ |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
123 NGX_HTTP_MODULE, /* module type */ |
541 | 124 NULL, /* init master */ |
681 | 125 NULL, /* init module */ |
541 | 126 NULL, /* init process */ |
127 NULL, /* init thread */ | |
128 NULL, /* exit thread */ | |
129 NULL, /* exit process */ | |
130 NULL, /* exit master */ | |
131 NGX_MODULE_V1_PADDING | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
132 }; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
133 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
134 |
487 | 135 static ngx_int_t |
136 ngx_http_rewrite_handler(ngx_http_request_t *r) | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
137 { |
509 | 138 ngx_http_script_code_pt code; |
139 ngx_http_script_engine_t *e; | |
635 | 140 ngx_http_rewrite_loc_conf_t *rlcf; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
141 |
635 | 142 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module); |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
143 |
635 | 144 if (rlcf->codes == NULL) { |
485 | 145 return NGX_DECLINED; |
146 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
147 |
509 | 148 e = ngx_pcalloc(r->pool, sizeof(ngx_http_script_engine_t)); |
501 | 149 if (e == NULL) { |
485 | 150 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
151 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
152 |
509 | 153 e->sp = ngx_pcalloc(r->pool, |
635 | 154 rlcf->stack_size * sizeof(ngx_http_variable_value_t)); |
485 | 155 if (e->sp == NULL) { |
156 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
157 } | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
158 |
635 | 159 e->ip = rlcf->codes->elts; |
509 | 160 e->request = r; |
485 | 161 e->quote = 1; |
635 | 162 e->log = rlcf->log; |
485 | 163 e->status = NGX_DECLINED; |
164 | |
165 while (*(uintptr_t *) e->ip) { | |
509 | 166 code = *(ngx_http_script_code_pt *) e->ip; |
485 | 167 code(e); |
168 } | |
169 | |
739
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
170 if (e->status == NGX_DECLINED) { |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
171 return NGX_DECLINED; |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
172 } |
9f983de19a1d
after redirecting by error_page any rewrite directive will return this code,
Igor Sysoev <igor@sysoev.ru>
parents:
726
diff
changeset
|
173 |
726
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
174 if (r->err_status == 0) { |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
175 return e->status; |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
176 } |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
177 |
7b71936d5299
fix: "return" always overrode "error_page" response code
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
178 return r->err_status; |
485 | 179 } |
180 | |
181 | |
573 | 182 static ngx_int_t |
183 ngx_http_rewrite_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
184 uintptr_t data) | |
501 | 185 { |
635 | 186 ngx_http_variable_t *var; |
187 ngx_http_core_main_conf_t *cmcf; | |
188 ngx_http_rewrite_loc_conf_t *rlcf; | |
189 | |
190 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_rewrite_module); | |
191 | |
192 if (rlcf->uninitialized_variable_warn == 0) { | |
193 *v = ngx_http_variable_null_value; | |
194 return NGX_OK; | |
195 } | |
501 | 196 |
197 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
198 | |
199 var = cmcf->variables.elts; | |
200 | |
201 /* | |
202 * the ngx_http_rewrite_module sets variables directly in r->variables, | |
519 | 203 * and they should be handled by ngx_http_get_indexed_variable(), |
501 | 204 * so the handler is called only if the variable is not initialized |
205 */ | |
206 | |
635 | 207 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, |
501 | 208 "using uninitialized \"%V\" variable", &var[data].name); |
209 | |
577 | 210 *v = ngx_http_variable_null_value; |
573 | 211 |
212 return NGX_OK; | |
501 | 213 } |
214 | |
215 | |
487 | 216 static void * |
217 ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf) | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
218 { |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
219 ngx_http_rewrite_loc_conf_t *conf; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
220 |
501 | 221 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rewrite_loc_conf_t)); |
222 if (conf == NULL) { | |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2547
diff
changeset
|
223 return NULL; |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
224 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
225 |
485 | 226 conf->stack_size = NGX_CONF_UNSET_UINT; |
227 conf->log = NGX_CONF_UNSET; | |
635 | 228 conf->uninitialized_variable_warn = NGX_CONF_UNSET; |
485 | 229 |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
230 return conf; |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
231 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
232 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
233 |
487 | 234 static char * |
235 ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
236 { |
485 | 237 ngx_http_rewrite_loc_conf_t *prev = parent; |
238 ngx_http_rewrite_loc_conf_t *conf = child; | |
239 | |
509 | 240 uintptr_t *code; |
485 | 241 |
242 ngx_conf_merge_value(conf->log, prev->log, 0); | |
635 | 243 ngx_conf_merge_value(conf->uninitialized_variable_warn, |
244 prev->uninitialized_variable_warn, 1); | |
663 | 245 ngx_conf_merge_uint_value(conf->stack_size, prev->stack_size, 10); |
485 | 246 |
247 if (conf->codes == NULL) { | |
248 return NGX_CONF_OK; | |
249 } | |
250 | |
251 if (conf->codes == prev->codes) { | |
252 return NGX_CONF_OK; | |
253 } | |
254 | |
501 | 255 code = ngx_array_push_n(conf->codes, sizeof(uintptr_t)); |
256 if (code == NULL) { | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
257 return NGX_CONF_ERROR; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
258 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
259 |
485 | 260 *code = (uintptr_t) NULL; |
261 | |
262 return NGX_CONF_OK; | |
263 } | |
264 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
265 |
509 | 266 static ngx_int_t |
681 | 267 ngx_http_rewrite_init(ngx_conf_t *cf) |
577 | 268 { |
509 | 269 ngx_http_handler_pt *h; |
270 ngx_http_core_main_conf_t *cmcf; | |
271 | |
681 | 272 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
509 | 273 |
573 | 274 h = ngx_array_push(&cmcf->phases[NGX_HTTP_SERVER_REWRITE_PHASE].handlers); |
275 if (h == NULL) { | |
276 return NGX_ERROR; | |
277 } | |
278 | |
279 *h = ngx_http_rewrite_handler; | |
280 | |
509 | 281 h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers); |
282 if (h == NULL) { | |
283 return NGX_ERROR; | |
284 } | |
285 | |
286 *h = ngx_http_rewrite_handler; | |
287 | |
288 return NGX_OK; | |
577 | 289 } |
509 | 290 |
291 | |
487 | 292 static char * |
293 ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 294 { |
577 | 295 ngx_http_rewrite_loc_conf_t *lcf = conf; |
296 | |
3325 | 297 ngx_str_t *value; |
509 | 298 ngx_uint_t last; |
3325 | 299 ngx_regex_compile_t rc; |
509 | 300 ngx_http_script_code_pt *code; |
301 ngx_http_script_compile_t sc; | |
302 ngx_http_script_regex_code_t *regex; | |
303 ngx_http_script_regex_end_code_t *regex_end; | |
304 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
485 | 305 |
509 | 306 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, |
307 sizeof(ngx_http_script_regex_code_t)); | |
485 | 308 if (regex == NULL) { |
309 return NGX_CONF_ERROR; | |
310 } | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
311 |
509 | 312 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
313 | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
314 value = cf->args->elts; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
315 |
3325 | 316 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); |
317 | |
318 rc.pattern = value[1]; | |
319 rc.err.len = NGX_MAX_CONF_ERRSTR; | |
320 rc.err.data = errstr; | |
485 | 321 |
322 /* TODO: NGX_REGEX_CASELESS */ | |
323 | |
3325 | 324 regex->regex = ngx_http_regex_compile(cf, &rc); |
485 | 325 if (regex->regex == NULL) { |
326 return NGX_CONF_ERROR; | |
327 } | |
328 | |
509 | 329 regex->code = ngx_http_script_regex_start_code; |
485 | 330 regex->uri = 1; |
331 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
|
332 |
509 | 333 if (value[2].data[value[2].len - 1] == '?') { |
334 | |
335 /* the last "?" drops the original arguments */ | |
336 value[2].len--; | |
337 | |
338 } else { | |
339 regex->add_args = 1; | |
340 } | |
341 | |
485 | 342 last = 0; |
343 | |
344 if (ngx_strncmp(value[2].data, "http://", sizeof("http://") - 1) == 0) { | |
345 regex->status = NGX_HTTP_MOVED_TEMPORARILY; | |
346 regex->redirect = 1; | |
347 last = 1; | |
348 } | |
349 | |
2054
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
350 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
|
351 regex->status = NGX_HTTP_MOVED_TEMPORARILY; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
352 regex->redirect = 1; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
353 last = 1; |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
354 } |
989176d3ffc5
auto detect https redirect in rewrite
Igor Sysoev <igor@sysoev.ru>
parents:
2027
diff
changeset
|
355 |
485 | 356 if (cf->args->nelts == 4) { |
357 if (ngx_strcmp(value[3].data, "last") == 0) { | |
358 last = 1; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
359 |
487 | 360 } else if (ngx_strcmp(value[3].data, "break") == 0) { |
361 regex->break_cycle = 1; | |
362 last = 1; | |
363 | |
485 | 364 } else if (ngx_strcmp(value[3].data, "redirect") == 0) { |
365 regex->status = NGX_HTTP_MOVED_TEMPORARILY; | |
366 regex->redirect = 1; | |
367 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
368 |
485 | 369 } else if (ngx_strcmp(value[3].data, "permanent") == 0) { |
370 regex->status = NGX_HTTP_MOVED_PERMANENTLY; | |
371 regex->redirect = 1; | |
372 last = 1; | |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
373 |
485 | 374 } else { |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
375 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
461 | 376 "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
|
377 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
|
378 } |
485 | 379 } |
326
8733703a37f3
nginx-0.0.3-2004-04-26-00:13:21 import
Igor Sysoev <igor@sysoev.ru>
parents:
325
diff
changeset
|
380 |
509 | 381 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); |
485 | 382 |
509 | 383 sc.cf = cf; |
384 sc.source = &value[2]; | |
385 sc.lengths = ®ex->lengths; | |
386 sc.values = &lcf->codes; | |
387 sc.variables = ngx_http_script_variables_count(&value[2]); | |
388 sc.main = regex; | |
389 sc.complete_lengths = 1; | |
390 sc.compile_args = !regex->redirect; | |
485 | 391 |
509 | 392 if (ngx_http_script_compile(&sc) != NGX_OK) { |
393 return NGX_CONF_ERROR; | |
394 } | |
485 | 395 |
509 | 396 regex = sc.main; |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
397 |
509 | 398 regex->size = sc.size; |
399 regex->args = sc.args; | |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
400 |
641 | 401 if (sc.variables == 0 && !sc.dup_capture) { |
509 | 402 regex->lengths = NULL; |
485 | 403 } |
404 | |
509 | 405 regex_end = ngx_http_script_add_code(lcf->codes, |
406 sizeof(ngx_http_script_regex_end_code_t), | |
407 ®ex); | |
485 | 408 if (regex_end == NULL) { |
409 return NGX_CONF_ERROR; | |
410 } | |
411 | |
509 | 412 regex_end->code = ngx_http_script_regex_end_code; |
485 | 413 regex_end->uri = regex->uri; |
414 regex_end->args = regex->args; | |
509 | 415 regex_end->add_args = regex->add_args; |
485 | 416 regex_end->redirect = regex->redirect; |
417 | |
418 if (last) { | |
517 | 419 code = ngx_http_script_add_code(lcf->codes, sizeof(uintptr_t), ®ex); |
485 | 420 if (code == NULL) { |
421 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
|
422 } |
216
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
423 |
3184 | 424 *code = NULL; |
485 | 425 } |
426 | |
427 regex->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
428 - (u_char *) regex; | |
429 | |
430 return NGX_CONF_OK; | |
431 } | |
432 | |
433 | |
487 | 434 static char * |
435 ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
485 | 436 { |
577 | 437 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
|
438 |
509 | 439 ngx_str_t *value; |
440 ngx_http_script_return_code_t *ret; | |
485 | 441 |
509 | 442 ret = ngx_http_script_start_code(cf->pool, &lcf->codes, |
443 sizeof(ngx_http_script_return_code_t)); | |
485 | 444 if (ret == NULL) { |
445 return NGX_CONF_ERROR; | |
446 } | |
447 | |
448 value = cf->args->elts; | |
449 | |
509 | 450 ret->code = ngx_http_script_return_code; |
485 | 451 ret->null = (uintptr_t) NULL; |
452 | |
453 ret->status = ngx_atoi(value[1].data, value[1].len); | |
454 | |
455 if (ret->status == (uintptr_t) NGX_ERROR) { | |
456 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
|
457 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
458 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
459 return NGX_CONF_OK; |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
460 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
461 |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
462 |
487 | 463 static char * |
527 | 464 ngx_http_rewrite_break(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
465 { | |
466 ngx_http_rewrite_loc_conf_t *lcf = conf; | |
467 | |
468 ngx_http_script_code_pt *code; | |
469 | |
470 code = ngx_http_script_start_code(cf->pool, &lcf->codes, sizeof(uintptr_t)); | |
471 if (code == NULL) { | |
472 return NGX_CONF_ERROR; | |
473 } | |
474 | |
475 *code = ngx_http_script_break_code; | |
476 | |
477 return NGX_CONF_OK; | |
478 } | |
479 | |
480 | |
481 static char * | |
487 | 482 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
|
483 { |
577 | 484 ngx_http_rewrite_loc_conf_t *lcf = conf; |
485 | 485 |
486 void *mconf; | |
487 char *rv; | |
488 u_char *elts; | |
489 ngx_uint_t i; | |
490 ngx_conf_t save; | |
491 ngx_http_module_t *module; | |
492 ngx_http_conf_ctx_t *ctx, *pctx; | |
2027 | 493 ngx_http_core_loc_conf_t *clcf, *pclcf; |
509 | 494 ngx_http_script_if_code_t *if_code; |
485 | 495 ngx_http_rewrite_loc_conf_t *nlcf; |
496 | |
501 | 497 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)); |
498 if (ctx == NULL) { | |
485 | 499 return NGX_CONF_ERROR; |
500 } | |
501 | |
502 pctx = cf->ctx; | |
503 ctx->main_conf = pctx->main_conf; | |
577 | 504 ctx->srv_conf = pctx->srv_conf; |
485 | 505 |
506 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module); | |
507 if (ctx->loc_conf == NULL) { | |
508 return NGX_CONF_ERROR; | |
509 } | |
510 | |
511 for (i = 0; ngx_modules[i]; i++) { | |
512 if (ngx_modules[i]->type != NGX_HTTP_MODULE) { | |
513 continue; | |
514 } | |
515 | |
516 module = ngx_modules[i]->ctx; | |
577 | 517 |
485 | 518 if (module->create_loc_conf) { |
519 | |
501 | 520 mconf = module->create_loc_conf(cf); |
521 if (mconf == NULL) { | |
485 | 522 return NGX_CONF_ERROR; |
523 } | |
524 | |
525 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf; | |
526 } | |
527 } | |
528 | |
529 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index]; | |
530 | |
531 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index]; | |
532 clcf->loc_conf = ctx->loc_conf; | |
533 clcf->name = pclcf->name; | |
534 clcf->noname = 1; | |
535 | |
2027 | 536 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) { |
485 | 537 return NGX_CONF_ERROR; |
538 } | |
539 | |
501 | 540 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) { |
485 | 541 return NGX_CONF_ERROR; |
542 } | |
543 | |
509 | 544 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t)); |
485 | 545 if (if_code == NULL) { |
3001 | 546 return NGX_CONF_ERROR; |
485 | 547 } |
548 | |
509 | 549 if_code->code = ngx_http_script_if_code; |
485 | 550 |
551 elts = lcf->codes->elts; | |
552 | |
553 | |
631 | 554 /* the inner directives must be compiled to the same code array */ |
485 | 555 |
556 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index]; | |
557 nlcf->codes = lcf->codes; | |
558 | |
559 | |
560 save = *cf; | |
561 cf->ctx = ctx; | |
562 | |
563 if (pclcf->name.len == 0) { | |
564 if_code->loc_conf = NULL; | |
565 cf->cmd_type = NGX_HTTP_SIF_CONF; | |
566 | |
567 } else { | |
568 if_code->loc_conf = ctx->loc_conf; | |
569 cf->cmd_type = NGX_HTTP_LIF_CONF; | |
570 } | |
571 | |
572 rv = ngx_conf_parse(cf, NULL); | |
573 | |
574 *cf = save; | |
575 | |
576 if (rv != NGX_CONF_OK) { | |
577 return rv; | |
578 } | |
579 | |
580 | |
581 if (elts != lcf->codes->elts) { | |
509 | 582 if_code = (ngx_http_script_if_code_t *) |
485 | 583 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts)); |
584 } | |
585 | |
586 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
587 - (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
|
588 |
555 | 589 /* the code array belong to parent block */ |
590 | |
591 nlcf->codes = NULL; | |
592 | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
593 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
|
594 } |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
595 |
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
596 |
487 | 597 static char * |
501 | 598 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) |
599 { | |
631 | 600 u_char *p; |
601 size_t len; | |
3325 | 602 ngx_str_t *value; |
603 ngx_uint_t cur, last; | |
604 ngx_regex_compile_t rc; | |
577 | 605 ngx_http_script_code_pt *code; |
631 | 606 ngx_http_script_file_code_t *fop; |
509 | 607 ngx_http_script_regex_code_t *regex; |
608 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
501 | 609 |
610 value = cf->args->elts; | |
611 last = cf->args->nelts - 1; | |
612 | |
613 if (value[1].len < 1 || value[1].data[0] != '(') { | |
614 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
615 "invalid condition \"%V\"", &value[1]); | |
616 return NGX_CONF_ERROR; | |
617 } | |
618 | |
619 if (value[1].len == 1) { | |
620 cur = 2; | |
621 | |
622 } else { | |
623 cur = 1; | |
624 value[1].len--; | |
625 value[1].data++; | |
626 } | |
627 | |
628 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') { | |
629 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
630 "invalid condition \"%V\"", &value[last]); | |
631 return NGX_CONF_ERROR; | |
632 } | |
633 | |
634 if (value[last].len == 1) { | |
635 last--; | |
636 | |
637 } else { | |
638 value[last].len--; | |
639 value[last].data[value[last].len] = '\0'; | |
640 } | |
641 | |
631 | 642 len = value[cur].len; |
643 p = value[cur].data; | |
644 | |
645 if (len > 1 && p[0] == '$') { | |
501 | 646 |
647 if (cur != last && cur + 2 != last) { | |
648 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
631 | 649 "invalid condition \"%V\"", &value[cur]); |
501 | 650 return NGX_CONF_ERROR; |
651 } | |
652 | |
577 | 653 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) { |
501 | 654 return NGX_CONF_ERROR; |
655 } | |
656 | |
657 if (cur == last) { | |
658 return NGX_CONF_OK; | |
659 } | |
660 | |
661 cur++; | |
662 | |
631 | 663 len = value[cur].len; |
664 p = value[cur].data; | |
665 | |
666 if (len == 1 && p[0] == '=') { | |
577 | 667 |
668 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
669 return NGX_CONF_ERROR; | |
670 } | |
671 | |
672 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
673 sizeof(uintptr_t)); | |
674 if (code == NULL) { | |
675 return NGX_CONF_ERROR; | |
676 } | |
677 | |
678 *code = ngx_http_script_equal_code; | |
679 | |
680 return NGX_CONF_OK; | |
501 | 681 } |
682 | |
631 | 683 if (len == 2 && p[0] == '!' && p[1] == '=') { |
684 | |
577 | 685 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { |
686 return NGX_CONF_ERROR; | |
687 } | |
688 | |
689 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
690 sizeof(uintptr_t)); | |
691 if (code == NULL) { | |
692 return NGX_CONF_ERROR; | |
693 } | |
694 | |
695 *code = ngx_http_script_not_equal_code; | |
696 return NGX_CONF_OK; | |
501 | 697 } |
698 | |
631 | 699 if ((len == 1 && p[0] == '~') |
700 || (len == 2 && p[0] == '~' && p[1] == '*') | |
701 || (len == 2 && p[0] == '!' && p[1] == '~') | |
702 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*')) | |
577 | 703 { |
704 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
705 sizeof(ngx_http_script_regex_code_t)); | |
706 if (regex == NULL) { | |
707 return NGX_CONF_ERROR; | |
708 } | |
509 | 709 |
577 | 710 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
711 | |
3325 | 712 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); |
577 | 713 |
3325 | 714 rc.pattern = value[last]; |
715 rc.options = (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0; | |
716 rc.err.len = NGX_MAX_CONF_ERRSTR; | |
717 rc.err.data = errstr; | |
501 | 718 |
3325 | 719 regex->regex = ngx_http_regex_compile(cf, &rc); |
577 | 720 if (regex->regex == NULL) { |
721 return NGX_CONF_ERROR; | |
722 } | |
723 | |
724 regex->code = ngx_http_script_regex_start_code; | |
725 regex->next = sizeof(ngx_http_script_regex_code_t); | |
726 regex->test = 1; | |
631 | 727 if (p[0] == '!') { |
728 regex->negative_test = 1; | |
729 } | |
577 | 730 regex->name = value[last]; |
501 | 731 |
577 | 732 return NGX_CONF_OK; |
501 | 733 } |
734 | |
577 | 735 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
736 "unexpected \"%V\" in condition", &value[cur]); | |
737 return NGX_CONF_ERROR; | |
631 | 738 |
739 } else if ((len == 2 && p[0] == '-') | |
740 || (len == 3 && p[0] == '!' && p[1] == '-')) | |
741 { | |
742 if (cur + 1 != last) { | |
743 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
744 "invalid condition \"%V\"", &value[cur]); | |
745 return NGX_CONF_ERROR; | |
746 } | |
747 | |
748 value[last].data[value[last].len] = '\0'; | |
749 value[last].len++; | |
750 | |
751 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
752 return NGX_CONF_ERROR; | |
753 } | |
754 | |
755 fop = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
756 sizeof(ngx_http_script_file_code_t)); | |
757 if (fop == NULL) { | |
758 return NGX_CONF_ERROR; | |
759 } | |
760 | |
761 fop->code = ngx_http_script_file_code; | |
762 | |
763 if (p[1] == 'f') { | |
764 fop->op = ngx_http_script_file_plain; | |
765 return NGX_CONF_OK; | |
766 } | |
767 | |
669 | 768 if (p[1] == 'd') { |
769 fop->op = ngx_http_script_file_dir; | |
770 return NGX_CONF_OK; | |
771 } | |
772 | |
773 if (p[1] == 'e') { | |
774 fop->op = ngx_http_script_file_exists; | |
775 return NGX_CONF_OK; | |
776 } | |
777 | |
778 if (p[1] == 'x') { | |
779 fop->op = ngx_http_script_file_exec; | |
780 return NGX_CONF_OK; | |
781 } | |
782 | |
631 | 783 if (p[0] == '!') { |
784 if (p[2] == 'f') { | |
785 fop->op = ngx_http_script_file_not_plain; | |
786 return NGX_CONF_OK; | |
787 } | |
669 | 788 |
789 if (p[2] == 'd') { | |
790 fop->op = ngx_http_script_file_not_dir; | |
791 return NGX_CONF_OK; | |
792 } | |
793 | |
794 if (p[2] == 'e') { | |
795 fop->op = ngx_http_script_file_not_exists; | |
796 return NGX_CONF_OK; | |
797 } | |
798 | |
799 if (p[2] == 'x') { | |
800 fop->op = ngx_http_script_file_not_exec; | |
801 return NGX_CONF_OK; | |
802 } | |
631 | 803 } |
804 | |
805 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
806 "invalid condition \"%V\"", &value[cur]); | |
807 return NGX_CONF_ERROR; | |
501 | 808 } |
809 | |
810 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
811 "invalid condition \"%V\"", &value[cur]); | |
812 | |
813 return NGX_CONF_ERROR; | |
814 } | |
815 | |
816 | |
817 static char * | |
818 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
819 ngx_str_t *value) | |
820 { | |
509 | 821 ngx_int_t index; |
822 ngx_http_script_var_code_t *var_code; | |
501 | 823 |
824 value->len--; | |
825 value->data++; | |
826 | |
577 | 827 index = ngx_http_get_variable_index(cf, value); |
501 | 828 |
577 | 829 if (index == NGX_ERROR) { |
830 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
|
831 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
832 |
577 | 833 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes, |
834 sizeof(ngx_http_script_var_code_t)); | |
835 if (var_code == NULL) { | |
836 return NGX_CONF_ERROR; | |
485 | 837 } |
838 | |
577 | 839 var_code->code = ngx_http_script_var_code; |
840 var_code->index = index; | |
485 | 841 |
842 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
|
843 } |
485 | 844 |
845 | |
501 | 846 static char * |
847 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
848 { | |
577 | 849 ngx_http_rewrite_loc_conf_t *lcf = conf; |
501 | 850 |
637 | 851 ngx_int_t index; |
852 ngx_str_t *value; | |
853 ngx_http_variable_t *v; | |
854 ngx_http_script_var_code_t *vcode; | |
855 ngx_http_script_var_handler_code_t *vhcode; | |
501 | 856 |
857 value = cf->args->elts; | |
858 | |
859 if (value[1].data[0] != '$') { | |
860 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
861 "invalid variable name \"%V\"", &value[1]); | |
862 return NGX_CONF_ERROR; | |
863 } | |
864 | |
865 value[1].len--; | |
866 value[1].data++; | |
867 | |
1565 | 868 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
501 | 869 if (v == NULL) { |
870 return NGX_CONF_ERROR; | |
871 } | |
872 | |
509 | 873 index = ngx_http_get_variable_index(cf, &value[1]); |
874 if (index == NGX_ERROR) { | |
875 return NGX_CONF_ERROR; | |
876 } | |
501 | 877 |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
878 if (v->get_handler == NULL |
1192 | 879 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0 |
880 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0 | |
881 && 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
|
882 { |
637 | 883 v->get_handler = ngx_http_rewrite_var; |
555 | 884 v->data = index; |
885 } | |
509 | 886 |
577 | 887 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) { |
888 return NGX_CONF_ERROR; | |
515 | 889 } |
501 | 890 |
637 | 891 if (v->set_handler) { |
892 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
893 sizeof(ngx_http_script_var_handler_code_t)); | |
894 if (vhcode == NULL) { | |
895 return NGX_CONF_ERROR; | |
896 } | |
897 | |
898 vhcode->code = ngx_http_script_var_set_handler_code; | |
899 vhcode->handler = v->set_handler; | |
900 vhcode->data = v->data; | |
901 | |
902 return NGX_CONF_OK; | |
903 } | |
904 | |
905 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
906 sizeof(ngx_http_script_var_code_t)); | |
907 if (vcode == NULL) { | |
501 | 908 return NGX_CONF_ERROR; |
909 } | |
910 | |
637 | 911 vcode->code = ngx_http_script_set_var_code; |
912 vcode->index = (uintptr_t) index; | |
501 | 913 |
914 return NGX_CONF_OK; | |
915 } | |
577 | 916 |
917 | |
918 static char * | |
919 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
920 ngx_str_t *value) | |
921 { | |
922 ngx_int_t n; | |
923 ngx_http_script_compile_t sc; | |
924 ngx_http_script_value_code_t *val; | |
925 ngx_http_script_complex_value_code_t *complex; | |
926 | |
927 n = ngx_http_script_variables_count(value); | |
928 | |
929 if (n == 0) { | |
930 val = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
931 sizeof(ngx_http_script_value_code_t)); | |
932 if (val == NULL) { | |
933 return NGX_CONF_ERROR; | |
934 } | |
935 | |
936 n = ngx_atoi(value->data, value->len); | |
937 | |
938 if (n == NGX_ERROR) { | |
939 n = 0; | |
940 } | |
941 | |
942 val->code = ngx_http_script_value_code; | |
943 val->value = (uintptr_t) n; | |
944 val->text_len = (uintptr_t) value->len; | |
945 val->text_data = (uintptr_t) value->data; | |
946 | |
947 return NGX_CONF_OK; | |
948 } | |
949 | |
950 complex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
951 sizeof(ngx_http_script_complex_value_code_t)); | |
952 if (complex == NULL) { | |
953 return NGX_CONF_ERROR; | |
954 } | |
955 | |
956 complex->code = ngx_http_script_complex_value_code; | |
957 complex->lengths = NULL; | |
958 | |
959 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
960 | |
961 sc.cf = cf; | |
962 sc.source = value; | |
963 sc.lengths = &complex->lengths; | |
964 sc.values = &lcf->codes; | |
965 sc.variables = n; | |
966 sc.complete_lengths = 1; | |
967 | |
968 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
969 return NGX_CONF_ERROR; | |
970 } | |
971 | |
972 return NGX_CONF_OK; | |
973 } |