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