Fix response rewriting
[metaproxy-moved-to-github.git] / src / filter_http_rewrite.hpp
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 #ifndef FILTER_HTTP_REWRITE_HPP
20 #define FILTER_HTTP_REWRITE_HPP
21
22 #include <metaproxy/filter.hpp>
23 #include <vector>
24 #include <map>
25 #include <metaproxy/util.hpp>
26
27 namespace mp = metaproxy_1;
28
29 namespace metaproxy_1 {
30     namespace filter {
31         class HttpRewrite : public Base {
32         public:
33             typedef std::pair<std::string, std::string> string_pair;
34             typedef std::vector<string_pair> spair_vec;
35             typedef spair_vec::iterator spv_iter;
36             HttpRewrite();
37             ~HttpRewrite();
38             void process(metaproxy_1::Package & package) const;
39             void configure(const xmlNode * ptr, bool test_only,
40                            const char *path);
41             void configure(const spair_vec req_uri_pats,
42                            const spair_vec res_uri_pats); 
43         private:
44             spair_vec req_uri_pats;
45             spair_vec res_uri_pats;
46             std::vector<std::map<int, std::string> > req_groups_bynum;
47             std::vector<std::map<int, std::string> > res_groups_bynum;
48             void rewrite_reqline (mp::odr & o, Z_HTTP_Request *hreq,
49                     std::map<std::string, std::string> & vars) const;
50             void rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
51                     std::map<std::string, std::string> & vars,
52                     const spair_vec & uri_pats,
53                     const std::vector<std::map<int, std::string> > & groups_bynum_vec) const;
54             void rewrite_body (mp::odr & o, char **content_buf, int *content_len,
55                     std::map<std::string, std::string> & vars,
56                     const spair_vec & uri_pats,
57                     const std::vector<std::map<int, std::string> > 
58                     & groups_bynum) const; 
59             const std::string test_patterns(
60                     std::map<std::string, std::string> & vars,
61                     const std::string & txt, 
62                     const spair_vec & uri_pats,
63                     const std::vector<std::map<int, std::string> > 
64                     & groups_bynum) const;
65             const std::string search_replace(
66                     std::map<std::string, std::string> & vars,
67                     const std::string & txt,
68                     const std::string & uri_re,
69                     const std::string & uri_pat,
70                     const std::map<int, std::string> & groups_bynum) const;
71             static void parse_groups(
72                     const spair_vec & uri_pats,
73                     std::vector<std::map<int, std::string> > & groups_bynum_vec);
74             static std::string sub_vars (const std::string & in, 
75                     const std::map<std::string, std::string> & vars);
76         };
77     }
78 }
79
80 extern "C" {
81     extern struct metaproxy_1_filter_struct metaproxy_1_filter_http_rewrite;
82 }
83
84 #endif
85 /*
86  * Local variables:
87  * c-basic-offset: 4
88  * c-file-style: "Stroustrup"
89  * indent-tabs-mode: nil
90  * End:
91  * vim: shiftwidth=4 tabstop=8 expandtab
92  */
93