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