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_rewrite.hpp"
24 #include "html_parser.hpp"
29 #include <boost/regex.hpp>
30 #include <boost/lexical_cast.hpp>
34 namespace mp = metaproxy_1;
35 namespace yf = mp::filter;
37 namespace metaproxy_1 {
39 class HttpRewrite::Replace {
43 std::map<int, std::string> group_index;
44 const std::string search_replace(
45 std::map<std::string, std::string> & vars,
46 const std::string & txt) const;
47 std::string sub_vars (
48 const std::map<std::string, std::string> & vars) const;
52 class HttpRewrite::Rule {
54 std::list<Replace> replace_list;
55 const std::string test_patterns(
56 std::map<std::string, std::string> & vars,
57 const std::string & txt) const;
59 class HttpRewrite::Within {
67 class HttpRewrite::Phase : public HTMLParserEvent {
69 std::list<Within> within_list;
70 void rewrite_reqline(mp::odr & o, Z_HTTP_Request *hreq,
71 std::map<std::string, std::string> & vars) const;
72 void rewrite_headers(mp::odr & o, Z_HTTP_Header *headers,
73 std::map<std::string, std::string> & vars) const;
74 void rewrite_body(mp::odr & o,
75 char **content_buf, int *content_len,
76 std::map<std::string, std::string> & vars) const;
77 void openTagStart(const char *name);
78 void anyTagEnd(const char *name);
79 void attribute(const char *tagName,
83 void closeTag(const char *name);
84 void text(const char *value, int len);
89 yf::HttpRewrite::HttpRewrite() :
90 req_phase(new Phase), res_phase(new Phase)
94 yf::HttpRewrite::~HttpRewrite()
98 void yf::HttpRewrite::process(mp::Package & package) const
100 yaz_log(YLOG_LOG, "HttpRewrite begins....");
101 Z_GDU *gdu = package.request().get();
102 //map of request/response vars
103 std::map<std::string, std::string> vars;
104 //we have an http req
105 if (gdu && gdu->which == Z_GDU_HTTP_Request)
107 Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
109 req_phase->rewrite_reqline(o, hreq, vars);
110 yaz_log(YLOG_LOG, ">> Request headers");
111 req_phase->rewrite_headers(o, hreq->headers, vars);
112 req_phase->rewrite_body(o,
113 &hreq->content_buf, &hreq->content_len, vars);
114 package.request() = gdu;
117 gdu = package.response().get();
118 if (gdu && gdu->which == Z_GDU_HTTP_Response)
120 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
121 yaz_log(YLOG_LOG, "Response code %d", hres->code);
123 yaz_log(YLOG_LOG, "<< Respose headers");
124 res_phase->rewrite_headers(o, hres->headers, vars);
125 res_phase->rewrite_body(o, &hres->content_buf,
126 &hres->content_len, vars);
127 package.response() = gdu;
131 void yf::HttpRewrite::Phase::rewrite_reqline (mp::odr & o,
132 Z_HTTP_Request *hreq,
133 std::map<std::string, std::string> & vars) const
135 //rewrite the request line
137 if (strstr(hreq->path, "http://") == hreq->path)
139 yaz_log(YLOG_LOG, "Path in the method line is absolute, "
140 "possibly a proxy request");
145 //TODO what about proto
147 path += z_HTTP_header_lookup(hreq->headers, "Host");
151 std::list<Within>::const_iterator it = within_list.begin();
152 if (it != within_list.end())
154 RulePtr rule = it->rule;
156 yaz_log(YLOG_LOG, "Proxy request URL is %s", path.c_str());
157 std::string npath = rule->test_patterns(vars, path);
160 yaz_log(YLOG_LOG, "Rewritten request URL is %s", npath.c_str());
161 hreq->path = odr_strdup(o, npath.c_str());
166 void yf::HttpRewrite::Phase::rewrite_headers(mp::odr & o,
167 Z_HTTP_Header *headers,
168 std::map<std::string, std::string> & vars) const
170 for (Z_HTTP_Header *header = headers;
172 header = header->next)
174 std::string sheader(header->name);
176 sheader += header->value;
177 yaz_log(YLOG_LOG, "%s: %s", header->name, header->value);
179 std::list<Within>::const_iterator it = within_list.begin();
180 if (it == within_list.end())
182 RulePtr rule = it->rule;
184 std::string out = rule->test_patterns(vars, sheader);
187 size_t pos = out.find(": ");
188 if (pos == std::string::npos)
190 yaz_log(YLOG_LOG, "Header malformed during rewrite, ignoring");
193 header->name = odr_strdup(o, out.substr(0, pos).c_str());
194 header->value = odr_strdup(o, out.substr(pos+2,
195 std::string::npos).c_str());
200 void yf::HttpRewrite::Phase::rewrite_body(mp::odr & o,
203 std::map<std::string, std::string> & vars) const
208 std::list<Within>::const_iterator it = within_list.begin();
209 if (it != within_list.end())
211 RulePtr rule = it->rule;
213 std::string body(*content_buf);
214 std::string nbody = rule->test_patterns(vars, body);
217 *content_buf = odr_strdup(o, nbody.c_str());
218 *content_len = nbody.size();
225 void yf::HttpRewrite::Phase::openTagStart(const char *name)
229 void yf::HttpRewrite::Phase::anyTagEnd(const char *name)
233 void yf::HttpRewrite::Phase::attribute(const char *tagName,
241 void yf::HttpRewrite::Phase::closeTag(const char *name)
245 void yf::HttpRewrite::Phase::text(const char *value, int len)
251 * Tests pattern from the vector in order and executes recipe on
254 const std::string yf::HttpRewrite::Rule::test_patterns(
255 std::map<std::string, std::string> & vars,
256 const std::string & txt) const
258 std::list<Replace>::const_iterator it = replace_list.begin();
260 for (; it != replace_list.end(); it++)
262 std::string out = it->search_replace(vars, txt);
263 if (!out.empty()) return out;
268 const std::string yf::HttpRewrite::Replace::search_replace(
269 std::map<std::string, std::string> & vars,
270 const std::string & txt) const
272 //exec regex against value
273 boost::regex re(regex);
275 std::string::const_iterator start, end;
279 while (regex_search(start, end, what, re)) //find next full match
282 for (i = 1; i < what.size(); ++i)
284 //check if the group is named
285 std::map<int, std::string>::const_iterator it
286 = group_index.find(i);
287 if (it != group_index.end())
289 if (!what[i].str().empty())
290 vars[it->second] = what[i];
294 //prepare replacement string
295 std::string rvalue = sub_vars(vars);
296 yaz_log(YLOG_LOG, "! Rewritten '%s' to '%s'",
297 what.str(0).c_str(), rvalue.c_str());
298 out.append(start, what[0].first);
300 start = what[0].second; //move search forward
302 //if we had a match cat the last part
303 if (start != txt.begin())
304 out.append(start, end);
308 void yf::HttpRewrite::Replace::parse_groups()
312 const std::string & str = regex;
314 yaz_log(YLOG_LOG, "Parsing groups from '%s'", str.c_str());
315 for (size_t i = 0; i < str.size(); ++i)
318 if (!esc && str[i] == '\\')
323 if (!esc && str[i] == '(') //group starts
326 if (i+1 < str.size() && str[i+1] == '?') //group with attrs
329 if (i+1 < str.size() && str[i+1] == ':') //non-capturing
331 if (gnum > 0) gnum--;
337 if (i+1 < str.size() && str[i+1] == 'P') //optional, python
339 if (i+1 < str.size() && str[i+1] == '<') //named
344 while (++i < str.size())
346 if (str[i] == '>') { term = true; break; }
347 if (!isalnum(str[i]))
348 throw mp::filter::FilterException
349 ("Only alphanumeric chars allowed, found "
353 + boost::lexical_cast<std::string>(i));
357 throw mp::filter::FilterException
358 ("Unterminated group name '" + gname
359 + " in '" + str +"'");
360 group_index[gnum] = gname;
361 yaz_log(YLOG_LOG, "Found named group '%s' at $%d",
362 gname.c_str(), gnum);
371 std::string yf::HttpRewrite::Replace::sub_vars (
372 const std::map<std::string, std::string> & vars) const
376 const std::string & in = recipe;
377 for (size_t i = 0; i < in.size(); ++i)
379 if (!esc && in[i] == '\\')
384 if (!esc && in[i] == '$') //var
386 if (i+1 < in.size() && in[i+1] == '{') //ref prefix
391 while (++i < in.size())
393 if (in[i] == '}') { term = true; break; }
396 if (!term) throw mp::filter::FilterException
397 ("Unterminated var ref in '"+in+"' at "
398 + boost::lexical_cast<std::string>(i));
399 std::map<std::string, std::string>::const_iterator it
401 if (it != vars.end())
408 throw mp::filter::FilterException
409 ("Malformed or trimmed var ref in '"
410 +in+"' at "+boost::lexical_cast<std::string>(i));
422 void yf::HttpRewrite::configure_phase(const xmlNode *ptr, Phase &phase)
424 std::map<std::string, RulePtr > rules;
425 for (ptr = ptr->children; ptr; ptr = ptr->next)
427 if (ptr->type != XML_ELEMENT_NODE)
429 else if (!strcmp((const char *) ptr->name, "rule"))
431 static const char *names[2] = { "name", 0 };
432 std::string values[1];
433 values[0] = "default";
434 mp::xml::parse_attr(ptr, names, values);
436 RulePtr rule(new Rule);
437 for (xmlNode *p = ptr->children; p; p = p->next)
439 if (p->type != XML_ELEMENT_NODE)
441 if (!strcmp((const char *) p->name, "rewrite"))
444 const struct _xmlAttr *attr;
445 for (attr = p->properties; attr; attr = attr->next)
447 if (!strcmp((const char *) attr->name, "from"))
448 replace.regex = mp::xml::get_text(attr->children);
449 else if (!strcmp((const char *) attr->name, "to"))
450 replace.recipe = mp::xml::get_text(attr->children);
452 throw mp::filter::FilterException
454 + std::string((const char *) attr->name)
455 + " in rewrite section of http_rewrite");
457 yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
458 replace.regex.c_str(), replace.recipe.c_str());
459 replace.parse_groups();
460 if (!replace.regex.empty())
461 rule->replace_list.push_back(replace);
464 throw mp::filter::FilterException
466 + std::string((const char *) p->name)
467 + " in http_rewrite filter");
469 if (!rule->replace_list.empty())
470 rules[values[0]] = rule;
472 else if (!strcmp((const char *) ptr->name, "within"))
474 static const char *names[5] =
475 { "header", "attr", "tag", "rule", 0 };
476 std::string values[4];
477 mp::xml::parse_attr(ptr, names, values);
479 w.header = values[0];
482 std::map<std::string,RulePtr>::const_iterator it =
483 rules.find(values[3]);
484 if (it == rules.end())
485 throw mp::filter::FilterException
486 ("Reference to non-existing rule '" + values[3] +
487 "' in http_rewrite filter");
489 phase.within_list.push_back(w);
493 throw mp::filter::FilterException
495 + std::string((const char *) ptr->name)
496 + " in http_rewrite filter");
501 void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
504 for (ptr = ptr->children; ptr; ptr = ptr->next)
506 if (ptr->type != XML_ELEMENT_NODE)
508 else if (!strcmp((const char *) ptr->name, "request"))
510 configure_phase(ptr, *req_phase);
512 else if (!strcmp((const char *) ptr->name, "response"))
514 configure_phase(ptr, *res_phase);
518 throw mp::filter::FilterException
520 + std::string((const char *) ptr->name)
521 + " in http_rewrite1 filter");
526 static mp::filter::Base* filter_creator()
528 return new mp::filter::HttpRewrite;
532 struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite = {
543 * c-file-style: "Stroustrup"
544 * indent-tabs-mode: nil
546 * vim: shiftwidth=4 tabstop=8 expandtab