From 409c020044df74e1883710cbc1f2860dee8c5ac4 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 18 Apr 2013 11:31:44 +0200 Subject: [PATCH] Implemented http_client MP-432 --- etc/config4.xml | 3 + src/Makefile.am | 1 + src/factory_static.cpp | 2 + src/filter_http_client.cpp | 148 +++++++++++++++++++++++++++++++++++++ src/filter_http_client.hpp | 56 ++++++++++++++ xml/schema/Makefile.am | 1 + xml/schema/filter_http_client.rnc | 10 +++ xml/schema/metaproxy.rnc | 2 + 8 files changed, 223 insertions(+) create mode 100644 src/filter_http_client.cpp create mode 100644 src/filter_http_client.hpp create mode 100644 xml/schema/filter_http_client.rnc diff --git a/etc/config4.xml b/etc/config4.xml index 267c6a5..15cd2eb 100644 --- a/etc/config4.xml +++ b/etc/config4.xml @@ -18,6 +18,9 @@ /etc + + localhost:9999 + diff --git a/src/Makefile.am b/src/Makefile.am index 17c0c9b..04e2f81 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,6 +28,7 @@ libmetaproxy_la_SOURCES = \ filter_cgi.cpp filter_cgi.hpp \ filter_cql_to_rpn.cpp filter_cql_to_rpn.hpp \ filter_frontend_net.cpp filter_frontend_net.hpp \ + filter_http_client.cpp filter_http_client.hpp \ filter_http_file.cpp filter_http_file.hpp \ filter_limit.cpp filter_limit.hpp \ filter_load_balance.cpp filter_load_balance.hpp \ diff --git a/src/factory_static.cpp b/src/factory_static.cpp index 2313bd3..3942637 100644 --- a/src/factory_static.cpp +++ b/src/factory_static.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #endif #include "filter_cql_to_rpn.hpp" #include "filter_frontend_net.hpp" +#include "filter_http_client.hpp" #include "filter_http_file.hpp" #include "filter_limit.hpp" #include "filter_load_balance.hpp" @@ -64,6 +65,7 @@ mp::FactoryStatic::FactoryStatic() #endif &metaproxy_1_filter_cql_to_rpn, &metaproxy_1_filter_frontend_net, + &metaproxy_1_filter_http_client, &metaproxy_1_filter_http_file, &metaproxy_1_filter_limit, &metaproxy_1_filter_load_balance, diff --git a/src/filter_http_client.cpp b/src/filter_http_client.cpp new file mode 100644 index 0000000..fd6945b --- /dev/null +++ b/src/filter_http_client.cpp @@ -0,0 +1,148 @@ +/* This file is part of Metaproxy. + Copyright (C) 2005-2013 Index Data + +Metaproxy is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "config.hpp" +#include +#include +#include +#include +#include "filter_http_client.hpp" + +#include +#include + +#include + +#include +#include +#include + +#if HAVE_SYS_TYPES_H +#include +#endif + +#if HAVE_SYS_STAT_H +#include +#endif + +namespace mp = metaproxy_1; +namespace yf = mp::filter; + +namespace metaproxy_1 { + namespace filter { + class HTTPClient::Rep { + friend class HTTPClient; + void proxy(mp::Package &package); + std::string proxy_host; + }; + } +} + +yf::HTTPClient::HTTPClient() : m_p(new Rep) +{ +} + +yf::HTTPClient::~HTTPClient() +{ +} + +void yf::HTTPClient::Rep::proxy(mp::Package &package) +{ + Z_GDU *req_gdu = package.request().get(); + if (req_gdu && req_gdu->which == Z_GDU_HTTP_Request) + { + Z_HTTP_Request *hreq = req_gdu->u.HTTP_Request; + Z_GDU *res_gdu = 0; + mp::odr o; + yaz_url_t yaz_url = yaz_url_create(); + + if (proxy_host.length()) + yaz_url_set_proxy(yaz_url, proxy_host.c_str()); + Z_HTTP_Response *http_response = + yaz_url_exec(yaz_url, hreq->path, hreq->method, + hreq->headers, hreq->content_buf, + hreq->content_len); + if (http_response) + { + res_gdu = o.create_HTTP_Response(package.session(), hreq, 200); + res_gdu->u.HTTP_Response = http_response; + } + else + { + res_gdu = o.create_HTTP_Response(package.session(), hreq, 404); + } + package.response() = res_gdu; + yaz_url_destroy(yaz_url); + } + else + package.move(); +} + +void yf::HTTPClient::process(mp::Package &package) const +{ + Z_GDU *gdu = package.request().get(); + if (gdu && gdu->which == Z_GDU_HTTP_Request) + m_p->proxy(package); + else + package.move(); +} + +void mp::filter::HTTPClient::configure(const xmlNode * ptr, bool test_only, + const char *path) +{ + for (ptr = ptr->children; ptr; ptr = ptr->next) + { + if (ptr->type != XML_ELEMENT_NODE) + continue; + else if (!strcmp((const char *) ptr->name, "proxy")) + { + m_p->proxy_host = mp::xml::get_text(ptr); + } + else + { + throw mp::filter::FilterException + ("Bad element " + + std::string((const char *) ptr->name) + + " in virt_db filter"); + } + } +} + +static mp::filter::Base* filter_creator() +{ + return new mp::filter::HTTPClient; +} + +extern "C" { + struct metaproxy_1_filter_struct metaproxy_1_filter_http_client = { + 0, + "http_client", + filter_creator + }; +} + + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/filter_http_client.hpp b/src/filter_http_client.hpp new file mode 100644 index 0000000..27c9ea6 --- /dev/null +++ b/src/filter_http_client.hpp @@ -0,0 +1,56 @@ +/* This file is part of Metaproxy. + Copyright (C) 2005-2013 Index Data + +Metaproxy is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef FILTER_HTTP_CLIENT_HPP +#define FILTER_HTTP_CLIENT_HPP + +#include + +#include + +namespace metaproxy_1 { + namespace filter { + class HTTPClient : public Base { + class Rep; + class Assoc; + public: + ~HTTPClient(); + HTTPClient(); + void process(metaproxy_1::Package & package) const; + void configure(const xmlNode * ptr, bool test_only, + const char *path); + private: + boost::scoped_ptr m_p; + }; + } +} + +extern "C" { + extern struct metaproxy_1_filter_struct metaproxy_1_filter_http_client; +} + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/xml/schema/Makefile.am b/xml/schema/Makefile.am index 8bfdc74..632b4a2 100644 --- a/xml/schema/Makefile.am +++ b/xml/schema/Makefile.am @@ -10,6 +10,7 @@ filter_bounce.rnc \ filter_cgi.rnc \ filter_cql_rpn.rnc \ filter_frontend_net.rnc \ +filter_http_client.rnc \ filter_http_file.rnc \ filter_limit.rnc \ filter_load_balance.rnc \ diff --git a/xml/schema/filter_http_client.rnc b/xml/schema/filter_http_client.rnc new file mode 100644 index 0000000..d4c1881 --- /dev/null +++ b/xml/schema/filter_http_client.rnc @@ -0,0 +1,10 @@ +# Metaproxy XML config file schema + +namespace mp = "http://indexdata.com/metaproxy" + +filter_http_client = + attribute type { "http_client" }, + attribute id { xsd:NCName }?, + attribute name { xsd:NCName }?, + element mp:proxy { xsd:string }? + diff --git a/xml/schema/metaproxy.rnc b/xml/schema/metaproxy.rnc index 286cd8b..39a02bd 100644 --- a/xml/schema/metaproxy.rnc +++ b/xml/schema/metaproxy.rnc @@ -28,6 +28,7 @@ include "filter_bounce.rnc" include "filter_cgi.rnc" include "filter_cql_rpn.rnc" include "filter_frontend_net.rnc" +include "filter_http_client.rnc" include "filter_http_file.rnc" include "filter_limit.rnc" include "filter_load_balance.rnc" @@ -76,6 +77,7 @@ filter = | filter_cgi | filter_cql_rpn | filter_frontend_net + | filter_http_client | filter_http_file | filter_limit | filter_load_balance -- 1.7.10.4