1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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
20 #include <metaproxy/util.hpp>
23 #include <yaz/comstack.h>
24 #include <yaz/pquery.h>
25 #include <yaz/otherinfo.h>
26 #include <yaz/querytowrbuf.h>
27 #include <yaz/oid_db.h>
32 namespace mp = metaproxy_1;
34 // Doxygen doesn't like mp::util, so we use this instead
35 namespace mp_util = metaproxy_1::util;
38 mp_util::record_composition_to_esn(Z_RecordComposition *comp)
40 if (comp && comp->which == Z_RecordComp_complex)
42 if (comp->u.complex->generic
43 && comp->u.complex->generic->elementSpec
44 && (comp->u.complex->generic->elementSpec->which ==
45 Z_ElementSpec_elementSetName))
46 return comp->u.complex->generic->elementSpec->u.elementSetName;
48 else if (comp && comp->which == Z_RecordComp_simple &&
49 comp->u.simple->which == Z_ElementSetNames_generic)
50 return comp->u.simple->u.generic;
56 std::string mp_util::http_header_value(const Z_HTTP_Header* header,
57 const std::string name)
59 while (header && header->name
60 && std::string(header->name) != name)
61 header = header->next;
63 if (header && header->name && std::string(header->name) == name
65 return std::string(header->value);
70 std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req)
72 std::string message("<html>\n<body>\n<h1>"
73 "Metaproxy SRUtoZ3950 filter"
76 message += "<h3>HTTP Info</h3><br/>\n";
78 message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
79 message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
80 message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
82 message += "<b>Content-Type:</b>"
83 + mp_util::http_header_value(http_req.headers, "Content-Type")
85 message += "<b>Content-Length:</b>"
86 + mp_util::http_header_value(http_req.headers, "Content-Length")
90 message += "<h3>Headers</h3><br/>\n";
92 Z_HTTP_Header* header = http_req.headers;
94 message += "<b>Header: </b> <i>"
95 + std::string(header->name) + ":</i> "
96 + std::string(header->value) + "<br/>\n";
97 header = header->next;
100 message += "</body>\n</html>\n";
105 void mp_util::http_response(metaproxy_1::Package &package,
106 const std::string &content,
110 Z_GDU *zgdu_req = package.request().get();
114 = odr.create_HTTP_Response(package.session(),
115 zgdu_req->u.HTTP_Request,
118 zgdu_res->u.HTTP_Response->content_len = content.size();
119 zgdu_res->u.HTTP_Response->content_buf
120 = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
122 strncpy(zgdu_res->u.HTTP_Response->content_buf,
123 content.c_str(), zgdu_res->u.HTTP_Response->content_len);
125 //z_HTTP_header_add(odr, &hres->headers,
126 // "Content-Type", content_type.c_str());
127 package.response() = zgdu_res;
131 int mp_util::memcmp2(const void *buf1, int len1,
132 const void *buf2, int len2)
136 // compare buffer (common length)
137 int c = memcmp(buf1, buf2, d > 0 ? len2 : len1);
143 // compare (remaining bytes)
152 std::string mp_util::database_name_normalize(const std::string &s)
156 for (i = 0; i < r.length(); i++)
159 if (ch >= 'A' && ch <= 'Z')
160 r[i] = ch + 'a' - 'A';
166 void mp_util::piggyback_sr(Z_SearchRequest *sreq,
167 Odr_int result_set_size,
168 Odr_int &number_to_present,
169 const char **element_set_name)
171 Z_ElementSetNames *esn;
172 const char *smallSetElementSetNames = 0;
173 const char *mediumSetElementSetNames = 0;
175 esn = sreq->smallSetElementSetNames;
176 if (esn && esn->which == Z_ElementSetNames_generic)
177 smallSetElementSetNames = esn->u.generic;
179 esn = sreq->mediumSetElementSetNames;
180 if (esn && esn->which == Z_ElementSetNames_generic)
181 mediumSetElementSetNames = esn->u.generic;
183 piggyback(*sreq->smallSetUpperBound,
184 *sreq->largeSetLowerBound,
185 *sreq->mediumSetPresentNumber,
186 smallSetElementSetNames,
187 mediumSetElementSetNames,
193 void mp_util::piggyback(int smallSetUpperBound,
194 int largeSetLowerBound,
195 int mediumSetPresentNumber,
197 int &number_to_present)
199 Odr_int tmp = number_to_present;
200 piggyback(smallSetUpperBound, largeSetLowerBound, mediumSetPresentNumber,
201 0, 0, result_set_size, tmp, 0);
202 number_to_present = tmp;
205 void mp_util::piggyback(Odr_int smallSetUpperBound,
206 Odr_int largeSetLowerBound,
207 Odr_int mediumSetPresentNumber,
208 const char *smallSetElementSetNames,
209 const char *mediumSetElementSetNames,
210 Odr_int result_set_size,
211 Odr_int &number_to_present,
212 const char **element_set_name)
214 // deal with piggyback
216 if (result_set_size < smallSetUpperBound)
218 // small set . Return all records in set
219 number_to_present = result_set_size;
220 if (element_set_name && smallSetElementSetNames)
221 *element_set_name = smallSetElementSetNames;
224 else if (result_set_size > largeSetLowerBound)
226 // large set . Return no records
227 number_to_present = 0;
228 if (element_set_name)
229 *element_set_name = 0;
233 // medium set . Return mediumSetPresentNumber records
234 number_to_present = mediumSetPresentNumber;
235 if (number_to_present > result_set_size)
236 number_to_present = result_set_size;
237 if (element_set_name && mediumSetElementSetNames)
238 *element_set_name = mediumSetElementSetNames;
242 bool mp_util::pqf(ODR odr, Z_APDU *apdu, const std::string &q)
244 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
246 Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
249 yaz_pqf_destroy(pqf_parser);
252 yaz_pqf_destroy(pqf_parser);
253 Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
254 query->which = Z_Query_type_1;
255 query->u.type_1 = rpn;
257 apdu->u.searchRequest->query = query;
262 std::string mp_util::zQueryToString(Z_Query *query)
264 std::string query_str = "";
266 if (query && query->which == Z_Query_type_1)
268 Z_RPNQuery *rpn = query->u.type_1;
275 yaz_rpnquery_to_wrbuf(w, rpn);
277 // from w to std::string
278 query_str = std::string(w.buf(), w.len());
283 if (query && query->which == Z_Query_type_1){
285 // allocate wrbuf (strings in YAZ!)
286 WRBUF w = wrbuf_alloc();
289 yaz_query_to_wrbuf(w, query);
291 // from w to std::string
292 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
301 void mp_util::get_default_diag(Z_DefaultDiagFormat *r,
302 int &error_code, std::string &addinfo)
304 error_code = *r->condition;
307 case Z_DefaultDiagFormat_v2Addinfo:
308 addinfo = std::string(r->u.v2Addinfo);
310 case Z_DefaultDiagFormat_v3Addinfo:
311 addinfo = r->u.v3Addinfo;
316 void mp_util::get_init_diagnostics(
317 Z_InitResponse *initrs, int &error_code, std::string &addinfo)
320 Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs);
323 get_default_diag(df, error_code, addinfo);
326 int mp_util::get_or_remove_vhost_otherinfo(
327 Z_OtherInformation **otherInformation,
329 std::list<std::string> &vhosts)
332 for (cat = 1; ; cat++)
334 // check virtual host
336 yaz_oi_get_string_oid(otherInformation,
337 yaz_oid_userinfo_proxy,
338 cat /* categoryValue */,
339 remove_flag /* delete flag */);
342 vhosts.push_back(std::string(vhost));
348 void mp_util::get_vhost_otherinfo(
349 Z_OtherInformation *otherInformation,
350 std::list<std::string> &vhosts)
352 get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
355 int mp_util::remove_vhost_otherinfo(
356 Z_OtherInformation **otherInformation,
357 std::list<std::string> &vhosts)
359 return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
362 void mp_util::set_vhost_otherinfo(
363 Z_OtherInformation **otherInformation, ODR odr,
364 const std::list<std::string> &vhosts)
367 std::list<std::string>::const_iterator it = vhosts.begin();
369 for (cat = 1; it != vhosts.end() ; cat++, it++)
371 yaz_oi_set_string_oid(otherInformation, odr,
372 yaz_oid_userinfo_proxy, cat, it->c_str());
376 void mp_util::set_vhost_otherinfo(
377 Z_OtherInformation **otherInformation, ODR odr,
378 const std::string vhost, const int cat)
380 yaz_oi_set_string_oid(otherInformation, odr,
381 yaz_oid_userinfo_proxy, cat, vhost.c_str());
384 void mp_util::split_zurl(std::string zurl, std::string &host,
385 std::list<std::string> &db)
387 const char *zurl_cstr = zurl.c_str();
388 const char *args = 0;
389 cs_get_host_args(zurl_cstr, &args);
393 host = std::string(zurl_cstr, args - zurl_cstr);
395 const char *cp1 = args;
398 const char *cp2 = strchr(cp1, '+');
400 db.push_back(std::string(cp1, cp2 - cp1));
403 db.push_back(std::string(cp1));
413 bool mp_util::set_databases_from_zurl(
414 ODR odr, std::string zurl,
415 int *db_num, char ***db_strings)
418 std::list<std::string> dblist;
420 split_zurl(zurl, host, dblist);
422 if (dblist.size() == 0)
424 *db_num = dblist.size();
425 *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
427 std::list<std::string>::const_iterator it = dblist.begin();
428 for (int i = 0; it != dblist.end(); it++, i++)
429 (*db_strings)[i] = odr_strdup(odr, it->c_str());
433 mp::odr::odr(int type)
435 m_odr = odr_createmem(type);
440 m_odr = odr_createmem(ODR_ENCODE);
448 mp::odr::operator ODR() const
453 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
454 int reason, const char *addinfo)
456 Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
458 *apdu->u.close->closeReason = reason;
460 apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
464 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
466 return mp::util::create_APDU(m_odr, type, in_apdu);
469 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
471 Z_APDU *out_apdu = zget_APDU(odr, type);
472 transfer_referenceId(odr, in_apdu, out_apdu);
476 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
478 Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
482 Z_ReferenceId **id_from = mp::util::get_referenceId(src);
483 if (id_from && *id_from && id_to)
485 *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
486 (*id_to)->size = (*id_to)->len = (*id_from)->len;
487 (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
488 memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
495 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
496 int error, const char *addinfo)
498 Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
501 apdu->u.initResponse->userInformationField =
502 zget_init_diagnostics(m_odr, error, addinfo);
503 *apdu->u.initResponse->result = 0;
505 apdu->u.initResponse->implementationName =
506 odr_prepend(m_odr, "Metaproxy",
507 apdu->u.initResponse->implementationName);
508 apdu->u.initResponse->implementationVersion =
510 VERSION, apdu->u.initResponse->implementationVersion);
515 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
516 int error, const char *addinfo)
518 Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
521 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
522 *apdu->u.searchResponse->searchStatus = 0;
523 apdu->u.searchResponse->records = rec;
524 rec->which = Z_Records_NSD;
525 rec->u.nonSurrogateDiagnostic =
526 zget_DefaultDiagFormat(m_odr, error, addinfo);
532 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
533 int error, const char *addinfo)
535 Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
538 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
539 apdu->u.presentResponse->records = rec;
541 rec->which = Z_Records_NSD;
542 rec->u.nonSurrogateDiagnostic =
543 zget_DefaultDiagFormat(m_odr, error, addinfo);
544 *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
549 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
550 int error, const char *addinfo)
552 Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
553 Z_ScanResponse *res = apdu->u.scanResponse;
554 res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
555 res->entries->num_entries = 0;
556 res->entries->entries = 0;
560 *res->scanStatus = Z_Scan_failure;
562 res->entries->num_nonsurrogateDiagnostics = 1;
563 res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
564 odr_malloc(m_odr, sizeof(Z_DiagRec *));
565 res->entries->nonsurrogateDiagnostics[0] =
566 zget_DiagRec(m_odr, error, addinfo);
570 res->entries->num_nonsurrogateDiagnostics = 0;
571 res->entries->nonsurrogateDiagnostics = 0;
576 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
577 Z_HTTP_Request *hreq, int code)
579 const char *response_version = "1.0";
580 bool keepalive = false;
581 if (!strcmp(hreq->version, "1.0"))
583 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
584 if (v && !strcmp(v, "Keep-Alive"))
588 response_version = "1.0";
592 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
593 if (v && !strcmp(v, "close"))
597 response_version = "1.1";
600 Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
601 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
602 hres->version = odr_strdup(m_odr, response_version);
604 z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
609 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
613 case Z_APDU_initRequest:
614 return &apdu->u.initRequest->referenceId;
615 case Z_APDU_initResponse:
616 return &apdu->u.initResponse->referenceId;
617 case Z_APDU_searchRequest:
618 return &apdu->u.searchRequest->referenceId;
619 case Z_APDU_searchResponse:
620 return &apdu->u.searchResponse->referenceId;
621 case Z_APDU_presentRequest:
622 return &apdu->u.presentRequest->referenceId;
623 case Z_APDU_presentResponse:
624 return &apdu->u.presentResponse->referenceId;
625 case Z_APDU_deleteResultSetRequest:
626 return &apdu->u.deleteResultSetRequest->referenceId;
627 case Z_APDU_deleteResultSetResponse:
628 return &apdu->u.deleteResultSetResponse->referenceId;
629 case Z_APDU_accessControlRequest:
630 return &apdu->u.accessControlRequest->referenceId;
631 case Z_APDU_accessControlResponse:
632 return &apdu->u.accessControlResponse->referenceId;
633 case Z_APDU_resourceControlRequest:
634 return &apdu->u.resourceControlRequest->referenceId;
635 case Z_APDU_resourceControlResponse:
636 return &apdu->u.resourceControlResponse->referenceId;
637 case Z_APDU_triggerResourceControlRequest:
638 return &apdu->u.triggerResourceControlRequest->referenceId;
639 case Z_APDU_resourceReportRequest:
640 return &apdu->u.resourceReportRequest->referenceId;
641 case Z_APDU_resourceReportResponse:
642 return &apdu->u.resourceReportResponse->referenceId;
643 case Z_APDU_scanRequest:
644 return &apdu->u.scanRequest->referenceId;
645 case Z_APDU_scanResponse:
646 return &apdu->u.scanResponse->referenceId;
647 case Z_APDU_sortRequest:
648 return &apdu->u.sortRequest->referenceId;
649 case Z_APDU_sortResponse:
650 return &apdu->u.sortResponse->referenceId;
651 case Z_APDU_segmentRequest:
652 return &apdu->u.segmentRequest->referenceId;
653 case Z_APDU_extendedServicesRequest:
654 return &apdu->u.extendedServicesRequest->referenceId;
655 case Z_APDU_extendedServicesResponse:
656 return &apdu->u.extendedServicesResponse->referenceId;
658 return &apdu->u.close->referenceId;
663 std::string mp_util::uri_encode(std::string s)
665 char *x = (char *) xmalloc(1 + s.length() * 3);
666 yaz_encode_uri_component(x, s.c_str());
667 std::string result(x);
673 std::string mp_util::uri_decode(std::string s)
675 char *x = (char *) xmalloc(1 + s.length());
676 yaz_decode_uri_component(x, s.c_str(), s.length());
677 std::string result(x);
684 m_wrbuf = wrbuf_alloc();
689 wrbuf_destroy(m_wrbuf);
692 mp::wrbuf::operator WRBUF() const
697 size_t mp::wrbuf::len()
699 return wrbuf_len(m_wrbuf);
702 const char *mp::wrbuf::buf()
704 return wrbuf_buf(m_wrbuf);
711 * c-file-style: "Stroustrup"
712 * indent-tabs-mode: nil
714 * vim: shiftwidth=4 tabstop=8 expandtab