1 /* This file is part of Metaproxy.
2 Copyright (C) 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"
30 #include <boost/regex.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/algorithm/string.hpp>
36 namespace mp = metaproxy_1;
37 namespace yf = mp::filter;
39 namespace metaproxy_1 {
41 class HttpRewrite::Replace {
46 std::map<int, std::string> group_index;
48 const std::map<std::string, std::string> & vars) const;
49 void parse_groups(std::string pattern);
52 class HttpRewrite::Rule {
54 std::list<Replace> replace_list;
56 std::map<std::string, std::string> &vars,
57 std::string &txt, bool anchor,
58 std::list<boost::regex> &skip_list);
60 class HttpRewrite::Within {
68 bool exec(std::map<std::string, std::string> &vars,
69 std::string &txt, bool anchor,
70 std::list<boost::regex> &skip_list) const;
73 class HttpRewrite::Content {
76 boost::regex content_re;
77 std::list<Within> within_list;
78 void configure(const xmlNode *ptr,
79 std::map<std::string, RulePtr > &rules);
80 void quoted_literal(std::string &content,
81 std::map<std::string, std::string> &vars,
82 std::list<boost::regex> & skip_list) const;
83 void parse(int verbose, std::string &content,
84 std::map<std::string, std::string> & vars,
85 std::list<boost::regex> & skip_list ) const;
87 class HttpRewrite::Phase {
91 std::list<Content> content_list;
92 void read_skip_headers(Z_HTTP_Request *hreq,
93 std::list<boost::regex> &skip_list);
94 void rewrite_reqline(mp::odr & o, Z_HTTP_Request *hreq,
95 std::map<std::string, std::string> & vars) const;
96 void rewrite_headers(mp::odr & o, Z_HTTP_Header *headers,
97 std::map<std::string, std::string> & vars) const;
98 void rewrite_body(mp::odr & o,
99 const char *content_type,
100 char **content_buf, int *content_len,
101 std::map<std::string, std::string> & vars,
102 std::list<boost::regex> & skip_list ) const;
104 class HttpRewrite::Event : public HTMLParserEvent {
105 void openTagStart(const char *tag, int tag_len);
106 void anyTagEnd(const char *tag, int tag_len, int close_it);
107 void attribute(const char *tag, int tag_len,
108 const char *attr, int attr_len,
109 const char *value, int val_len,
111 void closeTag(const char *tag, int tag_len);
112 void text(const char *value, int len);
113 const Content *m_content;
115 std::stack<std::list<Within>::const_iterator> s_within;
116 std::map<std::string, std::string> &m_vars;
117 std::list<boost::regex> & m_skips;
119 Event(const Content *p,
120 std::map<std::string, std::string> &vars,
121 std::list<boost::regex> & skip_list );
123 const char *result();
128 yf::HttpRewrite::HttpRewrite() :
129 req_phase(new Phase), res_phase(new Phase)
133 yf::HttpRewrite::~HttpRewrite()
137 void yf::HttpRewrite::process(mp::Package & package) const
139 yaz_log(YLOG_LOG, "HttpRewrite begins....");
140 Z_GDU *gdu = package.request().get();
141 //map of request/response vars
142 std::map<std::string, std::string> vars;
143 //we have an http req
145 std::list<boost::regex> skip_list;
147 if (gdu && gdu->which == Z_GDU_HTTP_Request)
149 Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
151 req_phase->rewrite_reqline(o, hreq, vars);
152 res_phase->read_skip_headers(hreq, skip_list);
153 yaz_log(YLOG_LOG, ">> Request headers");
154 req_phase->rewrite_headers(o, hreq->headers, vars);
155 req_phase->rewrite_body(o,
156 z_HTTP_header_lookup(hreq->headers,
158 &hreq->content_buf, &hreq->content_len,
160 package.request() = gdu;
163 gdu = package.response().get();
164 if (gdu && gdu->which == Z_GDU_HTTP_Response)
166 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
167 yaz_log(YLOG_LOG, "Response code %d", hres->code);
169 yaz_log(YLOG_LOG, "<< Respose headers");
170 res_phase->rewrite_headers(o, hres->headers, vars);
171 res_phase->rewrite_body(o,
172 z_HTTP_header_lookup(hres->headers,
174 &hres->content_buf, &hres->content_len,
176 package.response() = gdu;
180 // Read (and remove) the X-Metaproxy-SkipLink headers
181 void yf::HttpRewrite::Phase::read_skip_headers(Z_HTTP_Request *hreq,
182 std::list<boost::regex> &skip_list )
184 std::string url(hreq->path);
185 if ( url.substr(0,7) != "http://" )
186 { // path was relative, as it usually is
187 const char *host = z_HTTP_header_lookup(hreq->headers, "Host");
189 url = "http://" + std::string(host) + hreq->path ;
192 while ( const char *hv = z_HTTP_header_remove( &(hreq->headers),
193 "X-Metaproxy-SkipLink") )
195 yaz_log(YLOG_LOG,"Found SkipLink '%s'", hv );
196 const char *p = strchr(hv,' ');
198 continue; // should not happen
199 std::string page(hv,p);
200 std::string link(p+1);
201 boost::regex pagere(page);
202 if ( boost::regex_search(url, pagere) )
204 yaz_log(YLOG_LOG,"SkipLink '%s' matches URL %s",
205 page.c_str(), url.c_str() );
206 boost::regex linkre(link);
207 skip_list.push_back(linkre);
211 yaz_log(YLOG_LOG,"SkipLink ignored, '%s' does not match '%s'",
212 url.c_str(), page.c_str() );
218 void yf::HttpRewrite::Phase::rewrite_reqline (mp::odr & o,
219 Z_HTTP_Request *hreq,
220 std::map<std::string, std::string> & vars) const
222 //rewrite the request line
224 if (strstr(hreq->path, "http://") == hreq->path)
226 yaz_log(YLOG_LOG, "Path in the method line is absolute, "
227 "possibly a proxy request");
232 //TODO what about proto
233 const char *host = z_HTTP_header_lookup(hreq->headers, "Host");
242 std::list<Content>::const_iterator cit = content_list.begin();
243 for (; cit != content_list.end(); cit++)
244 if (cit->type == "headers")
247 if (cit == content_list.end())
250 std::list<Within>::const_iterator it = cit->within_list.begin();
251 for (; it != cit->within_list.end(); it++)
254 yaz_log(YLOG_LOG, "Proxy request URL is %s", path.c_str());
255 std::list<boost::regex> dummy_skip_list; // no skips here!
256 if (it->exec(vars, path, true, dummy_skip_list))
258 yaz_log(YLOG_LOG, "Rewritten request URL is %s", path.c_str());
259 hreq->path = odr_strdup(o, path.c_str());
264 void yf::HttpRewrite::Phase::rewrite_headers(mp::odr & o,
265 Z_HTTP_Header *headers,
266 std::map<std::string, std::string> & vars ) const
268 std::list<Content>::const_iterator cit = content_list.begin();
269 for (; cit != content_list.end(); cit++)
270 if (cit->type == "headers")
273 if (cit == content_list.end())
276 for (Z_HTTP_Header *header = headers; header; header = header->next)
278 std::list<Within>::const_iterator it = cit->within_list.begin();
279 for (; it != cit->within_list.end(); it++)
281 if (!it->header.empty() &&
282 regex_match(header->name, it->header))
284 // Match and replace only the header value
285 std::string hval(header->value);
286 std::list<boost::regex> dummy_skip_list; // no skips here!
287 if (it->exec(vars, hval, true, dummy_skip_list))
289 header->value = odr_strdup(o, hval.c_str());
296 void yf::HttpRewrite::Phase::rewrite_body(
298 const char *content_type,
301 std::map<std::string, std::string> & vars,
302 std::list<boost::regex> & skip_list ) const
304 if (*content_len == 0)
307 yaz_log(YLOG_LOG, "rewrite_body: null content_type, can not rewrite");
310 std::list<Content>::const_iterator cit = content_list.begin();
311 for (; cit != content_list.end(); cit++)
313 yaz_log(YLOG_LOG, "rewrite_body: content_type=%s type=%s",
314 content_type, cit->type.c_str());
315 if (cit->type != "headers"
316 && regex_match(content_type, cit->content_re))
319 if (cit == content_list.end()) {
320 yaz_log(YLOG_LOG,"rewrite_body: No content rule matched %s, not rewriting",
326 for (i = 0; i < *content_len; i++)
327 if ((*content_buf)[i] == 0) {
328 yaz_log(YLOG_LOG,"rewrite_body: Looks like binary stuff, not rewriting");
329 return; // binary content. skip
332 std::string content(*content_buf, *content_len);
333 cit->parse(m_verbose, content, vars, skip_list);
334 *content_buf = odr_strdup(o, content.c_str());
335 *content_len = strlen(*content_buf);
338 yf::HttpRewrite::Event::Event(const Content *p,
339 std::map<std::string, std::string> & vars,
340 std::list<boost::regex> & skip_list
341 ) : m_content(p), m_vars(vars), m_skips(skip_list)
346 yf::HttpRewrite::Event::~Event()
351 const char *yf::HttpRewrite::Event::result()
353 return wrbuf_cstr(m_w);
356 void yf::HttpRewrite::Event::openTagStart(const char *tag, int tag_len)
358 wrbuf_putc(m_w, '<');
359 wrbuf_write(m_w, tag, tag_len);
361 std::string t(tag, tag_len);
362 std::list<Within>::const_iterator it = m_content->within_list.begin();
363 for (; it != m_content->within_list.end(); it++)
365 if (!it->tag.empty() && regex_match(t, it->tag))
367 if (!it->attr.empty() && regex_match("#text", it->attr))
376 void yf::HttpRewrite::Event::anyTagEnd(const char *tag, int tag_len,
381 if (!s_within.empty())
383 std::list<Within>::const_iterator it = s_within.top();
384 std::string t(tag, tag_len);
385 if (regex_match(t, it->tag))
390 wrbuf_putc(m_w, '/');
391 wrbuf_putc(m_w, '>');
394 void yf::HttpRewrite::Event::attribute(const char *tag, int tag_len,
395 const char *attr, int attr_len,
396 const char *value, int val_len,
399 std::list<Within>::const_iterator it = m_content->within_list.begin();
402 for (; it != m_content->within_list.end(); it++)
404 std::string t(tag, tag_len);
405 if (it->tag.empty() || regex_match(t, it->tag))
407 std::string a(attr, attr_len);
408 if (!it->attr.empty() && regex_match(a, it->attr))
415 wrbuf_putc(m_w, ' ');
416 wrbuf_write(m_w, attr, attr_len);
419 wrbuf_puts(m_w, "=");
420 wrbuf_puts(m_w, sep);
425 std::string s(value, val_len);
426 it->exec(m_vars, s, true, m_skips);
427 wrbuf_puts(m_w, s.c_str());
430 wrbuf_write(m_w, value, val_len);
431 wrbuf_puts(m_w, sep);
435 void yf::HttpRewrite::Event::closeTag(const char *tag, int tag_len)
437 if (!s_within.empty())
439 std::list<Within>::const_iterator it = s_within.top();
440 std::string t(tag, tag_len);
441 if (regex_match(t, it->tag))
444 wrbuf_puts(m_w, "</");
445 wrbuf_write(m_w, tag, tag_len);
448 void yf::HttpRewrite::Event::text(const char *value, int len)
450 std::list<Within>::const_iterator it = m_content->within_list.end();
451 if (!s_within.empty())
453 if (it != m_content->within_list.end())
455 std::string s(value, len);
456 it->exec(m_vars, s, false, m_skips);
457 wrbuf_puts(m_w, s.c_str());
460 wrbuf_write(m_w, value, len);
463 static bool embed_quoted_literal(
464 std::string &content,
465 std::map<std::string, std::string> &vars,
466 mp::filter::HttpRewrite::RulePtr ruleptr,
468 std::list<boost::regex> &skip_list)
470 bool replace = false;
472 const char *cp = content.c_str();
473 const char *cp0 = cp;
476 if (html_context && !strncmp(cp, """, 6))
479 res.append(cp0, cp - cp0);
483 if (!strncmp(cp, """, 6))
491 std::string s(cp0, cp - cp0);
492 if (ruleptr->test_patterns(vars, s, true, skip_list))
497 else if (*cp == '"' || *cp == '\'')
501 res.append(cp0, cp - cp0);
505 if (cp[-1] != '\\' && *cp == m)
513 std::string s(cp0, cp - cp0);
514 if (ruleptr->test_patterns(vars, s, true, skip_list))
519 else if (*cp == '/' && cp[1] == '/')
521 while (cp[1] && cp[1] != '\n')
526 res.append(cp0, cp - cp0);
531 bool yf::HttpRewrite::Within::exec(
532 std::map<std::string, std::string> & vars,
533 std::string & txt, bool anchor,
534 std::list<boost::regex> & skip_list) const
536 if (type == "quoted-literal")
538 return embed_quoted_literal(txt, vars, rule, true, skip_list);
542 return rule->test_patterns(vars, txt, anchor, skip_list);
546 bool yf::HttpRewrite::Rule::test_patterns(
547 std::map<std::string, std::string> & vars,
548 std::string & txt, bool anchor,
549 std::list<boost::regex> & skip_list )
551 bool replaces = false;
554 std::string::const_iterator start, end;
559 std::list<Replace>::iterator bit = replace_list.end();
561 bool match_one = false;
563 std::list<Replace>::iterator it = replace_list.begin();
564 for (; it != replace_list.end(); it++)
566 if (it->start_anchor && !first)
569 if (regex_search(start, end, what, it->re))
571 if (!match_one || what[0].first < bwhat[0].first)
585 for (i = 1; i < bwhat.size(); ++i)
587 //check if the group is named
588 std::map<int, std::string>::const_iterator git
589 = bit->group_index.find(i);
590 if (git != bit->group_index.end())
592 vars[git->second] = bwhat[i];
596 // Compare against skip_list
597 bool skipthis = false;
598 std::list<boost::regex>::iterator si = skip_list.begin();
599 for ( ; si != skip_list.end(); si++) {
600 if ( boost::regex_search(bwhat.str(0), *si) )
606 //prepare replacement string
607 std::string rvalue = bit->sub_vars(vars);
608 out.append(start, bwhat[0].first);
611 yaz_log(YLOG_LOG,"! Not rewriting '%s', skiplist match",
612 bwhat.str(0).c_str() );
613 out.append(bwhat.str(0).c_str());
617 yaz_log(YLOG_LOG, "! Rewritten '%s' to '%s'",
618 bwhat.str(0).c_str(), rvalue.c_str());
621 start = bwhat[0].second; //move search forward
623 out.append(start, end);
628 void yf::HttpRewrite::Replace::parse_groups(std::string pattern)
632 const std::string &str = pattern;
634 start_anchor = str[0] == '^';
635 yaz_log(YLOG_LOG, "Parsing groups from '%s'", str.c_str());
636 for (size_t i = 0; i < str.size(); ++i)
639 if (!esc && str[i] == '\\')
644 if (!esc && str[i] == '(') //group starts
647 if (i+1 < str.size() && str[i+1] == '?') //group with attrs
650 if (i+1 < str.size() && str[i+1] == ':') //non-capturing
652 if (gnum > 0) gnum--;
658 if (i+1 < str.size() && str[i+1] == 'P') //optional, python
660 if (i+1 < str.size() && str[i+1] == '<') //named
665 while (++i < str.size())
667 if (str[i] == '>') { term = true; break; }
668 if (!isalnum(str[i]))
669 throw mp::filter::FilterException
670 ("Only alphanumeric chars allowed, found "
674 + boost::lexical_cast<std::string>(i));
678 throw mp::filter::FilterException
679 ("Unterminated group name '" + gname
680 + " in '" + str +"'");
681 group_index[gnum] = gname;
682 yaz_log(YLOG_LOG, "Found named group '%s' at $%d",
683 gname.c_str(), gnum);
692 std::string yf::HttpRewrite::Replace::sub_vars(
693 const std::map<std::string, std::string> & vars) const
697 const std::string & in = recipe;
698 for (size_t i = 0; i < in.size(); ++i)
700 if (!esc && in[i] == '\\')
705 if (!esc && in[i] == '$') //var
707 if (i+1 < in.size() && in[i+1] == '{') //ref prefix
712 while (++i < in.size())
714 if (in[i] == '}') { term = true; break; }
717 if (!term) throw mp::filter::FilterException
718 ("Unterminated var ref in '"+in+"' at "
719 + boost::lexical_cast<std::string>(i));
720 std::map<std::string, std::string>::const_iterator it
722 if (it != vars.end())
729 throw mp::filter::FilterException
730 ("Malformed or trimmed var ref in '"
731 +in+"' at "+boost::lexical_cast<std::string>(i));
742 yf::HttpRewrite::Phase::Phase() : m_verbose(0)
746 void yf::HttpRewrite::Content::parse(
748 std::string &content,
749 std::map<std::string, std::string> &vars,
750 std::list<boost::regex> & skip_list ) const
755 Event ev(this, vars, skip_list);
757 parser.set_verbose(verbose);
759 parser.parse(ev, content.c_str());
760 content = ev.result();
762 if (type == "quoted-literal")
764 quoted_literal(content, vars, skip_list);
768 void yf::HttpRewrite::Content::quoted_literal(
769 std::string &content,
770 std::map<std::string, std::string> &vars,
771 std::list<boost::regex> & skip_list ) const
773 std::list<Within>::const_iterator it = within_list.begin();
774 if (it != within_list.end())
775 embed_quoted_literal(content, vars, it->rule, false, skip_list);
778 void yf::HttpRewrite::Content::configure(
779 const xmlNode *ptr, std::map<std::string, RulePtr > &rules)
781 for (; ptr; ptr = ptr->next)
783 if (ptr->type != XML_ELEMENT_NODE)
785 if (!strcmp((const char *) ptr->name, "within"))
787 static const char *names[7] =
788 { "header", "attr", "tag", "rule", "reqline", "type", 0 };
789 std::string values[6];
790 mp::xml::parse_attr(ptr, names, values);
792 if (values[0].length() > 0)
793 w.header.assign(values[0], boost::regex_constants::icase);
794 if (values[1].length() > 0)
795 w.attr.assign(values[1], boost::regex_constants::icase);
796 if (values[2].length() > 0)
797 w.tag.assign(values[2], boost::regex_constants::icase);
799 std::vector<std::string> rulenames;
800 boost::split(rulenames, values[3], boost::is_any_of(","));
801 if (rulenames.size() == 0)
803 throw mp::filter::FilterException
804 ("Empty rule in '" + values[3] +
805 "' in http_rewrite filter");
807 else if (rulenames.size() == 1)
809 std::map<std::string,RulePtr>::const_iterator it =
810 rules.find(rulenames[0]);
811 if (it == rules.end())
812 throw mp::filter::FilterException
813 ("Reference to non-existing rule '" + rulenames[0] +
814 "' in http_rewrite filter");
820 RulePtr rule(new Rule);
822 for (i = 0; i < rulenames.size(); i++)
824 std::map<std::string,RulePtr>::const_iterator it =
825 rules.find(rulenames[i]);
826 if (it == rules.end())
827 throw mp::filter::FilterException
828 ("Reference to non-existing rule '" + rulenames[i] +
829 "' in http_rewrite filter");
830 RulePtr subRule = it->second;
831 std::list<Replace>::iterator rit =
832 subRule->replace_list.begin();
833 for (; rit != subRule->replace_list.end(); rit++)
834 rule->replace_list.push_back(*rit);
838 w.reqline = values[4] == "1";
840 if (w.type.empty() || w.type == "quoted-literal")
843 throw mp::filter::FilterException
844 ("within type must be quoted-literal or none in "
845 " in http_rewrite filter");
846 within_list.push_back(w);
851 void yf::HttpRewrite::configure_phase(const xmlNode *ptr, Phase &phase)
853 static const char *names[2] = { "verbose", 0 };
854 std::string values[1];
856 mp::xml::parse_attr(ptr, names, values);
858 phase.m_verbose = atoi(values[0].c_str());
860 std::map<std::string, RulePtr > rules;
861 for (ptr = ptr->children; ptr; ptr = ptr->next)
863 if (ptr->type != XML_ELEMENT_NODE)
865 else if (!strcmp((const char *) ptr->name, "rule"))
867 static const char *names[2] = { "name", 0 };
868 std::string values[1];
869 values[0] = "default";
870 mp::xml::parse_attr(ptr, names, values);
872 RulePtr rule(new Rule);
873 for (xmlNode *p = ptr->children; p; p = p->next)
875 if (p->type != XML_ELEMENT_NODE)
877 if (!strcmp((const char *) p->name, "rewrite"))
881 const struct _xmlAttr *attr;
882 for (attr = p->properties; attr; attr = attr->next)
884 if (!strcmp((const char *) attr->name, "from"))
885 from = mp::xml::get_text(attr->children);
886 else if (!strcmp((const char *) attr->name, "to"))
887 replace.recipe = mp::xml::get_text(attr->children);
889 throw mp::filter::FilterException
891 + std::string((const char *) attr->name)
892 + " in rewrite section of http_rewrite");
894 yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
895 from.c_str(), replace.recipe.c_str());
898 replace.parse_groups(from);
899 rule->replace_list.push_back(replace);
903 throw mp::filter::FilterException
905 + std::string((const char *) p->name)
906 + " in http_rewrite filter");
908 rules[values[0]] = rule;
910 else if (!strcmp((const char *) ptr->name, "content"))
912 static const char *names[3] =
913 { "type", "mime", 0 };
914 std::string values[2];
915 mp::xml::parse_attr(ptr, names, values);
916 if (values[0].empty())
918 throw mp::filter::FilterException
919 ("Missing attribute, type for for element "
920 + std::string((const char *) ptr->name)
921 + " in http_rewrite filter");
926 if (!values[1].empty())
927 c.content_re.assign(values[1], boost::regex::icase);
928 c.configure(ptr->children, rules);
929 phase.content_list.push_back(c);
933 throw mp::filter::FilterException
935 + std::string((const char *) ptr->name)
936 + " in http_rewrite filter");
941 void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
944 for (ptr = ptr->children; ptr; ptr = ptr->next)
946 if (ptr->type != XML_ELEMENT_NODE)
948 else if (!strcmp((const char *) ptr->name, "request"))
950 configure_phase(ptr, *req_phase);
952 else if (!strcmp((const char *) ptr->name, "response"))
954 configure_phase(ptr, *res_phase);
958 throw mp::filter::FilterException
960 + std::string((const char *) ptr->name)
961 + " in http_rewrite1 filter");
966 static mp::filter::Base* filter_creator()
968 return new mp::filter::HttpRewrite;
972 struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite = {
983 * c-file-style: "Stroustrup"
984 * indent-tabs-mode: nil
986 * vim: shiftwidth=4 tabstop=8 expandtab