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