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) {
40 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
43 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
44 c, (int) oid_class, str);
48 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
51 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
52 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
54 str = ZOOM_options_get(p->options, "package-name");
56 req->packageName = odr_strdup(p->odr_out, str);
58 str = ZOOM_options_get(p->options, "user-id");
60 req->userId = odr_strdup_null(p->odr_out, str);
62 req->packageType = odr_oiddup(p->odr_out, oid);
64 str = ZOOM_options_get(p->options, "function");
67 if (!strcmp (str, "create"))
68 *req->function = Z_ExtendedServicesRequest_create;
69 if (!strcmp (str, "delete"))
70 *req->function = Z_ExtendedServicesRequest_delete;
71 if (!strcmp (str, "modify"))
72 *req->function = Z_ExtendedServicesRequest_modify;
75 str = ZOOM_options_get(p->options, "waitAction");
78 if (!strcmp (str, "wait"))
79 *req->waitAction = Z_ExtendedServicesRequest_wait;
80 if (!strcmp (str, "waitIfPossible"))
81 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
82 if (!strcmp (str, "dontWait"))
83 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
84 if (!strcmp (str, "dontReturnPackage"))
85 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
90 static const char *ill_array_lookup(void *clientData, const char *idx)
92 ZOOM_package p = (ZOOM_package) clientData;
93 return ZOOM_options_get(p->options, idx+4);
96 static Z_External *encode_ill_request(ZOOM_package p)
101 struct ill_get_ctl ctl;
103 ctl.odr = p->odr_out;
105 ctl.f = ill_array_lookup;
107 req = ill_get_ILLRequest(&ctl, "ill", 0);
109 if (!ill_Request(out, &req, 0, 0))
111 int ill_request_size;
112 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
114 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
119 int illRequest_size = 0;
120 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
122 r = (Z_External *) odr_malloc(out, sizeof(*r));
123 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
124 r->indirect_reference = 0;
126 r->which = Z_External_single;
128 r->u.single_ASN1_type =
129 odr_create_Odr_oct(out, illRequest_buf, illRequest_size);
134 static Z_ItemOrder *encode_item_order(ZOOM_package p)
136 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
140 req->which = Z_IOItemOrder_esRequest;
141 req->u.esRequest = (Z_IORequest *)
142 odr_malloc(p->odr_out,sizeof(Z_IORequest));
144 /* to keep part ... */
145 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
146 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
147 req->u.esRequest->toKeep->supplDescription = 0;
148 req->u.esRequest->toKeep->contact = (Z_IOContact *)
149 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
151 str = ZOOM_options_get(p->options, "contact-name");
152 req->u.esRequest->toKeep->contact->name =
153 odr_strdup_null(p->odr_out, str);
155 str = ZOOM_options_get(p->options, "contact-phone");
156 req->u.esRequest->toKeep->contact->phone =
157 odr_strdup_null(p->odr_out, str);
159 str = ZOOM_options_get(p->options, "contact-email");
160 req->u.esRequest->toKeep->contact->email =
161 odr_strdup_null(p->odr_out, str);
163 req->u.esRequest->toKeep->addlBilling = 0;
165 /* not to keep part ... */
166 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
167 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
169 str = ZOOM_options_get(p->options, "itemorder-setname");
174 req->u.esRequest->notToKeep->resultSetItem = 0;
177 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
178 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
180 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
181 odr_strdup(p->odr_out, str);
182 req->u.esRequest->notToKeep->resultSetItem->item =
183 odr_intdup(p->odr_out, 0);
185 str = ZOOM_options_get(p->options, "itemorder-item");
186 *req->u.esRequest->notToKeep->resultSetItem->item =
187 (str ? atoi(str) : 1);
190 str = ZOOM_options_getl(p->options, "doc", &len);
193 req->u.esRequest->notToKeep->itemRequest =
194 z_ext_record_xml(p->odr_out, str, len);
197 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
202 Z_APDU *create_admin_package(ZOOM_package p, int type,
203 Z_ESAdminOriginPartToKeep **toKeepP,
204 Z_ESAdminOriginPartNotToKeep **notToKeepP)
206 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
209 Z_ESAdminOriginPartToKeep *toKeep;
210 Z_ESAdminOriginPartNotToKeep *notToKeep;
211 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
212 const char *first_db = "Default";
214 char **db = ZOOM_connection_get_databases(p->connection,
220 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
222 r->indirect_reference = 0;
223 r->which = Z_External_ESAdmin;
225 r->u.adminService = (Z_Admin *)
226 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
227 r->u.adminService->which = Z_Admin_esRequest;
228 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
229 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
231 toKeep = r->u.adminService->u.esRequest->toKeep =
232 (Z_ESAdminOriginPartToKeep *)
233 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
234 toKeep->which = type;
235 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
236 toKeep->u.create = odr_nullval();
237 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
239 r->u.adminService->u.esRequest->notToKeep = notToKeep =
240 (Z_ESAdminOriginPartNotToKeep *)
241 odr_malloc(p->odr_out,
242 sizeof(*r->u.adminService->u.esRequest->notToKeep));
243 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
244 notToKeep->u.recordsWillFollow = odr_nullval();
248 *notToKeepP = notToKeep;
253 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
255 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
256 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
257 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
259 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
267 req->taskSpecificParameters = ext;
268 ext->direct_reference = req->packageType;
270 ext->indirect_reference = 0;
272 ext->which = Z_External_octet;
273 ext->u.single_ASN1_type = odr_create_Odr_oct(p->odr_out, doc, len);
277 static Z_APDU *create_update_package(ZOOM_package p)
280 const char *first_db = "Default";
282 char **db = ZOOM_connection_get_databases(p->connection, p->options,
283 &num_db, p->odr_out);
284 const char *action = ZOOM_options_get(p->options, "action");
285 int recordIdOpaque_len;
286 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
287 &recordIdOpaque_len);
288 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
290 const char *record_buf = ZOOM_options_getl(p->options, "record",
292 int recordOpaque_len;
293 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
295 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
296 const char *version = ZOOM_options_get(p->options, "updateVersion");
298 const char *correlationInfo_note =
299 ZOOM_options_get(p->options, "correlationInfo.note");
300 const char *correlationInfo_id =
301 ZOOM_options_get(p->options, "correlationInfo.id");
303 Odr_oid *syntax_oid = 0;
304 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
310 if (!record_buf && !recordOpaque_buf)
314 syntax_str = "SUTRS";
319 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
320 CLASS_RECSYN, syntax_str,
325 ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_SYNTAX, syntax_str);
335 package_oid = yaz_oid_extserv_database_update_first_version;
336 /* old update does not support specialUpdate */
338 action = "recordInsert";
342 action = "specialUpdate";
343 package_oid = yaz_oid_extserv_database_update_second_version;
347 action = "specialUpdate";
348 package_oid = yaz_oid_extserv_database_update;
351 ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_VERSION, version);
355 if (!strcmp(action, "recordInsert"))
356 action_no = Z_IUOriginPartToKeep_recordInsert;
357 else if (!strcmp(action, "recordReplace"))
358 action_no = Z_IUOriginPartToKeep_recordReplace;
359 else if (!strcmp(action, "recordDelete"))
360 action_no = Z_IUOriginPartToKeep_recordDelete;
361 else if (!strcmp(action, "elementUpdate"))
362 action_no = Z_IUOriginPartToKeep_elementUpdate;
363 else if (!strcmp(action, "specialUpdate"))
364 action_no = Z_IUOriginPartToKeep_specialUpdate;
367 ZOOM_set_error(p->connection, ZOOM_ERROR_ES_INVALID_ACTION, action);
371 apdu = create_es_package(p, package_oid);
374 Z_IUOriginPartToKeep *toKeep;
375 Z_IUSuppliedRecords *notToKeep;
376 Z_External *r = (Z_External *)
377 odr_malloc(p->odr_out, sizeof(*r));
378 const char *elementSetName =
379 ZOOM_options_get(p->options, "elementSetName");
381 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
383 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
385 r->which = Z_External_update;
386 r->indirect_reference = 0;
387 r->u.update = (Z_IUUpdate *)
388 odr_malloc(p->odr_out, sizeof(*r->u.update));
390 r->u.update->which = Z_IUUpdate_esRequest;
391 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
392 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
393 toKeep = r->u.update->u.esRequest->toKeep =
394 (Z_IUOriginPartToKeep *)
395 odr_malloc(p->odr_out, sizeof(*toKeep));
397 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
400 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
402 toKeep->actionQualifier = 0;
403 toKeep->action = odr_intdup(p->odr_out, action_no);
405 notToKeep = r->u.update->u.esRequest->notToKeep =
406 (Z_IUSuppliedRecords *)
407 odr_malloc(p->odr_out, sizeof(*notToKeep));
409 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
410 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
411 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
412 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
413 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
416 notToKeep->elements[0]->u.opaque =
417 odr_create_Odr_oct(p->odr_out, recordIdOpaque,
420 else if (recordIdNumber)
422 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
424 notToKeep->elements[0]->u.number =
425 odr_intdup(p->odr_out, atoi(recordIdNumber));
428 notToKeep->elements[0]->u.opaque = 0;
429 notToKeep->elements[0]->supplementalId = 0;
430 if (correlationInfo_note || correlationInfo_id)
432 Z_IUCorrelationInfo *ci;
433 ci = notToKeep->elements[0]->correlationInfo =
434 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
435 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
436 ci->id = correlationInfo_id ?
437 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
440 notToKeep->elements[0]->correlationInfo = 0;
441 if (recordOpaque_buf)
443 notToKeep->elements[0]->record =
444 z_ext_record_oid_any(p->odr_out, syntax_oid,
445 recordOpaque_buf, recordOpaque_len);
449 notToKeep->elements[0]->record =
450 z_ext_record_oid(p->odr_out, syntax_oid,
451 record_buf, record_len);
456 ODR print = odr_createmem(ODR_PRINT);
458 z_APDU(print, &apdu, 0, 0);
465 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
468 for (i = 0; i<200; i++)
472 Z_OtherInformation **oi;
477 sprintf(buf, "otherInfo%d", i);
478 val = ZOOM_options_get(c->options, buf);
481 cp = strchr(val, ':');
485 if (len >= sizeof(buf))
487 memcpy(buf, val, len);
490 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
496 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
502 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
507 Z_OtherInformation **oi;
509 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
514 Z_OtherInformation **oi;
516 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
519 otherInfo_attach(c, a, out);
520 if (!z_APDU(out, &a, 0, 0))
522 FILE *outf = fopen("/tmp/apdu.txt", "a");
525 ODR odr_pr = odr_createmem(ODR_PRINT);
526 fprintf(outf, "a=%p\n", a);
527 odr_setprint(odr_pr, outf);
528 z_APDU(odr_pr, &a, 0, 0);
531 yaz_log(c->log_api, "%p encoding_APDU: encoding failed", c);
532 ZOOM_set_error(c, ZOOM_ERROR_ENCODE, 0);
537 z_APDU(c->odr_print, &a, 0, 0);
539 z_APDU(c->odr_save, &a, 0, 0);
540 yaz_log(c->log_details, "%p encoding_APDU encoding OK", c);
544 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
548 if (encode_APDU(c, a, c->odr_out))
549 return zoom_complete;
550 yaz_log(c->log_details, "%p send APDU type=%d", c, a->which);
551 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
552 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
553 ZOOM_connection_put_event(c, event);
554 odr_reset(c->odr_out);
555 return ZOOM_send_buf(c);
558 zoom_ret ZOOM_connection_Z3950_send_init(ZOOM_connection c)
560 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
561 Z_InitRequest *ireq = apdu->u.initRequest;
562 Z_IdAuthentication *auth = (Z_IdAuthentication *)
563 odr_malloc(c->odr_out, sizeof(*auth));
565 ODR_MASK_SET(ireq->options, Z_Options_search);
566 ODR_MASK_SET(ireq->options, Z_Options_present);
567 ODR_MASK_SET(ireq->options, Z_Options_scan);
568 ODR_MASK_SET(ireq->options, Z_Options_sort);
569 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
570 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
572 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
573 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
574 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
576 ireq->implementationId =
577 odr_prepend(c->odr_out,
578 ZOOM_options_get(c->options, "implementationId"),
579 ireq->implementationId);
581 ireq->implementationName =
582 odr_prepend(c->odr_out,
583 ZOOM_options_get(c->options, "implementationName"),
584 odr_prepend(c->odr_out, "ZOOM-C",
585 ireq->implementationName));
587 ireq->implementationVersion =
588 odr_prepend(c->odr_out,
589 ZOOM_options_get(c->options, "implementationVersion"),
590 ireq->implementationVersion);
592 *ireq->maximumRecordSize = c->maximum_record_size;
593 *ireq->preferredMessageSize = c->preferred_message_size;
595 if (c->group || c->password)
597 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
598 pass->groupId = odr_strdup_null(c->odr_out, c->group);
599 pass->userId = odr_strdup_null(c->odr_out, c->user);
600 pass->password = odr_strdup_null(c->odr_out, c->password);
601 auth->which = Z_IdAuthentication_idPass;
602 auth->u.idPass = pass;
603 ireq->idAuthentication = auth;
607 auth->which = Z_IdAuthentication_open;
608 auth->u.open = odr_strdup(c->odr_out, c->user);
609 ireq->idAuthentication = auth;
613 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
614 yaz_oid_userinfo_proxy, 1, c->host_port);
616 if (c->charset || c->lang)
618 Z_OtherInformation **oi;
619 Z_OtherInformationUnit *oi_unit;
621 yaz_oi_APDU(apdu, &oi);
623 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
625 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
626 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
627 oi_unit->information.externallyDefinedInfo =
628 yaz_set_proposal_charneg_list(c->odr_out, " ",
629 c->charset, c->lang, 1);
633 return send_APDU(c, apdu);
636 static zoom_ret Z3950_send_search(ZOOM_connection c)
639 int lslb, ssub, mspn;
643 Z_SearchRequest *search_req;
644 const char *elementSetName;
645 const char *smallSetElementSetName;
646 const char *mediumSetElementSetName;
650 assert(c->tasks->which == ZOOM_TASK_SEARCH);
651 r = c->tasks->u.search.resultset;
653 apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
654 search_req = apdu->u.searchRequest;
656 yaz_log(c->log_details, "%p ZOOM_connection_send_search set=%p", c, r);
658 elementSetName = c->tasks->u.search.elementSetName;
659 smallSetElementSetName =
660 ZOOM_options_get(r->options, "smallSetElementSetName");
661 mediumSetElementSetName =
662 ZOOM_options_get(r->options, "mediumSetElementSetName");
664 if (!smallSetElementSetName)
665 smallSetElementSetName = elementSetName;
667 if (!mediumSetElementSetName)
668 mediumSetElementSetName = elementSetName;
670 facets = ZOOM_options_get(r->options, "facets");
672 Z_FacetList *facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
674 Z_OtherInformation **oi = &search_req->additionalSearchInfo;
675 yaz_oi_set_facetlist(oi, c->odr_out, facet_list);
678 yaz_log(YLOG_WARN, "Unable to parse facets: %s", facets);
684 /* prepare query for the search request */
685 search_req->query = ZOOM_query_get_Z_Query(r->query);
686 if (!search_req->query)
688 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
689 return zoom_complete;
691 if (search_req->query->which == Z_Query_type_1 ||
692 search_req->query->which == Z_Query_type_101)
694 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
697 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
701 search_req->query = yaz_copy_Z_Query(search_req->query,
704 r = yaz_query_charset_convert_rpnquery_check(
705 search_req->query->u.type_1,
709 { /* query could not be char converted */
710 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
711 return zoom_complete;
716 search_req->databaseNames = r->databaseNames;
717 search_req->num_databaseNames = r->num_databaseNames;
719 /* get syntax (no need to provide unless piggyback is in effect) */
720 syntax = c->tasks->u.search.syntax;
722 schema = c->tasks->u.search.schema;
724 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
725 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
726 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
727 if (lslb != -1 && ssub != -1 && mspn != -1)
729 /* So're a Z39.50 expert? Let's hope you don't do sort */
730 *search_req->largeSetLowerBound = lslb;
731 *search_req->smallSetUpperBound = ssub;
732 *search_req->mediumSetPresentNumber = mspn;
734 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
735 && r->piggyback && !r->r_sort_spec && !schema)
737 /* Regular piggyback - do it unless we're going to do sort */
738 *search_req->largeSetLowerBound = 2000000000;
739 *search_req->smallSetUpperBound = 1;
740 *search_req->mediumSetPresentNumber =
741 r->step>0 ? r->step : c->tasks->u.search.count;
745 /* non-piggyback. Need not provide elementsets or syntaxes .. */
746 smallSetElementSetName = 0;
747 mediumSetElementSetName = 0;
750 if (smallSetElementSetName && *smallSetElementSetName)
752 Z_ElementSetNames *esn = (Z_ElementSetNames *)
753 odr_malloc(c->odr_out, sizeof(*esn));
755 esn->which = Z_ElementSetNames_generic;
756 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
757 search_req->smallSetElementSetNames = esn;
759 if (mediumSetElementSetName && *mediumSetElementSetName)
761 Z_ElementSetNames *esn =(Z_ElementSetNames *)
762 odr_malloc(c->odr_out, sizeof(*esn));
764 esn->which = Z_ElementSetNames_generic;
765 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
766 search_req->mediumSetElementSetNames = esn;
769 search_req->preferredRecordSyntax =
770 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
774 if (c->support_named_resultsets)
778 /* find the lowest unused ordinal so that we re-use
779 result sets on the server. */
780 for (ord = 1; ; ord++)
783 sprintf(setname, "%d", ord);
784 for (rp = c->resultsets; rp; rp = rp->next)
785 if (rp->setname && !strcmp(rp->setname, setname))
790 r->setname = xstrdup(setname);
791 yaz_log(c->log_details, "%p ZOOM_connection_send_search: "
792 "allocating set %s", c, r->setname);
796 yaz_log(c->log_details, "%p ZOOM_connection_send_search: using "
798 r->setname = xstrdup("default");
800 ZOOM_options_set(r->options, "setname", r->setname);
802 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
803 return send_APDU(c, apdu);
806 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c)
809 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
810 Z_ScanRequest *req = apdu->u.scanRequest;
813 yaz_log(c->log_details, "%p send_scan", c);
815 return zoom_complete;
816 assert (c->tasks->which == ZOOM_TASK_SCAN);
817 scan = c->tasks->u.scan.scan;
819 z_query = ZOOM_query_get_Z_Query(scan->query);
821 /* Z39.50 scan can only carry RPN */
822 if (z_query->which == Z_Query_type_1 ||
823 z_query->which == Z_Query_type_101)
825 Z_RPNQuery *rpn = z_query->u.type_1;
826 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
829 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
832 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
834 yaz_query_charset_convert_rpnquery(
835 rpn, c->odr_out, cd);
839 req->attributeSet = rpn->attributeSetId;
840 if (!req->attributeSet)
841 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
842 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
843 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
845 req->termListAndStartPoint =
846 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
850 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
851 return zoom_complete;
856 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
857 return zoom_complete;
860 *req->numberOfTermsRequested =
861 ZOOM_options_get_int(scan->options, "number", 20);
863 req->preferredPositionInResponse =
864 odr_intdup(c->odr_out,
865 ZOOM_options_get_int(scan->options, "position", 1));
868 odr_intdup(c->odr_out,
869 ZOOM_options_get_int(scan->options, "stepSize", 0));
871 req->databaseNames = scan->databaseNames;
872 req->num_databaseNames = scan->num_databaseNames;
874 return send_APDU(c, apdu);
878 ZOOM_package_send(ZOOM_package p, const char *type)
885 odr_reset(p->odr_out);
888 if (!strcmp(type, "itemorder"))
890 apdu = create_es_package(p, yaz_oid_extserv_item_order);
893 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
895 r->direct_reference =
896 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
898 r->which = Z_External_itemOrder;
899 r->indirect_reference = 0;
900 r->u.itemOrder = encode_item_order(p);
902 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
905 else if (!strcmp(type, "create")) /* create database */
907 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
910 else if (!strcmp(type, "drop")) /* drop database */
912 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
915 else if (!strcmp(type, "commit")) /* commit changes */
917 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
920 else if (!strcmp(type, "update")) /* update record(s) */
922 apdu = create_update_package(p);
924 else if (!strcmp(type, "xmlupdate"))
926 apdu = create_xmlupdate_package(p);
930 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
934 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
936 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
937 p->buf_out = (char *) xmalloc(p->len_out);
938 memcpy(p->buf_out, buf, p->len_out);
943 while (ZOOM_event(1, &c))
950 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
953 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
955 char oid_name_buf[OID_STR_MAX];
956 const char *oid_name;
959 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
962 case Z_DefaultDiagFormat_v2Addinfo:
963 addinfo = r->u.v2Addinfo;
965 case Z_DefaultDiagFormat_v3Addinfo:
966 addinfo = r->u.v3Addinfo;
971 ZOOM_set_dset_error(c, *r->condition, oid_name, addinfo, 0);
974 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
976 if (p->which != Z_DiagRec_defaultFormat)
977 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
979 response_default_diag(c, p->u.defaultFormat);
982 static int es_response_taskpackage_update(ZOOM_connection c,
983 Z_IUUpdateTaskPackage *utp)
985 if (utp && utp->targetPart)
987 Z_IUTargetPart *targetPart = utp->targetPart;
988 switch ( *targetPart->updateStatus ) {
989 case Z_IUTargetPart_success:
990 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
992 case Z_IUTargetPart_partial:
993 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
995 case Z_IUTargetPart_failure:
996 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
997 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
998 response_diag(c, targetPart->globalDiagnostics[0]);
1001 /* NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED. */
1006 static int es_response_taskpackage(ZOOM_connection c,
1007 Z_TaskPackage *taskPackage)
1009 Odr_oct *id = taskPackage->targetReference;
1011 ZOOM_options_setl(c->tasks->u.package->options,
1012 "targetReference", (char*) id->buf, id->len);
1014 switch ( *taskPackage->taskStatus ) {
1015 case Z_TaskPackage_pending:
1016 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
1018 case Z_TaskPackage_active:
1019 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
1021 case Z_TaskPackage_complete:
1022 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
1024 case Z_TaskPackage_aborted:
1025 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
1026 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
1027 response_diag(c, taskPackage->packageDiagnostics[0]);
1030 /* NOTE: Only Update implemented, no others. */
1031 if ( taskPackage->taskSpecificParameters->which == Z_External_update )
1033 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
1034 es_response_taskpackage_update(c, utp);
1039 static void handle_Z3950_es_response(ZOOM_connection c,
1040 Z_ExtendedServicesResponse *res)
1044 assert(c->tasks->which == ZOOM_TASK_PACKAGE);
1045 switch (*res->operationStatus)
1047 case Z_ExtendedServicesResponse_done:
1048 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
1050 case Z_ExtendedServicesResponse_accepted:
1051 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
1053 case Z_ExtendedServicesResponse_failure:
1054 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
1055 if (res->diagnostics && res->num_diagnostics > 0)
1056 response_diag(c, res->diagnostics[0]);
1059 if (res->taskPackage &&
1060 res->taskPackage->which == Z_External_extendedService)
1062 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1063 es_response_taskpackage(c, taskPackage);
1065 if (res->taskPackage &&
1066 res->taskPackage->which == Z_External_octet)
1068 Odr_oct *doc = res->taskPackage->u.octet_aligned;
1069 ZOOM_options_setl(c->tasks->u.package->options,
1070 "xmlUpdateDoc", (char*) doc->buf, doc->len);
1074 static char *get_term_cstr(ODR odr, Z_Term *term) {
1076 switch (term->which) {
1077 case Z_Term_general:
1078 return odr_strdupn(odr, (const char *) term->u.general->buf, (size_t) term->u.general->len);
1080 case Z_Term_characterString:
1081 return odr_strdup(odr, term->u.characterString);
1086 static ZOOM_facet_field get_zoom_facet_field(ODR odr, Z_FacetField *facet) {
1088 struct yaz_facet_attr attr_values;
1089 ZOOM_facet_field facet_field = odr_malloc(odr, sizeof(*facet_field));
1090 yaz_facet_attr_init(&attr_values);
1091 yaz_facet_attr_get_z_attributes(facet->attributes, &attr_values);
1092 facet_field->facet_name = odr_strdup(odr, attr_values.useattr);
1093 facet_field->num_terms = facet->num_terms;
1094 yaz_log(YLOG_DEBUG, "ZOOM_facet_field %s %d terms %d", attr_values.useattr, attr_values.limit, facet->num_terms);
1095 facet_field->facet_terms = odr_malloc(odr, facet_field->num_terms * sizeof(*facet_field->facet_terms));
1096 for (term_index = 0 ; term_index < facet->num_terms; term_index++) {
1097 Z_FacetTerm *facetTerm = facet->terms[term_index];
1098 facet_field->facet_terms[term_index].frequency = *facetTerm->count;
1099 facet_field->facet_terms[term_index].term = get_term_cstr(odr, facetTerm->term);
1100 yaz_log(YLOG_DEBUG, " term[%d] %s %d",
1101 term_index, facet_field->facet_terms[term_index].term, facet_field->facet_terms[term_index].frequency);
1106 /* Can be share with SOLR/SRU/SRW requests */
1107 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl)
1110 r->num_facets = fl->num;
1111 yaz_log(YLOG_DEBUG, "Facets found: %d", fl->num);
1112 r->facets = odr_malloc(r->odr, r->num_facets * sizeof(*r->facets));
1113 r->facets_names = odr_malloc(r->odr, r->num_facets * sizeof(*r->facets_names));
1114 for (j = 0; j < fl->num; j++)
1116 r->facets[j] = get_zoom_facet_field(r->odr, fl->elements[j]);
1118 yaz_log(YLOG_DEBUG, "Facet field missing on index %d !", j);
1119 r->facets_names[j] = (char *) ZOOM_facet_field_name(r->facets[j]);
1123 static void handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
1124 Z_OtherInformation *o)
1127 for (i = 0; o && i < o->num_elements; i++)
1129 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1131 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1132 if (ext->which == Z_External_userFacets)
1134 ZOOM_handle_facet_list(r, ext->u.facetList);
1140 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
1143 switch (term->which)
1145 case Z_Term_general:
1146 ZOOM_options_setl(opt, name,
1147 (const char *)(term->u.general->buf),
1148 term->u.general->len);
1150 case Z_Term_characterString:
1151 ZOOM_options_set(opt, name, term->u.characterString);
1153 case Z_Term_numeric:
1154 ZOOM_options_set_int(opt, name, *term->u.numeric);
1159 static void handle_queryExpression(ZOOM_options opt, const char *name,
1160 Z_QueryExpression *exp)
1166 case Z_QueryExpression_term:
1167 if (exp->u.term && exp->u.term->queryTerm)
1169 sprintf(opt_name, "%s.term", name);
1170 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
1173 case Z_QueryExpression_query:
1179 static void handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
1180 Z_OtherInformation *o)
1183 for (i = 0; o && i < o->num_elements; i++)
1185 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1187 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1189 if (ext->which == Z_External_searchResult1)
1192 Z_SearchInfoReport *sr = ext->u.searchResult1;
1195 ZOOM_options_set_int(
1196 resultset->options, "searchresult.size", sr->num);
1198 for (j = 0; j < sr->num; j++)
1200 Z_SearchInfoReport_s *ent =
1201 ext->u.searchResult1->elements[j];
1204 sprintf(pref, "searchresult.%d", j);
1206 if (ent->subqueryId)
1209 sprintf(opt_name, "%s.id", pref);
1210 ZOOM_options_set(resultset->options, opt_name,
1213 if (ent->subqueryExpression)
1216 sprintf(opt_name, "%s.subquery", pref);
1217 handle_queryExpression(resultset->options, opt_name,
1218 ent->subqueryExpression);
1220 if (ent->subqueryInterpretation)
1223 sprintf(opt_name, "%s.interpretation", pref);
1224 handle_queryExpression(resultset->options, opt_name,
1225 ent->subqueryInterpretation);
1227 if (ent->subqueryRecommendation)
1230 sprintf(opt_name, "%s.recommendation", pref);
1231 handle_queryExpression(resultset->options, opt_name,
1232 ent->subqueryRecommendation);
1234 if (ent->subqueryCount)
1237 sprintf(opt_name, "%s.count", pref);
1238 ZOOM_options_set_int(resultset->options, opt_name,
1239 *ent->subqueryCount);
1247 static void handle_Z3950_search_response(ZOOM_connection c,
1248 Z_SearchResponse *sr)
1250 ZOOM_resultset resultset;
1253 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1256 resultset = c->tasks->u.search.resultset;
1258 if (resultset->live_set == 0)
1260 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
1261 ZOOM_connection_put_event(c, event);
1263 if (sr->resultSetStatus)
1265 ZOOM_options_set_int(resultset->options, "resultSetStatus",
1266 *sr->resultSetStatus);
1268 if (sr->presentStatus)
1270 ZOOM_options_set_int(resultset->options, "presentStatus",
1271 *sr->presentStatus);
1273 handle_search_result(c, resultset, sr->additionalSearchInfo);
1275 handle_facet_result(c, resultset, sr->additionalSearchInfo);
1277 resultset->size = *sr->resultCount;
1278 resultset->live_set = 2;
1280 #if HAVE_LIBMEMCACHED_MEMCACHED_H
1284 memcached_return_t rc;
1285 time_t expiration = 36000;
1288 sprintf(str, ODR_INT_PRINTF, *sr->resultCount);
1289 rc = memcached_set(c->mc_st,
1290 wrbuf_buf(resultset->mc_key),wrbuf_len(resultset->mc_key),
1291 str, strlen(str), expiration, flags);
1292 yaz_log(YLOG_LOG, "Store Z39.50 hit count key=%s value=%s rc=%u %s",
1293 wrbuf_cstr(resultset->mc_key), str, (unsigned) rc,
1294 memcached_last_error_message(c->mc_st));
1297 handle_Z3950_records(c, sr->records, 0);
1300 static void handle_Z3950_sort_response(ZOOM_connection c, Z_SortResponse *res)
1302 if (res->diagnostics && res->num_diagnostics > 0)
1303 response_diag(c, res->diagnostics[0]);
1306 static void handle_Z3950_scan_response(ZOOM_connection c, Z_ScanResponse *res)
1308 NMEM nmem = odr_extract_mem(c->odr_in);
1311 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1313 scan = c->tasks->u.scan.scan;
1315 if (res->entries && res->entries->nonsurrogateDiagnostics)
1316 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1317 scan->scan_response = res;
1318 scan->srw_scan_response = 0;
1319 nmem_transfer(odr_getmem(scan->odr), nmem);
1321 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
1322 if (res->positionOfTerm)
1323 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
1324 if (res->scanStatus)
1325 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
1326 if (res->numberOfEntriesReturned)
1327 ZOOM_options_set_int(scan->options, "number",
1328 *res->numberOfEntriesReturned);
1332 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
1335 ZOOM_resultset resultset;
1337 const char *syntax = 0, *elementSetName = 0, *schema = 0;
1339 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1342 resultset = c->tasks->u.search.resultset;
1343 start = &c->tasks->u.search.start;
1344 count = &c->tasks->u.search.count;
1345 syntax = c->tasks->u.search.syntax;
1346 elementSetName = c->tasks->u.search.elementSetName;
1347 schema = c->tasks->u.search.schema;
1349 if (sr && sr->which == Z_Records_NSD)
1350 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
1351 else if (sr && sr->which == Z_Records_multipleNSD)
1353 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1354 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1356 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1360 if (*count + *start > resultset->size)
1361 *count = resultset->size - *start;
1364 if (sr && sr->which == Z_Records_DBOSD)
1367 NMEM nmem = odr_extract_mem(c->odr_in);
1368 Z_NamePlusRecordList *p =
1369 sr->u.databaseOrSurDiagnostics;
1370 for (i = 0; i<p->num_records; i++)
1372 ZOOM_record_cache_add(resultset, p->records[i], i + *start,
1373 syntax, elementSetName, schema, 0);
1379 yaz_log(c->log_details,
1380 "handle_records resultset=%p start=%d count=%d",
1381 resultset, *start, *count);
1383 /* transfer our response to search_nmem .. we need it later */
1384 nmem_transfer(odr_getmem(resultset->odr), nmem);
1386 if (present_phase && p->num_records == 0)
1388 /* present response and we didn't get any records! */
1389 Z_NamePlusRecord *myrec =
1390 zget_surrogateDiagRec(
1392 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1393 "ZOOM C generated. Present phase and no records");
1394 ZOOM_record_cache_add(resultset, myrec, *start,
1395 syntax, elementSetName, schema, 0);
1399 else if (present_phase)
1401 /* present response and we didn't get any records! */
1402 Z_NamePlusRecord *myrec =
1403 zget_surrogateDiagRec(
1405 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1406 "ZOOM C generated: Present response and no records");
1407 ZOOM_record_cache_add(resultset, myrec, *start,
1408 syntax, elementSetName, schema, 0);
1414 static void handle_Z3950_present_response(ZOOM_connection c,
1415 Z_PresentResponse *pr)
1417 handle_Z3950_records(c, pr->records, 1);
1420 static void set_init_option(const char *name, void *clientData)
1422 ZOOM_connection c = (ZOOM_connection) clientData;
1425 sprintf(buf, "init_opt_%.70s", name);
1426 ZOOM_connection_option_set(c, buf, "1");
1430 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset)
1433 resultset->r_sort_spec = 0;
1434 if (resultset->r_sort_spec)
1436 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1437 Z_SortRequest *req = apdu->u.sortRequest;
1439 req->num_inputResultSetNames = 1;
1440 req->inputResultSetNames = (Z_InternationalString **)
1441 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
1442 req->inputResultSetNames[0] =
1443 odr_strdup(c->odr_out, resultset->setname);
1444 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
1445 req->sortSequence = resultset->r_sort_spec;
1446 resultset->r_sort_spec = 0;
1447 return send_APDU(c, apdu);
1449 return zoom_complete;
1452 static zoom_ret Z3950_send_present(ZOOM_connection c)
1454 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1455 Z_PresentRequest *req = apdu->u.presentRequest;
1456 ZOOM_resultset resultset = c->tasks->u.search.resultset;
1457 const char *syntax = c->tasks->u.search.syntax;
1458 const char *elementSetName = c->tasks->u.search.elementSetName;
1459 const char *schema = c->tasks->u.search.schema;
1461 *req->resultSetStartPoint = c->tasks->u.search.start + 1;
1463 if (resultset->step > 0 && resultset->step < c->tasks->u.search.count)
1464 *req->numberOfRecordsRequested = resultset->step;
1466 *req->numberOfRecordsRequested = c->tasks->u.search.count;
1468 if (*req->numberOfRecordsRequested + c->tasks->u.search.start > resultset->size)
1469 *req->numberOfRecordsRequested = resultset->size - c->tasks->u.search.start;
1470 assert(*req->numberOfRecordsRequested > 0);
1472 if (syntax && *syntax)
1473 req->preferredRecordSyntax =
1474 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1476 if (schema && *schema)
1478 Z_RecordComposition *compo = (Z_RecordComposition *)
1479 odr_malloc(c->odr_out, sizeof(*compo));
1481 req->recordComposition = compo;
1482 compo->which = Z_RecordComp_complex;
1483 compo->u.complex = (Z_CompSpec *)
1484 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1485 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1486 odr_malloc(c->odr_out, sizeof(bool_t));
1487 *compo->u.complex->selectAlternativeSyntax = 0;
1489 compo->u.complex->generic = (Z_Specification *)
1490 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1492 compo->u.complex->generic->which = Z_Schema_oid;
1493 compo->u.complex->generic->schema.oid = (Odr_oid *)
1494 zoom_yaz_str_to_z3950oid(c, CLASS_SCHEMA, schema);
1496 if (!compo->u.complex->generic->schema.oid)
1498 /* OID wasn't a schema! Try record syntax instead. */
1500 compo->u.complex->generic->schema.oid = (Odr_oid *)
1501 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, schema);
1503 if (elementSetName && *elementSetName)
1505 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1506 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1507 compo->u.complex->generic->elementSpec->which =
1508 Z_ElementSpec_elementSetName;
1509 compo->u.complex->generic->elementSpec->u.elementSetName =
1510 odr_strdup(c->odr_out, elementSetName);
1513 compo->u.complex->generic->elementSpec = 0;
1514 compo->u.complex->num_dbSpecific = 0;
1515 compo->u.complex->dbSpecific = 0;
1516 compo->u.complex->num_recordSyntax = 0;
1517 compo->u.complex->recordSyntax = 0;
1519 else if (elementSetName && *elementSetName)
1521 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1522 odr_malloc(c->odr_out, sizeof(*esn));
1523 Z_RecordComposition *compo = (Z_RecordComposition *)
1524 odr_malloc(c->odr_out, sizeof(*compo));
1526 esn->which = Z_ElementSetNames_generic;
1527 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
1528 compo->which = Z_RecordComp_simple;
1529 compo->u.simple = esn;
1530 req->recordComposition = compo;
1532 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1533 return send_APDU(c, apdu);
1536 zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c)
1539 const char *syntax = 0;
1540 const char *elementSetName = 0;
1541 const char *schema = 0;
1542 ZOOM_resultset resultset;
1546 return zoom_complete;
1547 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1548 resultset = c->tasks->u.search.resultset;
1549 start = &c->tasks->u.search.start;
1550 count = &c->tasks->u.search.count;
1551 syntax = c->tasks->u.search.syntax;
1552 elementSetName = c->tasks->u.search.elementSetName;
1553 schema = c->tasks->u.search.schema;
1555 yaz_log(c->log_details, "%p send_present start=%d count=%d",
1558 if (*start < 0 || *count < 0)
1560 ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
1563 if (resultset->live_set && *start + *count > resultset->size)
1565 ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
1569 if (c->error) /* don't continue on error */
1570 return zoom_complete;
1571 yaz_log(c->log_details, "send_present resultset=%p start=%d count=%d",
1572 resultset, *start, *count);
1574 for (i = 0; i < *count; i++)
1577 ZOOM_record_cache_lookup(resultset, i + *start,
1578 syntax, elementSetName, schema);
1583 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1584 ZOOM_connection_put_event(c, event);
1590 if (*count == 0 && resultset->live_set)
1591 return zoom_complete;
1593 if (resultset->live_set == 2)
1594 return Z3950_send_present(c);
1596 return Z3950_send_search(c);
1599 static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
1601 zoom_ret r = zoom_complete;
1603 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
1604 r = send_Z3950_sort(c, c->tasks->u.search.resultset);
1605 if (r == zoom_complete)
1606 r = ZOOM_connection_Z3950_search(c);
1610 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
1612 Z_InitResponse *initrs;
1614 ZOOM_connection_set_mask(c, 0);
1615 yaz_log(c->log_details, "%p handle_Z3950_apdu apdu->which=%d",
1617 switch (apdu->which)
1619 case Z_APDU_initResponse:
1620 yaz_log(c->log_api, "%p handle_Z3950_apdu: Received Init response", c);
1621 initrs = apdu->u.initResponse;
1622 ZOOM_connection_option_set(c, "serverImplementationId",
1623 initrs->implementationId ?
1624 initrs->implementationId : "");
1625 ZOOM_connection_option_set(c, "serverImplementationName",
1626 initrs->implementationName ?
1627 initrs->implementationName : "");
1628 ZOOM_connection_option_set(c, "serverImplementationVersion",
1629 initrs->implementationVersion ?
1630 initrs->implementationVersion : "");
1631 /* Set the three old options too, for old applications */
1632 ZOOM_connection_option_set(c, "targetImplementationId",
1633 initrs->implementationId ?
1634 initrs->implementationId : "");
1635 ZOOM_connection_option_set(c, "targetImplementationName",
1636 initrs->implementationName ?
1637 initrs->implementationName : "");
1638 ZOOM_connection_option_set(c, "targetImplementationVersion",
1639 initrs->implementationVersion ?
1640 initrs->implementationVersion : "");
1642 /* Make initrs->options available as ZOOM-level options */
1643 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
1645 if (!*initrs->result)
1647 Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs);
1649 response_default_diag(c, df);
1651 ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */
1656 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
1657 yaz_oid_userinfo_cookie, 1, 0);
1658 xfree(c->cookie_in);
1661 c->cookie_in = xstrdup(cookie);
1662 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1663 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1664 c->support_named_resultsets = 1;
1667 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1668 ZOOM_connection_remove_task(c);
1670 ZOOM_connection_exec_task(c);
1672 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1674 NMEM tmpmem = nmem_create();
1675 Z_CharSetandLanguageNegotiation *p =
1676 yaz_get_charneg_record(initrs->otherInfo);
1680 char *charset = NULL, *lang = NULL;
1683 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
1684 yaz_log(c->log_details, "%p handle_Z3950_apdu target accepted: "
1685 "charset %s, language %s, select %d",
1687 charset ? charset : "none", lang ? lang : "none", sel);
1689 ZOOM_connection_option_set(c, "negotiation-charset",
1692 ZOOM_connection_option_set(c, "negotiation-lang",
1695 ZOOM_connection_option_set(
1696 c, "negotiation-charset-in-effect-for-records",
1697 (sel != 0) ? "1" : "0");
1698 nmem_destroy(tmpmem);
1702 case Z_APDU_searchResponse:
1703 yaz_log(c->log_api, "%p handle_Z3950_apdu Search response", c);
1704 handle_Z3950_search_response(c, apdu->u.searchResponse);
1705 if (send_Z3950_sort_present(c) == zoom_complete)
1706 ZOOM_connection_remove_task(c);
1708 case Z_APDU_presentResponse:
1709 yaz_log(c->log_api, "%p handle_Z3950_apdu Present response", c);
1710 handle_Z3950_present_response(c, apdu->u.presentResponse);
1711 if (ZOOM_connection_Z3950_search(c) == zoom_complete)
1712 ZOOM_connection_remove_task(c);
1714 case Z_APDU_sortResponse:
1715 yaz_log(c->log_api, "%p handle_Z3950_apdu Sort response", c);
1716 handle_Z3950_sort_response(c, apdu->u.sortResponse);
1717 ZOOM_connection_remove_task(c);
1719 case Z_APDU_scanResponse:
1720 yaz_log(c->log_api, "%p handle_Z3950_apdu Scan response", c);
1721 handle_Z3950_scan_response(c, apdu->u.scanResponse);
1722 ZOOM_connection_remove_task(c);
1724 case Z_APDU_extendedServicesResponse:
1725 yaz_log(c->log_api, "%p handle_Z3950_apdu Extended Services response", c);
1726 handle_Z3950_es_response(c, apdu->u.extendedServicesResponse);
1727 ZOOM_connection_remove_task(c);
1730 yaz_log(c->log_api, "%p handle_Z3950_apdu Close PDU", c);
1731 if (!ZOOM_test_reconnect(c))
1733 ZOOM_set_dset_error(c, ZOOM_ERROR_CONNECTION_LOST, "ZOOM", c->host_port, apdu->u.close->diagnosticInformation);
1734 ZOOM_connection_close(c);
1738 yaz_log(c->log_api, "%p Received unknown PDU", c);
1739 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1740 ZOOM_connection_close(c);
1747 * c-file-style: "Stroustrup"
1748 * indent-tabs-mode: nil
1750 * vim: shiftwidth=4 tabstop=8 expandtab