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