Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/metaproxy
[metaproxy-moved-to-github.git] / src / test_filter_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 <iostream>
21 #include <stdexcept>
22
23 #include "filter_http_client.hpp"
24 #include "filter_http_rewrite.hpp"
25 #include <metaproxy/util.hpp>
26 #include <metaproxy/router_chain.hpp>
27 #include <metaproxy/package.hpp>
28
29 #include <boost/regex.hpp>
30 #include <boost/lexical_cast.hpp>
31
32 #include <yaz/log.h>
33
34 #define BOOST_AUTO_TEST_MAIN
35 #define BOOST_TEST_DYN_LINK
36
37 #include <boost/test/auto_unit_test.hpp>
38
39 using namespace boost::unit_test;
40 namespace mp = metaproxy_1;
41
42 BOOST_AUTO_TEST_CASE( test_filter_rewrite_1 )
43 {
44     try
45     {
46         mp::RouterChain router;
47         mp::filter::HttpRewrite fhr;
48
49         std::string xmlconf =
50             "<?xml version='1.0'?>\n"
51             "<filter xmlns='http://indexdata.com/metaproxy'\n"
52             "        id='rewrite1' type='http_rewrite'>\n"
53             " <request verbose=\"1\">\n"
54             "  <rule name=\"null\"/>\n"
55             "  <rule name=\"url\">\n"
56             "     <rewrite from='"
57     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
58     "/(?&lt;host>[^ /]+)(?&lt;path>[^ ]*)'\n"
59             "            to='${proto}${host}${path}' />\n"
60             "     <rewrite from='(?:Host: )(.*)'\n"
61             "            to='Host: ${host}' />\n"
62             "  </rule>\n"
63             "  <content type=\"headers\">\n"
64             "    <within header=\"link\" rule=\"null\"/>\n"
65             "    <within reqline=\"1\" rule=\"url\"/>\n"
66             "  </content>\n"
67             " </request>\n"
68             " <response verbose=\"1\">\n"
69             "  <rule name=\"null\"/>\n"
70             "  <rule name=\"cx\">\n"
71             "     <rewrite from='^cx' to='cy'/>\n"
72             "  </rule>\n"
73             "  <rule name=\"url\">\n"
74             "     <rewrite from='foo' to='bar'/>\n"
75             "     <rewrite from='"
76     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
77             "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
78             "  </rule>\n"
79             "  <content type=\"headers\">\n"
80             "    <within header=\"link\" rule=\"url\"/>\n"
81             "  </content>\n"
82             "  <content type=\"html\" mime=\"text/xml|text/html\">\n"
83             "    <within tag=\"body\" attr=\"background\" rule=\"null\"/>\n"
84             "    <within tag=\"script\" attr=\"#text\" type=\"quoted-literal\" rule=\"url\"/>\n"
85             "    <within tag=\"style\" attr=\"#text\" rule=\"url\"/>\n"
86             "    <within attr=\"href|src\" rule=\"url,cx\"/>\n"
87             "    <within attr=\"onclick\" type=\"quoted-literal\" rule=\"url\"/>\n"
88             "  </content>\n"
89             "  <content type=\"quoted-literal\" mime=\".*javascript\">\n"
90             "    <within rule=\"url\"/>\n"
91             "  </content>\n"
92             " </response>\n"
93             "</filter>\n"
94         ;
95
96         std::cout << xmlconf;
97
98         // reading and parsing XML conf
99         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
100         BOOST_CHECK(doc);
101         xmlNode *root_element = xmlDocGetRootElement(doc);
102         fhr.configure(root_element, true, "");
103         xmlFreeDoc(doc);
104
105         router.append(fhr);
106
107         // create an http request
108         mp::Package pack;
109
110         mp::odr odr;
111         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
112         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
113
114         pack.request() = gdu_req;
115
116         //create the http response
117
118         const char *resp_buf =
119             "HTTP/1.1 200 OK\r\n"
120             "Content-Type: text/html\r\n"
121             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
122             "Link: </dir/file.xml>; rel=relative\r\n"
123             "\r\n"
124             "<html><head><title>Hello proxy!</title>"
125             "<style>"
126             "body {"
127             "  background-image:url('http://targetsite/images/bg.png');"
128             "}"
129             "</style>"
130             "</head>"
131             "<script>var jslink=\"http://targetsite/webservice.xml\";"
132             "for (i = 0; i<foo; i++) ;\n"
133             "var some=\"foo\"; foo=1;"
134             "</script>"
135             "<body>"
136             "<p>Welcome to our website. It doesn't make it easy to get pro"
137             "xified"
138             "<a href=\"http://targetsite/page2.html\">"
139             "  An absolute link</a>"
140             "<a target=_blank href=\"http://targetsite/page3.html\">"
141             "  Another abs link</a>"
142             "<a href=\"/docs/page4.html\" />"
143             "<A href=\"cxcx\" />"
144             "<a HREF=\"cx \" onclick=\"foo(&quot;foo&quot;);\"/>"
145             "</body></html>";
146
147         const char *resp_expected =
148             "HTTP/1.1 200 OK\r\n"
149             "Content-Length: 631\r\n"
150             "Content-Type: text/html\r\n"
151             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
152             "Link: </dir/file.xml>; rel=relative\r\n"
153             "\r\n"
154             "<html><head><title>Hello proxy!</title>"
155             "<style>"
156             "body {"
157             "  background-image:url('http://proxyhost/proxypath/targetsite/images/bg.png');"
158             "}"
159             "</style>"
160             "</head>"
161             "<script>var jslink=\"http://proxyhost/proxypath/targetsite/webservice.xml\";"
162             "for (i = 0; i<foo; i++) ;\n"
163             "var some=\"bar\"; foo=1;"
164             "</script>"
165             "<body>"
166             "<p>Welcome to our website. It doesn't make it easy to get pro"
167             "xified"
168             "<a href=\"http://proxyhost/proxypath/targetsite/page2.html\">"
169             "  An absolute link</a>"
170             "<a target=_blank href=\"http://proxyhost/proxypath/targetsite/page3.html\">"
171             "  Another abs link</a>"
172             "<a href=\"/docs/page4.html\"/>"
173             "<A href=\"cycx\"/>"
174             "<a HREF=\"cy \" onclick=\"foo(&quot;bar&quot;);\"/>"
175             "</body></html>";
176
177         Z_GDU *gdu_res;
178         mp::odr dec(ODR_DECODE);
179         mp::odr enc(ODR_ENCODE);
180         odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
181         int r = z_GDU(dec, &gdu_res, 0, 0);
182
183         BOOST_CHECK(r);
184         if (r)
185         {
186             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
187
188             pack.response() = gdu_res;
189
190             //feed to the router
191             pack.router(router).move();
192
193             //analyze the response
194             Z_GDU *gdu_res_rew = pack.response().get();
195             BOOST_CHECK(gdu_res_rew);
196             BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
197
198             Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
199             BOOST_CHECK(hres);
200
201             z_GDU(enc, &gdu_res_rew, 0, 0);
202             char *resp_result;
203             int resp_result_len;
204             resp_result = odr_getbuf(enc, &resp_result_len, 0);
205
206             int equal = ((size_t) resp_result_len == strlen(resp_expected))
207                 && !memcmp(resp_result, resp_expected, resp_result_len);
208             BOOST_CHECK(equal);
209
210             if (!equal)
211             {
212                 //compare buffers
213                 std::cout << "Expected result:\n" << resp_expected << "\n";
214                 std::cout << "Got result:\n";
215                 fflush(stdout);
216                 fwrite(resp_result, 1, resp_result_len, stdout);
217                 fflush(stdout);
218                 std::cout << "\nGot result buf len: " << resp_result_len
219                           << "\n";
220             }
221         }
222     }
223     catch (std::exception & e) {
224         std::cout << e.what();
225         std::cout << std::endl;
226         BOOST_CHECK (false);
227     }
228 }
229
230 BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
231 {
232     try
233     {
234         mp::RouterChain router;
235         mp::filter::HttpRewrite fhr;
236
237         std::string xmlconf =
238             "<?xml version='1.0'?>\n"
239             "<filter xmlns='http://indexdata.com/metaproxy'\n"
240             "        id='rewrite1' type='http_rewrite'>\n"
241             " <request verbose=\"1\">\n"
242             "  <rule name=\"null\"/>\n"
243             "  <rule name=\"url\">\n"
244             "     <rewrite from='"
245     "(?&lt;proto>https?://)(?&lt;pxhost>[^ /?#]+)/(?&lt;pxpath>[^ /]+)"
246     "/(?&lt;host>[^ /]+)(?&lt;path>[^ ]*)'\n"
247             "            to='${proto}${host}${path}' />\n"
248             "     <rewrite from='(?:Host: )(.*)'\n"
249             "            to='Host: ${host}' />\n"
250             "  </rule>\n"
251             "  <content type=\"headers\">\n"
252             "    <within header=\"link\" rule=\"null\"/>\n"
253             "    <within reqline=\"1\" rule=\"url\"/>\n"
254             "  </content>\n"
255             " </request>\n"
256             " <response verbose=\"1\">\n"
257             "  <rule name=\"null\"/>\n"
258             "  <rule name=\"url\">\n"
259             "     <rewrite from='foo' to='bar'/>\n"
260             "     <rewrite from='^cx' to='cy'/>\n"
261             "     <rewrite from='"
262     "(?&lt;proto>https?://)(?&lt;host>[^/?# &quot;&apos;>]+)/(?&lt;path>[^  &quot;&apos;>]+)'\n"
263             "            to='${proto}${pxhost}/${pxpath}/${host}/${path}' />\n"
264             "  </rule>\n"
265             "  <content type=\"headers\">\n"
266             "    <within header=\"link\" rule=\"url\"/>\n"
267             "  </content>\n"
268             "  <content type=\"quoted-literal\" mime=\".*javascript\">\n"
269             "    <within rule=\"url\"/>\n"
270             "  </content>\n"
271             " </response>\n"
272             "</filter>\n"
273         ;
274
275         std::cout << xmlconf;
276
277         // reading and parsing XML conf
278         xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
279         BOOST_CHECK(doc);
280         xmlNode *root_element = xmlDocGetRootElement(doc);
281         fhr.configure(root_element, true, "");
282         xmlFreeDoc(doc);
283
284         router.append(fhr);
285
286         // create an http request
287         mp::Package pack;
288
289         mp::odr odr;
290         Z_GDU *gdu_req = z_get_HTTP_Request_uri(odr,
291         "http://proxyhost/proxypath/targetsite/page1.html", 0, 1);
292
293         pack.request() = gdu_req;
294
295         //create the http response
296
297         const char *resp_buf =
298             "HTTP/1.1 200 OK\r\n"
299             "Content-Type: application/javascript\r\n"
300             "Link: <http://targetsite/file.xml>; rel=absolute\r\n"
301             "Link: </dir/file.xml>; rel=relative\r\n"
302             "\r\n"
303             "// \"\n"
304             "my.location = 'http://targetsite/images/bg.png';\n"
305             "my.other = \"http://targetsite/images/fg.png\";\n"
306             "my.thrd = \"other\";\n"
307             "// \"http://targetsite/images/bg.png\n";
308
309         const char *resp_expected =
310             "HTTP/1.1 200 OK\r\n"
311             "Content-Length: 195\r\n"
312             "Content-Type: application/javascript\r\n"
313             "Link: <http://proxyhost/proxypath/targetsite/file.xml>; rel=absolute\r\n"
314             "Link: </dir/file.xml>; rel=relative\r\n"
315             "\r\n"
316             "// \"\n"
317             "my.location = 'http://proxyhost/proxypath/targetsite/images/bg.png';\n"
318             "my.other = \"http://proxyhost/proxypath/targetsite/images/fg.png\";\n"
319             "my.thrd = \"other\";\n"
320             "// \"http://targetsite/images/bg.png\n";
321
322         Z_GDU *gdu_res;
323         mp::odr dec(ODR_DECODE);
324         mp::odr enc(ODR_ENCODE);
325         odr_setbuf(dec, (char *) resp_buf, strlen(resp_buf), 0);
326         int r = z_GDU(dec, &gdu_res, 0, 0);
327
328         BOOST_CHECK(r);
329         if (r)
330         {
331             BOOST_CHECK_EQUAL(gdu_res->which, Z_GDU_HTTP_Response);
332
333             pack.response() = gdu_res;
334
335             //feed to the router
336             pack.router(router).move();
337
338             //analyze the response
339             Z_GDU *gdu_res_rew = pack.response().get();
340             BOOST_CHECK(gdu_res_rew);
341             BOOST_CHECK_EQUAL(gdu_res_rew->which, Z_GDU_HTTP_Response);
342
343             Z_HTTP_Response *hres = gdu_res_rew->u.HTTP_Response;
344             BOOST_CHECK(hres);
345
346             z_GDU(enc, &gdu_res_rew, 0, 0);
347             char *resp_result;
348             int resp_result_len;
349             resp_result = odr_getbuf(enc, &resp_result_len, 0);
350
351             int equal = ((size_t) resp_result_len == strlen(resp_expected))
352                 && !memcmp(resp_result, resp_expected, resp_result_len);
353             BOOST_CHECK(equal);
354
355             if (!equal)
356             {
357                 //compare buffers
358                 std::cout << "Expected result:\n" << resp_expected << "\n";
359                 std::cout << "Got result:\n";
360                 fflush(stdout);
361                 fwrite(resp_result, 1, resp_result_len, stdout);
362                 fflush(stdout);
363                 std::cout << "\nGot result buf len: " << resp_result_len
364                           << "\n";
365             }
366         }
367     }
368     catch (std::exception & e) {
369         std::cout << e.what();
370         std::cout << std::endl;
371         BOOST_CHECK (false);
372     }
373 }
374
375 /*
376  * Local variables:
377  * c-basic-offset: 4
378  * c-file-style: "Stroustrup"
379  * indent-tabs-mode: nil
380  * End:
381  * vim: shiftwidth=4 tabstop=8 expandtab
382  */
383