X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_sru_to_z3950.cpp;h=9c1a12a65176e16013035ef2937d6b94e9b8d17e;hb=a911db8ff225da3d204c6e9371353895225ae231;hp=76c5df8702f9f45e42123465436c1e0245eca59e;hpb=c93d8c8db5ae24e01775060677f8e8e9c2afff34;p=metaproxy-moved-to-github.git diff --git a/src/filter_sru_to_z3950.cpp b/src/filter_sru_to_z3950.cpp index 76c5df8..9c1a12a 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.15 2006-09-29 08:42:47 marc Exp $ +/* $Id: filter_sru_to_z3950.cpp,v 1.22 2006-10-10 14:20:16 marc Exp $ Copyright (c) 2005-2006, Index Data. See the LICENSE file for details @@ -22,7 +22,7 @@ #include #include #include - +#include namespace mp = metaproxy_1; namespace mp_util = metaproxy_1::util; @@ -31,19 +31,19 @@ namespace yf = mp::filter; 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; + //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, @@ -90,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) { } @@ -115,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(); @@ -150,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(); @@ -214,7 +218,14 @@ void yf::SRUtoZ3950::Rep::process(mp::Package &package) const sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_scan_response); - if (z3950_init_request(package)) + // we do not do scan at the moment, therefore issuing a diagnostic + yaz_add_srw_diagnostic(odr_en, + &(sru_pdu_res->u.scan_response->diagnostics), + &(sru_pdu_res->u.scan_response->num_diagnostics), + 4, "scan"); + + // to be used when we do scan + if (false && z3950_init_request(package)) { z3950_scan_request(package, odr_en, sru_pdu_res, sr_req); z3950_close_request(package); @@ -234,43 +245,58 @@ 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::Rep::build_simple_explain(mp::Package &package, +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->recordPacking && 's' == *(er_req->recordPacking)) + 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->database) + 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( "\n" " \n" " ") - + package.origin().server_host() + + host + mp_util::to_string("\n" " ") - + mp_util::to_string(package.origin().server_port()) + + port + mp_util::to_string("\n" " ") + database @@ -298,15 +324,15 @@ bool yf::SRUtoZ3950::Rep::build_simple_explain(mp::Package &package, }; -bool yf::SRUtoZ3950::Rep::build_sru_debug_package(mp::Package &package) const +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); + mp_util::http_response(package, content, http_code); return true; } package.session().close(); @@ -314,7 +340,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, @@ -385,7 +411,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, @@ -436,7 +462,7 @@ bool yf::SRUtoZ3950::Rep::build_sru_response(mp::Package &package, } bool -yf::SRUtoZ3950::Rep::check_sru_query_exists(mp::Package &package, +yf::SRUtoZ3950::Impl::check_sru_query_exists(mp::Package &package, mp::odr &odr_en, Z_SRW_PDU *sru_pdu_res, Z_SRW_searchRetrieveRequest @@ -477,7 +503,7 @@ yf::SRUtoZ3950::Rep::check_sru_query_exists(mp::Package &package, 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 @@ -529,7 +555,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(); @@ -559,12 +585,15 @@ 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 const *sr_req) const { + + assert(sru_pdu_res->u.response); + Package z3950_package(package.session(), package.origin()); z3950_package.copy_filter(package); @@ -593,7 +622,10 @@ yf::SRUtoZ3950::Rep::z3950_search_request(mp::Package &package, (const SRW_query&)sr_req->query, sr_req->query_type)) { - //send_to_srw_client_error(7, "query"); + yaz_add_srw_diagnostic(odr_en, + &(sru_pdu_res->u.response->diagnostics), + &(sru_pdu_res->u.response->num_diagnostics), + 7, "query"); return false; } @@ -639,13 +671,14 @@ 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 const *sr_req) const { + assert(sru_pdu_res->u.response); if (!sr_req) return false; @@ -655,13 +688,6 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, if (!(sr_req->maximumRecords) || 0 == *(sr_req->maximumRecords)) return true; - // creating Z3950 package - Package z3950_package(package.session(), package.origin()); - z3950_package.copy_filter(package); - Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_presentRequest); - - assert(apdu->u.presentRequest); - bool send_z3950_present = true; // recordXPath unsupported. @@ -695,12 +721,16 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, 80, 0); } - // start record requested larger than number of records + // start record requested negative, or larger than number of records if (sr_req->startRecord - && sru_pdu_res->u.response->numberOfRecords - && *(sr_req->startRecord) > *(sru_pdu_res->u.response->numberOfRecords)) + && + ((*(sr_req->startRecord) < 0) // negative + || + (sru_pdu_res->u.response->numberOfRecords //out of range + && *(sr_req->startRecord) + > *(sru_pdu_res->u.response->numberOfRecords)) + )) { - // = *(sr_req->startRecord); send_z3950_present = false; yaz_add_srw_diagnostic(odr_en, &(sru_pdu_res->u.response->diagnostics), @@ -708,11 +738,28 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, 61, 0); } + // maximumRecords requested negative + if (sr_req->maximumRecords + && *(sr_req->maximumRecords) < 0) + + { + send_z3950_present = false; + yaz_add_srw_diagnostic(odr_en, + &(sru_pdu_res->u.response->diagnostics), + &(sru_pdu_res->u.response->num_diagnostics), + 6, "maximumRecords"); + } + // exit on all these above diagnostics if (!send_z3950_present) return false; // now packaging the z3950 present request + Package z3950_package(package.session(), package.origin()); + z3950_package.copy_filter(package); + Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_presentRequest); + + assert(apdu->u.presentRequest); // z3950'fy start record position if (sr_req->startRecord) @@ -722,9 +769,13 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, *(apdu->u.presentRequest->resultSetStartPoint) = 1; // z3950'fy number of records requested + // protect against requesting records out of range if (sr_req->maximumRecords) *(apdu->u.presentRequest->numberOfRecordsRequested) - = *(sr_req->maximumRecords); + = std::min(*(sr_req->maximumRecords), + *(sru_pdu_res->u.response->numberOfRecords) + - *(apdu->u.presentRequest->resultSetStartPoint) + + 1); // z3950'fy recordPacking int record_packing = Z_SRW_recordPacking_XML; @@ -739,11 +790,13 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, if (sr_req->recordSchema) { apdu->u.presentRequest->recordComposition - = (Z_RecordComposition *) odr_malloc(odr_en, sizeof(Z_RecordComposition)); + = (Z_RecordComposition *) + odr_malloc(odr_en, sizeof(Z_RecordComposition)); apdu->u.presentRequest->recordComposition->which = Z_RecordComp_simple; apdu->u.presentRequest->recordComposition->u.simple - = build_esn_from_schema(odr_en, (const char *) sr_req->recordSchema); + = build_esn_from_schema(odr_en, + (const char *) sr_req->recordSchema); } // z3950'fy time to live - flagged as diagnostics above @@ -783,7 +836,8 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, && pr->records->u.nonSurrogateDiagnostic) { //int http_code = - z3950_to_srw_diag(odr_en, sru_res, pr->records->u.nonSurrogateDiagnostic); + z3950_to_srw_diag(odr_en, sru_res, + pr->records->u.nonSurrogateDiagnostic); return false; } @@ -796,13 +850,19 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, sru_res->records = (Z_SRW_record *) odr_malloc(odr_en, - sru_res->num_records *sizeof(Z_SRW_record)); + sru_res->num_records + * sizeof(Z_SRW_record)); + // srw'fy nextRecordPosition - if (pr->nextResultSetPosition) + // next position never zero or behind the last z3950 record + if (pr->nextResultSetPosition + && *(pr->nextResultSetPosition) > 0 + && *(pr->nextResultSetPosition) + <= *(sru_pdu_res->u.response->numberOfRecords)) sru_res->nextRecordPosition = odr_intdup(odr_en, *(pr->nextResultSetPosition)); - + // inserting all records for (int i = 0; i < sru_res->num_records; i++) { @@ -810,7 +870,8 @@ yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package, = pr->records->u.databaseOrSurDiagnostics->records[i]; sru_res->records[i].recordPosition - = odr_intdup(odr_en, i + *(apdu->u.presentRequest->resultSetStartPoint)); + = odr_intdup(odr_en, + i + *(apdu->u.presentRequest->resultSetStartPoint)); sru_res->records[i].recordPacking = record_packing; @@ -849,11 +910,13 @@ 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 { + assert(sru_pdu_res->u.scan_response); + Package z3950_package(package.session(), package.origin()); z3950_package.copy_filter(package); //mp::odr odr_en(ODR_ENCODE); @@ -910,7 +973,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 { @@ -953,95 +1016,95 @@ 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 - = 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::Rep::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; -} - -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; - zgdu_res - = odr.create_HTTP_Response(package.session(), - zgdu_req->u.HTTP_Request, - http_code); +// 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, +// 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); +// 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); +// 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; -} +// //z_HTTP_header_add(odr, &hres->headers, +// // "Content-Type", content_type.c_str()); +// package.response() = zgdu_res; +// } Z_ElementSetNames * -yf::SRUtoZ3950::Rep::build_esn_from_schema(mp::odr &odr_en, +yf::SRUtoZ3950::Impl::build_esn_from_schema(mp::odr &odr_en, const char *schema) const { if (!schema) @@ -1055,7 +1118,7 @@ yf::SRUtoZ3950::Rep::build_esn_from_schema(mp::odr &odr_en, } int -yf::SRUtoZ3950::Rep::z3950_to_srw_diag(mp::odr &odr_en, +yf::SRUtoZ3950::Impl::z3950_to_srw_diag(mp::odr &odr_en, Z_SRW_searchRetrieveResponse *sru_res, Z_DefaultDiagFormat *ddf) const { @@ -1081,7 +1144,7 @@ static mp::filter::Base* filter_creator() extern "C" { struct metaproxy_1_filter_struct metaproxy_1_filter_sru_to_z3950 = { 0, - "SRUtoZ3950", + "sru_z3950", filter_creator }; }