metaproxy  1.13.0
filter_bounce.cpp
Go to the documentation of this file.
1 /* This file is part of Metaproxy.
2  Copyright (C) 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 = (char*) odr_malloc(odr, len);
73  memcpy(hres->content_buf, buf, len);
74  hres->content_len = len;
75  }
76  odr_destroy(enc);
77 
78 }
79 
80 
81 void yf::Bounce::process(mp::Package &package) const
82 {
83  package.session().close();
84 
85  Z_GDU *zgdu = package.request().get();
86 
87  if (!zgdu)
88  return;
89 
90  //std::string message("BOUNCE ");
91  std::ostringstream message;
92  message << "BOUNCE " << *zgdu;
93 
94  metaproxy_1::odr odr;
95 
96  if (zgdu->which == Z_GDU_Z3950)
97  {
98  Z_APDU *apdu_res = 0;
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;
105  }
106  else if (zgdu->which == Z_GDU_HTTP_Request)
107  {
108  Z_GDU *zgdu_res = 0;
109  zgdu_res
110  = odr.create_HTTP_Response(package.session(),
111  zgdu->u.HTTP_Request, 400);
112  if (m_p->echo)
113  {
114  http_echo(odr, zgdu, zgdu_res);
115  }
116  package.response() = zgdu_res;
117  }
118  else if (zgdu->which == Z_GDU_HTTP_Response)
119  {
120  }
121 
122 
123  return;
124 }
125 
126 void mp::filter::Bounce::configure(const xmlNode * ptr, bool test_only,
127  const char *path)
128 {
129  for (ptr = ptr->children; ptr; ptr = ptr->next)
130  {
131  if (ptr->type != XML_ELEMENT_NODE)
132  continue;
133  else if (!strcmp((const char *) ptr->name, "echo"))
134  {
135  m_p->echo = mp::xml::get_bool(ptr, 0);
136  }
137  else
138  {
139  throw mp::filter::FilterException
140  ("Bad element '"
141  + std::string((const char *) ptr->name)
142  + "' in bounce filter");
143  }
144  }
145 
146 }
147 
148 static mp::filter::Base* filter_creator()
149 {
150  return new mp::filter::Bounce;
151 }
152 
153 extern "C" {
154  struct metaproxy_1_filter_struct metaproxy_1_filter_bounce = {
155  0,
156  "bounce",
158  };
159 }
160 
161 
162 /*
163  * Local variables:
164  * c-basic-offset: 4
165  * c-file-style: "Stroustrup"
166  * indent-tabs-mode: nil
167  * End:
168  * vim: shiftwidth=4 tabstop=8 expandtab
169  */
170 
struct metaproxy_1_filter_struct metaproxy_1_filter_bounce
static void http_echo(mp::odr &odr, Z_GDU *zgdu, Z_GDU *zgdu_res)
static mp::filter::Base * filter_creator()
boost::scoped_ptr< Rep > m_p