From 3e6cd573da434709745d8ed06e4be45c2f19e283 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Wed, 13 Sep 2006 21:49:34 +0000 Subject: [PATCH] added code for SRU GET/POST/SOAP determination. TODO: there is a side effect in z_HTTP_header_lookup(http_req.headers, "Content-Type") which eats all headers, leaving http_req.headers=0. Needs fixing in YAZ source --- src/filter_sru_to_z3950.cpp | 174 +++++++++++++++++++++++++------------------ src/filter_sru_to_z3950.hpp | 3 +- 2 files changed, 105 insertions(+), 72 deletions(-) diff --git a/src/filter_sru_to_z3950.cpp b/src/filter_sru_to_z3950.cpp index 0718b94..77cfa21 100644 --- a/src/filter_sru_to_z3950.cpp +++ b/src/filter_sru_to_z3950.cpp @@ -1,4 +1,4 @@ -/* $Id: filter_sru_to_z3950.cpp,v 1.2 2006-09-13 14:56:07 marc Exp $ +/* $Id: filter_sru_to_z3950.cpp,v 1.3 2006-09-13 21:49:34 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -39,29 +39,42 @@ namespace metaproxy_1 namespace metaproxy_1 { namespace filter { class SRUtoZ3950::Rep { - friend class SRUtoZ3950; - void process(metaproxy_1::Package & package) const; - //int dummy; + //friend class SRUtoZ3950; + public: + void configure(const xmlNode *xmlnode); + void process(metaproxy_1::Package &package) const; + private: + std::string protocol_type(const Z_HTTP_Request &http_req) const; + std::string debug_http(const Z_HTTP_Request &http_req) const; + void http_response(metaproxy_1::Package &package, + const std::string &content, + int http_code = 200) const; }; } } yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Rep) { - //m_p->dummy = 1; } yf::SRUtoZ3950::~SRUtoZ3950() { // must have a destructor because of boost::scoped_ptr } +void yf::SRUtoZ3950::configure(const xmlNode *xmlnode) +{ + m_p->configure(xmlnode); +} + void yf::SRUtoZ3950::process(mp::Package &package) const { - //std::cout << m_p->protocol_type(package) << "\n"; - //m_p->debug_http_headers(package); m_p->process(package); } +void yf::SRUtoZ3950::Rep::configure(const xmlNode *xmlnode) +{ +} + void yf::SRUtoZ3950::Rep::process(mp::Package &package) const { Z_GDU *zgdu_req = package.request().get(); @@ -71,12 +84,20 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const package.move(); return; } + + + // only working on HTTP_Request packages now Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request; // TODO: SRU package checking and translation to Z3950 package + std::cout << protocol_type(*http_req) << "\n"; + + package.move(); + return; + // int ret_code = 2; /* 2=NOT TAKEN, 1=TAKEN, 0=SOAP TAKEN */ @@ -87,114 +108,125 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const // int num_diagnostic = 0; // mp::odr odr; -// if (!(ret_code = yaz_sru_decode(http_req, &sru_res, +// ret_code = yaz_sru_decode(http_req, &sru_res, // &soap_package, odr, &charset, // &diagnostic, &num_diagnostic))) -// { -// protocol = "SRU GET/POST"; -// } -// else if (!(ret_code = yaz_srw_decode(http_req, &sru_res, +// +// ret_code = yaz_srw_decode(http_req, &sru_res, // &soap_package, odr, &charset))) -// { -// protocol = "SRU SOAP"; -// } -// else -// { -// protocol = "HTTP"; -// } + + + + // sending Z3950 package through pipeline + package.move(); + + + // TODO: Z3950 response parsing and translation to SRU package + Z_HTTP_Response* http_res = 0; + + std::string content = debug_http(*http_req); + int http_code = 200; + + http_response(package, content, http_code); + + //package.response() = zgdu_res; +} + +std::string +yf::SRUtoZ3950::Rep::protocol_type(const Z_HTTP_Request &http_req) const +{ std::string protocol("HTTP"); const std::string mime_text_xml("text/xml"); const std::string mime_soap_xml("application/soap+xml"); const std::string mime_urlencoded("application/x-www-form-urlencoded"); - const std::string http_method(http_req->method); - const std::string http_type(z_HTTP_header_lookup(http_req->headers, - "Content-Type")); - + const std::string http_method(http_req.method); + + //const std::string http_type(z_HTTP_header_lookup(http_req.headers, + // "Content-Type")); + // TODO: there is a sude condition in the above, fix it in YAZ + const std::string http_type("xyz"); if (http_method == "GET") - protocol = "SRU GET"; - else if ( http_method == "POST" + return "SRU GET"; + + if (http_method == "POST" && http_type == mime_urlencoded) - protocol = "SRU POST"; - else if ( http_method == "POST" - && (http_type == mime_text_xml - || http_type == mime_soap_xml)) - protocol = "SRU SOAP"; + return "SRU POST"; - std::cout << "SRUtoZ3950 " << protocol << "\n"; + if ( http_method == "POST" + && (http_type == mime_text_xml + || http_type == mime_soap_xml)) + return "SRU SOAP"; - package.move(); - return; - - + return "HTTP"; +} +std::string +yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const +{ std::string message("\n\n

