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 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include "filter_http_rewrite1.hpp"
28 #include <boost/thread/mutex.hpp>
29 #include <boost/regex.hpp>
35 #include <sys/types.h>
38 namespace mp = metaproxy_1;
39 namespace yf = mp::filter;
41 namespace metaproxy_1 {
43 class HttpRewrite1::Rule {
45 std::string content_type;
47 std::string replacement;
50 class HttpRewrite1::Rep {
51 friend class HttpRewrite1;
52 void rewrite_response(mp::odr &o, Z_HTTP_Response *hres);
53 std::list<Rule> rules;
58 yf::HttpRewrite1::HttpRewrite1() : m_p(new Rep)
62 yf::HttpRewrite1::~HttpRewrite1()
66 void yf::HttpRewrite1::Rep::rewrite_response(mp::odr &o, Z_HTTP_Response *hres)
68 const char *ctype = z_HTTP_header_lookup(hres->headers, "Content-Type");
69 if (ctype && hres->content_buf)
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++)
76 if (strcmp(ctype, it->content_type.c_str()) == 0)
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);
89 if (number_of_replaces > 0)
91 hres->content_buf = odr_strdup(o, text.c_str());
92 hres->content_len = strlen(hres->content_buf);
97 void yf::HttpRewrite1::process(mp::Package &package) const
99 Z_GDU *gdu_req = package.request().get();
100 if (gdu_req && gdu_req->which == Z_GDU_HTTP_Request)
102 Z_HTTP_Request *hreq = gdu_req->u.HTTP_Request;
104 assert(hreq); // not changing request (such as POST content)
107 Z_GDU *gdu_res = package.response().get();
108 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
112 m_p->rewrite_response(o, hres);
113 package.response() = gdu_res;
120 void mp::filter::HttpRewrite1::configure(const xmlNode * ptr, bool test_only,
123 for (ptr = ptr->children; ptr; ptr = ptr->next)
125 if (ptr->type != XML_ELEMENT_NODE)
127 else if (!strcmp((const char *) ptr->name, "replace"))
129 HttpRewrite1::Rule rule;
131 const struct _xmlAttr *attr;
132 for (attr = ptr->properties; attr; attr = attr->next)
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);
143 throw mp::filter::FilterException
145 + std::string((const char *) attr->name)
146 + " in replace section of http_rewrite1");
148 if (rule.pattern.length() > 0)
149 m_p->rules.push_back(rule);
153 throw mp::filter::FilterException
155 + std::string((const char *) ptr->name)
156 + " in http_rewrite1 filter");
161 static mp::filter::Base* filter_creator()
163 return new mp::filter::HttpRewrite1;
167 struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite1 = {
178 * c-file-style: "Stroustrup"
179 * indent-tabs-mode: nil
181 * vim: shiftwidth=4 tabstop=8 expandtab