Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_rewrite_module.c @ 1678:d24280472a9f
log how big fastcgi record
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sat, 01 Dec 2007 14:23:53 +0000 |
parents | 4c43e25d11ea |
children | f321b59ae0e9 dc52b3f7591e |
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 | |
1236
5e4d3b5d7ad0
use pointer to an array instead of array for inclusive locations inside location
Igor Sysoev <igor@sysoev.ru>
parents:
1192
diff
changeset
|
570 if (pclcf->locations == NULL) { |
5e4d3b5d7ad0
use pointer to an array instead of array for inclusive locations inside location
Igor Sysoev <igor@sysoev.ru>
parents:
1192
diff
changeset
|
571 pclcf->locations = ngx_array_create(cf->pool, 2, sizeof(void *)); |
5e4d3b5d7ad0
use pointer to an array instead of array for inclusive locations inside location
Igor Sysoev <igor@sysoev.ru>
parents:
1192
diff
changeset
|
572 if (pclcf->locations == NULL) { |
485 | 573 return NGX_CONF_ERROR; |
574 } | |
575 } | |
576 | |
1236
5e4d3b5d7ad0
use pointer to an array instead of array for inclusive locations inside location
Igor Sysoev <igor@sysoev.ru>
parents:
1192
diff
changeset
|
577 clcfp = ngx_array_push(pclcf->locations); |
501 | 578 if (clcfp == NULL) { |
485 | 579 return NGX_CONF_ERROR; |
580 } | |
581 | |
582 *clcfp = clcf; | |
583 | |
584 | |
501 | 585 if (ngx_http_rewrite_if_condition(cf, lcf) != NGX_CONF_OK) { |
485 | 586 return NGX_CONF_ERROR; |
587 } | |
588 | |
509 | 589 if_code = ngx_array_push_n(lcf->codes, sizeof(ngx_http_script_if_code_t)); |
485 | 590 if (if_code == NULL) { |
591 return NULL; | |
592 } | |
593 | |
509 | 594 if_code->code = ngx_http_script_if_code; |
485 | 595 |
596 elts = lcf->codes->elts; | |
597 | |
598 | |
631 | 599 /* the inner directives must be compiled to the same code array */ |
485 | 600 |
601 nlcf = ctx->loc_conf[ngx_http_rewrite_module.ctx_index]; | |
602 nlcf->codes = lcf->codes; | |
603 | |
604 | |
605 save = *cf; | |
606 cf->ctx = ctx; | |
607 | |
608 if (pclcf->name.len == 0) { | |
609 if_code->loc_conf = NULL; | |
610 cf->cmd_type = NGX_HTTP_SIF_CONF; | |
611 | |
612 } else { | |
613 if_code->loc_conf = ctx->loc_conf; | |
614 cf->cmd_type = NGX_HTTP_LIF_CONF; | |
615 } | |
616 | |
617 rv = ngx_conf_parse(cf, NULL); | |
618 | |
619 *cf = save; | |
620 | |
621 if (rv != NGX_CONF_OK) { | |
622 return rv; | |
623 } | |
624 | |
625 | |
517 | 626 if (lcf->captures < nlcf->captures) { |
627 lcf->captures = nlcf->captures; | |
628 } | |
629 | |
630 | |
485 | 631 if (elts != lcf->codes->elts) { |
509 | 632 if_code = (ngx_http_script_if_code_t *) |
485 | 633 ((u_char *) if_code + ((u_char *) lcf->codes->elts - elts)); |
634 } | |
635 | |
636 if_code->next = (u_char *) lcf->codes->elts + lcf->codes->nelts | |
637 - (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
|
638 |
555 | 639 /* the code array belong to parent block */ |
640 | |
641 nlcf->codes = NULL; | |
642 | |
437
470270fa84d2
nginx-0.0.12-2004-09-23-20:39:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
396
diff
changeset
|
643 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
|
644 } |
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 |
487 | 647 static char * |
501 | 648 ngx_http_rewrite_if_condition(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf) |
649 { | |
631 | 650 u_char *p; |
651 size_t len; | |
509 | 652 ngx_str_t *value, err; |
515 | 653 ngx_uint_t cur, last, n; |
577 | 654 ngx_http_script_code_pt *code; |
631 | 655 ngx_http_script_file_code_t *fop; |
509 | 656 ngx_http_script_regex_code_t *regex; |
657 u_char errstr[NGX_MAX_CONF_ERRSTR]; | |
501 | 658 |
659 value = cf->args->elts; | |
660 last = cf->args->nelts - 1; | |
661 | |
662 if (value[1].len < 1 || value[1].data[0] != '(') { | |
663 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
664 "invalid condition \"%V\"", &value[1]); | |
665 return NGX_CONF_ERROR; | |
666 } | |
667 | |
668 if (value[1].len == 1) { | |
669 cur = 2; | |
670 | |
671 } else { | |
672 cur = 1; | |
673 value[1].len--; | |
674 value[1].data++; | |
675 } | |
676 | |
677 if (value[last].len < 1 || value[last].data[value[last].len - 1] != ')') { | |
678 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
679 "invalid condition \"%V\"", &value[last]); | |
680 return NGX_CONF_ERROR; | |
681 } | |
682 | |
683 if (value[last].len == 1) { | |
684 last--; | |
685 | |
686 } else { | |
687 value[last].len--; | |
688 value[last].data[value[last].len] = '\0'; | |
689 } | |
690 | |
631 | 691 len = value[cur].len; |
692 p = value[cur].data; | |
693 | |
694 if (len > 1 && p[0] == '$') { | |
501 | 695 |
696 if (cur != last && cur + 2 != last) { | |
697 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
631 | 698 "invalid condition \"%V\"", &value[cur]); |
501 | 699 return NGX_CONF_ERROR; |
700 } | |
701 | |
577 | 702 if (ngx_http_rewrite_variable(cf, lcf, &value[cur]) != NGX_CONF_OK) { |
501 | 703 return NGX_CONF_ERROR; |
704 } | |
705 | |
706 if (cur == last) { | |
707 return NGX_CONF_OK; | |
708 } | |
709 | |
710 cur++; | |
711 | |
631 | 712 len = value[cur].len; |
713 p = value[cur].data; | |
714 | |
715 if (len == 1 && p[0] == '=') { | |
577 | 716 |
717 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
718 return NGX_CONF_ERROR; | |
719 } | |
720 | |
721 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
722 sizeof(uintptr_t)); | |
723 if (code == NULL) { | |
724 return NGX_CONF_ERROR; | |
725 } | |
726 | |
727 *code = ngx_http_script_equal_code; | |
728 | |
729 return NGX_CONF_OK; | |
501 | 730 } |
731 | |
631 | 732 if (len == 2 && p[0] == '!' && p[1] == '=') { |
733 | |
577 | 734 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { |
735 return NGX_CONF_ERROR; | |
736 } | |
737 | |
738 code = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
739 sizeof(uintptr_t)); | |
740 if (code == NULL) { | |
741 return NGX_CONF_ERROR; | |
742 } | |
743 | |
744 *code = ngx_http_script_not_equal_code; | |
745 return NGX_CONF_OK; | |
501 | 746 } |
747 | |
631 | 748 if ((len == 1 && p[0] == '~') |
749 || (len == 2 && p[0] == '~' && p[1] == '*') | |
750 || (len == 2 && p[0] == '!' && p[1] == '~') | |
751 || (len == 3 && p[0] == '!' && p[1] == '~' && p[2] == '*')) | |
577 | 752 { |
753 regex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
754 sizeof(ngx_http_script_regex_code_t)); | |
755 if (regex == NULL) { | |
756 return NGX_CONF_ERROR; | |
757 } | |
509 | 758 |
577 | 759 ngx_memzero(regex, sizeof(ngx_http_script_regex_code_t)); |
760 | |
761 err.len = NGX_MAX_CONF_ERRSTR; | |
762 err.data = errstr; | |
763 | |
764 regex->regex = ngx_regex_compile(&value[last], | |
631 | 765 (p[len - 1] == '*') ? NGX_REGEX_CASELESS : 0, |
766 cf->pool, &err); | |
501 | 767 |
577 | 768 if (regex->regex == NULL) { |
769 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data); | |
770 return NGX_CONF_ERROR; | |
771 } | |
772 | |
773 regex->code = ngx_http_script_regex_start_code; | |
774 regex->next = sizeof(ngx_http_script_regex_code_t); | |
775 regex->test = 1; | |
631 | 776 if (p[0] == '!') { |
777 regex->negative_test = 1; | |
778 } | |
577 | 779 regex->name = value[last]; |
501 | 780 |
577 | 781 n = ngx_regex_capture_count(regex->regex); |
782 | |
783 if (n) { | |
784 regex->ncaptures = (n + 1) * 3; | |
785 | |
786 if (lcf->captures < regex->ncaptures) { | |
787 lcf->captures = regex->ncaptures; | |
788 } | |
789 } | |
790 | |
791 return NGX_CONF_OK; | |
501 | 792 } |
793 | |
577 | 794 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
795 "unexpected \"%V\" in condition", &value[cur]); | |
796 return NGX_CONF_ERROR; | |
631 | 797 |
798 } else if ((len == 2 && p[0] == '-') | |
799 || (len == 3 && p[0] == '!' && p[1] == '-')) | |
800 { | |
801 if (cur + 1 != last) { | |
802 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
803 "invalid condition \"%V\"", &value[cur]); | |
804 return NGX_CONF_ERROR; | |
805 } | |
806 | |
807 value[last].data[value[last].len] = '\0'; | |
808 value[last].len++; | |
809 | |
810 if (ngx_http_rewrite_value(cf, lcf, &value[last]) != NGX_CONF_OK) { | |
811 return NGX_CONF_ERROR; | |
812 } | |
813 | |
814 fop = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
815 sizeof(ngx_http_script_file_code_t)); | |
816 if (fop == NULL) { | |
817 return NGX_CONF_ERROR; | |
818 } | |
819 | |
820 fop->code = ngx_http_script_file_code; | |
821 | |
822 if (p[1] == 'f') { | |
823 fop->op = ngx_http_script_file_plain; | |
824 return NGX_CONF_OK; | |
825 } | |
826 | |
669 | 827 if (p[1] == 'd') { |
828 fop->op = ngx_http_script_file_dir; | |
829 return NGX_CONF_OK; | |
830 } | |
831 | |
832 if (p[1] == 'e') { | |
833 fop->op = ngx_http_script_file_exists; | |
834 return NGX_CONF_OK; | |
835 } | |
836 | |
837 if (p[1] == 'x') { | |
838 fop->op = ngx_http_script_file_exec; | |
839 return NGX_CONF_OK; | |
840 } | |
841 | |
631 | 842 if (p[0] == '!') { |
843 if (p[2] == 'f') { | |
844 fop->op = ngx_http_script_file_not_plain; | |
845 return NGX_CONF_OK; | |
846 } | |
669 | 847 |
848 if (p[2] == 'd') { | |
849 fop->op = ngx_http_script_file_not_dir; | |
850 return NGX_CONF_OK; | |
851 } | |
852 | |
853 if (p[2] == 'e') { | |
854 fop->op = ngx_http_script_file_not_exists; | |
855 return NGX_CONF_OK; | |
856 } | |
857 | |
858 if (p[2] == 'x') { | |
859 fop->op = ngx_http_script_file_not_exec; | |
860 return NGX_CONF_OK; | |
861 } | |
631 | 862 } |
863 | |
864 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
865 "invalid condition \"%V\"", &value[cur]); | |
866 return NGX_CONF_ERROR; | |
501 | 867 } |
868 | |
869 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
870 "invalid condition \"%V\"", &value[cur]); | |
871 | |
872 return NGX_CONF_ERROR; | |
873 } | |
874 | |
875 | |
876 static char * | |
877 ngx_http_rewrite_variable(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
878 ngx_str_t *value) | |
879 { | |
509 | 880 ngx_int_t index; |
881 ngx_http_script_var_code_t *var_code; | |
501 | 882 |
883 value->len--; | |
884 value->data++; | |
885 | |
577 | 886 index = ngx_http_get_variable_index(cf, value); |
501 | 887 |
577 | 888 if (index == NGX_ERROR) { |
889 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
|
890 } |
f1d0e5f09c1e
nginx-0.0.1-2003-12-25-23:26:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
891 |
577 | 892 var_code = ngx_http_script_start_code(cf->pool, &lcf->codes, |
893 sizeof(ngx_http_script_var_code_t)); | |
894 if (var_code == NULL) { | |
895 return NGX_CONF_ERROR; | |
485 | 896 } |
897 | |
577 | 898 var_code->code = ngx_http_script_var_code; |
899 var_code->index = index; | |
485 | 900 |
901 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
|
902 } |
485 | 903 |
904 | |
501 | 905 static char * |
906 ngx_http_rewrite_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
907 { | |
577 | 908 ngx_http_rewrite_loc_conf_t *lcf = conf; |
501 | 909 |
637 | 910 ngx_int_t index; |
911 ngx_str_t *value; | |
912 ngx_http_variable_t *v; | |
913 ngx_http_script_var_code_t *vcode; | |
914 ngx_http_script_var_handler_code_t *vhcode; | |
501 | 915 |
916 value = cf->args->elts; | |
917 | |
918 if (value[1].data[0] != '$') { | |
919 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
920 "invalid variable name \"%V\"", &value[1]); | |
921 return NGX_CONF_ERROR; | |
922 } | |
923 | |
924 value[1].len--; | |
925 value[1].data++; | |
926 | |
1565 | 927 v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE); |
501 | 928 if (v == NULL) { |
929 return NGX_CONF_ERROR; | |
930 } | |
931 | |
509 | 932 index = ngx_http_get_variable_index(cf, &value[1]); |
933 if (index == NGX_ERROR) { | |
934 return NGX_CONF_ERROR; | |
935 } | |
501 | 936 |
1189
5e1a8fe79c4b
allow to "set" $http_, $sent_http_, and $upstream_http_ variables
Igor Sysoev <igor@sysoev.ru>
parents:
739
diff
changeset
|
937 if (v->get_handler == NULL |
1192 | 938 && ngx_strncasecmp(value[1].data, (u_char *) "http_", 5) != 0 |
939 && ngx_strncasecmp(value[1].data, (u_char *) "sent_http_", 10) != 0 | |
940 && 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
|
941 { |
637 | 942 v->get_handler = ngx_http_rewrite_var; |
555 | 943 v->data = index; |
944 } | |
509 | 945 |
577 | 946 if (ngx_http_rewrite_value(cf, lcf, &value[2]) != NGX_CONF_OK) { |
947 return NGX_CONF_ERROR; | |
515 | 948 } |
501 | 949 |
637 | 950 if (v->set_handler) { |
951 vhcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
952 sizeof(ngx_http_script_var_handler_code_t)); | |
953 if (vhcode == NULL) { | |
954 return NGX_CONF_ERROR; | |
955 } | |
956 | |
957 vhcode->code = ngx_http_script_var_set_handler_code; | |
958 vhcode->handler = v->set_handler; | |
959 vhcode->data = v->data; | |
960 | |
961 return NGX_CONF_OK; | |
962 } | |
963 | |
964 vcode = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
965 sizeof(ngx_http_script_var_code_t)); | |
966 if (vcode == NULL) { | |
501 | 967 return NGX_CONF_ERROR; |
968 } | |
969 | |
637 | 970 vcode->code = ngx_http_script_set_var_code; |
971 vcode->index = (uintptr_t) index; | |
501 | 972 |
973 return NGX_CONF_OK; | |
974 } | |
577 | 975 |
976 | |
977 static char * | |
978 ngx_http_rewrite_value(ngx_conf_t *cf, ngx_http_rewrite_loc_conf_t *lcf, | |
979 ngx_str_t *value) | |
980 { | |
981 ngx_int_t n; | |
982 ngx_http_script_compile_t sc; | |
983 ngx_http_script_value_code_t *val; | |
984 ngx_http_script_complex_value_code_t *complex; | |
985 | |
986 n = ngx_http_script_variables_count(value); | |
987 | |
988 if (n == 0) { | |
989 val = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
990 sizeof(ngx_http_script_value_code_t)); | |
991 if (val == NULL) { | |
992 return NGX_CONF_ERROR; | |
993 } | |
994 | |
995 n = ngx_atoi(value->data, value->len); | |
996 | |
997 if (n == NGX_ERROR) { | |
998 n = 0; | |
999 } | |
1000 | |
1001 val->code = ngx_http_script_value_code; | |
1002 val->value = (uintptr_t) n; | |
1003 val->text_len = (uintptr_t) value->len; | |
1004 val->text_data = (uintptr_t) value->data; | |
1005 | |
1006 return NGX_CONF_OK; | |
1007 } | |
1008 | |
1009 complex = ngx_http_script_start_code(cf->pool, &lcf->codes, | |
1010 sizeof(ngx_http_script_complex_value_code_t)); | |
1011 if (complex == NULL) { | |
1012 return NGX_CONF_ERROR; | |
1013 } | |
1014 | |
1015 complex->code = ngx_http_script_complex_value_code; | |
1016 complex->lengths = NULL; | |
1017 | |
1018 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1019 | |
1020 sc.cf = cf; | |
1021 sc.source = value; | |
1022 sc.lengths = &complex->lengths; | |
1023 sc.values = &lcf->codes; | |
1024 sc.variables = n; | |
1025 sc.complete_lengths = 1; | |
1026 | |
1027 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1028 return NGX_CONF_ERROR; | |
1029 } | |
1030 | |
1031 return NGX_CONF_OK; | |
1032 } |