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