Working outgoing IP for {http,z3950}_client MP-575
[metaproxy-moved-to-github.git] / src / filter_http_client.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include <yaz/url.h>
24 #include "filter_http_client.hpp"
25
26 #include <yaz/zgdu.h>
27 #include <yaz/log.h>
28
29 #include <boost/thread/mutex.hpp>
30
31 #include <list>
32 #include <map>
33 #include <iostream>
34
35 #if HAVE_SYS_TYPES_H
36 #include <sys/types.h>
37 #endif
38
39 #if HAVE_SYS_STAT_H
40 #include <sys/stat.h>
41 #endif
42
43 namespace mp = metaproxy_1;
44 namespace yf = mp::filter;
45
46 namespace metaproxy_1 {
47     namespace filter {
48         class HTTPClient::Rep {
49             friend class HTTPClient;
50             void proxy(mp::Package &package);
51             std::string proxy_host;
52             std::string default_host;
53             int max_redirects;
54             bool x_forwarded_for;
55             bool bind_host;
56             Rep();
57         };
58     }
59 }
60
61 yf::HTTPClient::Rep::Rep()
62 {
63     max_redirects = 0;
64     x_forwarded_for = false;
65     bind_host = false;
66 }
67
68 yf::HTTPClient::HTTPClient() : m_p(new Rep)
69 {
70 }
71
72 yf::HTTPClient::~HTTPClient()
73 {
74 }
75
76 void yf::HTTPClient::Rep::proxy(mp::Package &package)
77 {
78     Z_GDU *req_gdu = package.request().get();
79     if (req_gdu && req_gdu->which == Z_GDU_HTTP_Request)
80     {
81         Z_HTTP_Request *hreq = req_gdu->u.HTTP_Request;
82         Z_GDU *res_gdu = 0;
83         mp::odr o;
84         yaz_url_t yaz_url = yaz_url_create();
85         const char *http_proxy =
86             z_HTTP_header_remove(&hreq->headers, "X-Metaproxy-Proxy");
87
88         if (!http_proxy)
89             http_proxy = proxy_host.c_str();
90
91         if (*http_proxy)
92             yaz_url_set_proxy(yaz_url, http_proxy);
93
94         yaz_url_set_max_redirects(yaz_url, max_redirects);
95
96         if (x_forwarded_for)
97         {
98             std::string peer_name2 = package.origin().get_address();
99             const char *peer_name1 =
100                 z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
101             std::string pcomb;
102             if (peer_name1)
103             {
104                 pcomb.append(peer_name1);
105                 pcomb.append(", ");
106             }
107             pcomb.append(peer_name2);
108             z_HTTP_header_set(o, &hreq->headers, "X-Forwarded-For",
109                               pcomb.c_str());
110         }
111         std::string uri;
112         if (hreq->path[0] == '/')
113         {
114             if (default_host.length())
115                 uri = default_host + hreq->path;
116         }
117         else
118             uri = hreq->path;
119
120
121         if (bind_host)
122         {
123             std::string host = package.origin().get_bind_address();
124             uri.append(" ");
125             uri.append(host);
126         }
127         Z_HTTP_Response *http_response = 0;
128         if (uri.length())
129             http_response =
130             yaz_url_exec(yaz_url, uri.c_str(), hreq->method,
131                          hreq->headers, hreq->content_buf,
132                          hreq->content_len);
133         if (http_response)
134         {
135             res_gdu = o.create_HTTP_Response(package.session(), hreq, 200);
136             z_HTTP_header_remove(&http_response->headers, "Transfer-Encoding");
137             res_gdu->u.HTTP_Response = http_response;
138         }
139         else
140         {
141             res_gdu = o.create_HTTP_Response_details(
142                 package.session(),
143                 hreq, 502,
144                 yaz_url_get_error(yaz_url));
145         }
146         package.response() = res_gdu;
147         yaz_url_destroy(yaz_url);
148     }
149     else
150         package.move();
151 }
152
153 void yf::HTTPClient::process(mp::Package &package) const
154 {
155     Z_GDU *gdu = package.request().get();
156     if (gdu && gdu->which == Z_GDU_HTTP_Request)
157         m_p->proxy(package);
158     else
159         package.move();
160 }
161
162 void mp::filter::HTTPClient::configure(const xmlNode * ptr, bool test_only,
163                                        const char *path)
164 {
165     for (ptr = ptr->children; ptr; ptr = ptr->next)
166     {
167         if (ptr->type != XML_ELEMENT_NODE)
168             continue;
169         else if (!strcmp((const char *) ptr->name, "proxy"))
170         {
171             m_p->proxy_host = mp::xml::get_text(ptr);
172         }
173         else if (!strcmp((const char *) ptr->name, "max-redirects"))
174         {
175             m_p->max_redirects = mp::xml::get_int(ptr, 0);
176         }
177         else if (!strcmp((const char *) ptr->name, "default-host"))
178         {
179             m_p->default_host = mp::xml::get_text(ptr);
180             if (m_p->default_host.find("://") == std::string::npos)
181             {
182                 throw mp::filter::FilterException
183                     ("default-host is missing method (such as http://)"
184                      " in http_client filter");
185             }
186         }
187         else if (!strcmp((const char *) ptr->name, "x-forwarded-for"))
188         {
189             m_p->x_forwarded_for = mp::xml::get_bool(ptr, 0);
190         }
191         else if (!strcmp((const char *) ptr->name, "bind_host"))
192         {
193             m_p->bind_host = mp::xml::get_bool(ptr, 0);
194         }
195         else
196         {
197             throw mp::filter::FilterException
198                 ("Bad element "
199                  + std::string((const char *) ptr->name)
200                  + " in http_client filter");
201         }
202     }
203 }
204
205 static mp::filter::Base* filter_creator()
206 {
207     return new mp::filter::HTTPClient;
208 }
209
210 extern "C" {
211     struct metaproxy_1_filter_struct metaproxy_1_filter_http_client = {
212         0,
213         "http_client",
214         filter_creator
215     };
216 }
217
218
219 /*
220  * Local variables:
221  * c-basic-offset: 4
222  * c-file-style: "Stroustrup"
223  * indent-tabs-mode: nil
224  * End:
225  * vim: shiftwidth=4 tabstop=8 expandtab
226  */
227