Mercurial > hg > nginx-quic
comparison src/http/modules/ngx_http_proxy_module.c @ 5084:f7fe817c92a2
Correctly handle multiple X-Forwarded-For headers (ticket #106).
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Wed, 27 Feb 2013 13:29:50 +0000 |
parents | 7fa7e60a7f66 |
children | 886800caf360 8c866e31bc39 |
comparison
equal
deleted
inserted
replaced
5083:a805dc9c85cd | 5084:f7fe817c92a2 |
---|---|
2012 | 2012 |
2013 static ngx_int_t | 2013 static ngx_int_t |
2014 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r, | 2014 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r, |
2015 ngx_http_variable_value_t *v, uintptr_t data) | 2015 ngx_http_variable_value_t *v, uintptr_t data) |
2016 { | 2016 { |
2017 u_char *p; | 2017 size_t len; |
2018 u_char *p; | |
2019 ngx_uint_t i, n; | |
2020 ngx_table_elt_t **h; | |
2018 | 2021 |
2019 v->valid = 1; | 2022 v->valid = 1; |
2020 v->no_cacheable = 0; | 2023 v->no_cacheable = 0; |
2021 v->not_found = 0; | 2024 v->not_found = 0; |
2022 | 2025 |
2023 if (r->headers_in.x_forwarded_for == NULL) { | 2026 n = r->headers_in.x_forwarded_for.nelts; |
2027 h = r->headers_in.x_forwarded_for.elts; | |
2028 | |
2029 len = 0; | |
2030 | |
2031 for (i = 0; i < n; i++) { | |
2032 len += h[i]->value.len + sizeof(", ") - 1; | |
2033 } | |
2034 | |
2035 if (len == 0) { | |
2024 v->len = r->connection->addr_text.len; | 2036 v->len = r->connection->addr_text.len; |
2025 v->data = r->connection->addr_text.data; | 2037 v->data = r->connection->addr_text.data; |
2026 return NGX_OK; | 2038 return NGX_OK; |
2027 } | 2039 } |
2028 | 2040 |
2029 v->len = r->headers_in.x_forwarded_for->value.len | 2041 len += r->connection->addr_text.len; |
2030 + sizeof(", ") - 1 + r->connection->addr_text.len; | 2042 |
2031 | 2043 p = ngx_pnalloc(r->pool, len); |
2032 p = ngx_pnalloc(r->pool, v->len); | |
2033 if (p == NULL) { | 2044 if (p == NULL) { |
2034 return NGX_ERROR; | 2045 return NGX_ERROR; |
2035 } | 2046 } |
2036 | 2047 |
2048 v->len = len; | |
2037 v->data = p; | 2049 v->data = p; |
2038 | 2050 |
2039 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data, | 2051 for (i = 0; i < n; i++) { |
2040 r->headers_in.x_forwarded_for->value.len); | 2052 p = ngx_copy(p, h[i]->value.data, h[i]->value.len); |
2041 | 2053 *p++ = ','; *p++ = ' '; |
2042 *p++ = ','; *p++ = ' '; | 2054 } |
2043 | 2055 |
2044 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len); | 2056 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len); |
2045 | 2057 |
2046 return NGX_OK; | 2058 return NGX_OK; |
2047 } | 2059 } |