session_shared: expire backend class when no instances left
[metaproxy-moved-to-github.git] / src / filter_bounce.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 /* filter_bounce
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
23 */
24
25 #include "filter_bounce.hpp"
26 #include <metaproxy/package.hpp>
27 #include <metaproxy/util.hpp>
28 #include "gduutil.hpp"
29
30 #include <yaz/zgdu.h>
31
32 #include <sstream>
33
34 namespace mp = metaproxy_1;
35 namespace yf = mp::filter;
36
37 namespace metaproxy_1 {
38     namespace filter {
39         class Bounce::Rep {
40             friend class Bounce;
41             bool echo;  // indicates that we wish to echo the request in the 
42                         // HTTP response
43         };
44     }
45 }
46
47 yf::Bounce::Bounce() : m_p(new Rep)
48 {
49     m_p->echo = false;
50 }
51
52 yf::Bounce::~Bounce()
53 {  // must have a destructor because of boost::scoped_ptr to m_p
54 }
55
56
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)
59 {
60     int len;
61     ODR enc = odr_createmem(ODR_ENCODE);
62     //int r =
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;
67     if (hres)
68     {
69         z_HTTP_header_set(odr, &hres->headers,
70                           "Content-Type", "text/plain");
71         
72         hres->content_buf = odr_strdup(odr, buf);
73         hres->content_len = len;        
74     }
75     odr_destroy(enc);
76     
77 }
78
79
80 void yf::Bounce::process(mp::Package &package) const
81 {
82     package.session().close();
83     
84     Z_GDU *zgdu = package.request().get();
85
86     if (!zgdu)
87         return;
88
89     //std::string message("BOUNCE ");
90     std::ostringstream message;
91     message << "BOUNCE " << *zgdu;
92
93     metaproxy_1::odr odr;
94
95     if (zgdu->which == Z_GDU_Z3950)
96     {
97         Z_APDU *apdu_res = 0;
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;
104     }
105     else if (zgdu->which == Z_GDU_HTTP_Request)
106     {
107         Z_GDU *zgdu_res = 0;
108         zgdu_res
109             = odr.create_HTTP_Response(package.session(),
110                                        zgdu->u.HTTP_Request, 400);
111         if (m_p->echo) 
112         {
113             http_echo(odr, zgdu, zgdu_res);
114         }
115         package.response() = zgdu_res;
116     }
117     else if (zgdu->which == Z_GDU_HTTP_Response)
118     {
119     }
120
121
122     return;
123 }
124
125 void mp::filter::Bounce::configure(const xmlNode * ptr, bool test_only,
126                                    const char *path)
127 {
128     for (ptr = ptr->children; ptr; ptr = ptr->next)
129     {
130         if (ptr->type != XML_ELEMENT_NODE)
131             continue;
132         else if (!strcmp((const char *) ptr->name, "echo"))
133         {
134             m_p->echo = mp::xml::get_bool(ptr, 0);
135         }
136         else
137         {
138             throw mp::filter::FilterException
139             ("Bad element '"
140             + std::string((const char *) ptr->name)
141             + "' in bounce filter");
142         }
143     }
144     
145 }
146
147 static mp::filter::Base* filter_creator()
148 {
149     return new mp::filter::Bounce;
150 }
151
152 extern "C" {
153     struct metaproxy_1_filter_struct metaproxy_1_filter_bounce = {
154         0,
155         "bounce",
156         filter_creator
157     };
158 }
159
160
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * c-file-style: "Stroustrup"
165  * indent-tabs-mode: nil
166  * End:
167  * vim: shiftwidth=4 tabstop=8 expandtab
168  */
169