1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 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);
76 std::string http_host_address
77 = mp_util::http_header_value(http_req->headers, "Host");
79 std::string::size_type iaddress = http_host_address.rfind(":");
80 if (iaddress != std::string::npos)
82 sruinfo.host.assign(http_host_address, 0, iaddress);
83 sruinfo.port.assign(http_host_address, iaddress + 1,
89 //std::cout << "sruinfo.database " << sruinfo.database << "\n";
90 //std::cout << "sruinfo.host " << sruinfo.host << "\n";
91 //std::cout << "sruinfo.port " << sruinfo.port << "\n";
97 bool mp_util::build_sru_explain(metaproxy_1::Package &package,
98 metaproxy_1::odr &odr_en,
99 Z_SRW_PDU *sru_pdu_res,
100 SRUServerInfo sruinfo,
101 const xmlNode *explain,
102 Z_SRW_explainRequest const *er_req)
105 // building SRU explain record
106 std::string explain_xml;
110 = mp_util::to_string(
111 "<explain xmlns=\"" + xmlns_explain + "\">\n"
112 " <serverInfo protocol='SRU'>\n"
115 + mp_util::to_string("</host>\n"
118 + mp_util::to_string("</port>\n"
121 + mp_util::to_string("</database>\n"
126 // make new XML DOC with given explain node
127 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
128 xmlDocSetRootElement(doc, (xmlNode*)explain);
132 xmlDocDumpFormatMemory(doc, &xmlbuff, &xmlbuffsz, 1);
134 explain_xml.assign((const char*)xmlbuff, 0, xmlbuffsz);
138 // z3950'fy recordPacking
139 int record_packing = Z_SRW_recordPacking_XML;
140 if (er_req && er_req->recordPacking && 's' == *(er_req->recordPacking))
141 record_packing = Z_SRW_recordPacking_string;
143 // preparing explain record insert
144 Z_SRW_explainResponse *sru_res = sru_pdu_res->u.explain_response;
146 // inserting one and only explain record
148 sru_res->record.recordPosition = odr_intdup(odr_en, 1);
149 sru_res->record.recordPacking = record_packing;
150 sru_res->record.recordSchema = (char *)xmlns_explain.c_str();
151 sru_res->record.recordData_len = 1 + explain_xml.size();
152 sru_res->record.recordData_buf
153 = odr_strdupn(odr_en, (const char *)explain_xml.c_str(),
154 1 + explain_xml.size());
160 bool mp_util::build_sru_response(mp::Package &package,
163 const Z_SRW_PDU *sru_pdu_res,
165 const char *stylesheet)
168 // SRU request package translation to Z3950 package
170 // std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
172 // std::cout << "SRU empty\n";
175 Z_GDU *zgdu_req = package.request().get();
176 if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
178 Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
179 = odr_en.create_HTTP_Response(package.session(),
180 zgdu_req->u.HTTP_Request,
183 // adding HTTP response code and headers
184 Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
185 //http_res->code = http_code;
187 std::string ctype("text/xml");
190 ctype += "; charset=";
194 z_HTTP_header_add(odr_en,
195 &http_res->headers, "Content-Type", ctype.c_str());
197 // packaging Z_SOAP into HTML response
198 static Z_SOAP_Handler soap_handlers[4] = {
199 {(char *)YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
200 {(char *)YAZ_XMLNS_SRU_v1_0, 0, (Z_SOAP_fun) yaz_srw_codec},
201 {(char *)YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec},
206 // empty stylesheet means NO stylesheet
207 if (stylesheet && *stylesheet == '\0')
210 // encoding SRU package
212 soap->u.generic->p = (void*) sru_pdu_res;
214 z_soap_codec_enc_xsl(odr_en, &soap,
215 &http_res->content_buf, &http_res->content_len,
216 soap_handlers, charset, stylesheet);
219 package.response() = zgdu_res;
222 package.session().close();
228 Z_SRW_PDU * mp_util::decode_sru_request(mp::Package &package,
231 Z_SRW_PDU *sru_pdu_res,
236 Z_GDU *zgdu_req = package.request().get();
237 Z_SRW_PDU *sru_pdu_req = 0;
239 //assert((zgdu_req->which == Z_GDU_HTTP_Request));
241 //ignoring all non HTTP_Request packages
242 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request))
247 Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
251 // checking if we got a SRU GET/POST/SOAP HTTP package
252 // closing connection if we did not ...
253 if (0 == yaz_sru_decode(http_req, &sru_pdu_req, soap,
255 &(sru_pdu_res->u.response->diagnostics),
256 &(sru_pdu_res->u.response->num_diagnostics)))
258 if (sru_pdu_res->u.response->num_diagnostics)
260 //sru_pdu_res = sru_pdu_res_exp;
261 package.session().close();
266 else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, soap,
271 //sru_pdu_res = sru_pdu_res_exp;
272 package.session().close();
280 mp_util::check_sru_query_exists(mp::Package &package,
282 Z_SRW_PDU *sru_pdu_res,
283 Z_SRW_searchRetrieveRequest const *sr_req)
285 if ((sr_req->query_type == Z_SRW_query_type_cql && !sr_req->query.cql))
287 yaz_add_srw_diagnostic(odr_en,
288 &(sru_pdu_res->u.response->diagnostics),
289 &(sru_pdu_res->u.response->num_diagnostics),
291 yaz_add_srw_diagnostic(odr_en,
292 &(sru_pdu_res->u.response->diagnostics),
293 &(sru_pdu_res->u.response->num_diagnostics),
294 10, "CQL query is empty");
297 if ((sr_req->query_type == Z_SRW_query_type_xcql && !sr_req->query.xcql))
299 yaz_add_srw_diagnostic(odr_en,
300 &(sru_pdu_res->u.response->diagnostics),
301 &(sru_pdu_res->u.response->num_diagnostics),
302 10, "XCQL query is empty");
305 if ((sr_req->query_type == Z_SRW_query_type_pqf && !sr_req->query.pqf))
307 yaz_add_srw_diagnostic(odr_en,
308 &(sru_pdu_res->u.response->diagnostics),
309 &(sru_pdu_res->u.response->num_diagnostics),
310 10, "PQF query is empty");
318 mp_util::build_esn_from_schema(mp::odr &odr_en, const char *schema)
323 Z_ElementSetNames *esn
324 = (Z_ElementSetNames *) odr_malloc(odr_en, sizeof(Z_ElementSetNames));
325 esn->which = Z_ElementSetNames_generic;
326 esn->u.generic = odr_strdup(odr_en, schema);
331 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu)
335 switch(srw_pdu.which) {
336 case Z_SRW_searchRetrieve_request:
337 os << " " << "searchRetrieveRequest";
339 Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
343 os << " " << (sr->database);
347 os << " " << *(sr->startRecord);
350 if (sr->maximumRecords)
351 os << " " << *(sr->maximumRecords);
354 if (sr->recordPacking)
355 os << " " << (sr->recordPacking);
359 if (sr->recordSchema)
360 os << " " << (sr->recordSchema);
364 switch (sr->query_type){
365 case Z_SRW_query_type_cql:
368 os << " " << sr->query.cql;
370 case Z_SRW_query_type_xcql:
373 case Z_SRW_query_type_pqf:
376 os << " " << sr->query.pqf;
382 case Z_SRW_searchRetrieve_response:
383 os << " " << "searchRetrieveResponse";
385 Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
388 if (! (sr->num_diagnostics))
391 if (sr->numberOfRecords)
392 os << " " << *(sr->numberOfRecords);
395 //if (sr->num_records)
396 os << " " << (sr->num_records);
399 if (sr->nextRecordPosition)
400 os << " " << *(sr->nextRecordPosition);
407 if (sr->diagnostics && sr->diagnostics->uri)
408 os << " " << (sr->diagnostics->uri);
411 if (sr->diagnostics && sr->diagnostics->message)
412 os << " " << (sr->diagnostics->message);
415 if (sr->diagnostics && sr->diagnostics->details)
416 os << " " << (sr->diagnostics->details);
425 case Z_SRW_explain_request:
426 os << " " << "explainRequest";
428 case Z_SRW_explain_response:
429 os << " " << "explainResponse";
431 case Z_SRW_scan_request:
432 os << " " << "scanRequest";
434 case Z_SRW_scan_response:
435 os << " " << "scanResponse";
437 case Z_SRW_update_request:
438 os << " " << "updateRequest";
440 case Z_SRW_update_response:
441 os << " " << "updateResponse";
444 os << " " << "UNKNOWN";
453 * c-file-style: "Stroustrup"
454 * indent-tabs-mode: nil
456 * vim: shiftwidth=4 tabstop=8 expandtab