added socket listen_host and listen_port to Origin class
[metaproxy-moved-to-github.git] / src / filter_sru_to_z3950.cpp
index 2d7bcb9..d8469c9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_sru_to_z3950.cpp,v 1.14 2006-09-26 13:15:33 marc Exp $
+/* $Id: filter_sru_to_z3950.cpp,v 1.17 2006-10-02 12:01:06 marc Exp $
    Copyright (c) 2005-2006, Index Data.
 
    See the LICENSE file for details
 
 
 namespace mp = metaproxy_1;
+namespace mp_util = metaproxy_1::util;
 namespace yf = mp::filter;
 
-namespace metaproxy_1 
-{
-
-    template<typename T>
-    std::string to_string(const T& t)
-    {
-        std::ostringstream o;
-        if(o << t)
-            return o.str();
-        
-        return std::string();
-    }
-
-    std::string http_header_value(const Z_HTTP_Header* header, 
-                                  const std::string name)
-    {
-        while (header && header->name
-               && std::string(header->name) !=  name)
-            header = header->next;
-        
-        if (header && header->name && std::string(header->name) == name
-            && header->value)
-            return std::string(header->value);
-
-        return std::string();
-    }
-    
-
-}
-
 
 namespace metaproxy_1 {
     namespace filter {
-        class SRUtoZ3950::Rep {
-        private:
-            union SRW_query {char * cql; char * xcql; char * pqf;};
-            typedef const int& SRW_query_type;
+        class SRUtoZ3950::Impl {
         public:
             void configure(const xmlNode *xmlnode);
             void process(metaproxy_1::Package &package) const;
         private:
+            union SRW_query {char * cql; char * xcql; char * pqf;};
+            typedef const int& SRW_query_type;
+        private:
             std::string sru_protocol(const Z_HTTP_Request &http_req) const;
             std::string debug_http(const Z_HTTP_Request &http_req) const;
             void http_response(mp::Package &package, 
@@ -119,14 +90,17 @@ namespace metaproxy_1 {
                                     Z_SRW_PDU *sru_pdu_res,
                                     Z_SRW_scanRequest 
                                     const *sr_req) const;
-            Z_ElementSetNames * build_esn_from_schema(mp::odr &odr_en, const char *schema) const;
-            int z3950_to_srw_diag(mp::odr &odr_en, Z_SRW_searchRetrieveResponse *srw_res,
+            Z_ElementSetNames * build_esn_from_schema(mp::odr &odr_en, 
+                                                      const char *schema) 
+                const;
+            int z3950_to_srw_diag(mp::odr &odr_en, 
+                                  Z_SRW_searchRetrieveResponse *srw_res,
                                   Z_DefaultDiagFormat *ddf) const;
         };
     }
 }
 
-yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Rep)
+yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Impl)
 {
 }
 
@@ -144,11 +118,11 @@ void yf::SRUtoZ3950::process(mp::Package &package) const
     m_p->process(package);
 }
 
-void yf::SRUtoZ3950::Rep::configure(const xmlNode *xmlnode)
+void yf::SRUtoZ3950::Impl::configure(const xmlNode *xmlnode)
 {
 }
 
-void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
+void yf::SRUtoZ3950::Impl::process(mp::Package &package) const
 {
     Z_GDU *zgdu_req = package.request().get();
 
@@ -273,13 +247,15 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
 }
 
 
-bool yf::SRUtoZ3950::Rep::build_simple_explain(mp::Package &package, 
+bool yf::SRUtoZ3950::Impl::build_simple_explain(mp::Package &package, 
                                                mp::odr &odr_en,
                                                Z_SRW_PDU *sru_pdu_res,
                                                Z_SRW_explainRequest 
                                                const *er_req) const
 {
 
+
     // z3950'fy recordPacking
     int record_packing = Z_SRW_recordPacking_XML;
     if (er_req->recordPacking && 's' == *(er_req->recordPacking))
@@ -290,20 +266,43 @@ bool yf::SRUtoZ3950::Rep::build_simple_explain(mp::Package &package,
     if (er_req->database)
         database = er_req->database;
 
+    // getting host and port info
+    std::string host = package.origin().listen_host();
+    std::string port = mp_util::to_string(package.origin().listen_port());
+
+    // overwriting host and port info if set from HTTP Host header
+    Z_GDU *zgdu_req = package.request().get();
+    if  (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
+    {
+        Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
+        if (http_req)
+        {
+            std::string http_host_address
+                = mp_util::http_header_value(http_req->headers, "Host");
+
+            std::string::size_type i = http_host_address.rfind(":");
+            if (i != std::string::npos)
+            {
+                host.assign(http_host_address, 0, i);
+                port.assign(http_host_address, i + 1, std::string::npos);
+            }
+        }
+    }
+
     // building SRU explain record
     std::string explain_xml 
-        = mp::to_string(
+        = mp_util::to_string(
             "<explain>\n"
             "  <serverInfo protocol='SRU'>\n"
             "  <host>")
-        + package.origin().server_host()
-        + mp::to_string("</host>\n"
+        + host
+        + mp_util::to_string("</host>\n"
             "  <port>")
-        + mp::to_string(package.origin().server_port())
-        + mp::to_string("</port>\n"
+        + port
+        + mp_util::to_string("</port>\n"
             "  <database>")
         + database
-        + mp::to_string("</database>\n"
+        + mp_util::to_string("</database>\n"
             "  </serverInfo>\n"
             "</explain>\n");
     
@@ -327,7 +326,7 @@ bool yf::SRUtoZ3950::Rep::build_simple_explain(mp::Package &package,
 };
 
 
-bool yf::SRUtoZ3950::Rep::build_sru_debug_package(mp::Package &package) const
+bool yf::SRUtoZ3950::Impl::build_sru_debug_package(mp::Package &package) const
 {
     Z_GDU *zgdu_req = package.request().get();
     if  (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
@@ -343,7 +342,7 @@ bool yf::SRUtoZ3950::Rep::build_sru_debug_package(mp::Package &package) const
 }
 
 
-bool yf::SRUtoZ3950::Rep::build_sru_response(mp::Package &package, 
+bool yf::SRUtoZ3950::Impl::build_sru_response(mp::Package &package, 
                                              mp::odr &odr_en,
                                              Z_SOAP *soap,
                                              const Z_SRW_PDU *sru_pdu_res,
@@ -414,7 +413,7 @@ bool yf::SRUtoZ3950::Rep::build_sru_response(mp::Package &package,
 
 
 
- Z_SRW_PDU * yf::SRUtoZ3950::Rep::decode_sru_request(mp::Package &package,
+ Z_SRW_PDU * yf::SRUtoZ3950::Impl::decode_sru_request(mp::Package &package,
                                                      mp::odr &odr_de,
                                                      mp::odr &odr_en,
                                                      Z_SRW_PDU *sru_pdu_res,
@@ -465,7 +464,7 @@ bool yf::SRUtoZ3950::Rep::build_sru_response(mp::Package &package,
 }
 
 bool 
-yf::SRUtoZ3950::Rep::check_sru_query_exists(mp::Package &package, 
+yf::SRUtoZ3950::Impl::check_sru_query_exists(mp::Package &package, 
                                             mp::odr &odr_en,
                                             Z_SRW_PDU *sru_pdu_res, 
                                             Z_SRW_searchRetrieveRequest 
@@ -506,7 +505,7 @@ yf::SRUtoZ3950::Rep::check_sru_query_exists(mp::Package &package,
 
 
 bool 
-yf::SRUtoZ3950::Rep::z3950_init_request(mp::Package &package, 
+yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package, 
                                              const std::string &database) const
 {
     // prepare Z3950 package
@@ -558,7 +557,7 @@ yf::SRUtoZ3950::Rep::z3950_init_request(mp::Package &package,
 }
 
 bool 
-yf::SRUtoZ3950::Rep::z3950_close_request(mp::Package &package) const
+yf::SRUtoZ3950::Impl::z3950_close_request(mp::Package &package) const
 {
     // close SRU package
     package.session().close();
@@ -588,7 +587,7 @@ yf::SRUtoZ3950::Rep::z3950_close_request(mp::Package &package) const
 }
 
 bool 
-yf::SRUtoZ3950::Rep::z3950_search_request(mp::Package &package,  
+yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,  
                                           mp::odr &odr_en,
                                           Z_SRW_PDU *sru_pdu_res,
                                           Z_SRW_searchRetrieveRequest 
@@ -668,7 +667,7 @@ yf::SRUtoZ3950::Rep::z3950_search_request(mp::Package &package,
 }
 
 bool 
-yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, 
+yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package, 
                                            mp::odr &odr_en,
                                            Z_SRW_PDU *sru_pdu_res,
                                            Z_SRW_searchRetrieveRequest 
@@ -878,7 +877,7 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package,
 }
 
 bool 
-yf::SRUtoZ3950::Rep::z3950_scan_request(mp::Package &package,
+yf::SRUtoZ3950::Impl::z3950_scan_request(mp::Package &package,
                                         mp::odr &odr_en,
                                         Z_SRW_PDU *sru_pdu_res,
                                         Z_SRW_scanRequest const *sr_req) const 
@@ -939,7 +938,7 @@ yf::SRUtoZ3950::Rep::z3950_scan_request(mp::Package &package,
     return false;
 }
 
-bool yf::SRUtoZ3950::Rep::z3950_build_query(mp::odr &odr_en, Z_Query *z_query, 
+bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query, 
                                             const SRW_query &query, 
                                             SRW_query_type query_type) const
 {        
@@ -983,7 +982,7 @@ bool yf::SRUtoZ3950::Rep::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
 
 
 std::string 
-yf::SRUtoZ3950::Rep::sru_protocol(const Z_HTTP_Request &http_req) const
+yf::SRUtoZ3950::Impl::sru_protocol(const Z_HTTP_Request &http_req) const
 {
     const std::string mime_urlencoded("application/x-www-form-urlencoded");
     const std::string mime_text_xml("text/xml");
@@ -991,7 +990,7 @@ yf::SRUtoZ3950::Rep::sru_protocol(const Z_HTTP_Request &http_req) const
 
     const std::string http_method(http_req.method);
     const std::string http_type 
-        =  http_header_value(http_req.headers, "Content-Type");
+        =  mp_util::http_header_value(http_req.headers, "Content-Type");
 
     if (http_method == "GET")
         return "SRU GET";
@@ -1009,7 +1008,7 @@ yf::SRUtoZ3950::Rep::sru_protocol(const Z_HTTP_Request &http_req) const
 }
 
 std::string 
-yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const
+yf::SRUtoZ3950::Impl::debug_http(const Z_HTTP_Request &http_req) const
 {
     std::string message("<html>\n<body>\n<h1>"
                         "Metaproxy SRUtoZ3950 filter"
@@ -1022,10 +1021,10 @@ yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const
     message += "<b>Path:   </b> " + std::string(http_req.path) + "<br/>\n";
 
     message += "<b>Content-Type:</b>"
-        + http_header_value(http_req.headers, "Content-Type")
+        + mp_util::http_header_value(http_req.headers, "Content-Type")
         + "<br/>\n";
     message += "<b>Content-Length:</b>"
-        + http_header_value(http_req.headers, "Content-Length")
+        + mp_util::http_header_value(http_req.headers, "Content-Length")
         + "<br/>\n";
     message += "</p>\n";    
     
@@ -1043,7 +1042,7 @@ yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const
     return message;
 }
 
-void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package, 
+void yf::SRUtoZ3950::Impl::http_response(metaproxy_1::Package &package, 
                                         const std::string &content, 
                                         int http_code) const
 {
@@ -1070,7 +1069,7 @@ void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package,
 
 
 Z_ElementSetNames * 
-yf::SRUtoZ3950::Rep::build_esn_from_schema(mp::odr &odr_en, 
+yf::SRUtoZ3950::Impl::build_esn_from_schema(mp::odr &odr_en, 
                                            const char *schema) const
 {
   if (!schema)
@@ -1084,7 +1083,7 @@ yf::SRUtoZ3950::Rep::build_esn_from_schema(mp::odr &odr_en,
 }
 
 int 
-yf::SRUtoZ3950::Rep::z3950_to_srw_diag(mp::odr &odr_en, 
+yf::SRUtoZ3950::Impl::z3950_to_srw_diag(mp::odr &odr_en, 
                                        Z_SRW_searchRetrieveResponse *sru_res,
                                        Z_DefaultDiagFormat *ddf) const
 {