1 /* $Id: sru_util.cpp,v 1.4 2007-01-05 12:26:50 marc Exp $
2 Copyright (c) 2005-2006, 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;
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 void mp_util::get_sru_server_info(mp::Package &package,
43 SRUServerInfo sruinfo;
45 // getting database info
46 std::string database("Default");
47 if (er_req && er_req->database)
48 database = er_req->database;
50 // getting host and port info
51 std::string host = package.origin().listen_host();
52 std::string port = mp_util::to_string(package.origin().listen_port());
54 // overwriting host and port info if set from HTTP Host header
55 Z_GDU *zgdu_req = package.request().get();
56 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
58 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
61 std::string http_host_address
62 = mp_util::http_header_value(http_req->headers, "Host");
64 std::string::size_type i = http_host_address.rfind(":");
65 if (i != std::string::npos)
67 host.assign(http_host_address, 0, i);
68 port.assign(http_host_address, i + 1, std::string::npos);
75 bool mp_util::build_simple_explain(mp::Package &package,
77 Z_SRW_PDU *sru_pdu_res,
81 // z3950'fy recordPacking
82 int record_packing = Z_SRW_recordPacking_XML;
83 if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
84 record_packing = Z_SRW_recordPacking_string;
86 // getting database info
87 std::string database("Default");
88 if (er_req && er_req->database)
89 database = er_req->database;
91 // getting host and port info
92 std::string host = package.origin().listen_host();
93 std::string port = mp_util::to_string(package.origin().listen_port());
95 // overwriting host and port info if set from HTTP Host header
96 Z_GDU *zgdu_req = package.request().get();
97 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
99 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
102 std::string http_host_address
103 = mp_util::http_header_value(http_req->headers, "Host");
105 std::string::size_type i = http_host_address.rfind(":");
106 if (i != std::string::npos)
108 host.assign(http_host_address, 0, i);
109 port.assign(http_host_address, i + 1, std::string::npos);
114 // building SRU explain record
115 std::string explain_xml
116 = mp_util::to_string(
117 "<explain xmlns=\"http://explain.z3950.org/dtd/2.0/\">\n"
118 " <serverInfo protocol='SRU'>\n"
121 + mp_util::to_string("</host>\n"
124 + mp_util::to_string("</port>\n"
127 + mp_util::to_string("</database>\n"
132 // preparing explain record insert
133 Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
135 // inserting one and only explain record
137 sru_res->record.recordPosition = odr_intdup(odr_en, 1);
138 sru_res->record.recordPacking = record_packing;
139 sru_res->record.recordSchema = "http://explain.z3950.org/dtd/2.0/";
140 sru_res->record.recordData_len = 1 + explain_xml.size();
141 sru_res->record.recordData_buf
142 = odr_strdupn(odr_en, (const char *)explain_xml.c_str(),
143 1 + explain_xml.size());
149 bool mp_util::build_sru_response(mp::Package &package,
152 const Z_SRW_PDU *sru_pdu_res,
154 const char *stylesheet)
157 // SRU request package translation to Z3950 package
159 // std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
161 // std::cout << "SRU empty\n";
164 Z_GDU *zgdu_req = package.request().get();
165 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
167 Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
168 = odr_en.create_HTTP_Response(package.session(),
169 zgdu_req->u.HTTP_Request,
172 // adding HTTP response code and headers
173 Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
174 //http_res->code = http_code;
176 std::string ctype("text/xml");
178 ctype += "; charset=";
182 z_HTTP_header_add(odr_en,
183 &http_res->headers, "Content-Type", ctype.c_str());
185 // packaging Z_SOAP into HTML response
186 static Z_SOAP_Handler soap_handlers[4] = {
187 {"http://www.loc.gov/zing/srw/", 0,
188 (Z_SOAP_fun) yaz_srw_codec},
189 {"http://www.loc.gov/zing/srw/v1.0/", 0,
190 (Z_SOAP_fun) yaz_srw_codec},
191 {"http://www.loc.gov/zing/srw/update/", 0,
192 (Z_SOAP_fun) yaz_ucp_codec},
197 // empty stylesheet means NO stylesheet
198 if (stylesheet && *stylesheet == '\0')
201 // encoding SRU package
203 soap->u.generic->p = (void*) sru_pdu_res;
205 z_soap_codec_enc_xsl(odr_en, &soap,
206 &http_res->content_buf, &http_res->content_len,
207 soap_handlers, charset, stylesheet);
210 package.response() = zgdu_res;
213 package.session().close();
219 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
222 Z_SRW_PDU *sru_pdu_res,
227 Z_GDU *zgdu_req = package.request().get();
228 Z_SRW_PDU *sru_pdu_req = 0;
230 //assert((zgdu_req->which == Z_GDU_HTTP_Request));
232 //ignoring all non HTTP_Request packages
233 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
237 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
241 // checking if we got a SRU GET/POST/SOAP HTTP package
242 // closing connection if we did not ...
243 if (0 == yaz_sru_decode(http_req, &sru_pdu_req, &soap,
245 &(sru_pdu_res->u.response->diagnostics),
246 &(sru_pdu_res->u.response->num_diagnostics)))
248 if (sru_pdu_res->u.response->num_diagnostics)
250 //sru_pdu_res = sru_pdu_res_exp;
251 package.session().close();
256 else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, &soap,
261 //sru_pdu_res = sru_pdu_res_exp;
262 package.session().close();
270 mp_util::check_sru_query_exists(mp::Package &package,
272 Z_SRW_PDU *sru_pdu_res,
273 Z_SRW_searchRetrieveRequest const *sr_req)
275 if( (sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql) )
277 yaz_add_srw_diagnostic(odr_en,
278 &(sru_pdu_res->u.response->diagnostics),
279 &(sru_pdu_res->u.response->num_diagnostics),
281 yaz_add_srw_diagnostic(odr_en,
282 &(sru_pdu_res->u.response->diagnostics),
283 &(sru_pdu_res->u.response->num_diagnostics),
284 10, "CQL query is empty");
287 if( (sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql) )
289 yaz_add_srw_diagnostic(odr_en,
290 &(sru_pdu_res->u.response->diagnostics),
291 &(sru_pdu_res->u.response->num_diagnostics),
292 10, "XCQL query is empty");
295 if( (sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf) )
297 yaz_add_srw_diagnostic(odr_en,
298 &(sru_pdu_res->u.response->diagnostics),
299 &(sru_pdu_res->u.response->num_diagnostics),
300 10, "PQF query is empty");
310 mp_util::build_esn_from_schema(mp::odr &odr_en,
316 Z_ElementSetNames *esn
317 = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
318 esn->which = Z_ElementSetNames_generic;
319 esn->u.generic = odr_strdup(odr_en, schema);
324 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
328 switch(srw_pdu.which) {
329 case Z_SRW_searchRetrieve_request:
330 os << " " << "searchRetrieveRequest";
332 Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
336 os << " " << (sr->database);
340 os << " " << *(sr->startRecord);
343 if (sr->maximumRecords)
344 os << " " << *(sr->maximumRecords);
347 if (sr->recordPacking)
348 os << " " << (sr->recordPacking);
352 if (sr->recordSchema)
353 os << " " << (sr->recordSchema);
357 switch (sr->query_type){
358 case Z_SRW_query_type_cql:
361 os << " " << sr->query.cql;
363 case Z_SRW_query_type_xcql:
366 case Z_SRW_query_type_pqf:
369 os << " " << sr->query.pqf;
375 case Z_SRW_searchRetrieve_response:
376 os << " " << "searchRetrieveResponse";
378 Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
381 if (! (sr->num_diagnostics))
384 if (sr->numberOfRecords)
385 os << " " << *(sr->numberOfRecords);
388 //if (sr->num_records)
389 os << " " << (sr->num_records);
392 if (sr->nextRecordPosition)
393 os << " " << *(sr->nextRecordPosition);
400 if (sr->diagnostics && sr->diagnostics->uri)
401 os << " " << (sr->diagnostics->uri);
404 if (sr->diagnostics && sr->diagnostics->message)
405 os << " " << (sr->diagnostics->message);
408 if (sr->diagnostics && sr->diagnostics->details)
409 os << " " << (sr->diagnostics->details);
418 case Z_SRW_explain_request:
419 os << " " << "explainRequest";
421 case Z_SRW_explain_response:
422 os << " " << "explainResponse";
424 case Z_SRW_scan_request:
425 os << " " << "scanRequest";
427 case Z_SRW_scan_response:
428 os << " " << "scanResponse";
430 case Z_SRW_update_request:
431 os << " " << "updateRequest";
433 case Z_SRW_update_response:
434 os << " " << "updateResponse";
437 os << " " << "UNKNOWN";
446 // mp_util::SRU::SRU_protocol_type
447 // mp_util::SRU::protocol(const Z_HTTP_Request &http_req) const
449 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
450 // const std::string mime_text_xml("text/xml");
451 // const std::string mime_soap_xml("application/soap+xml");
453 // const std::string http_method(http_req.method);
454 // const std::string http_type
455 // = mp_util::http_header_value(http_req.headers, "Content-Type");
457 // if (http_method == "GET")
460 // if (http_method == "POST"
461 // && http_type == mime_urlencoded)
464 // if ( http_method == "POST"
465 // && (http_type == mime_text_xml
466 // || http_type == mime_soap_xml))
473 // mp_util::sru_protocol(const Z_HTTP_Request &http_req) const
475 // const std::string mime_urlencoded("application/x-www-form-urlencoded");
476 // const std::string mime_text_xml("text/xml");
477 // const std::string mime_soap_xml("application/soap+xml");
479 // const std::string http_method(http_req.method);
480 // const std::string http_type
481 // = mp_util::http_header_value(http_req.headers, "Content-Type");
483 // if (http_method == "GET")
486 // if (http_method == "POST"
487 // && http_type == mime_urlencoded)
488 // return "SRU POST";
490 // if ( http_method == "POST"
491 // && (http_type == mime_text_xml
492 // || http_type == mime_soap_xml))
493 // return "SRU SOAP";
499 // mp_util::debug_http(const Z_HTTP_Request &http_req) const
501 // std::string message("<html>\n<body>\n<h1>"
502 // "Metaproxy SRUtoZ3950 filter"
505 // message += "<h3>HTTP Info</h3><br/>\n";
506 // message += "<p>\n";
507 // message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
508 // message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
509 // message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
511 // message += "<b>Content-Type:</b>"
512 // + mp_util::http_header_value(http_req.headers, "Content-Type")
514 // message += "<b>Content-Length:</b>"
515 // + mp_util::http_header_value(http_req.headers, "Content-Length")
517 // message += "</p>\n";
519 // message += "<h3>Headers</h3><br/>\n";
520 // message += "<p>\n";
521 // Z_HTTP_Header* header = http_req.headers;
523 // message += "<b>Header: </b> <i>"
524 // + std::string(header->name) + ":</i> "
525 // + std::string(header->value) + "<br/>\n";
526 // header = header->next;
528 // message += "</p>\n";
529 // message += "</body>\n</html>\n";
533 // void mp_util::http_response(metaproxy_1::Package &package,
534 // const std::string &content,
535 // int http_code) const
538 // Z_GDU *zgdu_req = package.request().get();
539 // Z_GDU *zgdu_res = 0;
542 // = odr.create_HTTP_Response(package.session(),
543 // zgdu_req->u.HTTP_Request,
546 // zgdu_res->u.HTTP_Response->content_len = content.size();
547 // zgdu_res->u.HTTP_Response->content_buf
548 // = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
550 // strncpy(zgdu_res->u.HTTP_Response->content_buf,
551 // content.c_str(), zgdu_res->u.HTTP_Response->content_len);
553 // //z_HTTP_header_add(odr, &hres->headers,
554 // // "Content-Type", content_type.c_str());
555 // package.response() = zgdu_res;
561 * indent-tabs-mode: nil
562 * c-file-style: "stroustrup"
564 * vim: shiftwidth=4 tabstop=8 expandtab