Using regex for comparing tags
[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             std::string header;
63             std::string 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.length() > 0 &&
230                 yaz_strcasecmp(it->header.c_str(), header->name) == 0)
231             {
232                 std::string sheader(header->name);
233                 sheader += ": ";
234                 sheader += header->value;
235
236                 if (it->exec(vars, sheader, true))
237                 {
238                     size_t pos = sheader.find(": ");
239                     if (pos == std::string::npos)
240                     {
241                         yaz_log(YLOG_LOG, "Header malformed during rewrite, ignoring");
242                         continue;
243                     }
244                     header->name = odr_strdup(
245                         o, sheader.substr(0, pos).c_str());
246                     header->value = odr_strdup(
247                         o, sheader.substr(pos + 2, std::string::npos).c_str());
248                 }
249             }
250         }
251     }
252 }
253
254 void yf::HttpRewrite::Phase::rewrite_body(
255     mp::odr &o,
256     const char *content_type,
257     char **content_buf,
258     int *content_len,
259     std::map<std::string, std::string> & vars) const
260 {
261     if (*content_len == 0)
262         return;
263     std::list<Content>::const_iterator cit = content_list.begin();
264     for (; cit != content_list.end(); cit++)
265     {
266         yaz_log(YLOG_LOG, "rewrite_body: content_type=%s type=%s",
267                 content_type, cit->type.c_str());
268         if (cit->type != "headers"
269             && regex_match(content_type, cit->content_re))
270             break;
271     }
272     if (cit == content_list.end())
273         return;
274
275     int i;
276     for (i = 0; i < *content_len; i++)
277         if ((*content_buf)[i] == 0)
278             return;  // binary content. skip
279
280     std::string content(*content_buf, *content_len);
281     cit->parse(m_verbose, content, vars);
282     *content_buf = odr_strdup(o, content.c_str());
283     *content_len = strlen(*content_buf);
284 }
285
286 yf::HttpRewrite::Event::Event(const Content *p,
287                               std::map<std::string, std::string> & vars
288     ) : m_content(p), m_vars(vars)
289 {
290     m_w = wrbuf_alloc();
291 }
292
293 yf::HttpRewrite::Event::~Event()
294 {
295     wrbuf_destroy(m_w);
296 }
297
298 const char *yf::HttpRewrite::Event::result()
299 {
300     return wrbuf_cstr(m_w);
301 }
302
303 void yf::HttpRewrite::Event::openTagStart(const char *tag, int tag_len)
304 {
305     wrbuf_putc(m_w, '<');
306     wrbuf_write(m_w, tag, tag_len);
307
308     std::string t(tag, tag_len);
309     std::list<Within>::const_iterator it = m_content->within_list.begin();
310     for (; it != m_content->within_list.end(); it++)
311     {
312         if (!it->tag.empty() && regex_match(t, it->tag))
313         {
314             std::vector<std::string> attr;
315             boost::split(attr, it->attr, boost::is_any_of(","));
316             size_t i;
317             for (i = 0; i < attr.size(); i++)
318             {
319                 if (attr[i].compare("#text") == 0)
320                 {
321                     s_within.push(it);
322                     return;
323                 }
324             }
325         }
326     }
327 }
328
329 void yf::HttpRewrite::Event::anyTagEnd(const char *tag, int tag_len,
330                                        int close_it)
331 {
332     if (close_it)
333     {
334         if (!s_within.empty())
335         {
336             std::list<Within>::const_iterator it = s_within.top();
337             std::string t(tag, tag_len);
338             if (regex_match(t, it->tag))
339                 s_within.pop();
340         }
341     }
342     if (close_it)
343         wrbuf_putc(m_w, '/');
344     wrbuf_putc(m_w, '>');
345 }
346
347 void yf::HttpRewrite::Event::attribute(const char *tag, int tag_len,
348                                        const char *attr, int attr_len,
349                                        const char *value, int val_len,
350                                        const char *sep)
351 {
352     std::list<Within>::const_iterator it = m_content->within_list.begin();
353     bool subst = false;
354
355     for (; it != m_content->within_list.end(); it++)
356     {
357         std::string t(tag, tag_len);
358         if (it->tag.empty() || regex_match(t, it->tag))
359         {
360             std::string a(attr, attr_len);
361             std::vector<std::string> attr;
362             boost::split(attr, it->attr, boost::is_any_of(","));
363             size_t i;
364             for (i = 0; i < attr.size(); i++)
365             {
366                 if (attr[i].compare("#text") &&
367                     yaz_strcasecmp(attr[i].c_str(), a.c_str()) == 0)
368                     subst = true;
369             }
370         }
371         if (subst)
372             break;
373     }
374
375     wrbuf_putc(m_w, ' ');
376     wrbuf_write(m_w, attr, attr_len);
377     if (value)
378     {
379         wrbuf_puts(m_w, "=");
380         wrbuf_puts(m_w, sep);
381
382         std::string output;
383         if (subst)
384         {
385             std::string s(value, val_len);
386             it->exec(m_vars, s, true);
387             wrbuf_puts(m_w, s.c_str());
388         }
389         else
390             wrbuf_write(m_w, value, val_len);
391         wrbuf_puts(m_w, sep);
392     }
393 }
394
395 void yf::HttpRewrite::Event::closeTag(const char *tag, int tag_len)
396 {
397     if (!s_within.empty())
398     {
399         std::list<Within>::const_iterator it = s_within.top();
400         std::string t(tag, tag_len);
401         if (regex_match(t, it->tag))
402             s_within.pop();
403     }
404     wrbuf_puts(m_w, "</");
405     wrbuf_write(m_w, tag, tag_len);
406 }
407
408 void yf::HttpRewrite::Event::text(const char *value, int len)
409 {
410     std::list<Within>::const_iterator it = m_content->within_list.end();
411     if (!s_within.empty())
412         it = s_within.top();
413     if (it != m_content->within_list.end())
414     {
415         std::string s(value, len);
416         it->exec(m_vars, s, false);
417         wrbuf_puts(m_w, s.c_str());
418     }
419     else
420         wrbuf_write(m_w, value, len);
421 }
422
423 static bool embed_quoted_literal(
424     std::string &content,
425     std::map<std::string, std::string> &vars,
426     mp::filter::HttpRewrite::RulePtr ruleptr,
427     bool html_context)
428 {
429     bool replace = false;
430     std::string res;
431     const char *cp = content.c_str();
432     const char *cp0 = cp;
433     while (*cp)
434     {
435         if (html_context && !strncmp(cp, "&quot;", 6))
436         {
437             cp += 6;
438             res.append(cp0, cp - cp0);
439             cp0 = cp;
440             while (*cp)
441             {
442                 if (!strncmp(cp, "&quot;", 6))
443                     break;
444                 if (*cp == '\n')
445                     break;
446                 cp++;
447             }
448             if (!*cp)
449                 break;
450             std::string s(cp0, cp - cp0);
451             if (ruleptr->test_patterns(vars, s, true))
452                 replace = true;
453             cp0 = cp;
454             res.append(s);
455         }
456         else if (*cp == '"' || *cp == '\'')
457         {
458             int m = *cp;
459             cp++;
460             res.append(cp0, cp - cp0);
461             cp0 = cp;
462             while (*cp)
463             {
464                 if (cp[-1] != '\\' && *cp == m)
465                     break;
466                 if (*cp == '\n')
467                     break;
468                 cp++;
469             }
470             if (!*cp)
471                 break;
472             std::string s(cp0, cp - cp0);
473             if (ruleptr->test_patterns(vars, s, true))
474                 replace = true;
475             cp0 = cp;
476             res.append(s);
477         }
478         else if (*cp == '/' && cp[1] == '/')
479         {
480             while (cp[1] && cp[1] != '\n')
481                 cp++;
482         }
483         cp++;
484     }
485     res.append(cp0, cp - cp0);
486     content = res;
487     return replace;
488 }
489
490 bool yf::HttpRewrite::Within::exec(
491     std::map<std::string, std::string> & vars,
492     std::string & txt, bool anchor) const
493 {
494     if (type == "quoted-literal")
495     {
496         return embed_quoted_literal(txt, vars, rule, true);
497     }
498     else
499     {
500         return rule->test_patterns(vars, txt, anchor);
501     }
502 }
503
504 bool yf::HttpRewrite::Rule::test_patterns(
505     std::map<std::string, std::string> & vars,
506     std::string & txt, bool anchor)
507 {
508     bool replaces = false;
509     bool first = anchor;
510     std::string out;
511     std::string::const_iterator start, end;
512     start = txt.begin();
513     end = txt.end();
514     while (1)
515     {
516         std::list<Replace>::iterator bit = replace_list.end();
517         {
518             std::string::const_iterator best_pos = txt.end();
519             std::list<Replace>::iterator it = replace_list.begin();
520             for (; it != replace_list.end(); it++)
521             {
522                 if (it->start_anchor && !first)
523                     continue;
524                 if (regex_search(start, end, it->what, it->re))
525                 {
526                     if (it->what[0].first < best_pos)
527                     {
528                         best_pos = it->what[0].first;
529                         bit = it;
530                     }
531                 }
532             }
533             if (bit == replace_list.end())
534                 break;
535         }
536         first = false;
537         replaces = true;
538         size_t i;
539         for (i = 1; i < bit->what.size(); ++i)
540         {
541             //check if the group is named
542             std::map<int, std::string>::const_iterator git
543                 = bit->group_index.find(i);
544             if (git != bit->group_index.end())
545             {   //it is
546                 vars[git->second] = bit->what[i];
547             }
548
549         }
550         //prepare replacement string
551         std::string rvalue = bit->sub_vars(vars);
552         yaz_log(YLOG_LOG, "! Rewritten '%s' to '%s'",
553                 bit->what.str(0).c_str(), rvalue.c_str());
554         out.append(start, bit->what[0].first);
555         out.append(rvalue);
556         start = bit->what[0].second; //move search forward
557     }
558     out.append(start, end);
559     txt = out;
560     return replaces;
561 }
562
563 void yf::HttpRewrite::Replace::parse_groups(std::string pattern)
564 {
565     int gnum = 0;
566     bool esc = false;
567     const std::string &str = pattern;
568     std::string res;
569     start_anchor = str[0] == '^';
570     yaz_log(YLOG_LOG, "Parsing groups from '%s'", str.c_str());
571     for (size_t i = 0; i < str.size(); ++i)
572     {
573         res += str[i];
574         if (!esc && str[i] == '\\')
575         {
576             esc = true;
577             continue;
578         }
579         if (!esc && str[i] == '(') //group starts
580         {
581             gnum++;
582             if (i+1 < str.size() && str[i+1] == '?') //group with attrs
583             {
584                 i++;
585                 if (i+1 < str.size() && str[i+1] == ':') //non-capturing
586                 {
587                     if (gnum > 0) gnum--;
588                     res += str[i];
589                     i++;
590                     res += str[i];
591                     continue;
592                 }
593                 if (i+1 < str.size() && str[i+1] == 'P') //optional, python
594                     i++;
595                 if (i+1 < str.size() && str[i+1] == '<') //named
596                 {
597                     i++;
598                     std::string gname;
599                     bool term = false;
600                     while (++i < str.size())
601                     {
602                         if (str[i] == '>') { term = true; break; }
603                         if (!isalnum(str[i]))
604                             throw mp::filter::FilterException
605                                 ("Only alphanumeric chars allowed, found "
606                                  " in '"
607                                  + str
608                                  + "' at "
609                                  + boost::lexical_cast<std::string>(i));
610                         gname += str[i];
611                     }
612                     if (!term)
613                         throw mp::filter::FilterException
614                             ("Unterminated group name '" + gname
615                              + " in '" + str +"'");
616                     group_index[gnum] = gname;
617                     yaz_log(YLOG_LOG, "Found named group '%s' at $%d",
618                             gname.c_str(), gnum);
619                 }
620             }
621         }
622         esc = false;
623     }
624     re = res;
625 }
626
627 std::string yf::HttpRewrite::Replace::sub_vars(
628     const std::map<std::string, std::string> & vars) const
629 {
630     std::string out;
631     bool esc = false;
632     const std::string & in = recipe;
633     for (size_t i = 0; i < in.size(); ++i)
634     {
635         if (!esc && in[i] == '\\')
636         {
637             esc = true;
638             continue;
639         }
640         if (!esc && in[i] == '$') //var
641         {
642             if (i+1 < in.size() && in[i+1] == '{') //ref prefix
643             {
644                 ++i;
645                 std::string name;
646                 bool term = false;
647                 while (++i < in.size())
648                 {
649                     if (in[i] == '}') { term = true; break; }
650                     name += in[i];
651                 }
652                 if (!term) throw mp::filter::FilterException
653                     ("Unterminated var ref in '"+in+"' at "
654                      + boost::lexical_cast<std::string>(i));
655                 std::map<std::string, std::string>::const_iterator it
656                     = vars.find(name);
657                 if (it != vars.end())
658                 {
659                     out += it->second;
660                 }
661             }
662             else
663             {
664                 throw mp::filter::FilterException
665                     ("Malformed or trimmed var ref in '"
666                      +in+"' at "+boost::lexical_cast<std::string>(i));
667             }
668             continue;
669         }
670         //passthru
671         out += in[i];
672         esc = false;
673     }
674     return out;
675 }
676
677 yf::HttpRewrite::Phase::Phase() : m_verbose(0)
678 {
679 }
680
681 void yf::HttpRewrite::Content::parse(
682     int verbose,
683     std::string &content,
684     std::map<std::string, std::string> &vars) const
685 {
686     if (type == "html")
687     {
688         HTMLParser parser;
689         Event ev(this, vars);
690
691         parser.set_verbose(verbose);
692
693         parser.parse(ev, content.c_str());
694         content = ev.result();
695     }
696     if (type == "quoted-literal")
697     {
698         quoted_literal(content, vars);
699     }
700 }
701
702 void yf::HttpRewrite::Content::quoted_literal(
703     std::string &content,
704     std::map<std::string, std::string> &vars) const
705 {
706     std::list<Within>::const_iterator it = within_list.begin();
707     if (it != within_list.end())
708         embed_quoted_literal(content, vars, it->rule, false);
709 }
710
711 void yf::HttpRewrite::Content::configure(
712     const xmlNode *ptr, std::map<std::string, RulePtr > &rules)
713 {
714     for (; ptr; ptr = ptr->next)
715     {
716         if (ptr->type != XML_ELEMENT_NODE)
717             continue;
718         if (!strcmp((const char *) ptr->name, "within"))
719         {
720             static const char *names[7] =
721                 { "header", "attr", "tag", "rule", "reqline", "type", 0 };
722             std::string values[6];
723             mp::xml::parse_attr(ptr, names, values);
724             Within w;
725             w.header = values[0];
726             w.attr = values[1];
727             if (values[2].length() > 0)
728                 w.tag = values[2];
729             std::map<std::string,RulePtr>::const_iterator it =
730                 rules.find(values[3]);
731             if (it == rules.end())
732                 throw mp::filter::FilterException
733                     ("Reference to non-existing rule '" + values[3] +
734                      "' in http_rewrite filter");
735             w.rule = it->second;
736             w.reqline = values[4] == "1";
737             w.type = values[5];
738             if (w.type.empty() || w.type == "quoted-literal")
739                 ;
740             else
741                 throw mp::filter::FilterException
742                     ("within type must be quoted-literal or none in "
743                      " in http_rewrite filter");
744             within_list.push_back(w);
745         }
746     }
747 }
748
749 void yf::HttpRewrite::configure_phase(const xmlNode *ptr, Phase &phase)
750 {
751     static const char *names[2] = { "verbose", 0 };
752     std::string values[1];
753     values[0] = "0";
754     mp::xml::parse_attr(ptr, names, values);
755
756     phase.m_verbose = atoi(values[0].c_str());
757
758     std::map<std::string, RulePtr > rules;
759     for (ptr = ptr->children; ptr; ptr = ptr->next)
760     {
761         if (ptr->type != XML_ELEMENT_NODE)
762             continue;
763         else if (!strcmp((const char *) ptr->name, "rule"))
764         {
765             static const char *names[2] = { "name", 0 };
766             std::string values[1];
767             values[0] = "default";
768             mp::xml::parse_attr(ptr, names, values);
769
770             RulePtr rule(new Rule);
771             for (xmlNode *p = ptr->children; p; p = p->next)
772             {
773                 if (p->type != XML_ELEMENT_NODE)
774                     continue;
775                 if (!strcmp((const char *) p->name, "rewrite"))
776                 {
777                     Replace replace;
778                     std::string from;
779                     const struct _xmlAttr *attr;
780                     for (attr = p->properties; attr; attr = attr->next)
781                     {
782                         if (!strcmp((const char *) attr->name,  "from"))
783                             from = mp::xml::get_text(attr->children);
784                         else if (!strcmp((const char *) attr->name,  "to"))
785                             replace.recipe = mp::xml::get_text(attr->children);
786                         else
787                             throw mp::filter::FilterException
788                                 ("Bad attribute "
789                                  + std::string((const char *) attr->name)
790                                  + " in rewrite section of http_rewrite");
791                     }
792                     yaz_log(YLOG_LOG, "Found rewrite rule from '%s' to '%s'",
793                             from.c_str(), replace.recipe.c_str());
794                     if (!from.empty())
795                     {
796                         replace.parse_groups(from);
797                         rule->replace_list.push_back(replace);
798                     }
799                 }
800                 else
801                     throw mp::filter::FilterException
802                         ("Bad element "
803                          + std::string((const char *) p->name)
804                          + " in http_rewrite filter");
805             }
806             rules[values[0]] = rule;
807         }
808         else if (!strcmp((const char *) ptr->name, "content"))
809         {
810             static const char *names[3] =
811                 { "type", "mime", 0 };
812             std::string values[2];
813             mp::xml::parse_attr(ptr, names, values);
814             if (values[0].empty())
815             {
816                     throw mp::filter::FilterException
817                         ("Missing attribute, type for for element "
818                          + std::string((const char *) ptr->name)
819                          + " in http_rewrite filter");
820             }
821             Content c;
822
823             c.type = values[0];
824             // if (!values[1].empty())
825                 c.content_re = values[1];
826             c.configure(ptr->children, rules);
827             phase.content_list.push_back(c);
828         }
829         else
830         {
831             throw mp::filter::FilterException
832                 ("Bad element "
833                  + std::string((const char *) ptr->name)
834                  + " in http_rewrite filter");
835         }
836     }
837 }
838
839 void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
840         const char *path)
841 {
842     for (ptr = ptr->children; ptr; ptr = ptr->next)
843     {
844         if (ptr->type != XML_ELEMENT_NODE)
845             continue;
846         else if (!strcmp((const char *) ptr->name, "request"))
847         {
848             configure_phase(ptr, *req_phase);
849         }
850         else if (!strcmp((const char *) ptr->name, "response"))
851         {
852             configure_phase(ptr, *res_phase);
853         }
854         else
855         {
856             throw mp::filter::FilterException
857                 ("Bad element "
858                  + std::string((const char *) ptr->name)
859                  + " in http_rewrite1 filter");
860         }
861     }
862 }
863
864 static mp::filter::Base* filter_creator()
865 {
866     return new mp::filter::HttpRewrite;
867 }
868
869 extern "C" {
870     struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite = {
871         0,
872         "http_rewrite",
873         filter_creator
874     };
875 }
876
877
878 /*
879  * Local variables:
880  * c-basic-offset: 4
881  * c-file-style: "Stroustrup"
882  * indent-tabs-mode: nil
883  * End:
884  * vim: shiftwidth=4 tabstop=8 expandtab
885  */
886