1 /* $Id: util.cpp,v 1.27 2007-04-13 09:57:51 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 See the LICENSE file for details
11 #include <yaz/pquery.h>
12 #include <yaz/otherinfo.h>
13 #include <yaz/querytowrbuf.h>
14 #include <yaz/oid_db.h>
18 namespace mp = metaproxy_1;
20 // Doxygen doesn't like mp::util, so we use this instead
21 namespace mp_util = metaproxy_1::util;
24 mp_util::record_composition_to_esn(Z_RecordComposition *comp)
26 if (comp && comp->which == Z_RecordComp_complex)
28 if (comp->u.complex->generic
29 && comp->u.complex->generic->elementSpec
30 && (comp->u.complex->generic->elementSpec->which ==
31 Z_ElementSpec_elementSetName))
32 return comp->u.complex->generic->elementSpec->u.elementSetName;
34 else if (comp && comp->which == Z_RecordComp_simple &&
35 comp->u.simple->which == Z_ElementSetNames_generic)
36 return comp->u.simple->u.generic;
42 std::string mp_util::http_header_value(const Z_HTTP_Header* header,
43 const std::string name)
45 while (header && header->name
46 && std::string(header->name) != name)
47 header = header->next;
49 if (header && header->name && std::string(header->name) == name
51 return std::string(header->value);
56 std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req)
58 std::string message("<html>\n<body>\n<h1>"
59 "Metaproxy SRUtoZ3950 filter"
62 message += "<h3>HTTP Info</h3><br/>\n";
64 message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
65 message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
66 message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
68 message += "<b>Content-Type:</b>"
69 + mp_util::http_header_value(http_req.headers, "Content-Type")
71 message += "<b>Content-Length:</b>"
72 + mp_util::http_header_value(http_req.headers, "Content-Length")
76 message += "<h3>Headers</h3><br/>\n";
78 Z_HTTP_Header* header = http_req.headers;
80 message += "<b>Header: </b> <i>"
81 + std::string(header->name) + ":</i> "
82 + std::string(header->value) + "<br/>\n";
83 header = header->next;
86 message += "</body>\n</html>\n";
91 void mp_util::http_response(metaproxy_1::Package &package,
92 const std::string &content,
96 Z_GDU *zgdu_req = package.request().get();
100 = odr.create_HTTP_Response(package.session(),
101 zgdu_req->u.HTTP_Request,
104 zgdu_res->u.HTTP_Response->content_len = content.size();
105 zgdu_res->u.HTTP_Response->content_buf
106 = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
108 strncpy(zgdu_res->u.HTTP_Response->content_buf,
109 content.c_str(), zgdu_res->u.HTTP_Response->content_len);
111 //z_HTTP_header_add(odr, &hres->headers,
112 // "Content-Type", content_type.c_str());
113 package.response() = zgdu_res;
117 int mp_util::memcmp2(const void *buf1, int len1,
118 const void *buf2, int len2)
122 // compare buffer (common length)
123 int c = memcmp(buf1, buf2, d > 0 ? len2 : len1);
129 // compare (remaining bytes)
138 std::string mp_util::database_name_normalize(const std::string &s)
142 for (i = 0; i < r.length(); i++)
145 if (ch >= 'A' && ch <= 'Z')
146 r[i] = ch + 'a' - 'A';
152 void mp_util::piggyback(int smallSetUpperBound,
153 int largeSetLowerBound,
154 int mediumSetPresentNumber,
156 int &number_to_present)
158 // deal with piggyback
160 if (result_set_size < smallSetUpperBound)
162 // small set . Return all records in set
163 number_to_present = result_set_size;
165 else if (result_set_size > largeSetLowerBound)
167 // large set . Return no records
168 number_to_present = 0;
172 // medium set . Return mediumSetPresentNumber records
173 number_to_present = mediumSetPresentNumber;
174 if (number_to_present > result_set_size)
175 number_to_present = result_set_size;
180 bool mp_util::pqf(ODR odr, Z_APDU *apdu, const std::string &q)
182 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
184 Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
187 yaz_pqf_destroy(pqf_parser);
190 yaz_pqf_destroy(pqf_parser);
191 Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
192 query->which = Z_Query_type_1;
193 query->u.type_1 = rpn;
195 apdu->u.searchRequest->query = query;
200 std::string mp_util::zQueryToString(Z_Query *query)
202 std::string query_str = "";
204 if (query && query->which == Z_Query_type_1){
205 Z_RPNQuery *rpn = query->u.type_1;
209 // allocate wrbuf (strings in YAZ!)
210 WRBUF w = wrbuf_alloc();
213 yaz_rpnquery_to_wrbuf(w, rpn);
215 // from w to std::string
216 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
224 if (query && query->which == Z_Query_type_1){
226 // allocate wrbuf (strings in YAZ!)
227 WRBUF w = wrbuf_alloc();
230 yaz_query_to_wrbuf(w, query);
232 // from w to std::string
233 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
242 void mp_util::get_default_diag(Z_DefaultDiagFormat *r,
243 int &error_code, std::string &addinfo)
245 error_code = *r->condition;
248 case Z_DefaultDiagFormat_v2Addinfo:
249 addinfo = std::string(r->u.v2Addinfo);
251 case Z_DefaultDiagFormat_v3Addinfo:
252 addinfo = r->u.v3Addinfo;
257 void mp_util::get_init_diagnostics(
258 Z_InitResponse *initrs, int &error_code, std::string &addinfo)
260 Z_External *uif = initrs->userInformationField;
262 if (uif && uif->which == Z_External_userInfo1)
264 Z_OtherInformation *ui = uif->u.userInfo1;
266 for (i = 0; i < ui->num_elements; i++)
268 Z_OtherInformationUnit *unit = ui->list[i];
269 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
270 unit->information.externallyDefinedInfo &&
271 unit->information.externallyDefinedInfo->which ==
274 Z_DiagnosticFormat *diag =
275 unit->information.externallyDefinedInfo->u.diag1;
279 Z_DiagnosticFormat_s *ds = diag->elements[0];
280 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
281 mp::util::get_default_diag(ds->u.defaultDiagRec,
282 error_code, addinfo);
289 int mp_util::get_or_remove_vhost_otherinfo(
290 Z_OtherInformation **otherInformation,
292 std::list<std::string> &vhosts)
295 const int *oid_proxy = yaz_string_to_oid(yaz_oid_std(),
298 for (cat = 1; ; cat++)
300 // check virtual host
302 yaz_oi_get_string_oid(otherInformation,
304 cat /* categoryValue */,
305 remove_flag /* delete flag */);
308 vhosts.push_back(std::string(vhost));
314 void mp_util::get_vhost_otherinfo(
315 Z_OtherInformation *otherInformation,
316 std::list<std::string> &vhosts)
318 get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
321 int mp_util::remove_vhost_otherinfo(
322 Z_OtherInformation **otherInformation,
323 std::list<std::string> &vhosts)
325 return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
328 void mp_util::set_vhost_otherinfo(
329 Z_OtherInformation **otherInformation, ODR odr,
330 const std::list<std::string> &vhosts)
333 std::list<std::string>::const_iterator it = vhosts.begin();
335 const int *oid_proxy = yaz_string_to_oid(yaz_oid_std(),
339 for (cat = 1; it != vhosts.end() ; cat++, it++)
341 yaz_oi_set_string_oid(otherInformation, odr,
342 oid_proxy, cat, it->c_str());
346 void mp_util::set_vhost_otherinfo(
347 Z_OtherInformation **otherInformation, ODR odr,
348 const std::string vhost, const int cat)
350 const int *oid_proxy = yaz_string_to_oid(yaz_oid_std(),
354 yaz_oi_set_string_oid(otherInformation, odr,
355 oid_proxy, cat, vhost.c_str());
358 void mp_util::split_zurl(std::string zurl, std::string &host,
359 std::list<std::string> &db)
361 const char *zurl_cstr = zurl.c_str();
362 const char *sep = strchr(zurl_cstr, '/');
366 host = std::string(zurl_cstr, sep - zurl_cstr);
368 const char *cp1 = sep+1;
371 const char *cp2 = strchr(cp1, '+');
373 db.push_back(std::string(cp1, cp2 - cp1));
376 db.push_back(std::string(cp1));
388 bool mp_util::set_databases_from_zurl(
389 ODR odr, std::string zurl,
390 int *db_num, char ***db_strings)
393 std::list<std::string> dblist;
395 split_zurl(zurl, host, dblist);
397 if (dblist.size() == 0)
399 *db_num = dblist.size();
400 *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
402 std::list<std::string>::const_iterator it = dblist.begin();
403 for (int i = 0; it != dblist.end(); it++, i++)
404 (*db_strings)[i] = odr_strdup(odr, it->c_str());
408 mp::odr::odr(int type)
410 m_odr = odr_createmem(type);
415 m_odr = odr_createmem(ODR_ENCODE);
423 mp::odr::operator ODR() const
428 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
429 int reason, const char *addinfo)
431 Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
433 *apdu->u.close->closeReason = reason;
435 apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
439 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
441 return mp::util::create_APDU(m_odr, type, in_apdu);
444 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
446 Z_APDU *out_apdu = zget_APDU(odr, type);
447 transfer_referenceId(odr, in_apdu, out_apdu);
451 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
453 Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
457 Z_ReferenceId **id_from = mp::util::get_referenceId(src);
458 if (id_from && *id_from && id_to)
460 *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
461 (*id_to)->size = (*id_to)->len = (*id_from)->len;
462 (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
463 memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
470 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
471 int error, const char *addinfo)
473 Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
476 apdu->u.initResponse->userInformationField =
477 zget_init_diagnostics(m_odr, error, addinfo);
478 *apdu->u.initResponse->result = 0;
483 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
484 int error, const char *addinfo)
486 Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
489 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
490 *apdu->u.searchResponse->searchStatus = 0;
491 apdu->u.searchResponse->records = rec;
492 rec->which = Z_Records_NSD;
493 rec->u.nonSurrogateDiagnostic =
494 zget_DefaultDiagFormat(m_odr, error, addinfo);
500 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
501 int error, const char *addinfo)
503 Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
506 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
507 apdu->u.presentResponse->records = rec;
509 rec->which = Z_Records_NSD;
510 rec->u.nonSurrogateDiagnostic =
511 zget_DefaultDiagFormat(m_odr, error, addinfo);
512 *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
517 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
518 int error, const char *addinfo)
520 Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
521 Z_ScanResponse *res = apdu->u.scanResponse;
522 res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
523 res->entries->num_entries = 0;
524 res->entries->entries = 0;
528 *res->scanStatus = Z_Scan_failure;
530 res->entries->num_nonsurrogateDiagnostics = 1;
531 res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
532 odr_malloc(m_odr, sizeof(Z_DiagRec *));
533 res->entries->nonsurrogateDiagnostics[0] =
534 zget_DiagRec(m_odr, error, addinfo);
538 res->entries->num_nonsurrogateDiagnostics = 0;
539 res->entries->nonsurrogateDiagnostics = 0;
544 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
545 Z_HTTP_Request *hreq, int code)
547 const char *response_version = "1.0";
548 bool keepalive = false;
549 if (!strcmp(hreq->version, "1.0"))
551 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
552 if (v && !strcmp(v, "Keep-Alive"))
556 response_version = "1.0";
560 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
561 if (v && !strcmp(v, "close"))
565 response_version = "1.1";
568 Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
569 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
570 hres->version = odr_strdup(m_odr, response_version);
572 z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
577 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
581 case Z_APDU_initRequest:
582 return &apdu->u.initRequest->referenceId;
583 case Z_APDU_initResponse:
584 return &apdu->u.initResponse->referenceId;
585 case Z_APDU_searchRequest:
586 return &apdu->u.searchRequest->referenceId;
587 case Z_APDU_searchResponse:
588 return &apdu->u.searchResponse->referenceId;
589 case Z_APDU_presentRequest:
590 return &apdu->u.presentRequest->referenceId;
591 case Z_APDU_presentResponse:
592 return &apdu->u.presentResponse->referenceId;
593 case Z_APDU_deleteResultSetRequest:
594 return &apdu->u.deleteResultSetRequest->referenceId;
595 case Z_APDU_deleteResultSetResponse:
596 return &apdu->u.deleteResultSetResponse->referenceId;
597 case Z_APDU_accessControlRequest:
598 return &apdu->u.accessControlRequest->referenceId;
599 case Z_APDU_accessControlResponse:
600 return &apdu->u.accessControlResponse->referenceId;
601 case Z_APDU_resourceControlRequest:
602 return &apdu->u.resourceControlRequest->referenceId;
603 case Z_APDU_resourceControlResponse:
604 return &apdu->u.resourceControlResponse->referenceId;
605 case Z_APDU_triggerResourceControlRequest:
606 return &apdu->u.triggerResourceControlRequest->referenceId;
607 case Z_APDU_resourceReportRequest:
608 return &apdu->u.resourceReportRequest->referenceId;
609 case Z_APDU_resourceReportResponse:
610 return &apdu->u.resourceReportResponse->referenceId;
611 case Z_APDU_scanRequest:
612 return &apdu->u.scanRequest->referenceId;
613 case Z_APDU_scanResponse:
614 return &apdu->u.scanResponse->referenceId;
615 case Z_APDU_sortRequest:
616 return &apdu->u.sortRequest->referenceId;
617 case Z_APDU_sortResponse:
618 return &apdu->u.sortResponse->referenceId;
619 case Z_APDU_segmentRequest:
620 return &apdu->u.segmentRequest->referenceId;
621 case Z_APDU_extendedServicesRequest:
622 return &apdu->u.extendedServicesRequest->referenceId;
623 case Z_APDU_extendedServicesResponse:
624 return &apdu->u.extendedServicesResponse->referenceId;
626 return &apdu->u.close->referenceId;
635 * indent-tabs-mode: nil
636 * c-file-style: "stroustrup"
638 * vim: shiftwidth=4 tabstop=8 expandtab