1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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;
56 yf::HTTPClient::HTTPClient() : m_p(new Rep)
60 yf::HTTPClient::~HTTPClient()
64 void yf::HTTPClient::Rep::proxy(mp::Package &package)
66 Z_GDU *req_gdu = package.request().get();
67 if (req_gdu && req_gdu->which == Z_GDU_HTTP_Request)
69 Z_HTTP_Request *hreq = req_gdu->u.HTTP_Request;
72 yaz_url_t yaz_url = yaz_url_create();
74 if (proxy_host.length())
75 yaz_url_set_proxy(yaz_url, proxy_host.c_str());
76 Z_HTTP_Response *http_response =
77 yaz_url_exec(yaz_url, hreq->path, hreq->method,
78 hreq->headers, hreq->content_buf,
82 res_gdu = o.create_HTTP_Response(package.session(), hreq, 200);
83 res_gdu->u.HTTP_Response = http_response;
87 res_gdu = o.create_HTTP_Response(package.session(), hreq, 404);
89 package.response() = res_gdu;
90 yaz_url_destroy(yaz_url);
96 void yf::HTTPClient::process(mp::Package &package) const
98 Z_GDU *gdu = package.request().get();
99 if (gdu && gdu->which == Z_GDU_HTTP_Request)
105 void mp::filter::HTTPClient::configure(const xmlNode * ptr, bool test_only,
108 for (ptr = ptr->children; ptr; ptr = ptr->next)
110 if (ptr->type != XML_ELEMENT_NODE)
112 else if (!strcmp((const char *) ptr->name, "proxy"))
114 m_p->proxy_host = mp::xml::get_text(ptr);
118 throw mp::filter::FilterException
120 + std::string((const char *) ptr->name)
121 + " in virt_db filter");
126 static mp::filter::Base* filter_creator()
128 return new mp::filter::HTTPClient;
132 struct metaproxy_1_filter_struct metaproxy_1_filter_http_client = {
143 * c-file-style: "Stroustrup"
144 * indent-tabs-mode: nil
146 * vim: shiftwidth=4 tabstop=8 expandtab