nginx 0.8.26
[nginx.git] / src / http / ngx_http_variables.c
bloba2852b0dd084f8a2171c5a4dc9b681f25c7e6b7c
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_http.h>
10 #include <nginx.h>
13 static ngx_int_t ngx_http_variable_request(ngx_http_request_t *r,
14 ngx_http_variable_value_t *v, uintptr_t data);
15 static void ngx_http_variable_request_set(ngx_http_request_t *r,
16 ngx_http_variable_value_t *v, uintptr_t data);
17 static ngx_int_t ngx_http_variable_request_get_size(ngx_http_request_t *r,
18 ngx_http_variable_value_t *v, uintptr_t data);
19 static void ngx_http_variable_request_set_size(ngx_http_request_t *r,
20 ngx_http_variable_value_t *v, uintptr_t data);
21 static ngx_int_t ngx_http_variable_header(ngx_http_request_t *r,
22 ngx_http_variable_value_t *v, uintptr_t data);
23 static ngx_int_t ngx_http_variable_headers(ngx_http_request_t *r,
24 ngx_http_variable_value_t *v, uintptr_t data);
26 static ngx_int_t ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
27 ngx_http_variable_value_t *v, uintptr_t data);
28 static ngx_int_t ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
29 ngx_http_variable_value_t *v, uintptr_t data);
30 static ngx_int_t ngx_http_variable_request_line(ngx_http_request_t *r,
31 ngx_http_variable_value_t *v, uintptr_t data);
32 static ngx_int_t ngx_http_variable_cookie(ngx_http_request_t *r,
33 ngx_http_variable_value_t *v, uintptr_t data);
34 static ngx_int_t ngx_http_variable_argument(ngx_http_request_t *r,
35 ngx_http_variable_value_t *v, uintptr_t data);
37 static ngx_int_t ngx_http_variable_host(ngx_http_request_t *r,
38 ngx_http_variable_value_t *v, uintptr_t data);
39 static ngx_int_t ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
40 ngx_http_variable_value_t *v, uintptr_t data);
41 static ngx_int_t ngx_http_variable_remote_addr(ngx_http_request_t *r,
42 ngx_http_variable_value_t *v, uintptr_t data);
43 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r,
44 ngx_http_variable_value_t *v, uintptr_t data);
45 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r,
46 ngx_http_variable_value_t *v, uintptr_t data);
47 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r,
48 ngx_http_variable_value_t *v, uintptr_t data);
49 static ngx_int_t ngx_http_variable_scheme(ngx_http_request_t *r,
50 ngx_http_variable_value_t *v, uintptr_t data);
51 static ngx_int_t ngx_http_variable_is_args(ngx_http_request_t *r,
52 ngx_http_variable_value_t *v, uintptr_t data);
53 static ngx_int_t ngx_http_variable_document_root(ngx_http_request_t *r,
54 ngx_http_variable_value_t *v, uintptr_t data);
55 static ngx_int_t ngx_http_variable_realpath_root(ngx_http_request_t *r,
56 ngx_http_variable_value_t *v, uintptr_t data);
57 static ngx_int_t ngx_http_variable_request_filename(ngx_http_request_t *r,
58 ngx_http_variable_value_t *v, uintptr_t data);
59 static ngx_int_t ngx_http_variable_server_name(ngx_http_request_t *r,
60 ngx_http_variable_value_t *v, uintptr_t data);
61 static ngx_int_t ngx_http_variable_request_method(ngx_http_request_t *r,
62 ngx_http_variable_value_t *v, uintptr_t data);
63 static ngx_int_t ngx_http_variable_remote_user(ngx_http_request_t *r,
64 ngx_http_variable_value_t *v, uintptr_t data);
65 static ngx_int_t ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
66 ngx_http_variable_value_t *v, uintptr_t data);
67 static ngx_int_t ngx_http_variable_request_completion(ngx_http_request_t *r,
68 ngx_http_variable_value_t *v, uintptr_t data);
69 static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r,
70 ngx_http_variable_value_t *v, uintptr_t data);
71 static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t *r,
72 ngx_http_variable_value_t *v, uintptr_t data);
74 static ngx_int_t ngx_http_variable_sent_content_type(ngx_http_request_t *r,
75 ngx_http_variable_value_t *v, uintptr_t data);
76 static ngx_int_t ngx_http_variable_sent_content_length(ngx_http_request_t *r,
77 ngx_http_variable_value_t *v, uintptr_t data);
78 static ngx_int_t ngx_http_variable_sent_location(ngx_http_request_t *r,
79 ngx_http_variable_value_t *v, uintptr_t data);
80 static ngx_int_t ngx_http_variable_sent_last_modified(ngx_http_request_t *r,
81 ngx_http_variable_value_t *v, uintptr_t data);
82 static ngx_int_t ngx_http_variable_sent_connection(ngx_http_request_t *r,
83 ngx_http_variable_value_t *v, uintptr_t data);
84 static ngx_int_t ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
85 ngx_http_variable_value_t *v, uintptr_t data);
86 static ngx_int_t ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
87 ngx_http_variable_value_t *v, uintptr_t data);
89 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r,
90 ngx_http_variable_value_t *v, uintptr_t data);
91 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
92 ngx_http_variable_value_t *v, uintptr_t data);
93 static ngx_int_t ngx_http_variable_pid(ngx_http_request_t *r,
94 ngx_http_variable_value_t *v, uintptr_t data);
97 * TODO:
98 * Apache CGI: AUTH_TYPE, PATH_INFO (null), PATH_TRANSLATED
99 * REMOTE_HOST (null), REMOTE_IDENT (null),
100 * SERVER_SOFTWARE
102 * Apache SSI: DOCUMENT_NAME, LAST_MODIFIED, USER_NAME (file owner)
106 * the $http_host, $http_user_agent, $http_referer, $http_via,
107 * and $http_x_forwarded_for variables may be handled by generic
108 * ngx_http_variable_unknown_header_in(), but for perfomance reasons
109 * they are handled using dedicated entries
112 static ngx_http_variable_t ngx_http_core_variables[] = {
114 { ngx_string("http_host"), NULL, ngx_http_variable_header,
115 offsetof(ngx_http_request_t, headers_in.host), 0, 0 },
117 { ngx_string("http_user_agent"), NULL, ngx_http_variable_header,
118 offsetof(ngx_http_request_t, headers_in.user_agent), 0, 0 },
120 { ngx_string("http_referer"), NULL, ngx_http_variable_header,
121 offsetof(ngx_http_request_t, headers_in.referer), 0, 0 },
123 #if (NGX_HTTP_GZIP)
124 { ngx_string("http_via"), NULL, ngx_http_variable_header,
125 offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
126 #endif
128 #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
129 { ngx_string("http_x_forwarded_for"), NULL, ngx_http_variable_header,
130 offsetof(ngx_http_request_t, headers_in.x_forwarded_for), 0, 0 },
131 #endif
133 { ngx_string("http_cookie"), NULL, ngx_http_variable_headers,
134 offsetof(ngx_http_request_t, headers_in.cookies), 0, 0 },
136 { ngx_string("content_length"), NULL, ngx_http_variable_header,
137 offsetof(ngx_http_request_t, headers_in.content_length), 0, 0 },
139 { ngx_string("content_type"), NULL, ngx_http_variable_header,
140 offsetof(ngx_http_request_t, headers_in.content_type), 0, 0 },
142 { ngx_string("host"), NULL, ngx_http_variable_host, 0, 0, 0 },
144 { ngx_string("binary_remote_addr"), NULL,
145 ngx_http_variable_binary_remote_addr, 0, 0, 0 },
147 { ngx_string("remote_addr"), NULL, ngx_http_variable_remote_addr, 0, 0, 0 },
149 { ngx_string("remote_port"), NULL, ngx_http_variable_remote_port, 0, 0, 0 },
151 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },
153 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },
155 { ngx_string("server_protocol"), NULL, ngx_http_variable_request,
156 offsetof(ngx_http_request_t, http_protocol), 0, 0 },
158 { ngx_string("scheme"), NULL, ngx_http_variable_scheme, 0, 0, 0 },
160 { ngx_string("request_uri"), NULL, ngx_http_variable_request,
161 offsetof(ngx_http_request_t, unparsed_uri), 0, 0 },
163 { ngx_string("uri"), NULL, ngx_http_variable_request,
164 offsetof(ngx_http_request_t, uri),
165 NGX_HTTP_VAR_NOCACHEABLE, 0 },
167 { ngx_string("document_uri"), NULL, ngx_http_variable_request,
168 offsetof(ngx_http_request_t, uri),
169 NGX_HTTP_VAR_NOCACHEABLE, 0 },
171 { ngx_string("request"), NULL, ngx_http_variable_request_line, 0, 0, 0 },
173 { ngx_string("document_root"), NULL,
174 ngx_http_variable_document_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
176 { ngx_string("realpath_root"), NULL,
177 ngx_http_variable_realpath_root, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
179 { ngx_string("query_string"), NULL, ngx_http_variable_request,
180 offsetof(ngx_http_request_t, args),
181 NGX_HTTP_VAR_NOCACHEABLE, 0 },
183 { ngx_string("args"),
184 ngx_http_variable_request_set,
185 ngx_http_variable_request,
186 offsetof(ngx_http_request_t, args),
187 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
189 { ngx_string("is_args"), NULL, ngx_http_variable_is_args,
190 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
192 { ngx_string("request_filename"), NULL,
193 ngx_http_variable_request_filename, 0,
194 NGX_HTTP_VAR_NOCACHEABLE, 0 },
196 { ngx_string("server_name"), NULL, ngx_http_variable_server_name, 0, 0, 0 },
198 { ngx_string("request_method"), NULL,
199 ngx_http_variable_request_method, 0, 0, 0 },
201 { ngx_string("remote_user"), NULL, ngx_http_variable_remote_user, 0, 0, 0 },
203 { ngx_string("body_bytes_sent"), NULL, ngx_http_variable_body_bytes_sent,
204 0, 0, 0 },
206 { ngx_string("request_completion"), NULL,
207 ngx_http_variable_request_completion,
208 0, 0, 0 },
210 { ngx_string("request_body"), NULL,
211 ngx_http_variable_request_body,
212 0, 0, 0 },
214 { ngx_string("request_body_file"), NULL,
215 ngx_http_variable_request_body_file,
216 0, 0, 0 },
218 { ngx_string("sent_http_content_type"), NULL,
219 ngx_http_variable_sent_content_type, 0, 0, 0 },
221 { ngx_string("sent_http_content_length"), NULL,
222 ngx_http_variable_sent_content_length, 0, 0, 0 },
224 { ngx_string("sent_http_location"), NULL,
225 ngx_http_variable_sent_location, 0, 0, 0 },
227 { ngx_string("sent_http_last_modified"), NULL,
228 ngx_http_variable_sent_last_modified, 0, 0, 0 },
230 { ngx_string("sent_http_connection"), NULL,
231 ngx_http_variable_sent_connection, 0, 0, 0 },
233 { ngx_string("sent_http_keep_alive"), NULL,
234 ngx_http_variable_sent_keep_alive, 0, 0, 0 },
236 { ngx_string("sent_http_transfer_encoding"), NULL,
237 ngx_http_variable_sent_transfer_encoding, 0, 0, 0 },
239 { ngx_string("sent_http_cache_control"), NULL, ngx_http_variable_headers,
240 offsetof(ngx_http_request_t, headers_out.cache_control), 0, 0 },
242 { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
243 ngx_http_variable_request_get_size,
244 offsetof(ngx_http_request_t, limit_rate),
245 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 },
247 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
248 0, 0, 0 },
250 { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
251 0, 0, 0 },
253 { ngx_string("pid"), NULL, ngx_http_variable_pid,
254 0, 0, 0 },
256 { ngx_null_string, NULL, NULL, 0, 0, 0 }
260 ngx_http_variable_value_t ngx_http_variable_null_value =
261 ngx_http_variable("");
262 ngx_http_variable_value_t ngx_http_variable_true_value =
263 ngx_http_variable("1");
266 ngx_http_variable_t *
267 ngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)
269 ngx_int_t rc;
270 ngx_uint_t i;
271 ngx_hash_key_t *key;
272 ngx_http_variable_t *v;
273 ngx_http_core_main_conf_t *cmcf;
275 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
277 key = cmcf->variables_keys->keys.elts;
278 for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {
279 if (name->len != key[i].key.len
280 || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)
282 continue;
285 v = key[i].value;
287 if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
288 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
289 "the duplicate \"%V\" variable", name);
290 return NULL;
293 return v;
296 v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));
297 if (v == NULL) {
298 return NULL;
301 v->name.len = name->len;
302 v->name.data = ngx_pnalloc(cf->pool, name->len);
303 if (v->name.data == NULL) {
304 return NULL;
307 ngx_strlow(v->name.data, name->data, name->len);
309 v->set_handler = NULL;
310 v->get_handler = NULL;
311 v->data = 0;
312 v->flags = flags;
313 v->index = 0;
315 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);
317 if (rc == NGX_ERROR) {
318 return NULL;
321 if (rc == NGX_BUSY) {
322 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
323 "conflicting variable name \"%V\"", name);
324 return NULL;
327 return v;
331 ngx_int_t
332 ngx_http_get_variable_index(ngx_conf_t *cf, ngx_str_t *name)
334 ngx_uint_t i;
335 ngx_http_variable_t *v;
336 ngx_http_core_main_conf_t *cmcf;
338 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
340 v = cmcf->variables.elts;
342 if (v == NULL) {
343 if (ngx_array_init(&cmcf->variables, cf->pool, 4,
344 sizeof(ngx_http_variable_t))
345 != NGX_OK)
347 return NGX_ERROR;
350 } else {
351 for (i = 0; i < cmcf->variables.nelts; i++) {
352 if (name->len != v[i].name.len
353 || ngx_strncasecmp(name->data, v[i].name.data, name->len) != 0)
355 continue;
358 return i;
362 v = ngx_array_push(&cmcf->variables);
363 if (v == NULL) {
364 return NGX_ERROR;
367 v->name.len = name->len;
368 v->name.data = ngx_pnalloc(cf->pool, name->len);
369 if (v->name.data == NULL) {
370 return NGX_ERROR;
373 ngx_strlow(v->name.data, name->data, name->len);
375 v->set_handler = NULL;
376 v->get_handler = NULL;
377 v->data = 0;
378 v->flags = 0;
379 v->index = cmcf->variables.nelts - 1;
381 return cmcf->variables.nelts - 1;
385 ngx_http_variable_value_t *
386 ngx_http_get_indexed_variable(ngx_http_request_t *r, ngx_uint_t index)
388 ngx_http_variable_t *v;
389 ngx_http_core_main_conf_t *cmcf;
391 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
393 if (cmcf->variables.nelts <= index) {
394 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
395 "unknown variable index: %d", index);
396 return NULL;
399 if (r->variables[index].not_found || r->variables[index].valid) {
400 return &r->variables[index];
403 v = cmcf->variables.elts;
405 if (v[index].get_handler(r, &r->variables[index], v[index].data)
406 == NGX_OK)
408 if (v[index].flags & NGX_HTTP_VAR_NOCACHEABLE) {
409 r->variables[index].no_cacheable = 1;
412 return &r->variables[index];
415 r->variables[index].valid = 0;
416 r->variables[index].not_found = 1;
418 return NULL;
422 ngx_http_variable_value_t *
423 ngx_http_get_flushed_variable(ngx_http_request_t *r, ngx_uint_t index)
425 ngx_http_variable_value_t *v;
427 v = &r->variables[index];
429 if (v->valid) {
430 if (!v->no_cacheable) {
431 return v;
434 v->valid = 0;
435 v->not_found = 0;
438 return ngx_http_get_indexed_variable(r, index);
442 ngx_http_variable_value_t *
443 ngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key,
444 ngx_uint_t nowarn)
446 ngx_http_variable_t *v;
447 ngx_http_variable_value_t *vv;
448 ngx_http_core_main_conf_t *cmcf;
450 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
452 v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len);
454 if (v) {
455 if (v->flags & NGX_HTTP_VAR_INDEXED) {
456 return ngx_http_get_indexed_variable(r, v->index);
458 } else {
460 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
462 if (vv && v->get_handler(r, vv, v->data) == NGX_OK) {
463 return vv;
466 return NULL;
470 vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
471 if (vv == NULL) {
472 return NULL;
475 if (ngx_strncmp(name->data, "http_", 5) == 0) {
477 if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name)
478 == NGX_OK)
480 return vv;
483 return NULL;
486 if (ngx_strncmp(name->data, "sent_http_", 10) == 0) {
488 if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name)
489 == NGX_OK)
491 return vv;
494 return NULL;
497 if (ngx_strncmp(name->data, "upstream_http_", 10) == 0) {
499 if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name)
500 == NGX_OK)
502 return vv;
505 return NULL;
508 if (ngx_strncmp(name->data, "cookie_", 7) == 0) {
510 if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) {
511 return vv;
514 return NULL;
517 if (ngx_strncmp(name->data, "arg_", 4) == 0) {
519 if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) {
520 return vv;
523 return NULL;
526 vv->not_found = 1;
528 if (nowarn == 0) {
529 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
530 "unknown \"%V\" variable", name);
533 return vv;
537 static ngx_int_t
538 ngx_http_variable_request(ngx_http_request_t *r, ngx_http_variable_value_t *v,
539 uintptr_t data)
541 ngx_str_t *s;
543 s = (ngx_str_t *) ((char *) r + data);
545 if (s->data) {
546 v->len = s->len;
547 v->valid = 1;
548 v->no_cacheable = 0;
549 v->not_found = 0;
550 v->data = s->data;
552 } else {
553 v->not_found = 1;
556 return NGX_OK;
560 static void
561 ngx_http_variable_request_set(ngx_http_request_t *r,
562 ngx_http_variable_value_t *v, uintptr_t data)
564 ngx_str_t *s;
566 s = (ngx_str_t *) ((char *) r + data);
568 s->len = v->len;
569 s->data = v->data;
573 static ngx_int_t
574 ngx_http_variable_request_get_size(ngx_http_request_t *r,
575 ngx_http_variable_value_t *v, uintptr_t data)
577 size_t *sp;
579 sp = (size_t *) ((char *) r + data);
581 v->data = ngx_pnalloc(r->pool, NGX_SIZE_T_LEN);
582 if (v->data == NULL) {
583 return NGX_ERROR;
586 v->len = ngx_sprintf(v->data, "%uz", *sp) - v->data;
587 v->valid = 1;
588 v->no_cacheable = 0;
589 v->not_found = 0;
591 return NGX_OK;
595 static void
596 ngx_http_variable_request_set_size(ngx_http_request_t *r,
597 ngx_http_variable_value_t *v, uintptr_t data)
599 ssize_t s, *sp;
600 ngx_str_t val;
602 val.len = v->len;
603 val.data = v->data;
605 s = ngx_parse_size(&val);
607 if (s == NGX_ERROR) {
608 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
609 "invalid size \"%V\"", &val);
610 return;
613 sp = (ssize_t *) ((char *) r + data);
615 *sp = s;
617 return;
621 static ngx_int_t
622 ngx_http_variable_header(ngx_http_request_t *r, ngx_http_variable_value_t *v,
623 uintptr_t data)
625 ngx_table_elt_t *h;
627 h = *(ngx_table_elt_t **) ((char *) r + data);
629 if (h) {
630 v->len = h->value.len;
631 v->valid = 1;
632 v->no_cacheable = 0;
633 v->not_found = 0;
634 v->data = h->value.data;
636 } else {
637 v->not_found = 1;
640 return NGX_OK;
644 static ngx_int_t
645 ngx_http_variable_headers(ngx_http_request_t *r, ngx_http_variable_value_t *v,
646 uintptr_t data)
648 ssize_t len;
649 u_char *p;
650 ngx_uint_t i, n;
651 ngx_array_t *a;
652 ngx_table_elt_t **h;
654 a = (ngx_array_t *) ((char *) r + data);
656 n = a->nelts;
658 if (n == 0) {
659 v->not_found = 1;
660 return NGX_OK;
663 v->valid = 1;
664 v->no_cacheable = 0;
665 v->not_found = 0;
667 h = a->elts;
669 if (n == 1) {
670 v->len = (*h)->value.len;
671 v->data = (*h)->value.data;
673 return NGX_OK;
676 len = - (ssize_t) (sizeof("; ") - 1);
678 for (i = 0; i < n; i++) {
679 len += h[i]->value.len + sizeof("; ") - 1;
682 p = ngx_pnalloc(r->pool, len);
683 if (p == NULL) {
684 return NGX_ERROR;
687 v->len = len;
688 v->data = p;
690 for (i = 0; /* void */ ; i++) {
691 p = ngx_copy(p, h[i]->value.data, h[i]->value.len);
693 if (i == n - 1) {
694 break;
697 *p++ = ';'; *p++ = ' ';
700 return NGX_OK;
704 static ngx_int_t
705 ngx_http_variable_unknown_header_in(ngx_http_request_t *r,
706 ngx_http_variable_value_t *v, uintptr_t data)
708 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
709 &r->headers_in.headers.part,
710 sizeof("http_") - 1);
714 static ngx_int_t
715 ngx_http_variable_unknown_header_out(ngx_http_request_t *r,
716 ngx_http_variable_value_t *v, uintptr_t data)
718 return ngx_http_variable_unknown_header(v, (ngx_str_t *) data,
719 &r->headers_out.headers.part,
720 sizeof("sent_http_") - 1);
724 ngx_int_t
725 ngx_http_variable_unknown_header(ngx_http_variable_value_t *v, ngx_str_t *var,
726 ngx_list_part_t *part, size_t prefix)
728 u_char ch;
729 ngx_uint_t i, n;
730 ngx_table_elt_t *header;
732 header = part->elts;
734 for (i = 0; /* void */ ; i++) {
736 if (i >= part->nelts) {
737 if (part->next == NULL) {
738 break;
741 part = part->next;
742 header = part->elts;
743 i = 0;
746 for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) {
747 ch = header[i].key.data[n];
749 if (ch >= 'A' && ch <= 'Z') {
750 ch |= 0x20;
752 } else if (ch == '-') {
753 ch = '_';
756 if (var->data[n + prefix] != ch) {
757 break;
761 if (n + prefix == var->len && n == header[i].key.len) {
762 v->len = header[i].value.len;
763 v->valid = 1;
764 v->no_cacheable = 0;
765 v->not_found = 0;
766 v->data = header[i].value.data;
768 return NGX_OK;
772 v->not_found = 1;
774 return NGX_OK;
778 static ngx_int_t
779 ngx_http_variable_request_line(ngx_http_request_t *r,
780 ngx_http_variable_value_t *v, uintptr_t data)
782 u_char *p, *s;
784 s = r->request_line.data;
786 if (s == NULL) {
787 s = r->request_start;
789 if (s == NULL) {
790 v->not_found = 1;
791 return NGX_OK;
794 for (p = s; p < r->header_in->last; p++) {
795 if (*p == CR || *p == LF) {
796 break;
800 r->request_line.len = p - s;
801 r->request_line.data = s;
804 v->len = r->request_line.len;
805 v->valid = 1;
806 v->no_cacheable = 0;
807 v->not_found = 0;
808 v->data = s;
810 return NGX_OK;
814 static ngx_int_t
815 ngx_http_variable_cookie(ngx_http_request_t *r, ngx_http_variable_value_t *v,
816 uintptr_t data)
818 ngx_str_t *name = (ngx_str_t *) data;
820 ngx_str_t cookie, s;
822 s.len = name->len - (sizeof("cookie_") - 1);
823 s.data = name->data + sizeof("cookie_") - 1;
825 if (ngx_http_parse_multi_header_lines(&r->headers_in.cookies, &s, &cookie)
826 == NGX_DECLINED)
828 v->not_found = 1;
829 return NGX_OK;
832 v->len = cookie.len;
833 v->valid = 1;
834 v->no_cacheable = 0;
835 v->not_found = 0;
836 v->data = cookie.data;
838 return NGX_OK;
842 static ngx_int_t
843 ngx_http_variable_argument(ngx_http_request_t *r, ngx_http_variable_value_t *v,
844 uintptr_t data)
846 ngx_str_t *name = (ngx_str_t *) data;
848 u_char *arg;
849 size_t len;
850 ngx_str_t value;
852 len = name->len - (sizeof("arg_") - 1);
853 arg = name->data + sizeof("arg_") - 1;
855 if (ngx_http_arg(r, arg, len, &value) != NGX_OK) {
856 v->not_found = 1;
857 return NGX_OK;
860 v->data = value.data;
861 v->len = value.len;
862 v->valid = 1;
863 v->no_cacheable = 0;
864 v->not_found = 0;
866 return NGX_OK;
870 static ngx_int_t
871 ngx_http_variable_host(ngx_http_request_t *r, ngx_http_variable_value_t *v,
872 uintptr_t data)
874 ngx_http_core_srv_conf_t *cscf;
876 if (r->headers_in.server.len) {
877 v->len = r->headers_in.server.len;
878 v->data = r->headers_in.server.data;
880 } else {
881 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
883 v->len = cscf->server_name.len;
884 v->data = cscf->server_name.data;
887 v->valid = 1;
888 v->no_cacheable = 0;
889 v->not_found = 0;
891 return NGX_OK;
895 static ngx_int_t
896 ngx_http_variable_binary_remote_addr(ngx_http_request_t *r,
897 ngx_http_variable_value_t *v, uintptr_t data)
899 struct sockaddr_in *sin;
900 #if (NGX_HAVE_INET6)
901 struct sockaddr_in6 *sin6;
902 #endif
904 switch (r->connection->sockaddr->sa_family) {
906 #if (NGX_HAVE_INET6)
907 case AF_INET6:
908 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
910 v->len = sizeof(struct in6_addr);
911 v->valid = 1;
912 v->no_cacheable = 0;
913 v->not_found = 0;
914 v->data = sin6->sin6_addr.s6_addr;
916 break;
917 #endif
919 default: /* AF_INET */
920 sin = (struct sockaddr_in *) r->connection->sockaddr;
922 v->len = sizeof(in_addr_t);
923 v->valid = 1;
924 v->no_cacheable = 0;
925 v->not_found = 0;
926 v->data = (u_char *) &sin->sin_addr;
928 break;
931 return NGX_OK;
935 static ngx_int_t
936 ngx_http_variable_remote_addr(ngx_http_request_t *r,
937 ngx_http_variable_value_t *v, uintptr_t data)
939 v->len = r->connection->addr_text.len;
940 v->valid = 1;
941 v->no_cacheable = 0;
942 v->not_found = 0;
943 v->data = r->connection->addr_text.data;
945 return NGX_OK;
949 static ngx_int_t
950 ngx_http_variable_remote_port(ngx_http_request_t *r,
951 ngx_http_variable_value_t *v, uintptr_t data)
953 ngx_uint_t port;
954 struct sockaddr_in *sin;
955 #if (NGX_HAVE_INET6)
956 struct sockaddr_in6 *sin6;
957 #endif
959 v->len = 0;
960 v->valid = 1;
961 v->no_cacheable = 0;
962 v->not_found = 0;
964 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
965 if (v->data == NULL) {
966 return NGX_ERROR;
969 switch (r->connection->sockaddr->sa_family) {
971 #if (NGX_HAVE_INET6)
972 case AF_INET6:
973 sin6 = (struct sockaddr_in6 *) r->connection->sockaddr;
974 port = ntohs(sin6->sin6_port);
975 break;
976 #endif
978 default: /* AF_INET */
979 sin = (struct sockaddr_in *) r->connection->sockaddr;
980 port = ntohs(sin->sin_port);
981 break;
984 if (port > 0 && port < 65536) {
985 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
988 return NGX_OK;
992 static ngx_int_t
993 ngx_http_variable_server_addr(ngx_http_request_t *r,
994 ngx_http_variable_value_t *v, uintptr_t data)
996 ngx_str_t s;
997 u_char addr[NGX_SOCKADDR_STRLEN];
999 s.len = NGX_SOCKADDR_STRLEN;
1000 s.data = addr;
1002 if (ngx_connection_local_sockaddr(r->connection, &s, 0) != NGX_OK) {
1003 return NGX_ERROR;
1006 s.data = ngx_pnalloc(r->pool, s.len);
1007 if (s.data == NULL) {
1008 return NGX_ERROR;
1011 ngx_memcpy(s.data, addr, s.len);
1013 v->len = s.len;
1014 v->valid = 1;
1015 v->no_cacheable = 0;
1016 v->not_found = 0;
1017 v->data = s.data;
1019 return NGX_OK;
1023 static ngx_int_t
1024 ngx_http_variable_server_port(ngx_http_request_t *r,
1025 ngx_http_variable_value_t *v, uintptr_t data)
1027 ngx_uint_t port;
1028 struct sockaddr_in *sin;
1029 #if (NGX_HAVE_INET6)
1030 struct sockaddr_in6 *sin6;
1031 #endif
1033 v->len = 0;
1034 v->valid = 1;
1035 v->no_cacheable = 0;
1036 v->not_found = 0;
1038 if (ngx_connection_local_sockaddr(r->connection, NULL, 0) != NGX_OK) {
1039 return NGX_ERROR;
1042 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
1043 if (v->data == NULL) {
1044 return NGX_ERROR;
1047 switch (r->connection->local_sockaddr->sa_family) {
1049 #if (NGX_HAVE_INET6)
1050 case AF_INET6:
1051 sin6 = (struct sockaddr_in6 *) r->connection->local_sockaddr;
1052 port = ntohs(sin6->sin6_port);
1053 break;
1054 #endif
1056 default: /* AF_INET */
1057 sin = (struct sockaddr_in *) r->connection->local_sockaddr;
1058 port = ntohs(sin->sin_port);
1059 break;
1062 if (port > 0 && port < 65536) {
1063 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
1066 return NGX_OK;
1070 static ngx_int_t
1071 ngx_http_variable_scheme(ngx_http_request_t *r,
1072 ngx_http_variable_value_t *v, uintptr_t data)
1074 #if (NGX_HTTP_SSL)
1076 if (r->connection->ssl) {
1077 v->len = sizeof("https") - 1;
1078 v->valid = 1;
1079 v->no_cacheable = 0;
1080 v->not_found = 0;
1081 v->data = (u_char *) "https";
1083 return NGX_OK;
1086 #endif
1088 v->len = sizeof("http") - 1;
1089 v->valid = 1;
1090 v->no_cacheable = 0;
1091 v->not_found = 0;
1092 v->data = (u_char *) "http";
1094 return NGX_OK;
1098 static ngx_int_t
1099 ngx_http_variable_is_args(ngx_http_request_t *r,
1100 ngx_http_variable_value_t *v, uintptr_t data)
1102 v->valid = 1;
1103 v->no_cacheable = 0;
1104 v->not_found = 0;
1106 if (r->args.len == 0) {
1107 v->len = 0;
1108 v->data = NULL;
1109 return NGX_OK;
1112 v->len = 1;
1113 v->data = (u_char *) "?";
1115 return NGX_OK;
1119 static ngx_int_t
1120 ngx_http_variable_document_root(ngx_http_request_t *r,
1121 ngx_http_variable_value_t *v, uintptr_t data)
1123 ngx_str_t path;
1124 ngx_http_core_loc_conf_t *clcf;
1126 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1128 if (clcf->root_lengths == NULL) {
1129 v->len = clcf->root.len;
1130 v->valid = 1;
1131 v->no_cacheable = 0;
1132 v->not_found = 0;
1133 v->data = clcf->root.data;
1135 } else {
1136 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 0,
1137 clcf->root_values->elts)
1138 == NULL)
1140 return NGX_ERROR;
1143 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) != NGX_OK) {
1144 return NGX_ERROR;
1147 v->len = path.len;
1148 v->valid = 1;
1149 v->no_cacheable = 0;
1150 v->not_found = 0;
1151 v->data = path.data;
1154 return NGX_OK;
1158 static ngx_int_t
1159 ngx_http_variable_realpath_root(ngx_http_request_t *r,
1160 ngx_http_variable_value_t *v, uintptr_t data)
1162 size_t len;
1163 ngx_str_t path;
1164 ngx_http_core_loc_conf_t *clcf;
1165 u_char real[NGX_MAX_PATH];
1167 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1169 if (clcf->root_lengths == NULL) {
1170 path = clcf->root;
1172 } else {
1173 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 1,
1174 clcf->root_values->elts)
1175 == NULL)
1177 return NGX_ERROR;
1180 path.data[path.len - 1] = '\0';
1182 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path, 0) != NGX_OK) {
1183 return NGX_ERROR;
1187 if (ngx_realpath(path.data, real) == NULL) {
1188 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
1189 ngx_realpath_n " \"%s\" failed", path.data);
1190 return NGX_ERROR;
1193 len = ngx_strlen(real);
1195 v->data = ngx_pnalloc(r->pool, len);
1196 if (v->data == NULL) {
1197 return NGX_ERROR;
1200 v->len = len;
1201 v->valid = 1;
1202 v->no_cacheable = 0;
1203 v->not_found = 0;
1205 ngx_memcpy(v->data, real, len);
1207 return NGX_OK;
1211 static ngx_int_t
1212 ngx_http_variable_request_filename(ngx_http_request_t *r,
1213 ngx_http_variable_value_t *v, uintptr_t data)
1215 size_t root;
1216 ngx_str_t path;
1218 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
1219 return NGX_ERROR;
1222 /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */
1224 v->len = path.len - 1;
1225 v->valid = 1;
1226 v->no_cacheable = 0;
1227 v->not_found = 0;
1228 v->data = path.data;
1230 return NGX_OK;
1234 static ngx_int_t
1235 ngx_http_variable_server_name(ngx_http_request_t *r,
1236 ngx_http_variable_value_t *v, uintptr_t data)
1238 ngx_http_core_srv_conf_t *cscf;
1240 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1242 v->len = cscf->server_name.len;
1243 v->valid = 1;
1244 v->no_cacheable = 0;
1245 v->not_found = 0;
1246 v->data = cscf->server_name.data;
1248 return NGX_OK;
1252 static ngx_int_t
1253 ngx_http_variable_request_method(ngx_http_request_t *r,
1254 ngx_http_variable_value_t *v, uintptr_t data)
1256 if (r->main->method_name.data) {
1257 v->len = r->main->method_name.len;
1258 v->valid = 1;
1259 v->no_cacheable = 0;
1260 v->not_found = 0;
1261 v->data = r->main->method_name.data;
1263 } else {
1264 v->not_found = 1;
1267 return NGX_OK;
1271 static ngx_int_t
1272 ngx_http_variable_remote_user(ngx_http_request_t *r,
1273 ngx_http_variable_value_t *v, uintptr_t data)
1275 ngx_int_t rc;
1277 rc = ngx_http_auth_basic_user(r);
1279 if (rc == NGX_DECLINED) {
1280 v->not_found = 1;
1281 return NGX_OK;
1284 if (rc == NGX_ERROR) {
1285 return NGX_ERROR;
1288 v->len = r->headers_in.user.len;
1289 v->valid = 1;
1290 v->no_cacheable = 0;
1291 v->not_found = 0;
1292 v->data = r->headers_in.user.data;
1294 return NGX_OK;
1298 static ngx_int_t
1299 ngx_http_variable_body_bytes_sent(ngx_http_request_t *r,
1300 ngx_http_variable_value_t *v, uintptr_t data)
1302 off_t sent;
1303 u_char *p;
1305 sent = r->connection->sent - r->header_size;
1307 if (sent < 0) {
1308 sent = 0;
1311 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
1312 if (p == NULL) {
1313 return NGX_ERROR;
1316 v->len = ngx_sprintf(p, "%O", sent) - p;
1317 v->valid = 1;
1318 v->no_cacheable = 0;
1319 v->not_found = 0;
1320 v->data = p;
1322 return NGX_OK;
1326 static ngx_int_t
1327 ngx_http_variable_sent_content_type(ngx_http_request_t *r,
1328 ngx_http_variable_value_t *v, uintptr_t data)
1330 if (r->headers_out.content_type.len) {
1331 v->len = r->headers_out.content_type.len;
1332 v->valid = 1;
1333 v->no_cacheable = 0;
1334 v->not_found = 0;
1335 v->data = r->headers_out.content_type.data;
1337 } else {
1338 v->not_found = 1;
1341 return NGX_OK;
1345 static ngx_int_t
1346 ngx_http_variable_sent_content_length(ngx_http_request_t *r,
1347 ngx_http_variable_value_t *v, uintptr_t data)
1349 u_char *p;
1351 if (r->headers_out.content_length) {
1352 v->len = r->headers_out.content_length->value.len;
1353 v->valid = 1;
1354 v->no_cacheable = 0;
1355 v->not_found = 0;
1356 v->data = r->headers_out.content_length->value.data;
1358 return NGX_OK;
1361 if (r->headers_out.content_length_n >= 0) {
1362 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN);
1363 if (p == NULL) {
1364 return NGX_ERROR;
1367 v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p;
1368 v->valid = 1;
1369 v->no_cacheable = 0;
1370 v->not_found = 0;
1371 v->data = p;
1373 return NGX_OK;
1376 v->not_found = 1;
1378 return NGX_OK;
1382 static ngx_int_t
1383 ngx_http_variable_sent_location(ngx_http_request_t *r,
1384 ngx_http_variable_value_t *v, uintptr_t data)
1386 ngx_str_t name;
1388 if (r->headers_out.location) {
1389 v->len = r->headers_out.location->value.len;
1390 v->valid = 1;
1391 v->no_cacheable = 0;
1392 v->not_found = 0;
1393 v->data = r->headers_out.location->value.data;
1395 return NGX_OK;
1398 name.len = sizeof("sent_http_location") - 1;
1399 name.data = (u_char *) "sent_http_location";
1401 return ngx_http_variable_unknown_header(v, &name,
1402 &r->headers_out.headers.part,
1403 sizeof("sent_http_") - 1);
1407 static ngx_int_t
1408 ngx_http_variable_sent_last_modified(ngx_http_request_t *r,
1409 ngx_http_variable_value_t *v, uintptr_t data)
1411 u_char *p;
1413 if (r->headers_out.last_modified) {
1414 v->len = r->headers_out.last_modified->value.len;
1415 v->valid = 1;
1416 v->no_cacheable = 0;
1417 v->not_found = 0;
1418 v->data = r->headers_out.last_modified->value.data;
1420 return NGX_OK;
1423 if (r->headers_out.last_modified_time >= 0) {
1424 p = ngx_pnalloc(r->pool,
1425 sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1);
1426 if (p == NULL) {
1427 return NGX_ERROR;
1430 v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p;
1431 v->valid = 1;
1432 v->no_cacheable = 0;
1433 v->not_found = 0;
1434 v->data = p;
1436 return NGX_OK;
1439 v->not_found = 1;
1441 return NGX_OK;
1445 static ngx_int_t
1446 ngx_http_variable_sent_connection(ngx_http_request_t *r,
1447 ngx_http_variable_value_t *v, uintptr_t data)
1449 size_t len;
1450 char *p;
1452 if (r->keepalive) {
1453 len = sizeof("keep-alive") - 1;
1454 p = "keep-alive";
1456 } else {
1457 len = sizeof("close") - 1;
1458 p = "close";
1461 v->len = len;
1462 v->valid = 1;
1463 v->no_cacheable = 0;
1464 v->not_found = 0;
1465 v->data = (u_char *) p;
1467 return NGX_OK;
1471 static ngx_int_t
1472 ngx_http_variable_sent_keep_alive(ngx_http_request_t *r,
1473 ngx_http_variable_value_t *v, uintptr_t data)
1475 u_char *p;
1476 ngx_http_core_loc_conf_t *clcf;
1478 if (r->keepalive) {
1479 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1481 if (clcf->keepalive_header) {
1483 p = ngx_pnalloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN);
1484 if (p == NULL) {
1485 return NGX_ERROR;
1488 v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p;
1489 v->valid = 1;
1490 v->no_cacheable = 0;
1491 v->not_found = 0;
1492 v->data = p;
1494 return NGX_OK;
1498 v->not_found = 1;
1500 return NGX_OK;
1504 static ngx_int_t
1505 ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r,
1506 ngx_http_variable_value_t *v, uintptr_t data)
1508 if (r->chunked) {
1509 v->len = sizeof("chunked") - 1;
1510 v->valid = 1;
1511 v->no_cacheable = 0;
1512 v->not_found = 0;
1513 v->data = (u_char *) "chunked";
1515 } else {
1516 v->not_found = 1;
1519 return NGX_OK;
1523 static ngx_int_t
1524 ngx_http_variable_request_completion(ngx_http_request_t *r,
1525 ngx_http_variable_value_t *v, uintptr_t data)
1527 if (r->request_complete) {
1528 v->len = 2;
1529 v->valid = 1;
1530 v->no_cacheable = 0;
1531 v->not_found = 0;
1532 v->data = (u_char *) "OK";
1534 return NGX_OK;
1537 v->len = 0;
1538 v->valid = 1;
1539 v->no_cacheable = 0;
1540 v->not_found = 0;
1541 v->data = (u_char *) "";
1543 return NGX_OK;
1547 static ngx_int_t
1548 ngx_http_variable_request_body(ngx_http_request_t *r,
1549 ngx_http_variable_value_t *v, uintptr_t data)
1551 u_char *p;
1552 size_t len;
1553 ngx_buf_t *buf, *next;
1554 ngx_chain_t *cl;
1556 if (r->request_body == NULL
1557 || r->request_body->bufs == NULL
1558 || r->request_body->temp_file)
1560 v->not_found = 1;
1562 return NGX_OK;
1565 cl = r->request_body->bufs;
1566 buf = cl->buf;
1568 if (cl->next == NULL) {
1569 v->len = buf->last - buf->pos;
1570 v->valid = 1;
1571 v->no_cacheable = 0;
1572 v->not_found = 0;
1573 v->data = buf->pos;
1575 return NGX_OK;
1578 next = cl->next->buf;
1579 len = (buf->last - buf->pos) + (next->last - next->pos);
1581 p = ngx_pnalloc(r->pool, len);
1582 if (p == NULL) {
1583 return NGX_ERROR;
1586 v->data = p;
1588 p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
1589 ngx_memcpy(p, next->pos, next->last - next->pos);
1591 v->len = len;
1592 v->valid = 1;
1593 v->no_cacheable = 0;
1594 v->not_found = 0;
1596 return NGX_OK;
1600 static ngx_int_t
1601 ngx_http_variable_request_body_file(ngx_http_request_t *r,
1602 ngx_http_variable_value_t *v, uintptr_t data)
1604 if (r->request_body == NULL || r->request_body->temp_file == NULL) {
1605 v->not_found = 1;
1607 return NGX_OK;
1610 v->len = r->request_body->temp_file->file.name.len;
1611 v->valid = 1;
1612 v->no_cacheable = 0;
1613 v->not_found = 0;
1614 v->data = r->request_body->temp_file->file.name.data;
1616 return NGX_OK;
1620 static ngx_int_t
1621 ngx_http_variable_nginx_version(ngx_http_request_t *r,
1622 ngx_http_variable_value_t *v, uintptr_t data)
1624 v->len = sizeof(NGINX_VERSION) - 1;
1625 v->valid = 1;
1626 v->no_cacheable = 0;
1627 v->not_found = 0;
1628 v->data = (u_char *) NGINX_VERSION;
1630 return NGX_OK;
1634 static ngx_int_t
1635 ngx_http_variable_hostname(ngx_http_request_t *r,
1636 ngx_http_variable_value_t *v, uintptr_t data)
1638 v->len = ngx_cycle->hostname.len;
1639 v->valid = 1;
1640 v->no_cacheable = 0;
1641 v->not_found = 0;
1642 v->data = ngx_cycle->hostname.data;
1644 return NGX_OK;
1648 static ngx_int_t
1649 ngx_http_variable_pid(ngx_http_request_t *r,
1650 ngx_http_variable_value_t *v, uintptr_t data)
1652 u_char *p;
1654 p = ngx_pnalloc(r->pool, NGX_INT64_LEN);
1655 if (p == NULL) {
1656 return NGX_ERROR;
1659 v->len = ngx_sprintf(p, "%P", ngx_pid) - p;
1660 v->valid = 1;
1661 v->no_cacheable = 0;
1662 v->not_found = 0;
1663 v->data = p;
1665 return NGX_OK;
1669 static ngx_int_t
1670 ngx_http_variable_not_found(ngx_http_request_t *r, ngx_http_variable_value_t *v,
1671 uintptr_t data)
1673 v->not_found = 1;
1674 return NGX_OK;
1678 ngx_http_regex_t *
1679 ngx_http_regex_compile(ngx_conf_t *cf, ngx_regex_compile_t *rc)
1681 u_char *p;
1682 size_t size;
1683 ngx_str_t name;
1684 ngx_uint_t i, n;
1685 ngx_http_variable_t *v;
1686 ngx_http_regex_t *re;
1687 ngx_http_regex_variable_t *rv;
1688 ngx_http_core_main_conf_t *cmcf;
1690 rc->pool = cf->pool;
1692 if (ngx_regex_compile(rc) != NGX_OK) {
1693 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc->err);
1694 return NULL;
1697 re = ngx_pcalloc(cf->pool, sizeof(ngx_http_regex_t));
1698 if (re == NULL) {
1699 return NULL;
1702 re->regex = rc->regex;
1703 re->ncaptures = rc->captures;
1705 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1706 cmcf->ncaptures = ngx_max(cmcf->ncaptures, re->ncaptures);
1708 n = (ngx_uint_t) rc->named_captures;
1710 if (n == 0) {
1711 return re;
1714 rv = ngx_palloc(rc->pool, n * sizeof(ngx_http_regex_variable_t));
1715 if (rv == NULL) {
1716 return NULL;
1719 re->variables = rv;
1720 re->nvariables = n;
1721 re->name = rc->pattern;
1723 size = rc->name_size;
1724 p = rc->names;
1726 for (i = 0; i < n; i++) {
1727 rv[i].capture = 2 * ((p[0] << 8) + p[1]);
1729 name.data = &p[2];
1730 name.len = ngx_strlen(name.data);
1732 v = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
1733 if (v == NULL) {
1734 return NULL;
1737 rv[i].index = ngx_http_get_variable_index(cf, &name);
1738 if (rv[i].index == NGX_ERROR) {
1739 return NULL;
1742 v->get_handler = ngx_http_variable_not_found;
1744 p += i + size;
1747 return re;
1751 ngx_int_t
1752 ngx_http_regex_exec(ngx_http_request_t *r, ngx_http_regex_t *re, ngx_str_t *s)
1754 ngx_int_t rc, index;
1755 ngx_uint_t i, n, len;
1756 ngx_http_variable_value_t *vv;
1757 ngx_http_core_main_conf_t *cmcf;
1759 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
1761 if (re->ncaptures) {
1762 len = cmcf->ncaptures;
1764 if (r->captures == NULL) {
1765 r->captures = ngx_palloc(r->pool, len * sizeof(int));
1766 if (r->captures == NULL) {
1767 return NGX_ERROR;
1771 } else {
1772 len = 0;
1775 rc = ngx_regex_exec(re->regex, s, r->captures, len);
1777 if (rc == NGX_REGEX_NO_MATCHED) {
1778 return NGX_DECLINED;
1781 if (rc < 0) {
1782 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1783 ngx_regex_exec_n " failed: %i on \"%V\" using \"%V\"",
1784 rc, s, &re->name);
1785 return NGX_ERROR;
1788 for (i = 0; i < re->nvariables; i++) {
1790 n = re->variables[i].capture;
1791 index = re->variables[i].index;
1792 vv = &r->variables[index];
1794 vv->len = r->captures[n + 1] - r->captures[n];
1795 vv->valid = 1;
1796 vv->no_cacheable = 0;
1797 vv->not_found = 0;
1798 vv->data = &s->data[r->captures[n]];
1800 #if (NGX_DEBUG)
1802 ngx_http_variable_t *v;
1804 v = cmcf->variables.elts;
1806 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1807 "http regex set $%V to \"%*s\"",
1808 &v[index].name, vv->len, vv->data);
1810 #endif
1813 r->ncaptures = rc * 2;
1814 r->captures_data = s->data;
1816 return NGX_OK;
1820 ngx_int_t
1821 ngx_http_variables_add_core_vars(ngx_conf_t *cf)
1823 ngx_int_t rc;
1824 ngx_http_variable_t *v;
1825 ngx_http_core_main_conf_t *cmcf;
1827 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1829 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool,
1830 sizeof(ngx_hash_keys_arrays_t));
1831 if (cmcf->variables_keys == NULL) {
1832 return NGX_ERROR;
1835 cmcf->variables_keys->pool = cf->pool;
1836 cmcf->variables_keys->temp_pool = cf->pool;
1838 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL)
1839 != NGX_OK)
1841 return NGX_ERROR;
1844 for (v = ngx_http_core_variables; v->name.len; v++) {
1845 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v,
1846 NGX_HASH_READONLY_KEY);
1848 if (rc == NGX_OK) {
1849 continue;
1852 if (rc == NGX_BUSY) {
1853 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1854 "conflicting variable name \"%V\"", &v->name);
1857 return NGX_ERROR;
1860 return NGX_OK;
1864 ngx_int_t
1865 ngx_http_variables_init_vars(ngx_conf_t *cf)
1867 ngx_uint_t i, n;
1868 ngx_hash_key_t *key;
1869 ngx_hash_init_t hash;
1870 ngx_http_variable_t *v, *av;
1871 ngx_http_core_main_conf_t *cmcf;
1873 /* set the handlers for the indexed http variables */
1875 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1877 v = cmcf->variables.elts;
1878 key = cmcf->variables_keys->keys.elts;
1880 for (i = 0; i < cmcf->variables.nelts; i++) {
1882 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
1884 av = key[n].value;
1886 if (av->get_handler
1887 && v[i].name.len == key[n].key.len
1888 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len)
1889 == 0)
1891 v[i].get_handler = av->get_handler;
1892 v[i].data = av->data;
1894 av->flags |= NGX_HTTP_VAR_INDEXED;
1895 v[i].flags = av->flags;
1897 av->index = i;
1899 goto next;
1903 if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) {
1904 v[i].get_handler = ngx_http_variable_unknown_header_in;
1905 v[i].data = (uintptr_t) &v[i].name;
1907 continue;
1910 if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) {
1911 v[i].get_handler = ngx_http_variable_unknown_header_out;
1912 v[i].data = (uintptr_t) &v[i].name;
1914 continue;
1917 if (ngx_strncmp(v[i].name.data, "upstream_http_", 14) == 0) {
1918 v[i].get_handler = ngx_http_upstream_header_variable;
1919 v[i].data = (uintptr_t) &v[i].name;
1920 v[i].flags = NGX_HTTP_VAR_NOCACHEABLE;
1922 continue;
1925 if (ngx_strncmp(v[i].name.data, "cookie_", 7) == 0) {
1926 v[i].get_handler = ngx_http_variable_cookie;
1927 v[i].data = (uintptr_t) &v[i].name;
1929 continue;
1932 if (ngx_strncmp(v[i].name.data, "arg_", 4) == 0) {
1933 v[i].get_handler = ngx_http_variable_argument;
1934 v[i].data = (uintptr_t) &v[i].name;
1936 continue;
1939 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
1940 "unknown \"%V\" variable", &v[i].name);
1942 return NGX_ERROR;
1944 next:
1945 continue;
1949 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) {
1950 av = key[n].value;
1952 if (av->flags & NGX_HTTP_VAR_NOHASH) {
1953 key[n].key.data = NULL;
1958 hash.hash = &cmcf->variables_hash;
1959 hash.key = ngx_hash_key;
1960 hash.max_size = cmcf->variables_hash_max_size;
1961 hash.bucket_size = cmcf->variables_hash_bucket_size;
1962 hash.name = "variables_hash";
1963 hash.pool = cf->pool;
1964 hash.temp_pool = NULL;
1966 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts,
1967 cmcf->variables_keys->keys.nelts)
1968 != NGX_OK)
1970 return NGX_ERROR;
1973 cmcf->variables_keys = NULL;
1975 return NGX_OK;
1979 void
1980 ngx_http_variable_value_rbtree_insert(ngx_rbtree_node_t *temp,
1981 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1983 ngx_rbtree_node_t **p;
1984 ngx_http_variable_value_node_t *vvn, *vvt;
1986 for ( ;; ) {
1988 vvn = (ngx_http_variable_value_node_t *) node;
1989 vvt = (ngx_http_variable_value_node_t *) temp;
1991 if (node->key != temp->key) {
1993 p = (node->key < temp->key) ? &temp->left : &temp->right;
1995 } else if (vvn->len != vvt->len) {
1997 p = (vvn->len < vvt->len) ? &temp->left : &temp->right;
1999 } else {
2000 p = (ngx_memcmp(vvn->value->data, vvt->value->data, vvn->len) < 0)
2001 ? &temp->left : &temp->right;
2004 if (*p == sentinel) {
2005 break;
2008 temp = *p;
2011 *p = node;
2012 node->parent = temp;
2013 node->left = sentinel;
2014 node->right = sentinel;
2015 ngx_rbt_red(node);
2019 ngx_http_variable_value_t *
2020 ngx_http_variable_value_lookup(ngx_rbtree_t *rbtree, ngx_str_t *val,
2021 uint32_t hash)
2023 ngx_int_t rc;
2024 ngx_rbtree_node_t *node, *sentinel;
2025 ngx_http_variable_value_node_t *vvn;
2027 node = rbtree->root;
2028 sentinel = rbtree->sentinel;
2030 while (node != sentinel) {
2032 vvn = (ngx_http_variable_value_node_t *) node;
2034 if (hash != node->key) {
2035 node = (hash < node->key) ? node->left : node->right;
2036 continue;
2039 if (val->len != vvn->len) {
2040 node = (val->len < vvn->len) ? node->left : node->right;
2041 continue;
2044 rc = ngx_memcmp(val->data, vvn->value->data, val->len);
2046 if (rc < 0) {
2047 node = node->left;
2048 continue;
2051 if (rc > 0) {
2052 node = node->right;
2053 continue;
2056 return vvn->value;
2059 return NULL;