1 /* $Id: util.cpp,v 1.13 2006-01-20 22:38:12 marc Exp $
2 Copyright (c) 2005, Index Data.
10 #include <yaz/pquery.h>
11 #include <yaz/otherinfo.h>
12 #include <yaz/querytowrbuf.h> // for yaz_query_to_wrbuf()
17 void yp2::util::piggyback(int smallSetUpperBound,
18 int largeSetLowerBound,
19 int mediumSetPresentNumber,
21 int &number_to_present)
23 // deal with piggyback
25 if (result_set_size < smallSetUpperBound)
27 // small set . Return all records in set
28 number_to_present = result_set_size;
30 else if (result_set_size > largeSetLowerBound)
32 // large set . Return no records
33 number_to_present = 0;
37 // medium set . Return mediumSetPresentNumber records
38 number_to_present = mediumSetPresentNumber;
39 if (number_to_present > result_set_size)
40 number_to_present = result_set_size;
45 bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
46 YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
48 Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
51 yaz_pqf_destroy(pqf_parser);
54 yaz_pqf_destroy(pqf_parser);
55 Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
56 query->which = Z_Query_type_1;
57 query->u.type_1 = rpn;
59 apdu->u.searchRequest->query = query;
64 std::string yp2::util::zQueryToString(Z_Query *query)
66 std::string query_str = "";
68 if (query && query->which == Z_Query_type_1){
69 Z_RPNQuery *rpn = query->u.type_1;
73 // allocate wrbuf (strings in YAZ!)
74 WRBUF w = wrbuf_alloc();
77 yaz_rpnquery_to_wrbuf(w, rpn);
79 // from w to std::string
80 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
88 if (query && query->which == Z_Query_type_1){
90 // allocate wrbuf (strings in YAZ!)
91 WRBUF w = wrbuf_alloc();
94 yaz_query_to_wrbuf(w, query);
96 // from w to std::string
97 query_str = std::string(wrbuf_buf(w), wrbuf_len(w));
106 void yp2::util::get_default_diag(Z_DefaultDiagFormat *r,
107 int &error_code, std::string &addinfo)
109 error_code = *r->condition;
112 case Z_DefaultDiagFormat_v2Addinfo:
113 addinfo = std::string(r->u.v2Addinfo);
115 case Z_DefaultDiagFormat_v3Addinfo:
116 addinfo = r->u.v3Addinfo;
121 void yp2::util::get_init_diagnostics(Z_InitResponse *initrs,
122 int &error_code, std::string &addinfo)
124 Z_External *uif = initrs->userInformationField;
126 if (uif && uif->which == Z_External_userInfo1)
128 Z_OtherInformation *ui = uif->u.userInfo1;
130 for (i = 0; i < ui->num_elements; i++)
132 Z_OtherInformationUnit *unit = ui->list[i];
133 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
134 unit->information.externallyDefinedInfo &&
135 unit->information.externallyDefinedInfo->which ==
138 Z_DiagnosticFormat *diag =
139 unit->information.externallyDefinedInfo->u.diag1;
143 Z_DiagnosticFormat_s *ds = diag->elements[0];
144 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
145 yp2::util::get_default_diag(ds->u.defaultDiagRec,
146 error_code, addinfo);
153 int yp2::util::get_vhost_otherinfo(Z_OtherInformation **otherInformation,
155 std::list<std::string> &vhosts)
158 for (cat = 1; ; cat++)
160 // check virtual host
162 yaz_oi_get_string_oidval(otherInformation,
164 cat /* categoryValue */,
165 remove_flag /* delete flag */);
168 vhosts.push_back(std::string(vhost));
174 void yp2::util::set_vhost_otherinfo(Z_OtherInformation **otherInformation,
176 const std::list<std::string> &vhosts)
179 std::list<std::string>::const_iterator it = vhosts.begin();
180 for (cat = 1; it != vhosts.end() ; cat++, it++)
182 yaz_oi_set_string_oidval(otherInformation, odr,
183 VAL_PROXY, cat, it->c_str());
187 void yp2::util::split_zurl(std::string zurl, std::string &host,
188 std::list<std::string> &db)
190 const char *zurl_cstr = zurl.c_str();
191 const char *sep = strchr(zurl_cstr, '/');
195 host = std::string(zurl_cstr, sep - zurl_cstr);
197 const char *cp1 = sep+1;
200 const char *cp2 = strchr(cp1, '+');
202 db.push_back(std::string(cp1, cp2 - cp1));
205 db.push_back(std::string(cp1));
217 bool yp2::util::set_databases_from_zurl(ODR odr, std::string zurl,
218 int *db_num, char ***db_strings)
221 std::list<std::string> dblist;
223 split_zurl(zurl, host, dblist);
225 if (dblist.size() == 0)
227 *db_num = dblist.size();
228 *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
230 std::list<std::string>::const_iterator it = dblist.begin();
231 for (int i = 0; it != dblist.end(); it++, i++)
232 (*db_strings)[i] = odr_strdup(odr, it->c_str());
236 yp2::odr::odr(int type)
238 m_odr = odr_createmem(type);
243 m_odr = odr_createmem(ODR_ENCODE);
251 yp2::odr::operator ODR() const
256 Z_APDU *yp2::odr::create_close(Z_APDU *in_apdu,
257 int reason, const char *addinfo)
259 Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
261 *apdu->u.close->closeReason = reason;
263 apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
267 Z_APDU *yp2::odr::create_APDU(int type, Z_APDU *in_apdu)
269 return yp2::util::create_APDU(m_odr, type, in_apdu);
272 Z_APDU *yp2::util::create_APDU(ODR odr, int type, Z_APDU *in_apdu)
274 Z_APDU *out_apdu = zget_APDU(odr, type);
276 Z_ReferenceId **id_to = yp2::util::get_referenceId(out_apdu);
280 Z_ReferenceId **id_from = yp2::util::get_referenceId(in_apdu);
281 if (id_from && *id_from && id_to)
283 *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
284 (*id_to)->size = (*id_to)->len = (*id_from)->len;
285 (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
286 memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
294 Z_APDU *yp2::odr::create_initResponse(Z_APDU *in_apdu,
295 int error, const char *addinfo)
297 Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
300 apdu->u.initResponse->userInformationField =
301 zget_init_diagnostics(m_odr, error, addinfo);
302 *apdu->u.initResponse->result = 0;
307 Z_APDU *yp2::odr::create_searchResponse(Z_APDU *in_apdu,
308 int error, const char *addinfo)
310 Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
313 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
314 *apdu->u.searchResponse->searchStatus = 0;
315 apdu->u.searchResponse->records = rec;
316 rec->which = Z_Records_NSD;
317 rec->u.nonSurrogateDiagnostic =
318 zget_DefaultDiagFormat(m_odr, error, addinfo);
324 Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
325 int error, const char *addinfo)
327 Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
330 Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
331 apdu->u.presentResponse->records = rec;
333 rec->which = Z_Records_NSD;
334 rec->u.nonSurrogateDiagnostic =
335 zget_DefaultDiagFormat(m_odr, error, addinfo);
336 *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
341 Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
342 int error, const char *addinfo)
344 Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
345 Z_ScanResponse *res = apdu->u.scanResponse;
346 res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
347 res->entries->num_entries = 0;
348 res->entries->entries = 0;
352 *res->scanStatus = Z_Scan_failure;
354 res->entries->num_nonsurrogateDiagnostics = 1;
355 res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
356 odr_malloc(m_odr, sizeof(Z_DiagRec *));
357 res->entries->nonsurrogateDiagnostics[0] =
358 zget_DiagRec(m_odr, error, addinfo);
362 res->entries->num_nonsurrogateDiagnostics = 0;
363 res->entries->nonsurrogateDiagnostics = 0;
368 Z_GDU *yp2::odr::create_HTTP_Response(yp2::Session &session,
369 Z_HTTP_Request *hreq, int code)
371 const char *response_version = "1.0";
372 bool keepalive = false;
373 if (!strcmp(hreq->version, "1.0"))
375 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
376 if (v && !strcmp(v, "Keep-Alive"))
380 response_version = "1.0";
384 const char *v = z_HTTP_header_lookup(hreq->headers, "Connection");
385 if (v && !strcmp(v, "close"))
389 response_version = "1.1";
392 Z_GDU *gdu = z_get_HTTP_Response(m_odr, code);
393 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
394 hres->version = odr_strdup(m_odr, response_version);
396 z_HTTP_header_add(m_odr, &hres->headers, "Connection", "Keep-Alive");
401 Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
405 case Z_APDU_initRequest:
406 return &apdu->u.initRequest->referenceId;
407 case Z_APDU_initResponse:
408 return &apdu->u.initResponse->referenceId;
409 case Z_APDU_searchRequest:
410 return &apdu->u.searchRequest->referenceId;
411 case Z_APDU_searchResponse:
412 return &apdu->u.searchResponse->referenceId;
413 case Z_APDU_presentRequest:
414 return &apdu->u.presentRequest->referenceId;
415 case Z_APDU_presentResponse:
416 return &apdu->u.presentResponse->referenceId;
417 case Z_APDU_deleteResultSetRequest:
418 return &apdu->u.deleteResultSetRequest->referenceId;
419 case Z_APDU_deleteResultSetResponse:
420 return &apdu->u.deleteResultSetResponse->referenceId;
421 case Z_APDU_accessControlRequest:
422 return &apdu->u.accessControlRequest->referenceId;
423 case Z_APDU_accessControlResponse:
424 return &apdu->u.accessControlResponse->referenceId;
425 case Z_APDU_resourceControlRequest:
426 return &apdu->u.resourceControlRequest->referenceId;
427 case Z_APDU_resourceControlResponse:
428 return &apdu->u.resourceControlResponse->referenceId;
429 case Z_APDU_triggerResourceControlRequest:
430 return &apdu->u.triggerResourceControlRequest->referenceId;
431 case Z_APDU_resourceReportRequest:
432 return &apdu->u.resourceReportRequest->referenceId;
433 case Z_APDU_resourceReportResponse:
434 return &apdu->u.resourceReportResponse->referenceId;
435 case Z_APDU_scanRequest:
436 return &apdu->u.scanRequest->referenceId;
437 case Z_APDU_scanResponse:
438 return &apdu->u.scanResponse->referenceId;
439 case Z_APDU_sortRequest:
440 return &apdu->u.sortRequest->referenceId;
441 case Z_APDU_sortResponse:
442 return &apdu->u.sortResponse->referenceId;
443 case Z_APDU_segmentRequest:
444 return &apdu->u.segmentRequest->referenceId;
445 case Z_APDU_extendedServicesRequest:
446 return &apdu->u.extendedServicesRequest->referenceId;
447 case Z_APDU_extendedServicesResponse:
448 return &apdu->u.extendedServicesResponse->referenceId;
450 return &apdu->u.close->referenceId;
459 * indent-tabs-mode: nil
460 * c-file-style: "stroustrup"
462 * vim: shiftwidth=4 tabstop=8 expandtab