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