X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fsru_util.cpp;h=213b271610f12348cf4c2d60df65308e9eb332fa;hb=b6ea100d3fb9f64ca9fd88606c77ba1d715224ea;hp=575acc63e08cb6753dbd5e5da0bdc5ccf22076cc;hpb=474b0a3096b1bf37f2180efeb94ee7f69cf35b17;p=metaproxy-moved-to-github.git diff --git a/src/sru_util.cpp b/src/sru_util.cpp index 575acc6..213b271 100644 --- a/src/sru_util.cpp +++ b/src/sru_util.cpp @@ -1,14 +1,23 @@ -/* $Id: sru_util.cpp,v 1.8 2007-02-08 09:22:32 marc Exp $ - Copyright (c) 2005-2007, Index Data. - - See the LICENSE file for details +/* This file is part of Metaproxy. + Copyright (C) 2005-2011 Index Data + +Metaproxy is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 2, or (at your option) any later +version. + +Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "sru_util.hpp" -#include "util.hpp" - -//#include -//#include +#include #include #include @@ -23,7 +32,7 @@ const std::string xmlns_explain("http://explain.z3950.org/dtd/2.0/"); bool mp_util::build_sru_debug_package(mp::Package &package) { Z_GDU *zgdu_req = package.request().get(); - if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request) + if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request) { Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request; std::string content = mp_util::http_headers_debug(*http_req); @@ -40,12 +49,12 @@ mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package) mp_util::SRUServerInfo sruinfo; // getting host and port info - sruinfo.host = package.origin().listen_host(); - sruinfo.port = mp_util::to_string(package.origin().listen_port()); + sruinfo.host = "localhost"; + sruinfo.port = "80"; // 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) + if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request) { Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request; if (http_req) @@ -58,9 +67,8 @@ mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package) http_path.assign(http_path, 0, ipath); // assign to database name - if (http_path.size() > 1){ + if (http_path.size() > 1) sruinfo.database.assign(http_path, 1, std::string::npos); - } std::string http_host_address = mp_util::http_header_value(http_req->headers, "Host"); @@ -83,7 +91,6 @@ mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package) } - bool mp_util::build_sru_explain(metaproxy_1::Package &package, metaproxy_1::odr &odr_en, Z_SRW_PDU *sru_pdu_res, @@ -95,7 +102,8 @@ bool mp_util::build_sru_explain(metaproxy_1::Package &package, // building SRU explain record std::string explain_xml; - if (explain == 0){ + if (explain == 0) + { explain_xml = mp_util::to_string( "\n" @@ -112,7 +120,8 @@ bool mp_util::build_sru_explain(metaproxy_1::Package &package, " \n" "\n"); } - else { + else + { // make new XML DOC with given explain node xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0"); xmlDocSetRootElement(doc, (xmlNode*)explain); @@ -144,15 +153,15 @@ bool mp_util::build_sru_explain(metaproxy_1::Package &package, 1 + explain_xml.size()); return true; -}; +} bool mp_util::build_sru_response(mp::Package &package, - mp::odr &odr_en, - Z_SOAP *soap, - const Z_SRW_PDU *sru_pdu_res, - char *charset, - const char *stylesheet) + mp::odr &odr_en, + Z_SOAP *soap, + const Z_SRW_PDU *sru_pdu_res, + char *charset, + const char *stylesheet) { // SRU request package translation to Z3950 package @@ -175,7 +184,8 @@ bool mp_util::build_sru_response(mp::Package &package, //http_res->code = http_code; std::string ctype("text/xml"); - if (charset){ + if (charset) + { ctype += "; charset="; ctype += charset; } @@ -183,33 +193,30 @@ bool mp_util::build_sru_response(mp::Package &package, z_HTTP_header_add(odr_en, &http_res->headers, "Content-Type", ctype.c_str()); - // packaging Z_SOAP into HTML response - static Z_SOAP_Handler soap_handlers[4] = { - {"http://www.loc.gov/zing/srw/", 0, - (Z_SOAP_fun) yaz_srw_codec}, - {"http://www.loc.gov/zing/srw/v1.0/", 0, - (Z_SOAP_fun) yaz_srw_codec}, - {"http://www.loc.gov/zing/srw/update/", 0, - (Z_SOAP_fun) yaz_ucp_codec}, - {0, 0, 0} - }; - - - // empty stylesheet means NO stylesheet - if (stylesheet && *stylesheet == '\0') - stylesheet = 0; + // packaging Z_SOAP into HTML response + static Z_SOAP_Handler soap_handlers[4] = { + {(char *)YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec}, + {(char *)YAZ_XMLNS_SRU_v1_0, 0, (Z_SOAP_fun) yaz_srw_codec}, + {(char *)YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec}, + {0, 0, 0} + }; + + + // empty stylesheet means NO stylesheet + if (stylesheet && *stylesheet == '\0') + stylesheet = 0; - // encoding SRU package + // encoding SRU package - soap->u.generic->p = (void*) sru_pdu_res; - //int ret = - z_soap_codec_enc_xsl(odr_en, &soap, - &http_res->content_buf, &http_res->content_len, - soap_handlers, charset, stylesheet); + soap->u.generic->p = (void*) sru_pdu_res; + //int ret = + z_soap_codec_enc_xsl(odr_en, &soap, + &http_res->content_buf, &http_res->content_len, + soap_handlers, charset, stylesheet); - package.response() = zgdu_res; - return true; + package.response() = zgdu_res; + return true; } package.session().close(); return false; @@ -217,13 +224,13 @@ bool mp_util::build_sru_response(mp::Package &package, - Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package, - mp::odr &odr_de, - mp::odr &odr_en, - Z_SRW_PDU *sru_pdu_res, - Z_SOAP *&soap, - char *charset, - char *stylesheet) +Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package, + mp::odr &odr_de, + mp::odr &odr_en, + Z_SRW_PDU *sru_pdu_res, + Z_SOAP **soap, + char *charset, + char *stylesheet) { Z_GDU *zgdu_req = package.request().get(); Z_SRW_PDU *sru_pdu_req = 0; @@ -231,7 +238,8 @@ bool mp_util::build_sru_response(mp::Package &package, //assert((zgdu_req->which == Z_GDU_HTTP_Request)); //ignoring all non HTTP_Request packages - if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){ + if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)) + { return 0; } @@ -241,7 +249,7 @@ bool mp_util::build_sru_response(mp::Package &package, // checking if we got a SRU GET/POST/SOAP HTTP package // closing connection if we did not ... - if (0 == yaz_sru_decode(http_req, &sru_pdu_req, &soap, + if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap, odr_de, &charset, &(sru_pdu_res->u.response->diagnostics), &(sru_pdu_res->u.response->num_diagnostics))) @@ -254,7 +262,7 @@ bool mp_util::build_sru_response(mp::Package &package, } return sru_pdu_req; } - else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, &soap, + else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap, odr_de, &charset)) return sru_pdu_req; else @@ -273,47 +281,48 @@ mp_util::check_sru_query_exists(mp::Package &package, Z_SRW_PDU *sru_pdu_res, Z_SRW_searchRetrieveRequest const *sr_req) { - if( (sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql) ) + if ((sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql)) { yaz_add_srw_diagnostic(odr_en, &(sru_pdu_res->u.response->diagnostics), &(sru_pdu_res->u.response->num_diagnostics), - 7, "query"); + YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, + "query"); yaz_add_srw_diagnostic(odr_en, &(sru_pdu_res->u.response->diagnostics), &(sru_pdu_res->u.response->num_diagnostics), - 10, "CQL query is empty"); + YAZ_SRW_QUERY_SYNTAX_ERROR, + "CQL query is empty"); return false; } - if( (sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql) ) + if ((sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql)) { - yaz_add_srw_diagnostic(odr_en, + yaz_add_srw_diagnostic(odr_en, &(sru_pdu_res->u.response->diagnostics), &(sru_pdu_res->u.response->num_diagnostics), - 10, "XCQL query is empty"); - return false; - } - if( (sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf) ) + YAZ_SRW_QUERY_SYNTAX_ERROR, + "XCQL query is empty"); + return false; + } + if ((sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf)) { yaz_add_srw_diagnostic(odr_en, &(sru_pdu_res->u.response->diagnostics), &(sru_pdu_res->u.response->num_diagnostics), - 10, "PQF query is empty"); + YAZ_SRW_QUERY_SYNTAX_ERROR, + "PQF query is empty"); return false; } return true; -}; - - +} Z_ElementSetNames * -mp_util::build_esn_from_schema(mp::odr &odr_en, - const char *schema) +mp_util::build_esn_from_schema(mp::odr &odr_en, const char *schema) { - if (!schema) + if (!schema) return 0; - + Z_ElementSetNames *esn = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames)); esn->which = Z_ElementSetNames_generic; @@ -326,7 +335,8 @@ std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) { os << "SRU"; - switch(srw_pdu.which) { + switch (srw_pdu.which) + { case Z_SRW_searchRetrieve_request: os << " " << "searchRetrieveRequest"; { @@ -441,126 +451,12 @@ std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) return os; } - - - -// 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::string -// 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 "HTTP"; -// } - -// std::string -// mp_util::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 += "

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 mp_util::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; -// 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; -// } - /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil - * c-file-style: "stroustrup" * End: * vim: shiftwidth=4 tabstop=8 expandtab */ +