From cac118fe88baae4092e18e55736e173d44aa7ad7 Mon Sep 17 00:00:00 2001 From: Marc Cromme Date: Mon, 2 Oct 2006 13:44:48 +0000 Subject: [PATCH] factorizing http utils and sru utild code out of SRUtoZ3959 filter and into util.hpp and sru_util.hpp --- src/filter_sru_to_z3950.cpp | 124 ++++++++++++++++++++----------------------- src/sru_util.cpp | 35 ++++++++++-- src/sru_util.hpp | 20 ++++++- src/util.cpp | 35 +++++++++++- src/util.hpp | 5 +- 5 files changed, 143 insertions(+), 76 deletions(-) diff --git a/src/filter_sru_to_z3950.cpp b/src/filter_sru_to_z3950.cpp index 03eeb34..f1a18a2 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.18 2006-10-02 12:17:54 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 @@ -39,7 +39,7 @@ namespace metaproxy_1 { 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 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, @@ -238,13 +238,6 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package) const build_sru_response(package, odr_en, soap, sru_pdu_res, charset, stylesheet); return; - - - - - - - } @@ -254,9 +247,6 @@ bool yf::SRUtoZ3950::Impl::build_simple_explain(mp::Package &package, 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)) @@ -333,7 +323,7 @@ bool yf::SRUtoZ3950::Impl::build_sru_debug_package(mp::Package &package) const 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; @@ -982,66 +972,66 @@ bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query, } -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"); +// 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 - = mp_util::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::Impl::debug_http(const Z_HTTP_Request &http_req) const -{ - std::string message("\n\n

" - "Metaproxy SRUtoZ3950 filter" - "

\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("\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 += "Content-Type:" - + mp_util::http_header_value(http_req.headers, "Content-Type") - + "
\n"; - message += "Content-Length:" - + mp_util::http_header_value(http_req.headers, "Content-Length") - + "
\n"; - message += "

\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 += "Content-Type:" +// + mp_util::http_header_value(http_req.headers, "Content-Type") +// + "
\n"; +// message += "Content-Length:" +// + mp_util::http_header_value(http_req.headers, "Content-Length") +// + "
\n"; +// message += "

\n"; - message += "

Headers


\n"; - message += "

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

\n"; - message += "\n\n"; - return message; -} +// message += "

Headers


\n"; +// message += "

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

\n"; +// message += "\n\n"; +// return message; +// } void yf::SRUtoZ3950::Impl::http_response(metaproxy_1::Package &package, const std::string &content, diff --git a/src/sru_util.cpp b/src/sru_util.cpp index c84c918..fe72c4c 100644 --- a/src/sru_util.cpp +++ b/src/sru_util.cpp @@ -1,22 +1,49 @@ -/* $Id: sru_util.cpp,v 1.1 2006-09-26 13:15:33 marc Exp $ +/* $Id: sru_util.cpp,v 1.2 2006-10-02 13:44:48 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details */ #include "sru_util.hpp" -//#include "util.hpp" +#include "util.hpp" //#include //#include #include -//#include +#include namespace mp = metaproxy_1; // Doxygen doesn't like mp::gdu, so we use this instead -namespace mp_sru = metaproxy_1::sru; +namespace mp_util = metaproxy_1::util; + + +mp_util::SRU::SRU_protocol_type +mp_util::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 + = mp_util::http_header_value(http_req.headers, "Content-Type"); + + 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_text_xml + || http_type == mime_soap_xml)) + return SRU_SOAP; + + return SRU_NONE; +} std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) diff --git a/src/sru_util.hpp b/src/sru_util.hpp index fd9ef5a..68317dc 100644 --- a/src/sru_util.hpp +++ b/src/sru_util.hpp @@ -1,4 +1,4 @@ -/* $Id: sru_util.hpp,v 1.1 2006-09-26 13:15:33 marc Exp $ +/* $Id: sru_util.hpp,v 1.2 2006-10-02 13:44:48 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -12,6 +12,7 @@ #include #include +#include namespace std { @@ -21,7 +22,22 @@ namespace std namespace metaproxy_1 { - namespace sru { + namespace util { + + class SRU + { + public: + enum SRU_protocol_type { SRU_NONE, SRU_GET, SRU_POST, SRU_SOAP}; + typedef const int& SRU_query_type; + union SRW_query {char * cql; char * xcql; char * pqf;}; + private: + //bool decode(const Z_HTTP_Request &http_req); + SRU_protocol_type protocol(const Z_HTTP_Request &http_req) const; + private: + SRU_protocol_type m_protocol; + std::string m_charset; + std::string m_stylesheet; + }; } } diff --git a/src/util.cpp b/src/util.cpp index 4615347..0783b1d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,4 +1,4 @@ -/* $Id: util.cpp,v 1.20 2006-09-29 08:42:47 marc Exp $ +/* $Id: util.cpp,v 1.21 2006-10-02 13:44:48 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -34,6 +34,39 @@ std::string mp_util::http_header_value(const Z_HTTP_Header* header, return std::string(); } +std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req) +{ + 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 += "Content-Type:" + + mp_util::http_header_value(http_req.headers, "Content-Type") + + "
\n"; + message += "Content-Length:" + + mp_util::http_header_value(http_req.headers, "Content-Length") + + "
\n"; + message += "

\n"; + + message += "

Headers


\n"; + message += "

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

\n"; + message += "\n\n"; + return message; +} int mp_util::memcmp2(const void *buf1, int len1, diff --git a/src/util.hpp b/src/util.hpp index fbdd367..2d3abd5 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -1,4 +1,4 @@ -/* $Id: util.hpp,v 1.19 2006-09-29 08:42:47 marc Exp $ +/* $Id: util.hpp,v 1.20 2006-10-02 13:44:48 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -14,7 +14,6 @@ #include #include -#include #include #include #include @@ -37,6 +36,8 @@ namespace metaproxy_1 { std::string http_header_value(const Z_HTTP_Header* header, const std::string name); + std::string http_headers_debug(const Z_HTTP_Request &http_req); + int memcmp2(const void *buf1, int len1, const void *buf2, int len2); std::string database_name_normalize(const std::string &s); -- 1.7.10.4