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