1 /* $Id: util.cpp,v 1.28 2007-04-16 21:54:52 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 for (cat = 1; ; cat++)
297 // check virtual host
299 yaz_oi_get_string_oid(otherInformation,
300 yaz_oid_userinfo_proxy,
301 cat /* categoryValue */,
302 remove_flag /* delete flag */);
305 vhosts.push_back(std::string(vhost));
311 void mp_util::get_vhost_otherinfo(
312 Z_OtherInformation *otherInformation,
313 std::list<std::string> &vhosts)
315 get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
318 int mp_util::remove_vhost_otherinfo(
319 Z_OtherInformation **otherInformation,
320 std::list<std::string> &vhosts)
322 return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
325 void mp_util::set_vhost_otherinfo(
326 Z_OtherInformation **otherInformation, ODR odr,
327 const std::list<std::string> &vhosts)
330 std::list<std::string>::const_iterator it = vhosts.begin();
332 for (cat = 1; it != vhosts.end() ; cat++, it++)
334 yaz_oi_set_string_oid(otherInformation, odr,
335 yaz_oid_userinfo_proxy, cat, it->c_str());
339 void mp_util::set_vhost_otherinfo(
340 Z_OtherInformation **otherInformation, ODR odr,
341 const std::string vhost, const int cat)
343 yaz_oi_set_string_oid(otherInformation, odr,
344 yaz_oid_userinfo_proxy, cat, vhost.c_str());
347 void mp_util::split_zurl(std::string zurl, std::string &host,
348 std::list<std::string> &db)
350 const char *zurl_cstr = zurl.c_str();
351 const char *sep = strchr(zurl_cstr, '/');
355 host = std::string(zurl_cstr, sep - zurl_cstr);
357 const char *cp1 = sep+1;
360 const char *cp2 = strchr(cp1, '+');
362 db.push_back(std::string(cp1, cp2 - cp1));
365 db.push_back(std::string(cp1));
377 bool mp_util::set_databases_from_zurl(
378 ODR odr, std::string zurl,
379 int *db_num, char ***db_strings)
382 std::list<std::string> dblist;
384 split_zurl(zurl, host, dblist);
386 if (dblist.size() == 0)
388 *db_num = dblist.size();
389 *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
391 std::list<std::string>::const_iterator it = dblist.begin();
392 for (int i = 0; it != dblist.end(); it++, i++)
393 (*db_strings)[i] = odr_strdup(odr, it->c_str());
397 mp::odr::odr(int type)
399 m_odr = odr_createmem(type);
404 m_odr = odr_createmem(ODR_ENCODE);
412 mp::odr::operator ODR() const
417 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
418 int reason, const char *addinfo)
420 Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
422 *apdu->u.close->closeReason = reason;
424 apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
428 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
430 return mp::util::create_APDU(m_odr, type, in_apdu);
433 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
435 Z_APDU *out_apdu = zget_APDU(odr, type);
436 transfer_referenceId(odr, in_apdu, out_apdu);
440 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
442 Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
446 Z_ReferenceId **id_from = mp::util::get_referenceId(src);
447 if (id_from && *id_from && id_to)
449 *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
450 (*id_to)->size = (*id_to)->len = (*id_from)->len;
451 (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
452 memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
459 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
460 int error, const char *addinfo)
462 Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
465 apdu->u.initResponse->userInformationField =
466 zget_init_diagnostics(m_odr, error, addinfo);
467 *apdu->u.initResponse->result = 0;
472 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
473 int error, const char *addinfo)
475 Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
478 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
479 *apdu->u.searchResponse->searchStatus = 0;
480 apdu->u.searchResponse->records = rec;
481 rec->which = Z_Records_NSD;
482 rec->u.nonSurrogateDiagnostic =
483 zget_DefaultDiagFormat(m_odr, error, addinfo);
489 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
490 int error, const char *addinfo)
492 Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
495 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
496 apdu->u.presentResponse->records = rec;
498 rec->which = Z_Records_NSD;
499 rec->u.nonSurrogateDiagnostic =
500 zget_DefaultDiagFormat(m_odr, error, addinfo);
501 *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
506 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
507 int error, const char *addinfo)
509 Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
510 Z_ScanResponse *res = apdu->u.scanResponse;
511 res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
512 res->entries->num_entries = 0;
513 res->entries->entries = 0;
517 *res->scanStatus = Z_Scan_failure;
519 res->entries->num_nonsurrogateDiagnostics = 1;
520 res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
521 odr_malloc(m_odr, sizeof(Z_DiagRec *));
522 res->entries->nonsurrogateDiagnostics[0] =
523 zget_DiagRec(m_odr, error, addinfo);
527 res->entries->num_nonsurrogateDiagnostics = 0;
528 res->entries->nonsurrogateDiagnostics = 0;
533 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
534 Z_HTTP_Request *hreq, int code)
536 const char *response_version = "1.0";
537 bool keepalive = false;
538 if (!strcmp(hreq->version, "1.0"))
540 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
541 if (v && !strcmp(v, "Keep-Alive"))
545 response_version = "1.0";
549 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
550 if (v && !strcmp(v, "close"))
554 response_version = "1.1";
557 Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
558 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
559 hres->version = odr_strdup(m_odr, response_version);
561 z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
566 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
570 case Z_APDU_initRequest:
571 return &apdu->u.initRequest->referenceId;
572 case Z_APDU_initResponse:
573 return &apdu->u.initResponse->referenceId;
574 case Z_APDU_searchRequest:
575 return &apdu->u.searchRequest->referenceId;
576 case Z_APDU_searchResponse:
577 return &apdu->u.searchResponse->referenceId;
578 case Z_APDU_presentRequest:
579 return &apdu->u.presentRequest->referenceId;
580 case Z_APDU_presentResponse:
581 return &apdu->u.presentResponse->referenceId;
582 case Z_APDU_deleteResultSetRequest:
583 return &apdu->u.deleteResultSetRequest->referenceId;
584 case Z_APDU_deleteResultSetResponse:
585 return &apdu->u.deleteResultSetResponse->referenceId;
586 case Z_APDU_accessControlRequest:
587 return &apdu->u.accessControlRequest->referenceId;
588 case Z_APDU_accessControlResponse:
589 return &apdu->u.accessControlResponse->referenceId;
590 case Z_APDU_resourceControlRequest:
591 return &apdu->u.resourceControlRequest->referenceId;
592 case Z_APDU_resourceControlResponse:
593 return &apdu->u.resourceControlResponse->referenceId;
594 case Z_APDU_triggerResourceControlRequest:
595 return &apdu->u.triggerResourceControlRequest->referenceId;
596 case Z_APDU_resourceReportRequest:
597 return &apdu->u.resourceReportRequest->referenceId;
598 case Z_APDU_resourceReportResponse:
599 return &apdu->u.resourceReportResponse->referenceId;
600 case Z_APDU_scanRequest:
601 return &apdu->u.scanRequest->referenceId;
602 case Z_APDU_scanResponse:
603 return &apdu->u.scanResponse->referenceId;
604 case Z_APDU_sortRequest:
605 return &apdu->u.sortRequest->referenceId;
606 case Z_APDU_sortResponse:
607 return &apdu->u.sortResponse->referenceId;
608 case Z_APDU_segmentRequest:
609 return &apdu->u.segmentRequest->referenceId;
610 case Z_APDU_extendedServicesRequest:
611 return &apdu->u.extendedServicesRequest->referenceId;
612 case Z_APDU_extendedServicesResponse:
613 return &apdu->u.extendedServicesResponse->referenceId;
615 return &apdu->u.close->referenceId;
624 * indent-tabs-mode: nil
625 * c-file-style: "stroustrup"
627 * vim: shiftwidth=4 tabstop=8 expandtab