Implemented http_client MP-432
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 18 Apr 2013 09:31:44 +0000 (11:31 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 18 Apr 2013 09:31:44 +0000 (11:31 +0200)
etc/config4.xml
src/Makefile.am
src/factory_static.cpp
src/filter_http_client.cpp [new file with mode: 0644]
src/filter_http_client.hpp [new file with mode: 0644]
xml/schema/Makefile.am
xml/schema/filter_http_client.rnc [new file with mode: 0644]
xml/schema/metaproxy.rnc

index 267c6a5..15cd2eb 100644 (file)
@@ -18,6 +18,9 @@
        <prefix>/etc</prefix>
       </area>
       </filter>
+      <filter type="http_client">
+       <proxy>localhost:9999</proxy>
+      </filter>
       <filter type="bounce"/>
     </route>
   </routes>
index 17c0c9b..04e2f81 100644 (file)
@@ -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 \
index 2313bd3..3942637 100644 (file)
@@ -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 (file)
index 0000000..fd6945b
--- /dev/null
@@ -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 <metaproxy/filter.hpp>
+#include <metaproxy/package.hpp>
+#include <metaproxy/util.hpp>
+#include <yaz/url.h>
+#include "filter_http_client.hpp"
+
+#include <yaz/zgdu.h>
+#include <yaz/log.h>
+
+#include <boost/thread/mutex.hpp>
+
+#include <list>
+#include <map>
+#include <iostream>
+
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
+#if HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#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 (file)
index 0000000..27c9ea6
--- /dev/null
@@ -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 <boost/scoped_ptr.hpp>
+
+#include <metaproxy/filter.hpp>
+
+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<Rep> 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
+ */
+
index 8bfdc74..632b4a2 100644 (file)
@@ -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 (file)
index 0000000..d4c1881
--- /dev/null
@@ -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 }?
+
index 286cd8b..39a02bd 100644 (file)
@@ -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