Mercurial > hg > nginx-quic
annotate src/http/ngx_http_variables.c @ 4433:dfb04ceb266f stable-1.0
Merge of r4335:
Fixed: some of $sent_http_* variables might contain header entries
which actually wasn't sent to a client.
The ngx_http_variable_headers() and ngx_http_variable_unknown_header()
functions did not ignore response header entries with zero "hash"
field.
Thanks to Yichun Zhang (agentzh).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sun, 05 Feb 2012 12:37:48 +0000 |
parents | fa4612bfb9fa |
children | 4919fb357a5d |
rev | line source |
---|---|
499 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
1329 | 10 #include <nginx.h> |
499 | 11 |
12 | |
573 | 13 static ngx_int_t ngx_http_variable_request(ngx_http_request_t *r, |
14 ngx_http_variable_value_t *v, uintptr_t data); | |
1350 | 15 static void ngx_http_variable_request_set(ngx_http_request_t *r, |
16 ngx_http_variable_value_t *v, uintptr_t data); | |
3282
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
17 static ngx_int_t ngx_http_variable_request_get_size(ngx_http_request_t *r, |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
18 ngx_http_variable_value_t *v, uintptr_t data); |
637 | 19 static void ngx_http_variable_request_set_size(ngx_http_request_t *r, |
20 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 21 static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r, |
22 ngx_http_variable_value_t *v, uintptr_t data); | |
23 static ngx_int_t ngx_http_variable_headers(ngx_http_request_t *r, | |
24 ngx_http_variable_value_t *v, uintptr_t data); | |
577 | 25 |
26 static ngx_int_t ngx_http_variable_unknown_header_in(ngx_http_request_t *r, | |
573 | 27 ngx_http_variable_value_t *v, uintptr_t data); |
577 | 28 static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r, |
29 ngx_http_variable_value_t *v, uintptr_t data); | |
3144
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
30 static ngx_int_t ngx_http_variable_request_line(ngx_http_request_t *r, |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
31 ngx_http_variable_value_t *v, uintptr_t data); |
2359
4fee4ebbfb0b
$cookie_... variable did not for SSI and perl
Igor Sysoev <igor@sysoev.ru>
parents:
2333
diff
changeset
|
32 static ngx_int_t ngx_http_variable_cookie(ngx_http_request_t *r, |
4fee4ebbfb0b
$cookie_... variable did not for SSI and perl
Igor Sysoev <igor@sysoev.ru>
parents:
2333
diff
changeset
|
33 ngx_http_variable_value_t *v, uintptr_t data); |
2137 | 34 static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r, |
35 ngx_http_variable_value_t *v, uintptr_t data); | |
577 | 36 |
573 | 37 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r, |
38 ngx_http_variable_value_t *v, uintptr_t data); | |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
39 static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r, |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
40 ngx_http_variable_value_t *v, uintptr_t data); |
573 | 41 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r, |
42 ngx_http_variable_value_t *v, uintptr_t data); | |
43 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r, | |
44 ngx_http_variable_value_t *v, uintptr_t data); | |
45 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r, | |
46 ngx_http_variable_value_t *v, uintptr_t data); | |
47 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r, | |
48 ngx_http_variable_value_t *v, uintptr_t data); | |
731 | 49 static ngx_int_t ngx_http_variable_scheme(ngx_http_request_t *r, |
50 ngx_http_variable_value_t *v, uintptr_t data); | |
1351 | 51 static ngx_int_t ngx_http_variable_is_args(ngx_http_request_t *r, |
52 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 53 static ngx_int_t ngx_http_variable_document_root(ngx_http_request_t *r, |
54 ngx_http_variable_value_t *v, uintptr_t data); | |
2259 | 55 static ngx_int_t ngx_http_variable_realpath_root(ngx_http_request_t *r, |
56 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 57 static ngx_int_t ngx_http_variable_request_filename(ngx_http_request_t *r, |
58 ngx_http_variable_value_t *v, uintptr_t data); | |
1811 | 59 static ngx_int_t ngx_http_variable_server_name(ngx_http_request_t *r, |
60 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 61 static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r, |
62 ngx_http_variable_value_t *v, uintptr_t data); | |
63 static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r, | |
64 ngx_http_variable_value_t *v, uintptr_t data); | |
611 | 65 static ngx_int_t ngx_http_variable_body_bytes_sent(ngx_http_request_t *r, |
66 ngx_http_variable_value_t *v, uintptr_t data); | |
629 | 67 static ngx_int_t ngx_http_variable_request_completion(ngx_http_request_t *r, |
68 ngx_http_variable_value_t *v, uintptr_t data); | |
2844 | 69 static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r, |
70 ngx_http_variable_value_t *v, uintptr_t data); | |
759 | 71 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r, |
72 ngx_http_variable_value_t *v, uintptr_t data); | |
499 | 73 |
641 | 74 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r, |
75 ngx_http_variable_value_t *v, uintptr_t data); | |
76 static ngx_int_t ngx_http_variable_sent_content_length(ngx_http_request_t *r, | |
77 ngx_http_variable_value_t *v, uintptr_t data); | |
2485
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
78 static ngx_int_t ngx_http_variable_sent_location(ngx_http_request_t *r, |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
79 ngx_http_variable_value_t *v, uintptr_t data); |
641 | 80 static ngx_int_t ngx_http_variable_sent_last_modified(ngx_http_request_t *r, |
81 ngx_http_variable_value_t *v, uintptr_t data); | |
82 static ngx_int_t ngx_http_variable_sent_connection(ngx_http_request_t *r, | |
83 ngx_http_variable_value_t *v, uintptr_t data); | |
84 static ngx_int_t ngx_http_variable_sent_keep_alive(ngx_http_request_t *r, | |
85 ngx_http_variable_value_t *v, uintptr_t data); | |
86 static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r, | |
87 ngx_http_variable_value_t *v, uintptr_t data); | |
88 | |
1329 | 89 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r, |
90 ngx_http_variable_value_t *v, uintptr_t data); | |
2011 | 91 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r, |
92 ngx_http_variable_value_t *v, uintptr_t data); | |
2249 | 93 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r, |
94 ngx_http_variable_value_t *v, uintptr_t data); | |
499 | 95 |
509 | 96 /* |
97 * TODO: | |
98 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED | |
99 * REMOTE_HOST (null), REMOTE_IDENT (null), | |
100 * SERVER_SOFTWARE | |
101 * | |
571 | 102 * Apache SSI: DOCUMENT_NAME, LAST_MODIFIED, USER_NAME (file owner) |
509 | 103 */ |
499 | 104 |
641 | 105 /* |
106 * the $http_host, $http_user_agent, $http_referer, $http_via, | |
107 * and $http_x_forwarded_for variables may be handled by generic | |
108 * ngx_http_variable_unknown_header_in(), but for perfomance reasons | |
109 * they are handled using dedicated entries | |
110 */ | |
111 | |
509 | 112 static ngx_http_variable_t ngx_http_core_variables[] = { |
113 | |
637 | 114 { ngx_string("http_host"), NULL, ngx_http_variable_header, |
533 | 115 offsetof(ngx_http_request_t, headers_in.host), 0, 0 }, |
499 | 116 |
637 | 117 { ngx_string("http_user_agent"), NULL, ngx_http_variable_header, |
533 | 118 offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 }, |
509 | 119 |
637 | 120 { ngx_string("http_referer"), NULL, ngx_http_variable_header, |
533 | 121 offsetof(ngx_http_request_t, headers_in.referer), 0, 0 }, |
499 | 122 |
123 #if (NGX_HTTP_GZIP) | |
637 | 124 { ngx_string("http_via"), NULL, ngx_http_variable_header, |
533 | 125 offsetof(ngx_http_request_t, headers_in.via), 0, 0 }, |
499 | 126 #endif |
127 | |
1113
f1d7cf0f68e3
optimize $http_x_forwarded_for
Igor Sysoev <igor@sysoev.ru>
parents:
983
diff
changeset
|
128 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP) |
637 | 129 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header, |
533 | 130 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 }, |
499 | 131 #endif |
132 | |
637 | 133 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers, |
533 | 134 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 }, |
515 | 135 |
637 | 136 { ngx_string("content_length"), NULL, ngx_http_variable_header, |
533 | 137 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 }, |
509 | 138 |
637 | 139 { ngx_string("content_type"), NULL, ngx_http_variable_header, |
533 | 140 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 }, |
509 | 141 |
637 | 142 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 }, |
509 | 143 |
982 | 144 { ngx_string("binary_remote_addr"), NULL, |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
145 ngx_http_variable_binary_remote_addr, 0, 0, 0 }, |
982 | 146 |
637 | 147 { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 }, |
499 | 148 |
637 | 149 { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 }, |
509 | 150 |
637 | 151 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 }, |
509 | 152 |
637 | 153 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 }, |
509 | 154 |
637 | 155 { ngx_string("server_protocol"), NULL, ngx_http_variable_request, |
533 | 156 offsetof(ngx_http_request_t, http_protocol), 0, 0 }, |
509 | 157 |
731 | 158 { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 }, |
159 | |
637 | 160 { ngx_string("request_uri"), NULL, ngx_http_variable_request, |
533 | 161 offsetof(ngx_http_request_t, unparsed_uri), 0, 0 }, |
499 | 162 |
637 | 163 { ngx_string("uri"), NULL, ngx_http_variable_request, |
164 offsetof(ngx_http_request_t, uri), | |
1565 | 165 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
637 | 166 |
167 { ngx_string("document_uri"), NULL, ngx_http_variable_request, | |
573 | 168 offsetof(ngx_http_request_t, uri), |
1565 | 169 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
573 | 170 |
3144
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
171 { ngx_string("request"), NULL, ngx_http_variable_request_line, 0, 0, 0 }, |
569 | 172 |
637 | 173 { ngx_string("document_root"), NULL, |
1565 | 174 ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
509 | 175 |
2259 | 176 { ngx_string("realpath_root"), NULL, |
177 ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, | |
178 | |
637 | 179 { ngx_string("query_string"), NULL, ngx_http_variable_request, |
509 | 180 offsetof(ngx_http_request_t, args), |
1565 | 181 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
499 | 182 |
1350 | 183 { ngx_string("args"), |
184 ngx_http_variable_request_set, | |
185 ngx_http_variable_request, | |
589 | 186 offsetof(ngx_http_request_t, args), |
1565 | 187 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
589 | 188 |
1351 | 189 { ngx_string("is_args"), NULL, ngx_http_variable_is_args, |
1565 | 190 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
1351 | 191 |
637 | 192 { ngx_string("request_filename"), NULL, |
193 ngx_http_variable_request_filename, 0, | |
1565 | 194 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
509 | 195 |
1811 | 196 { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 }, |
509 | 197 |
637 | 198 { ngx_string("request_method"), NULL, |
3463
0904bd7d5b37
make $request_method non-cacheable
Igor Sysoev <igor@sysoev.ru>
parents:
3451
diff
changeset
|
199 ngx_http_variable_request_method, 0, |
0904bd7d5b37
make $request_method non-cacheable
Igor Sysoev <igor@sysoev.ru>
parents:
3451
diff
changeset
|
200 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
509 | 201 |
637 | 202 { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 }, |
509 | 203 |
637 | 204 { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent, |
611 | 205 0, 0, 0 }, |
206 | |
637 | 207 { ngx_string("request_completion"), NULL, |
208 ngx_http_variable_request_completion, | |
629 | 209 0, 0, 0 }, |
210 | |
2844 | 211 { ngx_string("request_body"), NULL, |
212 ngx_http_variable_request_body, | |
213 0, 0, 0 }, | |
214 | |
759 | 215 { ngx_string("request_body_file"), NULL, |
216 ngx_http_variable_request_body_file, | |
217 0, 0, 0 }, | |
218 | |
641 | 219 { ngx_string("sent_http_content_type"), NULL, |
220 ngx_http_variable_sent_content_type, 0, 0, 0 }, | |
221 | |
222 { ngx_string("sent_http_content_length"), NULL, | |
223 ngx_http_variable_sent_content_length, 0, 0, 0 }, | |
224 | |
2485
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
225 { ngx_string("sent_http_location"), NULL, |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
226 ngx_http_variable_sent_location, 0, 0, 0 }, |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
227 |
641 | 228 { ngx_string("sent_http_last_modified"), NULL, |
229 ngx_http_variable_sent_last_modified, 0, 0, 0 }, | |
230 | |
231 { ngx_string("sent_http_connection"), NULL, | |
232 ngx_http_variable_sent_connection, 0, 0, 0 }, | |
233 | |
234 { ngx_string("sent_http_keep_alive"), NULL, | |
235 ngx_http_variable_sent_keep_alive, 0, 0, 0 }, | |
236 | |
237 { ngx_string("sent_http_transfer_encoding"), NULL, | |
238 ngx_http_variable_sent_transfer_encoding, 0, 0, 0 }, | |
239 | |
240 { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers, | |
241 offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 }, | |
242 | |
637 | 243 { ngx_string("limit_rate"), ngx_http_variable_request_set_size, |
3282
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
244 ngx_http_variable_request_get_size, |
637 | 245 offsetof(ngx_http_request_t, limit_rate), |
1565 | 246 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
637 | 247 |
1329 | 248 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version, |
249 0, 0, 0 }, | |
250 | |
2011 | 251 { ngx_string("hostname"), NULL, ngx_http_variable_hostname, |
252 0, 0, 0 }, | |
253 | |
2249 | 254 { ngx_string("pid"), NULL, ngx_http_variable_pid, |
255 0, 0, 0 }, | |
256 | |
637 | 257 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
499 | 258 }; |
259 | |
260 | |
577 | 261 ngx_http_variable_value_t ngx_http_variable_null_value = |
262 ngx_http_variable(""); | |
263 ngx_http_variable_value_t ngx_http_variable_true_value = | |
264 ngx_http_variable("1"); | |
265 | |
266 | |
499 | 267 ngx_http_variable_t * |
509 | 268 ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags) |
269 { | |
611 | 270 ngx_int_t rc; |
509 | 271 ngx_uint_t i; |
611 | 272 ngx_hash_key_t *key; |
509 | 273 ngx_http_variable_t *v; |
274 ngx_http_core_main_conf_t *cmcf; | |
275 | |
276 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
277 | |
611 | 278 key = cmcf->variables_keys->keys.elts; |
279 for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) { | |
280 if (name->len != key[i].key.len | |
281 || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0) | |
509 | 282 { |
283 continue; | |
284 } | |
285 | |
611 | 286 v = key[i].value; |
287 | |
1565 | 288 if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) { |
509 | 289 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
290 "the duplicate \"%V\" variable", name); | |
291 return NULL; | |
292 } | |
293 | |
611 | 294 return v; |
509 | 295 } |
296 | |
611 | 297 v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t)); |
509 | 298 if (v == NULL) { |
299 return NULL; | |
300 } | |
301 | |
302 v->name.len = name->len; | |
2049 | 303 v->name.data = ngx_pnalloc(cf->pool, name->len); |
509 | 304 if (v->name.data == NULL) { |
305 return NULL; | |
306 } | |
307 | |
2135 | 308 ngx_strlow(v->name.data, name->data, name->len); |
509 | 309 |
637 | 310 v->set_handler = NULL; |
311 v->get_handler = NULL; | |
509 | 312 v->data = 0; |
313 v->flags = flags; | |
533 | 314 v->index = 0; |
509 | 315 |
611 | 316 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0); |
317 | |
318 if (rc == NGX_ERROR) { | |
319 return NULL; | |
320 } | |
321 | |
322 if (rc == NGX_BUSY) { | |
323 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
324 "conflicting variable name \"%V\"", name); | |
325 return NULL; | |
326 } | |
327 | |
509 | 328 return v; |
329 } | |
330 | |
331 | |
332 ngx_int_t | |
333 ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name) | |
499 | 334 { |
501 | 335 ngx_uint_t i; |
336 ngx_http_variable_t *v; | |
499 | 337 ngx_http_core_main_conf_t *cmcf; |
338 | |
339 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
340 | |
501 | 341 v = cmcf->variables.elts; |
342 | |
343 if (v == NULL) { | |
499 | 344 if (ngx_array_init(&cmcf->variables, cf->pool, 4, |
2536
a6d6d762c554
small optimization: " == NGX_ERROR" > " != NGX_OK"
Igor Sysoev <igor@sysoev.ru>
parents:
2533
diff
changeset
|
345 sizeof(ngx_http_variable_t)) |
a6d6d762c554
small optimization: " == NGX_ERROR" > " != NGX_OK"
Igor Sysoev <igor@sysoev.ru>
parents:
2533
diff
changeset
|
346 != NGX_OK) |
499 | 347 { |
509 | 348 return NGX_ERROR; |
499 | 349 } |
501 | 350 |
351 } else { | |
352 for (i = 0; i < cmcf->variables.nelts; i++) { | |
353 if (name->len != v[i].name.len | |
354 || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0) | |
355 { | |
356 continue; | |
357 } | |
358 | |
509 | 359 return i; |
501 | 360 } |
499 | 361 } |
362 | |
501 | 363 v = ngx_array_push(&cmcf->variables); |
364 if (v == NULL) { | |
509 | 365 return NGX_ERROR; |
499 | 366 } |
367 | |
501 | 368 v->name.len = name->len; |
2049 | 369 v->name.data = ngx_pnalloc(cf->pool, name->len); |
501 | 370 if (v->name.data == NULL) { |
509 | 371 return NGX_ERROR; |
501 | 372 } |
499 | 373 |
2135 | 374 ngx_strlow(v->name.data, name->data, name->len); |
501 | 375 |
637 | 376 v->set_handler = NULL; |
377 v->get_handler = NULL; | |
501 | 378 v->data = 0; |
509 | 379 v->flags = 0; |
533 | 380 v->index = cmcf->variables.nelts - 1; |
501 | 381 |
509 | 382 return cmcf->variables.nelts - 1; |
499 | 383 } |
384 | |
385 | |
386 ngx_http_variable_value_t * | |
387 ngx_http_get_indexed_variable(ngx_http_request_t *r, ngx_uint_t index) | |
388 { | |
501 | 389 ngx_http_variable_t *v; |
499 | 390 ngx_http_core_main_conf_t *cmcf; |
391 | |
392 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
393 | |
553 | 394 if (cmcf->variables.nelts <= index) { |
499 | 395 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
396 "unknown variable index: %d", index); | |
397 return NULL; | |
398 } | |
399 | |
573 | 400 if (r->variables[index].not_found || r->variables[index].valid) { |
401 return &r->variables[index]; | |
501 | 402 } |
499 | 403 |
501 | 404 v = cmcf->variables.elts; |
499 | 405 |
637 | 406 if (v[index].get_handler(r, &r->variables[index], v[index].data) |
407 == NGX_OK) | |
408 { | |
1565 | 409 if (v[index].flags & NGX_HTTP_VAR_NOCACHEABLE) { |
410 r->variables[index].no_cacheable = 1; | |
499 | 411 } |
573 | 412 |
413 return &r->variables[index]; | |
499 | 414 } |
415 | |
657 | 416 r->variables[index].valid = 0; |
417 r->variables[index].not_found = 1; | |
418 | |
798 | 419 return NULL; |
573 | 420 } |
421 | |
422 | |
423 ngx_http_variable_value_t * | |
424 ngx_http_get_flushed_variable(ngx_http_request_t *r, ngx_uint_t index) | |
425 { | |
1150 | 426 ngx_http_variable_value_t *v; |
573 | 427 |
428 v = &r->variables[index]; | |
429 | |
430 if (v->valid) { | |
1565 | 431 if (!v->no_cacheable) { |
573 | 432 return v; |
433 } | |
434 | |
435 v->valid = 0; | |
436 v->not_found = 0; | |
509 | 437 } |
501 | 438 |
573 | 439 return ngx_http_get_indexed_variable(r, index); |
499 | 440 } |
441 | |
442 | |
443 ngx_http_variable_value_t * | |
3500
0eb46e3c5c02
change processing variables accessed by SSI and perl module:
Igor Sysoev <igor@sysoev.ru>
parents:
3499
diff
changeset
|
444 ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key) |
499 | 445 { |
501 | 446 ngx_http_variable_t *v; |
573 | 447 ngx_http_variable_value_t *vv; |
499 | 448 ngx_http_core_main_conf_t *cmcf; |
449 | |
450 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
451 | |
611 | 452 v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len); |
499 | 453 |
611 | 454 if (v) { |
455 if (v->flags & NGX_HTTP_VAR_INDEXED) { | |
3500
0eb46e3c5c02
change processing variables accessed by SSI and perl module:
Igor Sysoev <igor@sysoev.ru>
parents:
3499
diff
changeset
|
456 return ngx_http_get_flushed_variable(r, v->index); |
519 | 457 |
458 } else { | |
499 | 459 |
573 | 460 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); |
501 | 461 |
637 | 462 if (vv && v->get_handler(r, vv, v->data) == NGX_OK) { |
573 | 463 return vv; |
464 } | |
509 | 465 |
573 | 466 return NULL; |
467 } | |
509 | 468 } |
469 | |
470 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); | |
471 if (vv == NULL) { | |
472 return NULL; | |
473 } | |
474 | |
573 | 475 if (ngx_strncmp(name->data, "http_", 5) == 0) { |
499 | 476 |
577 | 477 if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name) |
478 == NGX_OK) | |
479 { | |
480 return vv; | |
481 } | |
482 | |
483 return NULL; | |
484 } | |
485 | |
486 if (ngx_strncmp(name->data, "sent_http_", 10) == 0) { | |
487 | |
488 if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name) | |
489 == NGX_OK) | |
573 | 490 { |
491 return vv; | |
492 } | |
499 | 493 |
494 return NULL; | |
495 } | |
496 | |
3451
7a1958778351
fix $upstream_http_ variable prefix length
Igor Sysoev <igor@sysoev.ru>
parents:
3409
diff
changeset
|
497 if (ngx_strncmp(name->data, "upstream_http_", 14) == 0) { |
1162 | 498 |
499 if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name) | |
500 == NGX_OK) | |
501 { | |
502 return vv; | |
503 } | |
504 | |
505 return NULL; | |
506 } | |
507 | |
2307 | 508 if (ngx_strncmp(name->data, "cookie_", 7) == 0) { |
509 | |
2359
4fee4ebbfb0b
$cookie_... variable did not for SSI and perl
Igor Sysoev <igor@sysoev.ru>
parents:
2333
diff
changeset
|
510 if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) { |
2307 | 511 return vv; |
512 } | |
513 | |
514 return NULL; | |
515 } | |
516 | |
2137 | 517 if (ngx_strncmp(name->data, "arg_", 4) == 0) { |
518 | |
519 if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) { | |
520 return vv; | |
521 } | |
522 | |
523 return NULL; | |
524 } | |
525 | |
635 | 526 vv->not_found = 1; |
573 | 527 |
501 | 528 return vv; |
499 | 529 } |
530 | |
531 | |
573 | 532 static ngx_int_t |
533 ngx_http_variable_request(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
534 uintptr_t data) | |
535 { | |
536 ngx_str_t *s; | |
537 | |
538 s = (ngx_str_t *) ((char *) r + data); | |
539 | |
540 if (s->data) { | |
541 v->len = s->len; | |
542 v->valid = 1; | |
1565 | 543 v->no_cacheable = 0; |
573 | 544 v->not_found = 0; |
545 v->data = s->data; | |
546 | |
547 } else { | |
548 v->not_found = 1; | |
549 } | |
550 | |
551 return NGX_OK; | |
552 } | |
553 | |
554 | |
637 | 555 static void |
1350 | 556 ngx_http_variable_request_set(ngx_http_request_t *r, |
557 ngx_http_variable_value_t *v, uintptr_t data) | |
558 { | |
559 ngx_str_t *s; | |
560 | |
561 s = (ngx_str_t *) ((char *) r + data); | |
562 | |
563 s->len = v->len; | |
564 s->data = v->data; | |
565 } | |
566 | |
567 | |
3282
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
568 static ngx_int_t |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
569 ngx_http_variable_request_get_size(ngx_http_request_t *r, |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
570 ngx_http_variable_value_t *v, uintptr_t data) |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
571 { |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
572 size_t *sp; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
573 |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
574 sp = (size_t *) ((char *) r + data); |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
575 |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
576 v->data = ngx_pnalloc(r->pool, NGX_SIZE_T_LEN); |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
577 if (v->data == NULL) { |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
578 return NGX_ERROR; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
579 } |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
580 |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
581 v->len = ngx_sprintf(v->data, "%uz", *sp) - v->data; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
582 v->valid = 1; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
583 v->no_cacheable = 0; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
584 v->not_found = 0; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
585 |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
586 return NGX_OK; |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
587 } |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
588 |
c76b7a7959d5
fix segfault if $limit_rate was logged
Igor Sysoev <igor@sysoev.ru>
parents:
3268
diff
changeset
|
589 |
1350 | 590 static void |
637 | 591 ngx_http_variable_request_set_size(ngx_http_request_t *r, |
592 ngx_http_variable_value_t *v, uintptr_t data) | |
593 { | |
594 ssize_t s, *sp; | |
595 ngx_str_t val; | |
596 | |
1310
33d6c994a0b2
Sun Studio on sparc uses different bit order
Igor Sysoev <igor@sysoev.ru>
parents:
1191
diff
changeset
|
597 val.len = v->len; |
637 | 598 val.data = v->data; |
599 | |
600 s = ngx_parse_size(&val); | |
601 | |
602 if (s == NGX_ERROR) { | |
603 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
604 "invalid size \"%V\"", &val); | |
605 return; | |
606 } | |
607 | |
608 sp = (ssize_t *) ((char *) r + data); | |
609 | |
610 *sp = s; | |
611 | |
612 return; | |
613 } | |
614 | |
615 | |
573 | 616 static ngx_int_t |
617 ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
618 uintptr_t data) | |
515 | 619 { |
573 | 620 ngx_table_elt_t *h; |
621 | |
622 h = *(ngx_table_elt_t **) ((char *) r + data); | |
623 | |
624 if (h) { | |
625 v->len = h->value.len; | |
626 v->valid = 1; | |
1565 | 627 v->no_cacheable = 0; |
573 | 628 v->not_found = 0; |
629 v->data = h->value.data; | |
630 | |
631 } else { | |
632 v->not_found = 1; | |
633 } | |
634 | |
635 return NGX_OK; | |
636 } | |
637 | |
638 | |
639 static ngx_int_t | |
640 ngx_http_variable_headers(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
641 uintptr_t data) | |
642 { | |
4433 | 643 size_t len; |
644 u_char *p, *end; | |
667 | 645 ngx_uint_t i, n; |
573 | 646 ngx_array_t *a; |
647 ngx_table_elt_t **h; | |
515 | 648 |
649 a = (ngx_array_t *) ((char *) r + data); | |
650 | |
667 | 651 n = a->nelts; |
4433 | 652 h = a->elts; |
653 | |
654 len = 0; | |
655 | |
656 for (i = 0; i < n; i++) { | |
657 | |
658 if (h[i]->hash == 0) { | |
659 continue; | |
660 } | |
661 | |
662 len += h[i]->value.len + sizeof("; ") - 1; | |
663 } | |
664 | |
665 if (len == 0) { | |
573 | 666 v->not_found = 1; |
667 return NGX_OK; | |
515 | 668 } |
669 | |
4433 | 670 len -= sizeof("; ") - 1; |
671 | |
573 | 672 v->valid = 1; |
1565 | 673 v->no_cacheable = 0; |
573 | 674 v->not_found = 0; |
515 | 675 |
667 | 676 if (n == 1) { |
573 | 677 v->len = (*h)->value.len; |
678 v->data = (*h)->value.data; | |
515 | 679 |
573 | 680 return NGX_OK; |
515 | 681 } |
682 | |
2049 | 683 p = ngx_pnalloc(r->pool, len); |
573 | 684 if (p == NULL) { |
685 return NGX_ERROR; | |
686 } | |
687 | |
688 v->len = len; | |
689 v->data = p; | |
515 | 690 |
4433 | 691 end = p + len; |
692 | |
515 | 693 for (i = 0; /* void */ ; i++) { |
4433 | 694 |
695 if (h[i]->hash == 0) { | |
696 continue; | |
697 } | |
698 | |
573 | 699 p = ngx_copy(p, h[i]->value.data, h[i]->value.len); |
515 | 700 |
4433 | 701 if (p == end) { |
515 | 702 break; |
703 } | |
704 | |
705 *p++ = ';'; *p++ = ' '; | |
706 } | |
707 | |
573 | 708 return NGX_OK; |
515 | 709 } |
710 | |
711 | |
573 | 712 static ngx_int_t |
577 | 713 ngx_http_variable_unknown_header_in(ngx_http_request_t *r, |
714 ngx_http_variable_value_t *v, uintptr_t data) | |
715 { | |
716 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data, | |
717 &r->headers_in.headers.part, | |
718 sizeof("http_") - 1); | |
719 } | |
720 | |
721 | |
722 static ngx_int_t | |
723 ngx_http_variable_unknown_header_out(ngx_http_request_t *r, | |
573 | 724 ngx_http_variable_value_t *v, uintptr_t data) |
499 | 725 { |
577 | 726 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data, |
727 &r->headers_out.headers.part, | |
728 sizeof("sent_http_") - 1); | |
729 } | |
501 | 730 |
577 | 731 |
1162 | 732 ngx_int_t |
577 | 733 ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var, |
734 ngx_list_part_t *part, size_t prefix) | |
735 { | |
573 | 736 u_char ch; |
737 ngx_uint_t i, n; | |
738 ngx_table_elt_t *header; | |
499 | 739 |
740 header = part->elts; | |
741 | |
742 for (i = 0; /* void */ ; i++) { | |
743 | |
744 if (i >= part->nelts) { | |
745 if (part->next == NULL) { | |
746 break; | |
747 } | |
748 | |
749 part = part->next; | |
750 header = part->elts; | |
751 i = 0; | |
752 } | |
753 | |
4433 | 754 if (header[i].hash == 0) { |
755 continue; | |
756 } | |
757 | |
577 | 758 for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) { |
499 | 759 ch = header[i].key.data[n]; |
760 | |
509 | 761 if (ch >= 'A' && ch <= 'Z') { |
762 ch |= 0x20; | |
499 | 763 |
764 } else if (ch == '-') { | |
765 ch = '_'; | |
766 } | |
767 | |
577 | 768 if (var->data[n + prefix] != ch) { |
499 | 769 break; |
770 } | |
771 } | |
772 | |
742
75d767d32624
nonexistent $sent_http_content has value of $sent_http_content_length or so
Igor Sysoev <igor@sysoev.ru>
parents:
732
diff
changeset
|
773 if (n + prefix == var->len && n == header[i].key.len) { |
573 | 774 v->len = header[i].value.len; |
775 v->valid = 1; | |
1565 | 776 v->no_cacheable = 0; |
573 | 777 v->not_found = 0; |
778 v->data = header[i].value.data; | |
499 | 779 |
573 | 780 return NGX_OK; |
499 | 781 } |
782 } | |
783 | |
573 | 784 v->not_found = 1; |
785 | |
786 return NGX_OK; | |
509 | 787 } |
788 | |
789 | |
573 | 790 static ngx_int_t |
3144
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
791 ngx_http_variable_request_line(ngx_http_request_t *r, |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
792 ngx_http_variable_value_t *v, uintptr_t data) |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
793 { |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
794 u_char *p, *s; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
795 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
796 s = r->request_line.data; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
797 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
798 if (s == NULL) { |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
799 s = r->request_start; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
800 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
801 if (s == NULL) { |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
802 v->not_found = 1; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
803 return NGX_OK; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
804 } |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
805 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
806 for (p = s; p < r->header_in->last; p++) { |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
807 if (*p == CR || *p == LF) { |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
808 break; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
809 } |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
810 } |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
811 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
812 r->request_line.len = p - s; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
813 r->request_line.data = s; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
814 } |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
815 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
816 v->len = r->request_line.len; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
817 v->valid = 1; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
818 v->no_cacheable = 0; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
819 v->not_found = 0; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
820 v->data = s; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
821 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
822 return NGX_OK; |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
823 } |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
824 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
825 |
ecbfc3396c42
allow to log invalid $request in access_log always,
Igor Sysoev <igor@sysoev.ru>
parents:
2878
diff
changeset
|
826 static ngx_int_t |
2307 | 827 ngx_http_variable_cookie(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
828 uintptr_t data) | |
829 { | |
830 ngx_str_t *name = (ngx_str_t *) data; | |
831 | |
832 ngx_str_t cookie, s; | |
833 | |
834 s.len = name->len - (sizeof("cookie_") - 1); | |
835 s.data = name->data + sizeof("cookie_") - 1; | |
836 | |
837 if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &s, &cookie) | |
838 == NGX_DECLINED) | |
839 { | |
840 v->not_found = 1; | |
841 return NGX_OK; | |
842 } | |
843 | |
844 v->len = cookie.len; | |
845 v->valid = 1; | |
846 v->no_cacheable = 0; | |
847 v->not_found = 0; | |
848 v->data = cookie.data; | |
849 | |
850 return NGX_OK; | |
851 } | |
852 | |
853 | |
854 static ngx_int_t | |
2137 | 855 ngx_http_variable_argument(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
856 uintptr_t data) | |
857 { | |
858 ngx_str_t *name = (ngx_str_t *) data; | |
859 | |
2415 | 860 u_char *arg; |
861 size_t len; | |
862 ngx_str_t value; | |
2137 | 863 |
2415 | 864 len = name->len - (sizeof("arg_") - 1); |
865 arg = name->data + sizeof("arg_") - 1; | |
866 | |
867 if (ngx_http_arg(r, arg, len, &value) != NGX_OK) { | |
2137 | 868 v->not_found = 1; |
869 return NGX_OK; | |
870 } | |
871 | |
2415 | 872 v->data = value.data; |
873 v->len = value.len; | |
874 v->valid = 1; | |
875 v->no_cacheable = 0; | |
876 v->not_found = 0; | |
2137 | 877 |
878 return NGX_OK; | |
879 } | |
880 | |
881 | |
882 static ngx_int_t | |
573 | 883 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
884 uintptr_t data) | |
509 | 885 { |
1811 | 886 ngx_http_core_srv_conf_t *cscf; |
887 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
888 if (r->headers_in.server.len) { |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
889 v->len = r->headers_in.server.len; |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
890 v->data = r->headers_in.server.data; |
928
a6fe6bedb9e3
fix segfault when $host is used and
Igor Sysoev <igor@sysoev.ru>
parents:
864
diff
changeset
|
891 |
a6fe6bedb9e3
fix segfault when $host is used and
Igor Sysoev <igor@sysoev.ru>
parents:
864
diff
changeset
|
892 } else { |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
893 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
894 |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
895 v->len = cscf->server_name.len; |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
896 v->data = cscf->server_name.data; |
509 | 897 } |
898 | |
573 | 899 v->valid = 1; |
1565 | 900 v->no_cacheable = 0; |
573 | 901 v->not_found = 0; |
509 | 902 |
573 | 903 return NGX_OK; |
499 | 904 } |
905 | |
906 | |
573 | 907 static ngx_int_t |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
908 ngx_http_variable_binary_remote_addr(ngx_http_request_t *r, |
573 | 909 ngx_http_variable_value_t *v, uintptr_t data) |
499 | 910 { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
911 struct sockaddr_in *sin; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
912 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
913 struct sockaddr_in6 *sin6; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
914 #endif |
982 | 915 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
916 switch (r->connection->sockaddr->sa_family) { |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
917 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
918 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
919 case AF_INET6: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
920 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; |
982 | 921 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
922 v->len = sizeof(struct in6_addr); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
923 v->valid = 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
924 v->no_cacheable = 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
925 v->not_found = 0; |
3268
c6a40c815d45
use sin6_addr.s6_addr instead of "(u_char *) & .sin6_addr"
Igor Sysoev <igor@sysoev.ru>
parents:
3144
diff
changeset
|
926 v->data = sin6->sin6_addr.s6_addr; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
927 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
928 break; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
929 #endif |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
930 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
931 default: /* AF_INET */ |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
932 sin = (struct sockaddr_in *) r->connection->sockaddr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
933 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
934 v->len = sizeof(in_addr_t); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
935 v->valid = 1; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
936 v->no_cacheable = 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
937 v->not_found = 0; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
938 v->data = (u_char *) &sin->sin_addr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
939 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
940 break; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
941 } |
982 | 942 |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
943 return NGX_OK; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
944 } |
982 | 945 |
946 | |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
947 static ngx_int_t |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
948 ngx_http_variable_remote_addr(ngx_http_request_t *r, |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
949 ngx_http_variable_value_t *v, uintptr_t data) |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
950 { |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
951 v->len = r->connection->addr_text.len; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
952 v->valid = 1; |
1565 | 953 v->no_cacheable = 0; |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
954 v->not_found = 0; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
955 v->data = r->connection->addr_text.data; |
499 | 956 |
573 | 957 return NGX_OK; |
499 | 958 } |
959 | |
960 | |
573 | 961 static ngx_int_t |
962 ngx_http_variable_remote_port(ngx_http_request_t *r, | |
963 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 964 { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
965 ngx_uint_t port; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
966 struct sockaddr_in *sin; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
967 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
968 struct sockaddr_in6 *sin6; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
969 #endif |
509 | 970 |
573 | 971 v->len = 0; |
972 v->valid = 1; | |
1565 | 973 v->no_cacheable = 0; |
573 | 974 v->not_found = 0; |
509 | 975 |
2049 | 976 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1); |
573 | 977 if (v->data == NULL) { |
978 return NGX_ERROR; | |
509 | 979 } |
980 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
981 switch (r->connection->sockaddr->sa_family) { |
577 | 982 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
983 #if (NGX_HAVE_INET6) |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
984 case AF_INET6: |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
985 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
986 port = ntohs(sin6->sin6_port); |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
987 break; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
988 #endif |
577 | 989 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
990 default: /* AF_INET */ |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
991 sin = (struct sockaddr_in *) r->connection->sockaddr; |
509 | 992 port = ntohs(sin->sin_port); |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
993 break; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
994 } |
577 | 995 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
996 if (port > 0 && port < 65536) { |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
997 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; |
509 | 998 } |
999 | |
573 | 1000 return NGX_OK; |
509 | 1001 } |
1002 | |
1003 | |
573 | 1004 static ngx_int_t |
1005 ngx_http_variable_server_addr(ngx_http_request_t *r, | |
1006 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 1007 { |
1805 | 1008 ngx_str_t s; |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1009 u_char addr[NGX_SOCKADDR_STRLEN]; |
499 | 1010 |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1011 s.len = NGX_SOCKADDR_STRLEN; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1012 s.data = addr; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1013 |
2857
507fc5ac9839
use ngx_connection_local_sockaddr() instead of ngx_http_server_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
2844
diff
changeset
|
1014 if (ngx_connection_local_sockaddr(r->connection, &s, 0) != NGX_OK) { |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1015 return NGX_ERROR; |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1016 } |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1017 |
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1018 s.data = ngx_pnalloc(r->pool, s.len); |
1805 | 1019 if (s.data == NULL) { |
573 | 1020 return NGX_ERROR; |
509 | 1021 } |
1022 | |
2512
2e91aecb9e57
a prelimiary IPv6 support, HTTP listen
Igor Sysoev <igor@sysoev.ru>
parents:
2485
diff
changeset
|
1023 ngx_memcpy(s.data, addr, s.len); |
509 | 1024 |
1805 | 1025 v->len = s.len; |
573 | 1026 v->valid = 1; |
1565 | 1027 v->no_cacheable = 0; |
573 | 1028 v->not_found = 0; |
1805 | 1029 v->data = s.data; |
499 | 1030 |
573 | 1031 return NGX_OK; |
499 | 1032 } |
1033 | |
1034 | |
573 | 1035 static ngx_int_t |
1036 ngx_http_variable_server_port(ngx_http_request_t *r, | |
1037 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 1038 { |
2533 | 1039 ngx_uint_t port; |
1040 struct sockaddr_in *sin; | |
1041 #if (NGX_HAVE_INET6) | |
1042 struct sockaddr_in6 *sin6; | |
1043 #endif | |
1044 | |
1045 v->len = 0; | |
573 | 1046 v->valid = 1; |
1565 | 1047 v->no_cacheable = 0; |
573 | 1048 v->not_found = 0; |
2533 | 1049 |
2857
507fc5ac9839
use ngx_connection_local_sockaddr() instead of ngx_http_server_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
2844
diff
changeset
|
1050 if (ngx_connection_local_sockaddr(r->connection, NULL, 0) != NGX_OK) { |
2533 | 1051 return NGX_ERROR; |
1052 } | |
1053 | |
1054 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1); | |
1055 if (v->data == NULL) { | |
1056 return NGX_ERROR; | |
1057 } | |
1058 | |
1059 switch (r->connection->local_sockaddr->sa_family) { | |
1060 | |
1061 #if (NGX_HAVE_INET6) | |
1062 case AF_INET6: | |
1063 sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr; | |
1064 port = ntohs(sin6->sin6_port); | |
1065 break; | |
1066 #endif | |
1067 | |
1068 default: /* AF_INET */ | |
1069 sin = (struct sockaddr_in *) r->connection->local_sockaddr; | |
1070 port = ntohs(sin->sin_port); | |
1071 break; | |
1072 } | |
1073 | |
1074 if (port > 0 && port < 65536) { | |
1075 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; | |
1076 } | |
509 | 1077 |
573 | 1078 return NGX_OK; |
509 | 1079 } |
1080 | |
1081 | |
573 | 1082 static ngx_int_t |
731 | 1083 ngx_http_variable_scheme(ngx_http_request_t *r, |
1084 ngx_http_variable_value_t *v, uintptr_t data) | |
1085 { | |
1086 #if (NGX_HTTP_SSL) | |
1087 | |
1088 if (r->connection->ssl) { | |
1089 v->len = sizeof("https") - 1; | |
1090 v->valid = 1; | |
1565 | 1091 v->no_cacheable = 0; |
731 | 1092 v->not_found = 0; |
732 | 1093 v->data = (u_char *) "https"; |
731 | 1094 |
1095 return NGX_OK; | |
1096 } | |
1097 | |
1098 #endif | |
1099 | |
1100 v->len = sizeof("http") - 1; | |
1101 v->valid = 1; | |
1565 | 1102 v->no_cacheable = 0; |
731 | 1103 v->not_found = 0; |
732 | 1104 v->data = (u_char *) "http"; |
731 | 1105 |
1106 return NGX_OK; | |
1107 } | |
1108 | |
1109 | |
1110 static ngx_int_t | |
1351 | 1111 ngx_http_variable_is_args(ngx_http_request_t *r, |
1112 ngx_http_variable_value_t *v, uintptr_t data) | |
1113 { | |
1114 v->valid = 1; | |
1565 | 1115 v->no_cacheable = 0; |
1351 | 1116 v->not_found = 0; |
1117 | |
1118 if (r->args.len == 0) { | |
1119 v->len = 0; | |
1120 v->data = NULL; | |
1121 return NGX_OK; | |
1122 } | |
1123 | |
1124 v->len = 1; | |
1125 v->data = (u_char *) "?"; | |
1126 | |
1127 return NGX_OK; | |
1128 } | |
1129 | |
1130 | |
1131 static ngx_int_t | |
573 | 1132 ngx_http_variable_document_root(ngx_http_request_t *r, |
1133 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 1134 { |
671 | 1135 ngx_str_t path; |
573 | 1136 ngx_http_core_loc_conf_t *clcf; |
509 | 1137 |
1138 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1139 | |
671 | 1140 if (clcf->root_lengths == NULL) { |
1141 v->len = clcf->root.len; | |
1142 v->valid = 1; | |
1565 | 1143 v->no_cacheable = 0; |
671 | 1144 v->not_found = 0; |
1145 v->data = clcf->root.data; | |
1146 | |
1147 } else { | |
1148 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 0, | |
1149 clcf->root_values->elts) | |
1150 == NULL) | |
1151 { | |
1152 return NGX_ERROR; | |
1153 } | |
1154 | |
2536
a6d6d762c554
small optimization: " == NGX_ERROR" > " != NGX_OK"
Igor Sysoev <igor@sysoev.ru>
parents:
2533
diff
changeset
|
1155 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) != NGX_OK) { |
671 | 1156 return NGX_ERROR; |
1157 } | |
1158 | |
1159 v->len = path.len; | |
1160 v->valid = 1; | |
1565 | 1161 v->no_cacheable = 0; |
671 | 1162 v->not_found = 0; |
1163 v->data = path.data; | |
1164 } | |
509 | 1165 |
573 | 1166 return NGX_OK; |
509 | 1167 } |
1168 | |
1169 | |
573 | 1170 static ngx_int_t |
2259 | 1171 ngx_http_variable_realpath_root(ngx_http_request_t *r, |
1172 ngx_http_variable_value_t *v, uintptr_t data) | |
1173 { | |
1174 size_t len; | |
1175 ngx_str_t path; | |
1176 ngx_http_core_loc_conf_t *clcf; | |
1177 u_char real[NGX_MAX_PATH]; | |
1178 | |
1179 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1180 | |
1181 if (clcf->root_lengths == NULL) { | |
1182 path = clcf->root; | |
1183 | |
1184 } else { | |
1185 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 1, | |
1186 clcf->root_values->elts) | |
1187 == NULL) | |
1188 { | |
1189 return NGX_ERROR; | |
1190 } | |
1191 | |
1192 path.data[path.len - 1] = '\0'; | |
1193 | |
2536
a6d6d762c554
small optimization: " == NGX_ERROR" > " != NGX_OK"
Igor Sysoev <igor@sysoev.ru>
parents:
2533
diff
changeset
|
1194 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) != NGX_OK) { |
2259 | 1195 return NGX_ERROR; |
1196 } | |
1197 } | |
1198 | |
1199 if (ngx_realpath(path.data, real) == NULL) { | |
1200 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, | |
1201 ngx_realpath_n " \"%s\" failed", path.data); | |
1202 return NGX_ERROR; | |
1203 } | |
1204 | |
1205 len = ngx_strlen(real); | |
1206 | |
1207 v->data = ngx_pnalloc(r->pool, len); | |
1208 if (v->data == NULL) { | |
1209 return NGX_ERROR; | |
1210 } | |
1211 | |
1212 v->len = len; | |
1213 v->valid = 1; | |
1214 v->no_cacheable = 0; | |
1215 v->not_found = 0; | |
1216 | |
1217 ngx_memcpy(v->data, real, len); | |
1218 | |
1219 return NGX_OK; | |
1220 } | |
1221 | |
1222 | |
1223 static ngx_int_t | |
573 | 1224 ngx_http_variable_request_filename(ngx_http_request_t *r, |
1225 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 1226 { |
773 | 1227 size_t root; |
573 | 1228 ngx_str_t path; |
499 | 1229 |
773 | 1230 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
573 | 1231 return NGX_ERROR; |
557 | 1232 } |
509 | 1233 |
557 | 1234 /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */ |
509 | 1235 |
573 | 1236 v->len = path.len - 1; |
1237 v->valid = 1; | |
1565 | 1238 v->no_cacheable = 0; |
573 | 1239 v->not_found = 0; |
1240 v->data = path.data; | |
499 | 1241 |
573 | 1242 return NGX_OK; |
499 | 1243 } |
1244 | |
1245 | |
573 | 1246 static ngx_int_t |
1811 | 1247 ngx_http_variable_server_name(ngx_http_request_t *r, |
1248 ngx_http_variable_value_t *v, uintptr_t data) | |
1249 { | |
1250 ngx_http_core_srv_conf_t *cscf; | |
1251 | |
1252 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); | |
1253 | |
1254 v->len = cscf->server_name.len; | |
1255 v->valid = 1; | |
1256 v->no_cacheable = 0; | |
1257 v->not_found = 0; | |
1258 v->data = cscf->server_name.data; | |
1259 | |
1260 return NGX_OK; | |
1261 } | |
1262 | |
1263 | |
1264 static ngx_int_t | |
573 | 1265 ngx_http_variable_request_method(ngx_http_request_t *r, |
1266 ngx_http_variable_value_t *v, uintptr_t data) | |
561 | 1267 { |
647 | 1268 if (r->main->method_name.data) { |
1269 v->len = r->main->method_name.len; | |
573 | 1270 v->valid = 1; |
1565 | 1271 v->no_cacheable = 0; |
573 | 1272 v->not_found = 0; |
647 | 1273 v->data = r->main->method_name.data; |
573 | 1274 |
1275 } else { | |
1276 v->not_found = 1; | |
561 | 1277 } |
1278 | |
573 | 1279 return NGX_OK; |
561 | 1280 } |
1281 | |
1282 | |
573 | 1283 static ngx_int_t |
1284 ngx_http_variable_remote_user(ngx_http_request_t *r, | |
1285 ngx_http_variable_value_t *v, uintptr_t data) | |
539 | 1286 { |
573 | 1287 ngx_int_t rc; |
539 | 1288 |
1289 rc = ngx_http_auth_basic_user(r); | |
1290 | |
1291 if (rc == NGX_DECLINED) { | |
573 | 1292 v->not_found = 1; |
1293 return NGX_OK; | |
539 | 1294 } |
1295 | |
1296 if (rc == NGX_ERROR) { | |
573 | 1297 return NGX_ERROR; |
539 | 1298 } |
1299 | |
573 | 1300 v->len = r->headers_in.user.len; |
1301 v->valid = 1; | |
1565 | 1302 v->no_cacheable = 0; |
573 | 1303 v->not_found = 0; |
1304 v->data = r->headers_in.user.data; | |
571 | 1305 |
573 | 1306 return NGX_OK; |
571 | 1307 } |
1308 | |
1309 | |
611 | 1310 static ngx_int_t |
1311 ngx_http_variable_body_bytes_sent(ngx_http_request_t *r, | |
1312 ngx_http_variable_value_t *v, uintptr_t data) | |
1313 { | |
1314 off_t sent; | |
1315 u_char *p; | |
1316 | |
1317 sent = r->connection->sent - r->header_size; | |
1318 | |
1319 if (sent < 0) { | |
1320 sent = 0; | |
1321 } | |
1322 | |
2049 | 1323 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN); |
611 | 1324 if (p == NULL) { |
1325 return NGX_ERROR; | |
1326 } | |
1327 | |
1328 v->len = ngx_sprintf(p, "%O", sent) - p; | |
1329 v->valid = 1; | |
1565 | 1330 v->no_cacheable = 0; |
611 | 1331 v->not_found = 0; |
1332 v->data = p; | |
1333 | |
1334 return NGX_OK; | |
1335 } | |
1336 | |
1337 | |
629 | 1338 static ngx_int_t |
641 | 1339 ngx_http_variable_sent_content_type(ngx_http_request_t *r, |
1340 ngx_http_variable_value_t *v, uintptr_t data) | |
1341 { | |
1342 if (r->headers_out.content_type.len) { | |
1343 v->len = r->headers_out.content_type.len; | |
1344 v->valid = 1; | |
1565 | 1345 v->no_cacheable = 0; |
641 | 1346 v->not_found = 0; |
1347 v->data = r->headers_out.content_type.data; | |
1348 | |
1349 } else { | |
1350 v->not_found = 1; | |
1351 } | |
1352 | |
1353 return NGX_OK; | |
1354 } | |
1355 | |
1356 | |
1357 static ngx_int_t | |
1358 ngx_http_variable_sent_content_length(ngx_http_request_t *r, | |
1359 ngx_http_variable_value_t *v, uintptr_t data) | |
1360 { | |
1361 u_char *p; | |
1362 | |
1363 if (r->headers_out.content_length) { | |
1364 v->len = r->headers_out.content_length->value.len; | |
1365 v->valid = 1; | |
1565 | 1366 v->no_cacheable = 0; |
641 | 1367 v->not_found = 0; |
1368 v->data = r->headers_out.content_length->value.data; | |
1369 | |
1370 return NGX_OK; | |
1371 } | |
1372 | |
1373 if (r->headers_out.content_length_n >= 0) { | |
2049 | 1374 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN); |
641 | 1375 if (p == NULL) { |
1376 return NGX_ERROR; | |
1377 } | |
1378 | |
1379 v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p; | |
1380 v->valid = 1; | |
1565 | 1381 v->no_cacheable = 0; |
641 | 1382 v->not_found = 0; |
1383 v->data = p; | |
1384 | |
1385 return NGX_OK; | |
1386 } | |
1387 | |
1388 v->not_found = 1; | |
1389 | |
1390 return NGX_OK; | |
1391 } | |
1392 | |
1393 | |
1394 static ngx_int_t | |
2485
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1395 ngx_http_variable_sent_location(ngx_http_request_t *r, |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1396 ngx_http_variable_value_t *v, uintptr_t data) |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1397 { |
2539
051e9b12428e
fix segfault introduced in r2486 in $sent_http_location processing
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
1398 ngx_str_t name; |
051e9b12428e
fix segfault introduced in r2486 in $sent_http_location processing
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
1399 |
2485
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1400 if (r->headers_out.location) { |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1401 v->len = r->headers_out.location->value.len; |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1402 v->valid = 1; |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1403 v->no_cacheable = 0; |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1404 v->not_found = 0; |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1405 v->data = r->headers_out.location->value.data; |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1406 |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1407 return NGX_OK; |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1408 } |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1409 |
3516
dd1570b6f237
ngx_str_set() and ngx_str_null()
Igor Sysoev <igor@sysoev.ru>
parents:
3500
diff
changeset
|
1410 ngx_str_set(&name, "sent_http_location"); |
2539
051e9b12428e
fix segfault introduced in r2486 in $sent_http_location processing
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
1411 |
051e9b12428e
fix segfault introduced in r2486 in $sent_http_location processing
Igor Sysoev <igor@sysoev.ru>
parents:
2536
diff
changeset
|
1412 return ngx_http_variable_unknown_header(v, &name, |
2485
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1413 &r->headers_out.headers.part, |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1414 sizeof("sent_http_") - 1); |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1415 } |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1416 |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1417 |
51b1097dbb67
fix $sent_http_location for local redirects
Igor Sysoev <igor@sysoev.ru>
parents:
2415
diff
changeset
|
1418 static ngx_int_t |
641 | 1419 ngx_http_variable_sent_last_modified(ngx_http_request_t *r, |
1420 ngx_http_variable_value_t *v, uintptr_t data) | |
1421 { | |
1422 u_char *p; | |
1423 | |
1424 if (r->headers_out.last_modified) { | |
1425 v->len = r->headers_out.last_modified->value.len; | |
1426 v->valid = 1; | |
1565 | 1427 v->no_cacheable = 0; |
641 | 1428 v->not_found = 0; |
1429 v->data = r->headers_out.last_modified->value.data; | |
1430 | |
1431 return NGX_OK; | |
1432 } | |
1433 | |
1434 if (r->headers_out.last_modified_time >= 0) { | |
2049 | 1435 p = ngx_pnalloc(r->pool, |
641 | 1436 sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1); |
1437 if (p == NULL) { | |
1438 return NGX_ERROR; | |
1439 } | |
1440 | |
1441 v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p; | |
1442 v->valid = 1; | |
1565 | 1443 v->no_cacheable = 0; |
641 | 1444 v->not_found = 0; |
1445 v->data = p; | |
1446 | |
1447 return NGX_OK; | |
1448 } | |
1449 | |
1450 v->not_found = 1; | |
1451 | |
1452 return NGX_OK; | |
1453 } | |
1454 | |
1455 | |
1456 static ngx_int_t | |
1457 ngx_http_variable_sent_connection(ngx_http_request_t *r, | |
1458 ngx_http_variable_value_t *v, uintptr_t data) | |
1459 { | |
1460 size_t len; | |
1461 char *p; | |
1462 | |
1463 if (r->keepalive) { | |
1464 len = sizeof("keep-alive") - 1; | |
1465 p = "keep-alive"; | |
1466 | |
1467 } else { | |
1468 len = sizeof("close") - 1; | |
1469 p = "close"; | |
1470 } | |
1471 | |
1472 v->len = len; | |
1473 v->valid = 1; | |
1565 | 1474 v->no_cacheable = 0; |
641 | 1475 v->not_found = 0; |
1476 v->data = (u_char *) p; | |
1477 | |
1478 return NGX_OK; | |
1479 } | |
1480 | |
1481 | |
1482 static ngx_int_t | |
1483 ngx_http_variable_sent_keep_alive(ngx_http_request_t *r, | |
1484 ngx_http_variable_value_t *v, uintptr_t data) | |
1485 { | |
1486 u_char *p; | |
1487 ngx_http_core_loc_conf_t *clcf; | |
1488 | |
1489 if (r->keepalive) { | |
1490 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1491 | |
1492 if (clcf->keepalive_header) { | |
1493 | |
2049 | 1494 p = ngx_pnalloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN); |
641 | 1495 if (p == NULL) { |
1496 return NGX_ERROR; | |
1497 } | |
1498 | |
1499 v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p; | |
1500 v->valid = 1; | |
1565 | 1501 v->no_cacheable = 0; |
641 | 1502 v->not_found = 0; |
1503 v->data = p; | |
1504 | |
1505 return NGX_OK; | |
1506 } | |
1507 } | |
1508 | |
1509 v->not_found = 1; | |
1510 | |
1511 return NGX_OK; | |
1512 } | |
1513 | |
1514 | |
1515 static ngx_int_t | |
1516 ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r, | |
1517 ngx_http_variable_value_t *v, uintptr_t data) | |
1518 { | |
1519 if (r->chunked) { | |
1520 v->len = sizeof("chunked") - 1; | |
1521 v->valid = 1; | |
1565 | 1522 v->no_cacheable = 0; |
641 | 1523 v->not_found = 0; |
1524 v->data = (u_char *) "chunked"; | |
1525 | |
1526 } else { | |
1527 v->not_found = 1; | |
1528 } | |
1529 | |
1530 return NGX_OK; | |
1531 } | |
1532 | |
1533 | |
1534 static ngx_int_t | |
629 | 1535 ngx_http_variable_request_completion(ngx_http_request_t *r, |
1536 ngx_http_variable_value_t *v, uintptr_t data) | |
1537 { | |
1538 if (r->request_complete) { | |
1539 v->len = 2; | |
1540 v->valid = 1; | |
1565 | 1541 v->no_cacheable = 0; |
629 | 1542 v->not_found = 0; |
1543 v->data = (u_char *) "OK"; | |
1544 | |
1545 return NGX_OK; | |
1546 } | |
1547 | |
1548 v->len = 0; | |
1549 v->valid = 1; | |
1565 | 1550 v->no_cacheable = 0; |
629 | 1551 v->not_found = 0; |
1552 v->data = (u_char *) ""; | |
1553 | |
1554 return NGX_OK; | |
1555 } | |
1556 | |
1557 | |
759 | 1558 static ngx_int_t |
2844 | 1559 ngx_http_variable_request_body(ngx_http_request_t *r, |
1560 ngx_http_variable_value_t *v, uintptr_t data) | |
1561 { | |
1562 u_char *p; | |
1563 size_t len; | |
1564 ngx_buf_t *buf, *next; | |
1565 ngx_chain_t *cl; | |
1566 | |
2878
8535736ace1a
fix segfault introduced in r2845
Igor Sysoev <igor@sysoev.ru>
parents:
2857
diff
changeset
|
1567 if (r->request_body == NULL |
8535736ace1a
fix segfault introduced in r2845
Igor Sysoev <igor@sysoev.ru>
parents:
2857
diff
changeset
|
1568 || r->request_body->bufs == NULL |
8535736ace1a
fix segfault introduced in r2845
Igor Sysoev <igor@sysoev.ru>
parents:
2857
diff
changeset
|
1569 || r->request_body->temp_file) |
8535736ace1a
fix segfault introduced in r2845
Igor Sysoev <igor@sysoev.ru>
parents:
2857
diff
changeset
|
1570 { |
2844 | 1571 v->not_found = 1; |
1572 | |
1573 return NGX_OK; | |
1574 } | |
1575 | |
1576 cl = r->request_body->bufs; | |
1577 buf = cl->buf; | |
1578 | |
1579 if (cl->next == NULL) { | |
1580 v->len = buf->last - buf->pos; | |
1581 v->valid = 1; | |
1582 v->no_cacheable = 0; | |
1583 v->not_found = 0; | |
1584 v->data = buf->pos; | |
1585 | |
1586 return NGX_OK; | |
1587 } | |
1588 | |
1589 next = cl->next->buf; | |
1590 len = (buf->last - buf->pos) + (next->last - next->pos); | |
1591 | |
1592 p = ngx_pnalloc(r->pool, len); | |
1593 if (p == NULL) { | |
1594 return NGX_ERROR; | |
1595 } | |
1596 | |
1597 v->data = p; | |
1598 | |
1599 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos); | |
1600 ngx_memcpy(p, next->pos, next->last - next->pos); | |
1601 | |
1602 v->len = len; | |
1603 v->valid = 1; | |
1604 v->no_cacheable = 0; | |
1605 v->not_found = 0; | |
1606 | |
1607 return NGX_OK; | |
1608 } | |
1609 | |
1610 | |
1611 static ngx_int_t | |
759 | 1612 ngx_http_variable_request_body_file(ngx_http_request_t *r, |
1613 ngx_http_variable_value_t *v, uintptr_t data) | |
1614 { | |
1615 if (r->request_body == NULL || r->request_body->temp_file == NULL) { | |
763
0b0f3d4854c0
variable should not be found if no request body file
Igor Sysoev <igor@sysoev.ru>
parents:
759
diff
changeset
|
1616 v->not_found = 1; |
759 | 1617 |
1618 return NGX_OK; | |
1619 } | |
1620 | |
1621 v->len = r->request_body->temp_file->file.name.len; | |
1622 v->valid = 1; | |
1565 | 1623 v->no_cacheable = 0; |
759 | 1624 v->not_found = 0; |
1625 v->data = r->request_body->temp_file->file.name.data; | |
1626 | |
1627 return NGX_OK; | |
1628 } | |
1629 | |
1630 | |
1329 | 1631 static ngx_int_t |
1632 ngx_http_variable_nginx_version(ngx_http_request_t *r, | |
1633 ngx_http_variable_value_t *v, uintptr_t data) | |
1634 { | |
1635 v->len = sizeof(NGINX_VERSION) - 1; | |
1636 v->valid = 1; | |
1565 | 1637 v->no_cacheable = 0; |
1329 | 1638 v->not_found = 0; |
1639 v->data = (u_char *) NGINX_VERSION; | |
1640 | |
1641 return NGX_OK; | |
1642 } | |
1643 | |
1644 | |
2011 | 1645 static ngx_int_t |
1646 ngx_http_variable_hostname(ngx_http_request_t *r, | |
1647 ngx_http_variable_value_t *v, uintptr_t data) | |
1648 { | |
1649 v->len = ngx_cycle->hostname.len; | |
1650 v->valid = 1; | |
1651 v->no_cacheable = 0; | |
1652 v->not_found = 0; | |
1653 v->data = ngx_cycle->hostname.data; | |
1654 | |
1655 return NGX_OK; | |
1656 } | |
1657 | |
1658 | |
2249 | 1659 static ngx_int_t |
1660 ngx_http_variable_pid(ngx_http_request_t *r, | |
1661 ngx_http_variable_value_t *v, uintptr_t data) | |
1662 { | |
1663 u_char *p; | |
1664 | |
1665 p = ngx_pnalloc(r->pool, NGX_INT64_LEN); | |
1666 if (p == NULL) { | |
1667 return NGX_ERROR; | |
1668 } | |
1669 | |
1670 v->len = ngx_sprintf(p, "%P", ngx_pid) - p; | |
1671 v->valid = 1; | |
1672 v->no_cacheable = 0; | |
1673 v->not_found = 0; | |
1674 v->data = p; | |
1675 | |
1676 return NGX_OK; | |
1677 } | |
1678 | |
1679 | |
3872
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1680 void * |
3929
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1681 ngx_http_map_find(ngx_http_request_t *r, ngx_http_map_t *map, ngx_str_t *match) |
3872
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1682 { |
3929
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1683 void *value; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1684 u_char *low; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1685 size_t len; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1686 ngx_uint_t key; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1687 |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1688 len = match->len; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1689 |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1690 if (len) { |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1691 low = ngx_pnalloc(r->pool, len); |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1692 if (low == NULL) { |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1693 return NULL; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1694 } |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1695 |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1696 } else { |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1697 low = NULL; |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1698 } |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1699 |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1700 key = ngx_hash_strlow(low, match->data, len); |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1701 |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1702 value = ngx_hash_find_combined(&map->hash, key, low, len); |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1703 if (value) { |
fa4612bfb9fa
change ngx_http_map_find(): use case sensitive regexes
Igor Sysoev <igor@sysoev.ru>
parents:
3872
diff
changeset
|
1704 return value; |
3872
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1705 } |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1706 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1707 #if (NGX_PCRE) |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1708 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1709 if (len && map->nregex) { |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1710 ngx_int_t n; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1711 ngx_uint_t i; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1712 ngx_http_map_regex_t *reg; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1713 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1714 reg = map->regex; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1715 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1716 for (i = 0; i < map->nregex; i++) { |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1717 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1718 n = ngx_http_regex_exec(r, reg[i].regex, match); |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1719 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1720 if (n == NGX_OK) { |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1721 return reg[i].value; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1722 } |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1723 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1724 if (n == NGX_DECLINED) { |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1725 continue; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1726 } |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1727 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1728 /* NGX_ERROR */ |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1729 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1730 return NULL; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1731 } |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1732 } |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1733 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1734 #endif |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1735 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1736 return NULL; |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1737 } |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1738 |
83cd1910329c
allow regex as "map" parameter
Igor Sysoev <igor@sysoev.ru>
parents:
3641
diff
changeset
|
1739 |
3356
fe08c14530e9
fix building without PCRE, the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3345
diff
changeset
|
1740 #if (NGX_PCRE) |
fe08c14530e9
fix building without PCRE, the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3345
diff
changeset
|
1741 |
3325 | 1742 static ngx_int_t |
1743 ngx_http_variable_not_found(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
1744 uintptr_t data) | |
1745 { | |
1746 v->not_found = 1; | |
1747 return NGX_OK; | |
1748 } | |
1749 | |
1750 | |
1751 ngx_http_regex_t * | |
1752 ngx_http_regex_compile(ngx_conf_t *cf, ngx_regex_compile_t *rc) | |
1753 { | |
1754 u_char *p; | |
1755 size_t size; | |
1756 ngx_str_t name; | |
1757 ngx_uint_t i, n; | |
1758 ngx_http_variable_t *v; | |
1759 ngx_http_regex_t *re; | |
1760 ngx_http_regex_variable_t *rv; | |
1761 ngx_http_core_main_conf_t *cmcf; | |
1762 | |
1763 rc->pool = cf->pool; | |
1764 | |
1765 if (ngx_regex_compile(rc) != NGX_OK) { | |
1766 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc->err); | |
1767 return NULL; | |
1768 } | |
1769 | |
1770 re = ngx_pcalloc(cf->pool, sizeof(ngx_http_regex_t)); | |
1771 if (re == NULL) { | |
1772 return NULL; | |
1773 } | |
1774 | |
1775 re->regex = rc->regex; | |
1776 re->ncaptures = rc->captures; | |
1777 | |
1778 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
1779 cmcf->ncaptures = ngx_max(cmcf->ncaptures, re->ncaptures); | |
1780 | |
1781 n = (ngx_uint_t) rc->named_captures; | |
1782 | |
1783 if (n == 0) { | |
1784 return re; | |
1785 } | |
1786 | |
1787 rv = ngx_palloc(rc->pool, n * sizeof(ngx_http_regex_variable_t)); | |
1788 if (rv == NULL) { | |
1789 return NULL; | |
1790 } | |
1791 | |
1792 re->variables = rv; | |
1793 re->nvariables = n; | |
1794 re->name = rc->pattern; | |
1795 | |
1796 size = rc->name_size; | |
1797 p = rc->names; | |
1798 | |
1799 for (i = 0; i < n; i++) { | |
1800 rv[i].capture = 2 * ((p[0] << 8) + p[1]); | |
1801 | |
1802 name.data = &p[2]; | |
1803 name.len = ngx_strlen(name.data); | |
1804 | |
1805 v = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE); | |
1806 if (v == NULL) { | |
1807 return NULL; | |
1808 } | |
1809 | |
1810 rv[i].index = ngx_http_get_variable_index(cf, &name); | |
1811 if (rv[i].index == NGX_ERROR) { | |
1812 return NULL; | |
1813 } | |
1814 | |
1815 v->get_handler = ngx_http_variable_not_found; | |
1816 | |
3409
bb79608169eb
named captures worked for two names only
Igor Sysoev <igor@sysoev.ru>
parents:
3356
diff
changeset
|
1817 p += size; |
3325 | 1818 } |
1819 | |
1820 return re; | |
1821 } | |
1822 | |
1823 | |
1824 ngx_int_t | |
1825 ngx_http_regex_exec(ngx_http_request_t *r, ngx_http_regex_t *re, ngx_str_t *s) | |
1826 { | |
1827 ngx_int_t rc, index; | |
1828 ngx_uint_t i, n, len; | |
1829 ngx_http_variable_value_t *vv; | |
1830 ngx_http_core_main_conf_t *cmcf; | |
1831 | |
1832 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
1833 | |
1834 if (re->ncaptures) { | |
3345
d8228f0b5113
evaluate maximum captures size on configuration phase
Igor Sysoev <igor@sysoev.ru>
parents:
3344
diff
changeset
|
1835 len = cmcf->ncaptures; |
3325 | 1836 |
1837 if (r->captures == NULL) { | |
1838 r->captures = ngx_palloc(r->pool, len * sizeof(int)); | |
1839 if (r->captures == NULL) { | |
1840 return NGX_ERROR; | |
1841 } | |
1842 } | |
1843 | |
1844 } else { | |
1845 len = 0; | |
1846 } | |
1847 | |
1848 rc = ngx_regex_exec(re->regex, s, r->captures, len); | |
1849 | |
1850 if (rc == NGX_REGEX_NO_MATCHED) { | |
1851 return NGX_DECLINED; | |
1852 } | |
1853 | |
1854 if (rc < 0) { | |
1855 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
1856 ngx_regex_exec_n " failed: %i on \"%V\" using \"%V\"", | |
1857 rc, s, &re->name); | |
1858 return NGX_ERROR; | |
1859 } | |
1860 | |
1861 for (i = 0; i < re->nvariables; i++) { | |
1862 | |
1863 n = re->variables[i].capture; | |
1864 index = re->variables[i].index; | |
1865 vv = &r->variables[index]; | |
1866 | |
1867 vv->len = r->captures[n + 1] - r->captures[n]; | |
1868 vv->valid = 1; | |
1869 vv->no_cacheable = 0; | |
1870 vv->not_found = 0; | |
1871 vv->data = &s->data[r->captures[n]]; | |
1872 | |
1873 #if (NGX_DEBUG) | |
1874 { | |
1875 ngx_http_variable_t *v; | |
1876 | |
1877 v = cmcf->variables.elts; | |
1878 | |
1879 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1880 "http regex set $%V to \"%*s\"", | |
1881 &v[index].name, vv->len, vv->data); | |
1882 } | |
1883 #endif | |
1884 } | |
1885 | |
3344
1aed55182ea2
fix captures in "rewrite", the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
1886 r->ncaptures = rc * 2; |
3325 | 1887 r->captures_data = s->data; |
1888 | |
1889 return NGX_OK; | |
1890 } | |
1891 | |
3356
fe08c14530e9
fix building without PCRE, the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3345
diff
changeset
|
1892 #endif |
fe08c14530e9
fix building without PCRE, the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3345
diff
changeset
|
1893 |
3325 | 1894 |
499 | 1895 ngx_int_t |
509 | 1896 ngx_http_variables_add_core_vars(ngx_conf_t *cf) |
499 | 1897 { |
611 | 1898 ngx_int_t rc; |
1899 ngx_http_variable_t *v; | |
501 | 1900 ngx_http_core_main_conf_t *cmcf; |
499 | 1901 |
509 | 1902 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
1903 | |
611 | 1904 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, |
1905 sizeof(ngx_hash_keys_arrays_t)); | |
1906 if (cmcf->variables_keys == NULL) { | |
1907 return NGX_ERROR; | |
1908 } | |
1909 | |
1910 cmcf->variables_keys->pool = cf->pool; | |
1911 cmcf->variables_keys->temp_pool = cf->pool; | |
1912 | |
1913 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) | |
1914 != NGX_OK) | |
509 | 1915 { |
499 | 1916 return NGX_ERROR; |
1917 } | |
1918 | |
611 | 1919 for (v = ngx_http_core_variables; v->name.len; v++) { |
1920 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, | |
1921 NGX_HASH_READONLY_KEY); | |
1922 | |
1923 if (rc == NGX_OK) { | |
1924 continue; | |
499 | 1925 } |
1926 | |
611 | 1927 if (rc == NGX_BUSY) { |
1928 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1929 "conflicting variable name \"%V\"", &v->name); | |
1930 } | |
1931 | |
1932 return NGX_ERROR; | |
499 | 1933 } |
1934 | |
1935 return NGX_OK; | |
1936 } | |
509 | 1937 |
1938 | |
1939 ngx_int_t | |
1940 ngx_http_variables_init_vars(ngx_conf_t *cf) | |
1941 { | |
1942 ngx_uint_t i, n; | |
611 | 1943 ngx_hash_key_t *key; |
1944 ngx_hash_init_t hash; | |
509 | 1945 ngx_http_variable_t *v, *av; |
1946 ngx_http_core_main_conf_t *cmcf; | |
1947 | |
1948 /* set the handlers for the indexed http variables */ | |
1949 | |
1950 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
1951 | |
1952 v = cmcf->variables.elts; | |
611 | 1953 key = cmcf->variables_keys->keys.elts; |
509 | 1954 |
1955 for (i = 0; i < cmcf->variables.nelts; i++) { | |
1956 | |
611 | 1957 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { |
509 | 1958 |
1191
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1959 av = key[n].value; |
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1960 |
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1961 if (av->get_handler |
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1962 && v[i].name.len == key[n].key.len |
611 | 1963 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len) |
509 | 1964 == 0) |
1965 { | |
637 | 1966 v[i].get_handler = av->get_handler; |
611 | 1967 v[i].data = av->data; |
527 | 1968 |
611 | 1969 av->flags |= NGX_HTTP_VAR_INDEXED; |
1970 v[i].flags = av->flags; | |
509 | 1971 |
611 | 1972 av->index = i; |
533 | 1973 |
509 | 1974 goto next; |
1975 } | |
1976 } | |
1977 | |
1978 if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) { | |
637 | 1979 v[i].get_handler = ngx_http_variable_unknown_header_in; |
577 | 1980 v[i].data = (uintptr_t) &v[i].name; |
1981 | |
1982 continue; | |
1983 } | |
1984 | |
1985 if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) { | |
637 | 1986 v[i].get_handler = ngx_http_variable_unknown_header_out; |
509 | 1987 v[i].data = (uintptr_t) &v[i].name; |
1988 | |
1989 continue; | |
1990 } | |
1991 | |
1162 | 1992 if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) { |
1993 v[i].get_handler = ngx_http_upstream_header_variable; | |
1994 v[i].data = (uintptr_t) &v[i].name; | |
1565 | 1995 v[i].flags = NGX_HTTP_VAR_NOCACHEABLE; |
1162 | 1996 |
1997 continue; | |
1998 } | |
1999 | |
2307 | 2000 if (ngx_strncmp(v[i].name.data, "cookie_", 7) == 0) { |
2001 v[i].get_handler = ngx_http_variable_cookie; | |
2002 v[i].data = (uintptr_t) &v[i].name; | |
2003 | |
2004 continue; | |
2005 } | |
2006 | |
2137 | 2007 if (ngx_strncmp(v[i].name.data, "arg_", 4) == 0) { |
2008 v[i].get_handler = ngx_http_variable_argument; | |
2009 v[i].data = (uintptr_t) &v[i].name; | |
3499
b4a14d50388b
make $arg_ variables non-cacheable
Igor Sysoev <igor@sysoev.ru>
parents:
3463
diff
changeset
|
2010 v[i].flags = NGX_HTTP_VAR_NOCACHEABLE; |
2137 | 2011 |
2012 continue; | |
2013 } | |
2014 | |
509 | 2015 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
2016 "unknown \"%V\" variable", &v[i].name); | |
2017 | |
2018 return NGX_ERROR; | |
2019 | |
2020 next: | |
2021 continue; | |
2022 } | |
2023 | |
2024 | |
611 | 2025 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { |
2026 av = key[n].value; | |
2027 | |
2028 if (av->flags & NGX_HTTP_VAR_NOHASH) { | |
2029 key[n].key.data = NULL; | |
583 | 2030 } |
2031 } | |
2032 | |
2033 | |
611 | 2034 hash.hash = &cmcf->variables_hash; |
2035 hash.key = ngx_hash_key; | |
2036 hash.max_size = cmcf->variables_hash_max_size; | |
2037 hash.bucket_size = cmcf->variables_hash_bucket_size; | |
2038 hash.name = "variables_hash"; | |
2039 hash.pool = cf->pool; | |
2040 hash.temp_pool = NULL; | |
509 | 2041 |
611 | 2042 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts, |
2043 cmcf->variables_keys->keys.nelts) | |
581 | 2044 != NGX_OK) |
509 | 2045 { |
2046 return NGX_ERROR; | |
2047 } | |
2048 | |
611 | 2049 cmcf->variables_keys = NULL; |
509 | 2050 |
2051 return NGX_OK; | |
2052 } |