1 /* This file is part of Metaproxy.
2 Copyright (C) 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 = (char*) odr_malloc(odr, len);
73 memcpy(hres->content_buf, buf, len);
74 hres->content_len = len;
81 void yf::Bounce::process(mp::Package &package) const
83 package.session().close();
85 Z_GDU *zgdu = package.request().get();
90 //std::string message("BOUNCE ");
91 std::ostringstream message;
92 message << "BOUNCE " << *zgdu;
96 if (zgdu->which == Z_GDU_Z3950)
99 apdu_res = odr.create_close(zgdu->u.z3950,
100 Z_Close_systemProblem,
101 message.str().c_str());
102 // TODO - Some day we may want a dump of the request in some
103 // addinfo in the close response
104 package.response() = apdu_res;
106 else if (zgdu->which == Z_GDU_HTTP_Request)
110 = odr.create_HTTP_Response(package.session(),
111 zgdu->u.HTTP_Request, 400);
114 http_echo(odr, zgdu, zgdu_res);
116 package.response() = zgdu_res;
118 else if (zgdu->which == Z_GDU_HTTP_Response)
126 void mp::filter::Bounce::configure(const xmlNode * ptr, bool test_only,
129 for (ptr = ptr->children; ptr; ptr = ptr->next)
131 if (ptr->type != XML_ELEMENT_NODE)
133 else if (!strcmp((const char *) ptr->name, "echo"))
135 m_p->echo = mp::xml::get_bool(ptr, 0);
139 throw mp::filter::FilterException
141 + std::string((const char *) ptr->name)
142 + "' in bounce filter");
148 static mp::filter::Base* filter_creator()
150 return new mp::filter::Bounce;
154 struct metaproxy_1_filter_struct metaproxy_1_filter_bounce = {
165 * c-file-style: "Stroustrup"
166 * indent-tabs-mode: nil
168 * vim: shiftwidth=4 tabstop=8 expandtab