Prepend with proto
[metaproxy-moved-to-github.git] / src / filter_http_rewrite.cpp
index 47b33ed..0ee1afb 100644 (file)
@@ -48,6 +48,7 @@ yf::HttpRewrite::~HttpRewrite()
 
 void yf::HttpRewrite::process(mp::Package & package) const 
 {
+    yaz_log(YLOG_DEBUG, "HttpRewrite begins....");
     Z_GDU *gdu = package.request().get();
     //map of request/response vars
     std::map<std::string, std::string> vars;
@@ -56,8 +57,8 @@ void yf::HttpRewrite::process(mp::Package & package) const
     {
         Z_HTTP_Request *hreq = gdu->u.HTTP_Request;
         mp::odr o;
-        std::cout << ">> Request headers" << std::endl;
         rewrite_reqline(o, hreq, vars);
+        yaz_log(YLOG_DEBUG, ">> Request headers");
         rewrite_headers(o, hreq->headers, vars);
         rewrite_body(o, &hreq->content_buf, &hreq->content_len, vars);
         package.request() = gdu;
@@ -67,9 +68,9 @@ void yf::HttpRewrite::process(mp::Package & package) const
     if (gdu && gdu->which == Z_GDU_HTTP_Response)
     {
         Z_HTTP_Response *hres = gdu->u.HTTP_Response;
-        std::cout << "Response " << hres->code;
-        std::cout << "<< Respose headers" << std::endl;
+        yaz_log(YLOG_DEBUG, "Response code %d", hres->code);
         mp::odr o;
+        yaz_log(YLOG_DEBUG, "<< Respose headers");
         rewrite_headers(o, hres->headers, vars);
         rewrite_body(o, &hres->content_buf, &hres->content_len, vars);
         package.response() = gdu;
@@ -83,22 +84,25 @@ void yf::HttpRewrite::rewrite_reqline (mp::odr & o, Z_HTTP_Request *hreq,
     std::string path;
     if (strstr(hreq->path, "http://") == hreq->path)
     {
-        std::cout << "Path in the method line is absolute, " 
-            "possibly a proxy request\n";
+        yaz_log(YLOG_DEBUG, "Path in the method line is absolute, " 
+            "possibly a proxy request");
         path += hreq->path;
     }
     else
     {
         //TODO what about proto
+        path += "http://";
         path += z_HTTP_header_lookup(hreq->headers, "Host");
         path += hreq->path; 
     }
-    std::cout << "Proxy request URL is " << path << std::endl;
+    yaz_log(YLOG_DEBUG, "Proxy request URL is %s", path.c_str());
     std::string npath = 
         test_patterns(vars, path, req_uri_pats, req_groups_bynum);
-    std::cout << "Resp request URL is " << npath << std::endl;
     if (!npath.empty())
+    {
+        yaz_log(YLOG_DEBUG, "Rewritten request URL is %s", npath.c_str());
         hreq->path = odr_strdup(o, npath.c_str());
+    }
 }
 
 void yf::HttpRewrite::rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
@@ -111,7 +115,7 @@ void yf::HttpRewrite::rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
         std::string sheader(header->name);
         sheader += ": ";
         sheader += header->value;
-        std::cout << header->name << ": " << header->value << std::endl;
+        yaz_log(YLOG_DEBUG, "%s: %s", header->name, header->value);
         std::string out = test_patterns(vars, 
                 sheader, 
                 req_uri_pats, req_groups_bynum);
@@ -120,7 +124,7 @@ void yf::HttpRewrite::rewrite_headers (mp::odr & o, Z_HTTP_Header *headers,
             size_t pos = out.find(": ");
             if (pos == std::string::npos)
             {
-                std::cout << "Header malformed during rewrite, ignoring";
+                yaz_log(YLOG_DEBUG, "Header malformed during rewrite, ignoring");
                 continue;
             }
             header->name = odr_strdup(o, out.substr(0, pos).c_str());
@@ -146,9 +150,6 @@ void yf::HttpRewrite::rewrite_body (mp::odr & o, char **content_buf, int *conten
     }
 }
 
-
-void yf::HttpRewrite::configure(const xmlNode* ptr, bool test_only, const char *path) {};
-
 /**
  * Tests pattern from the vector in order and executes recipe on
  the first match.
@@ -160,7 +161,7 @@ const std::string yf::HttpRewrite::test_patterns(
         const std::vector<std::map<int, std::string> > & groups_bynum_vec)
     const
 {
-    for (int i = 0; i < uri_pats.size(); i++) 
+    for (unsigned i = 0; i < uri_pats.size(); i++) 
     {
         std::string out = search_replace(vars, txt, 
                 uri_pats[i].first, uri_pats[i].second,
@@ -206,7 +207,8 @@ const std::string yf::HttpRewrite::search_replace(
         //rewrite value
         std::string rhvalue = what.prefix().str() 
             + rvalue + what.suffix().str();
-        std::cout << "! Rewritten '"+what.str(0)+"' to '"+rvalue+"'\n";
+        yaz_log(YLOG_DEBUG, "! Rewritten '%s' to '%s'", 
+                what.str(0).c_str(), rvalue.c_str());
         out += rhvalue;
         start = what[0].second; //move search forward
     }
@@ -217,7 +219,7 @@ void yf::HttpRewrite::parse_groups(
         const spair_vec & uri_pats,
         std::vector<std::map<int, std::string> > & groups_bynum_vec)
 {
-    for (int h = 0; h < uri_pats.size(); h++) 
+    for (unsigned h = 0; h < uri_pats.size(); h++) 
     {
         int gnum = 0;
         bool esc = false;
@@ -225,7 +227,7 @@ void yf::HttpRewrite::parse_groups(
         std::string str = uri_pats[h].first;
         //for each pair we have an indexing map
         std::map<int, std::string> groups_bynum;
-        for (int i = 0; i < str.size(); ++i)
+        for (unsigned i = 0; i < str.size(); ++i)
         {
             if (!esc && str[i] == '\\')
             {
@@ -268,8 +270,8 @@ void yf::HttpRewrite::parse_groups(
                                 ("Unterminated group name '" + gname 
                                  + " in '" + str +"'");
                         groups_bynum[gnum] = gname;
-                        std::cout << "Found named group '" << gname 
-                            << "' at $" << gnum << std::endl;
+                        yaz_log(YLOG_DEBUG, "Found named group '%s' at $%d",
+                                gname.c_str(), gnum);
                     }
                 }
             }
@@ -284,7 +286,7 @@ std::string yf::HttpRewrite::sub_vars (const std::string & in,
 {
     std::string out;
     bool esc = false;
-    for (int i = 0; i < in.size(); ++i)
+    for (unsigned i = 0; i < in.size(); ++i)
     {
         if (!esc && in[i] == '\\')
         {
@@ -340,6 +342,72 @@ void yf::HttpRewrite::configure(
     parse_groups(res_uri_pats, res_groups_bynum);
 }
 
+
+static void configure_rules(const xmlNode *ptr, yf::HttpRewrite::spair_vec & dest)
+{
+    for (ptr = ptr->children; ptr; ptr = ptr->next)
+    {
+        if (ptr->type != XML_ELEMENT_NODE)
+            continue;
+        else if (!strcmp((const char *) ptr->name, "rewrite"))
+        {
+            std::string from, to;
+            const struct _xmlAttr *attr;
+            for (attr = ptr->properties; attr; attr = attr->next)
+            {
+                if (!strcmp((const char *) attr->name,  "from"))
+                    from = mp::xml::get_text(attr->children);
+                else if (!strcmp((const char *) attr->name,  "to"))
+                    to = mp::xml::get_text(attr->children);
+                else
+                    throw mp::filter::FilterException
+                        ("Bad attribute "
+                         + std::string((const char *) attr->name)
+                         + " in rewrite section of http_rewrite");
+            }
+            yaz_log(YLOG_DEBUG, "Found rewrite rule from '%s' to '%s'", 
+                    from.c_str(), to.c_str());
+            if (!from.empty())
+                dest.push_back(std::make_pair(from, to));
+        }
+        else
+        {
+            throw mp::filter::FilterException
+                ("Bad element o"
+                 + std::string((const char *) ptr->name)
+                 + " in http_rewrite1 filter");
+        }
+    }
+}
+
+void yf::HttpRewrite::configure(const xmlNode * ptr, bool test_only,
+        const char *path)
+{
+    spair_vec req_uri_pats;
+    spair_vec res_uri_pats;
+    for (ptr = ptr->children; ptr; ptr = ptr->next)
+    {
+        if (ptr->type != XML_ELEMENT_NODE)
+            continue;
+        else if (!strcmp((const char *) ptr->name, "request"))
+        {
+            configure_rules(ptr, req_uri_pats);
+        }
+        else if (!strcmp((const char *) ptr->name, "response"))
+        {
+            configure_rules(ptr, res_uri_pats);
+        }
+        else
+        {
+            throw mp::filter::FilterException
+                ("Bad element "
+                 + std::string((const char *) ptr->name)
+                 + " in http_rewrite1 filter");
+        }
+    }
+    configure(req_uri_pats, res_uri_pats);
+}
+
 static mp::filter::Base* filter_creator()
 {
     return new mp::filter::HttpRewrite;