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 A very simple filter that produces some response, in case no earlier
21 filter has done so, a kind of last resort fallback. Also supports dumping
22 the request in that response, for debugging and testing purposes
25 #include "filter_bounce.hpp"
26 #include <metaproxy/package.hpp>
27 #include <metaproxy/util.hpp>
28 #include "gduutil.hpp"
34 namespace mp = metaproxy_1;
35 namespace yf = mp::filter;
37 namespace metaproxy_1 {
41 bool echo; // indicates that we wish to echo the request in the
47 yf::Bounce::Bounce() : m_p(new Rep)
53 { // must have a destructor because of boost::scoped_ptr to m_p
57 // Dump the http request into the content of the http response
58 static void http_echo(mp::odr &odr, Z_GDU *zgdu, Z_GDU *zgdu_res)
61 ODR enc = odr_createmem(ODR_ENCODE);
63 (void) z_GDU(enc, &zgdu, 0, 0);
64 char *buf = odr_getbuf(enc, &len, 0);
65 //h.db( "\n" + msg + "\n" + std::string(buf,len) );
66 Z_HTTP_Response *hres = zgdu_res->u.HTTP_Response;
69 z_HTTP_header_set(odr, &hres->headers,
70 "Content-Type", "text/plain");
72 hres->content_buf = odr_strdup(odr, buf);
73 hres->content_len = len;
80 void yf::Bounce::process(mp::Package &package) const
82 package.session().close();
84 Z_GDU *zgdu = package.request().get();
89 //std::string message("BOUNCE ");
90 std::ostringstream message;
91 message << "BOUNCE " << *zgdu;
95 if (zgdu->which == Z_GDU_Z3950)
98 apdu_res = odr.create_close(zgdu->u.z3950,
99 Z_Close_systemProblem,
100 message.str().c_str());
101 // TODO - Some day we may want a dump of the request in some
102 // addinfo in the close response
103 package.response() = apdu_res;
105 else if (zgdu->which == Z_GDU_HTTP_Request)
109 = odr.create_HTTP_Response(package.session(),
110 zgdu->u.HTTP_Request, 400);
113 http_echo(odr, zgdu, zgdu_res);
115 package.response() = zgdu_res;
117 else if (zgdu->which == Z_GDU_HTTP_Response)
125 void mp::filter::Bounce::configure(const xmlNode * ptr, bool test_only,
128 for (ptr = ptr->children; ptr; ptr = ptr->next)
130 if (ptr->type != XML_ELEMENT_NODE)
132 else if (!strcmp((const char *) ptr->name, "echo"))
134 m_p->echo = mp::xml::get_bool(ptr, 0);
138 throw mp::filter::FilterException
140 + std::string((const char *) ptr->name)
141 + "' in bounce filter");
147 static mp::filter::Base* filter_creator()
149 return new mp::filter::Bounce;
153 struct metaproxy_1_filter_struct metaproxy_1_filter_bounce = {
164 * c-file-style: "Stroustrup"
165 * indent-tabs-mode: nil
167 * vim: shiftwidth=4 tabstop=8 expandtab