1 /* $Id: util.cpp,v 1.26 2007-03-20 07:57:54 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> // for yaz_query_to_wrbuf()
17 namespace mp = metaproxy_1;
19 // Doxygen doesn't like mp::util, so we use this instead
20 namespace mp_util = metaproxy_1::util;
23 mp_util::record_composition_to_esn(Z_RecordComposition *comp)
25 if (comp && comp->which == Z_RecordComp_complex)
27 if (comp->u.complex->generic
28 && comp->u.complex->generic->elementSpec
29 && (comp->u.complex->generic->elementSpec->which ==
30 Z_ElementSpec_elementSetName))
31 return comp->u.complex->generic->elementSpec->u.elementSetName;
33 else if (comp && comp->which == Z_RecordComp_simple &&
34 comp->u.simple->which == Z_ElementSetNames_generic)
35 return comp->u.simple->u.generic;
41 std::string mp_util::http_header_value(const Z_HTTP_Header* header,
42 const std::string name)
44 while (header && header->name
45 && std::string(header->name) != name)
46 header = header->next;
48 if (header && header->name && std::string(header->name) == name
50 return std::string(header->value);
55 std::string mp_util::http_headers_debug(const Z_HTTP_Request &http_req)
57 std::string message("<html>\n<body>\n<h1>"
58 "Metaproxy SRUtoZ3950 filter"
61 message += "<h3>HTTP Info</h3><br/>\n";
63 message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
64 message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
65 message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
67 message += "<b>Content-Type:</b>"
68 + mp_util::http_header_value(http_req.headers, "Content-Type")
70 message += "<b>Content-Length:</b>"
71 + mp_util::http_header_value(http_req.headers, "Content-Length")
75 message += "<h3>Headers</h3><br/>\n";
77 Z_HTTP_Header* header = http_req.headers;
79 message += "<b>Header: </b> <i>"
80 + std::string(header->name) + ":</i> "
81 + std::string(header->value) + "<br/>\n";
82 header = header->next;
85 message += "</body>\n</html>\n";
90 void mp_util::http_response(metaproxy_1::Package &package,
91 const std::string &content,
95 Z_GDU *zgdu_req = package.request().get();
99 = odr.create_HTTP_Response(package.session(),
100 zgdu_req->u.HTTP_Request,
103 zgdu_res->u.HTTP_Response->content_len = content.size();
104 zgdu_res->u.HTTP_Response->content_buf
105 = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
107 strncpy(zgdu_res->u.HTTP_Response->content_buf,
108 content.c_str(), zgdu_res->u.HTTP_Response->content_len);
110 //z_HTTP_header_add(odr, &hres->headers,
111 // "Content-Type", content_type.c_str());
112 package.response() = zgdu_res;
116 int mp_util::memcmp2(const void *buf1, int len1,
117 const void *buf2, int len2)
121 // compare buffer (common length)
122 int c = memcmp(buf1, buf2, d > 0 ? len2 : len1);
128 // compare (remaining bytes)
137 std::string mp_util::database_name_normalize(const std::string &s)
141 for (i = 0; i < r.length(); i++)
144 if (ch >= 'A' && ch <= 'Z')
145 r[i] = ch + 'a' - 'A';
151 void mp_util::piggyback(int smallSetUpperBound,
152 int largeSetLowerBound,
153 int mediumSetPresentNumber,
155 int &number_to_present)
157 // deal with piggyback
159 if (result_set_size < smallSetUpperBound)
161 // small set . Return all records in set
162 number_to_present = result_set_size;
164 else if (result_set_size > largeSetLowerBound)
166 // large set . Return no records
167 number_to_present = 0;
171 // medium set . Return mediumSetPresentNumber records
172 number_to_present = mediumSetPresentNumber;
173 if (number_to_present > result_set_size)
174 number_to_present = result_set_size;
179 bool mp_util::pqf(ODR odr, Z_APDU *apdu, const std::string &q)
181 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
183 Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
186 yaz_pqf_destroy(pqf_parser);
189 yaz_pqf_destroy(pqf_parser);
190 Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
191 query->which = Z_Query_type_1;
192 query->u.type_1 = rpn;
194 apdu->u.searchRequest->query = query;
199 std::string mp_util::zQueryToString(Z_Query *query)
201 std::string query_str = "";
203 if (query && query->which == Z_Query_type_1){
204 Z_RPNQuery *rpn = query->u.type_1;
208 // allocate wrbuf (strings in YAZ!)
209 WRBUF w = wrbuf_alloc();
212 yaz_rpnquery_to_wrbuf(w, rpn);
214 // from w to std::string
215 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
223 if (query && query->which == Z_Query_type_1){
225 // allocate wrbuf (strings in YAZ!)
226 WRBUF w = wrbuf_alloc();
229 yaz_query_to_wrbuf(w, query);
231 // from w to std::string
232 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
241 void mp_util::get_default_diag(Z_DefaultDiagFormat *r,
242 int &error_code, std::string &addinfo)
244 error_code = *r->condition;
247 case Z_DefaultDiagFormat_v2Addinfo:
248 addinfo = std::string(r->u.v2Addinfo);
250 case Z_DefaultDiagFormat_v3Addinfo:
251 addinfo = r->u.v3Addinfo;
256 void mp_util::get_init_diagnostics(
257 Z_InitResponse *initrs, int &error_code, std::string &addinfo)
259 Z_External *uif = initrs->userInformationField;
261 if (uif && uif->which == Z_External_userInfo1)
263 Z_OtherInformation *ui = uif->u.userInfo1;
265 for (i = 0; i < ui->num_elements; i++)
267 Z_OtherInformationUnit *unit = ui->list[i];
268 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
269 unit->information.externallyDefinedInfo &&
270 unit->information.externallyDefinedInfo->which ==
273 Z_DiagnosticFormat *diag =
274 unit->information.externallyDefinedInfo->u.diag1;
278 Z_DiagnosticFormat_s *ds = diag->elements[0];
279 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
280 mp::util::get_default_diag(ds->u.defaultDiagRec,
281 error_code, addinfo);
288 int mp_util::get_or_remove_vhost_otherinfo(
289 Z_OtherInformation **otherInformation,
291 std::list<std::string> &vhosts)
294 for (cat = 1; ; cat++)
296 // check virtual host
298 yaz_oi_get_string_oidval(otherInformation,
300 cat /* categoryValue */,
301 remove_flag /* delete flag */);
304 vhosts.push_back(std::string(vhost));
310 void mp_util::get_vhost_otherinfo(
311 Z_OtherInformation *otherInformation,
312 std::list<std::string> &vhosts)
314 get_or_remove_vhost_otherinfo(&otherInformation, false, vhosts);
317 int mp_util::remove_vhost_otherinfo(
318 Z_OtherInformation **otherInformation,
319 std::list<std::string> &vhosts)
321 return get_or_remove_vhost_otherinfo(otherInformation, true, vhosts);
324 void mp_util::set_vhost_otherinfo(
325 Z_OtherInformation **otherInformation, ODR odr,
326 const std::list<std::string> &vhosts)
329 std::list<std::string>::const_iterator it = vhosts.begin();
330 for (cat = 1; it != vhosts.end() ; cat++, it++)
332 yaz_oi_set_string_oidval(otherInformation, odr,
333 VAL_PROXY, cat, it->c_str());
337 void mp_util::set_vhost_otherinfo(
338 Z_OtherInformation **otherInformation, ODR odr,
339 const std::string vhost, const int cat)
341 yaz_oi_set_string_oidval(otherInformation, odr,
342 VAL_PROXY, cat, vhost.c_str());
345 void mp_util::split_zurl(std::string zurl, std::string &host,
346 std::list<std::string> &db)
348 const char *zurl_cstr = zurl.c_str();
349 const char *sep = strchr(zurl_cstr, '/');
353 host = std::string(zurl_cstr, sep - zurl_cstr);
355 const char *cp1 = sep+1;
358 const char *cp2 = strchr(cp1, '+');
360 db.push_back(std::string(cp1, cp2 - cp1));
363 db.push_back(std::string(cp1));
375 bool mp_util::set_databases_from_zurl(
376 ODR odr, std::string zurl,
377 int *db_num, char ***db_strings)
380 std::list<std::string> dblist;
382 split_zurl(zurl, host, dblist);
384 if (dblist.size() == 0)
386 *db_num = dblist.size();
387 *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
389 std::list<std::string>::const_iterator it = dblist.begin();
390 for (int i = 0; it != dblist.end(); it++, i++)
391 (*db_strings)[i] = odr_strdup(odr, it->c_str());
395 mp::odr::odr(int type)
397 m_odr = odr_createmem(type);
402 m_odr = odr_createmem(ODR_ENCODE);
410 mp::odr::operator ODR() const
415 Z_APDU *mp::odr::create_close(const Z_APDU *in_apdu,
416 int reason, const char *addinfo)
418 Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
420 *apdu->u.close->closeReason = reason;
422 apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
426 Z_APDU *mp::odr::create_APDU(int type, const Z_APDU *in_apdu)
428 return mp::util::create_APDU(m_odr, type, in_apdu);
431 Z_APDU *mp_util::create_APDU(ODR odr, int type, const Z_APDU *in_apdu)
433 Z_APDU *out_apdu = zget_APDU(odr, type);
434 transfer_referenceId(odr, in_apdu, out_apdu);
438 void mp_util::transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst)
440 Z_ReferenceId **id_to = mp::util::get_referenceId(dst);
444 Z_ReferenceId **id_from = mp::util::get_referenceId(src);
445 if (id_from && *id_from && id_to)
447 *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
448 (*id_to)->size = (*id_to)->len = (*id_from)->len;
449 (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
450 memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
457 Z_APDU *mp::odr::create_initResponse(const Z_APDU *in_apdu,
458 int error, const char *addinfo)
460 Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
463 apdu->u.initResponse->userInformationField =
464 zget_init_diagnostics(m_odr, error, addinfo);
465 *apdu->u.initResponse->result = 0;
470 Z_APDU *mp::odr::create_searchResponse(const Z_APDU *in_apdu,
471 int error, const char *addinfo)
473 Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
476 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
477 *apdu->u.searchResponse->searchStatus = 0;
478 apdu->u.searchResponse->records = rec;
479 rec->which = Z_Records_NSD;
480 rec->u.nonSurrogateDiagnostic =
481 zget_DefaultDiagFormat(m_odr, error, addinfo);
487 Z_APDU *mp::odr::create_presentResponse(const Z_APDU *in_apdu,
488 int error, const char *addinfo)
490 Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
493 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
494 apdu->u.presentResponse->records = rec;
496 rec->which = Z_Records_NSD;
497 rec->u.nonSurrogateDiagnostic =
498 zget_DefaultDiagFormat(m_odr, error, addinfo);
499 *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
504 Z_APDU *mp::odr::create_scanResponse(const Z_APDU *in_apdu,
505 int error, const char *addinfo)
507 Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
508 Z_ScanResponse *res = apdu->u.scanResponse;
509 res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
510 res->entries->num_entries = 0;
511 res->entries->entries = 0;
515 *res->scanStatus = Z_Scan_failure;
517 res->entries->num_nonsurrogateDiagnostics = 1;
518 res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
519 odr_malloc(m_odr, sizeof(Z_DiagRec *));
520 res->entries->nonsurrogateDiagnostics[0] =
521 zget_DiagRec(m_odr, error, addinfo);
525 res->entries->num_nonsurrogateDiagnostics = 0;
526 res->entries->nonsurrogateDiagnostics = 0;
531 Z_GDU *mp::odr::create_HTTP_Response(mp::Session &session,
532 Z_HTTP_Request *hreq, int code)
534 const char *response_version = "1.0";
535 bool keepalive = false;
536 if (!strcmp(hreq->version, "1.0"))
538 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
539 if (v && !strcmp(v, "Keep-Alive"))
543 response_version = "1.0";
547 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
548 if (v && !strcmp(v, "close"))
552 response_version = "1.1";
555 Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
556 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
557 hres->version = odr_strdup(m_odr, response_version);
559 z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
564 Z_ReferenceId **mp_util::get_referenceId(const Z_APDU *apdu)
568 case Z_APDU_initRequest:
569 return &apdu->u.initRequest->referenceId;
570 case Z_APDU_initResponse:
571 return &apdu->u.initResponse->referenceId;
572 case Z_APDU_searchRequest:
573 return &apdu->u.searchRequest->referenceId;
574 case Z_APDU_searchResponse:
575 return &apdu->u.searchResponse->referenceId;
576 case Z_APDU_presentRequest:
577 return &apdu->u.presentRequest->referenceId;
578 case Z_APDU_presentResponse:
579 return &apdu->u.presentResponse->referenceId;
580 case Z_APDU_deleteResultSetRequest:
581 return &apdu->u.deleteResultSetRequest->referenceId;
582 case Z_APDU_deleteResultSetResponse:
583 return &apdu->u.deleteResultSetResponse->referenceId;
584 case Z_APDU_accessControlRequest:
585 return &apdu->u.accessControlRequest->referenceId;
586 case Z_APDU_accessControlResponse:
587 return &apdu->u.accessControlResponse->referenceId;
588 case Z_APDU_resourceControlRequest:
589 return &apdu->u.resourceControlRequest->referenceId;
590 case Z_APDU_resourceControlResponse:
591 return &apdu->u.resourceControlResponse->referenceId;
592 case Z_APDU_triggerResourceControlRequest:
593 return &apdu->u.triggerResourceControlRequest->referenceId;
594 case Z_APDU_resourceReportRequest:
595 return &apdu->u.resourceReportRequest->referenceId;
596 case Z_APDU_resourceReportResponse:
597 return &apdu->u.resourceReportResponse->referenceId;
598 case Z_APDU_scanRequest:
599 return &apdu->u.scanRequest->referenceId;
600 case Z_APDU_scanResponse:
601 return &apdu->u.scanResponse->referenceId;
602 case Z_APDU_sortRequest:
603 return &apdu->u.sortRequest->referenceId;
604 case Z_APDU_sortResponse:
605 return &apdu->u.sortResponse->referenceId;
606 case Z_APDU_segmentRequest:
607 return &apdu->u.segmentRequest->referenceId;
608 case Z_APDU_extendedServicesRequest:
609 return &apdu->u.extendedServicesRequest->referenceId;
610 case Z_APDU_extendedServicesResponse:
611 return &apdu->u.extendedServicesResponse->referenceId;
613 return &apdu->u.close->referenceId;
622 * indent-tabs-mode: nil
623 * c-file-style: "stroustrup"
625 * vim: shiftwidth=4 tabstop=8 expandtab