From 99d83cbd75e1a9ff8e0275bd081afc514555fb4b Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 6 Mar 2014 10:36:06 +0100 Subject: [PATCH] http_client: x-forwarded-for controls X-Forwarded-For presence If x-forwarded-for setting (boolean) is enabled, http_client will generate an X-Forwarded-For header entry which includes existing X-Forwarded-For (if any) and current peer address (immediate client of Metaproxy). By default x-forwarded-for disabled and X-Forwarded-For will not be generated. --- etc/config4.xml | 1 + src/filter_http_client.cpp | 23 ++++++++++++++++++++++- xml/schema/filter_http_client.rnc | 3 ++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/etc/config4.xml b/etc/config4.xml index 733791e..021c1cc 100644 --- a/etc/config4.xml +++ b/etc/config4.xml @@ -29,6 +29,7 @@ http://localhost:9999 + true diff --git a/src/filter_http_client.cpp b/src/filter_http_client.cpp index e756ce5..9e9512a 100644 --- a/src/filter_http_client.cpp +++ b/src/filter_http_client.cpp @@ -51,6 +51,7 @@ namespace metaproxy_1 { std::string proxy_host; std::string default_host; int max_redirects; + bool x_forwarded_for; Rep(); }; } @@ -58,7 +59,8 @@ namespace metaproxy_1 { yf::HTTPClient::Rep::Rep() { - max_redirects = -0; + max_redirects = 0; + x_forwarded_for = false; } yf::HTTPClient::HTTPClient() : m_p(new Rep) @@ -89,6 +91,21 @@ void yf::HTTPClient::Rep::proxy(mp::Package &package) yaz_url_set_max_redirects(yaz_url, max_redirects); + if (x_forwarded_for) + { + std::string peer_name2 = package.origin().get_address(); + const char *peer_name1 = + z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For"); + std::string pcomb; + if (peer_name1) + { + pcomb.append(peer_name1); + pcomb.append(", "); + } + pcomb.append(peer_name2); + z_HTTP_header_set(o, &hreq->headers, "X-Forwarded-For", + pcomb.c_str()); + } std::string uri; if (hreq->path[0] == '/') { @@ -157,6 +174,10 @@ void mp::filter::HTTPClient::configure(const xmlNode * ptr, bool test_only, " in http_client filter"); } } + else if (!strcmp((const char *) ptr->name, "x-forwarded-for")) + { + m_p->x_forwarded_for = mp::xml::get_bool(ptr, 0); + } else { throw mp::filter::FilterException diff --git a/xml/schema/filter_http_client.rnc b/xml/schema/filter_http_client.rnc index 070992a..7eb9e46 100644 --- a/xml/schema/filter_http_client.rnc +++ b/xml/schema/filter_http_client.rnc @@ -8,5 +8,6 @@ filter_http_client = attribute name { xsd:NCName }?, element mp:default-host { xsd:string }?, element mp:max-redirects { xsd:integer }?, - element mp:proxy { xsd:string }? + element mp:proxy { xsd:string }?, + element mp:x-forwarded-for { xsd:boolean }? -- 1.7.10.4