" "Metaproxy SRUtoZ3950 filter" "

\n"); - + message += "

HTTP Info


\n"; message += "

\n"; - message += "Method: " + std::string(http_req->method) + "
\n"; - message += "Version: " + std::string(http_req->version) + "
\n"; - message += "Path: " + std::string(http_req->path) + "
\n"; + message += "Method: " + std::string(http_req.method) + "
\n"; + message += "Version: " + std::string(http_req.version) + "
\n"; + message += "Path: " + std::string(http_req.path) + "
\n"; message += "Content-Type:" - + std::string(z_HTTP_header_lookup(http_req->headers, "Content-Type")) + + std::string(z_HTTP_header_lookup(http_req.headers, "Content-Type")) + "
\n"; message += "Content-Length:" - + std::string(z_HTTP_header_lookup(http_req->headers, + + std::string(z_HTTP_header_lookup(http_req.headers, "Content-Length")) + "
\n"; message += "

\n"; - + message += "

Headers


\n"; message += "

\n"; - Z_HTTP_Header* header = http_req->headers; + Z_HTTP_Header* header = http_req.headers; while (header){ - message += "Header: " - + std::string(header->name) + ": " - + std::string(header->value) + "
\n"; - header = header->next; + message += "Header: " + + std::string(header->name) + ": " + + std::string(header->value) + "
\n"; + header = header->next; } message += "

\n"; message += "\n\n"; + return message; +} - //std::cout << message << "\n"; - - - - // sending Z3950 package through pipeline - package.move(); - - - // TODO: Z3950 response parsing and translation to SRU package - //Z_HTTP_Response* http_res = 0; - +void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package, + const std::string &content, + int http_code) const +{ + Z_GDU *zgdu_req = package.request().get(); Z_GDU *zgdu_res = 0; - mp::odr odr; + mp::odr odr; zgdu_res = odr.create_HTTP_Response(package.session(), - zgdu_req->u.HTTP_Request, 200); - + zgdu_req->u.HTTP_Request, + http_code); -// zgdu_res->u.HTTP_Response->content_len = message.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, -// message.c_str(), zgdu_res->u.HTTP_Response->content_len); + 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); - // z_HTTP_header_add(o, &hres->headers, - // "Content-Type", content_type.c_str()); - + 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; - } + + static mp::filter::Base* filter_creator() { return new mp::filter::SRUtoZ3950; diff --git a/src/filter_sru_to_z3950.hpp b/src/filter_sru_to_z3950.hpp index 334601b..261beee 100644 --- a/src/filter_sru_to_z3950.hpp +++ b/src/filter_sru_to_z3950.hpp @@ -1,4 +1,4 @@ -/* $Id: filter_sru_to_z3950.hpp,v 1.1 2006-09-13 10:43:24 marc Exp $ +/* $Id: filter_sru_to_z3950.hpp,v 1.2 2006-09-13 21:49:34 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -20,6 +20,7 @@ namespace metaproxy_1 { public: SRUtoZ3950(); ~SRUtoZ3950(); + void configure(const xmlNode *xmlnode); void process(metaproxy_1::Package & package) const; }; } -- 1.7.10.4