-static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
-{
- int i;
- for (i = 0; i<200; i++)
- {
- size_t len;
- Z_OtherInformation **oi;
- char buf[80];
- const char *val;
- const char *cp;
- int oidval;
-
- sprintf(buf, "otherInfo%d", i);
- val = ZOOM_options_get(c->options, buf);
- if (!val)
- break;
- cp = strchr(val, ':');
- if (!cp)
- continue;
- len = cp - val;
- if (len >= sizeof(buf))
- len = sizeof(buf)-1;
- memcpy(buf, val, len);
- buf[len] = '\0';
- oidval = oid_getvalbyname(buf);
- if (oidval == VAL_NONE)
- continue;
-
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
- }
-}
-
-static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
-{
- assert(a);
- if (c->cookie_out)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
- }
- if (c->client_IP)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
- }
- otherInfo_attach(c, a, out);
- if (!z_APDU(out, &a, 0, 0))
- {
- FILE *outf = fopen("/tmp/apdu.txt", "a");
- if (a && outf)
- {
- ODR odr_pr = odr_createmem(ODR_PRINT);
- fprintf(outf, "a=%p\n", a);
- odr_setprint(odr_pr, outf);
- z_APDU(odr_pr, &a, 0, 0);
- odr_destroy(odr_pr);
- }
- yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
- set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
- odr_reset(out);
- return -1;
- }
- yaz_log(log_details, "%p encoding_APDU encoding OK", c);
- return 0;
-}
-
-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)