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