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