cql_rpn: use path for cql2rpn file MP-481
[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             bool start_anchor;
44             boost::regex re;
45             boost::smatch what;
46             std::string recipe;
47             std::map<int, std::string> group_index;
48             std::string sub_vars(
49                 const std::map<std::string, std::string> & vars) const;
50             void parse_groups(std::string pattern);
51         };
52
53         class HttpRewrite::Rule {
54         public:
55             std::list<Replace> replace_list;
56             bool test_patterns(
57                 std::map<std::string, std::string> &vars,
58                 std::string &txt, bool anchor);
59         };
60         class HttpRewrite::Within {
61         public:
62             boost::regex header;
63             boost::regex attr;
64             boost::regex tag;
65             std::string type;
66             bool reqline;
67             RulePtr rule;
68             bool exec(std::map<std::string, std::string> &vars,
69                       std::string &txt, bool anchor) const;
70         };
71
72         class HttpRewrite::Content {
73         public:
74             std::string type;
75             boost::regex content_re;
76             std::list<Within> within_list;
77             void configure(const xmlNode *ptr,
78                            std::map<std::string, RulePtr > &rules);
79             void quoted_literal(std::string &content,
80                                 std::map<std::string, std::string> &vars) const;
81             void parse(int verbose, std::string &content,
82                        std::map<std::string, std::string> & vars) const;
83         };
84         class HttpRewrite::Phase {
85         public:
86             Phase();
87             int m_verbose;
88             std::list<Content> content_list;
89             void rewrite_reqline(mp::odr & o, Z_HTTP_Request *hreq,
90                 std::map<std::string, std::string> & vars) const;
91             void rewrite_headers(mp::odr & o, Z_HTTP_Header *headers,
92                 std::map<std::string, std::string> & vars) const;
93             void rewrite_body(mp::odr & o,
94                               const char *content_type,
95                               char **content_buf, int *content_len,
96                               std::map<std::string, std::string> & vars) const;
97         };
98         class HttpRewrite::Event : public HTMLParserEvent {
99             void openTagStart(const char *tag, int tag_len);
100             void anyTagEnd(const char *tag, int tag_len, int close_it);
101             void attribute(const char *tag, int tag_len,
102                            const char *attr, int attr_len,
103                            const char *value, int val_len,
104                            const char *sep);
105             void closeTag(const char *tag, int tag_len);
106             void text(const char *value, int len);
107             const Content *m_content;
108             WRBUF m_w;
109             std::stack<std::list<Within>::const_iterator> s_within;
110             std::map<std::string, std::string> &m_vars;
111         public:
112             Event(const Content *p, std::map<std::string, std::string> &vars);
113             ~Event();
114             const char *result();
115         };
116     }
117 }
118
119 yf::HttpRewrite::HttpRewrite() :
120     req_phase(new Phase), res_phase(new Phase)
121 {
122 }
123
124 yf::HttpRewrite::~HttpRewrite()
125 {
126 }
127
128 void yf::HttpRewrite::process(mp::Package & package) const
129 {
130     yaz_log(YLOG_LOG, "HttpRewrite begins....");
131     Z_GDU *gdu = package.request().get();
132     //map of request/response vars
133     std::map<std::string, std::string> vars;
134     //we have an http req
135     if (gdu && gdu->which == Z_GDU_HTTP_Request)
136     {
137         Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
138         mp::odr o;
139         req_phase->rewrite_reqline(o, hreq, vars);
140         yaz_log(YLOG_LOG, ">> Request headers");
141         req_phase->rewrite_headers(o, hreq->headers, vars);
142         req_phase->rewrite_body(o,
143                                 z_HTTP_header_lookup(hreq->headers,
144                                                      "Content-Type"),
145                                 &hreq->content_buf, &hreq->content_len,
146                                 vars);
147         package.request() = gdu;
148     }
149     package.move();
150     gdu = package.response().get();
151     if (gdu && gdu->which == Z_GDU_HTTP_Response)
152     {
153         Z_HTTP_Response *hres = gdu->u.HTTP_Response;
154         yaz_log(YLOG_LOG, "Response code %d", hres->code);
155         mp::odr o;
156         yaz_log(YLOG_LOG, "<< Respose headers");
157         res_phase->rewrite_headers(o, hres->headers, vars);
158         res_phase->rewrite_body(o,
159                                 z_HTTP_header_lookup(hres->headers,
160                                                      "Content-Type"),
161                                 &hres->content_buf, &hres->content_len,
162                                 vars);
163         package.response() = gdu;
164     }
165 }
166
167 void yf::HttpRewrite::Phase::rewrite_reqline (mp::odr & o,
168         Z_HTTP_Request *hreq,
169         std::map<std::string, std::string> & vars) const
170 {
171     //rewrite the request line
172     std::string path;
173     if (strstr(hreq->path, "http://") == hreq->path)
174     {
175         yaz_log(YLOG_LOG, "Path in the method line is absolute, "
176             "possibly a proxy request");
177         path += hreq->path;
178     }
179     else
180     {
181         //TODO what about proto
182         const char *host = z_HTTP_header_lookup(hreq->headers, "Host");
183         if (!host)
184             return;
185
186         path += "http://";
187         path += host;
188         path += hreq->path;
189     }
190
191     std::list<Content>::const_iterator cit = content_list.begin();
192     for (; cit != content_list.end(); cit++)
193         if (cit->type == "headers")
194             break;
195
196     if (cit == content_list.end())
197         return;
198
199     std::list<Within>::const_iterator it = cit->within_list.begin();
200     for (; it != cit->within_list.end(); it++)
201         if (it->reqline)
202         {
203             yaz_log(YLOG_LOG, "Proxy request URL is %s", path.c_str());
204             if (it->exec(vars, path, true))
205             {
206                 yaz_log(YLOG_LOG, "Rewritten request URL is %s", path.c_str());
207                 hreq->path = odr_strdup(o, path.c_str());
208             }
209         }
210 }
211
212 void yf::HttpRewrite::Phase::rewrite_headers(mp::odr & o,
213         Z_HTTP_Header *headers,
214         std::map<std::string, std::string> & vars) const
215 {
216     std::list<Content>::const_iterator cit = content_list.begin();
217     for (; cit != content_list.end(); cit++)
218         if (cit->type == "headers")
219             break;
220
221     if (cit == content_list.end())
222         return;
223
224     for (Z_HTTP_Header *header = headers; header; header = header->next)
225     {
226         std::list<Within>::const_iterator it = cit->within_list.begin();
227         for (; it != cit->within_list.end(); it++)
228         {
229             if (!it->header.empty() &&
230                 regex_match(header->name, it->header))
231             {
232 #ifdef OLDHEADERMATCH                
233                 // Matches and replaces the whole header line.
234                 // This is good if you want to play with the header name too,
235                 // but useless for patterns that want to anchor to the beginning
236                 // or end of the header value, as we want to do with host-relative
237                 // links. This code should probably be removed.
238                 std::string sheader(header->name);
239                 sheader += ": ";
240                 sheader += header->value;
241
242                 if (it->exec(vars, sheader, true))
243                 {
244                     size_t pos = sheader.find(": ");
245                     if (pos == std::string::npos)
246                     {
247                         yaz_log(YLOG_LOG, "Header malformed during rewrite, ignoring");
248                         continue;
249                     }
250                     header->name = odr_strdup(
251                         o, sheader.substr(0, pos).c_str());
252                     header->value = odr_strdup(
253                         o, sheader.substr(pos + 2, std::string::npos).c_str());
254                 }
255 #else
256                 // Match and replace only the header value
257                 std::string hval(header->value);
258                 if (it->exec(vars, hval, true))
259                 {
260                     header->value = odr_strdup(o, hval.c_str());
261                 }
262                     
263 #endif
264             }
265         }
266     }
267 }
268
269 void yf::HttpRewrite::Phase::rewrite_body(
270     mp::odr &o,
271     const char *content_type,
272     char **content_buf,
273     int *content_len,
274     std::map<std::string, std::string> & vars) const
275 {
276     if (*content_len == 0)
277         return;
278     if (!content_type) {
279         yaz_log(YLOG_LOG, "rewrite_body: null content_type, can not rewrite");
280         return;
281     }
282     std::list<Content>::const_iterator cit = content_list.begin();
283     for (; cit != content_list.end(); cit++)
284     {
285         yaz_log(YLOG_LOG, "rewrite_body: content_type=%s type=%s",
286                 content_type, cit->type.c_str());
287         if (cit->type != "headers"
288             && regex_match(content_type, cit->content_re))
289             break;
290     }
291     if (cit == content_list.end()) {
292         yaz_log(YLOG_LOG,"rewrite_body: No content rule matched %s, not rewriting",
293                 content_type );  
294         return;
295     }
296
297     int i;
298     for (i = 0; i < *content_len; i++)
299         if ((*content_buf)[i] == 0) {
300             yaz_log(YLOG_LOG,"rewrite_body: Looks like binary stuff, not rewriting");
301             return;  // binary content. skip
302         }
303
304     std::string content(*content_buf, *content_len);
305     cit->parse(m_verbose, content, vars);
306     *content_buf = odr_strdup(o, content.c_str());
307     *content_len = strlen(*content_buf);
308 }
309
310 yf::HttpRewrite::Event::Event(const Content *p,
311                               std::map<std::string, std::string> & vars
312     ) : m_content(p), m_vars(vars)
313 {
314     m_w = wrbuf_alloc();
315 }
316
317 yf::HttpRewrite::Event::~Event()
318 {
319     wrbuf_destroy(m_w);
320 }
321
322 const char *yf::HttpRewrite::Event::result()
323 {
324     return wrbuf_cstr(m_w);
325 }
326
327 void yf::HttpRewrite::Event::openTagStart(const char *tag, int tag_len)
328 {
329     wrbuf_putc(m_w, '<');
330     wrbuf_write(m_w, tag, tag_len);
331
332     std::string t(tag, tag_len);
333     std::list<Within>::const_iterator it = m_content->within_list.begin();
334     for (; it != m_content->within_list.end(); it++)
335     {
336         if (!it->tag.empty() && regex_match(t, it->tag))
337         {
338             if (!it->attr.empty() && regex_match("#text", it->attr))
339             {
340                 s_within.push(it);
341                 return;
342             }
343         }
344     }
345 }
346
347 void yf::HttpRewrite::Event::anyTagEnd(const char *tag, int tag_len,
348                                        int close_it)
349 {
350     if (close_it)
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 (regex_match(t, it->tag))
357                 s_within.pop();
358         }
359     }
360     if (close_it)
361         wrbuf_putc(m_w, '/');
362     wrbuf_putc(m_w, '>');
363 }
364
365 void yf::HttpRewrite::Event::attribute(const char *tag, int tag_len,
366                                        const char *attr, int attr_len,
367                                        const char *value, int val_len,
368                                        const char *sep)
369 {
370     std::list<Within>::const_iterator it = m_content->within_list.begin();
371     bool subst = false;
372
373     for (; it != m_content->within_list.end(); it++)
374     {
375         std::string t(tag, tag_len);
376         if (it->tag.empty() || regex_match(t, it->tag))
377         {
378             std::string a(attr, attr_len);
379             if (!it->attr.empty() && regex_match(a, it->attr))
380                 subst = true;
381         }
382         if (subst)
383             break;
384     }
385
386     wrbuf_putc(m_w, ' ');
387     wrbuf_write(m_w, attr, attr_len);
388     if (value)
389     {
390         wrbuf_puts(m_w, "=");
391         wrbuf_puts(m_w, sep);
392
393         std::string output;
394         if (subst)
395         {
396             std::string s(value, val_len);
397             it->exec(m_vars, s, true);
398             wrbuf_puts(m_w, s.c_str());
399         }
400         else
401             wrbuf_write(m_w, value, val_len);
402         wrbuf_puts(m_w, sep);
403     }
404 }
405
406 void yf::HttpRewrite::Event::closeTag(const char *tag, int tag_len)
407 {
408     if (!s_within.empty())
409     {
410         std::list<Within>::const_iterator it = s_within.top();
411         std::string t(tag, tag_len);
412         if (regex_match(t, it->tag))
413             s_within.pop();
414     }
415     wrbuf_puts(m_w, "</");
416     wrbuf_write(m_w, tag, tag_len);
417 }
418
419 void yf::HttpRewrite::Event::text(const char *value, int len)
420 {
421     std::list<Within>::const_iterator it = m_content->within_list.end();
422     if (!s_within.empty())
423         it = s_within.top();
424     if (it != m_content->within_list.end())
425     {
426         std::string s(value, len);
427         it->exec(m_vars, s, false);
428         wrbuf_puts(m_w, s.c_str());
429     }
430     else
431         wrbuf_write(m_w, value, len);
432 }
433
434 static bool embed_quoted_literal(
435     std::string &content,
436     std::map<std::string, std::string> &vars,
437     mp::filter::HttpRewrite::RulePtr ruleptr,
438     bool html_context)
439 {
440     bool replace = false;
441     std::string res;
442     const char *cp = content.c_str();
443     const char *cp0 = cp;
444     while (*cp)
445     {
446         if (html_context && !strncmp(cp, "&quot;", 6))
447         {
448             cp += 6;
449             res.append(cp0, cp - cp0);
450             cp0 = cp;
451             while (*cp)
452             {
453                 if (!strncmp(cp, "&quot;", 6))
454                     break;
455                 if (*cp == '\n')
456                     break;
457                 cp++;
458             }
459             if (!*cp)
460                 break;
461             std::string s(cp0, cp - cp0);
462             if (ruleptr->test_patterns(vars, s, true))
463                 replace = true;
464             cp0 = cp;
465             res.append(s);
466         }
467         else if (*cp == '"' || *cp == '\'')
468         {
469             int m = *cp;
470             cp++;
471             res.append(cp0, cp - cp0);
472             cp0 = cp;
473             while (*cp)
474             {
475                 if (cp[-1] != '\\' && *cp == m)
476                     break;
477                 if (*cp == '\n')
478                     break;
479                 cp++;
480             }
481             if (!*cp)
482                 break;
483             std::string s(cp0, cp - cp0);
484             if (ruleptr->test_patterns(vars, s, true))
485                 replace = true;
486             cp0 = cp;
487             res.append(s);
488         }
489         else if (*cp == '/' && cp[1] == '/')
490         {
491             while (cp[1] && cp[1] != '\n')
492                 cp++;
493         }
494         cp++;
495     }
496     res.append(cp0, cp - cp0);
497     content = res;
498     return replace;
499 }
500
501 bool yf::HttpRewrite::Within::exec(
502     std::map<std::string, std::string> & vars,
503     std::string & txt, bool anchor) const
504 {
505     if (type == "quoted-literal")
506     {
507         return embed_quoted_literal(txt, vars, rule, true);
508     }
509     else
510     {
511         return rule->test_patterns(vars, txt, anchor);
512     }
513 }
514
515 bool yf::HttpRewrite::Rule::test_patterns(
516     std::map<std::string, std::string> & vars,
517     std::string & txt, bool anchor)
518 {
519     bool replaces = false;
520     bool first = anchor;
521     std::string out;
522     std::string::const_iterator start, end;
523     start = txt.begin();
524     end = txt.end();
525     while (1)
526     {
527         std::list<Replace>::iterator bit = replace_list.end();
528         {
529             std::string::const_iterator best_pos = txt.end();
530             std::list<Replace>::iterator it = replace_list.begin();
531             for (; it != replace_list.end(); it++)
532             {
533                 if (it->start_anchor && !first)
534                     continue;
535                 if (regex_search(start, end, it->what, it->re))
536                 {
537                     if (it->what[0].first < best_pos)
538                     {
539                         best_pos = it->what[0].first;
540                         bit = it;
541                     }
542                 }
543             }
544             if (bit == replace_list.end())
545                 break;
546         }
547         first = false;
548         replaces = true;
549         size_t i;
550         for (i = 1; i < bit->what.size(); ++i)
551         {
552             //check if the group is named
553             std::map<int, std::string>::const_iterator git
554                 = bit->group_index.find(i);
555             if (git != bit->group_index.end())
556             {   //it is
557                 vars[git->second] = bit->what[i];
558             }
559
560         }
561         //prepare replacement string
562         std::string rvalue = bit->sub_vars(vars);
563         yaz_log(YLOG_LOG, "! Rewritten '%s' to '%s'",
564                 bit->what.str(0).c_str(), rvalue.c_str());
565         out.append(start, bit->what[0].first);
566         out.append(rvalue);
567         start = bit->what[0].second; //move search forward
568     }
569     out.append(start, end);
570     txt = out;
571     return replaces;
572 }
573
574 void yf::HttpRewrite::Replace::parse_groups(std::string pattern)
575 {
576     int gnum = 0;
577     bool esc = false;
578     const std::string &str = pattern;
579     std::string res;
580     start_anchor = str[0] == '^';
581     yaz_log(YLOG_LOG, "Parsing groups from '%s'", str.c_str());
582     for (size_t i = 0; i < str.size(); ++i)
583     {
584         res += str[i];
585         if (!esc && str[i] == '\\')
586         {
587             esc = true;
588             continue;
589         }
590         if (!esc && str[i] == '(') //group starts
591         {
592             gnum++;
593             if (i+1 < str.size() && str[i+1] == '?') //group with attrs
594             {
595                 i++;
596                 if (i+1 < str.size() && str[i+1] == ':') //non-capturing
597                 {
598                     if (gnum > 0) gnum--;
599                     res += str[i];
600                     i++;
601                     res += str[i];
602                     continue;
603                 }
604                 if (i+1 < str.size() && str[i+1] == 'P') //optional, python
605                     i++;
606                 if (i+1 < str.size() && str[i+1] == '<') //named
607                 {
608                     i++;
609                     std::string gname;
610                     bool term = false;
611                     while (++i < str.size())
612                     {
613                         if (str[i] == '>') { term = true; break; }
614                         if (!isalnum(str[i]))
615                             throw mp::filter::FilterException
616                                 ("Only alphanumeric chars allowed, found "
617                                  " in '"
618                                  + str
619                                  + "' at "
620                                  + boost::lexical_cast<std::string>(i));
621                         gname += str[i];
622                     }
623                     if (!term)
624                         throw mp::filter::FilterException
625                             ("Unterminated group name '" + gname
626                              + " in '" + str +"'");
627                     group_index[gnum] = gname;
628                     yaz_log(YLOG_LOG, "Found named group '%s' at $%d",
629                             gname.c_str(), gnum);
630                 }
631             }
632         }
633         esc = false;
634     }
635     re = res;
636 }
637
638 std::string yf::HttpRewrite::Replace::sub_vars(
639     const std::map<std::string, std::string> & vars) const
640 {
641     std::string out;
642     bool esc = false;
643     const std::string & in = recipe;
644     for (size_t i = 0; i < in.size(); ++i)
645     {
646         if (!esc && in[i] == '\\')
647         {
648             esc = true;
649             continue;
650         }
651         if (!esc && in[i] == '$') //var
652         {
653             if (i+1 < in.size() && in[i+1] == '{') //ref prefix
654             {
655                 ++i;
656                 std::string name;
657                 bool term = false;
658                 while (++i < in.size())
659                 {
660                     if (in[i] == '}') { term = true; break; }
661                     name += in[i];
662                 }
663                 if (!term) throw mp::filter::FilterException
664                     ("Unterminated var ref in '"+in+"' at "
665                      + boost::lexical_cast<std::string>(i));
666                 std::map<std::string, std::string>::const_iterator it
667                     = vars.find(name);
668                 if (it != vars.end())
669                 {
670                     out += it->second;
671                 }
672             }
673             else
674             {
675                 throw mp::filter::FilterException
676                     ("Malformed or trimmed var ref in '"
677                      +in+"' at "+boost::lexical_cast<std::string>(i));
678             }
679             continue;
680         }
681         //passthru
682         out += in[i];
683         esc = false;
684     }
685     return out;
686 }
687
688 yf::HttpRewrite::Phase::Phase() : m_verbose(0)
689 {
690 }
691
692 void yf::HttpRewrite::Content::parse(
693     int verbose,
694     std::string &content,
695     std::map<std::string, std::string> &vars) const
696 {
697     if (type == "html")
698     {
699         HTMLParser parser;
700         Event ev(this, vars);
701
702         parser.set_verbose(verbose);
703
704         parser.parse(ev, content.c_str());
705         content = ev.result();
706     }
707     if (type == "quoted-literal")
708     {
709         quoted_literal(content, vars);
710     }
711 }
712
713 void yf::HttpRewrite::Content::quoted_literal(
714     std::string &content,
715     std::map<std::string, std::string> &vars) const
716 {
717     std::list<Within>::const_iterator it = within_list.begin();
718     if (it != within_list.end())
719         embed_quoted_literal(content, vars, it->rule, false);
720 }
721
722 void yf::HttpRewrite::Content::configure(
723     const xmlNode *ptr, std::map<std::string, RulePtr > &rules)
724 {
725     for (; ptr; ptr = ptr->next)
726     {
727         if (ptr->type != XML_ELEMENT_NODE)
728             continue;
729         if (!strcmp((const char *) ptr->name, "within"))
730         {
731             static const char *names[7] =
732                 { "header", "attr", "tag", "rule", "reqline", "type", 0 };
733             std::string values[6];
734             mp::xml::parse_attr(ptr, names, values);
735             Within w;
736             if (values[0].length() > 0)
737                 w.header.assign(values[0], boost::regex_constants::icase);
738             if (values[1].length() > 0)
739                 w.attr.assign(values[1], boost::regex_constants::icase);
740             if (values[2].length() > 0)
741                 w.tag.assign(values[2], boost::regex_constants::icase);
742
743             std::vector<std::string> rulenames;
744             boost::split(rulenames, values[3], boost::is_any_of(","));
745             if (rulenames.size() == 0)
746             {
747                 throw mp::filter::FilterException
748                     ("Empty rule in '" + values[3] +
749                      "' in http_rewrite filter");
750             }
751             else if (rulenames.size() == 1)
752             {
753                 std::map<std::string,RulePtr>::const_iterator it =
754                     rules.find(rulenames[0]);
755                 if (it == rules.end())
756                     throw mp::filter::FilterException
757                         ("Reference to non-existing rule '" + rulenames[0] +
758                          "' in http_rewrite filter");
759                 w.rule = it->second;
760
761             }
762             else
763             {
764                 RulePtr rule(new Rule);
765                 size_t i;
766                 for (i = 0; i < rulenames.size(); i++)
767                 {
768                     std::map<std::string,RulePtr>::const_iterator it =
769                         rules.find(rulenames[i]);
770                     if (it == rules.end())
771                         throw mp::filter::FilterException
772                             ("Reference to non-existing rule '" + rulenames[i] +
773                              "' in http_rewrite filter");
774                     RulePtr subRule = it->second;
775                     std::list<Replace>::iterator rit =
776                         subRule->replace_list.begin();
777                     for (; rit != subRule->replace_list.end(); rit++)
778                         rule->replace_list.push_back(*rit);
779                 }
780                 w.rule = rule;
781             }
782             w.reqline = values[4] == "1";
783             w.type = values[5];
784             if (w.type.empty() || w.type == "quoted-literal")
785                 ;
786             else
787                 throw mp::filter::FilterException
788                     ("within type must be quoted-literal or none in "
789                      " in http_rewrite filter");
790             within_list.push_back(w);
791         }
792     }
793 }
794
795 void yf::HttpRewrite::configure_phase(const xmlNode *ptr, Phase &phase)
796 {
797     static const char *names[2] = { "verbose", 0 };
798     std::string values[1];
799     values[0] = "0";
800     mp::xml::parse_attr(ptr, names, values);
801
802     phase.m_verbose = atoi(values[0].c_str());
803
804     std::map<std::string, RulePtr > rules;
805     for (ptr = ptr->children; ptr; ptr = ptr->next)
806     {
807         if (ptr->type != XML_ELEMENT_NODE)
808             continue;
809         else if (!strcmp((const char *) ptr->name, "rule"))
810         {
811             static const char *names[2] = { "name", 0 };
812             std::string values[1];
813             values[0] = "default";
814             mp::xml::parse_attr(ptr, names, values);
815
816             RulePtr rule(new Rule);
817             for (xmlNode *p = ptr->children; p; p = p->next)
818             {
819                 if (p->type != XML_ELEMENT_NODE)
820                     continue;
821                 if (!strcmp((const char *) p->name, "rewrite"))
822                 {
823                     Replace replace;
824                     std::string from;
825                     const struct _xmlAttr *attr;
826                     for (attr = p->properties; attr; attr = attr->next)
827                     {
828                         if (!strcmp((const char *) attr->name,  "from"))
829                             from = mp::xml::get_text(attr->children);
830                         else if (!strcmp((const char *) attr->name,  "to"))
831                             replace.recipe = mp::xml::get_text(attr->children);
832                         else
833                             throw mp::filter::FilterException
834                                 ("Bad attribute "
835                                  + std::string((const char *) attr->name)
836                                  + " in rewrite section of http_rewrite");
837                     }
838                     yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
839                             from.c_str(), replace.recipe.c_str());
840                     if (!from.empty())
841                     {
842                         replace.parse_groups(from);
843                         rule->replace_list.push_back(replace);
844                     }
845                 }
846                 else
847                     throw mp::filter::FilterException
848                         ("Bad element "
849                          + std::string((const char *) p->name)
850                          + " in http_rewrite filter");
851             }
852             rules[values[0]] = rule;
853         }
854         else if (!strcmp((const char *) ptr->name, "content"))
855         {
856             static const char *names[3] =
857                 { "type", "mime", 0 };
858             std::string values[2];
859             mp::xml::parse_attr(ptr, names, values);
860             if (values[0].empty())
861             {
862                     throw mp::filter::FilterException
863                         ("Missing attribute, type for for element "
864                          + std::string((const char *) ptr->name)
865                          + " in http_rewrite filter");
866             }
867             Content c;
868
869             c.type = values[0];
870             if (!values[1].empty())
871                 c.content_re.assign(values[1], boost::regex::icase);
872             c.configure(ptr->children, rules);
873             phase.content_list.push_back(c);
874         }
875         else
876         {
877             throw mp::filter::FilterException
878                 ("Bad element "
879                  + std::string((const char *) ptr->name)
880                  + " in http_rewrite filter");
881         }
882     }
883 }
884
885 void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
886         const char *path)
887 {
888     for (ptr = ptr->children; ptr; ptr = ptr->next)
889     {
890         if (ptr->type != XML_ELEMENT_NODE)
891             continue;
892         else if (!strcmp((const char *) ptr->name, "request"))
893         {
894             configure_phase(ptr, *req_phase);
895         }
896         else if (!strcmp((const char *) ptr->name, "response"))
897         {
898             configure_phase(ptr, *res_phase);
899         }
900         else
901         {
902             throw mp::filter::FilterException
903                 ("Bad element "
904                  + std::string((const char *) ptr->name)
905                  + " in http_rewrite1 filter");
906         }
907     }
908 }
909
910 static mp::filter::Base* filter_creator()
911 {
912     return new mp::filter::HttpRewrite;
913 }
914
915 extern "C" {
916     struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite = {
917         0,
918         "http_rewrite",
919         filter_creator
920     };
921 }
922
923
924 /*
925  * Local variables:
926  * c-basic-offset: 4
927  * c-file-style: "Stroustrup"
928  * indent-tabs-mode: nil
929  * End:
930  * vim: shiftwidth=4 tabstop=8 expandtab
931  */
932