9e9512a4a6992db55c5c0d38228d10460e60d979
[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             Rep();
56         };
57     }
58 }
59
60 yf::HTTPClient::Rep::Rep()
61 {
62     max_redirects = 0;
63     x_forwarded_for = false;
64 }
65
66 yf::HTTPClient::HTTPClient() : m_p(new Rep)
67 {
68 }
69
70 yf::HTTPClient::~HTTPClient()
71 {
72 }
73
74 void yf::HTTPClient::Rep::proxy(mp::Package &package)
75 {
76     Z_GDU *req_gdu = package.request().get();
77     if (req_gdu && req_gdu->which == Z_GDU_HTTP_Request)
78     {
79         Z_HTTP_Request *hreq = req_gdu->u.HTTP_Request;
80         Z_GDU *res_gdu = 0;
81         mp::odr o;
82         yaz_url_t yaz_url = yaz_url_create();
83         const char *http_proxy =
84             z_HTTP_header_remove(&hreq->headers, "X-Metaproxy-Proxy");
85
86         if (!http_proxy)
87             http_proxy = proxy_host.c_str();
88
89         if (*http_proxy)
90             yaz_url_set_proxy(yaz_url, http_proxy);
91
92         yaz_url_set_max_redirects(yaz_url, max_redirects);
93
94         if (x_forwarded_for)
95         {
96             std::string peer_name2 = package.origin().get_address();
97             const char *peer_name1 =
98                 z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
99             std::string pcomb;
100             if (peer_name1)
101             {
102                 pcomb.append(peer_name1);
103                 pcomb.append(", ");
104             }
105             pcomb.append(peer_name2);
106             z_HTTP_header_set(o, &hreq->headers, "X-Forwarded-For",
107                               pcomb.c_str());
108         }
109         std::string uri;
110         if (hreq->path[0] == '/')
111         {
112             if (default_host.length())
113                 uri = default_host + hreq->path;
114         }
115         else
116             uri = hreq->path;
117         Z_HTTP_Response *http_response = 0;
118         if (uri.length())
119             http_response =
120             yaz_url_exec(yaz_url, uri.c_str(), hreq->method,
121                          hreq->headers, hreq->content_buf,
122                          hreq->content_len);
123         if (http_response)
124         {
125             res_gdu = o.create_HTTP_Response(package.session(), hreq, 200);
126             z_HTTP_header_remove(&http_response->headers, "Transfer-Encoding");
127             res_gdu->u.HTTP_Response = http_response;
128         }
129         else
130         {
131             res_gdu = o.create_HTTP_Response_details(
132                 package.session(),
133                 hreq, 502,
134                 yaz_url_get_error(yaz_url));
135         }
136         package.response() = res_gdu;
137         yaz_url_destroy(yaz_url);
138     }
139     else
140         package.move();
141 }
142
143 void yf::HTTPClient::process(mp::Package &package) const
144 {
145     Z_GDU *gdu = package.request().get();
146     if (gdu && gdu->which == Z_GDU_HTTP_Request)
147         m_p->proxy(package);
148     else
149         package.move();
150 }
151
152 void mp::filter::HTTPClient::configure(const xmlNode * ptr, bool test_only,
153                                        const char *path)
154 {
155     for (ptr = ptr->children; ptr; ptr = ptr->next)
156     {
157         if (ptr->type != XML_ELEMENT_NODE)
158             continue;
159         else if (!strcmp((const char *) ptr->name, "proxy"))
160         {
161             m_p->proxy_host = mp::xml::get_text(ptr);
162         }
163         else if (!strcmp((const char *) ptr->name, "max-redirects"))
164         {
165             m_p->max_redirects = mp::xml::get_int(ptr, 0);
166         }
167         else if (!strcmp((const char *) ptr->name, "default-host"))
168         {
169             m_p->default_host = mp::xml::get_text(ptr);
170             if (m_p->default_host.find("://") == std::string::npos)
171             {
172                 throw mp::filter::FilterException
173                     ("default-host is missing method (such as http://)"
174                      " in http_client filter");
175             }
176         }
177         else if (!strcmp((const char *) ptr->name, "x-forwarded-for"))
178         {
179             m_p->x_forwarded_for = mp::xml::get_bool(ptr, 0);
180         }
181         else
182         {
183             throw mp::filter::FilterException
184                 ("Bad element "
185                  + std::string((const char *) ptr->name)
186                  + " in http_client filter");
187         }
188     }
189 }
190
191 static mp::filter::Base* filter_creator()
192 {
193     return new mp::filter::HTTPClient;
194 }
195
196 extern "C" {
197     struct metaproxy_1_filter_struct metaproxy_1_filter_http_client = {
198         0,
199         "http_client",
200         filter_creator
201     };
202 }
203
204
205 /*
206  * Local variables:
207  * c-basic-offset: 4
208  * c-file-style: "Stroustrup"
209  * indent-tabs-mode: nil
210  * End:
211  * vim: shiftwidth=4 tabstop=8 expandtab
212  */
213