From 48d8459a8f0a3d1238e0f23e978c1e8db534ab72 Mon Sep 17 00:00:00 2001 From: Heikki Levanto Date: Thu, 11 Jul 2013 13:37:58 +0200 Subject: [PATCH] Add echo option to the bounce filter --- src/filter_bounce.cpp | 67 +++++++++++++++++++++++++++++++++++++++++-------- src/filter_bounce.hpp | 1 + 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/filter_bounce.cpp b/src/filter_bounce.cpp index 4a6e099..acfdde1 100644 --- a/src/filter_bounce.cpp +++ b/src/filter_bounce.cpp @@ -16,6 +16,12 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +/* filter_bounce +A very simple filter that produces some response, in case no earlier +filter has done so, a kind of last resort fallback. Also supports dumping +the request in that response, for debugging and testing purposes +*/ + #include "filter_bounce.hpp" #include #include @@ -32,29 +38,49 @@ namespace metaproxy_1 { namespace filter { class Bounce::Rep { friend class Bounce; - bool bounce; + bool echo; // indicates that we wish to echo the request in the + // HTTP response }; } } yf::Bounce::Bounce() : m_p(new Rep) { - m_p->bounce = true; + m_p->echo = false; } yf::Bounce::~Bounce() -{ // must have a destructor because of boost::scoped_ptr +{ // must have a destructor because of boost::scoped_ptr to m_p } -void yf::Bounce::process(mp::Package &package) const + +// Dump the http request into the content of the http response +static void http_echo(mp::odr &odr, Z_GDU *zgdu, Z_GDU *zgdu_res) { - if (! m_p->bounce ) + int len; + ODR enc = odr_createmem(ODR_ENCODE); + //int r = + (void) z_GDU(enc, &zgdu, 0, 0); + char *buf = odr_getbuf(enc, &len, 0); + //h.db( "\n" + msg + "\n" + std::string(buf,len) ); + Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response; + if (hres) { - package.move(); - return; + z_HTTP_header_set(odr, &hres->headers, + "Content-Type", "text/plain"); + + hres->content_buf = odr_strdup(odr, buf); + hres->content_len = len; } - package.session().close(); + odr_destroy(enc); + +} + +void yf::Bounce::process(mp::Package &package) const +{ + package.session().close(); + Z_GDU *zgdu = package.request().get(); if (!zgdu) @@ -72,6 +98,8 @@ void yf::Bounce::process(mp::Package &package) const apdu_res = odr.create_close(zgdu->u.z3950, Z_Close_systemProblem, message.str().c_str()); + // TODO - Some day we may want a dump of the request in some + // addinfo in the close response package.response() = apdu_res; } else if (zgdu->which == Z_GDU_HTTP_Request) @@ -80,7 +108,10 @@ void yf::Bounce::process(mp::Package &package) const zgdu_res = odr.create_HTTP_Response(package.session(), zgdu->u.HTTP_Request, 400); - + if (m_p->echo) + { + http_echo(odr, zgdu, zgdu_res); + } package.response() = zgdu_res; } else if (zgdu->which == Z_GDU_HTTP_Response) @@ -94,7 +125,23 @@ void yf::Bounce::process(mp::Package &package) const void mp::filter::Bounce::configure(const xmlNode * ptr, bool test_only, const char *path) { - mp::xml::check_empty(ptr); + for (ptr = ptr->children; ptr; ptr = ptr->next) + { + if (ptr->type != XML_ELEMENT_NODE) + continue; + else if (!strcmp((const char *) ptr->name, "echo")) + { + m_p->echo = mp::xml::get_bool(ptr, 0); + } + else + { + throw mp::filter::FilterException + ("Bad element '" + + std::string((const char *) ptr->name) + + "' in bounce filter"); + } + } + } static mp::filter::Base* filter_creator() diff --git a/src/filter_bounce.hpp b/src/filter_bounce.hpp index 7895919..758214a 100644 --- a/src/filter_bounce.hpp +++ b/src/filter_bounce.hpp @@ -18,6 +18,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // Filter that bounces all requests packages // No packages are ever passed to later filters in the chain +// Can optionally return a dump of the request in the http response #ifndef FILTER_BOUNCE_HPP #define FILTER_BOUNCE_HPP -- 1.7.10.4