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