-static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
-{
- ZOOM_Event event;
- assert(a);
- if (encode_APDU(c, a, c->odr_out))
- return zoom_complete;
- yaz_log(log_details, "%p send APDU type=%d", c, a->which);
- c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
- event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event(c, event);
- odr_reset(c->odr_out);
- return do_write(c);
-}
-
-/* returns 1 if PDU was sent OK (still pending )
- 0 if PDU was not sent OK (nothing to wait for)
-*/
-
-static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
-{
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
- Z_InitRequest *ireq = apdu->u.initRequest;
- Z_IdAuthentication *auth = (Z_IdAuthentication *)
- odr_malloc(c->odr_out, sizeof(*auth));
- const char *auth_groupId = ZOOM_options_get(c->options, "group");
- const char *auth_userId = ZOOM_options_get(c->options, "user");
- const char *auth_password = ZOOM_options_get(c->options, "password");
- char *version;
-
- /* support the pass for backwards compatibility */
- if (!auth_password)
- auth_password = ZOOM_options_get(c->options, "pass");
-
- ODR_MASK_SET(ireq->options, Z_Options_search);
- ODR_MASK_SET(ireq->options, Z_Options_present);
- ODR_MASK_SET(ireq->options, Z_Options_scan);
- ODR_MASK_SET(ireq->options, Z_Options_sort);
- ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
- ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
-
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
-
- /* Index Data's Z39.50 Implementor Id is 81 */
- ireq->implementationId =
- odr_prepend(c->odr_out,
- ZOOM_options_get(c->options, "implementationId"),
- odr_prepend(c->odr_out, "81", ireq->implementationId));
-
- ireq->implementationName =
- odr_prepend(c->odr_out,
- ZOOM_options_get(c->options, "implementationName"),
- odr_prepend(c->odr_out, "ZOOM-C",
- ireq->implementationName));
-
- version = odr_strdup(c->odr_out, "$Revision: 1.87 $");
- if (strlen(version) > 10) /* check for unexpanded CVS strings */
- version[strlen(version)-2] = '\0';
- ireq->implementationVersion =
- odr_prepend(c->odr_out,
- ZOOM_options_get(c->options, "implementationVersion"),
- odr_prepend(c->odr_out, &version[11],
- ireq->implementationVersion));
-
- *ireq->maximumRecordSize =
- ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
- *ireq->preferredMessageSize =
- ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
-
- if (auth_groupId || auth_password)
- {
- Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
- int i = 0;
- pass->groupId = 0;
- if (auth_groupId && *auth_groupId)
- {
- pass->groupId = (char *)
- odr_malloc(c->odr_out, strlen(auth_groupId)+1);
- strcpy(pass->groupId, auth_groupId);
- i++;
- }
- pass->userId = 0;
- if (auth_userId && *auth_userId)
- {
- pass->userId = (char *)
- odr_malloc(c->odr_out, strlen(auth_userId)+1);
- strcpy(pass->userId, auth_userId);
- i++;
- }
- pass->password = 0;
- if (auth_password && *auth_password)
- {
- pass->password = (char *)
- odr_malloc(c->odr_out, strlen(auth_password)+1);
- strcpy(pass->password, auth_password);
- i++;
- }
- if (i)
- {
- auth->which = Z_IdAuthentication_idPass;
- auth->u.idPass = pass;
- ireq->idAuthentication = auth;
- }
- }
- else if (auth_userId)
- {
- auth->which = Z_IdAuthentication_open;
- auth->u.open = (char *)
- odr_malloc(c->odr_out, strlen(auth_userId)+1);
- strcpy(auth->u.open, auth_userId);
- ireq->idAuthentication = auth;
- }
- if (c->proxy)
- yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
- VAL_PROXY, 1, c->host_port);
- if (c->charset || c->lang)
- {
- Z_OtherInformation **oi;
- Z_OtherInformationUnit *oi_unit;
-
- yaz_oi_APDU(apdu, &oi);
-
- if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
- {
- ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
- oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
- oi_unit->information.externallyDefinedInfo =
- yaz_set_proposal_charneg_list(c->odr_out, " ",
- c->charset, c->lang, 1);
- }
- }
- assert(apdu);
- return send_APDU(c, apdu);
-}
-
-#if YAZ_HAVE_XML2
-static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
-{
- Z_GDU *gdu;
- ZOOM_Event event;
-
- gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
-
- if (c->sru_mode == zoom_sru_get)
- {
- yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
- }
- else if (c->sru_mode == zoom_sru_post)
- {
- yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
- }
- else if (c->sru_mode == zoom_sru_soap)
- {
- yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
- }
- if (!z_GDU(c->odr_out, &gdu, 0, 0))
- return zoom_complete;
- c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
-
- event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event(c, event);
- odr_reset(c->odr_out);
- return do_write(c);
-}
-#endif
-
-#if YAZ_HAVE_XML2
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
-{
- int i;
- int *start, *count;
- ZOOM_resultset resultset = 0;
- Z_SRW_PDU *sr = 0;
- const char *option_val = 0;
-
- if (c->error) /* don't continue on error */
- return zoom_complete;
- assert(c->tasks);
- if (c->tasks->which == ZOOM_TASK_SEARCH)
- {
- resultset = c->tasks->u.search.resultset;
- resultset->setname = xstrdup("default");
- ZOOM_options_set(resultset->options, "setname", resultset->setname);
- start = &c->tasks->u.search.start;
- count = &c->tasks->u.search.count;
- }
- else if (c->tasks->which == ZOOM_TASK_RETRIEVE)
- {
- resultset = c->tasks->u.retrieve.resultset;
-
- start = &c->tasks->u.retrieve.start;
- count = &c->tasks->u.retrieve.count;
-
- if (*start >= resultset->size)
- return zoom_complete;
- if (*start + *count > resultset->size)
- *count = resultset->size - *start;
-
- for (i = 0; i < *count; i++)
- {
- ZOOM_record rec =
- record_cache_lookup(resultset, i + *start);
- if (!rec)
- break;
- else
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
- ZOOM_connection_put_event(c, event);
- }
- }
- *start += i;
- *count -= i;
-
- if (*count == 0)
- return zoom_complete;
- }
- assert(resultset->query);
-
- sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
-
- if (resultset->query->z_query->which == Z_Query_type_104
- && resultset->query->z_query->u.type_104->which == Z_External_CQL)
- {
- sr->u.request->query_type = Z_SRW_query_type_cql;
- sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
- }
- else if (resultset->query->z_query->which == Z_Query_type_1 &&
- resultset->query->z_query->u.type_1)
- {
- sr->u.request->query_type = Z_SRW_query_type_pqf;
- sr->u.request->query.pqf = resultset->query->query_string;
- }
- else
- {
- set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
- return zoom_complete;
- }
- sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
- sr->u.request->maximumRecords = odr_intdup(
- c->odr_out, resultset->step>0 ? resultset->step : *count);
- sr->u.request->recordSchema = resultset->schema;
-
- option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
- if (option_val)
- sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
-
- option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
- if (option_val)
- sr->extra_args = odr_strdup(c->odr_out, option_val);
- return send_srw(c, sr);
-}
-#else
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
-{
- return zoom_complete;
-}
-#endif
-
-static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
-{
- ZOOM_resultset r;
- int lslb, ssub, mspn;
- const char *syntax;
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
- Z_SearchRequest *search_req = apdu->u.searchRequest;
- const char *elementSetName;
- const char *smallSetElementSetName;
- const char *mediumSetElementSetName;
-
- assert(c->tasks);
- assert(c->tasks->which == ZOOM_TASK_SEARCH);
-
- r = c->tasks->u.search.resultset;
-
- yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
-
- elementSetName =
- ZOOM_options_get(r->options, "elementSetName");
- smallSetElementSetName =
- ZOOM_options_get(r->options, "smallSetElementSetName");
- mediumSetElementSetName =
- ZOOM_options_get(r->options, "mediumSetElementSetName");
-
- if (!smallSetElementSetName)
- smallSetElementSetName = elementSetName;
-
- if (!mediumSetElementSetName)
- mediumSetElementSetName = elementSetName;
-
- assert(r);
- assert(r->query);
-
- /* prepare query for the search request */
- search_req->query = r->query->z_query;
- if (!search_req->query)
- {
- set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
- return zoom_complete;
- }
-
- search_req->databaseNames =
- set_DatabaseNames(c, r->options, &search_req->num_databaseNames);
-
- /* get syntax (no need to provide unless piggyback is in effect) */
- syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
-
- lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
- ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
- mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
- if (lslb != -1 && ssub != -1 && mspn != -1)
- {
- /* So're a Z39.50 expert? Let's hope you don't do sort */
- *search_req->largeSetLowerBound = lslb;
- *search_req->smallSetUpperBound = ssub;
- *search_req->mediumSetPresentNumber = mspn;
- }
- else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
- && r->piggyback && !r->r_sort_spec && !r->schema)
- {
- /* Regular piggyback - do it unless we're going to do sort */
- *search_req->largeSetLowerBound = 2000000000;
- *search_req->smallSetUpperBound = 1;
- *search_req->mediumSetPresentNumber =
- r->step>0 ? r->step : c->tasks->u.search.count;
- }
- else
- {
- /* non-piggyback. Need not provide elementsets or syntaxes .. */
- smallSetElementSetName = 0;
- mediumSetElementSetName = 0;
- syntax = 0;
- }
- if (smallSetElementSetName && *smallSetElementSetName)
- {
- Z_ElementSetNames *esn = (Z_ElementSetNames *)
- odr_malloc(c->odr_out, sizeof(*esn));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
- search_req->smallSetElementSetNames = esn;
- }
- if (mediumSetElementSetName && *mediumSetElementSetName)
- {
- Z_ElementSetNames *esn =(Z_ElementSetNames *)
- odr_malloc(c->odr_out, sizeof(*esn));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
- search_req->mediumSetElementSetNames = esn;
- }
- if (syntax)
- search_req->preferredRecordSyntax =
- yaz_str_to_z3950oid(c->odr_out, CLASS_RECSYN, syntax);
-
- if (!r->setname)
- {
- if (c->support_named_resultsets)
- {
- char setname[14];
- int ord;
- /* find the lowest unused ordinal so that we re-use
- result sets on the server. */
- for (ord = 1; ; ord++)
- {
- ZOOM_resultset rp;
- sprintf(setname, "%d", ord);
- for (rp = c->resultsets; rp; rp = rp->next)
- if (rp->setname && !strcmp(rp->setname, setname))
- break;
- if (!rp)
- break;
- }
- r->setname = xstrdup(setname);
- yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
- "set %s", c, r->setname);
- }
- else
- {
- yaz_log(log_details, "%p ZOOM_connection_send_search: using "
- "default set", c);
- r->setname = xstrdup("default");
- }
- ZOOM_options_set(r->options, "setname", r->setname);
- }
- search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
- return send_APDU(c, apdu);
-}
-
-static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)