Fix response rewriting
authorJakub Skoczen <jakub@indexdata.dk>
Tue, 7 May 2013 13:44:57 +0000 (15:44 +0200)
committerJakub Skoczen <jakub@indexdata.dk>
Tue, 7 May 2013 13:44:57 +0000 (15:44 +0200)
src/filter_http_rewrite.cpp
src/filter_http_rewrite.hpp
src/test_filter_rewrite.cpp

index e937a73..ff1a25c 100644 (file)
@@ -59,8 +59,9 @@ void yf::HttpRewrite::process(mp::Package & package) const
         mp::odr o;
         rewrite_reqline(o, hreq, vars);
         yaz_log(YLOG_LOG, ">> Request headers");
-        rewrite_headers(o, hreq->headers, vars);
-        rewrite_body(o, &hreq->content_buf, &hreq->content_len, vars);
+        rewrite_headers(o, hreq->headers, vars, req_uri_pats, req_groups_bynum);
+        rewrite_body(o, &hreq->content_buf, &hreq->content_len, vars,
+                req_uri_pats, req_groups_bynum);
         package.request() = gdu;
     }
     package.move();
@@ -71,8 +72,9 @@ void yf::HttpRewrite::process(mp::Package & package) const
         yaz_log(YLOG_LOG, "Response code %d", hres->code);
         mp::odr o;
         yaz_log(YLOG_LOG, "<< Respose headers");
-        rewrite_headers(o, hres->headers, vars);
-        rewrite_body(o, &hres->content_buf, &hres->content_len, vars);
+        rewrite_headers(o, hres->headers, vars, res_uri_pats, res_groups_bynum);
+        rewrite_body(o, &hres->content_buf, &hres->content_len, vars,
+                res_uri_pats, res_groups_bynum);
         package.response() = gdu;
     }
 }
@@ -106,7 +108,9 @@ void yf::HttpRewrite::rewrite_reqline (mp::odr & o, Z_HTTP_Request *hreq,
 }
 
 void yf::HttpRewrite::rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
-        std::map<std::string, std::string> & vars) const 
+        std::map<std::string, std::string> & vars, 
+        const spair_vec & uri_pats,
+        const std::vector<std::map<int, std::string> > & groups_bynum) const 
 {
     for (Z_HTTP_Header *header = headers;
             header != 0; 
@@ -116,9 +120,7 @@ void yf::HttpRewrite::rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
         sheader += ": ";
         sheader += header->value;
         yaz_log(YLOG_LOG, "%s: %s", header->name, header->value);
-        std::string out = test_patterns(vars, 
-                sheader, 
-                req_uri_pats, req_groups_bynum);
+        std::string out = test_patterns(vars, sheader, uri_pats, groups_bynum);
         if (!out.empty()) 
         {
             size_t pos = out.find(": ");
@@ -135,13 +137,15 @@ void yf::HttpRewrite::rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
 }
 
 void yf::HttpRewrite::rewrite_body (mp::odr & o, char **content_buf, int *content_len,
-        std::map<std::string, std::string> & vars) const 
+        std::map<std::string, std::string> & vars,
+        const spair_vec & uri_pats,
+        const std::vector<std::map<int, std::string> > & groups_bynum) const 
 {
     if (*content_buf)
     {
         std::string body(*content_buf);
         std::string nbody = 
-            test_patterns(vars, body, req_uri_pats, req_groups_bynum);
+            test_patterns(vars, body, uri_pats, groups_bynum);
         if (!nbody.empty())
         {
             *content_buf = odr_strdup(o, nbody.c_str());
index bcfba00..11b43e6 100644 (file)
@@ -48,14 +48,20 @@ namespace metaproxy_1 {
             void rewrite_reqline (mp::odr & o, Z_HTTP_Request *hreq,
                     std::map<std::string, std::string> & vars) const;
             void rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
-                    std::map<std::string, std::string> & vars) const; 
+                    std::map<std::string, std::string> & vars,
+                    const spair_vec & uri_pats,
+                    const std::vector<std::map<int, std::string> > & groups_bynum_vec) const;
             void rewrite_body (mp::odr & o, char **content_buf, int *content_len,
-                    std::map<std::string, std::string> & vars) const;
+                    std::map<std::string, std::string> & vars,
+                    const spair_vec & uri_pats,
+                    const std::vector<std::map<int, std::string> > 
+                    & groups_bynum) const; 
             const std::string test_patterns(
                     std::map<std::string, std::string> & vars,
                     const std::string & txt, 
                     const spair_vec & uri_pats,
-                    const std::vector<std::map<int, std::string> > & groups_bynum_vec) const;
+                    const std::vector<std::map<int, std::string> > 
+                    & groups_bynum) const;
             const std::string search_replace(
                     std::map<std::string, std::string> & vars,
                     const std::string & txt,
index d69d927..7753f0e 100644 (file)
@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_2 )
         mp::filter::HttpRewrite::spair_vec vec_res;
         vec_res.push_back(std::make_pair(
         "(?<proto>http\\:\\/\\/s?)(?<host>[^\\/?#]+)\\/(?<path>[^ >]+)",
-        "http://${pxhost}/${pxpath}/${host}/${path}"
+        "THAT WAS MATCHED"
         ));
         
         fhr.configure(vec_req, vec_res);
@@ -143,7 +143,7 @@ BOOST_AUTO_TEST_CASE( test_filter_rewrite_3 )
             " <response>\n"
             "   <rewrite from='"
     "(?&lt;proto>http\\:\\/\\/s?)(?&lt;host>[^\\/?#]+)\\/(?&lt;path>[^ >]+)'\n"
-            "            to='http://${pxhost}/${pxpath}/${host}/${path}' />\n" 
+            "            to='THAT WAS MATCHED' />\n" 
             " </response>\n"
             "</filter>\n"
             ;