20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/regex.hpp>
35 #include <sys/types.h>
39 namespace yf = mp::filter;
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;
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"))
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"))
143 throw mp::filter::FilterException
145 + std::string((
const char *) attr->name)
146 +
" in replace section of http_rewrite1");
149 m_p->rules.push_back(rule);
153 throw mp::filter::FilterException
155 + std::string((
const char *) ptr->name)
156 +
" in http_rewrite1 filter");
163 return new mp::filter::HttpRewrite1;
struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite1
static mp::filter::Base * filter_creator()