Set vars in replacement also if empty
[metaproxy-moved-to-github.git] / src / filter_http_rewrite.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 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_rewrite.hpp"
24 #include "html_parser.hpp"
25
26 #include <yaz/zgdu.h>
27 #include <yaz/log.h>
28
29 #include <stack>
30 #include <boost/regex.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/algorithm/string.hpp>
33
34 #include <map>
35
36 namespace mp = metaproxy_1;
37 namespace yf = mp::filter;
38
39 namespace metaproxy_1 {
40     namespace filter {
41         class HttpRewrite::Replace {
42         public:
43             std::string regex;
44             std::string recipe;
45             std::map<int, std::string> group_index;
46             const std::string search_replace(
47                 std::map<std::string, std::string> & vars,
48                 const std::string & txt) const;
49             std::string sub_vars(
50                 const std::map<std::string, std::string> & vars) const;
51             void parse_groups();
52         };
53
54         class HttpRewrite::Rule {
55         public:
56             std::list<Replace> replace_list;
57             const std::string test_patterns(
58                 std::map<std::string, std::string> & vars,
59                 const std::string & txt) const;
60         };
61         class HttpRewrite::Within {
62         public:
63             std::string header;
64             std::string attr;
65             std::string tag;
66             bool reqline;
67             RulePtr rule;
68         };
69
70         class HttpRewrite::Phase {
71         public:
72             Phase();
73             std::list<Within> within_list;
74             int m_verbose;
75             void rewrite_reqline(mp::odr & o, Z_HTTP_Request *hreq,
76                 std::map<std::string, std::string> & vars) const;
77             void rewrite_headers(mp::odr & o, Z_HTTP_Header *headers,
78                 std::map<std::string, std::string> & vars) const;
79             void rewrite_body(mp::odr & o,
80                 char **content_buf, int *content_len,
81                 std::map<std::string, std::string> & vars) const;
82         };
83         class HttpRewrite::Event : public HTMLParserEvent {
84             void openTagStart(const char *tag, int tag_len);
85             void anyTagEnd(const char *tag, int tag_len, int close_it);
86             void attribute(const char *tag, int tag_len,
87                            const char *attr, int attr_len,
88                            const char *value, int val_len);
89             void closeTag(const char *tag, int tag_len);
90             void text(const char *value, int len);
91             const Phase *m_phase;
92             WRBUF m_w;
93             std::stack<std::list<Within>::const_iterator> s_within;
94             std::map<std::string, std::string> &m_vars;
95         public:
96             Event(const Phase *p, std::map<std::string, std::string> &vars);
97             ~Event();
98             const char *result();
99         };
100     }
101 }
102
103 yf::HttpRewrite::HttpRewrite() :
104     req_phase(new Phase), res_phase(new Phase)
105 {
106 }
107
108 yf::HttpRewrite::~HttpRewrite()
109 {
110 }
111
112 void yf::HttpRewrite::process(mp::Package & package) const
113 {
114     yaz_log(YLOG_LOG, "HttpRewrite begins....");
115     Z_GDU *gdu = package.request().get();
116     //map of request/response vars
117     std::map<std::string, std::string> vars;
118     //we have an http req
119     if (gdu && gdu->which == Z_GDU_HTTP_Request)
120     {
121         Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
122         mp::odr o;
123         req_phase->rewrite_reqline(o, hreq, vars);
124         yaz_log(YLOG_LOG, ">> Request headers");
125         req_phase->rewrite_headers(o, hreq->headers, vars);
126         req_phase->rewrite_body(o,
127                 &hreq->content_buf, &hreq->content_len, vars);
128         package.request() = gdu;
129     }
130     package.move();
131     gdu = package.response().get();
132     if (gdu && gdu->which == Z_GDU_HTTP_Response)
133     {
134         Z_HTTP_Response *hres = gdu->u.HTTP_Response;
135         yaz_log(YLOG_LOG, "Response code %d", hres->code);
136         mp::odr o;
137         yaz_log(YLOG_LOG, "<< Respose headers");
138         res_phase->rewrite_headers(o, hres->headers, vars);
139         res_phase->rewrite_body(o, &hres->content_buf,
140                 &hres->content_len, vars);
141         package.response() = gdu;
142     }
143 }
144
145 void yf::HttpRewrite::Phase::rewrite_reqline (mp::odr & o,
146         Z_HTTP_Request *hreq,
147         std::map<std::string, std::string> & vars) const
148 {
149     //rewrite the request line
150     std::string path;
151     if (strstr(hreq->path, "http://") == hreq->path)
152     {
153         yaz_log(YLOG_LOG, "Path in the method line is absolute, "
154             "possibly a proxy request");
155         path += hreq->path;
156     }
157     else
158     {
159         //TODO what about proto
160         path += "http://";
161         path += z_HTTP_header_lookup(hreq->headers, "Host");
162         path += hreq->path;
163     }
164
165
166     std::list<Within>::const_iterator it = within_list.begin();
167     for (; it != within_list.end(); it++)
168         if (it->reqline)
169         {
170             RulePtr rule = it->rule;
171             yaz_log(YLOG_LOG, "Proxy request URL is %s", path.c_str());
172             std::string npath = rule->test_patterns(vars, path);
173             if (!npath.empty())
174             {
175                 yaz_log(YLOG_LOG, "Rewritten request URL is %s", npath.c_str());
176                 hreq->path = odr_strdup(o, npath.c_str());
177             }
178         }
179 }
180
181 void yf::HttpRewrite::Phase::rewrite_headers(mp::odr & o,
182         Z_HTTP_Header *headers,
183         std::map<std::string, std::string> & vars) const
184 {
185     for (Z_HTTP_Header *header = headers; header; header = header->next)
186     {
187         std::list<Within>::const_iterator it = within_list.begin();
188         for (; it != within_list.end(); it++)
189         {
190             if (it->header.length() > 0 &&
191                 yaz_strcasecmp(it->header.c_str(), header->name) == 0)
192             {
193                 std::string sheader(header->name);
194                 sheader += ": ";
195                 sheader += header->value;
196
197                 RulePtr rule = it->rule;
198                 std::string out = rule->test_patterns(vars, sheader);
199                 if (!out.empty())
200                 {
201                     size_t pos = out.find(": ");
202                     if (pos == std::string::npos)
203                     {
204                         yaz_log(YLOG_LOG, "Header malformed during rewrite, ignoring");
205                         continue;
206                     }
207                     header->name = odr_strdup(o, out.substr(0, pos).c_str());
208                     header->value = odr_strdup(o,
209                                                out.substr(pos + 2,
210                                                           std::string::npos).c_str());
211                 }
212             }
213         }
214     }
215 }
216
217 void yf::HttpRewrite::Phase::rewrite_body(mp::odr & o,
218         char **content_buf,
219         int *content_len,
220         std::map<std::string, std::string> & vars) const
221 {
222     if (*content_buf)
223     {
224         int i;
225         for (i = 0; i < *content_len; i++)
226             if ((*content_buf)[i] == 0)
227                 return;  // binary content. skip
228
229         HTMLParser parser;
230         Event ev(this, vars);
231
232         parser.set_verbose(m_verbose);
233
234         std::string buf(*content_buf, *content_len);
235
236         parser.parse(ev, buf.c_str());
237         const char *res = ev.result();
238         *content_buf = odr_strdup(o, res);
239         *content_len = strlen(res);
240     }
241 }
242
243 yf::HttpRewrite::Event::Event(const Phase *p,
244                               std::map<std::string, std::string> & vars
245     ) : m_phase(p), m_vars(vars)
246 {
247     m_w = wrbuf_alloc();
248 }
249
250 yf::HttpRewrite::Event::~Event()
251 {
252     wrbuf_destroy(m_w);
253 }
254
255 const char *yf::HttpRewrite::Event::result()
256 {
257     return wrbuf_cstr(m_w);
258 }
259
260 void yf::HttpRewrite::Event::openTagStart(const char *tag, int tag_len)
261 {
262     wrbuf_putc(m_w, '<');
263     wrbuf_write(m_w, tag, tag_len);
264
265     std::string t(tag, tag_len);
266     std::list<Within>::const_iterator it = m_phase->within_list.begin();
267     for (; it != m_phase->within_list.end(); it++)
268     {
269         if (it->tag.length() > 0 && yaz_strcasecmp(it->tag.c_str(),
270                                                    t.c_str()) == 0)
271         {
272             std::vector<std::string> attr;
273             boost::split(attr, it->attr, boost::is_any_of(","));
274             size_t i;
275             for (i = 0; i < attr.size(); i++)
276             {
277                 if (attr[i].compare("#text") == 0)
278                 {
279                     s_within.push(it);
280                     return;
281                 }
282             }
283         }
284     }
285 }
286
287 void yf::HttpRewrite::Event::anyTagEnd(const char *tag, int tag_len,
288                                        int close_it)
289 {
290     if (close_it)
291     {
292         if (!s_within.empty())
293         {
294             std::list<Within>::const_iterator it = s_within.top();
295             std::string t(tag, tag_len);
296             if (yaz_strcasecmp(it->tag.c_str(), t.c_str()) == 0)
297                 s_within.pop();
298         }
299     }
300     if (close_it)
301         wrbuf_putc(m_w, '/');
302     wrbuf_putc(m_w, '>');
303 }
304
305 void yf::HttpRewrite::Event::attribute(const char *tag, int tag_len,
306                                        const char *attr, int attr_len,
307                                        const char *value, int val_len)
308 {
309     std::list<Within>::const_iterator it = m_phase->within_list.begin();
310     bool subst = false;
311
312     for (; it != m_phase->within_list.end(); it++)
313     {
314         std::string t(tag, tag_len);
315         if (it->tag.length() == 0 ||
316             yaz_strcasecmp(it->tag.c_str(), t.c_str()) == 0)
317         {
318             std::string a(attr, attr_len);
319             std::vector<std::string> attr;
320             boost::split(attr, it->attr, boost::is_any_of(","));
321             size_t i;
322             for (i = 0; i < attr.size(); i++)
323             {
324                 if (attr[i].compare("#text") &&
325                     yaz_strcasecmp(attr[i].c_str(), a.c_str()) == 0)
326                     subst = true;
327             }
328         }
329         if (subst)
330             break;
331     }
332
333     wrbuf_putc(m_w, ' ');
334     wrbuf_write(m_w, attr, attr_len);
335     wrbuf_puts(m_w, "=\"");
336
337     std::string output;
338     if (subst)
339     {
340         std::string input(value, val_len);
341         output = it->rule->test_patterns(m_vars, input);
342     }
343     if (output.empty())
344         wrbuf_write(m_w, value, val_len);
345     else
346         wrbuf_puts(m_w, output.c_str());
347     wrbuf_puts(m_w, "\"");
348 }
349
350 void yf::HttpRewrite::Event::closeTag(const char *tag, int tag_len)
351 {
352     if (!s_within.empty())
353     {
354         std::list<Within>::const_iterator it = s_within.top();
355         std::string t(tag, tag_len);
356         if (yaz_strcasecmp(it->tag.c_str(), t.c_str()) == 0)
357             s_within.pop();
358     }
359     wrbuf_puts(m_w, "</");
360     wrbuf_write(m_w, tag, tag_len);
361 }
362
363 void yf::HttpRewrite::Event::text(const char *value, int len)
364 {
365     std::list<Within>::const_iterator it = m_phase->within_list.end();
366     if (!s_within.empty())
367         it = s_within.top();
368     std::string output;
369     if (it != m_phase->within_list.end())
370     {
371         std::string input(value, len);
372         output = it->rule->test_patterns(m_vars, input);
373     }
374     if (output.empty())
375         wrbuf_write(m_w, value, len);
376     else
377         wrbuf_puts(m_w, output.c_str());
378 }
379
380
381 /**
382  * Tests pattern from the vector in order and executes recipe on
383  the first match.
384  */
385 const std::string yf::HttpRewrite::Rule::test_patterns(
386         std::map<std::string, std::string> & vars,
387         const std::string & txt) const
388 {
389     std::list<Replace>::const_iterator it = replace_list.begin();
390
391     for (; it != replace_list.end(); it++)
392     {
393         std::string out = it->search_replace(vars, txt);
394         if (!out.empty()) return out;
395     }
396     return "";
397 }
398
399 const std::string yf::HttpRewrite::Replace::search_replace(
400         std::map<std::string, std::string> & vars,
401         const std::string & txt) const
402 {
403     //exec regex against value
404     boost::regex re(regex);
405     boost::smatch what;
406     std::string::const_iterator start, end;
407     start = txt.begin();
408     end = txt.end();
409     std::string out;
410     while (regex_search(start, end, what, re)) //find next full match
411     {
412         size_t i;
413         for (i = 1; i < what.size(); ++i)
414         {
415             //check if the group is named
416             std::map<int, std::string>::const_iterator it
417                 = group_index.find(i);
418             if (it != group_index.end())
419             {   //it is
420                 vars[it->second] = what[i];
421             }
422
423         }
424         //prepare replacement string
425         std::string rvalue = sub_vars(vars);
426         yaz_log(YLOG_LOG, "! Rewritten '%s' to '%s'",
427                 what.str(0).c_str(), rvalue.c_str());
428         out.append(start, what[0].first);
429         out.append(rvalue);
430         start = what[0].second; //move search forward
431     }
432     //if we had a match cat the last part
433     if (start != txt.begin())
434         out.append(start, end);
435     return out;
436 }
437
438 void yf::HttpRewrite::Replace::parse_groups()
439 {
440     int gnum = 0;
441     bool esc = false;
442     const std::string & str = regex;
443     std::string res;
444     yaz_log(YLOG_LOG, "Parsing groups from '%s'", str.c_str());
445     for (size_t i = 0; i < str.size(); ++i)
446     {
447         res += str[i];
448         if (!esc && str[i] == '\\')
449         {
450             esc = true;
451             continue;
452         }
453         if (!esc && str[i] == '(') //group starts
454         {
455             gnum++;
456             if (i+1 < str.size() && str[i+1] == '?') //group with attrs
457             {
458                 i++;
459                 if (i+1 < str.size() && str[i+1] == ':') //non-capturing
460                 {
461                     if (gnum > 0) gnum--;
462                     res += str[i];
463                     i++;
464                     res += str[i];
465                     continue;
466                 }
467                 if (i+1 < str.size() && str[i+1] == 'P') //optional, python
468                     i++;
469                 if (i+1 < str.size() && str[i+1] == '<') //named
470                 {
471                     i++;
472                     std::string gname;
473                     bool term = false;
474                     while (++i < str.size())
475                     {
476                         if (str[i] == '>') { term = true; break; }
477                         if (!isalnum(str[i]))
478                             throw mp::filter::FilterException
479                                 ("Only alphanumeric chars allowed, found "
480                                  " in '"
481                                  + str
482                                  + "' at "
483                                  + boost::lexical_cast<std::string>(i));
484                         gname += str[i];
485                     }
486                     if (!term)
487                         throw mp::filter::FilterException
488                             ("Unterminated group name '" + gname
489                              + " in '" + str +"'");
490                     group_index[gnum] = gname;
491                     yaz_log(YLOG_LOG, "Found named group '%s' at $%d",
492                             gname.c_str(), gnum);
493                 }
494             }
495         }
496         esc = false;
497     }
498     regex = res;
499 }
500
501 std::string yf::HttpRewrite::Replace::sub_vars(
502     const std::map<std::string, std::string> & vars) const
503 {
504     std::string out;
505     bool esc = false;
506     const std::string & in = recipe;
507     for (size_t i = 0; i < in.size(); ++i)
508     {
509         if (!esc && in[i] == '\\')
510         {
511             esc = true;
512             continue;
513         }
514         if (!esc && in[i] == '$') //var
515         {
516             if (i+1 < in.size() && in[i+1] == '{') //ref prefix
517             {
518                 ++i;
519                 std::string name;
520                 bool term = false;
521                 while (++i < in.size())
522                 {
523                     if (in[i] == '}') { term = true; break; }
524                     name += in[i];
525                 }
526                 if (!term) throw mp::filter::FilterException
527                     ("Unterminated var ref in '"+in+"' at "
528                      + boost::lexical_cast<std::string>(i));
529                 std::map<std::string, std::string>::const_iterator it
530                     = vars.find(name);
531                 if (it != vars.end())
532                 {
533                     out += it->second;
534                 }
535             }
536             else
537             {
538                 throw mp::filter::FilterException
539                     ("Malformed or trimmed var ref in '"
540                      +in+"' at "+boost::lexical_cast<std::string>(i));
541             }
542             continue;
543         }
544         //passthru
545         out += in[i];
546         esc = false;
547     }
548     return out;
549 }
550
551 yf::HttpRewrite::Phase::Phase() : m_verbose(0)
552 {
553 }
554
555 void yf::HttpRewrite::configure_phase(const xmlNode *ptr, Phase &phase)
556 {
557     static const char *names[2] = { "verbose", 0 };
558     std::string values[1];
559     values[0] = "0";
560     mp::xml::parse_attr(ptr, names, values);
561
562     phase.m_verbose = atoi(values[0].c_str());
563
564     std::map<std::string, RulePtr > rules;
565     for (ptr = ptr->children; ptr; ptr = ptr->next)
566     {
567         if (ptr->type != XML_ELEMENT_NODE)
568             continue;
569         else if (!strcmp((const char *) ptr->name, "rule"))
570         {
571             static const char *names[2] = { "name", 0 };
572             std::string values[1];
573             values[0] = "default";
574             mp::xml::parse_attr(ptr, names, values);
575
576             RulePtr rule(new Rule);
577             for (xmlNode *p = ptr->children; p; p = p->next)
578             {
579                 if (p->type != XML_ELEMENT_NODE)
580                     continue;
581                 if (!strcmp((const char *) p->name, "rewrite"))
582                 {
583                     Replace replace;
584                     const struct _xmlAttr *attr;
585                     for (attr = p->properties; attr; attr = attr->next)
586                     {
587                         if (!strcmp((const char *) attr->name,  "from"))
588                             replace.regex = mp::xml::get_text(attr->children);
589                         else if (!strcmp((const char *) attr->name,  "to"))
590                             replace.recipe = mp::xml::get_text(attr->children);
591                         else
592                             throw mp::filter::FilterException
593                                 ("Bad attribute "
594                                  + std::string((const char *) attr->name)
595                                  + " in rewrite section of http_rewrite");
596                     }
597                     yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
598                             replace.regex.c_str(), replace.recipe.c_str());
599                     replace.parse_groups();
600                     if (!replace.regex.empty())
601                         rule->replace_list.push_back(replace);
602                 }
603                 else
604                     throw mp::filter::FilterException
605                         ("Bad element "
606                          + std::string((const char *) p->name)
607                          + " in http_rewrite filter");
608             }
609             rules[values[0]] = rule;
610         }
611         else if (!strcmp((const char *) ptr->name, "within"))
612         {
613             static const char *names[6] =
614                 { "header", "attr", "tag", "rule", "reqline", 0 };
615             std::string values[5];
616             mp::xml::parse_attr(ptr, names, values);
617             Within w;
618             w.header = values[0];
619             w.attr = values[1];
620             w.tag = values[2];
621             std::map<std::string,RulePtr>::const_iterator it =
622                 rules.find(values[3]);
623             if (it == rules.end())
624                 throw mp::filter::FilterException
625                     ("Reference to non-existing rule '" + values[3] +
626                      "' in http_rewrite filter");
627             w.rule = it->second;
628             w.reqline = values[4] == "1";
629             phase.within_list.push_back(w);
630         }
631         else
632         {
633             throw mp::filter::FilterException
634                 ("Bad element "
635                  + std::string((const char *) ptr->name)
636                  + " in http_rewrite filter");
637         }
638     }
639 }
640
641 void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
642         const char *path)
643 {
644     for (ptr = ptr->children; ptr; ptr = ptr->next)
645     {
646         if (ptr->type != XML_ELEMENT_NODE)
647             continue;
648         else if (!strcmp((const char *) ptr->name, "request"))
649         {
650             configure_phase(ptr, *req_phase);
651         }
652         else if (!strcmp((const char *) ptr->name, "response"))
653         {
654             configure_phase(ptr, *res_phase);
655         }
656         else
657         {
658             throw mp::filter::FilterException
659                 ("Bad element "
660                  + std::string((const char *) ptr->name)
661                  + " in http_rewrite1 filter");
662         }
663     }
664 }
665
666 static mp::filter::Base* filter_creator()
667 {
668     return new mp::filter::HttpRewrite;
669 }
670
671 extern "C" {
672     struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite = {
673         0,
674         "http_rewrite",
675         filter_creator
676     };
677 }
678
679
680 /*
681  * Local variables:
682  * c-basic-offset: 4
683  * c-file-style: "Stroustrup"
684  * indent-tabs-mode: nil
685  * End:
686  * vim: shiftwidth=4 tabstop=8 expandtab
687  */
688