1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
24 #include "filter_http_client.hpp"
29 #include <boost/thread/mutex.hpp>
36 #include <sys/types.h>
43 namespace mp = metaproxy_1;
44 namespace yf = mp::filter;
46 namespace metaproxy_1 {
48 class HTTPClient::Rep {
49 friend class HTTPClient;
50 void proxy(mp::Package &package);
51 std::string proxy_host;
52 std::string default_host;
61 yf::HTTPClient::Rep::Rep()
64 x_forwarded_for = false;
68 yf::HTTPClient::HTTPClient() : m_p(new Rep)
72 yf::HTTPClient::~HTTPClient()
76 void yf::HTTPClient::Rep::proxy(mp::Package &package)
78 Z_GDU *req_gdu = package.request().get();
79 if (req_gdu && req_gdu->which == Z_GDU_HTTP_Request)
81 Z_HTTP_Request *hreq = req_gdu->u.HTTP_Request;
84 yaz_url_t yaz_url = yaz_url_create();
85 const char *http_proxy =
86 z_HTTP_header_remove(&hreq->headers, "X-Metaproxy-Proxy");
89 http_proxy = proxy_host.c_str();
92 yaz_url_set_proxy(yaz_url, http_proxy);
94 yaz_url_set_max_redirects(yaz_url, max_redirects);
98 std::string peer_name2 = package.origin().get_address();
99 const char *peer_name1 =
100 z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
104 pcomb.append(peer_name1);
107 pcomb.append(peer_name2);
108 z_HTTP_header_set(o, &hreq->headers, "X-Forwarded-For",
112 if (hreq->path[0] == '/')
114 if (default_host.length())
115 uri = default_host + hreq->path;
122 std::string host = package.origin().get_bind_address();
128 res_gdu = o.create_HTTP_Response_details(
131 "http_client: no target URI specified");
135 Z_HTTP_Response * http_response =
136 yaz_url_exec(yaz_url, uri.c_str(), hreq->method,
137 hreq->headers, hreq->content_buf,
141 res_gdu = o.create_HTTP_Response(package.session(), hreq, 200);
142 z_HTTP_header_remove(&http_response->headers, "Transfer-Encoding");
143 res_gdu->u.HTTP_Response = http_response;
147 res_gdu = o.create_HTTP_Response_details(
149 hreq, 502, yaz_url_get_error(yaz_url));
152 package.response() = res_gdu;
153 yaz_url_destroy(yaz_url);
159 void yf::HTTPClient::process(mp::Package &package) const
161 Z_GDU *gdu = package.request().get();
162 if (gdu && gdu->which == Z_GDU_HTTP_Request)
168 void mp::filter::HTTPClient::configure(const xmlNode * ptr, bool test_only,
171 for (ptr = ptr->children; ptr; ptr = ptr->next)
173 if (ptr->type != XML_ELEMENT_NODE)
175 else if (!strcmp((const char *) ptr->name, "proxy"))
177 m_p->proxy_host = mp::xml::get_text(ptr);
179 else if (!strcmp((const char *) ptr->name, "max-redirects"))
181 m_p->max_redirects = mp::xml::get_int(ptr, 0);
183 else if (!strcmp((const char *) ptr->name, "default-host"))
185 m_p->default_host = mp::xml::get_text(ptr);
186 if (m_p->default_host.find("://") == std::string::npos)
188 throw mp::filter::FilterException
189 ("default-host is missing method (such as http://)"
190 " in http_client filter");
193 else if (!strcmp((const char *) ptr->name, "x-forwarded-for"))
195 m_p->x_forwarded_for = mp::xml::get_bool(ptr, 0);
197 else if (!strcmp((const char *) ptr->name, "bind_host"))
199 m_p->bind_host = mp::xml::get_bool(ptr, 0);
203 throw mp::filter::FilterException
205 + std::string((const char *) ptr->name)
206 + " in http_client filter");
211 static mp::filter::Base* filter_creator()
213 return new mp::filter::HTTPClient;
217 struct metaproxy_1_filter_struct metaproxy_1_filter_http_client = {
228 * c-file-style: "Stroustrup"
229 * indent-tabs-mode: nil
231 * vim: shiftwidth=4 tabstop=8 expandtab