1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "sru_util.hpp"
22 //#include <yaz/wrbuf.h>
23 //#include <yaz/querytowrbuf.h>
28 namespace mp = metaproxy_1;
30 // Doxygen doesn't like mp::gdu, so we use this instead
31 namespace mp_util = metaproxy_1::util;
33 const std::string xmlns_explain("http://explain.z3950.org/dtd/2.0/");
35 bool mp_util::build_sru_debug_package(mp::Package &package)
37 Z_GDU *zgdu_req = package.request().get();
38 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
40 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
41 std::string content = mp_util::http_headers_debug(*http_req);
43 mp_util::http_response(package, content, http_code);
46 package.session().close();
50 mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package)
52 mp_util::SRUServerInfo sruinfo;
54 // getting host and port info
55 sruinfo.host = package.origin().listen_host();
56 sruinfo.port = mp_util::to_string(package.origin().listen_port());
58 // overwriting host and port info if set from HTTP Host header
59 Z_GDU *zgdu_req = package.request().get();
60 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
62 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
65 std::string http_path = http_req->path;
67 // taking out GET parameters
68 std::string::size_type ipath = http_path.rfind("?");
69 if (ipath != std::string::npos)
70 http_path.assign(http_path, 0, ipath);
72 // assign to database name
73 if (http_path.size() > 1){
74 sruinfo.database.assign(http_path, 1, std::string::npos);
77 std::string http_host_address
78 = mp_util::http_header_value(http_req->headers, "Host");
80 std::string::size_type iaddress = http_host_address.rfind(":");
81 if (iaddress != std::string::npos)
83 sruinfo.host.assign(http_host_address, 0, iaddress);
84 sruinfo.port.assign(http_host_address, iaddress + 1,
90 //std::cout << "sruinfo.database " << sruinfo.database << "\n";
91 //std::cout << "sruinfo.host " << sruinfo.host << "\n";
92 //std::cout << "sruinfo.port " << sruinfo.port << "\n";
99 bool mp_util::build_sru_explain(metaproxy_1::Package &package,
100 metaproxy_1::odr &odr_en,
101 Z_SRW_PDU *sru_pdu_res,
102 SRUServerInfo sruinfo,
103 const xmlNode *explain,
104 Z_SRW_explainRequest const *er_req)
107 // building SRU explain record
108 std::string explain_xml;
112 = mp_util::to_string(
113 "<explain xmlns=\"" + xmlns_explain + "\">\n"
114 " <serverInfo protocol='SRU'>\n"
117 + mp_util::to_string("</host>\n"
120 + mp_util::to_string("</port>\n"
123 + mp_util::to_string("</database>\n"
128 // make new XML DOC with given explain node
129 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
130 xmlDocSetRootElement(doc, (xmlNode*)explain);
134 xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
136 explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
140 // z3950'fy recordPacking
141 int record_packing = Z_SRW_recordPacking_XML;
142 if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
143 record_packing = Z_SRW_recordPacking_string;
145 // preparing explain record insert
146 Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
148 // inserting one and only explain record
150 sru_res->record.recordPosition = odr_intdup(odr_en, 1);
151 sru_res->record.recordPacking = record_packing;
152 sru_res->record.recordSchema = (char *)xmlns_explain.c_str();
153 sru_res->record.recordData_len = 1 + explain_xml.size();
154 sru_res->record.recordData_buf
155 = odr_strdupn(odr_en, (const char *)explain_xml.c_str(),
156 1 + explain_xml.size());
162 bool mp_util::build_sru_response(mp::Package &package,
165 const Z_SRW_PDU *sru_pdu_res,
167 const char *stylesheet)
170 // SRU request package translation to Z3950 package
172 // std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
174 // std::cout << "SRU empty\n";
177 Z_GDU *zgdu_req = package.request().get();
178 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
180 Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
181 = odr_en.create_HTTP_Response(package.session(),
182 zgdu_req->u.HTTP_Request,
185 // adding HTTP response code and headers
186 Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
187 //http_res->code = http_code;
189 std::string ctype("text/xml");
191 ctype += "; charset=";
195 z_HTTP_header_add(odr_en,
196 &http_res->headers, "Content-Type", ctype.c_str());
198 // packaging Z_SOAP into HTML response
199 static Z_SOAP_Handler soap_handlers[4] = {
200 {"http://www.loc.gov/zing/srw/", 0,
201 (Z_SOAP_fun) yaz_srw_codec},
202 {"http://www.loc.gov/zing/srw/v1.0/", 0,
203 (Z_SOAP_fun) yaz_srw_codec},
204 {"http://www.loc.gov/zing/srw/update/", 0,
205 (Z_SOAP_fun) yaz_ucp_codec},
210 // empty stylesheet means NO stylesheet
211 if (stylesheet && *stylesheet == '\0')
214 // encoding SRU package
216 soap->u.generic->p = (void*) sru_pdu_res;
218 z_soap_codec_enc_xsl(odr_en, &soap,
219 &http_res->content_buf, &http_res->content_len,
220 soap_handlers, charset, stylesheet);
223 package.response() = zgdu_res;
226 package.session().close();
232 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
235 Z_SRW_PDU *sru_pdu_res,
240 Z_GDU *zgdu_req = package.request().get();
241 Z_SRW_PDU *sru_pdu_req = 0;
243 //assert((zgdu_req->which == Z_GDU_HTTP_Request));
245 //ignoring all non HTTP_Request packages
246 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
250 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
254 // checking if we got a SRU GET/POST/SOAP HTTP package
255 // closing connection if we did not ...
256 if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap,
258 &(sru_pdu_res->u.response->diagnostics),
259 &(sru_pdu_res->u.response->num_diagnostics)))
261 if (sru_pdu_res->u.response->num_diagnostics)
263 //sru_pdu_res = sru_pdu_res_exp;
264 package.session().close();
269 else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap,
274 //sru_pdu_res = sru_pdu_res_exp;
275 package.session().close();
283 mp_util::check_sru_query_exists(mp::Package &package,
285 Z_SRW_PDU *sru_pdu_res,
286 Z_SRW_searchRetrieveRequest const *sr_req)
288 if( (sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql) )
290 yaz_add_srw_diagnostic(odr_en,
291 &(sru_pdu_res->u.response->diagnostics),
292 &(sru_pdu_res->u.response->num_diagnostics),
294 yaz_add_srw_diagnostic(odr_en,
295 &(sru_pdu_res->u.response->diagnostics),
296 &(sru_pdu_res->u.response->num_diagnostics),
297 10, "CQL query is empty");
300 if( (sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql) )
302 yaz_add_srw_diagnostic(odr_en,
303 &(sru_pdu_res->u.response->diagnostics),
304 &(sru_pdu_res->u.response->num_diagnostics),
305 10, "XCQL query is empty");
308 if( (sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf) )
310 yaz_add_srw_diagnostic(odr_en,
311 &(sru_pdu_res->u.response->diagnostics),
312 &(sru_pdu_res->u.response->num_diagnostics),
313 10, "PQF query is empty");
323 mp_util::build_esn_from_schema(mp::odr &odr_en,
329 Z_ElementSetNames *esn
330 = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
331 esn->which = Z_ElementSetNames_generic;
332 esn->u.generic = odr_strdup(odr_en, schema);
337 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
341 switch(srw_pdu.which) {
342 case Z_SRW_searchRetrieve_request:
343 os << " " << "searchRetrieveRequest";
345 Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
349 os << " " << (sr->database);
353 os << " " << *(sr->startRecord);
356 if (sr->maximumRecords)
357 os << " " << *(sr->maximumRecords);
360 if (sr->recordPacking)
361 os << " " << (sr->recordPacking);
365 if (sr->recordSchema)
366 os << " " << (sr->recordSchema);
370 switch (sr->query_type){
371 case Z_SRW_query_type_cql:
374 os << " " << sr->query.cql;
376 case Z_SRW_query_type_xcql:
379 case Z_SRW_query_type_pqf:
382 os << " " << sr->query.pqf;
388 case Z_SRW_searchRetrieve_response:
389 os << " " << "searchRetrieveResponse";
391 Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
394 if (! (sr->num_diagnostics))
397 if (sr->numberOfRecords)
398 os << " " << *(sr->numberOfRecords);
401 //if (sr->num_records)
402 os << " " << (sr->num_records);
405 if (sr->nextRecordPosition)
406 os << " " << *(sr->nextRecordPosition);
413 if (sr->diagnostics && sr->diagnostics->uri)
414 os << " " << (sr->diagnostics->uri);
417 if (sr->diagnostics && sr->diagnostics->message)
418 os << " " << (sr->diagnostics->message);
421 if (sr->diagnostics && sr->diagnostics->details)
422 os << " " << (sr->diagnostics->details);
431 case Z_SRW_explain_request:
432 os << " " << "explainRequest";
434 case Z_SRW_explain_response:
435 os << " " << "explainResponse";
437 case Z_SRW_scan_request:
438 os << " " << "scanRequest";
440 case Z_SRW_scan_response:
441 os << " " << "scanResponse";
443 case Z_SRW_update_request:
444 os << " " << "updateRequest";
446 case Z_SRW_update_response:
447 os << " " << "updateResponse";
450 os << " " << "UNKNOWN";
459 // mp_util::SRU::SRU_protocol_type
460 // mp_util::SRU::protocol(const Z_HTTP_Request &http_req) const
462 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
463 // const std::string mime_text_xml("text/xml");
464 // const std::string mime_soap_xml("application/soap+xml");
466 // const std::string http_method(http_req.method);
467 // const std::string http_type
468 // = mp_util::http_header_value(http_req.headers, "Content-Type");
470 // if (http_method == "GET")
473 // if (http_method == "POST"
474 // && http_type == mime_urlencoded)
477 // if ( http_method == "POST"
478 // && (http_type == mime_text_xml
479 // || http_type == mime_soap_xml))
486 // mp_util::sru_protocol(const Z_HTTP_Request &http_req) const
488 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
489 // const std::string mime_text_xml("text/xml");
490 // const std::string mime_soap_xml("application/soap+xml");
492 // const std::string http_method(http_req.method);
493 // const std::string http_type
494 // = mp_util::http_header_value(http_req.headers, "Content-Type");
496 // if (http_method == "GET")
499 // if (http_method == "POST"
500 // && http_type == mime_urlencoded)
501 // return "SRU POST";
503 // if ( http_method == "POST"
504 // && (http_type == mime_text_xml
505 // || http_type == mime_soap_xml))
506 // return "SRU SOAP";
512 // mp_util::debug_http(const Z_HTTP_Request &http_req) const
514 // std::string message("<html>\n<body>\n<h1>"
515 // "Metaproxy SRUtoZ3950 filter"
518 // message += "<h3>HTTP Info</h3><br/>\n";
519 // message += "<p>\n";
520 // message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
521 // message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
522 // message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
524 // message += "<b>Content-Type:</b>"
525 // + mp_util::http_header_value(http_req.headers, "Content-Type")
527 // message += "<b>Content-Length:</b>"
528 // + mp_util::http_header_value(http_req.headers, "Content-Length")
530 // message += "</p>\n";
532 // message += "<h3>Headers</h3><br/>\n";
533 // message += "<p>\n";
534 // Z_HTTP_Header* header = http_req.headers;
536 // message += "<b>Header: </b> <i>"
537 // + std::string(header->name) + ":</i> "
538 // + std::string(header->value) + "<br/>\n";
539 // header = header->next;
541 // message += "</p>\n";
542 // message += "</body>\n</html>\n";
546 // void mp_util::http_response(metaproxy_1::Package &package,
547 // const std::string &content,
548 // int http_code) const
551 // Z_GDU *zgdu_req = package.request().get();
552 // Z_GDU *zgdu_res = 0;
555 // = odr.create_HTTP_Response(package.session(),
556 // zgdu_req->u.HTTP_Request,
559 // zgdu_res->u.HTTP_Response->content_len = content.size();
560 // zgdu_res->u.HTTP_Response->content_buf
561 // = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
563 // strncpy(zgdu_res->u.HTTP_Response->content_buf,
564 // content.c_str(), zgdu_res->u.HTTP_Response->content_len);
566 // //z_HTTP_header_add(odr, &hres->headers,
567 // // "Content-Type", content_type.c_str());
568 // package.response() = zgdu_res;
574 * c-file-style: "Stroustrup"
575 * indent-tabs-mode: nil
577 * vim: shiftwidth=4 tabstop=8 expandtab