1 /* $Id: sru_util.cpp,v 1.9 2007-03-20 07:20:16 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 See the LICENSE file for details
7 #include "sru_util.hpp"
10 //#include <yaz/wrbuf.h>
11 //#include <yaz/querytowrbuf.h>
16 namespace mp = metaproxy_1;
18 // Doxygen doesn't like mp::gdu, so we use this instead
19 namespace mp_util = metaproxy_1::util;
21 const std::string xmlns_explain("http://explain.z3950.org/dtd/2.0/");
23 bool mp_util::build_sru_debug_package(mp::Package &package)
25 Z_GDU *zgdu_req = package.request().get();
26 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
28 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
29 std::string content = mp_util::http_headers_debug(*http_req);
31 mp_util::http_response(package, content, http_code);
34 package.session().close();
38 mp_util::SRUServerInfo mp_util::get_sru_server_info(mp::Package &package)
40 mp_util::SRUServerInfo sruinfo;
42 // getting host and port info
43 sruinfo.host = package.origin().listen_host();
44 sruinfo.port = mp_util::to_string(package.origin().listen_port());
46 // overwriting host and port info if set from HTTP Host header
47 Z_GDU *zgdu_req = package.request().get();
48 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
50 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
53 std::string http_path = http_req->path;
55 // taking out GET parameters
56 std::string::size_type ipath = http_path.rfind("?");
57 if (ipath != std::string::npos)
58 http_path.assign(http_path, 0, ipath);
60 // assign to database name
61 if (http_path.size() > 1){
62 sruinfo.database.assign(http_path, 1, std::string::npos);
65 std::string http_host_address
66 = mp_util::http_header_value(http_req->headers, "Host");
68 std::string::size_type iaddress = http_host_address.rfind(":");
69 if (iaddress != std::string::npos)
71 sruinfo.host.assign(http_host_address, 0, iaddress);
72 sruinfo.port.assign(http_host_address, iaddress + 1,
78 //std::cout << "sruinfo.database " << sruinfo.database << "\n";
79 //std::cout << "sruinfo.host " << sruinfo.host << "\n";
80 //std::cout << "sruinfo.port " << sruinfo.port << "\n";
87 bool mp_util::build_sru_explain(metaproxy_1::Package &package,
88 metaproxy_1::odr &odr_en,
89 Z_SRW_PDU *sru_pdu_res,
90 SRUServerInfo sruinfo,
91 const xmlNode *explain,
92 Z_SRW_explainRequest const *er_req)
95 // building SRU explain record
96 std::string explain_xml;
100 = mp_util::to_string(
101 "<explain xmlns=\"" + xmlns_explain + "\">\n"
102 " <serverInfo protocol='SRU'>\n"
105 + mp_util::to_string("</host>\n"
108 + mp_util::to_string("</port>\n"
111 + mp_util::to_string("</database>\n"
116 // make new XML DOC with given explain node
117 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
118 xmlDocSetRootElement(doc, (xmlNode*)explain);
122 xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
124 explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
128 // z3950'fy recordPacking
129 int record_packing = Z_SRW_recordPacking_XML;
130 if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
131 record_packing = Z_SRW_recordPacking_string;
133 // preparing explain record insert
134 Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
136 // inserting one and only explain record
138 sru_res->record.recordPosition = odr_intdup(odr_en, 1);
139 sru_res->record.recordPacking = record_packing;
140 sru_res->record.recordSchema = (char *)xmlns_explain.c_str();
141 sru_res->record.recordData_len = 1 + explain_xml.size();
142 sru_res->record.recordData_buf
143 = odr_strdupn(odr_en, (const char *)explain_xml.c_str(),
144 1 + explain_xml.size());
150 bool mp_util::build_sru_response(mp::Package &package,
153 const Z_SRW_PDU *sru_pdu_res,
155 const char *stylesheet)
158 // SRU request package translation to Z3950 package
160 // std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
162 // std::cout << "SRU empty\n";
165 Z_GDU *zgdu_req = package.request().get();
166 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
168 Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
169 = odr_en.create_HTTP_Response(package.session(),
170 zgdu_req->u.HTTP_Request,
173 // adding HTTP response code and headers
174 Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
175 //http_res->code = http_code;
177 std::string ctype("text/xml");
179 ctype += "; charset=";
183 z_HTTP_header_add(odr_en,
184 &http_res->headers, "Content-Type", ctype.c_str());
186 // packaging Z_SOAP into HTML response
187 static Z_SOAP_Handler soap_handlers[4] = {
188 {"http://www.loc.gov/zing/srw/", 0,
189 (Z_SOAP_fun) yaz_srw_codec},
190 {"http://www.loc.gov/zing/srw/v1.0/", 0,
191 (Z_SOAP_fun) yaz_srw_codec},
192 {"http://www.loc.gov/zing/srw/update/", 0,
193 (Z_SOAP_fun) yaz_ucp_codec},
198 // empty stylesheet means NO stylesheet
199 if (stylesheet && *stylesheet == '\0')
202 // encoding SRU package
204 soap->u.generic->p = (void*) sru_pdu_res;
206 z_soap_codec_enc_xsl(odr_en, &soap,
207 &http_res->content_buf, &http_res->content_len,
208 soap_handlers, charset, stylesheet);
211 package.response() = zgdu_res;
214 package.session().close();
220 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
223 Z_SRW_PDU *sru_pdu_res,
228 Z_GDU *zgdu_req = package.request().get();
229 Z_SRW_PDU *sru_pdu_req = 0;
231 //assert((zgdu_req->which == Z_GDU_HTTP_Request));
233 //ignoring all non HTTP_Request packages
234 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
238 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
242 // checking if we got a SRU GET/POST/SOAP HTTP package
243 // closing connection if we did not ...
244 if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap,
246 &(sru_pdu_res->u.response->diagnostics),
247 &(sru_pdu_res->u.response->num_diagnostics)))
249 if (sru_pdu_res->u.response->num_diagnostics)
251 //sru_pdu_res = sru_pdu_res_exp;
252 package.session().close();
257 else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap,
262 //sru_pdu_res = sru_pdu_res_exp;
263 package.session().close();
271 mp_util::check_sru_query_exists(mp::Package &package,
273 Z_SRW_PDU *sru_pdu_res,
274 Z_SRW_searchRetrieveRequest const *sr_req)
276 if( (sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql) )
278 yaz_add_srw_diagnostic(odr_en,
279 &(sru_pdu_res->u.response->diagnostics),
280 &(sru_pdu_res->u.response->num_diagnostics),
282 yaz_add_srw_diagnostic(odr_en,
283 &(sru_pdu_res->u.response->diagnostics),
284 &(sru_pdu_res->u.response->num_diagnostics),
285 10, "CQL query is empty");
288 if( (sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql) )
290 yaz_add_srw_diagnostic(odr_en,
291 &(sru_pdu_res->u.response->diagnostics),
292 &(sru_pdu_res->u.response->num_diagnostics),
293 10, "XCQL query is empty");
296 if( (sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf) )
298 yaz_add_srw_diagnostic(odr_en,
299 &(sru_pdu_res->u.response->diagnostics),
300 &(sru_pdu_res->u.response->num_diagnostics),
301 10, "PQF query is empty");
311 mp_util::build_esn_from_schema(mp::odr &odr_en,
317 Z_ElementSetNames *esn
318 = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
319 esn->which = Z_ElementSetNames_generic;
320 esn->u.generic = odr_strdup(odr_en, schema);
325 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
329 switch(srw_pdu.which) {
330 case Z_SRW_searchRetrieve_request:
331 os << " " << "searchRetrieveRequest";
333 Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
337 os << " " << (sr->database);
341 os << " " << *(sr->startRecord);
344 if (sr->maximumRecords)
345 os << " " << *(sr->maximumRecords);
348 if (sr->recordPacking)
349 os << " " << (sr->recordPacking);
353 if (sr->recordSchema)
354 os << " " << (sr->recordSchema);
358 switch (sr->query_type){
359 case Z_SRW_query_type_cql:
362 os << " " << sr->query.cql;
364 case Z_SRW_query_type_xcql:
367 case Z_SRW_query_type_pqf:
370 os << " " << sr->query.pqf;
376 case Z_SRW_searchRetrieve_response:
377 os << " " << "searchRetrieveResponse";
379 Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
382 if (! (sr->num_diagnostics))
385 if (sr->numberOfRecords)
386 os << " " << *(sr->numberOfRecords);
389 //if (sr->num_records)
390 os << " " << (sr->num_records);
393 if (sr->nextRecordPosition)
394 os << " " << *(sr->nextRecordPosition);
401 if (sr->diagnostics && sr->diagnostics->uri)
402 os << " " << (sr->diagnostics->uri);
405 if (sr->diagnostics && sr->diagnostics->message)
406 os << " " << (sr->diagnostics->message);
409 if (sr->diagnostics && sr->diagnostics->details)
410 os << " " << (sr->diagnostics->details);
419 case Z_SRW_explain_request:
420 os << " " << "explainRequest";
422 case Z_SRW_explain_response:
423 os << " " << "explainResponse";
425 case Z_SRW_scan_request:
426 os << " " << "scanRequest";
428 case Z_SRW_scan_response:
429 os << " " << "scanResponse";
431 case Z_SRW_update_request:
432 os << " " << "updateRequest";
434 case Z_SRW_update_response:
435 os << " " << "updateResponse";
438 os << " " << "UNKNOWN";
447 // mp_util::SRU::SRU_protocol_type
448 // mp_util::SRU::protocol(const Z_HTTP_Request &http_req) const
450 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
451 // const std::string mime_text_xml("text/xml");
452 // const std::string mime_soap_xml("application/soap+xml");
454 // const std::string http_method(http_req.method);
455 // const std::string http_type
456 // = mp_util::http_header_value(http_req.headers, "Content-Type");
458 // if (http_method == "GET")
461 // if (http_method == "POST"
462 // && http_type == mime_urlencoded)
465 // if ( http_method == "POST"
466 // && (http_type == mime_text_xml
467 // || http_type == mime_soap_xml))
474 // mp_util::sru_protocol(const Z_HTTP_Request &http_req) const
476 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
477 // const std::string mime_text_xml("text/xml");
478 // const std::string mime_soap_xml("application/soap+xml");
480 // const std::string http_method(http_req.method);
481 // const std::string http_type
482 // = mp_util::http_header_value(http_req.headers, "Content-Type");
484 // if (http_method == "GET")
487 // if (http_method == "POST"
488 // && http_type == mime_urlencoded)
489 // return "SRU POST";
491 // if ( http_method == "POST"
492 // && (http_type == mime_text_xml
493 // || http_type == mime_soap_xml))
494 // return "SRU SOAP";
500 // mp_util::debug_http(const Z_HTTP_Request &http_req) const
502 // std::string message("<html>\n<body>\n<h1>"
503 // "Metaproxy SRUtoZ3950 filter"
506 // message += "<h3>HTTP Info</h3><br/>\n";
507 // message += "<p>\n";
508 // message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
509 // message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
510 // message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
512 // message += "<b>Content-Type:</b>"
513 // + mp_util::http_header_value(http_req.headers, "Content-Type")
515 // message += "<b>Content-Length:</b>"
516 // + mp_util::http_header_value(http_req.headers, "Content-Length")
518 // message += "</p>\n";
520 // message += "<h3>Headers</h3><br/>\n";
521 // message += "<p>\n";
522 // Z_HTTP_Header* header = http_req.headers;
524 // message += "<b>Header: </b> <i>"
525 // + std::string(header->name) + ":</i> "
526 // + std::string(header->value) + "<br/>\n";
527 // header = header->next;
529 // message += "</p>\n";
530 // message += "</body>\n</html>\n";
534 // void mp_util::http_response(metaproxy_1::Package &package,
535 // const std::string &content,
536 // int http_code) const
539 // Z_GDU *zgdu_req = package.request().get();
540 // Z_GDU *zgdu_res = 0;
543 // = odr.create_HTTP_Response(package.session(),
544 // zgdu_req->u.HTTP_Request,
547 // zgdu_res->u.HTTP_Response->content_len = content.size();
548 // zgdu_res->u.HTTP_Response->content_buf
549 // = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
551 // strncpy(zgdu_res->u.HTTP_Response->content_buf,
552 // content.c_str(), zgdu_res->u.HTTP_Response->content_len);
554 // //z_HTTP_header_add(odr, &hres->headers,
555 // // "Content-Type", content_type.c_str());
556 // package.response() = zgdu_res;
562 * indent-tabs-mode: nil
563 * c-file-style: "stroustrup"
565 * vim: shiftwidth=4 tabstop=8 expandtab