1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
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
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
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
23 #include "filter_http_client.hpp"
24 #include <metaproxy/util.hpp>
25 #include "router_chain.hpp"
26 #include <metaproxy/package.hpp>
28 #define BOOST_REGEX_MATCH_EXTRA
30 #include <boost/regex.hpp>
32 #define BOOST_AUTO_TEST_MAIN
33 #define BOOST_TEST_DYN_LINK
35 #include <boost/test/auto_unit_test.hpp>
37 using namespace boost::unit_test;
38 namespace mp = metaproxy_1;
40 class FilterHeaderRewrite: public mp::filter::Base {
42 void process(mp::Package & package) const {
43 Z_GDU *gdu = package.request().get();
45 if (gdu && gdu->which == Z_GDU_HTTP_Request)
47 std::cout << "Request headers" << std::endl;
48 Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
50 for (Z_HTTP_Header *header = hreq->headers;
52 header = header->next)
54 std::cout << header->name << ": " << header->value << std::endl;
55 rewrite_req_header(header);
59 gdu = package.response().get();
60 if (gdu && gdu->which == Z_GDU_HTTP_Response)
62 std::cout << "Respose headers" << std::endl;
63 Z_HTTP_Response *hr = gdu->u.HTTP_Response;
65 for (Z_HTTP_Header *header = hr->headers;
67 header = header->next)
69 std::cout << header->name << ": " << header->value << std::endl;
74 void configure(const xmlNode* ptr, bool test_only, const char *path) {};
76 void rewrite_req_header(Z_HTTP_Header *header) const
78 //exec regex against value
79 boost::regex e(req_uri_pat, boost::regex::perl);
81 std::string hvalue(header->value);
82 if(boost::regex_match(hvalue, what, e, boost::match_extra))
85 std::cout << "** Match found **\n Sub-Expressions:\n";
86 for(i = 0; i < what.size(); ++i)
87 std::cout << " $" << i << " = \"" << what[i] << "\"\n";
88 std::cout << " Captures:\n";
89 for(i = 0; i < what.size(); ++i)
91 std::cout << " $" << i << " = {";
92 for(j = 0; j < what.captures(i).size(); ++j)
98 std::cout << "\"" << what.captures(i)[j] << "\"";
105 std::cout << "** No Match found **\n";
107 //iteratate over named groups
108 //set the captured values in the map
109 //rewrite the header according to the hardcoded recipe
112 void configure(const std::string & req_uri_pat,
113 const std::string & resp_uri_pat)
115 this->req_uri_pat = req_uri_pat;
116 this->resp_uri_pat = resp_uri_pat;
120 std::map<std::string, std::string> vars;
121 std::string req_uri_pat;
122 std::string resp_uri_pat;
126 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
130 FilterHeaderRewrite fhr;
137 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
141 mp::RouterChain router;
143 FilterHeaderRewrite fhr;
144 fhr.configure(".*(localhost).*", ".*(localhost).*");
145 mp::filter::HTTPClient hc;
150 // create an http request
154 Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
155 "http://localhost:80/~jakub/targetsite.php", 0, 1);
157 pack.request() = gdu_req;
160 pack.router(router).move();
162 //analyze the response
163 Z_GDU *gdu_res = pack.response().get();
164 BOOST_CHECK(gdu_res);
165 BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
167 Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
179 * c-file-style: "Stroustrup"
180 * indent-tabs-mode: nil
182 * vim: shiftwidth=4 tabstop=8 expandtab