metaproxy  1.13.0
filter_http_rewrite1.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 #include "config.hpp"
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include "filter_http_rewrite1.hpp"
24 
25 #include <yaz/zgdu.h>
26 #include <yaz/log.h>
27 
28 #include <boost/thread/mutex.hpp>
29 #include <boost/regex.hpp>
30 
31 #include <list>
32 #include <map>
33 
34 #if HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 
38 namespace mp = metaproxy_1;
39 namespace yf = mp::filter;
40 
41 namespace metaproxy_1 {
42  namespace filter {
44  public:
45  std::string content_type;
46  std::string pattern;
47  std::string replacement;
48  std::string mode;
49  };
51  friend class HttpRewrite1;
52  void rewrite_response(mp::odr &o, Z_HTTP_Response *hres);
53  std::list<Rule> rules;
54  };
55  }
56 }
57 
58 yf::HttpRewrite1::HttpRewrite1() : m_p(new Rep)
59 {
60 }
61 
62 yf::HttpRewrite1::~HttpRewrite1()
63 {
64 }
65 
66 void yf::HttpRewrite1::Rep::rewrite_response(mp::odr &o, Z_HTTP_Response *hres)
67 {
68  const char *ctype = z_HTTP_header_lookup(hres->headers, "Content-Type");
69  if (ctype && hres->content_buf)
70  {
71  std::string text(hres->content_buf, hres->content_len);
72  std::list<Rule>::const_iterator it;
73  int number_of_replaces = 0;
74  for (it = rules.begin(); it != rules.end(); it++)
75  {
76  if (strcmp(ctype, it->content_type.c_str()) == 0)
77  {
78  boost::regex::flag_type b_mode = boost::regex::perl;
79  if (it->mode.find_first_of('i') != std::string::npos)
80  b_mode |= boost::regex::icase;
81  boost::regex e(it->pattern, b_mode);
82  boost::match_flag_type match_mode = boost::format_first_only;
83  if (it->mode.find_first_of('g') != std::string::npos)
84  match_mode = boost::format_all;
85  text = regex_replace(text, e, it->replacement, match_mode);
86  number_of_replaces++;
87  }
88  }
89  if (number_of_replaces > 0)
90  {
91  hres->content_buf = odr_strdup(o, text.c_str());
92  hres->content_len = strlen(hres->content_buf);
93  }
94  }
95 }
96 
97 void yf::HttpRewrite1::process(mp::Package &package) const
98 {
99  Z_GDU *gdu_req = package.request().get();
100  if (gdu_req && gdu_req->which == Z_GDU_HTTP_Request)
101  {
102  Z_HTTP_Request *hreq = gdu_req->u.HTTP_Request;
103 
104  assert(hreq); // not changing request (such as POST content)
105  package.move();
106 
107  Z_GDU *gdu_res = package.response().get();
108  Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
109  if (hres)
110  {
111  mp::odr o;
112  m_p->rewrite_response(o, hres);
113  package.response() = gdu_res;
114  }
115  }
116  else
117  package.move();
118 }
119 
120 void mp::filter::HttpRewrite1::configure(const xmlNode * ptr, bool test_only,
121  const char *path)
122 {
123  for (ptr = ptr->children; ptr; ptr = ptr->next)
124  {
125  if (ptr->type != XML_ELEMENT_NODE)
126  continue;
127  else if (!strcmp((const char *) ptr->name, "replace"))
128  {
129  HttpRewrite1::Rule rule;
130 
131  const struct _xmlAttr *attr;
132  for (attr = ptr->properties; attr; attr = attr->next)
133  {
134  if (!strcmp((const char *) attr->name, "pattern"))
135  rule.pattern = mp::xml::get_text(attr->children);
136  else if (!strcmp((const char *) attr->name, "replacement"))
137  rule.replacement = mp::xml::get_text(attr->children);
138  else if (!strcmp((const char *) attr->name, "mode"))
139  rule.mode = mp::xml::get_text(attr->children);
140  else if (!strcmp((const char *) attr->name, "content-type"))
141  rule.content_type = mp::xml::get_text(attr->children);
142  else
143  throw mp::filter::FilterException
144  ("Bad attribute "
145  + std::string((const char *) attr->name)
146  + " in replace section of http_rewrite1");
147  }
148  if (rule.pattern.length() > 0)
149  m_p->rules.push_back(rule);
150  }
151  else
152  {
153  throw mp::filter::FilterException
154  ("Bad element "
155  + std::string((const char *) ptr->name)
156  + " in http_rewrite1 filter");
157  }
158  }
159 }
160 
161 static mp::filter::Base* filter_creator()
162 {
163  return new mp::filter::HttpRewrite1;
164 }
165 
166 extern "C" {
167  struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite1 = {
168  0,
169  "http_rewrite1",
171  };
172 }
173 
174 
175 /*
176  * Local variables:
177  * c-basic-offset: 4
178  * c-file-style: "Stroustrup"
179  * indent-tabs-mode: nil
180  * End:
181  * vim: shiftwidth=4 tabstop=8 expandtab
182  */
183 
struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite1
static mp::filter::Base * filter_creator()