factorizing http utils and sru utild code out of SRUtoZ3959 filter and into util...
[metaproxy-moved-to-github.git] / src / filter_sru_to_z3950.cpp
index 7a392d7..f1a18a2 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_sru_to_z3950.cpp,v 1.12 2006-09-22 14:09:27 marc Exp $
+/* $Id: filter_sru_to_z3950.cpp,v 1.19 2006-10-02 13:44:48 marc Exp $
    Copyright (c) 2005-2006, Index Data.
 
    See the LICENSE file for details
@@ -9,6 +9,7 @@
 #include "package.hpp"
 #include "util.hpp"
 #include "gduutil.hpp"
+#include "sru_util.hpp"
 #include "filter_sru_to_z3950.hpp"
 
 #include <yaz/zgdu.h>
 
 
 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:
-            std::string sru_protocol(const Z_HTTP_Request &http_req) const;
+            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, 
                                const std::string &content, 
                                int http_code = 200) const;
             bool build_sru_debug_package(mp::Package &package) const;
+            bool build_simple_explain(mp::Package &package, 
+                                      mp::odr &odr_en,
+                                      Z_SRW_PDU *sru_pdu_res,
+                                      Z_SRW_explainRequest const *er_req) 
+                const;
             bool build_sru_response(mp::Package &package, 
                                     mp::odr &odr_en,
                                     Z_SOAP *soap,
@@ -113,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)
 {
 }
 
@@ -138,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();
 
@@ -173,6 +153,7 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
                                             sru_pdu_res, soap,
                                             charset, stylesheet)))
     {
+        build_simple_explain(package, odr_en, sru_pdu_res, 0);
         build_sru_response(package, odr_en, soap, 
                            sru_pdu_res, charset, stylesheet);
         package.session().close();
@@ -190,15 +171,14 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
     // explain
     if (sru_pdu_req && sru_pdu_req->which == Z_SRW_explain_request)
     {
-        //Z_SRW_searchRetrieveRequest *sr_req = sru_pdu_req->u.request;
-        // sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
-        std::cout << "TODO: implement explain response";
-        
+        Z_SRW_explainRequest *er_req = sru_pdu_req->u.explain_request;
+        //sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
+
+        build_simple_explain(package, odr_en, sru_pdu_res, er_req);
     }
-    
 
     // searchRetrieve
-    if (sru_pdu_req 
+    else if (sru_pdu_req 
         && sru_pdu_req->which == Z_SRW_searchRetrieve_request
         && sru_pdu_req->u.request)
     {
@@ -258,22 +238,92 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
     build_sru_response(package, odr_en, soap, 
                        sru_pdu_res, charset, stylesheet);
     return;
+}
 
 
+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 && er_req->recordPacking && 's' == *(er_req->recordPacking))
+        record_packing = Z_SRW_recordPacking_string;
 
+    // getting database info
+    std::string database("Default");
+    if (er_req && 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_util::to_string(
+            "<explain>\n"
+            "  <serverInfo protocol='SRU'>\n"
+            "  <host>")
+        + host
+        + mp_util::to_string("</host>\n"
+            "  <port>")
+        + port
+        + mp_util::to_string("</port>\n"
+            "  <database>")
+        + database
+        + mp_util::to_string("</database>\n"
+            "  </serverInfo>\n"
+            "</explain>\n");
+    
+    
+    // preparing explain record insert
+    Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
+    //sru_res->record 
+    //    = (Z_SRW_record *) odr_malloc(odr_en, sizeof(Z_SRW_record));
+    
+    // inserting one and only explain record
+    
+    sru_res->record.recordPosition = odr_intdup(odr_en, 1);
+    sru_res->record.recordPacking = record_packing;
+    sru_res->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
+    sru_res->record.recordData_len = 1 + explain_xml.size();
+    sru_res->record.recordData_buf
+        = odr_strdupn(odr_en, (const char *)explain_xml.c_str(), 
+                      1 + explain_xml.size());
 
-bool yf::SRUtoZ3950::Rep::build_sru_debug_package(mp::Package &package) const
+    return true;
+};
+
+
+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)
     {    
         Z_HTTP_Request* http_req =  zgdu_req->u.HTTP_Request;
-        std::string content = debug_http(*http_req);
+        std::string content = mp_util::http_headers_debug(*http_req);
         int http_code = 400;    
         http_response(package, content, http_code);
         return true;
@@ -283,7 +333,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,
@@ -354,7 +404,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,
@@ -405,9 +455,11 @@ bool yf::SRUtoZ3950::Rep::build_sru_response(mp::Package &package,
 }
 
 bool 
-yf::SRUtoZ3950::Rep::check_sru_query_exists(mp::Package &package, mp::odr &odr_en,
+yf::SRUtoZ3950::Impl::check_sru_query_exists(mp::Package &package, 
+                                            mp::odr &odr_en,
                                             Z_SRW_PDU *sru_pdu_res, 
-                                            Z_SRW_searchRetrieveRequest const *sr_req)
+                                            Z_SRW_searchRetrieveRequest 
+                                                              const *sr_req)
     const
 {
     if( (sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql) )
@@ -444,7 +496,7 @@ yf::SRUtoZ3950::Rep::check_sru_query_exists(mp::Package &package, mp::odr &odr_e
 
 
 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
@@ -496,7 +548,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();
@@ -526,7 +578,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 
@@ -606,7 +658,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 
@@ -618,7 +670,7 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package,
         return false;
 
     
-    // no need to work if nobody wants records seen ..
+    // no need to work if nobody wants record ..
     if (!(sr_req->maximumRecords) || 0 == *(sr_req->maximumRecords))
         return true;
 
@@ -816,7 +868,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 
@@ -877,7 +929,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
 {        
@@ -920,68 +972,68 @@ 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
-{
-    const std::string mime_urlencoded("application/x-www-form-urlencoded");
-    const std::string mime_text_xml("text/xml");
-    const std::string mime_soap_xml("application/soap+xml");
+// std::string 
+// 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");
+//     const std::string mime_soap_xml("application/soap+xml");
 
-    const std::string http_method(http_req.method);
-    const std::string http_type 
-        =  http_header_value(http_req.headers, "Content-Type");
+//     const std::string http_method(http_req.method);
+//     const std::string http_type 
+//         =  mp_util::http_header_value(http_req.headers, "Content-Type");
 
-    if (http_method == "GET")
-        return "SRU GET";
+//     if (http_method == "GET")
+//         return "SRU GET";
 
-    if (http_method == "POST"
-              && http_type  == mime_urlencoded)
-        return "SRU POST";
+//     if (http_method == "POST"
+//               && http_type  == mime_urlencoded)
+//         return "SRU POST";
     
-    if ( http_method == "POST"
-         && (http_type  == mime_text_xml
-             || http_type  == mime_soap_xml))
-        return "SRU SOAP";
-
-    return "HTTP";
-}
-
-std::string 
-yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const
-{
-    std::string message("<html>\n<body>\n<h1>"
-                        "Metaproxy SRUtoZ3950 filter"
-                        "</h1>\n");
+//     if ( http_method == "POST"
+//          && (http_type  == mime_text_xml
+//              || http_type  == mime_soap_xml))
+//         return "SRU SOAP";
+
+//     return "HTTP";
+// }
+
+// std::string 
+// yf::SRUtoZ3950::Impl::debug_http(const Z_HTTP_Request &http_req) const
+// {
+//     std::string message("<html>\n<body>\n<h1>"
+//                         "Metaproxy SRUtoZ3950 filter"
+//                         "</h1>\n");
     
-    message += "<h3>HTTP Info</h3><br/>\n";
-    message += "<p>\n";
-    message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
-    message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
-    message += "<b>Path:   </b> " + std::string(http_req.path) + "<br/>\n";
-
-    message += "<b>Content-Type:</b>"
-        + http_header_value(http_req.headers, "Content-Type")
-        + "<br/>\n";
-    message += "<b>Content-Length:</b>"
-        + http_header_value(http_req.headers, "Content-Length")
-        + "<br/>\n";
-    message += "</p>\n";    
+//     message += "<h3>HTTP Info</h3><br/>\n";
+//     message += "<p>\n";
+//     message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
+//     message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
+//     message += "<b>Path:   </b> " + std::string(http_req.path) + "<br/>\n";
+
+//     message += "<b>Content-Type:</b>"
+//         + mp_util::http_header_value(http_req.headers, "Content-Type")
+//         + "<br/>\n";
+//     message += "<b>Content-Length:</b>"
+//         + mp_util::http_header_value(http_req.headers, "Content-Length")
+//         + "<br/>\n";
+//     message += "</p>\n";    
     
-    message += "<h3>Headers</h3><br/>\n";
-    message += "<p>\n";    
-    Z_HTTP_Header* header = http_req.headers;
-    while (header){
-        message += "<b>Header: </b> <i>" 
-            + std::string(header->name) + ":</i> "
-            + std::string(header->value) + "<br/>\n";
-        header = header->next;
-    }
-    message += "</p>\n";    
-    message += "</body>\n</html>\n";
-    return message;
-}
-
-void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package, 
+//     message += "<h3>Headers</h3><br/>\n";
+//     message += "<p>\n";    
+//     Z_HTTP_Header* header = http_req.headers;
+//     while (header){
+//         message += "<b>Header: </b> <i>" 
+//             + std::string(header->name) + ":</i> "
+//             + std::string(header->value) + "<br/>\n";
+//         header = header->next;
+//     }
+//     message += "</p>\n";    
+//     message += "</body>\n</html>\n";
+//     return message;
+// }
+
+void yf::SRUtoZ3950::Impl::http_response(metaproxy_1::Package &package, 
                                         const std::string &content, 
                                         int http_code) const
 {
@@ -1007,7 +1059,9 @@ void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package,
 }
 
 
-Z_ElementSetNames * yf::SRUtoZ3950::Rep::build_esn_from_schema(mp::odr &odr_en, const char *schema) const
+Z_ElementSetNames * 
+yf::SRUtoZ3950::Impl::build_esn_from_schema(mp::odr &odr_en, 
+                                           const char *schema) const
 {
   if (!schema)
         return 0;
@@ -1019,8 +1073,10 @@ Z_ElementSetNames * yf::SRUtoZ3950::Rep::build_esn_from_schema(mp::odr &odr_en,
     return esn; 
 }
 
-int yf::SRUtoZ3950::Rep::z3950_to_srw_diag(mp::odr &odr_en, Z_SRW_searchRetrieveResponse *sru_res,
-                                           Z_DefaultDiagFormat *ddf) const
+int 
+yf::SRUtoZ3950::Impl::z3950_to_srw_diag(mp::odr &odr_en, 
+                                       Z_SRW_searchRetrieveResponse *sru_res,
+                                       Z_DefaultDiagFormat *ddf) const
 {
     int bib1_code = *ddf->condition;
     if (bib1_code == 109)