Mercurial > hg > nginx-quic
annotate src/http/ngx_http_variables.c @ 828:597c83dad983
fix format
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 30 Oct 2006 20:03:49 +0000 |
parents | 97e89a42711f |
children | 7521e5f5d555 |
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 { |
573 | 670 if (r->headers_in.host) { |
671 v->len = r->headers_in.host_name_len; | |
672 v->data = r->headers_in.host->value.data; | |
509 | 673 |
573 | 674 } else { |
675 v->len = r->server_name.len; | |
676 v->data = r->server_name.data; | |
509 | 677 } |
678 | |
573 | 679 v->valid = 1; |
680 v->no_cachable = 0; | |
681 v->not_found = 0; | |
509 | 682 |
573 | 683 return NGX_OK; |
499 | 684 } |
685 | |
686 | |
573 | 687 static ngx_int_t |
688 ngx_http_variable_remote_addr(ngx_http_request_t *r, | |
689 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 690 { |
573 | 691 v->len = r->connection->addr_text.len; |
692 v->valid = 1; | |
693 v->no_cachable = 0; | |
694 v->not_found = 0; | |
695 v->data = r->connection->addr_text.data; | |
499 | 696 |
573 | 697 return NGX_OK; |
499 | 698 } |
699 | |
700 | |
573 | 701 static ngx_int_t |
702 ngx_http_variable_remote_port(ngx_http_request_t *r, | |
703 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 704 { |
573 | 705 ngx_uint_t port; |
706 struct sockaddr_in *sin; | |
509 | 707 |
573 | 708 v->len = 0; |
709 v->valid = 1; | |
710 v->no_cachable = 0; | |
711 v->not_found = 0; | |
509 | 712 |
573 | 713 v->data = ngx_palloc(r->pool, sizeof("65535") - 1); |
714 if (v->data == NULL) { | |
715 return NGX_ERROR; | |
509 | 716 } |
717 | |
718 /* AF_INET only */ | |
577 | 719 |
509 | 720 if (r->connection->sockaddr->sa_family == AF_INET) { |
721 sin = (struct sockaddr_in *) r->connection->sockaddr; | |
577 | 722 |
509 | 723 port = ntohs(sin->sin_port); |
577 | 724 |
509 | 725 if (port > 0 && port < 65536) { |
573 | 726 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; |
509 | 727 } |
728 } | |
729 | |
573 | 730 return NGX_OK; |
509 | 731 } |
732 | |
733 | |
573 | 734 static ngx_int_t |
735 ngx_http_variable_server_addr(ngx_http_request_t *r, | |
736 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 737 { |
573 | 738 socklen_t len; |
739 ngx_connection_t *c; | |
740 struct sockaddr_in sin; | |
499 | 741 |
573 | 742 v->data = ngx_palloc(r->pool, INET_ADDRSTRLEN); |
743 if (v->data == NULL) { | |
744 return NGX_ERROR; | |
509 | 745 } |
746 | |
747 c = r->connection; | |
748 | |
749 if (r->in_addr == 0) { | |
750 len = sizeof(struct sockaddr_in); | |
751 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
|
752 ngx_connection_error(c, ngx_socket_errno, "getsockname() failed"); |
573 | 753 return NGX_ERROR; |
509 | 754 } |
755 | |
756 r->in_addr = sin.sin_addr.s_addr; | |
757 } | |
758 | |
573 | 759 v->len = ngx_inet_ntop(c->listening->family, &r->in_addr, |
760 v->data, INET_ADDRSTRLEN); | |
761 v->valid = 1; | |
762 v->no_cachable = 0; | |
763 v->not_found = 0; | |
499 | 764 |
573 | 765 return NGX_OK; |
499 | 766 } |
767 | |
768 | |
573 | 769 static ngx_int_t |
770 ngx_http_variable_server_port(ngx_http_request_t *r, | |
771 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 772 { |
573 | 773 v->len = r->port_text->len - 1; |
774 v->valid = 1; | |
775 v->no_cachable = 0; | |
776 v->not_found = 0; | |
777 v->data = r->port_text->data + 1; | |
509 | 778 |
573 | 779 return NGX_OK; |
509 | 780 } |
781 | |
782 | |
573 | 783 static ngx_int_t |
731 | 784 ngx_http_variable_scheme(ngx_http_request_t *r, |
785 ngx_http_variable_value_t *v, uintptr_t data) | |
786 { | |
787 #if (NGX_HTTP_SSL) | |
788 | |
789 if (r->connection->ssl) { | |
790 v->len = sizeof("https") - 1; | |
791 v->valid = 1; | |
792 v->no_cachable = 0; | |
793 v->not_found = 0; | |
732 | 794 v->data = (u_char *) "https"; |
731 | 795 |
796 return NGX_OK; | |
797 } | |
798 | |
799 #endif | |
800 | |
801 v->len = sizeof("http") - 1; | |
802 v->valid = 1; | |
803 v->no_cachable = 0; | |
804 v->not_found = 0; | |
732 | 805 v->data = (u_char *) "http"; |
731 | 806 |
807 return NGX_OK; | |
808 } | |
809 | |
810 | |
811 static ngx_int_t | |
573 | 812 ngx_http_variable_document_root(ngx_http_request_t *r, |
813 ngx_http_variable_value_t *v, uintptr_t data) | |
499 | 814 { |
671 | 815 ngx_str_t path; |
573 | 816 ngx_http_core_loc_conf_t *clcf; |
509 | 817 |
818 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
819 | |
671 | 820 if (clcf->root_lengths == NULL) { |
821 v->len = clcf->root.len; | |
822 v->valid = 1; | |
823 v->no_cachable = 0; | |
824 v->not_found = 0; | |
825 v->data = clcf->root.data; | |
826 | |
827 } else { | |
828 if (ngx_http_script_run(r, &path, clcf->root_lengths->elts, 0, | |
829 clcf->root_values->elts) | |
830 == NULL) | |
831 { | |
832 return NGX_ERROR; | |
833 } | |
834 | |
835 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, &path) == NGX_ERROR) { | |
836 return NGX_ERROR; | |
837 } | |
838 | |
839 v->len = path.len; | |
840 v->valid = 1; | |
841 v->no_cachable = 0; | |
842 v->not_found = 0; | |
843 v->data = path.data; | |
844 } | |
509 | 845 |
573 | 846 return NGX_OK; |
509 | 847 } |
848 | |
849 | |
573 | 850 static ngx_int_t |
851 ngx_http_variable_request_filename(ngx_http_request_t *r, | |
852 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 853 { |
773 | 854 size_t root; |
573 | 855 ngx_str_t path; |
499 | 856 |
773 | 857 if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) { |
573 | 858 return NGX_ERROR; |
557 | 859 } |
509 | 860 |
557 | 861 /* ngx_http_map_uri_to_path() allocates memory for terminating '\0' */ |
509 | 862 |
573 | 863 v->len = path.len - 1; |
864 v->valid = 1; | |
865 v->no_cachable = 0; | |
866 v->not_found = 0; | |
867 v->data = path.data; | |
499 | 868 |
573 | 869 return NGX_OK; |
499 | 870 } |
871 | |
872 | |
573 | 873 static ngx_int_t |
874 ngx_http_variable_request_method(ngx_http_request_t *r, | |
875 ngx_http_variable_value_t *v, uintptr_t data) | |
561 | 876 { |
647 | 877 if (r->main->method_name.data) { |
878 v->len = r->main->method_name.len; | |
573 | 879 v->valid = 1; |
880 v->no_cachable = 0; | |
881 v->not_found = 0; | |
647 | 882 v->data = r->main->method_name.data; |
573 | 883 |
884 } else { | |
885 v->not_found = 1; | |
561 | 886 } |
887 | |
573 | 888 return NGX_OK; |
561 | 889 } |
890 | |
891 | |
573 | 892 static ngx_int_t |
893 ngx_http_variable_remote_user(ngx_http_request_t *r, | |
894 ngx_http_variable_value_t *v, uintptr_t data) | |
539 | 895 { |
573 | 896 ngx_int_t rc; |
539 | 897 |
898 rc = ngx_http_auth_basic_user(r); | |
899 | |
900 if (rc == NGX_DECLINED) { | |
573 | 901 v->not_found = 1; |
902 return NGX_OK; | |
539 | 903 } |
904 | |
905 if (rc == NGX_ERROR) { | |
573 | 906 return NGX_ERROR; |
539 | 907 } |
908 | |
573 | 909 v->len = r->headers_in.user.len; |
910 v->valid = 1; | |
911 v->no_cachable = 0; | |
912 v->not_found = 0; | |
913 v->data = r->headers_in.user.data; | |
571 | 914 |
573 | 915 return NGX_OK; |
571 | 916 } |
917 | |
918 | |
611 | 919 static ngx_int_t |
920 ngx_http_variable_body_bytes_sent(ngx_http_request_t *r, | |
921 ngx_http_variable_value_t *v, uintptr_t data) | |
922 { | |
923 off_t sent; | |
924 u_char *p; | |
925 | |
926 sent = r->connection->sent - r->header_size; | |
927 | |
928 if (sent < 0) { | |
929 sent = 0; | |
930 } | |
931 | |
932 p = ngx_palloc(r->pool, NGX_OFF_T_LEN); | |
933 if (p == NULL) { | |
934 return NGX_ERROR; | |
935 } | |
936 | |
937 v->len = ngx_sprintf(p, "%O", sent) - p; | |
938 v->valid = 1; | |
939 v->no_cachable = 0; | |
940 v->not_found = 0; | |
941 v->data = p; | |
942 | |
943 return NGX_OK; | |
944 } | |
945 | |
946 | |
629 | 947 static ngx_int_t |
641 | 948 ngx_http_variable_sent_content_type(ngx_http_request_t *r, |
949 ngx_http_variable_value_t *v, uintptr_t data) | |
950 { | |
951 if (r->headers_out.content_type.len) { | |
952 v->len = r->headers_out.content_type.len; | |
953 v->valid = 1; | |
954 v->no_cachable = 0; | |
955 v->not_found = 0; | |
956 v->data = r->headers_out.content_type.data; | |
957 | |
958 } else { | |
959 v->not_found = 1; | |
960 } | |
961 | |
962 return NGX_OK; | |
963 } | |
964 | |
965 | |
966 static ngx_int_t | |
967 ngx_http_variable_sent_content_length(ngx_http_request_t *r, | |
968 ngx_http_variable_value_t *v, uintptr_t data) | |
969 { | |
970 u_char *p; | |
971 | |
972 if (r->headers_out.content_length) { | |
973 v->len = r->headers_out.content_length->value.len; | |
974 v->valid = 1; | |
975 v->no_cachable = 0; | |
976 v->not_found = 0; | |
977 v->data = r->headers_out.content_length->value.data; | |
978 | |
979 return NGX_OK; | |
980 } | |
981 | |
982 if (r->headers_out.content_length_n >= 0) { | |
983 p = ngx_palloc(r->pool, NGX_OFF_T_LEN); | |
984 if (p == NULL) { | |
985 return NGX_ERROR; | |
986 } | |
987 | |
988 v->len = ngx_sprintf(p, "%O", r->headers_out.content_length_n) - p; | |
989 v->valid = 1; | |
990 v->no_cachable = 0; | |
991 v->not_found = 0; | |
992 v->data = p; | |
993 | |
994 return NGX_OK; | |
995 } | |
996 | |
997 v->not_found = 1; | |
998 | |
999 return NGX_OK; | |
1000 } | |
1001 | |
1002 | |
1003 static ngx_int_t | |
1004 ngx_http_variable_sent_last_modified(ngx_http_request_t *r, | |
1005 ngx_http_variable_value_t *v, uintptr_t data) | |
1006 { | |
1007 u_char *p; | |
1008 | |
1009 if (r->headers_out.last_modified) { | |
1010 v->len = r->headers_out.last_modified->value.len; | |
1011 v->valid = 1; | |
1012 v->no_cachable = 0; | |
1013 v->not_found = 0; | |
1014 v->data = r->headers_out.last_modified->value.data; | |
1015 | |
1016 return NGX_OK; | |
1017 } | |
1018 | |
1019 if (r->headers_out.last_modified_time >= 0) { | |
1020 p = ngx_palloc(r->pool, | |
1021 sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT") - 1); | |
1022 if (p == NULL) { | |
1023 return NGX_ERROR; | |
1024 } | |
1025 | |
1026 v->len = ngx_http_time(p, r->headers_out.last_modified_time) - p; | |
1027 v->valid = 1; | |
1028 v->no_cachable = 0; | |
1029 v->not_found = 0; | |
1030 v->data = p; | |
1031 | |
1032 return NGX_OK; | |
1033 } | |
1034 | |
1035 v->not_found = 1; | |
1036 | |
1037 return NGX_OK; | |
1038 } | |
1039 | |
1040 | |
1041 static ngx_int_t | |
1042 ngx_http_variable_sent_connection(ngx_http_request_t *r, | |
1043 ngx_http_variable_value_t *v, uintptr_t data) | |
1044 { | |
1045 size_t len; | |
1046 char *p; | |
1047 | |
1048 if (r->keepalive) { | |
1049 len = sizeof("keep-alive") - 1; | |
1050 p = "keep-alive"; | |
1051 | |
1052 } else { | |
1053 len = sizeof("close") - 1; | |
1054 p = "close"; | |
1055 } | |
1056 | |
1057 v->len = len; | |
1058 v->valid = 1; | |
1059 v->no_cachable = 0; | |
1060 v->not_found = 0; | |
1061 v->data = (u_char *) p; | |
1062 | |
1063 return NGX_OK; | |
1064 } | |
1065 | |
1066 | |
1067 static ngx_int_t | |
1068 ngx_http_variable_sent_keep_alive(ngx_http_request_t *r, | |
1069 ngx_http_variable_value_t *v, uintptr_t data) | |
1070 { | |
1071 u_char *p; | |
1072 ngx_http_core_loc_conf_t *clcf; | |
1073 | |
1074 if (r->keepalive) { | |
1075 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); | |
1076 | |
1077 if (clcf->keepalive_header) { | |
1078 | |
1079 p = ngx_palloc(r->pool, sizeof("timeout=") - 1 + NGX_TIME_T_LEN); | |
1080 if (p == NULL) { | |
1081 return NGX_ERROR; | |
1082 } | |
1083 | |
1084 v->len = ngx_sprintf(p, "timeout=%T", clcf->keepalive_header) - p; | |
1085 v->valid = 1; | |
1086 v->no_cachable = 0; | |
1087 v->not_found = 0; | |
1088 v->data = p; | |
1089 | |
1090 return NGX_OK; | |
1091 } | |
1092 } | |
1093 | |
1094 v->not_found = 1; | |
1095 | |
1096 return NGX_OK; | |
1097 } | |
1098 | |
1099 | |
1100 static ngx_int_t | |
1101 ngx_http_variable_sent_transfer_encoding(ngx_http_request_t *r, | |
1102 ngx_http_variable_value_t *v, uintptr_t data) | |
1103 { | |
1104 if (r->chunked) { | |
1105 v->len = sizeof("chunked") - 1; | |
1106 v->valid = 1; | |
1107 v->no_cachable = 0; | |
1108 v->not_found = 0; | |
1109 v->data = (u_char *) "chunked"; | |
1110 | |
1111 } else { | |
1112 v->not_found = 1; | |
1113 } | |
1114 | |
1115 return NGX_OK; | |
1116 } | |
1117 | |
1118 | |
1119 static ngx_int_t | |
629 | 1120 ngx_http_variable_request_completion(ngx_http_request_t *r, |
1121 ngx_http_variable_value_t *v, uintptr_t data) | |
1122 { | |
1123 if (r->request_complete) { | |
1124 v->len = 2; | |
1125 v->valid = 1; | |
1126 v->no_cachable = 0; | |
1127 v->not_found = 0; | |
1128 v->data = (u_char *) "OK"; | |
1129 | |
1130 return NGX_OK; | |
1131 } | |
1132 | |
1133 v->len = 0; | |
1134 v->valid = 1; | |
1135 v->no_cachable = 0; | |
1136 v->not_found = 0; | |
1137 v->data = (u_char *) ""; | |
1138 | |
1139 return NGX_OK; | |
1140 } | |
1141 | |
1142 | |
759 | 1143 static ngx_int_t |
1144 ngx_http_variable_request_body_file(ngx_http_request_t *r, | |
1145 ngx_http_variable_value_t *v, uintptr_t data) | |
1146 { | |
1147 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
|
1148 v->not_found = 1; |
759 | 1149 |
1150 return NGX_OK; | |
1151 } | |
1152 | |
1153 v->len = r->request_body->temp_file->file.name.len; | |
1154 v->valid = 1; | |
1155 v->no_cachable = 0; | |
1156 v->not_found = 0; | |
1157 v->data = r->request_body->temp_file->file.name.data; | |
1158 | |
1159 return NGX_OK; | |
1160 } | |
1161 | |
1162 | |
499 | 1163 ngx_int_t |
509 | 1164 ngx_http_variables_add_core_vars(ngx_conf_t *cf) |
499 | 1165 { |
611 | 1166 ngx_int_t rc; |
1167 ngx_http_variable_t *v; | |
501 | 1168 ngx_http_core_main_conf_t *cmcf; |
499 | 1169 |
509 | 1170 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
1171 | |
611 | 1172 cmcf->variables_keys = ngx_pcalloc(cf->temp_pool, |
1173 sizeof(ngx_hash_keys_arrays_t)); | |
1174 if (cmcf->variables_keys == NULL) { | |
1175 return NGX_ERROR; | |
1176 } | |
1177 | |
1178 cmcf->variables_keys->pool = cf->pool; | |
1179 cmcf->variables_keys->temp_pool = cf->pool; | |
1180 | |
1181 if (ngx_hash_keys_array_init(cmcf->variables_keys, NGX_HASH_SMALL) | |
1182 != NGX_OK) | |
509 | 1183 { |
499 | 1184 return NGX_ERROR; |
1185 } | |
1186 | |
611 | 1187 for (v = ngx_http_core_variables; v->name.len; v++) { |
1188 rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, | |
1189 NGX_HASH_READONLY_KEY); | |
1190 | |
1191 if (rc == NGX_OK) { | |
1192 continue; | |
499 | 1193 } |
1194 | |
611 | 1195 if (rc == NGX_BUSY) { |
1196 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1197 "conflicting variable name \"%V\"", &v->name); | |
1198 } | |
1199 | |
1200 return NGX_ERROR; | |
499 | 1201 } |
1202 | |
1203 return NGX_OK; | |
1204 } | |
509 | 1205 |
1206 | |
1207 ngx_int_t | |
1208 ngx_http_variables_init_vars(ngx_conf_t *cf) | |
1209 { | |
1210 ngx_uint_t i, n; | |
611 | 1211 ngx_hash_key_t *key; |
1212 ngx_hash_init_t hash; | |
509 | 1213 ngx_http_variable_t *v, *av; |
1214 ngx_http_core_main_conf_t *cmcf; | |
1215 | |
1216 /* set the handlers for the indexed http variables */ | |
1217 | |
1218 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
1219 | |
1220 v = cmcf->variables.elts; | |
611 | 1221 key = cmcf->variables_keys->keys.elts; |
509 | 1222 |
1223 for (i = 0; i < cmcf->variables.nelts; i++) { | |
1224 | |
611 | 1225 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { |
509 | 1226 |
611 | 1227 if (v[i].name.len == key[n].key.len |
1228 && ngx_strncmp(v[i].name.data, key[n].key.data, v[i].name.len) | |
509 | 1229 == 0) |
1230 { | |
611 | 1231 av = key[n].value; |
1232 | |
637 | 1233 v[i].get_handler = av->get_handler; |
611 | 1234 v[i].data = av->data; |
527 | 1235 |
611 | 1236 av->flags |= NGX_HTTP_VAR_INDEXED; |
1237 v[i].flags = av->flags; | |
509 | 1238 |
611 | 1239 av->index = i; |
533 | 1240 |
509 | 1241 goto next; |
1242 } | |
1243 } | |
1244 | |
1245 if (ngx_strncmp(v[i].name.data, "http_", 5) == 0) { | |
637 | 1246 v[i].get_handler = ngx_http_variable_unknown_header_in; |
577 | 1247 v[i].data = (uintptr_t) &v[i].name; |
1248 | |
1249 continue; | |
1250 } | |
1251 | |
1252 if (ngx_strncmp(v[i].name.data, "sent_http_", 10) == 0) { | |
637 | 1253 v[i].get_handler = ngx_http_variable_unknown_header_out; |
509 | 1254 v[i].data = (uintptr_t) &v[i].name; |
1255 | |
1256 continue; | |
1257 } | |
1258 | |
1259 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1260 "unknown \"%V\" variable", &v[i].name); | |
1261 | |
1262 return NGX_ERROR; | |
1263 | |
1264 next: | |
1265 continue; | |
1266 } | |
1267 | |
1268 | |
611 | 1269 for (n = 0; n < cmcf->variables_keys->keys.nelts; n++) { |
1270 av = key[n].value; | |
1271 | |
1272 if (av->flags & NGX_HTTP_VAR_NOHASH) { | |
1273 key[n].key.data = NULL; | |
583 | 1274 } |
1275 } | |
1276 | |
1277 | |
611 | 1278 hash.hash = &cmcf->variables_hash; |
1279 hash.key = ngx_hash_key; | |
1280 hash.max_size = cmcf->variables_hash_max_size; | |
1281 hash.bucket_size = cmcf->variables_hash_bucket_size; | |
1282 hash.name = "variables_hash"; | |
1283 hash.pool = cf->pool; | |
1284 hash.temp_pool = NULL; | |
509 | 1285 |
611 | 1286 if (ngx_hash_init(&hash, cmcf->variables_keys->keys.elts, |
1287 cmcf->variables_keys->keys.nelts) | |
581 | 1288 != NGX_OK) |
509 | 1289 { |
1290 return NGX_ERROR; | |
1291 } | |
1292 | |
611 | 1293 cmcf->variables_keys = NULL; |
509 | 1294 |
1295 return NGX_OK; | |
1296 } |