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