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;
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 Z3950_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;
672 Z_FacetList *facet_list =
673 yaz_pqf_parse_facet_list(c->odr_out, r->req_facets);
676 Z_OtherInformation **oi = &search_req->additionalSearchInfo;
677 yaz_oi_set_facetlist(oi, c->odr_out, facet_list);
680 yaz_log(YLOG_WARN, "Unable to parse facets: %s", r->req_facets);
686 /* prepare query for the search request */
687 search_req->query = ZOOM_query_get_Z_Query(r->query);
688 if (!search_req->query)
690 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
691 return zoom_complete;
693 if (search_req->query->which == Z_Query_type_1 ||
694 search_req->query->which == Z_Query_type_101)
696 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
699 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
703 search_req->query = yaz_copy_Z_Query(search_req->query,
706 r = yaz_query_charset_convert_rpnquery_check(
707 search_req->query->u.type_1,
711 { /* query could not be char converted */
712 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
713 return zoom_complete;
718 search_req->databaseNames = r->databaseNames;
719 search_req->num_databaseNames = r->num_databaseNames;
721 /* get syntax (no need to provide unless piggyback is in effect) */
722 syntax = c->tasks->u.search.syntax;
724 schema = c->tasks->u.search.schema;
726 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
727 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
728 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
729 if (lslb != -1 && ssub != -1 && mspn != -1)
731 /* So're a Z39.50 expert? Let's hope you don't do sort */
732 *search_req->largeSetLowerBound = lslb;
733 *search_req->smallSetUpperBound = ssub;
734 *search_req->mediumSetPresentNumber = mspn;
736 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
737 && r->piggyback && !r->r_sort_spec && !schema)
739 /* Regular piggyback - do it unless we're going to do sort */
740 *search_req->largeSetLowerBound = 2000000000;
741 *search_req->smallSetUpperBound = 1;
742 *search_req->mediumSetPresentNumber =
743 r->step>0 ? r->step : c->tasks->u.search.count;
747 /* non-piggyback. Need not provide elementsets or syntaxes .. */
748 smallSetElementSetName = 0;
749 mediumSetElementSetName = 0;
752 if (smallSetElementSetName && *smallSetElementSetName)
754 Z_ElementSetNames *esn = (Z_ElementSetNames *)
755 odr_malloc(c->odr_out, sizeof(*esn));
757 esn->which = Z_ElementSetNames_generic;
758 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
759 search_req->smallSetElementSetNames = esn;
761 if (mediumSetElementSetName && *mediumSetElementSetName)
763 Z_ElementSetNames *esn =(Z_ElementSetNames *)
764 odr_malloc(c->odr_out, sizeof(*esn));
766 esn->which = Z_ElementSetNames_generic;
767 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
768 search_req->mediumSetElementSetNames = esn;
771 search_req->preferredRecordSyntax =
772 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
776 if (c->support_named_resultsets)
780 /* find the lowest unused ordinal so that we re-use
781 result sets on the server. */
782 for (ord = 1; ; ord++)
785 sprintf(setname, "%d", ord);
786 for (rp = c->resultsets; rp; rp = rp->next)
787 if (rp->setname && !strcmp(rp->setname, setname))
792 r->setname = odr_strdup(r->odr, setname);
793 yaz_log(c->log_details, "%p ZOOM_connection_send_search: "
794 "allocating set %s", c, r->setname);
798 yaz_log(c->log_details, "%p ZOOM_connection_send_search: using "
800 r->setname = odr_strdup(r->odr, "default");
802 ZOOM_options_set(r->options, "setname", r->setname);
804 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
805 return send_APDU(c, apdu);
808 zoom_ret ZOOM_connection_Z3950_send_scan(ZOOM_connection c)
811 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
812 Z_ScanRequest *req = apdu->u.scanRequest;
815 yaz_log(c->log_details, "%p send_scan", c);
817 return zoom_complete;
818 assert (c->tasks->which == ZOOM_TASK_SCAN);
819 scan = c->tasks->u.scan.scan;
821 z_query = ZOOM_query_get_Z_Query(scan->query);
823 /* Z39.50 scan can only carry RPN */
824 if (z_query->which == Z_Query_type_1 ||
825 z_query->which == Z_Query_type_101)
827 Z_RPNQuery *rpn = z_query->u.type_1;
828 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
831 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
834 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
836 yaz_query_charset_convert_rpnquery(
837 rpn, c->odr_out, cd);
841 req->attributeSet = rpn->attributeSetId;
842 if (!req->attributeSet)
843 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
844 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
845 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
847 req->termListAndStartPoint =
848 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
852 ZOOM_set_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
853 return zoom_complete;
858 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
859 return zoom_complete;
862 *req->numberOfTermsRequested =
863 ZOOM_options_get_int(scan->options, "number", 20);
865 req->preferredPositionInResponse =
866 odr_intdup(c->odr_out,
867 ZOOM_options_get_int(scan->options, "position", 1));
870 odr_intdup(c->odr_out,
871 ZOOM_options_get_int(scan->options, "stepSize", 0));
873 req->databaseNames = scan->databaseNames;
874 req->num_databaseNames = scan->num_databaseNames;
876 return send_APDU(c, apdu);
880 ZOOM_package_send(ZOOM_package p, const char *type)
887 odr_reset(p->odr_out);
890 if (!strcmp(type, "itemorder"))
892 apdu = create_es_package(p, yaz_oid_extserv_item_order);
895 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
897 r->direct_reference =
898 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
900 r->which = Z_External_itemOrder;
901 r->indirect_reference = 0;
902 r->u.itemOrder = encode_item_order(p);
904 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
907 else if (!strcmp(type, "create")) /* create database */
909 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
912 else if (!strcmp(type, "drop")) /* drop database */
914 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
917 else if (!strcmp(type, "commit")) /* commit changes */
919 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
922 else if (!strcmp(type, "update")) /* update record(s) */
924 apdu = create_update_package(p);
926 else if (!strcmp(type, "xmlupdate"))
928 apdu = create_xmlupdate_package(p);
932 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
936 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
938 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
939 p->buf_out = (char *) xmalloc(p->len_out);
940 memcpy(p->buf_out, buf, p->len_out);
945 while (ZOOM_event(1, &c))
952 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
955 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
957 char oid_name_buf[OID_STR_MAX];
958 const char *oid_name;
961 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
964 case Z_DefaultDiagFormat_v2Addinfo:
965 addinfo = r->u.v2Addinfo;
967 case Z_DefaultDiagFormat_v3Addinfo:
968 addinfo = r->u.v3Addinfo;
973 ZOOM_set_dset_error(c, *r->condition, oid_name, addinfo, 0);
976 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
978 if (p->which != Z_DiagRec_defaultFormat)
979 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
981 response_default_diag(c, p->u.defaultFormat);
984 static int es_response_taskpackage_update(ZOOM_connection c,
985 Z_IUUpdateTaskPackage *utp)
987 if (utp && utp->targetPart)
989 Z_IUTargetPart *targetPart = utp->targetPart;
990 switch (*targetPart->updateStatus)
992 case Z_IUTargetPart_success:
993 ZOOM_options_set(c->tasks->u.package->options,
994 "updateStatus", "success");
996 case Z_IUTargetPart_partial:
997 ZOOM_options_set(c->tasks->u.package->options,
998 "updateStatus", "partial");
1000 case Z_IUTargetPart_failure:
1001 ZOOM_options_set(c->tasks->u.package->options,
1002 "updateStatus", "failure");
1003 if (targetPart->globalDiagnostics &&
1004 targetPart->num_globalDiagnostics > 0)
1005 response_diag(c, targetPart->globalDiagnostics[0]);
1008 /* NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED. */
1013 static int es_response_taskpackage(ZOOM_connection c,
1014 Z_TaskPackage *taskPackage)
1016 Odr_oct *id = taskPackage->targetReference;
1018 ZOOM_options_setl(c->tasks->u.package->options,
1019 "targetReference", (char*) id->buf, id->len);
1020 switch (*taskPackage->taskStatus)
1022 case Z_TaskPackage_pending:
1023 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
1025 case Z_TaskPackage_active:
1026 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
1028 case Z_TaskPackage_complete:
1029 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
1031 case Z_TaskPackage_aborted:
1032 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
1033 if (taskPackage->num_packageDiagnostics &&
1034 taskPackage->packageDiagnostics )
1035 response_diag(c, taskPackage->packageDiagnostics[0]);
1038 /* NOTE: Only Update implemented, no others. */
1039 if (taskPackage->taskSpecificParameters->which == Z_External_update)
1041 Z_IUUpdateTaskPackage *utp =
1042 taskPackage->taskSpecificParameters->u.update->u.taskPackage;
1043 es_response_taskpackage_update(c, utp);
1048 static void handle_Z3950_es_response(ZOOM_connection c,
1049 Z_ExtendedServicesResponse *res)
1053 assert(c->tasks->which == ZOOM_TASK_PACKAGE);
1054 switch (*res->operationStatus)
1056 case Z_ExtendedServicesResponse_done:
1057 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
1059 case Z_ExtendedServicesResponse_accepted:
1060 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
1062 case Z_ExtendedServicesResponse_failure:
1063 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
1064 if (res->diagnostics && res->num_diagnostics > 0)
1065 response_diag(c, res->diagnostics[0]);
1068 if (res->taskPackage &&
1069 res->taskPackage->which == Z_External_extendedService)
1071 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1072 es_response_taskpackage(c, taskPackage);
1074 if (res->taskPackage &&
1075 res->taskPackage->which == Z_External_octet)
1077 Odr_oct *doc = res->taskPackage->u.octet_aligned;
1078 ZOOM_options_setl(c->tasks->u.package->options,
1079 "xmlUpdateDoc", (char*) doc->buf, doc->len);
1083 static char *get_term_cstr(ODR odr, Z_Term *term)
1085 switch (term->which)
1087 case Z_Term_general:
1088 return odr_strdupn(odr, term->u.general->buf, term->u.general->len);
1090 case Z_Term_characterString:
1091 return odr_strdup(odr, term->u.characterString);
1096 static ZOOM_facet_field get_zoom_facet_field(ODR odr, Z_FacetField *facet)
1099 struct yaz_facet_attr attr_values;
1100 ZOOM_facet_field facet_field = odr_malloc(odr, sizeof(*facet_field));
1101 yaz_facet_attr_init(&attr_values);
1102 yaz_facet_attr_get_z_attributes(facet->attributes, &attr_values);
1103 facet_field->facet_name = odr_strdup(odr, attr_values.useattr);
1104 facet_field->num_terms = facet->num_terms;
1105 yaz_log(YLOG_DEBUG, "ZOOM_facet_field %s %d terms %d",
1106 attr_values.useattr, attr_values.limit, facet->num_terms);
1107 facet_field->facet_terms =
1108 odr_malloc(odr, facet->num_terms * sizeof(*facet_field->facet_terms));
1109 for (i = 0 ; i < facet->num_terms; i++)
1111 Z_FacetTerm *facetTerm = facet->terms[i];
1112 facet_field->facet_terms[i].frequency = *facetTerm->count;
1113 facet_field->facet_terms[i].term = get_term_cstr(odr, facetTerm->term);
1114 yaz_log(YLOG_DEBUG, " term[%d] %s %d",
1115 i, facet_field->facet_terms[i].term,
1116 facet_field->facet_terms[i].frequency);
1121 /* Can be share with SOLR/SRU/SRW requests */
1122 void ZOOM_handle_facet_list(ZOOM_resultset r, Z_FacetList *fl)
1125 r->num_res_facets = fl->num;
1126 yaz_log(YLOG_DEBUG, "Facets found: %d", fl->num);
1127 r->res_facets = odr_malloc(r->odr, fl->num * sizeof(*r->res_facets));
1128 r->facets_names = odr_malloc(r->odr, fl->num * sizeof(*r->facets_names));
1129 for (j = 0; j < fl->num; j++)
1131 r->res_facets[j] = get_zoom_facet_field(r->odr, fl->elements[j]);
1132 if (!r->res_facets[j])
1134 r->facets_names[j] = 0;
1135 yaz_log(YLOG_DEBUG, "Facet field missing on index %d !", j);
1138 r->facets_names[j] = (char *)
1139 ZOOM_facet_field_name(r->res_facets[j]);
1143 void ZOOM_handle_facet_result(ZOOM_connection c, ZOOM_resultset r,
1144 Z_OtherInformation *o)
1147 for (i = 0; o && i < o->num_elements; i++)
1149 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1151 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1152 if (ext->which == Z_External_userFacets)
1154 ZOOM_handle_facet_list(r, ext->u.facetList);
1160 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
1163 switch (term->which)
1165 case Z_Term_general:
1166 ZOOM_options_setl(opt, name,
1167 term->u.general->buf, term->u.general->len);
1169 case Z_Term_characterString:
1170 ZOOM_options_set(opt, name, term->u.characterString);
1172 case Z_Term_numeric:
1173 ZOOM_options_set_int(opt, name, *term->u.numeric);
1178 static void handle_queryExpression(ZOOM_options opt, const char *name,
1179 Z_QueryExpression *exp)
1185 case Z_QueryExpression_term:
1186 if (exp->u.term && exp->u.term->queryTerm)
1188 sprintf(opt_name, "%s.term", name);
1189 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
1192 case Z_QueryExpression_query:
1198 void ZOOM_handle_search_result(ZOOM_connection c, ZOOM_resultset resultset,
1199 Z_OtherInformation *o)
1202 for (i = 0; o && i < o->num_elements; i++)
1204 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1206 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1208 if (ext->which == Z_External_searchResult1)
1211 Z_SearchInfoReport *sr = ext->u.searchResult1;
1214 ZOOM_options_set_int(
1215 resultset->options, "searchresult.size", sr->num);
1217 for (j = 0; j < sr->num; j++)
1219 Z_SearchInfoReport_s *ent =
1220 ext->u.searchResult1->elements[j];
1223 sprintf(pref, "searchresult.%d", j);
1225 if (ent->subqueryId)
1228 sprintf(opt_name, "%s.id", pref);
1229 ZOOM_options_set(resultset->options, opt_name,
1232 if (ent->subqueryExpression)
1235 sprintf(opt_name, "%s.subquery", pref);
1236 handle_queryExpression(resultset->options, opt_name,
1237 ent->subqueryExpression);
1239 if (ent->subqueryInterpretation)
1242 sprintf(opt_name, "%s.interpretation", pref);
1243 handle_queryExpression(resultset->options, opt_name,
1244 ent->subqueryInterpretation);
1246 if (ent->subqueryRecommendation)
1249 sprintf(opt_name, "%s.recommendation", pref);
1250 handle_queryExpression(resultset->options, opt_name,
1251 ent->subqueryRecommendation);
1253 if (ent->subqueryCount)
1256 sprintf(opt_name, "%s.count", pref);
1257 ZOOM_options_set_int(resultset->options, opt_name,
1258 *ent->subqueryCount);
1266 static void handle_Z3950_search_response(ZOOM_connection c,
1267 Z_SearchResponse *sr)
1269 ZOOM_resultset resultset;
1271 const char *resultCountPrecision = "exact";
1273 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1276 resultset = c->tasks->u.search.resultset;
1278 if (resultset->live_set == 0)
1280 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
1281 ZOOM_connection_put_event(c, event);
1283 if (sr->resultSetStatus)
1285 if (*sr->resultSetStatus == Z_SearchResponse_estimate)
1286 resultCountPrecision = "estimate";
1287 ZOOM_options_set_int(resultset->options, "resultSetStatus",
1288 *sr->resultSetStatus);
1290 if (sr->presentStatus)
1292 ZOOM_options_set_int(resultset->options, "presentStatus",
1293 *sr->presentStatus);
1295 ZOOM_options_set(resultset->options, "resultCountPrecision",
1296 resultCountPrecision);
1297 ZOOM_handle_search_result(c, resultset, sr->additionalSearchInfo);
1299 ZOOM_handle_facet_result(c, resultset, sr->additionalSearchInfo);
1301 resultset->size = *sr->resultCount;
1303 ZOOM_memcached_hitcount(c, resultset, sr->additionalSearchInfo,
1304 resultCountPrecision);
1305 resultset->live_set = 2;
1306 handle_Z3950_records(c, sr->records, 0);
1309 static void handle_Z3950_sort_response(ZOOM_connection c, Z_SortResponse *res)
1311 if (res->diagnostics && res->num_diagnostics > 0)
1312 response_diag(c, res->diagnostics[0]);
1315 static void handle_Z3950_scan_response(ZOOM_connection c, Z_ScanResponse *res)
1317 NMEM nmem = odr_extract_mem(c->odr_in);
1320 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1322 scan = c->tasks->u.scan.scan;
1324 if (res->entries && res->entries->nonsurrogateDiagnostics)
1325 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1326 scan->scan_response = res;
1327 scan->srw_scan_response = 0;
1328 nmem_transfer(odr_getmem(scan->odr), nmem);
1330 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
1331 if (res->positionOfTerm)
1332 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
1333 if (res->scanStatus)
1334 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
1335 if (res->numberOfEntriesReturned)
1336 ZOOM_options_set_int(scan->options, "number",
1337 *res->numberOfEntriesReturned);
1341 static void handle_Z3950_records(ZOOM_connection c, Z_Records *sr,
1344 ZOOM_resultset resultset;
1346 const char *syntax = 0, *elementSetName = 0, *schema = 0;
1348 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1351 resultset = c->tasks->u.search.resultset;
1352 start = &c->tasks->u.search.start;
1353 count = &c->tasks->u.search.count;
1354 syntax = c->tasks->u.search.syntax;
1355 elementSetName = c->tasks->u.search.elementSetName;
1356 schema = c->tasks->u.search.schema;
1358 if (sr && sr->which == Z_Records_NSD)
1359 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
1360 else if (sr && sr->which == Z_Records_multipleNSD)
1362 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1363 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1365 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1369 if (*count + *start > resultset->size)
1370 *count = resultset->size - *start;
1373 if (sr && sr->which == Z_Records_DBOSD)
1376 NMEM nmem = odr_extract_mem(c->odr_in);
1377 Z_NamePlusRecordList *p =
1378 sr->u.databaseOrSurDiagnostics;
1379 for (i = 0; i < p->num_records; i++)
1381 ZOOM_record_cache_add(resultset, p->records[i], i + *start,
1382 syntax, elementSetName, schema, 0);
1388 yaz_log(c->log_details,
1389 "handle_records resultset=%p start=%d count=%d",
1390 resultset, *start, *count);
1392 /* transfer our response to search_nmem .. we need it later */
1393 nmem_transfer(odr_getmem(resultset->odr), nmem);
1395 if (present_phase && p->num_records == 0)
1397 /* present response and we didn't get any records! */
1398 Z_NamePlusRecord *myrec =
1399 zget_surrogateDiagRec(
1401 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1402 "ZOOM C generated. Present phase and no records");
1403 ZOOM_record_cache_add(resultset, myrec, *start,
1404 syntax, elementSetName, schema, 0);
1408 else if (present_phase)
1410 /* present response and we didn't get any records! */
1411 Z_NamePlusRecord *myrec =
1412 zget_surrogateDiagRec(
1414 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1415 "ZOOM C generated: Present response and no records");
1416 ZOOM_record_cache_add(resultset, myrec, *start,
1417 syntax, elementSetName, schema, 0);
1423 static void handle_Z3950_present_response(ZOOM_connection c,
1424 Z_PresentResponse *pr)
1426 handle_Z3950_records(c, pr->records, 1);
1429 static void set_init_option(const char *name, void *clientData)
1431 ZOOM_connection c = (ZOOM_connection) clientData;
1434 sprintf(buf, "init_opt_%.70s", name);
1435 ZOOM_connection_option_set(c, buf, "1");
1438 zoom_ret send_Z3950_sort(ZOOM_connection c, ZOOM_resultset resultset)
1441 resultset->r_sort_spec = 0;
1442 if (resultset->r_sort_spec)
1444 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1445 Z_SortRequest *req = apdu->u.sortRequest;
1447 yaz_log(c->log_details, "%p send_Z3950_sort set=%p", c, resultset);
1449 req->num_inputResultSetNames = 1;
1450 req->inputResultSetNames = (Z_InternationalString **)
1451 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
1452 req->inputResultSetNames[0] =
1453 odr_strdup(c->odr_out, resultset->setname);
1454 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
1455 req->sortSequence = resultset->r_sort_spec;
1456 resultset->r_sort_spec = 0;
1457 return send_APDU(c, apdu);
1459 return zoom_complete;
1462 static zoom_ret Z3950_send_present(ZOOM_connection c)
1464 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1465 Z_PresentRequest *req = apdu->u.presentRequest;
1466 ZOOM_resultset resultset = c->tasks->u.search.resultset;
1467 const char *syntax = c->tasks->u.search.syntax;
1468 const char *elementSetName = c->tasks->u.search.elementSetName;
1469 const char *schema = c->tasks->u.search.schema;
1471 yaz_log(c->log_details, "%p Z3950_send_present", c);
1473 *req->resultSetStartPoint = c->tasks->u.search.start + 1;
1475 if (resultset->step > 0 && resultset->step < c->tasks->u.search.count)
1476 *req->numberOfRecordsRequested = resultset->step;
1478 *req->numberOfRecordsRequested = c->tasks->u.search.count;
1480 if (*req->numberOfRecordsRequested + c->tasks->u.search.start > resultset->size)
1481 *req->numberOfRecordsRequested = resultset->size - c->tasks->u.search.start;
1482 assert(*req->numberOfRecordsRequested > 0);
1484 if (syntax && *syntax)
1485 req->preferredRecordSyntax =
1486 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1488 if (schema && *schema)
1490 Z_RecordComposition *compo = (Z_RecordComposition *)
1491 odr_malloc(c->odr_out, sizeof(*compo));
1493 req->recordComposition = compo;
1494 compo->which = Z_RecordComp_complex;
1495 compo->u.complex = (Z_CompSpec *)
1496 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1497 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1498 odr_malloc(c->odr_out, sizeof(bool_t));
1499 *compo->u.complex->selectAlternativeSyntax = 0;
1501 compo->u.complex->generic = (Z_Specification *)
1502 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1504 compo->u.complex->generic->which = Z_Schema_oid;
1505 compo->u.complex->generic->schema.oid = (Odr_oid *)
1506 zoom_yaz_str_to_z3950oid(c, CLASS_SCHEMA, schema);
1508 if (!compo->u.complex->generic->schema.oid)
1510 /* OID wasn't a schema! Try record syntax instead. */
1512 compo->u.complex->generic->schema.oid = (Odr_oid *)
1513 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, schema);
1515 if (elementSetName && *elementSetName)
1517 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1518 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1519 compo->u.complex->generic->elementSpec->which =
1520 Z_ElementSpec_elementSetName;
1521 compo->u.complex->generic->elementSpec->u.elementSetName =
1522 odr_strdup(c->odr_out, elementSetName);
1525 compo->u.complex->generic->elementSpec = 0;
1526 compo->u.complex->num_dbSpecific = 0;
1527 compo->u.complex->dbSpecific = 0;
1528 compo->u.complex->num_recordSyntax = 0;
1529 compo->u.complex->recordSyntax = 0;
1531 else if (elementSetName && *elementSetName)
1533 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1534 odr_malloc(c->odr_out, sizeof(*esn));
1535 Z_RecordComposition *compo = (Z_RecordComposition *)
1536 odr_malloc(c->odr_out, sizeof(*compo));
1538 esn->which = Z_ElementSetNames_generic;
1539 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
1540 compo->which = Z_RecordComp_simple;
1541 compo->u.simple = esn;
1542 req->recordComposition = compo;
1544 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1545 return send_APDU(c, apdu);
1548 zoom_ret ZOOM_connection_Z3950_search(ZOOM_connection c)
1551 const char *syntax = 0;
1552 const char *elementSetName = 0;
1553 const char *schema = 0;
1554 ZOOM_resultset resultset;
1558 return zoom_complete;
1559 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1560 resultset = c->tasks->u.search.resultset;
1561 start = &c->tasks->u.search.start;
1562 count = &c->tasks->u.search.count;
1563 syntax = c->tasks->u.search.syntax;
1564 elementSetName = c->tasks->u.search.elementSetName;
1565 schema = c->tasks->u.search.schema;
1567 yaz_log(c->log_details, "%p ZOOM_connection_Z3950_search start=%d count=%d",
1570 ZOOM_memcached_search(c, resultset);
1572 if (*start < 0 || *count < 0)
1574 ZOOM_set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
1575 "start/count < 0", 0);
1578 if (resultset->live_set)
1580 if (*start >= resultset->size)
1581 return zoom_complete;
1582 if (*start + *count > resultset->size)
1583 *count = resultset->size - *start;
1586 if (c->error) /* don't continue on error */
1587 return zoom_complete;
1589 for (i = 0; i < *count; i++)
1592 ZOOM_record_cache_lookup(resultset, i + *start,
1593 syntax, elementSetName, schema);
1600 if (*count == 0 && resultset->live_set)
1601 return zoom_complete;
1603 if (resultset->live_set == 2)
1604 return Z3950_send_present(c);
1606 return Z3950_send_search(c);
1609 static zoom_ret send_Z3950_sort_present(ZOOM_connection c)
1611 zoom_ret r = zoom_complete;
1613 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
1614 r = send_Z3950_sort(c, c->tasks->u.search.resultset);
1615 if (r == zoom_complete)
1616 r = ZOOM_connection_Z3950_search(c);
1620 void ZOOM_handle_Z3950_apdu(ZOOM_connection c, Z_APDU *apdu)
1622 Z_InitResponse *initrs;
1624 ZOOM_connection_set_mask(c, 0);
1625 yaz_log(c->log_details, "%p handle_Z3950_apdu apdu->which=%d",
1627 switch (apdu->which)
1629 case Z_APDU_initResponse:
1630 yaz_log(c->log_api, "%p handle_Z3950_apdu: Received Init response", c);
1631 initrs = apdu->u.initResponse;
1632 ZOOM_connection_option_set(c, "serverImplementationId",
1633 initrs->implementationId ?
1634 initrs->implementationId : "");
1635 ZOOM_connection_option_set(c, "serverImplementationName",
1636 initrs->implementationName ?
1637 initrs->implementationName : "");
1638 ZOOM_connection_option_set(c, "serverImplementationVersion",
1639 initrs->implementationVersion ?
1640 initrs->implementationVersion : "");
1641 /* Set the three old options too, for old applications */
1642 ZOOM_connection_option_set(c, "targetImplementationId",
1643 initrs->implementationId ?
1644 initrs->implementationId : "");
1645 ZOOM_connection_option_set(c, "targetImplementationName",
1646 initrs->implementationName ?
1647 initrs->implementationName : "");
1648 ZOOM_connection_option_set(c, "targetImplementationVersion",
1649 initrs->implementationVersion ?
1650 initrs->implementationVersion : "");
1652 /* Make initrs->options available as ZOOM-level options */
1653 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
1655 if (!*initrs->result)
1657 Z_DefaultDiagFormat *df = yaz_decode_init_diag(0, initrs);
1659 response_default_diag(c, df);
1661 ZOOM_set_error(c, ZOOM_ERROR_INIT, 0); /* default error */
1666 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
1667 yaz_oid_userinfo_cookie, 1, 0);
1668 xfree(c->cookie_in);
1671 c->cookie_in = xstrdup(cookie);
1672 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1673 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1674 c->support_named_resultsets = 1;
1677 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1678 ZOOM_connection_remove_task(c);
1680 ZOOM_connection_exec_task(c);
1682 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1684 NMEM tmpmem = nmem_create();
1685 Z_CharSetandLanguageNegotiation *p =
1686 yaz_get_charneg_record(initrs->otherInfo);
1690 char *charset = NULL, *lang = NULL;
1693 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
1694 yaz_log(c->log_details, "%p handle_Z3950_apdu target accepted: "
1695 "charset %s, language %s, select %d",
1697 charset ? charset : "none", lang ? lang : "none", sel);
1699 ZOOM_connection_option_set(c, "negotiation-charset",
1702 ZOOM_connection_option_set(c, "negotiation-lang",
1705 ZOOM_connection_option_set(
1706 c, "negotiation-charset-in-effect-for-records",
1707 (sel != 0) ? "1" : "0");
1708 nmem_destroy(tmpmem);
1712 case Z_APDU_searchResponse:
1713 yaz_log(c->log_api, "%p handle_Z3950_apdu Search response", c);
1714 handle_Z3950_search_response(c, apdu->u.searchResponse);
1715 if (send_Z3950_sort_present(c) == zoom_complete)
1716 ZOOM_connection_remove_task(c);
1718 case Z_APDU_presentResponse:
1719 yaz_log(c->log_api, "%p handle_Z3950_apdu Present response", c);
1720 handle_Z3950_present_response(c, apdu->u.presentResponse);
1721 if (ZOOM_connection_Z3950_search(c) == zoom_complete)
1722 ZOOM_connection_remove_task(c);
1724 case Z_APDU_sortResponse:
1725 yaz_log(c->log_api, "%p handle_Z3950_apdu Sort response", c);
1726 handle_Z3950_sort_response(c, apdu->u.sortResponse);
1727 ZOOM_connection_remove_task(c);
1729 case Z_APDU_scanResponse:
1730 yaz_log(c->log_api, "%p handle_Z3950_apdu Scan response", c);
1731 handle_Z3950_scan_response(c, apdu->u.scanResponse);
1732 ZOOM_connection_remove_task(c);
1734 case Z_APDU_extendedServicesResponse:
1735 yaz_log(c->log_api, "%p handle_Z3950_apdu Extended Services response", c);
1736 handle_Z3950_es_response(c, apdu->u.extendedServicesResponse);
1737 ZOOM_connection_remove_task(c);
1740 yaz_log(c->log_api, "%p handle_Z3950_apdu Close PDU", c);
1741 if (!ZOOM_test_reconnect(c))
1743 ZOOM_set_dset_error(c, ZOOM_ERROR_CONNECTION_LOST, "ZOOM", c->host_port, apdu->u.close->diagnosticInformation);
1744 ZOOM_connection_close(c);
1748 yaz_log(c->log_api, "%p Received unknown PDU", c);
1749 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1750 ZOOM_connection_close(c);
1757 * c-file-style: "Stroustrup"
1758 * indent-tabs-mode: nil
1760 * vim: shiftwidth=4 tabstop=8 expandtab