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