Mercurial > hg > nginx-quic
annotate src/http/ngx_http_variables.c @ 2052:b4085596a7e6
fix "proxy_pass https://..." broken in r1427
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 20 Jun 2008 14:42:54 +0000 |
parents | 2a92804f4109 |
children | 8c6521eedf84 |
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_event.h> | |
10 #include <ngx_http.h> | |
1329 | 11 #include <nginx.h> |
499 | 12 |
13 | |
573 | 14 static ngx_int_t ngx_http_variable_request(ngx_http_request_t *r, |
15 ngx_http_variable_value_t *v, uintptr_t data); | |
1350 | 16 static void ngx_http_variable_request_set(ngx_http_request_t *r, |
17 ngx_http_variable_value_t *v, uintptr_t data); | |
637 | 18 static void ngx_http_variable_request_set_size(ngx_http_request_t *r, |
19 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 20 static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r, |
21 ngx_http_variable_value_t *v, uintptr_t data); | |
22 static ngx_int_t ngx_http_variable_headers(ngx_http_request_t *r, | |
23 ngx_http_variable_value_t *v, uintptr_t data); | |
577 | 24 |
25 static ngx_int_t ngx_http_variable_unknown_header_in(ngx_http_request_t *r, | |
573 | 26 ngx_http_variable_value_t *v, uintptr_t data); |
577 | 27 static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r, |
28 ngx_http_variable_value_t *v, uintptr_t data); | |
29 | |
573 | 30 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r, |
31 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
|
32 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
|
33 ngx_http_variable_value_t *v, uintptr_t data); |
573 | 34 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r, |
35 ngx_http_variable_value_t *v, uintptr_t data); | |
36 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r, | |
37 ngx_http_variable_value_t *v, uintptr_t data); | |
38 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r, | |
39 ngx_http_variable_value_t *v, uintptr_t data); | |
40 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r, | |
41 ngx_http_variable_value_t *v, uintptr_t data); | |
731 | 42 static ngx_int_t ngx_http_variable_scheme(ngx_http_request_t *r, |
43 ngx_http_variable_value_t *v, uintptr_t data); | |
1351 | 44 static ngx_int_t ngx_http_variable_is_args(ngx_http_request_t *r, |
45 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 46 static ngx_int_t ngx_http_variable_document_root(ngx_http_request_t *r, |
47 ngx_http_variable_value_t *v, uintptr_t data); | |
48 static ngx_int_t ngx_http_variable_request_filename(ngx_http_request_t *r, | |
49 ngx_http_variable_value_t *v, uintptr_t data); | |
1811 | 50 static ngx_int_t ngx_http_variable_server_name(ngx_http_request_t *r, |
51 ngx_http_variable_value_t *v, uintptr_t data); | |
573 | 52 static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r, |
53 ngx_http_variable_value_t *v, uintptr_t data); | |
54 static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r, | |
55 ngx_http_variable_value_t *v, uintptr_t data); | |
611 | 56 static ngx_int_t ngx_http_variable_body_bytes_sent(ngx_http_request_t *r, |
57 ngx_http_variable_value_t *v, uintptr_t data); | |
629 | 58 static ngx_int_t ngx_http_variable_request_completion(ngx_http_request_t *r, |
59 ngx_http_variable_value_t *v, uintptr_t data); | |
759 | 60 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r, |
61 ngx_http_variable_value_t *v, uintptr_t data); | |
499 | 62 |
641 | 63 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r, |
64 ngx_http_variable_value_t *v, uintptr_t data); | |
65 static ngx_int_t ngx_http_variable_sent_content_length(ngx_http_request_t *r, | |
66 ngx_http_variable_value_t *v, uintptr_t data); | |
67 static ngx_int_t ngx_http_variable_sent_last_modified(ngx_http_request_t *r, | |
68 ngx_http_variable_value_t *v, uintptr_t data); | |
69 static ngx_int_t ngx_http_variable_sent_connection(ngx_http_request_t *r, | |
70 ngx_http_variable_value_t *v, uintptr_t data); | |
71 static ngx_int_t ngx_http_variable_sent_keep_alive(ngx_http_request_t *r, | |
72 ngx_http_variable_value_t *v, uintptr_t data); | |
73 static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r, | |
74 ngx_http_variable_value_t *v, uintptr_t data); | |
75 | |
1329 | 76 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r, |
77 ngx_http_variable_value_t *v, uintptr_t data); | |
2011 | 78 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r, |
79 ngx_http_variable_value_t *v, uintptr_t data); | |
499 | 80 |
509 | 81 /* |
82 * TODO: | |
83 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED | |
84 * REMOTE_HOST (null), REMOTE_IDENT (null), | |
85 * SERVER_SOFTWARE | |
86 * | |
571 | 87 * Apache SSI: DOCUMENT_NAME, LAST_MODIFIED, USER_NAME (file owner) |
509 | 88 */ |
499 | 89 |
641 | 90 /* |
91 * the $http_host, $http_user_agent, $http_referer, $http_via, | |
92 * and $http_x_forwarded_for variables may be handled by generic | |
93 * ngx_http_variable_unknown_header_in(), but for perfomance reasons | |
94 * they are handled using dedicated entries | |
95 */ | |
96 | |
509 | 97 static ngx_http_variable_t ngx_http_core_variables[] = { |
98 | |
637 | 99 { ngx_string("http_host"), NULL, ngx_http_variable_header, |
533 | 100 offsetof(ngx_http_request_t, headers_in.host), 0, 0 }, |
499 | 101 |
637 | 102 { ngx_string("http_user_agent"), NULL, ngx_http_variable_header, |
533 | 103 offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 }, |
509 | 104 |
637 | 105 { ngx_string("http_referer"), NULL, ngx_http_variable_header, |
533 | 106 offsetof(ngx_http_request_t, headers_in.referer), 0, 0 }, |
499 | 107 |
108 #if (NGX_HTTP_GZIP) | |
637 | 109 { ngx_string("http_via"), NULL, ngx_http_variable_header, |
533 | 110 offsetof(ngx_http_request_t, headers_in.via), 0, 0 }, |
499 | 111 #endif |
112 | |
1113
f1d7cf0f68e3
optimize $http_x_forwarded_for
Igor Sysoev <igor@sysoev.ru>
parents:
983
diff
changeset
|
113 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP) |
637 | 114 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header, |
533 | 115 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 }, |
499 | 116 #endif |
117 | |
637 | 118 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers, |
533 | 119 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 }, |
515 | 120 |
637 | 121 { ngx_string("content_length"), NULL, ngx_http_variable_header, |
533 | 122 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 }, |
509 | 123 |
637 | 124 { ngx_string("content_type"), NULL, ngx_http_variable_header, |
533 | 125 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 }, |
509 | 126 |
637 | 127 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 }, |
509 | 128 |
982 | 129 { ngx_string("binary_remote_addr"), NULL, |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
130 ngx_http_variable_binary_remote_addr, 0, 0, 0 }, |
982 | 131 |
637 | 132 { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 }, |
499 | 133 |
637 | 134 { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 }, |
509 | 135 |
637 | 136 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 }, |
509 | 137 |
637 | 138 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 }, |
509 | 139 |
637 | 140 { ngx_string("server_protocol"), NULL, ngx_http_variable_request, |
533 | 141 offsetof(ngx_http_request_t, http_protocol), 0, 0 }, |
509 | 142 |
731 | 143 { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 }, |
144 | |
637 | 145 { ngx_string("request_uri"), NULL, ngx_http_variable_request, |
533 | 146 offsetof(ngx_http_request_t, unparsed_uri), 0, 0 }, |
499 | 147 |
637 | 148 { ngx_string("uri"), NULL, ngx_http_variable_request, |
149 offsetof(ngx_http_request_t, uri), | |
1565 | 150 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
637 | 151 |
152 { ngx_string("document_uri"), NULL, ngx_http_variable_request, | |
573 | 153 offsetof(ngx_http_request_t, uri), |
1565 | 154 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
573 | 155 |
637 | 156 { ngx_string("request"), NULL, ngx_http_variable_request, |
569 | 157 offsetof(ngx_http_request_t, request_line), 0, 0 }, |
158 | |
637 | 159 { ngx_string("document_root"), NULL, |
1565 | 160 ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
509 | 161 |
637 | 162 { ngx_string("query_string"), NULL, ngx_http_variable_request, |
509 | 163 offsetof(ngx_http_request_t, args), |
1565 | 164 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
499 | 165 |
1350 | 166 { ngx_string("args"), |
167 ngx_http_variable_request_set, | |
168 ngx_http_variable_request, | |
589 | 169 offsetof(ngx_http_request_t, args), |
1565 | 170 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
589 | 171 |
1351 | 172 { ngx_string("is_args"), NULL, ngx_http_variable_is_args, |
1565 | 173 0, NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
1351 | 174 |
637 | 175 { ngx_string("request_filename"), NULL, |
176 ngx_http_variable_request_filename, 0, | |
1565 | 177 NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
509 | 178 |
1811 | 179 { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 }, |
509 | 180 |
637 | 181 { ngx_string("request_method"), NULL, |
182 ngx_http_variable_request_method, 0, 0, 0 }, | |
509 | 183 |
637 | 184 { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 }, |
509 | 185 |
637 | 186 { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent, |
611 | 187 0, 0, 0 }, |
188 | |
637 | 189 { ngx_string("request_completion"), NULL, |
190 ngx_http_variable_request_completion, | |
629 | 191 0, 0, 0 }, |
192 | |
759 | 193 { ngx_string("request_body_file"), NULL, |
194 ngx_http_variable_request_body_file, | |
195 0, 0, 0 }, | |
196 | |
641 | 197 { ngx_string("sent_http_content_type"), NULL, |
198 ngx_http_variable_sent_content_type, 0, 0, 0 }, | |
199 | |
200 { ngx_string("sent_http_content_length"), NULL, | |
201 ngx_http_variable_sent_content_length, 0, 0, 0 }, | |
202 | |
203 { ngx_string("sent_http_last_modified"), NULL, | |
204 ngx_http_variable_sent_last_modified, 0, 0, 0 }, | |
205 | |
206 { ngx_string("sent_http_connection"), NULL, | |
207 ngx_http_variable_sent_connection, 0, 0, 0 }, | |
208 | |
209 { ngx_string("sent_http_keep_alive"), NULL, | |
210 ngx_http_variable_sent_keep_alive, 0, 0, 0 }, | |
211 | |
212 { ngx_string("sent_http_transfer_encoding"), NULL, | |
213 ngx_http_variable_sent_transfer_encoding, 0, 0, 0 }, | |
214 | |
215 { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers, | |
216 offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 }, | |
217 | |
637 | 218 { ngx_string("limit_rate"), ngx_http_variable_request_set_size, |
219 ngx_http_variable_request, | |
220 offsetof(ngx_http_request_t, limit_rate), | |
1565 | 221 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
637 | 222 |
1329 | 223 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version, |
224 0, 0, 0 }, | |
225 | |
2011 | 226 { ngx_string("hostname"), NULL, ngx_http_variable_hostname, |
227 0, 0, 0 }, | |
228 | |
637 | 229 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
499 | 230 }; |
231 | |
232 | |
577 | 233 ngx_http_variable_value_t ngx_http_variable_null_value = |
234 ngx_http_variable(""); | |
235 ngx_http_variable_value_t ngx_http_variable_true_value = | |
236 ngx_http_variable("1"); | |
237 | |
238 | |
499 | 239 ngx_http_variable_t * |
509 | 240 ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags) |
241 { | |
611 | 242 ngx_int_t rc; |
509 | 243 ngx_uint_t i; |
611 | 244 ngx_hash_key_t *key; |
509 | 245 ngx_http_variable_t *v; |
246 ngx_http_core_main_conf_t *cmcf; | |
247 | |
248 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
249 | |
611 | 250 key = cmcf->variables_keys->keys.elts; |
251 for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) { | |
252 if (name->len != key[i].key.len | |
253 || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0) | |
509 | 254 { |
255 continue; | |
256 } | |
257 | |
611 | 258 v = key[i].value; |
259 | |
1565 | 260 if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) { |
509 | 261 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
262 "the duplicate \"%V\" variable", name); | |
263 return NULL; | |
264 } | |
265 | |
611 | 266 return v; |
509 | 267 } |
268 | |
611 | 269 v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t)); |
509 | 270 if (v == NULL) { |
271 return NULL; | |
272 } | |
273 | |
274 v->name.len = name->len; | |
2049 | 275 v->name.data = ngx_pnalloc(cf->pool, name->len); |
509 | 276 if (v->name.data == NULL) { |
277 return NULL; | |
278 } | |
279 | |
280 for (i = 0; i < name->len; i++) { | |
281 v->name.data[i] = ngx_tolower(name->data[i]); | |
282 } | |
283 | |
637 | 284 v->set_handler = NULL; |
285 v->get_handler = NULL; | |
509 | 286 v->data = 0; |
287 v->flags = flags; | |
533 | 288 v->index = 0; |
509 | 289 |
611 | 290 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0); |
291 | |
292 if (rc == NGX_ERROR) { | |
293 return NULL; | |
294 } | |
295 | |
296 if (rc == NGX_BUSY) { | |
297 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
298 "conflicting variable name \"%V\"", name); | |
299 return NULL; | |
300 } | |
301 | |
509 | 302 return v; |
303 } | |
304 | |
305 | |
306 ngx_int_t | |
307 ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name) | |
499 | 308 { |
501 | 309 ngx_uint_t i; |
310 ngx_http_variable_t *v; | |
499 | 311 ngx_http_core_main_conf_t *cmcf; |
312 | |
313 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
314 | |
501 | 315 v = cmcf->variables.elts; |
316 | |
317 if (v == NULL) { | |
499 | 318 if (ngx_array_init(&cmcf->variables, cf->pool, 4, |
319 sizeof(ngx_http_variable_t)) == NGX_ERROR) | |
320 { | |
509 | 321 return NGX_ERROR; |
499 | 322 } |
501 | 323 |
324 } else { | |
325 for (i = 0; i < cmcf->variables.nelts; i++) { | |
326 if (name->len != v[i].name.len | |
327 || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0) | |
328 { | |
329 continue; | |
330 } | |
331 | |
509 | 332 return i; |
501 | 333 } |
499 | 334 } |
335 | |
501 | 336 v = ngx_array_push(&cmcf->variables); |
337 if (v == NULL) { | |
509 | 338 return NGX_ERROR; |
499 | 339 } |
340 | |
501 | 341 v->name.len = name->len; |
2049 | 342 v->name.data = ngx_pnalloc(cf->pool, name->len); |
501 | 343 if (v->name.data == NULL) { |
509 | 344 return NGX_ERROR; |
501 | 345 } |
499 | 346 |
501 | 347 for (i = 0; i < name->len; i++) { |
509 | 348 v->name.data[i] = ngx_tolower(name->data[i]); |
501 | 349 } |
350 | |
637 | 351 v->set_handler = NULL; |
352 v->get_handler = NULL; | |
501 | 353 v->data = 0; |
509 | 354 v->flags = 0; |
533 | 355 v->index = cmcf->variables.nelts - 1; |
501 | 356 |
509 | 357 return cmcf->variables.nelts - 1; |
499 | 358 } |
359 | |
360 | |
361 ngx_http_variable_value_t * | |
362 ngx_http_get_indexed_variable(ngx_http_request_t *r, ngx_uint_t index) | |
363 { | |
501 | 364 ngx_http_variable_t *v; |
499 | 365 ngx_http_core_main_conf_t *cmcf; |
366 | |
367 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
368 | |
553 | 369 if (cmcf->variables.nelts <= index) { |
499 | 370 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
371 "unknown variable index: %d", index); | |
372 return NULL; | |
373 } | |
374 | |
573 | 375 if (r->variables[index].not_found || r->variables[index].valid) { |
376 return &r->variables[index]; | |
501 | 377 } |
499 | 378 |
501 | 379 v = cmcf->variables.elts; |
499 | 380 |
637 | 381 if (v[index].get_handler(r, &r->variables[index], v[index].data) |
382 == NGX_OK) | |
383 { | |
1565 | 384 if (v[index].flags & NGX_HTTP_VAR_NOCACHEABLE) { |
385 r->variables[index].no_cacheable = 1; | |
499 | 386 } |
573 | 387 |
388 return &r->variables[index]; | |
499 | 389 } |
390 | |
657 | 391 r->variables[index].valid = 0; |
392 r->variables[index].not_found = 1; | |
393 | |
798 | 394 return NULL; |
573 | 395 } |
396 | |
397 | |
398 ngx_http_variable_value_t * | |
399 ngx_http_get_flushed_variable(ngx_http_request_t *r, ngx_uint_t index) | |
400 { | |
1150 | 401 ngx_http_variable_value_t *v; |
573 | 402 |
403 v = &r->variables[index]; | |
404 | |
405 if (v->valid) { | |
1565 | 406 if (!v->no_cacheable) { |
573 | 407 return v; |
408 } | |
409 | |
410 v->valid = 0; | |
411 v->not_found = 0; | |
509 | 412 } |
501 | 413 |
573 | 414 return ngx_http_get_indexed_variable(r, index); |
499 | 415 } |
416 | |
417 | |
418 ngx_http_variable_value_t * | |
635 | 419 ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key, |
420 ngx_uint_t nowarn) | |
499 | 421 { |
501 | 422 ngx_http_variable_t *v; |
573 | 423 ngx_http_variable_value_t *vv; |
499 | 424 ngx_http_core_main_conf_t *cmcf; |
425 | |
426 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); | |
427 | |
611 | 428 v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len); |
499 | 429 |
611 | 430 if (v) { |
431 if (v->flags & NGX_HTTP_VAR_INDEXED) { | |
432 return ngx_http_get_indexed_variable(r, v->index); | |
519 | 433 |
434 } else { | |
499 | 435 |
573 | 436 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); |
501 | 437 |
637 | 438 if (vv && v->get_handler(r, vv, v->data) == NGX_OK) { |
573 | 439 return vv; |
440 } | |
509 | 441 |
573 | 442 return NULL; |
443 } | |
509 | 444 } |
445 | |
446 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); | |
447 if (vv == NULL) { | |
448 return NULL; | |
449 } | |
450 | |
573 | 451 if (ngx_strncmp(name->data, "http_", 5) == 0) { |
499 | 452 |
577 | 453 if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name) |
454 == NGX_OK) | |
455 { | |
456 return vv; | |
457 } | |
458 | |
459 return NULL; | |
460 } | |
461 | |
462 if (ngx_strncmp(name->data, "sent_http_", 10) == 0) { | |
463 | |
464 if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name) | |
465 == NGX_OK) | |
573 | 466 { |
467 return vv; | |
468 } | |
499 | 469 |
470 return NULL; | |
471 } | |
472 | |
1162 | 473 if (ngx_strncmp(name->data, "upstream_http_", 10) == 0) { |
474 | |
475 if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name) | |
476 == NGX_OK) | |
477 { | |
478 return vv; | |
479 } | |
480 | |
481 return NULL; | |
482 } | |
483 | |
635 | 484 vv->not_found = 1; |
573 | 485 |
635 | 486 if (nowarn == 0) { |
487 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
488 "unknown \"%V\" variable", name); | |
489 } | |
499 | 490 |
501 | 491 return vv; |
499 | 492 } |
493 | |
494 | |
573 | 495 static ngx_int_t |
496 ngx_http_variable_request(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
497 uintptr_t data) | |
498 { | |
499 ngx_str_t *s; | |
500 | |
501 s = (ngx_str_t *) ((char *) r + data); | |
502 | |
503 if (s->data) { | |
504 v->len = s->len; | |
505 v->valid = 1; | |
1565 | 506 v->no_cacheable = 0; |
573 | 507 v->not_found = 0; |
508 v->data = s->data; | |
509 | |
510 } else { | |
511 v->not_found = 1; | |
512 } | |
513 | |
514 return NGX_OK; | |
515 } | |
516 | |
517 | |
637 | 518 static void |
1350 | 519 ngx_http_variable_request_set(ngx_http_request_t *r, |
520 ngx_http_variable_value_t *v, uintptr_t data) | |
521 { | |
522 ngx_str_t *s; | |
523 | |
524 s = (ngx_str_t *) ((char *) r + data); | |
525 | |
526 s->len = v->len; | |
527 s->data = v->data; | |
528 } | |
529 | |
530 | |
531 static void | |
637 | 532 ngx_http_variable_request_set_size(ngx_http_request_t *r, |
533 ngx_http_variable_value_t *v, uintptr_t data) | |
534 { | |
535 ssize_t s, *sp; | |
536 ngx_str_t val; | |
537 | |
1310
33d6c994a0b2
Sun Studio on sparc uses different bit order
Igor Sysoev <igor@sysoev.ru>
parents:
1191
diff
changeset
|
538 val.len = v->len; |
637 | 539 val.data = v->data; |
540 | |
541 s = ngx_parse_size(&val); | |
542 | |
543 if (s == NGX_ERROR) { | |
544 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
545 "invalid size \"%V\"", &val); | |
546 return; | |
547 } | |
548 | |
549 sp = (ssize_t *) ((char *) r + data); | |
550 | |
551 *sp = s; | |
552 | |
553 return; | |
554 } | |
555 | |
556 | |
573 | 557 static ngx_int_t |
558 ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
559 uintptr_t data) | |
515 | 560 { |
573 | 561 ngx_table_elt_t *h; |
562 | |
563 h = *(ngx_table_elt_t **) ((char *) r + data); | |
564 | |
565 if (h) { | |
566 v->len = h->value.len; | |
567 v->valid = 1; | |
1565 | 568 v->no_cacheable = 0; |
573 | 569 v->not_found = 0; |
570 v->data = h->value.data; | |
571 | |
572 } else { | |
573 v->not_found = 1; | |
574 } | |
575 | |
576 return NGX_OK; | |
577 } | |
578 | |
579 | |
580 static ngx_int_t | |
581 ngx_http_variable_headers(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
582 uintptr_t data) | |
583 { | |
667 | 584 ssize_t len; |
573 | 585 u_char *p; |
667 | 586 ngx_uint_t i, n; |
573 | 587 ngx_array_t *a; |
588 ngx_table_elt_t **h; | |
515 | 589 |
590 a = (ngx_array_t *) ((char *) r + data); | |
591 | |
667 | 592 n = a->nelts; |
593 | |
594 if (n == 0) { | |
573 | 595 v->not_found = 1; |
596 return NGX_OK; | |
515 | 597 } |
598 | |
573 | 599 v->valid = 1; |
1565 | 600 v->no_cacheable = 0; |
573 | 601 v->not_found = 0; |
515 | 602 |
603 h = a->elts; | |
604 | |
667 | 605 if (n == 1) { |
573 | 606 v->len = (*h)->value.len; |
607 v->data = (*h)->value.data; | |
515 | 608 |
573 | 609 return NGX_OK; |
515 | 610 } |
611 | |
667 | 612 len = - (ssize_t) (sizeof("; ") - 1); |
573 | 613 |
667 | 614 for (i = 0; i < n; i++) { |
573 | 615 len += h[i]->value.len + sizeof("; ") - 1; |
515 | 616 } |
617 | |
2049 | 618 p = ngx_pnalloc(r->pool, len); |
573 | 619 if (p == NULL) { |
620 return NGX_ERROR; | |
621 } | |
622 | |
623 v->len = len; | |
624 v->data = p; | |
515 | 625 |
626 for (i = 0; /* void */ ; i++) { | |
573 | 627 p = ngx_copy(p, h[i]->value.data, h[i]->value.len); |
515 | 628 |
667 | 629 if (i == n - 1) { |
515 | 630 break; |
631 } | |
632 | |
633 *p++ = ';'; *p++ = ' '; | |
634 } | |
635 | |
573 | 636 return NGX_OK; |
515 | 637 } |
638 | |
639 | |
573 | 640 static ngx_int_t |
577 | 641 ngx_http_variable_unknown_header_in(ngx_http_request_t *r, |
642 ngx_http_variable_value_t *v, uintptr_t data) | |
643 { | |
644 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data, | |
645 &r->headers_in.headers.part, | |
646 sizeof("http_") - 1); | |
647 } | |
648 | |
649 | |
650 static ngx_int_t | |
651 ngx_http_variable_unknown_header_out(ngx_http_request_t *r, | |
573 | 652 ngx_http_variable_value_t *v, uintptr_t data) |
499 | 653 { |
577 | 654 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data, |
655 &r->headers_out.headers.part, | |
656 sizeof("sent_http_") - 1); | |
657 } | |
501 | 658 |
577 | 659 |
1162 | 660 ngx_int_t |
577 | 661 ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var, |
662 ngx_list_part_t *part, size_t prefix) | |
663 { | |
573 | 664 u_char ch; |
665 ngx_uint_t i, n; | |
666 ngx_table_elt_t *header; | |
499 | 667 |
668 header = part->elts; | |
669 | |
670 for (i = 0; /* void */ ; i++) { | |
671 | |
672 if (i >= part->nelts) { | |
673 if (part->next == NULL) { | |
674 break; | |
675 } | |
676 | |
677 part = part->next; | |
678 header = part->elts; | |
679 i = 0; | |
680 } | |
681 | |
577 | 682 for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) { |
499 | 683 ch = header[i].key.data[n]; |
684 | |
509 | 685 if (ch >= 'A' && ch <= 'Z') { |
686 ch |= 0x20; | |
499 | 687 |
688 } else if (ch == '-') { | |
689 ch = '_'; | |
690 } | |
691 | |
577 | 692 if (var->data[n + prefix] != ch) { |
499 | 693 break; |
694 } | |
695 } | |
696 | |
742
75d767d32624
nonexistent $sent_http_content has value of $sent_http_content_length or so
Igor Sysoev <igor@sysoev.ru>
parents:
732
diff
changeset
|
697 if (n + prefix == var->len && n == header[i].key.len) { |
573 | 698 v->len = header[i].value.len; |
699 v->valid = 1; | |
1565 | 700 v->no_cacheable = 0; |
573 | 701 v->not_found = 0; |
702 v->data = header[i].value.data; | |
499 | 703 |
573 | 704 return NGX_OK; |
499 | 705 } |
706 } | |
707 | |
573 | 708 v->not_found = 1; |
709 | |
710 return NGX_OK; | |
509 | 711 } |
712 | |
713 | |
573 | 714 static ngx_int_t |
715 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
716 uintptr_t data) | |
509 | 717 { |
1811 | 718 ngx_http_core_srv_conf_t *cscf; |
719 | |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
720 if (r->headers_in.server.len) { |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
721 v->len = r->headers_in.server.len; |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
722 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
|
723 |
a6fe6bedb9e3
fix segfault when $host is used and
Igor Sysoev <igor@sysoev.ru>
parents:
864
diff
changeset
|
724 } else { |
2007
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
725 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
|
726 |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
727 v->len = cscf->server_name.len; |
b9de93d804ea
*) host in request line has priority
Igor Sysoev <igor@sysoev.ru>
parents:
1811
diff
changeset
|
728 v->data = cscf->server_name.data; |
509 | 729 } |
730 | |
573 | 731 v->valid = 1; |
1565 | 732 v->no_cacheable = 0; |
573 | 733 v->not_found = 0; |
509 | 734 |
573 | 735 return NGX_OK; |
499 | 736 } |
737 | |
738 | |
573 | 739 static ngx_int_t |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
740 ngx_http_variable_binary_remote_addr(ngx_http_request_t *r, |
573 | 741 ngx_http_variable_value_t *v, uintptr_t data) |
499 | 742 { |
982 | 743 struct sockaddr_in *sin; |
744 | |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
745 /* AF_INET only */ |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
746 |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
747 sin = (struct sockaddr_in *) r->connection->sockaddr; |
982 | 748 |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
749 v->len = sizeof(in_addr_t); |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
750 v->valid = 1; |
1565 | 751 v->no_cacheable = 0; |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
752 v->not_found = 0; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
753 v->data = (u_char *) &sin->sin_addr.s_addr; |
982 | 754 |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
755 return NGX_OK; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
756 } |
982 | 757 |
758 | |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
759 static ngx_int_t |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
760 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
|
761 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
|
762 { |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
763 v->len = r->connection->addr_text.len; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
764 v->valid = 1; |
1565 | 765 v->no_cacheable = 0; |
983
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
766 v->not_found = 0; |
7a8ca436d611
ngx_http_variable_binary_remote_addr()
Igor Sysoev <igor@sysoev.ru>
parents:
982
diff
changeset
|
767 v->data = r->connection->addr_text.data; |
499 | 768 |
573 | 769 return NGX_OK; |
499 | 770 } |
771 | |
772 | |
573 | 773 static ngx_int_t |
774 ngx_http_variable_remote_port(ngx_http_request_t *r, | |
775 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 776 { |
573 | 777 ngx_uint_t port; |
778 struct sockaddr_in *sin; | |
509 | 779 |
573 | 780 v->len = 0; |
781 v->valid = 1; | |
1565 | 782 v->no_cacheable = 0; |
573 | 783 v->not_found = 0; |
509 | 784 |
2049 | 785 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1); |
573 | 786 if (v->data == NULL) { |
787 return NGX_ERROR; | |
509 | 788 } |
789 | |
790 /* AF_INET only */ | |
577 | 791 |
509 | 792 if (r->connection->sockaddr->sa_family == AF_INET) { |
793 sin = (struct sockaddr_in *) r->connection->sockaddr; | |
577 | 794 |
509 | 795 port = ntohs(sin->sin_port); |
577 | 796 |
509 | 797 if (port > 0 && port < 65536) { |
573 | 798 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; |
509 | 799 } |
800 } | |
801 | |
573 | 802 return NGX_OK; |
509 | 803 } |
804 | |
805 | |
573 | 806 static ngx_int_t |
807 ngx_http_variable_server_addr(ngx_http_request_t *r, | |
808 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 809 { |
1805 | 810 ngx_str_t s; |
499 | 811 |
2049 | 812 s.data = ngx_pnalloc(r->pool, INET_ADDRSTRLEN); |
1805 | 813 if (s.data == NULL) { |
573 | 814 return NGX_ERROR; |
509 | 815 } |
816 | |
1805 | 817 if (ngx_http_server_addr(r, &s) != NGX_OK) { |
818 return NGX_ERROR; | |
509 | 819 } |
820 | |
1805 | 821 v->len = s.len; |
573 | 822 v->valid = 1; |
1565 | 823 v->no_cacheable = 0; |
573 | 824 v->not_found = 0; |
1805 | 825 v->data = s.data; |
499 | 826 |
573 | 827 return NGX_OK; |
499 | 828 } |
829 | |
830 | |
573 | 831 static ngx_int_t |
832 ngx_http_variable_server_port(ngx_http_request_t *r, | |
833 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 834 { |
573 | 835 v->len = r->port_text->len - 1; |
836 v->valid = 1; | |
1565 | 837 v->no_cacheable = 0; |
573 | 838 v->not_found = 0; |
839 v->data = r->port_text->data + 1; | |
509 | 840 |
573 | 841 return NGX_OK; |
509 | 842 } |
843 | |
844 | |
573 | 845 static ngx_int_t |
731 | 846 ngx_http_variable_scheme(ngx_http_request_t *r, |
847 ngx_http_variable_value_t *v, uintptr_t data) | |
848 { | |
849 #if (NGX_HTTP_SSL) | |
850 | |
851 if (r->connection->ssl) { | |
852 v->len = sizeof("https") - 1; | |
853 v->valid = 1; | |
1565 | 854 v->no_cacheable = 0; |
731 | 855 v->not_found = 0; |
732 | 856 v->data = (u_char *) "https"; |
731 | 857 |
858 return NGX_OK; | |
859 } | |
860 | |
861 #endif | |
862 | |
863 v->len = sizeof("http") - 1; | |
864 v->valid = 1; | |
1565 | 865 v->no_cacheable = 0; |
731 | 866 v->not_found = 0; |
732 | 867 v->data = (u_char *) "http"; |
731 | 868 |
869 return NGX_OK; | |
870 } | |
871 | |
872 | |
873 static ngx_int_t | |
1351 | 874 ngx_http_variable_is_args(ngx_http_request_t *r, |
875 ngx_http_variable_value_t *v, uintptr_t data) | |
876 { | |
877 v->valid = 1; | |
1565 | 878 v->no_cacheable = 0; |
1351 | 879 v->not_found = 0; |
880 | |
881 if (r->args.len == 0) { | |
882 v->len = 0; | |
883 v->data = NULL; | |
884 return NGX_OK; | |
885 } | |
886 | |
887 v->len = 1; | |
888 v->data = (u_char *) "?"; | |
889 | |
890 return NGX_OK; | |
891 } | |
892 | |
893 | |
894 static ngx_int_t | |
573 | 895 ngx_http_variable_document_root(ngx_http_request_t *r, |
896 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 897 { |
671 | 898 ngx_str_t path; |
573 | 899 ngx_http_core_loc_conf_t *clcf; |
509 | 900 |
901 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
902 | |
671 | 903 if (clcf->root_lengths == NULL) { |
904 v->len = clcf->root.len; | |
905 v->valid = 1; | |
1565 | 906 v->no_cacheable = 0; |
671 | 907 v->not_found = 0; |
908 v->data = clcf->root.data; | |
909 | |
910 } else { | |
911 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 0, | |
912 clcf->root_values->elts) | |
913 == NULL) | |
914 { | |
915 return NGX_ERROR; | |
916 } | |
917 | |
1352 | 918 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) |
919 == NGX_ERROR) | |
920 { | |
671 | 921 return NGX_ERROR; |
922 } | |
923 | |
924 v->len = path.len; | |
925 v->valid = 1; | |
1565 | 926 v->no_cacheable = 0; |
671 | 927 v->not_found = 0; |
928 v->data = path.data; | |
929 } | |
509 | 930 |
573 | 931 return NGX_OK; |
509 | 932 } |
933 | |
934 | |
573 | 935 static ngx_int_t |
936 ngx_http_variable_request_filename(ngx_http_request_t *r, | |
937 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 938 { |
773 | 939 size_t root; |
573 | 940 ngx_str_t path; |
499 | 941 |
773 | 942 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
573 | 943 return NGX_ERROR; |
557 | 944 } |
509 | 945 |
557 | 946 /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */ |
509 | 947 |
573 | 948 v->len = path.len - 1; |
949 v->valid = 1; | |
1565 | 950 v->no_cacheable = 0; |
573 | 951 v->not_found = 0; |
952 v->data = path.data; | |
499 | 953 |
573 | 954 return NGX_OK; |
499 | 955 } |
956 | |
957 | |
573 | 958 static ngx_int_t |
1811 | 959 ngx_http_variable_server_name(ngx_http_request_t *r, |
960 ngx_http_variable_value_t *v, uintptr_t data) | |
961 { | |
962 ngx_http_core_srv_conf_t *cscf; | |
963 | |
964 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); | |
965 | |
966 v->len = cscf->server_name.len; | |
967 v->valid = 1; | |
968 v->no_cacheable = 0; | |
969 v->not_found = 0; | |
970 v->data = cscf->server_name.data; | |
971 | |
972 return NGX_OK; | |
973 } | |
974 | |
975 | |
976 static ngx_int_t | |
573 | 977 ngx_http_variable_request_method(ngx_http_request_t *r, |
978 ngx_http_variable_value_t *v, uintptr_t data) | |
561 | 979 { |
647 | 980 if (r->main->method_name.data) { |
981 v->len = r->main->method_name.len; | |
573 | 982 v->valid = 1; |
1565 | 983 v->no_cacheable = 0; |
573 | 984 v->not_found = 0; |
647 | 985 v->data = r->main->method_name.data; |
573 | 986 |
987 } else { | |
988 v->not_found = 1; | |
561 | 989 } |
990 | |
573 | 991 return NGX_OK; |
561 | 992 } |
993 | |
994 | |
573 | 995 static ngx_int_t |
996 ngx_http_variable_remote_user(ngx_http_request_t *r, | |
997 ngx_http_variable_value_t *v, uintptr_t data) | |
539 | 998 { |
573 | 999 ngx_int_t rc; |
539 | 1000 |
1001 rc = ngx_http_auth_basic_user(r); | |
1002 | |
1003 if (rc == NGX_DECLINED) { | |
573 | 1004 v->not_found = 1; |
1005 return NGX_OK; | |
539 | 1006 } |
1007 | |
1008 if (rc == NGX_ERROR) { | |
573 | 1009 return NGX_ERROR; |
539 | 1010 } |
1011 | |
573 | 1012 v->len = r->headers_in.user.len; |
1013 v->valid = 1; | |
1565 | 1014 v->no_cacheable = 0; |
573 | 1015 v->not_found = 0; |
1016 v->data = r->headers_in.user.data; | |
571 | 1017 |
573 | 1018 return NGX_OK; |
571 | 1019 } |
1020 | |
1021 | |
611 | 1022 static ngx_int_t |
1023 ngx_http_variable_body_bytes_sent(ngx_http_request_t *r, | |
1024 ngx_http_variable_value_t *v, uintptr_t data) | |
1025 { | |
1026 off_t sent; | |
1027 u_char *p; | |
1028 | |
1029 sent = r->connection->sent - r->header_size; | |
1030 | |
1031 if (sent < 0) { | |
1032 sent = 0; | |
1033 } | |
1034 | |
2049 | 1035 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN); |
611 | 1036 if (p == NULL) { |
1037 return NGX_ERROR; | |
1038 } | |
1039 | |
1040 v->len = ngx_sprintf(p, "%O", sent) - p; | |
1041 v->valid = 1; | |
1565 | 1042 v->no_cacheable = 0; |
611 | 1043 v->not_found = 0; |
1044 v->data = p; | |
1045 | |
1046 return NGX_OK; | |
1047 } | |
1048 | |
1049 | |
629 | 1050 static ngx_int_t |
641 | 1051 ngx_http_variable_sent_content_type(ngx_http_request_t *r, |
1052 ngx_http_variable_value_t *v, uintptr_t data) | |
1053 { | |
1054 if (r->headers_out.content_type.len) { | |
1055 v->len = r->headers_out.content_type.len; | |
1056 v->valid = 1; | |
1565 | 1057 v->no_cacheable = 0; |
641 | 1058 v->not_found = 0; |
1059 v->data = r->headers_out.content_type.data; | |
1060 | |
1061 } else { | |
1062 v->not_found = 1; | |
1063 } | |
1064 | |
1065 return NGX_OK; | |
1066 } | |
1067 | |
1068 | |
1069 static ngx_int_t | |
1070 ngx_http_variable_sent_content_length(ngx_http_request_t *r, | |
1071 ngx_http_variable_value_t *v, uintptr_t data) | |
1072 { | |
1073 u_char *p; | |
1074 | |
1075 if (r->headers_out.content_length) { | |
1076 v->len = r->headers_out.content_length->value.len; | |
1077 v->valid = 1; | |
1565 | 1078 v->no_cacheable = 0; |
641 | 1079 v->not_found = 0; |
1080 v->data = r->headers_out.content_length->value.data; | |
1081 | |
1082 return NGX_OK; | |
1083 } | |
1084 | |
1085 if (r->headers_out.content_length_n >= 0) { | |
2049 | 1086 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN); |
641 | 1087 if (p == NULL) { |
1088 return NGX_ERROR; | |
1089 } | |
1090 | |
1091 v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p; | |
1092 v->valid = 1; | |
1565 | 1093 v->no_cacheable = 0; |
641 | 1094 v->not_found = 0; |
1095 v->data = p; | |
1096 | |
1097 return NGX_OK; | |
1098 } | |
1099 | |
1100 v->not_found = 1; | |
1101 | |
1102 return NGX_OK; | |
1103 } | |
1104 | |
1105 | |
1106 static ngx_int_t | |
1107 ngx_http_variable_sent_last_modified(ngx_http_request_t *r, | |
1108 ngx_http_variable_value_t *v, uintptr_t data) | |
1109 { | |
1110 u_char *p; | |
1111 | |
1112 if (r->headers_out.last_modified) { | |
1113 v->len = r->headers_out.last_modified->value.len; | |
1114 v->valid = 1; | |
1565 | 1115 v->no_cacheable = 0; |
641 | 1116 v->not_found = 0; |
1117 v->data = r->headers_out.last_modified->value.data; | |
1118 | |
1119 return NGX_OK; | |
1120 } | |
1121 | |
1122 if (r->headers_out.last_modified_time >= 0) { | |
2049 | 1123 p = ngx_pnalloc(r->pool, |
641 | 1124 sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1); |
1125 if (p == NULL) { | |
1126 return NGX_ERROR; | |
1127 } | |
1128 | |
1129 v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p; | |
1130 v->valid = 1; | |
1565 | 1131 v->no_cacheable = 0; |
641 | 1132 v->not_found = 0; |
1133 v->data = p; | |
1134 | |
1135 return NGX_OK; | |
1136 } | |
1137 | |
1138 v->not_found = 1; | |
1139 | |
1140 return NGX_OK; | |
1141 } | |
1142 | |
1143 | |
1144 static ngx_int_t | |
1145 ngx_http_variable_sent_connection(ngx_http_request_t *r, | |
1146 ngx_http_variable_value_t *v, uintptr_t data) | |
1147 { | |
1148 size_t len; | |
1149 char *p; | |
1150 | |
1151 if (r->keepalive) { | |
1152 len = sizeof("keep-alive") - 1; | |
1153 p = "keep-alive"; | |
1154 | |
1155 } else { | |
1156 len = sizeof("close") - 1; | |
1157 p = "close"; | |
1158 } | |
1159 | |
1160 v->len = len; | |
1161 v->valid = 1; | |
1565 | 1162 v->no_cacheable = 0; |
641 | 1163 v->not_found = 0; |
1164 v->data = (u_char *) p; | |
1165 | |
1166 return NGX_OK; | |
1167 } | |
1168 | |
1169 | |
1170 static ngx_int_t | |
1171 ngx_http_variable_sent_keep_alive(ngx_http_request_t *r, | |
1172 ngx_http_variable_value_t *v, uintptr_t data) | |
1173 { | |
1174 u_char *p; | |
1175 ngx_http_core_loc_conf_t *clcf; | |
1176 | |
1177 if (r->keepalive) { | |
1178 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1179 | |
1180 if (clcf->keepalive_header) { | |
1181 | |
2049 | 1182 p = ngx_pnalloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN); |
641 | 1183 if (p == NULL) { |
1184 return NGX_ERROR; | |
1185 } | |
1186 | |
1187 v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p; | |
1188 v->valid = 1; | |
1565 | 1189 v->no_cacheable = 0; |
641 | 1190 v->not_found = 0; |
1191 v->data = p; | |
1192 | |
1193 return NGX_OK; | |
1194 } | |
1195 } | |
1196 | |
1197 v->not_found = 1; | |
1198 | |
1199 return NGX_OK; | |
1200 } | |
1201 | |
1202 | |
1203 static ngx_int_t | |
1204 ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r, | |
1205 ngx_http_variable_value_t *v, uintptr_t data) | |
1206 { | |
1207 if (r->chunked) { | |
1208 v->len = sizeof("chunked") - 1; | |
1209 v->valid = 1; | |
1565 | 1210 v->no_cacheable = 0; |
641 | 1211 v->not_found = 0; |
1212 v->data = (u_char *) "chunked"; | |
1213 | |
1214 } else { | |
1215 v->not_found = 1; | |
1216 } | |
1217 | |
1218 return NGX_OK; | |
1219 } | |
1220 | |
1221 | |
1222 static ngx_int_t | |
629 | 1223 ngx_http_variable_request_completion(ngx_http_request_t *r, |
1224 ngx_http_variable_value_t *v, uintptr_t data) | |
1225 { | |
1226 if (r->request_complete) { | |
1227 v->len = 2; | |
1228 v->valid = 1; | |
1565 | 1229 v->no_cacheable = 0; |
629 | 1230 v->not_found = 0; |
1231 v->data = (u_char *) "OK"; | |
1232 | |
1233 return NGX_OK; | |
1234 } | |
1235 | |
1236 v->len = 0; | |
1237 v->valid = 1; | |
1565 | 1238 v->no_cacheable = 0; |
629 | 1239 v->not_found = 0; |
1240 v->data = (u_char *) ""; | |
1241 | |
1242 return NGX_OK; | |
1243 } | |
1244 | |
1245 | |
759 | 1246 static ngx_int_t |
1247 ngx_http_variable_request_body_file(ngx_http_request_t *r, | |
1248 ngx_http_variable_value_t *v, uintptr_t data) | |
1249 { | |
1250 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
|
1251 v->not_found = 1; |
759 | 1252 |
1253 return NGX_OK; | |
1254 } | |
1255 | |
1256 v->len = r->request_body->temp_file->file.name.len; | |
1257 v->valid = 1; | |
1565 | 1258 v->no_cacheable = 0; |
759 | 1259 v->not_found = 0; |
1260 v->data = r->request_body->temp_file->file.name.data; | |
1261 | |
1262 return NGX_OK; | |
1263 } | |
1264 | |
1265 | |
1329 | 1266 static ngx_int_t |
1267 ngx_http_variable_nginx_version(ngx_http_request_t *r, | |
1268 ngx_http_variable_value_t *v, uintptr_t data) | |
1269 { | |
1270 v->len = sizeof(NGINX_VERSION) - 1; | |
1271 v->valid = 1; | |
1565 | 1272 v->no_cacheable = 0; |
1329 | 1273 v->not_found = 0; |
1274 v->data = (u_char *) NGINX_VERSION; | |
1275 | |
1276 return NGX_OK; | |
1277 } | |
1278 | |
1279 | |
2011 | 1280 static ngx_int_t |
1281 ngx_http_variable_hostname(ngx_http_request_t *r, | |
1282 ngx_http_variable_value_t *v, uintptr_t data) | |
1283 { | |
1284 v->len = ngx_cycle->hostname.len; | |
1285 v->valid = 1; | |
1286 v->no_cacheable = 0; | |
1287 v->not_found = 0; | |
1288 v->data = ngx_cycle->hostname.data; | |
1289 | |
1290 return NGX_OK; | |
1291 } | |
1292 | |
1293 | |
499 | 1294 ngx_int_t |
509 | 1295 ngx_http_variables_add_core_vars(ngx_conf_t *cf) |
499 | 1296 { |
611 | 1297 ngx_int_t rc; |
1298 ngx_http_variable_t *v; | |
501 | 1299 ngx_http_core_main_conf_t *cmcf; |
499 | 1300 |
509 | 1301 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
1302 | |
611 | 1303 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, |
1304 sizeof(ngx_hash_keys_arrays_t)); | |
1305 if (cmcf->variables_keys == NULL) { | |
1306 return NGX_ERROR; | |
1307 } | |
1308 | |
1309 cmcf->variables_keys->pool = cf->pool; | |
1310 cmcf->variables_keys->temp_pool = cf->pool; | |
1311 | |
1312 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) | |
1313 != NGX_OK) | |
509 | 1314 { |
499 | 1315 return NGX_ERROR; |
1316 } | |
1317 | |
611 | 1318 for (v = ngx_http_core_variables; v->name.len; v++) { |
1319 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, | |
1320 NGX_HASH_READONLY_KEY); | |
1321 | |
1322 if (rc == NGX_OK) { | |
1323 continue; | |
499 | 1324 } |
1325 | |
611 | 1326 if (rc == NGX_BUSY) { |
1327 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1328 "conflicting variable name \"%V\"", &v->name); | |
1329 } | |
1330 | |
1331 return NGX_ERROR; | |
499 | 1332 } |
1333 | |
1334 return NGX_OK; | |
1335 } | |
509 | 1336 |
1337 | |
1338 ngx_int_t | |
1339 ngx_http_variables_init_vars(ngx_conf_t *cf) | |
1340 { | |
1341 ngx_uint_t i, n; | |
611 | 1342 ngx_hash_key_t *key; |
1343 ngx_hash_init_t hash; | |
509 | 1344 ngx_http_variable_t *v, *av; |
1345 ngx_http_core_main_conf_t *cmcf; | |
1346 | |
1347 /* set the handlers for the indexed http variables */ | |
1348 | |
1349 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
1350 | |
1351 v = cmcf->variables.elts; | |
611 | 1352 key = cmcf->variables_keys->keys.elts; |
509 | 1353 |
1354 for (i = 0; i < cmcf->variables.nelts; i++) { | |
1355 | |
611 | 1356 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { |
509 | 1357 |
1191
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1358 av = key[n].value; |
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1359 |
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1360 if (av->get_handler |
0eb2dc4fdea8
fix segfault introduced in r1190
Igor Sysoev <igor@sysoev.ru>
parents:
1173
diff
changeset
|
1361 && v[i].name.len == key[n].key.len |
611 | 1362 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len) |
509 | 1363 == 0) |
1364 { | |
637 | 1365 v[i].get_handler = av->get_handler; |
611 | 1366 v[i].data = av->data; |
527 | 1367 |
611 | 1368 av->flags |= NGX_HTTP_VAR_INDEXED; |
1369 v[i].flags = av->flags; | |
509 | 1370 |
611 | 1371 av->index = i; |
533 | 1372 |
509 | 1373 goto next; |
1374 } | |
1375 } | |
1376 | |
1377 if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) { | |
637 | 1378 v[i].get_handler = ngx_http_variable_unknown_header_in; |
577 | 1379 v[i].data = (uintptr_t) &v[i].name; |
1380 | |
1381 continue; | |
1382 } | |
1383 | |
1384 if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) { | |
637 | 1385 v[i].get_handler = ngx_http_variable_unknown_header_out; |
509 | 1386 v[i].data = (uintptr_t) &v[i].name; |
1387 | |
1388 continue; | |
1389 } | |
1390 | |
1162 | 1391 if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) { |
1392 v[i].get_handler = ngx_http_upstream_header_variable; | |
1393 v[i].data = (uintptr_t) &v[i].name; | |
1565 | 1394 v[i].flags = NGX_HTTP_VAR_NOCACHEABLE; |
1162 | 1395 |
1396 continue; | |
1397 } | |
1398 | |
509 | 1399 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
1400 "unknown \"%V\" variable", &v[i].name); | |
1401 | |
1402 return NGX_ERROR; | |
1403 | |
1404 next: | |
1405 continue; | |
1406 } | |
1407 | |
1408 | |
611 | 1409 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { |
1410 av = key[n].value; | |
1411 | |
1412 if (av->flags & NGX_HTTP_VAR_NOHASH) { | |
1413 key[n].key.data = NULL; | |
583 | 1414 } |
1415 } | |
1416 | |
1417 | |
611 | 1418 hash.hash = &cmcf->variables_hash; |
1419 hash.key = ngx_hash_key; | |
1420 hash.max_size = cmcf->variables_hash_max_size; | |
1421 hash.bucket_size = cmcf->variables_hash_bucket_size; | |
1422 hash.name = "variables_hash"; | |
1423 hash.pool = cf->pool; | |
1424 hash.temp_pool = NULL; | |
509 | 1425 |
611 | 1426 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts, |
1427 cmcf->variables_keys->keys.nelts) | |
581 | 1428 != NGX_OK) |
509 | 1429 { |
1430 return NGX_ERROR; | |
1431 } | |
1432 | |
611 | 1433 cmcf->variables_keys = NULL; |
509 | 1434 |
1435 return NGX_OK; | |
1436 } |