1 /* $Id: sru_util.cpp,v 1.10 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "sru_util.hpp"
25 //#include <yaz/wrbuf.h>
26 //#include <yaz/querytowrbuf.h>
31 namespace mp = metaproxy_1;
33 // Doxygen doesn't like mp::gdu, so we use this instead
34 namespace mp_util = metaproxy_1::util;
36 const std::string xmlns_explain("http://explain.z3950.org/dtd/2.0/");
38 bool mp_util::build_sru_debug_package(mp::Package &package)
40 Z_GDU *zgdu_req = package.request().get();
41 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
43 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
44 std::string content = mp_util::http_headers_debug(*http_req);
46 mp_util::http_response(package, content, http_code);
49 package.session().close();
53 mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package)
55 mp_util::SRUServerInfo sruinfo;
57 // getting host and port info
58 sruinfo.host = package.origin().listen_host();
59 sruinfo.port = mp_util::to_string(package.origin().listen_port());
61 // overwriting host and port info if set from HTTP Host header
62 Z_GDU *zgdu_req = package.request().get();
63 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
65 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
68 std::string http_path = http_req->path;
70 // taking out GET parameters
71 std::string::size_type ipath = http_path.rfind("?");
72 if (ipath != std::string::npos)
73 http_path.assign(http_path, 0, ipath);
75 // assign to database name
76 if (http_path.size() > 1){
77 sruinfo.database.assign(http_path, 1, std::string::npos);
80 std::string http_host_address
81 = mp_util::http_header_value(http_req->headers, "Host");
83 std::string::size_type iaddress = http_host_address.rfind(":");
84 if (iaddress != std::string::npos)
86 sruinfo.host.assign(http_host_address, 0, iaddress);
87 sruinfo.port.assign(http_host_address, iaddress + 1,
93 //std::cout << "sruinfo.database " << sruinfo.database << "\n";
94 //std::cout << "sruinfo.host " << sruinfo.host << "\n";
95 //std::cout << "sruinfo.port " << sruinfo.port << "\n";
102 bool mp_util::build_sru_explain(metaproxy_1::Package &package,
103 metaproxy_1::odr &odr_en,
104 Z_SRW_PDU *sru_pdu_res,
105 SRUServerInfo sruinfo,
106 const xmlNode *explain,
107 Z_SRW_explainRequest const *er_req)
110 // building SRU explain record
111 std::string explain_xml;
115 = mp_util::to_string(
116 "<explain xmlns=\"" + xmlns_explain + "\">\n"
117 " <serverInfo protocol='SRU'>\n"
120 + mp_util::to_string("</host>\n"
123 + mp_util::to_string("</port>\n"
126 + mp_util::to_string("</database>\n"
131 // make new XML DOC with given explain node
132 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
133 xmlDocSetRootElement(doc, (xmlNode*)explain);
137 xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
139 explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
143 // z3950'fy recordPacking
144 int record_packing = Z_SRW_recordPacking_XML;
145 if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
146 record_packing = Z_SRW_recordPacking_string;
148 // preparing explain record insert
149 Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
151 // inserting one and only explain record
153 sru_res->record.recordPosition = odr_intdup(odr_en, 1);
154 sru_res->record.recordPacking = record_packing;
155 sru_res->record.recordSchema = (char *)xmlns_explain.c_str();
156 sru_res->record.recordData_len = 1 + explain_xml.size();
157 sru_res->record.recordData_buf
158 = odr_strdupn(odr_en, (const char *)explain_xml.c_str(),
159 1 + explain_xml.size());
165 bool mp_util::build_sru_response(mp::Package &package,
168 const Z_SRW_PDU *sru_pdu_res,
170 const char *stylesheet)
173 // SRU request package translation to Z3950 package
175 // std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
177 // std::cout << "SRU empty\n";
180 Z_GDU *zgdu_req = package.request().get();
181 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
183 Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
184 = odr_en.create_HTTP_Response(package.session(),
185 zgdu_req->u.HTTP_Request,
188 // adding HTTP response code and headers
189 Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
190 //http_res->code = http_code;
192 std::string ctype("text/xml");
194 ctype += "; charset=";
198 z_HTTP_header_add(odr_en,
199 &http_res->headers, "Content-Type", ctype.c_str());
201 // packaging Z_SOAP into HTML response
202 static Z_SOAP_Handler soap_handlers[4] = {
203 {"http://www.loc.gov/zing/srw/", 0,
204 (Z_SOAP_fun) yaz_srw_codec},
205 {"http://www.loc.gov/zing/srw/v1.0/", 0,
206 (Z_SOAP_fun) yaz_srw_codec},
207 {"http://www.loc.gov/zing/srw/update/", 0,
208 (Z_SOAP_fun) yaz_ucp_codec},
213 // empty stylesheet means NO stylesheet
214 if (stylesheet && *stylesheet == '\0')
217 // encoding SRU package
219 soap->u.generic->p = (void*) sru_pdu_res;
221 z_soap_codec_enc_xsl(odr_en, &soap,
222 &http_res->content_buf, &http_res->content_len,
223 soap_handlers, charset, stylesheet);
226 package.response() = zgdu_res;
229 package.session().close();
235 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
238 Z_SRW_PDU *sru_pdu_res,
243 Z_GDU *zgdu_req = package.request().get();
244 Z_SRW_PDU *sru_pdu_req = 0;
246 //assert((zgdu_req->which == Z_GDU_HTTP_Request));
248 //ignoring all non HTTP_Request packages
249 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
253 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
257 // checking if we got a SRU GET/POST/SOAP HTTP package
258 // closing connection if we did not ...
259 if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap,
261 &(sru_pdu_res->u.response->diagnostics),
262 &(sru_pdu_res->u.response->num_diagnostics)))
264 if (sru_pdu_res->u.response->num_diagnostics)
266 //sru_pdu_res = sru_pdu_res_exp;
267 package.session().close();
272 else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap,
277 //sru_pdu_res = sru_pdu_res_exp;
278 package.session().close();
286 mp_util::check_sru_query_exists(mp::Package &package,
288 Z_SRW_PDU *sru_pdu_res,
289 Z_SRW_searchRetrieveRequest const *sr_req)
291 if( (sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql) )
293 yaz_add_srw_diagnostic(odr_en,
294 &(sru_pdu_res->u.response->diagnostics),
295 &(sru_pdu_res->u.response->num_diagnostics),
297 yaz_add_srw_diagnostic(odr_en,
298 &(sru_pdu_res->u.response->diagnostics),
299 &(sru_pdu_res->u.response->num_diagnostics),
300 10, "CQL query is empty");
303 if( (sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql) )
305 yaz_add_srw_diagnostic(odr_en,
306 &(sru_pdu_res->u.response->diagnostics),
307 &(sru_pdu_res->u.response->num_diagnostics),
308 10, "XCQL query is empty");
311 if( (sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf) )
313 yaz_add_srw_diagnostic(odr_en,
314 &(sru_pdu_res->u.response->diagnostics),
315 &(sru_pdu_res->u.response->num_diagnostics),
316 10, "PQF query is empty");
326 mp_util::build_esn_from_schema(mp::odr &odr_en,
332 Z_ElementSetNames *esn
333 = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
334 esn->which = Z_ElementSetNames_generic;
335 esn->u.generic = odr_strdup(odr_en, schema);
340 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
344 switch(srw_pdu.which) {
345 case Z_SRW_searchRetrieve_request:
346 os << " " << "searchRetrieveRequest";
348 Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
352 os << " " << (sr->database);
356 os << " " << *(sr->startRecord);
359 if (sr->maximumRecords)
360 os << " " << *(sr->maximumRecords);
363 if (sr->recordPacking)
364 os << " " << (sr->recordPacking);
368 if (sr->recordSchema)
369 os << " " << (sr->recordSchema);
373 switch (sr->query_type){
374 case Z_SRW_query_type_cql:
377 os << " " << sr->query.cql;
379 case Z_SRW_query_type_xcql:
382 case Z_SRW_query_type_pqf:
385 os << " " << sr->query.pqf;
391 case Z_SRW_searchRetrieve_response:
392 os << " " << "searchRetrieveResponse";
394 Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
397 if (! (sr->num_diagnostics))
400 if (sr->numberOfRecords)
401 os << " " << *(sr->numberOfRecords);
404 //if (sr->num_records)
405 os << " " << (sr->num_records);
408 if (sr->nextRecordPosition)
409 os << " " << *(sr->nextRecordPosition);
416 if (sr->diagnostics && sr->diagnostics->uri)
417 os << " " << (sr->diagnostics->uri);
420 if (sr->diagnostics && sr->diagnostics->message)
421 os << " " << (sr->diagnostics->message);
424 if (sr->diagnostics && sr->diagnostics->details)
425 os << " " << (sr->diagnostics->details);
434 case Z_SRW_explain_request:
435 os << " " << "explainRequest";
437 case Z_SRW_explain_response:
438 os << " " << "explainResponse";
440 case Z_SRW_scan_request:
441 os << " " << "scanRequest";
443 case Z_SRW_scan_response:
444 os << " " << "scanResponse";
446 case Z_SRW_update_request:
447 os << " " << "updateRequest";
449 case Z_SRW_update_response:
450 os << " " << "updateResponse";
453 os << " " << "UNKNOWN";
462 // mp_util::SRU::SRU_protocol_type
463 // mp_util::SRU::protocol(const Z_HTTP_Request &http_req) const
465 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
466 // const std::string mime_text_xml("text/xml");
467 // const std::string mime_soap_xml("application/soap+xml");
469 // const std::string http_method(http_req.method);
470 // const std::string http_type
471 // = mp_util::http_header_value(http_req.headers, "Content-Type");
473 // if (http_method == "GET")
476 // if (http_method == "POST"
477 // && http_type == mime_urlencoded)
480 // if ( http_method == "POST"
481 // && (http_type == mime_text_xml
482 // || http_type == mime_soap_xml))
489 // mp_util::sru_protocol(const Z_HTTP_Request &http_req) const
491 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
492 // const std::string mime_text_xml("text/xml");
493 // const std::string mime_soap_xml("application/soap+xml");
495 // const std::string http_method(http_req.method);
496 // const std::string http_type
497 // = mp_util::http_header_value(http_req.headers, "Content-Type");
499 // if (http_method == "GET")
502 // if (http_method == "POST"
503 // && http_type == mime_urlencoded)
504 // return "SRU POST";
506 // if ( http_method == "POST"
507 // && (http_type == mime_text_xml
508 // || http_type == mime_soap_xml))
509 // return "SRU SOAP";
515 // mp_util::debug_http(const Z_HTTP_Request &http_req) const
517 // std::string message("<html>\n<body>\n<h1>"
518 // "Metaproxy SRUtoZ3950 filter"
521 // message += "<h3>HTTP Info</h3><br/>\n";
522 // message += "<p>\n";
523 // message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
524 // message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
525 // message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
527 // message += "<b>Content-Type:</b>"
528 // + mp_util::http_header_value(http_req.headers, "Content-Type")
530 // message += "<b>Content-Length:</b>"
531 // + mp_util::http_header_value(http_req.headers, "Content-Length")
533 // message += "</p>\n";
535 // message += "<h3>Headers</h3><br/>\n";
536 // message += "<p>\n";
537 // Z_HTTP_Header* header = http_req.headers;
539 // message += "<b>Header: </b> <i>"
540 // + std::string(header->name) + ":</i> "
541 // + std::string(header->value) + "<br/>\n";
542 // header = header->next;
544 // message += "</p>\n";
545 // message += "</body>\n</html>\n";
549 // void mp_util::http_response(metaproxy_1::Package &package,
550 // const std::string &content,
551 // int http_code) const
554 // Z_GDU *zgdu_req = package.request().get();
555 // Z_GDU *zgdu_res = 0;
558 // = odr.create_HTTP_Response(package.session(),
559 // zgdu_req->u.HTTP_Request,
562 // zgdu_res->u.HTTP_Response->content_len = content.size();
563 // zgdu_res->u.HTTP_Response->content_buf
564 // = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
566 // strncpy(zgdu_res->u.HTTP_Response->content_buf,
567 // content.c_str(), zgdu_res->u.HTTP_Response->content_len);
569 // //z_HTTP_header_add(odr, &hres->headers,
570 // // "Content-Type", content_type.c_str());
571 // package.response() = zgdu_res;
577 * indent-tabs-mode: nil
578 * c-file-style: "stroustrup"
580 * vim: shiftwidth=4 tabstop=8 expandtab