1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM Z39.50 handling
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/otherinfo.h>
22 #include <yaz/pquery.h>
23 #include <yaz/marcdisp.h>
24 #include <yaz/diagbib1.h>
25 #include <yaz/charneg.h>
27 #include <yaz/query-charset.h>
28 #include <yaz/copy_types.h>
29 #include <yaz/snprintf.h>
30 #include <yaz/facet.h>
32 #include <yaz/shptr.h>
35 * This wrapper is just for logging failed lookups. It would be nicer
36 * if it could cause failure when a lookup fails, but that's hard.
38 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
39 oid_class oid_class, const char *str)
41 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
44 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
45 c, (int) oid_class, str);
49 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
52 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
53 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
55 str = ZOOM_options_get(p->options, "package-name");
57 req->packageName = odr_strdup(p->odr_out, str);
59 str = ZOOM_options_get(p->options, "user-id");
61 req->userId = odr_strdup_null(p->odr_out, str);
63 req->packageType = odr_oiddup(p->odr_out, oid);
65 str = ZOOM_options_get(p->options, "function");
68 if (!strcmp (str, "create"))
69 *req->function = Z_ExtendedServicesRequest_create;
70 if (!strcmp (str, "delete"))
71 *req->function = Z_ExtendedServicesRequest_delete;
72 if (!strcmp (str, "modify"))
73 *req->function = Z_ExtendedServicesRequest_modify;
76 str = ZOOM_options_get(p->options, "waitAction");
79 if (!strcmp (str, "wait"))
80 *req->waitAction = Z_ExtendedServicesRequest_wait;
81 if (!strcmp (str, "waitIfPossible"))
82 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
83 if (!strcmp (str, "dontWait"))
84 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
85 if (!strcmp (str, "dontReturnPackage"))
86 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
91 static const char *ill_array_lookup(void *clientData, const char *idx)
93 ZOOM_package p = (ZOOM_package) clientData;
94 return ZOOM_options_get(p->options, idx+4);
97 static Z_External *encode_ill_request(ZOOM_package p)
102 struct ill_get_ctl ctl;
104 ctl.odr = p->odr_out;
106 ctl.f = ill_array_lookup;
108 req = ill_get_ILLRequest(&ctl, "ill", 0);
110 if (!ill_Request(out, &req, 0, 0))
112 int ill_request_size;
113 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
115 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
120 int illRequest_size = 0;
121 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
123 r = (Z_External *) odr_malloc(out, sizeof(*r));
124 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
125 r->indirect_reference = 0;
127 r->which = Z_External_single;
129 r->u.single_ASN1_type =
130 odr_create_Odr_oct(out, illRequest_buf, illRequest_size);
135 static Z_ItemOrder *encode_item_order(ZOOM_package p)
137 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
141 req->which = Z_IOItemOrder_esRequest;
142 req->u.esRequest = (Z_IORequest *)
143 odr_malloc(p->odr_out,sizeof(Z_IORequest));
145 /* to keep part ... */
146 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
147 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
148 req->u.esRequest->toKeep->supplDescription = 0;
149 req->u.esRequest->toKeep->contact = (Z_IOContact *)
150 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
152 str = ZOOM_options_get(p->options, "contact-name");
153 req->u.esRequest->toKeep->contact->name =
154 odr_strdup_null(p->odr_out, str);
156 str = ZOOM_options_get(p->options, "contact-phone");
157 req->u.esRequest->toKeep->contact->phone =
158 odr_strdup_null(p->odr_out, str);
160 str = ZOOM_options_get(p->options, "contact-email");
161 req->u.esRequest->toKeep->contact->email =
162 odr_strdup_null(p->odr_out, str);
164 req->u.esRequest->toKeep->addlBilling = 0;
166 /* not to keep part ... */
167 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
168 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
170 str = ZOOM_options_get(p->options, "itemorder-setname");
175 req->u.esRequest->notToKeep->resultSetItem = 0;
178 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
179 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
181 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
182 odr_strdup(p->odr_out, str);
183 req->u.esRequest->notToKeep->resultSetItem->item =
184 odr_intdup(p->odr_out, 0);
186 str = ZOOM_options_get(p->options, "itemorder-item");
187 *req->u.esRequest->notToKeep->resultSetItem->item =
188 (str ? atoi(str) : 1);
191 str = ZOOM_options_getl(p->options, "doc", &len);
194 req->u.esRequest->notToKeep->itemRequest =
195 z_ext_record_xml(p->odr_out, str, len);
198 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
203 Z_APDU *create_admin_package(ZOOM_package p, int type,
204 Z_ESAdminOriginPartToKeep **toKeepP,
205 Z_ESAdminOriginPartNotToKeep **notToKeepP)
207 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
210 Z_ESAdminOriginPartToKeep *toKeep;
211 Z_ESAdminOriginPartNotToKeep *notToKeep;
212 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
213 const char *first_db = "Default";
215 char **db = ZOOM_connection_get_databases(p->connection,
221 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
223 r->indirect_reference = 0;
224 r->which = Z_External_ESAdmin;
226 r->u.adminService = (Z_Admin *)
227 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
228 r->u.adminService->which = Z_Admin_esRequest;
229 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
230 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
232 toKeep = r->u.adminService->u.esRequest->toKeep =
233 (Z_ESAdminOriginPartToKeep *)
234 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
235 toKeep->which = type;
236 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
237 toKeep->u.create = odr_nullval();
238 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
240 r->u.adminService->u.esRequest->notToKeep = notToKeep =
241 (Z_ESAdminOriginPartNotToKeep *)
242 odr_malloc(p->odr_out,
243 sizeof(*r->u.adminService->u.esRequest->notToKeep));
244 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
245 notToKeep->u.recordsWillFollow = odr_nullval();
249 *notToKeepP = notToKeep;
254 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
256 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
257 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
258 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
260 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
268 req->taskSpecificParameters = ext;
269 ext->direct_reference = req->packageType;
271 ext->indirect_reference = 0;
273 ext->which = Z_External_octet;
274 ext->u.single_ASN1_type = odr_create_Odr_oct(p->odr_out, doc, len);
278 static Z_APDU *create_update_package(ZOOM_package p)
281 const char *first_db = "Default";
283 char **db = ZOOM_connection_get_databases(p->connection, p->options,
284 &num_db, p->odr_out);
285 const char *action = ZOOM_options_get(p->options, "action");
286 int recordIdOpaque_len;
287 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
288 &recordIdOpaque_len);
289 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
291 const char *record_buf = ZOOM_options_getl(p->options, "record",
293 int recordOpaque_len;
294 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
296 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
297 const char *version = ZOOM_options_get(p->options, "updateVersion");
299 const char *correlationInfo_note =
300 ZOOM_options_get(p->options, "correlationInfo.note");
301 const char *correlationInfo_id =
302 ZOOM_options_get(p->options, "correlationInfo.id");
304 Odr_oid *syntax_oid = 0;
305 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
311 if (!record_buf && !recordOpaque_buf)
315 syntax_str = "SUTRS";
320 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
321 CLASS_RECSYN, syntax_str,
326 ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_SYNTAX, syntax_str);
336 package_oid = yaz_oid_extserv_database_update_first_version;
337 /* old update does not support specialUpdate */
339 action = "recordInsert";
343 action = "specialUpdate";
344 package_oid = yaz_oid_extserv_database_update_second_version;
348 action = "specialUpdate";
349 package_oid = yaz_oid_extserv_database_update;
352 ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_VERSION, version);
356 if (!strcmp(action, "recordInsert"))
357 action_no = Z_IUOriginPartToKeep_recordInsert;
358 else if (!strcmp(action, "recordReplace"))
359 action_no = Z_IUOriginPartToKeep_recordReplace;
360 else if (!strcmp(action, "recordDelete"))
361 action_no = Z_IUOriginPartToKeep_recordDelete;
362 else if (!strcmp(action, "elementUpdate"))
363 action_no = Z_IUOriginPartToKeep_elementUpdate;
364 else if (!strcmp(action, "specialUpdate"))
365 action_no = Z_IUOriginPartToKeep_specialUpdate;
368 ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_ACTION, action);
372 apdu = create_es_package(p, package_oid);
375 Z_IUOriginPartToKeep *toKeep;
376 Z_IUSuppliedRecords *notToKeep;
377 Z_External *r = (Z_External *)
378 odr_malloc(p->odr_out, sizeof(*r));
379 const char *elementSetName =
380 ZOOM_options_get(p->options, "elementSetName");
382 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
384 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
386 r->which = Z_External_update;
387 r->indirect_reference = 0;
388 r->u.update = (Z_IUUpdate *)
389 odr_malloc(p->odr_out, sizeof(*r->u.update));
391 r->u.update->which = Z_IUUpdate_esRequest;
392 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
393 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
394 toKeep = r->u.update->u.esRequest->toKeep =
395 (Z_IUOriginPartToKeep *)
396 odr_malloc(p->odr_out, sizeof(*toKeep));
398 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
401 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
403 toKeep->actionQualifier = 0;
404 toKeep->action = odr_intdup(p->odr_out, action_no);
406 notToKeep = r->u.update->u.esRequest->notToKeep =
407 (Z_IUSuppliedRecords *)
408 odr_malloc(p->odr_out, sizeof(*notToKeep));
410 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
411 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
412 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
413 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
414 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
417 notToKeep->elements[0]->u.opaque =
418 odr_create_Odr_oct(p->odr_out, recordIdOpaque,
421 else if (recordIdNumber)
423 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
425 notToKeep->elements[0]->u.number =
426 odr_intdup(p->odr_out, atoi(recordIdNumber));
429 notToKeep->elements[0]->u.opaque = 0;
430 notToKeep->elements[0]->supplementalId = 0;
431 if (correlationInfo_note || correlationInfo_id)
433 Z_IUCorrelationInfo *ci;
434 ci = notToKeep->elements[0]->correlationInfo =
435 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
436 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
437 ci->id = correlationInfo_id ?
438 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
441 notToKeep->elements[0]->correlationInfo = 0;
442 if (recordOpaque_buf)
444 notToKeep->elements[0]->record =
445 z_ext_record_oid_any(p->odr_out, syntax_oid,
446 recordOpaque_buf, recordOpaque_len);
450 notToKeep->elements[0]->record =
451 z_ext_record_oid(p->odr_out, syntax_oid,
452 record_buf, record_len);
457 ODR print = odr_createmem(ODR_PRINT);
459 z_APDU(print, &apdu, 0, 0);
466 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
469 for (i = 0; i < 200; i++)
473 Z_OtherInformation **oi;
478 sprintf(buf, "otherInfo%d", i);
479 val = ZOOM_options_get(c->options, buf);
482 cp = strchr(val, ':');
486 if (len >= sizeof(buf))
488 memcpy(buf, val, len);
491 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
497 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
503 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
508 Z_OtherInformation **oi;
510 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
515 Z_OtherInformation **oi;
517 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
520 otherInfo_attach(c, a, out);
521 if (!z_APDU(out, &a, 0, 0))
523 FILE *outf = fopen("/tmp/apdu.txt", "a");
526 ODR odr_pr = odr_createmem(ODR_PRINT);
527 fprintf(outf, "a=%p\n", a);
528 odr_setprint(odr_pr, outf);
529 z_APDU(odr_pr, &a, 0, 0);
532 yaz_log(c->log_api, "%p encoding_APDU: encoding failed", c);
533 ZOOM_set_error(c, ZOOM_ERROR_ENCODE, 0);
538 z_APDU(c->odr_print, &a, 0, 0);
540 z_APDU(c->odr_save, &a, 0, 0);
541 yaz_log(c->log_details, "%p encoding_APDU encoding OK", c);
545 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
549 if (encode_APDU(c, a, c->odr_out))
550 return zoom_complete;
551 yaz_log(c->log_details, "%p send APDU type=%d", c, a->which);
552 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
553 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
554 ZOOM_connection_put_event(c, event);
555 odr_reset(c->odr_out);
556 return ZOOM_send_buf(c);
559 zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c)
561 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
562 Z_InitRequest *ireq = apdu->u.initRequest;
563 Z_IdAuthentication *auth = (Z_IdAuthentication *)
564 odr_malloc(c->odr_out, sizeof(*auth));
566 ODR_MASK_SET(ireq->options, Z_Options_search);
567 ODR_MASK_SET(ireq->options, Z_Options_present);
568 ODR_MASK_SET(ireq->options, Z_Options_scan);
569 ODR_MASK_SET(ireq->options, Z_Options_sort);
570 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
571 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
573 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
574 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
575 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
577 ireq->implementationId =
578 odr_prepend(c->odr_out,
579 ZOOM_options_get(c->options, "implementationId"),
580 ireq->implementationId);
582 ireq->implementationName =
583 odr_prepend(c->odr_out,
584 ZOOM_options_get(c->options, "implementationName"),
585 odr_prepend(c->odr_out, "ZOOM-C",
586 ireq->implementationName));
588 ireq->implementationVersion =
589 odr_prepend(c->odr_out,
590 ZOOM_options_get(c->options, "implementationVersion"),
591 ireq->implementationVersion);
593 *ireq->maximumRecordSize = c->maximum_record_size;
594 *ireq->preferredMessageSize = c->preferred_message_size;
596 if (c->group || c->password)
598 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
599 pass->groupId = odr_strdup_null(c->odr_out, c->group);
600 pass->userId = odr_strdup_null(c->odr_out, c->user);
601 pass->password = odr_strdup_null(c->odr_out, c->password);
602 auth->which = Z_IdAuthentication_idPass;
603 auth->u.idPass = pass;
604 ireq->idAuthentication = auth;
608 auth->which = Z_IdAuthentication_open;
609 auth->u.open = odr_strdup(c->odr_out, c->user);
610 ireq->idAuthentication = auth;
614 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
615 yaz_oid_userinfo_proxy, 1, c->host_port);
617 if (c->charset || c->lang)
619 Z_OtherInformation **oi;
620 Z_OtherInformationUnit *oi_unit;
622 yaz_oi_APDU(apdu, &oi);
624 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
626 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
627 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
628 oi_unit->information.externallyDefinedInfo =
629 yaz_set_proposal_charneg_list(c->odr_out, " ",
630 c->charset, c->lang, 1);
634 return send_APDU(c, apdu);
637 static zoom_ret Z3950_send_search(ZOOM_connection c)
640 int lslb, ssub, mspn;
644 Z_SearchRequest *search_req;
645 const char *elementSetName;
646 const char *smallSetElementSetName;
647 const char *mediumSetElementSetName;
651 assert(c->tasks->which == ZOOM_TASK_SEARCH);
652 r = c->tasks->u.search.resultset;
654 apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
655 search_req = apdu->u.searchRequest;
657 yaz_log(c->log_details, "%p ZOOM_connection_send_search set=%p", c, r);
659 elementSetName = c->tasks->u.search.elementSetName;
660 smallSetElementSetName =
661 ZOOM_options_get(r->options, "smallSetElementSetName");
662 mediumSetElementSetName =
663 ZOOM_options_get(r->options, "mediumSetElementSetName");
665 if (!smallSetElementSetName)
666 smallSetElementSetName = elementSetName;
668 if (!mediumSetElementSetName)
669 mediumSetElementSetName = elementSetName;
671 facets = ZOOM_options_get(r->options, "facets");
673 Z_FacetList *facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
675 Z_OtherInformation **oi = &search_req->additionalSearchInfo;
676 yaz_oi_set_facetlist(oi, c->odr_out, facet_list);
679 yaz_log(YLOG_WARN, "Unable to parse facets: %s", facets);
685 /* prepare query for the search request */
686 search_req->query = ZOOM_query_get_Z_Query(r->query);
687 if (!search_req->query)
689 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
690 return zoom_complete;
692 if (search_req->query->which == Z_Query_type_1 ||
693 search_req->query->which == Z_Query_type_101)
695 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
698 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
702 search_req->query = yaz_copy_Z_Query(search_req->query,
705 r = yaz_query_charset_convert_rpnquery_check(
706 search_req->query->u.type_1,
710 { /* query could not be char converted */
711 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
712 return zoom_complete;
717 search_req->databaseNames = r->databaseNames;
718 search_req->num_databaseNames = r->num_databaseNames;
720 /* get syntax (no need to provide unless piggyback is in effect) */
721 syntax = c->tasks->u.search.syntax;
723 schema = c->tasks->u.search.schema;
725 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
726 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
727 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
728 if (lslb != -1 && ssub != -1 && mspn != -1)
730 /* So're a Z39.50 expert? Let's hope you don't do sort */
731 *search_req->largeSetLowerBound = lslb;
732 *search_req->smallSetUpperBound = ssub;
733 *search_req->mediumSetPresentNumber = mspn;
735 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
736 && r->piggyback && !r->r_sort_spec && !schema)
738 /* Regular piggyback - do it unless we're going to do sort */
739 *search_req->largeSetLowerBound = 2000000000;
740 *search_req->smallSetUpperBound = 1;
741 *search_req->mediumSetPresentNumber =
742 r->step>0 ? r->step : c->tasks->u.search.count;
746 /* non-piggyback. Need not provide elementsets or syntaxes .. */
747 smallSetElementSetName = 0;
748 mediumSetElementSetName = 0;
751 if (smallSetElementSetName && *smallSetElementSetName)
753 Z_ElementSetNames *esn = (Z_ElementSetNames *)
754 odr_malloc(c->odr_out, sizeof(*esn));
756 esn->which = Z_ElementSetNames_generic;
757 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
758 search_req->smallSetElementSetNames = esn;
760 if (mediumSetElementSetName && *mediumSetElementSetName)
762 Z_ElementSetNames *esn =(Z_ElementSetNames *)
763 odr_malloc(c->odr_out, sizeof(*esn));
765 esn->which = Z_ElementSetNames_generic;
766 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
767 search_req->mediumSetElementSetNames = esn;
770 search_req->preferredRecordSyntax =
771 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
775 if (c->support_named_resultsets)
779 /* find the lowest unused ordinal so that we re-use
780 result sets on the server. */
781 for (ord = 1; ; ord++)
784 sprintf(setname, "%d", ord);
785 for (rp = c->resultsets; rp; rp = rp->next)
786 if (rp->setname && !strcmp(rp->setname, setname))
791 r->setname = xstrdup(setname);
792 yaz_log(c->log_details, "%p ZOOM_connection_send_search: "
793 "allocating set %s", c, r->setname);
797 yaz_log(c->log_details, "%p ZOOM_connection_send_search: using "
799 r->setname = xstrdup("default");
801 ZOOM_options_set(r->options, "setname", r->setname);
803 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
804 return send_APDU(c, apdu);
807 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c)
810 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
811 Z_ScanRequest *req = apdu->u.scanRequest;
814 yaz_log(c->log_details, "%p send_scan", c);
816 return zoom_complete;
817 assert (c->tasks->which == ZOOM_TASK_SCAN);
818 scan = c->tasks->u.scan.scan;
820 z_query = ZOOM_query_get_Z_Query(scan->query);
822 /* Z39.50 scan can only carry RPN */
823 if (z_query->which == Z_Query_type_1 ||
824 z_query->which == Z_Query_type_101)
826 Z_RPNQuery *rpn = z_query->u.type_1;
827 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
830 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
833 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
835 yaz_query_charset_convert_rpnquery(
836 rpn, c->odr_out, cd);
840 req->attributeSet = rpn->attributeSetId;
841 if (!req->attributeSet)
842 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
843 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
844 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
846 req->termListAndStartPoint =
847 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
851 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
852 return zoom_complete;
857 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
858 return zoom_complete;
861 *req->numberOfTermsRequested =
862 ZOOM_options_get_int(scan->options, "number", 20);
864 req->preferredPositionInResponse =
865 odr_intdup(c->odr_out,
866 ZOOM_options_get_int(scan->options, "position", 1));
869 odr_intdup(c->odr_out,
870 ZOOM_options_get_int(scan->options, "stepSize", 0));
872 req->databaseNames = scan->databaseNames;
873 req->num_databaseNames = scan->num_databaseNames;
875 return send_APDU(c, apdu);
879 ZOOM_package_send(ZOOM_package p, const char *type)
886 odr_reset(p->odr_out);
889 if (!strcmp(type, "itemorder"))
891 apdu = create_es_package(p, yaz_oid_extserv_item_order);
894 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
896 r->direct_reference =
897 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
899 r->which = Z_External_itemOrder;
900 r->indirect_reference = 0;
901 r->u.itemOrder = encode_item_order(p);
903 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
906 else if (!strcmp(type, "create")) /* create database */
908 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
911 else if (!strcmp(type, "drop")) /* drop database */
913 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
916 else if (!strcmp(type, "commit")) /* commit changes */
918 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
921 else if (!strcmp(type, "update")) /* update record(s) */
923 apdu = create_update_package(p);
925 else if (!strcmp(type, "xmlupdate"))
927 apdu = create_xmlupdate_package(p);
931 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
935 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
937 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
938 p->buf_out = (char *) xmalloc(p->len_out);
939 memcpy(p->buf_out, buf, p->len_out);
944 while (ZOOM_event(1, &c))
951 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
954 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
956 char oid_name_buf[OID_STR_MAX];
957 const char *oid_name;
960 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
963 case Z_DefaultDiagFormat_v2Addinfo:
964 addinfo = r->u.v2Addinfo;
966 case Z_DefaultDiagFormat_v3Addinfo:
967 addinfo = r->u.v3Addinfo;
972 ZOOM_set_dset_error(c, *r->condition, oid_name, addinfo, 0);
975 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
977 if (p->which != Z_DiagRec_defaultFormat)
978 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
980 response_default_diag(c, p->u.defaultFormat);
983 static int es_response_taskpackage_update(ZOOM_connection c,
984 Z_IUUpdateTaskPackage *utp)
986 if (utp && utp->targetPart)
988 Z_IUTargetPart *targetPart = utp->targetPart;
989 switch (*targetPart->updateStatus)
991 case Z_IUTargetPart_success:
992 ZOOM_options_set(c->tasks->u.package->options,
993 "updateStatus", "success");
995 case Z_IUTargetPart_partial:
996 ZOOM_options_set(c->tasks->u.package->options,
997 "updateStatus", "partial");
999 case Z_IUTargetPart_failure:
1000 ZOOM_options_set(c->tasks->u.package->options,
1001 "updateStatus", "failure");
1002 if (targetPart->globalDiagnostics &&
1003 targetPart->num_globalDiagnostics > 0)
1004 response_diag(c, targetPart->globalDiagnostics[0]);
1007 /* NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED. */
1012 static int es_response_taskpackage(ZOOM_connection c,
1013 Z_TaskPackage *taskPackage)
1015 Odr_oct *id = taskPackage->targetReference;
1017 ZOOM_options_setl(c->tasks->u.package->options,
1018 "targetReference", (char*) id->buf, id->len);
1019 switch (*taskPackage->taskStatus)
1021 case Z_TaskPackage_pending:
1022 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
1024 case Z_TaskPackage_active:
1025 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
1027 case Z_TaskPackage_complete:
1028 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
1030 case Z_TaskPackage_aborted:
1031 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
1032 if (taskPackage->num_packageDiagnostics &&
1033 taskPackage->packageDiagnostics )
1034 response_diag(c, taskPackage->packageDiagnostics[0]);
1037 /* NOTE: Only Update implemented, no others. */
1038 if (taskPackage->taskSpecificParameters->which == Z_External_update)
1040 Z_IUUpdateTaskPackage *utp =
1041 taskPackage->taskSpecificParameters->u.update->u.taskPackage;
1042 es_response_taskpackage_update(c, utp);
1047 static void handle_Z3950_es_response(ZOOM_connection c,
1048 Z_ExtendedServicesResponse *res)
1052 assert(c->tasks->which == ZOOM_TASK_PACKAGE);
1053 switch (*res->operationStatus)
1055 case Z_ExtendedServicesResponse_done:
1056 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
1058 case Z_ExtendedServicesResponse_accepted:
1059 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
1061 case Z_ExtendedServicesResponse_failure:
1062 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
1063 if (res->diagnostics && res->num_diagnostics > 0)
1064 response_diag(c, res->diagnostics[0]);
1067 if (res->taskPackage &&
1068 res->taskPackage->which == Z_External_extendedService)
1070 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1071 es_response_taskpackage(c, taskPackage);
1073 if (res->taskPackage &&
1074 res->taskPackage->which == Z_External_octet)
1076 Odr_oct *doc = res->taskPackage->u.octet_aligned;
1077 ZOOM_options_setl(c->tasks->u.package->options,
1078 "xmlUpdateDoc", (char*) doc->buf, doc->len);
1082 static char *get_term_cstr(ODR odr, Z_Term *term)
1084 switch (term->which)
1086 case Z_Term_general:
1087 return odr_strdupn(odr, term->u.general->buf, term->u.general->len);
1089 case Z_Term_characterString:
1090 return odr_strdup(odr, term->u.characterString);
1095 static ZOOM_facet_field get_zoom_facet_field(ODR odr, Z_FacetField *facet)
1098 struct yaz_facet_attr attr_values;
1099 ZOOM_facet_field facet_field = odr_malloc(odr, sizeof(*facet_field));
1100 yaz_facet_attr_init(&attr_values);
1101 yaz_facet_attr_get_z_attributes(facet->attributes, &attr_values);
1102 facet_field->facet_name = odr_strdup(odr, attr_values.useattr);
1103 facet_field->num_terms = facet->num_terms;
1104 yaz_log(YLOG_DEBUG, "ZOOM_facet_field %s %d terms %d",
1105 attr_values.useattr, attr_values.limit, facet->num_terms);
1106 facet_field->facet_terms =
1107 odr_malloc(odr, facet->num_terms * sizeof(*facet_field->facet_terms));
1108 for (i = 0 ; i < facet->num_terms; i++)
1110 Z_FacetTerm *facetTerm = facet->terms[i];
1111 facet_field->facet_terms[i].frequency = *facetTerm->count;
1112 facet_field->facet_terms[i].term = get_term_cstr(odr, facetTerm->term);
1113 yaz_log(YLOG_DEBUG, " term[%d] %s %d",
1114 i, facet_field->facet_terms[i].term,
1115 facet_field->facet_terms[i].frequency);
1120 /* Can be share with SOLR/SRU/SRW requests */
1121 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl)
1124 r->num_facets = fl->num;
1125 yaz_log(YLOG_DEBUG, "Facets found: %d", fl->num);
1126 r->facets = odr_malloc(r->odr, fl->num * sizeof(*r->facets));
1127 r->facets_names = odr_malloc(r->odr, fl->num * sizeof(*r->facets_names));
1128 for (j = 0; j < fl->num; j++)
1130 r->facets[j] = get_zoom_facet_field(r->odr, fl->elements[j]);
1132 yaz_log(YLOG_DEBUG, "Facet field missing on index %d !", j);
1133 r->facets_names[j] = (char *) ZOOM_facet_field_name(r->facets[j]);
1137 static void handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
1138 Z_OtherInformation *o)
1141 for (i = 0; o && i < o->num_elements; i++)
1143 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1145 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1146 if (ext->which == Z_External_userFacets)
1148 ZOOM_handle_facet_list(r, ext->u.facetList);
1154 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
1157 switch (term->which)
1159 case Z_Term_general:
1160 ZOOM_options_setl(opt, name,
1161 term->u.general->buf, term->u.general->len);
1163 case Z_Term_characterString:
1164 ZOOM_options_set(opt, name, term->u.characterString);
1166 case Z_Term_numeric:
1167 ZOOM_options_set_int(opt, name, *term->u.numeric);
1172 static void handle_queryExpression(ZOOM_options opt, const char *name,
1173 Z_QueryExpression *exp)
1179 case Z_QueryExpression_term:
1180 if (exp->u.term && exp->u.term->queryTerm)
1182 sprintf(opt_name, "%s.term", name);
1183 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
1186 case Z_QueryExpression_query:
1192 static void handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
1193 Z_OtherInformation *o)
1196 for (i = 0; o && i < o->num_elements; i++)
1198 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1200 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1202 if (ext->which == Z_External_searchResult1)
1205 Z_SearchInfoReport *sr = ext->u.searchResult1;
1208 ZOOM_options_set_int(
1209 resultset->options, "searchresult.size", sr->num);
1211 for (j = 0; j < sr->num; j++)
1213 Z_SearchInfoReport_s *ent =
1214 ext->u.searchResult1->elements[j];
1217 sprintf(pref, "searchresult.%d", j);
1219 if (ent->subqueryId)
1222 sprintf(opt_name, "%s.id", pref);
1223 ZOOM_options_set(resultset->options, opt_name,
1226 if (ent->subqueryExpression)
1229 sprintf(opt_name, "%s.subquery", pref);
1230 handle_queryExpression(resultset->options, opt_name,
1231 ent->subqueryExpression);
1233 if (ent->subqueryInterpretation)
1236 sprintf(opt_name, "%s.interpretation", pref);
1237 handle_queryExpression(resultset->options, opt_name,
1238 ent->subqueryInterpretation);
1240 if (ent->subqueryRecommendation)
1243 sprintf(opt_name, "%s.recommendation", pref);
1244 handle_queryExpression(resultset->options, opt_name,
1245 ent->subqueryRecommendation);
1247 if (ent->subqueryCount)
1250 sprintf(opt_name, "%s.count", pref);
1251 ZOOM_options_set_int(resultset->options, opt_name,
1252 *ent->subqueryCount);
1260 static void handle_Z3950_search_response(ZOOM_connection c,
1261 Z_SearchResponse *sr)
1263 ZOOM_resultset resultset;
1266 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1269 resultset = c->tasks->u.search.resultset;
1271 if (resultset->live_set == 0)
1273 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
1274 ZOOM_connection_put_event(c, event);
1276 if (sr->resultSetStatus)
1278 ZOOM_options_set_int(resultset->options, "resultSetStatus",
1279 *sr->resultSetStatus);
1281 if (sr->presentStatus)
1283 ZOOM_options_set_int(resultset->options, "presentStatus",
1284 *sr->presentStatus);
1286 handle_search_result(c, resultset, sr->additionalSearchInfo);
1288 handle_facet_result(c, resultset, sr->additionalSearchInfo);
1290 resultset->size = *sr->resultCount;
1292 ZOOM_memcached_hitcount(c, resultset);
1293 resultset->live_set = 2;
1294 handle_Z3950_records(c, sr->records, 0);
1297 static void handle_Z3950_sort_response(ZOOM_connection c, Z_SortResponse *res)
1299 if (res->diagnostics && res->num_diagnostics > 0)
1300 response_diag(c, res->diagnostics[0]);
1303 static void handle_Z3950_scan_response(ZOOM_connection c, Z_ScanResponse *res)
1305 NMEM nmem = odr_extract_mem(c->odr_in);
1308 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1310 scan = c->tasks->u.scan.scan;
1312 if (res->entries && res->entries->nonsurrogateDiagnostics)
1313 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1314 scan->scan_response = res;
1315 scan->srw_scan_response = 0;
1316 nmem_transfer(odr_getmem(scan->odr), nmem);
1318 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
1319 if (res->positionOfTerm)
1320 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
1321 if (res->scanStatus)
1322 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
1323 if (res->numberOfEntriesReturned)
1324 ZOOM_options_set_int(scan->options, "number",
1325 *res->numberOfEntriesReturned);
1329 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
1332 ZOOM_resultset resultset;
1334 const char *syntax = 0, *elementSetName = 0, *schema = 0;
1336 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1339 resultset = c->tasks->u.search.resultset;
1340 start = &c->tasks->u.search.start;
1341 count = &c->tasks->u.search.count;
1342 syntax = c->tasks->u.search.syntax;
1343 elementSetName = c->tasks->u.search.elementSetName;
1344 schema = c->tasks->u.search.schema;
1346 if (sr && sr->which == Z_Records_NSD)
1347 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
1348 else if (sr && sr->which == Z_Records_multipleNSD)
1350 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1351 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1353 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1357 if (*count + *start > resultset->size)
1358 *count = resultset->size - *start;
1361 if (sr && sr->which == Z_Records_DBOSD)
1364 NMEM nmem = odr_extract_mem(c->odr_in);
1365 Z_NamePlusRecordList *p =
1366 sr->u.databaseOrSurDiagnostics;
1367 for (i = 0; i < p->num_records; i++)
1369 ZOOM_record_cache_add(resultset, p->records[i], i + *start,
1370 syntax, elementSetName, schema, 0);
1376 yaz_log(c->log_details,
1377 "handle_records resultset=%p start=%d count=%d",
1378 resultset, *start, *count);
1380 /* transfer our response to search_nmem .. we need it later */
1381 nmem_transfer(odr_getmem(resultset->odr), nmem);
1383 if (present_phase && p->num_records == 0)
1385 /* present response and we didn't get any records! */
1386 Z_NamePlusRecord *myrec =
1387 zget_surrogateDiagRec(
1389 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1390 "ZOOM C generated. Present phase and no records");
1391 ZOOM_record_cache_add(resultset, myrec, *start,
1392 syntax, elementSetName, schema, 0);
1396 else if (present_phase)
1398 /* present response and we didn't get any records! */
1399 Z_NamePlusRecord *myrec =
1400 zget_surrogateDiagRec(
1402 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1403 "ZOOM C generated: Present response and no records");
1404 ZOOM_record_cache_add(resultset, myrec, *start,
1405 syntax, elementSetName, schema, 0);
1411 static void handle_Z3950_present_response(ZOOM_connection c,
1412 Z_PresentResponse *pr)
1414 handle_Z3950_records(c, pr->records, 1);
1417 static void set_init_option(const char *name, void *clientData)
1419 ZOOM_connection c = (ZOOM_connection) clientData;
1422 sprintf(buf, "init_opt_%.70s", name);
1423 ZOOM_connection_option_set(c, buf, "1");
1426 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset)
1429 resultset->r_sort_spec = 0;
1430 if (resultset->r_sort_spec)
1432 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1433 Z_SortRequest *req = apdu->u.sortRequest;
1435 req->num_inputResultSetNames = 1;
1436 req->inputResultSetNames = (Z_InternationalString **)
1437 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
1438 req->inputResultSetNames[0] =
1439 odr_strdup(c->odr_out, resultset->setname);
1440 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
1441 req->sortSequence = resultset->r_sort_spec;
1442 resultset->r_sort_spec = 0;
1443 return send_APDU(c, apdu);
1445 return zoom_complete;
1448 static zoom_ret Z3950_send_present(ZOOM_connection c)
1450 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1451 Z_PresentRequest *req = apdu->u.presentRequest;
1452 ZOOM_resultset resultset = c->tasks->u.search.resultset;
1453 const char *syntax = c->tasks->u.search.syntax;
1454 const char *elementSetName = c->tasks->u.search.elementSetName;
1455 const char *schema = c->tasks->u.search.schema;
1457 *req->resultSetStartPoint = c->tasks->u.search.start + 1;
1459 if (resultset->step > 0 && resultset->step < c->tasks->u.search.count)
1460 *req->numberOfRecordsRequested = resultset->step;
1462 *req->numberOfRecordsRequested = c->tasks->u.search.count;
1464 if (*req->numberOfRecordsRequested + c->tasks->u.search.start > resultset->size)
1465 *req->numberOfRecordsRequested = resultset->size - c->tasks->u.search.start;
1466 assert(*req->numberOfRecordsRequested > 0);
1468 if (syntax && *syntax)
1469 req->preferredRecordSyntax =
1470 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1472 if (schema && *schema)
1474 Z_RecordComposition *compo = (Z_RecordComposition *)
1475 odr_malloc(c->odr_out, sizeof(*compo));
1477 req->recordComposition = compo;
1478 compo->which = Z_RecordComp_complex;
1479 compo->u.complex = (Z_CompSpec *)
1480 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1481 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1482 odr_malloc(c->odr_out, sizeof(bool_t));
1483 *compo->u.complex->selectAlternativeSyntax = 0;
1485 compo->u.complex->generic = (Z_Specification *)
1486 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1488 compo->u.complex->generic->which = Z_Schema_oid;
1489 compo->u.complex->generic->schema.oid = (Odr_oid *)
1490 zoom_yaz_str_to_z3950oid(c, CLASS_SCHEMA, schema);
1492 if (!compo->u.complex->generic->schema.oid)
1494 /* OID wasn't a schema! Try record syntax instead. */
1496 compo->u.complex->generic->schema.oid = (Odr_oid *)
1497 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, schema);
1499 if (elementSetName && *elementSetName)
1501 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1502 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1503 compo->u.complex->generic->elementSpec->which =
1504 Z_ElementSpec_elementSetName;
1505 compo->u.complex->generic->elementSpec->u.elementSetName =
1506 odr_strdup(c->odr_out, elementSetName);
1509 compo->u.complex->generic->elementSpec = 0;
1510 compo->u.complex->num_dbSpecific = 0;
1511 compo->u.complex->dbSpecific = 0;
1512 compo->u.complex->num_recordSyntax = 0;
1513 compo->u.complex->recordSyntax = 0;
1515 else if (elementSetName && *elementSetName)
1517 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1518 odr_malloc(c->odr_out, sizeof(*esn));
1519 Z_RecordComposition *compo = (Z_RecordComposition *)
1520 odr_malloc(c->odr_out, sizeof(*compo));
1522 esn->which = Z_ElementSetNames_generic;
1523 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
1524 compo->which = Z_RecordComp_simple;
1525 compo->u.simple = esn;
1526 req->recordComposition = compo;
1528 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1529 return send_APDU(c, apdu);
1532 zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c)
1535 const char *syntax = 0;
1536 const char *elementSetName = 0;
1537 const char *schema = 0;
1538 ZOOM_resultset resultset;
1542 return zoom_complete;
1543 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1544 resultset = c->tasks->u.search.resultset;
1545 start = &c->tasks->u.search.start;
1546 count = &c->tasks->u.search.count;
1547 syntax = c->tasks->u.search.syntax;
1548 elementSetName = c->tasks->u.search.elementSetName;
1549 schema = c->tasks->u.search.schema;
1551 yaz_log(c->log_details, "%p send_present start=%d count=%d",
1554 ZOOM_memcached_search(c, resultset);
1556 if (*start < 0 || *count < 0)
1558 ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
1559 "start/count < 0", 0);
1562 if (resultset->live_set)
1564 if (*start >= resultset->size)
1565 return zoom_complete;
1566 if (*start + *count > resultset->size)
1567 *count = resultset->size - *start;
1570 if (c->error) /* don't continue on error */
1571 return zoom_complete;
1572 yaz_log(c->log_details, "send_present resultset=%p start=%d count=%d",
1573 resultset, *start, *count);
1575 for (i = 0; i < *count; i++)
1578 ZOOM_record_cache_lookup(resultset, i + *start,
1579 syntax, elementSetName, schema);
1586 if (*count == 0 && resultset->live_set)
1587 return zoom_complete;
1589 if (resultset->live_set == 2)
1590 return Z3950_send_present(c);
1592 return Z3950_send_search(c);
1595 static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
1597 zoom_ret r = zoom_complete;
1599 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
1600 r = send_Z3950_sort(c, c->tasks->u.search.resultset);
1601 if (r == zoom_complete)
1602 r = ZOOM_connection_Z3950_search(c);
1606 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
1608 Z_InitResponse *initrs;
1610 ZOOM_connection_set_mask(c, 0);
1611 yaz_log(c->log_details, "%p handle_Z3950_apdu apdu->which=%d",
1613 switch (apdu->which)
1615 case Z_APDU_initResponse:
1616 yaz_log(c->log_api, "%p handle_Z3950_apdu: Received Init response", c);
1617 initrs = apdu->u.initResponse;
1618 ZOOM_connection_option_set(c, "serverImplementationId",
1619 initrs->implementationId ?
1620 initrs->implementationId : "");
1621 ZOOM_connection_option_set(c, "serverImplementationName",
1622 initrs->implementationName ?
1623 initrs->implementationName : "");
1624 ZOOM_connection_option_set(c, "serverImplementationVersion",
1625 initrs->implementationVersion ?
1626 initrs->implementationVersion : "");
1627 /* Set the three old options too, for old applications */
1628 ZOOM_connection_option_set(c, "targetImplementationId",
1629 initrs->implementationId ?
1630 initrs->implementationId : "");
1631 ZOOM_connection_option_set(c, "targetImplementationName",
1632 initrs->implementationName ?
1633 initrs->implementationName : "");
1634 ZOOM_connection_option_set(c, "targetImplementationVersion",
1635 initrs->implementationVersion ?
1636 initrs->implementationVersion : "");
1638 /* Make initrs->options available as ZOOM-level options */
1639 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
1641 if (!*initrs->result)
1643 Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs);
1645 response_default_diag(c, df);
1647 ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */
1652 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
1653 yaz_oid_userinfo_cookie, 1, 0);
1654 xfree(c->cookie_in);
1657 c->cookie_in = xstrdup(cookie);
1658 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1659 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1660 c->support_named_resultsets = 1;
1663 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1664 ZOOM_connection_remove_task(c);
1666 ZOOM_connection_exec_task(c);
1668 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1670 NMEM tmpmem = nmem_create();
1671 Z_CharSetandLanguageNegotiation *p =
1672 yaz_get_charneg_record(initrs->otherInfo);
1676 char *charset = NULL, *lang = NULL;
1679 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
1680 yaz_log(c->log_details, "%p handle_Z3950_apdu target accepted: "
1681 "charset %s, language %s, select %d",
1683 charset ? charset : "none", lang ? lang : "none", sel);
1685 ZOOM_connection_option_set(c, "negotiation-charset",
1688 ZOOM_connection_option_set(c, "negotiation-lang",
1691 ZOOM_connection_option_set(
1692 c, "negotiation-charset-in-effect-for-records",
1693 (sel != 0) ? "1" : "0");
1694 nmem_destroy(tmpmem);
1698 case Z_APDU_searchResponse:
1699 yaz_log(c->log_api, "%p handle_Z3950_apdu Search response", c);
1700 handle_Z3950_search_response(c, apdu->u.searchResponse);
1701 if (send_Z3950_sort_present(c) == zoom_complete)
1702 ZOOM_connection_remove_task(c);
1704 case Z_APDU_presentResponse:
1705 yaz_log(c->log_api, "%p handle_Z3950_apdu Present response", c);
1706 handle_Z3950_present_response(c, apdu->u.presentResponse);
1707 if (ZOOM_connection_Z3950_search(c) == zoom_complete)
1708 ZOOM_connection_remove_task(c);
1710 case Z_APDU_sortResponse:
1711 yaz_log(c->log_api, "%p handle_Z3950_apdu Sort response", c);
1712 handle_Z3950_sort_response(c, apdu->u.sortResponse);
1713 ZOOM_connection_remove_task(c);
1715 case Z_APDU_scanResponse:
1716 yaz_log(c->log_api, "%p handle_Z3950_apdu Scan response", c);
1717 handle_Z3950_scan_response(c, apdu->u.scanResponse);
1718 ZOOM_connection_remove_task(c);
1720 case Z_APDU_extendedServicesResponse:
1721 yaz_log(c->log_api, "%p handle_Z3950_apdu Extended Services response", c);
1722 handle_Z3950_es_response(c, apdu->u.extendedServicesResponse);
1723 ZOOM_connection_remove_task(c);
1726 yaz_log(c->log_api, "%p handle_Z3950_apdu Close PDU", c);
1727 if (!ZOOM_test_reconnect(c))
1729 ZOOM_set_dset_error(c, ZOOM_ERROR_CONNECTION_LOST, "ZOOM", c->host_port, apdu->u.close->diagnosticInformation);
1730 ZOOM_connection_close(c);
1734 yaz_log(c->log_api, "%p Received unknown PDU", c);
1735 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1736 ZOOM_connection_close(c);
1743 * c-file-style: "Stroustrup"
1744 * indent-tabs-mode: nil
1746 * vim: shiftwidth=4 tabstop=8 expandtab