-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_level, "encoding failed");
- set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
- odr_reset(out);
- return -1;
- }
-
- 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_level, "send APDU type=%d", 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.34 $");
- 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
- (c->odr_out,
- (const char **)&c->charset, (c->charset) ? 1:0,
- (const char **)&c->lang, (c->lang) ? 1:0, 1);
- }
- }
- assert (apdu);
- return send_APDU (c, apdu);
-}
-
-#if HAVE_XML2
-static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr)
-{
- char ctype[50];
- Z_SOAP_Handler h[2] = {
- {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
- {0, 0, 0}
- };
- ODR o = odr_createmem(ODR_ENCODE);
- int ret;
- Z_SOAP *p = (Z_SOAP*) odr_malloc(o, sizeof(*p));
- Z_GDU *gdu;
- ZOOM_Event event;
-
- gdu = z_get_HTTP_Request(c->odr_out);
- gdu->u.HTTP_Request->path = c->path;
-
- if (c->host_port)
- {
- const char *cp0 = strstr(c->host_port, "://");
- const char *cp1 = 0;
- if (cp0)
- cp0 = cp0+3;
- else
- cp0 = c->host_port;
-
- cp1 = strchr(cp0, '/');
- if (!cp1)
- cp1 = cp0+strlen(cp0);
-
- if (cp0 && cp1)
- {
- char *h = (char*) odr_malloc(c->odr_out, cp1 - cp0 + 1);
- memcpy (h, cp0, cp1 - cp0);
- h[cp1-cp0] = '\0';
- z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
- "Host", h);
- }
- }
-
- strcpy(ctype, "text/xml");
- if (c->charset && strlen(c->charset) < 20)
- {
- strcat(ctype, "; charset=");
- strcat(ctype, c->charset);
- }
- z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
- "Content-Type", ctype);
- z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
- "SOAPAction", "\"\"");
- p->which = Z_SOAP_generic;
- p->u.generic = (Z_SOAP_Generic *) odr_malloc(o, sizeof(*p->u.generic));
- p->u.generic->no = 0;
- p->u.generic->ns = 0;
- p->u.generic->p = sr;
- p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
-
- ret = z_soap_codec_enc(o, &p,
- &gdu->u.HTTP_Request->content_buf,
- &gdu->u.HTTP_Request->content_len, h,
- 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);
-
- odr_destroy(o);
-
- 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 HAVE_XML2
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
-{
- int i;
- ZOOM_resultset resultset = 0;
- Z_SRW_PDU *sr = 0;
- const char *recordPacking = 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);
- }
- else if(c->tasks->which == ZOOM_TASK_RETRIEVE)
- {
- resultset = c->tasks->u.retrieve.resultset;
-
- resultset->start = c->tasks->u.retrieve.start;
- resultset->count = c->tasks->u.retrieve.count;
-
- if (resultset->start >= resultset->size)
- return zoom_complete;
- if (resultset->start + resultset->count > resultset->size)
- resultset->count = resultset->size - resultset->start;
-
- for (i = 0; i<resultset->count; i++)
- {
- ZOOM_record rec =
- record_cache_lookup (resultset, i + resultset->start);
- if (!rec)
- break;
- }
- if (i == resultset->count)
- 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, resultset->start + 1);
- sr->u.request->maximumRecords = odr_intdup (
- c->odr_out, resultset->step>0 ? resultset->step : resultset->count);
- sr->u.request->recordSchema = resultset->schema;
-
- recordPacking = ZOOM_resultset_option_get (resultset, "recordPacking");
-
- if (recordPacking)
- sr->u.request->recordPacking = odr_strdup(c->odr_out, recordPacking);
-
- 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;
-
- 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 (r->start == 0 && r->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 : r->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_level, "allocating set %s", r->setname);
- }
- else
- r->setname = xstrdup ("default");
- ZOOM_options_set (r->options, "setname", r->setname);
- }
- search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
- /* send search request */
- return send_APDU (c, apdu);
-}
-
-static void response_diag (ZOOM_connection c, Z_DiagRec *p)
-{
- int oclass;
- Z_DefaultDiagFormat *r;
- char *addinfo = 0;
-
- xfree (c->addinfo);
- c->addinfo = 0;
- if (p->which != Z_DiagRec_defaultFormat)
- {
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- return;
- }
- r = p->u.defaultFormat;
- switch (r->which)
- {
- case Z_DefaultDiagFormat_v2Addinfo:
- addinfo = r->u.v2Addinfo;
- break;
- case Z_DefaultDiagFormat_v3Addinfo:
- addinfo = r->u.v3Addinfo;
- break;
- }
- set_dset_error(c, *r->condition,
- yaz_z3950oid_to_str(r->diagnosticSetId, &oclass),
- addinfo, 0);
-}
-
-ZOOM_API(ZOOM_record)
-ZOOM_record_clone (ZOOM_record srec)
-{
- char *buf;
- int size;
- ODR odr_enc;
- ZOOM_record nrec;
-
- odr_enc = odr_createmem(ODR_ENCODE);
- if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
- return 0;
- buf = odr_getbuf (odr_enc, &size, 0);
-
- nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
- nrec->odr = odr_createmem(ODR_DECODE);
- nrec->wrbuf_marc = 0;
- nrec->wrbuf_iconv = 0;
- nrec->wrbuf_opac = 0;
- odr_setbuf (nrec->odr, buf, size, 0);
- z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
-
- odr_destroy (odr_enc);
- return nrec;
-}
-
-ZOOM_API(ZOOM_record)
-ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
-{
- return record_cache_lookup (s, pos);
-}
-
-ZOOM_API(ZOOM_record)
-ZOOM_resultset_record (ZOOM_resultset r, size_t pos)