factorizing HTTP specific code out of SRU2Z3950 filter into utils.hpp
[metaproxy-moved-to-github.git] / src / util.cpp
index e177410..df9984c 100644 (file)
-/* $Id: util.cpp,v 1.19 2006-08-30 12:27:34 adam Exp $
+/* $Id: util.cpp,v 1.22 2006-10-03 07:57:40 marc Exp $
    Copyright (c) 2005-2006, Index Data.
 
    See the LICENSE file for details
  */
 
 #include "config.hpp"
+#include "util.hpp"
 
 #include <yaz/odr.h>
 #include <yaz/pquery.h>
 #include <yaz/otherinfo.h>
 #include <yaz/querytowrbuf.h> // for yaz_query_to_wrbuf()
-#include "util.hpp"
 
-//#include <iostream>
+#include <iostream>
 
 namespace mp = metaproxy_1;
 
 // Doxygen doesn't like mp::util, so we use this instead
 namespace mp_util = metaproxy_1::util;
 
+
+std::string mp_util::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();
+}
+    
+std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req)
+{
+    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>"
+        + 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 mp_util::http_response(metaproxy_1::Package &package, 
+                     const std::string &content, 
+                     int http_code)
+{
+
+    Z_GDU *zgdu_req = package.request().get(); 
+    Z_GDU *zgdu_res = 0; 
+    mp::odr odr;
+    zgdu_res 
+       = odr.create_HTTP_Response(package.session(), 
+                                  zgdu_req->u.HTTP_Request, 
+                                  http_code);
+        
+    zgdu_res->u.HTTP_Response->content_len = content.size();
+    zgdu_res->u.HTTP_Response->content_buf 
+        = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
+    
+    strncpy(zgdu_res->u.HTTP_Response->content_buf, 
+            content.c_str(),  zgdu_res->u.HTTP_Response->content_len);
+    
+    //z_HTTP_header_add(odr, &hres->headers,
+    //                  "Content-Type", content_type.c_str());
+    package.response() = zgdu_res;
+}
+
+
 int mp_util::memcmp2(const void *buf1, int len1,
                      const void *buf2, int len2)
 {