2 * Copyright (c) 2000-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.5 2003-11-24 11:25:07 mike Exp $
7 * ZOOM layer for C, connections, result sets, queries.
13 #include <yaz/yaz-util.h>
14 #include <yaz/xmalloc.h>
15 #include <yaz/otherinfo.h>
17 #include <yaz/pquery.h>
18 #include <yaz/marcdisp.h>
19 #include <yaz/diagbib1.h>
20 #include <yaz/charneg.h>
33 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c);
34 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
36 static ZOOM_Event ZOOM_Event_create (int kind)
38 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
45 static void ZOOM_Event_destroy (ZOOM_Event event)
50 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
54 c->m_queue_back->prev = event;
55 assert (c->m_queue_front);
59 assert (!c->m_queue_front);
60 c->m_queue_front = event;
62 event->next = c->m_queue_back;
64 c->m_queue_back = event;
67 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
69 ZOOM_Event event = c->m_queue_front;
72 c->last_event = ZOOM_EVENT_NONE;
75 assert (c->m_queue_back);
76 c->m_queue_front = event->prev;
79 assert (c->m_queue_back);
80 c->m_queue_front->next = 0;
84 c->last_event = event->kind;
89 static void set_dset_error (ZOOM_connection c, int error,
91 const char *addinfo, const char *addinfo2)
97 if (addinfo && addinfo2)
99 c->addinfo = xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
100 strcpy(c->addinfo, addinfo);
101 strcat(c->addinfo, addinfo2);
104 c->addinfo = xstrdup(addinfo);
106 yaz_log(LOG_DEBUG, "Error %s %s:%d %s %s",
107 c->host_port ? c->host_port : "<>", dset, error,
108 addinfo ? addinfo : "",
109 addinfo2 ? addinfo2 : "");
113 static void set_HTTP_error (ZOOM_connection c, int error,
114 const char *addinfo, const char *addinfo2)
116 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
120 static void set_ZOOM_error (ZOOM_connection c, int error,
123 set_dset_error(c, error, "ZOOM", addinfo, 0);
126 static void clear_error (ZOOM_connection c)
131 case ZOOM_ERROR_CONNECT:
132 case ZOOM_ERROR_MEMORY:
133 case ZOOM_ERROR_DECODE:
134 case ZOOM_ERROR_CONNECTION_LOST:
135 case ZOOM_ERROR_INIT:
136 case ZOOM_ERROR_INTERNAL:
137 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
140 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
144 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
146 ZOOM_task *taskp = &c->tasks;
148 taskp = &(*taskp)->next;
149 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
150 (*taskp)->running = 0;
151 (*taskp)->which = which;
157 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
159 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
161 task->next = c->tasks;
170 void ZOOM_connection_remove_task (ZOOM_connection c)
172 ZOOM_task task = c->tasks;
176 c->tasks = task->next;
179 case ZOOM_TASK_SEARCH:
181 ZOOM_resultset_destroy (task->u.search.resultset);
183 case ZOOM_TASK_RETRIEVE:
184 ZOOM_resultset_destroy (task->u.retrieve.resultset);
186 case ZOOM_TASK_CONNECT:
189 ZOOM_scanset_destroy (task->u.scan.scan);
191 case ZOOM_TASK_PACKAGE:
192 ZOOM_package_destroy (task->u.package);
201 static int ZOOM_connection_exec_task (ZOOM_connection c);
203 void ZOOM_connection_remove_tasks (ZOOM_connection c)
206 ZOOM_connection_remove_task(c);
209 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
211 ZOOM_API(ZOOM_connection)
212 ZOOM_connection_create (ZOOM_options options)
214 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
216 c->proto = PROTO_Z3950;
220 c->state = STATE_IDLE;
222 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
229 c->options = ZOOM_options_create_with_parent(options);
235 c->charset = c->lang = 0;
242 c->odr_in = odr_createmem (ODR_DECODE);
243 c->odr_out = odr_createmem (ODR_ENCODE);
246 c->support_named_resultsets = 0;
247 c->last_event = ZOOM_EVENT_NONE;
249 c->m_queue_front = 0;
254 /* set database names. Take local databases (if set); otherwise
255 take databases given in ZURL (if set); otherwise use Default */
256 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
259 char **databaseNames;
262 const char *cp = ZOOM_options_get (options, "databaseName");
266 if (strncmp (con->host_port, "unix:", 5) == 0)
267 cp = strchr (con->host_port+5, ':');
269 cp = strchr (con->host_port, '/');
276 while ((c = strchr(c, '+')))
284 databaseNames = (char**)
285 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
289 c = strchr (cp, '+');
297 /* cp ptr to first char of db name, c is char
299 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
300 memcpy (databaseNames[no], cp, c-cp);
301 databaseNames[no++][c-cp] = '\0';
306 databaseNames[no] = NULL;
308 return databaseNames;
311 ZOOM_API(ZOOM_connection)
312 ZOOM_connection_new (const char *host, int portnum)
314 ZOOM_connection c = ZOOM_connection_create (0);
316 ZOOM_connection_connect (c, host, portnum);
321 ZOOM_connection_connect(ZOOM_connection c,
322 const char *host, int portnum)
329 yaz_log (LOG_DEBUG, "reconnect");
333 yaz_log(LOG_DEBUG, "connect");
335 val = ZOOM_options_get (c->options, "proxy");
337 c->proxy = xstrdup (val);
342 val = ZOOM_options_get (c->options, "charset");
344 c->charset = xstrdup (val);
349 val = ZOOM_options_get (c->options, "lang");
351 c->lang = xstrdup (val);
355 xfree (c->host_port);
359 sprintf (hostn, "%.80s:%d", host, portnum);
360 c->host_port = xstrdup(hostn);
363 c->host_port = xstrdup(host);
365 ZOOM_options_set(c->options, "host", c->host_port);
367 val = ZOOM_options_get (c->options, "cookie");
369 c->cookie_out = xstrdup (val);
371 val = ZOOM_options_get (c->options, "clientIP");
373 c->client_IP = xstrdup (val);
375 c->async = ZOOM_options_get_bool (c->options, "async", 0);
377 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
379 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
383 while (ZOOM_event (1, &c))
389 ZOOM_query_create(void)
391 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
396 s->odr = odr_createmem (ODR_ENCODE);
403 ZOOM_query_destroy(ZOOM_query s)
409 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
410 if (s->refcount == 0)
412 odr_destroy (s->odr);
418 ZOOM_query_prefix(ZOOM_query s, const char *str)
420 s->query_string = odr_strdup(s->odr, str);
421 s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query));
422 s->z_query->which = Z_Query_type_1;
423 s->z_query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
424 if (!s->z_query->u.type_1)
430 ZOOM_query_cql(ZOOM_query s, const char *str)
434 s->query_string = odr_strdup(s->odr, str);
436 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
437 ext->direct_reference = odr_getoidbystr(s->odr, "1.2.840.10003.16.2");
438 ext->indirect_reference = 0;
440 ext->which = Z_External_CQL;
441 ext->u.cql = s->query_string;
443 s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query));
444 s->z_query->which = Z_Query_type_104;
445 s->z_query->u.type_104 = ext;
451 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
453 s->sort_spec = yaz_sort_spec (s->odr, criteria);
459 static zoom_ret do_write(ZOOM_connection c);
462 ZOOM_connection_destroy(ZOOM_connection c)
469 for (r = c->resultsets; r; r = r->next)
474 odr_destroy (c->odr_in);
475 odr_destroy (c->odr_out);
476 ZOOM_options_destroy (c->options);
477 ZOOM_connection_remove_tasks (c);
478 xfree (c->host_port);
483 xfree (c->cookie_out);
484 xfree (c->cookie_in);
485 xfree (c->client_IP);
489 void ZOOM_resultset_addref (ZOOM_resultset r)
494 yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
498 ZOOM_resultset ZOOM_resultset_create ()
500 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
502 yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
505 r->odr = odr_createmem (ODR_ENCODE);
520 ZOOM_API(ZOOM_resultset)
521 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
524 ZOOM_query s = ZOOM_query_create();
526 ZOOM_query_prefix (s, q);
528 r = ZOOM_connection_search (c, s);
529 ZOOM_query_destroy (s);
533 ZOOM_API(ZOOM_resultset)
534 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
536 ZOOM_resultset r = ZOOM_resultset_create ();
540 r->r_sort_spec = q->sort_spec;
543 r->options = ZOOM_options_create_with_parent(c->options);
545 r->start = ZOOM_options_get_int(r->options, "start", 0);
546 r->count = ZOOM_options_get_int(r->options, "count", 0);
547 r->step = ZOOM_options_get_int(r->options, "step", 0);
548 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
549 cp = ZOOM_options_get (r->options, "setname");
551 r->setname = xstrdup(cp);
552 cp = ZOOM_options_get (r->options, "schema");
554 r->schema = xstrdup(cp);
558 r->next = c->resultsets;
561 if (c->host_port && c->proto == PROTO_HTTP)
565 yaz_log(LOG_DEBUG, "NO COMSTACK");
566 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
570 yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
575 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
576 task->u.search.resultset = r;
577 ZOOM_resultset_addref (r);
583 while (ZOOM_event (1, &c))
590 ZOOM_resultset_destroy(ZOOM_resultset r)
595 yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
597 if (r->refcount == 0)
599 ZOOM_record_cache rc;
601 for (rc = r->record_cache; rc; rc = rc->next)
603 if (rc->rec.wrbuf_marc)
604 wrbuf_free (rc->rec.wrbuf_marc, 1);
605 if (rc->rec.wrbuf_iconv)
606 wrbuf_free (rc->rec.wrbuf_iconv, 1);
607 if (rc->rec.wrbuf_opac)
608 wrbuf_free (rc->rec.wrbuf_opac, 1);
612 /* remove ourselves from the resultsets in connection */
613 ZOOM_resultset *rp = &r->connection->resultsets;
616 assert (*rp); /* we must be in this list!! */
618 { /* OK, we're here - take us out of it */
625 ZOOM_query_destroy (r->query);
626 ZOOM_options_destroy (r->options);
627 odr_destroy (r->odr);
635 ZOOM_resultset_size (ZOOM_resultset r)
640 static void do_close (ZOOM_connection c)
646 c->state = STATE_IDLE;
649 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
650 int force_sync, int start, int count)
662 if (c->host_port && c->proto == PROTO_HTTP)
666 yaz_log(LOG_DEBUG, "NO COMSTACK");
667 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
671 yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
675 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
676 task->u.retrieve.resultset = r;
677 task->u.retrieve.start = start;
678 task->u.retrieve.count = count;
680 cp = ZOOM_options_get (r->options, "schema");
683 if (!r->schema || strcmp(r->schema, cp))
686 r->schema = xstrdup(cp);
690 ZOOM_resultset_addref (r);
692 if (!r->connection->async || force_sync)
693 while (r->connection && ZOOM_event (1, &r->connection))
698 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
699 size_t start, size_t count)
701 int force_present = 0;
707 ZOOM_resultset_retrieve (r, force_present, start, count);
711 for (i = 0; i< count; i++)
712 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
716 static zoom_ret do_connect (ZOOM_connection c)
719 const char *effective_host;
722 effective_host = c->proxy;
724 effective_host = c->host_port;
726 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
729 c->cs = cs_create_host (effective_host, 0, &add);
731 if (c->cs && c->cs->protocol == PROTO_HTTP)
734 const char *path = 0;
736 c->proto = PROTO_HTTP;
737 cs_get_host_args(c->host_port, &path);
739 c->path = xmalloc(strlen(path)+2);
741 strcpy (c->path+1, path);
743 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
745 return zoom_complete;
750 int ret = cs_connect (c->cs, add);
753 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
754 ZOOM_connection_put_event(c, event);
755 if (c->proto == PROTO_Z3950)
756 ZOOM_connection_send_init(c);
759 /* no init request for SRW .. */
760 assert (c->tasks->which == ZOOM_TASK_CONNECT);
761 ZOOM_connection_remove_task (c);
763 ZOOM_connection_exec_task (c);
765 c->state = STATE_ESTABLISHED;
770 c->state = STATE_CONNECTING;
771 c->mask = ZOOM_SELECT_EXCEPT;
772 if (c->cs->io_pending & CS_WANT_WRITE)
773 c->mask += ZOOM_SELECT_WRITE;
774 if (c->cs->io_pending & CS_WANT_READ)
775 c->mask += ZOOM_SELECT_READ;
779 c->state = STATE_IDLE;
780 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, effective_host);
781 return zoom_complete;
784 int z3950_connection_socket(ZOOM_connection c)
787 return cs_fileno(c->cs);
791 int z3950_connection_mask(ZOOM_connection c)
798 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
801 for (i = 0; i<200; i++)
804 Z_OtherInformation **oi;
810 sprintf (buf, "otherInfo%d", i);
811 val = ZOOM_options_get (c->options, buf);
814 cp = strchr (val, ':');
818 if (len >= sizeof(buf))
820 memcpy (buf, val, len);
822 oidval = oid_getvalbyname (buf);
823 if (oidval == VAL_NONE)
827 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
831 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
836 Z_OtherInformation **oi;
838 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
842 Z_OtherInformation **oi;
844 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
846 otherInfo_attach (c, a, out);
847 if (!z_APDU(out, &a, 0, 0))
849 FILE *outf = fopen("/tmp/apdu.txt", "a");
852 ODR odr_pr = odr_createmem(ODR_PRINT);
853 fprintf (outf, "a=%p\n", a);
854 odr_setprint(odr_pr, outf);
855 z_APDU(odr_pr, &a, 0, 0);
858 yaz_log (LOG_DEBUG, "encoding failed");
859 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
867 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
871 if (encode_APDU(c, a, c->odr_out))
872 return zoom_complete;
873 yaz_log(LOG_DEBUG, "send APDU type=%d", a->which);
874 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
875 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
876 ZOOM_connection_put_event (c, event);
877 odr_reset(c->odr_out);
881 /* returns 1 if PDU was sent OK (still pending )
882 0 if PDU was not sent OK (nothing to wait for)
885 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
887 const char *impid, *impname, *impver;
888 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
889 Z_InitRequest *ireq = apdu->u.initRequest;
890 Z_IdAuthentication *auth = (Z_IdAuthentication *)
891 odr_malloc(c->odr_out, sizeof(*auth));
892 const char *auth_groupId = ZOOM_options_get (c->options, "group");
893 const char *auth_userId = ZOOM_options_get (c->options, "user");
894 const char *auth_password = ZOOM_options_get (c->options, "pass");
896 ODR_MASK_SET(ireq->options, Z_Options_search);
897 ODR_MASK_SET(ireq->options, Z_Options_present);
898 ODR_MASK_SET(ireq->options, Z_Options_scan);
899 ODR_MASK_SET(ireq->options, Z_Options_sort);
900 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
901 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
903 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
904 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
905 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
907 impid = ZOOM_options_get (c->options, "implementationId");
908 ireq->implementationId =
909 (char *) odr_malloc (c->odr_out, 15 + (impid ? strlen(impid) : 0));
910 strcpy (ireq->implementationId, "");
913 strcat (ireq->implementationId, impid);
914 strcat (ireq->implementationId, "/");
916 strcat (ireq->implementationId, "81"); /* Index's implementor ID */
918 impname = ZOOM_options_get (c->options, "implementationName");
919 ireq->implementationName =
920 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
921 strcpy (ireq->implementationName, "");
924 strcat (ireq->implementationName, impname);
925 strcat (ireq->implementationName, "/");
927 strcat (ireq->implementationName, "ZOOM-C/YAZ");
929 impver = ZOOM_options_get (c->options, "implementationVersion");
930 ireq->implementationVersion =
931 (char *) odr_malloc (c->odr_out, strlen("$Revision: 1.5 $") + 2 +
932 (impver ? strlen(impver) : 0));
933 strcpy (ireq->implementationVersion, "");
936 strcat (ireq->implementationVersion, impver);
937 strcat (ireq->implementationVersion, "/");
939 strcat (ireq->implementationVersion, "$Revision: 1.5 $");
941 *ireq->maximumRecordSize =
942 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
943 *ireq->preferredMessageSize =
944 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
946 if (auth_groupId || auth_password)
948 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
951 if (auth_groupId && *auth_groupId)
953 pass->groupId = (char *)
954 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
955 strcpy(pass->groupId, auth_groupId);
959 if (auth_userId && *auth_userId)
961 pass->userId = (char *)
962 odr_malloc(c->odr_out, strlen(auth_userId)+1);
963 strcpy(pass->userId, auth_userId);
967 if (auth_password && *auth_password)
969 pass->password = (char *)
970 odr_malloc(c->odr_out, strlen(auth_password)+1);
971 strcpy(pass->password, auth_password);
976 auth->which = Z_IdAuthentication_idPass;
977 auth->u.idPass = pass;
978 ireq->idAuthentication = auth;
981 else if (auth_userId)
983 auth->which = Z_IdAuthentication_open;
984 auth->u.open = (char *)
985 odr_malloc(c->odr_out, strlen(auth_userId)+1);
986 strcpy(auth->u.open, auth_userId);
987 ireq->idAuthentication = auth;
990 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
991 VAL_PROXY, 1, c->host_port);
992 if (c->charset||c->lang)
994 Z_OtherInformation **oi;
995 Z_OtherInformationUnit *oi_unit;
997 yaz_oi_APDU(apdu, &oi);
999 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1001 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1003 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1004 oi_unit->information.externallyDefinedInfo =
1005 yaz_set_proposal_charneg
1007 (const char **)&c->charset, (c->charset) ? 1:0,
1008 (const char **)&c->lang, (c->lang) ? 1:0, 1);
1012 return send_APDU (c, apdu);
1016 static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr)
1019 Z_SOAP_Handler h[2] = {
1020 {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
1023 ODR o = odr_createmem(ODR_ENCODE);
1025 Z_SOAP *p = odr_malloc(o, sizeof(*p));
1029 gdu = z_get_HTTP_Request(c->odr_out);
1030 gdu->u.HTTP_Request->path = c->path;
1034 const char *cp0 = strstr(c->host_port, "://");
1035 const char *cp1 = 0;
1041 cp1 = strchr(cp0, '/');
1043 cp1 = cp0+strlen(cp0);
1047 char *h = odr_malloc(c->odr_out, cp1 - cp0 + 1);
1048 memcpy (h, cp0, cp1 - cp0);
1050 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1055 strcpy(ctype, "text/xml");
1056 if (c->charset && strlen(c->charset) < 20)
1058 strcat(ctype, "; charset=");
1059 strcat(ctype, c->charset);
1061 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1062 "Content-Type", ctype);
1063 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1064 "SOAPAction", "\"\"");
1065 p->which = Z_SOAP_generic;
1066 p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
1067 p->u.generic->no = 0;
1068 p->u.generic->ns = 0;
1069 p->u.generic->p = sr;
1070 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1072 ret = z_soap_codec_enc(o, &p,
1073 &gdu->u.HTTP_Request->content_buf,
1074 &gdu->u.HTTP_Request->content_len, h,
1077 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1078 return zoom_complete;
1079 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1083 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1084 ZOOM_connection_put_event (c, event);
1085 odr_reset(c->odr_out);
1086 return do_write (c);
1091 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1094 ZOOM_resultset resultset = 0;
1096 const char *recordPacking = 0;
1098 if (c->error) /* don't continue on error */
1099 return zoom_complete;
1101 if (c->tasks->which == ZOOM_TASK_SEARCH)
1103 resultset = c->tasks->u.search.resultset;
1104 resultset->setname = xstrdup ("default");
1105 ZOOM_options_set (resultset->options, "setname", resultset->setname);
1107 else if(c->tasks->which == ZOOM_TASK_RETRIEVE)
1109 resultset = c->tasks->u.retrieve.resultset;
1111 resultset->start = c->tasks->u.retrieve.start;
1112 resultset->count = c->tasks->u.retrieve.count;
1114 if (resultset->start >= resultset->size)
1115 return zoom_complete;
1116 if (resultset->start + resultset->count > resultset->size)
1117 resultset->count = resultset->size - resultset->start;
1119 for (i = 0; i<resultset->count; i++)
1122 record_cache_lookup (resultset, i + resultset->start);
1126 if (i == resultset->count)
1127 return zoom_complete;
1129 assert(resultset->query);
1131 sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
1133 if (resultset->query->z_query->which == Z_Query_type_104
1134 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1137 sr->u.request->query_type = Z_SRW_query_type_cql;
1138 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1140 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1141 resultset->query->z_query->u.type_1)
1143 sr->u.request->query_type = Z_SRW_query_type_pqf;
1144 sr->u.request->query.pqf = resultset->query->query_string;
1148 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1149 return zoom_complete;
1151 sr->u.request->startRecord = odr_intdup (c->odr_out, resultset->start + 1);
1152 sr->u.request->maximumRecords = odr_intdup (
1153 c->odr_out, resultset->step>0 ? resultset->step : resultset->count);
1154 sr->u.request->recordSchema = resultset->schema;
1156 recordPacking = ZOOM_resultset_option_get (resultset, "recordPacking");
1159 sr->u.request->recordPacking = odr_strdup(c->odr_out, recordPacking);
1161 return send_srw(c, sr);
1164 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1166 return zoom_complete;
1170 static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
1173 int lslb, ssub, mspn;
1175 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1176 Z_SearchRequest *search_req = apdu->u.searchRequest;
1177 const char *elementSetName;
1178 const char *smallSetElementSetName;
1179 const char *mediumSetElementSetName;
1182 assert (c->tasks->which == ZOOM_TASK_SEARCH);
1184 r = c->tasks->u.search.resultset;
1187 ZOOM_options_get (r->options, "elementSetName");
1188 smallSetElementSetName =
1189 ZOOM_options_get (r->options, "smallSetElementSetName");
1190 mediumSetElementSetName =
1191 ZOOM_options_get (r->options, "mediumSetElementSetName");
1193 if (!smallSetElementSetName)
1194 smallSetElementSetName = elementSetName;
1196 if (!mediumSetElementSetName)
1197 mediumSetElementSetName = elementSetName;
1202 /* prepare query for the search request */
1203 search_req->query = r->query->z_query;
1205 search_req->databaseNames =
1206 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
1208 /* get syntax (no need to provide unless piggyback is in effect) */
1209 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
1211 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
1212 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
1213 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
1214 if (lslb != -1 && ssub != -1 && mspn != -1)
1216 /* So're a Z39.50 expert? Let's hope you don't do sort */
1217 *search_req->largeSetLowerBound = lslb;
1218 *search_req->smallSetUpperBound = ssub;
1219 *search_req->mediumSetPresentNumber = mspn;
1221 else if (r->start == 0 && r->count > 0
1222 && r->piggyback && !r->r_sort_spec && !r->schema)
1224 /* Regular piggyback - do it unless we're going to do sort */
1225 *search_req->largeSetLowerBound = 2000000000;
1226 *search_req->smallSetUpperBound = 1;
1227 *search_req->mediumSetPresentNumber = r->step>0 ? r->step : r->count;
1231 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1232 smallSetElementSetName = 0;
1233 mediumSetElementSetName = 0;
1236 if (smallSetElementSetName && *smallSetElementSetName)
1238 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1239 odr_malloc (c->odr_out, sizeof(*esn));
1241 esn->which = Z_ElementSetNames_generic;
1242 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
1243 search_req->smallSetElementSetNames = esn;
1245 if (mediumSetElementSetName && *mediumSetElementSetName)
1247 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1248 odr_malloc (c->odr_out, sizeof(*esn));
1250 esn->which = Z_ElementSetNames_generic;
1251 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
1252 search_req->mediumSetElementSetNames = esn;
1255 search_req->preferredRecordSyntax =
1256 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1260 if (c->support_named_resultsets)
1264 /* find the lowest unused ordinal so that we re-use
1265 result sets on the server. */
1266 for (ord = 1; ; ord++)
1269 sprintf (setname, "%d", ord);
1270 for (rp = c->resultsets; rp; rp = rp->next)
1271 if (rp->setname && !strcmp (rp->setname, setname))
1276 r->setname = xstrdup (setname);
1277 yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
1280 r->setname = xstrdup ("default");
1281 ZOOM_options_set (r->options, "setname", r->setname);
1283 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1284 /* send search request */
1285 return send_APDU (c, apdu);
1288 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
1291 Z_DefaultDiagFormat *r;
1296 if (p->which != Z_DiagRec_defaultFormat)
1298 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1301 r = p->u.defaultFormat;
1304 case Z_DefaultDiagFormat_v2Addinfo:
1305 addinfo = r->u.v2Addinfo;
1307 case Z_DefaultDiagFormat_v3Addinfo:
1308 addinfo = r->u.v3Addinfo;
1311 set_dset_error(c, *r->condition,
1312 yaz_z3950oid_to_str(r->diagnosticSetId, &oclass),
1316 ZOOM_API(ZOOM_record)
1317 ZOOM_record_clone (ZOOM_record srec)
1324 odr_enc = odr_createmem(ODR_ENCODE);
1325 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
1327 buf = odr_getbuf (odr_enc, &size, 0);
1329 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
1330 nrec->odr = odr_createmem(ODR_DECODE);
1331 nrec->wrbuf_marc = 0;
1332 nrec->wrbuf_iconv = 0;
1333 nrec->wrbuf_opac = 0;
1334 odr_setbuf (nrec->odr, buf, size, 0);
1335 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
1337 odr_destroy (odr_enc);
1341 ZOOM_API(ZOOM_record)
1342 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1344 return record_cache_lookup (s, pos);
1347 ZOOM_API(ZOOM_record)
1348 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1350 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1354 ZOOM_resultset_retrieve (r, 1, pos, 1);
1355 rec = ZOOM_resultset_record_immediate (r, pos);
1361 ZOOM_record_destroy (ZOOM_record rec)
1365 if (rec->wrbuf_marc)
1366 wrbuf_free (rec->wrbuf_marc, 1);
1367 if (rec->wrbuf_iconv)
1368 wrbuf_free (rec->wrbuf_iconv, 1);
1369 if (rec->wrbuf_opac)
1370 wrbuf_free (rec->wrbuf_opac, 1);
1371 odr_destroy (rec->odr);
1375 static const char *record_iconv_return(ZOOM_record rec, int *len,
1376 const char *buf, int sz,
1377 const char *record_charset)
1384 strcpy(to, "UTF-8");
1385 if (record_charset && *record_charset)
1387 /* Use "from,to" or just "from" */
1388 const char *cp =strchr(record_charset, ',');
1389 int clen = strlen(record_charset);
1392 strncpy( to, cp+1, sizeof(to)-1);
1393 to[sizeof(to)-1] = '\0';
1394 clen = cp - record_charset;
1396 if (clen > sizeof(from)-1)
1397 clen = sizeof(from)-1;
1400 strncpy(from, record_charset, clen);
1404 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1407 size_t inbytesleft = sz;
1408 const char *inp = buf;
1410 if (!rec->wrbuf_iconv)
1411 rec->wrbuf_iconv = wrbuf_alloc();
1413 wrbuf_rewind(rec->wrbuf_iconv);
1417 size_t outbytesleft = sizeof(outbuf);
1418 char *outp = outbuf;
1419 size_t r = yaz_iconv (cd, (char**) &inp,
1421 &outp, &outbytesleft);
1422 if (r == (size_t) (-1))
1424 int e = yaz_iconv_error(cd);
1425 if (e != YAZ_ICONV_E2BIG)
1428 wrbuf_write(rec->wrbuf_iconv, outbuf, outp - outbuf);
1430 wrbuf_puts(rec->wrbuf_iconv, "");
1431 buf = wrbuf_buf(rec->wrbuf_iconv);
1432 sz = wrbuf_len(rec->wrbuf_iconv);
1433 yaz_iconv_close(cd);
1440 ZOOM_API(const char *)
1441 ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
1447 Z_NamePlusRecord *npr;
1450 *len = 0; /* default return */
1459 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
1461 if (cp[i] == ';' || cp[i] == ' ')
1467 if (type_spec[i] == ';')
1470 while (type_spec[i] == ' ')
1472 if (!strncmp(type_spec+i, "charset=", 8))
1475 for (i = 0; cp[i] && i < sizeof(charset)-1; i++)
1477 if (cp[i] == ';' || cp[i] == ' ')
1485 if (!strcmp (type, "database"))
1488 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1489 return npr->databaseName;
1491 else if (!strcmp (type, "syntax"))
1493 const char *desc = 0;
1494 if (npr->which == Z_NamePlusRecord_databaseRecord)
1496 Z_External *r = (Z_External *) npr->u.databaseRecord;
1497 oident *ent = oid_getentbyoid(r->direct_reference);
1504 *len = strlen(desc);
1507 else if (!strcmp (type, "render") &&
1508 npr->which == Z_NamePlusRecord_databaseRecord)
1510 Z_External *r = (Z_External *) npr->u.databaseRecord;
1511 oident *ent = oid_getentbyoid(r->direct_reference);
1513 /* render bibliographic record .. */
1514 if (r->which == Z_External_OPAC)
1516 r = r->u.opac->bibliographicRecord;
1519 ent = oid_getentbyoid(r->direct_reference);
1521 if (r->which == Z_External_sutrs)
1522 return record_iconv_return(rec, len,
1523 r->u.sutrs->buf, r->u.sutrs->len,
1525 else if (r->which == Z_External_octet)
1535 case VAL_APPLICATION_XML:
1538 if (!rec->wrbuf_marc)
1539 rec->wrbuf_marc = wrbuf_alloc();
1541 mt = yaz_marc_create();
1542 wrbuf_rewind (rec->wrbuf_marc);
1543 if (yaz_marc_decode_wrbuf (
1544 mt, (const char *) r->u.octet_aligned->buf,
1545 r->u.octet_aligned->len,
1546 rec->wrbuf_marc) > 0)
1548 yaz_marc_destroy(mt);
1549 return record_iconv_return(rec, len,
1550 wrbuf_buf(rec->wrbuf_marc),
1551 wrbuf_len(rec->wrbuf_marc),
1554 yaz_marc_destroy(mt);
1556 return record_iconv_return(rec, len,
1557 (const char *) r->u.octet_aligned->buf,
1558 r->u.octet_aligned->len,
1561 else if (r->which == Z_External_grs1)
1563 if (!rec->wrbuf_marc)
1564 rec->wrbuf_marc = wrbuf_alloc();
1565 wrbuf_rewind (rec->wrbuf_marc);
1566 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
1567 return record_iconv_return(rec, len,
1568 wrbuf_buf(rec->wrbuf_marc),
1569 wrbuf_len(rec->wrbuf_marc),
1574 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1575 (!strcmp (type, "xml") || !strcmp(type, "oai")))
1577 Z_External *r = (Z_External *) npr->u.databaseRecord;
1578 oident *ent = oid_getentbyoid(r->direct_reference);
1580 /* render bibliographic record .. */
1581 if (r->which == Z_External_OPAC)
1583 r = r->u.opac->bibliographicRecord;
1586 ent = oid_getentbyoid(r->direct_reference);
1589 if (r->which == Z_External_sutrs)
1590 return record_iconv_return(rec, len,
1591 (const char *) r->u.sutrs->buf,
1594 else if (r->which == Z_External_octet)
1597 int marc_decode_type = YAZ_MARC_MARCXML;
1599 if (!strcmp(type, "oai"))
1600 marc_decode_type = YAZ_MARC_OAIMARC;
1608 case VAL_APPLICATION_XML:
1611 if (!rec->wrbuf_marc)
1612 rec->wrbuf_marc = wrbuf_alloc();
1613 wrbuf_rewind (rec->wrbuf_marc);
1614 mt = yaz_marc_create();
1616 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
1617 if (yaz_marc_decode_wrbuf (
1618 mt, (const char *) r->u.octet_aligned->buf,
1619 r->u.octet_aligned->len,
1620 rec->wrbuf_marc) > 0)
1622 yaz_marc_destroy(mt);
1623 return record_iconv_return(rec, len,
1624 wrbuf_buf(rec->wrbuf_marc),
1625 wrbuf_len(rec->wrbuf_marc),
1628 yaz_marc_destroy(mt);
1630 return record_iconv_return(rec, len,
1631 (const char *) r->u.octet_aligned->buf,
1632 r->u.octet_aligned->len,
1635 else if (r->which == Z_External_grs1)
1642 else if (!strcmp (type, "raw"))
1644 if (npr->which == Z_NamePlusRecord_databaseRecord)
1646 Z_External *r = (Z_External *) npr->u.databaseRecord;
1648 if (r->which == Z_External_sutrs)
1650 if (len) *len = r->u.sutrs->len;
1651 return (const char *) r->u.sutrs->buf;
1653 else if (r->which == Z_External_octet)
1655 if (len) *len = r->u.octet_aligned->len;
1656 return (const char *) r->u.octet_aligned->buf;
1658 else /* grs-1, explain, OPAC, ... */
1661 return (const char *) npr->u.databaseRecord;
1666 else if (!strcmp (type, "ext"))
1668 if (npr->which == Z_NamePlusRecord_databaseRecord)
1669 return (const char *) npr->u.databaseRecord;
1672 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1673 !strcmp (type, "opac"))
1676 Z_External *r = (Z_External *) npr->u.databaseRecord;
1677 if (r->which == Z_External_OPAC)
1679 if (!rec->wrbuf_opac)
1680 rec->wrbuf_opac = wrbuf_alloc();
1681 wrbuf_rewind (rec->wrbuf_opac);
1682 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
1683 return record_iconv_return(rec, len,
1684 wrbuf_buf(rec->wrbuf_opac),
1685 wrbuf_len(rec->wrbuf_opac),
1692 static int strcmp_null(const char *v1, const char *v2)
1698 return strcmp(v1, v2);
1701 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr,
1704 ZOOM_record_cache rc;
1705 const char *elementSetName =
1706 ZOOM_resultset_option_get (r, "elementSetName");
1707 const char *syntax =
1708 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1710 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1711 ZOOM_connection_put_event(r->connection, event);
1713 for (rc = r->record_cache; rc; rc = rc->next)
1717 if (strcmp_null(r->schema, rc->schema))
1719 if (strcmp_null(elementSetName,rc->elementSetName))
1721 if (strcmp_null(syntax, rc->syntax))
1723 /* not destroying rc->npr (it's handled by nmem )*/
1725 /* keeping wrbuf_marc too */
1729 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1732 rc->rec.wrbuf_marc = 0;
1733 rc->rec.wrbuf_iconv = 0;
1734 rc->rec.wrbuf_opac = 0;
1736 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1738 rc->elementSetName = 0;
1741 rc->syntax = odr_strdup (r->odr, syntax);
1746 rc->schema = odr_strdup (r->odr, r->schema);
1751 rc->next = r->record_cache;
1752 r->record_cache = rc;
1755 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1757 ZOOM_record_cache rc;
1758 const char *elementSetName =
1759 ZOOM_resultset_option_get (r, "elementSetName");
1760 const char *syntax =
1761 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1763 for (rc = r->record_cache; rc; rc = rc->next)
1767 if (strcmp_null(r->schema, rc->schema))
1769 if (strcmp_null(elementSetName,rc->elementSetName))
1771 if (strcmp_null(syntax, rc->syntax))
1779 static void handle_records (ZOOM_connection c, Z_Records *sr,
1782 ZOOM_resultset resultset;
1786 switch (c->tasks->which)
1788 case ZOOM_TASK_SEARCH:
1789 resultset = c->tasks->u.search.resultset;
1791 case ZOOM_TASK_RETRIEVE:
1792 resultset = c->tasks->u.retrieve.resultset;
1797 if (sr && sr->which == Z_Records_NSD)
1799 Z_DiagRec dr, *dr_p = &dr;
1800 dr.which = Z_DiagRec_defaultFormat;
1801 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1803 response_diag (c, dr_p);
1805 else if (sr && sr->which == Z_Records_multipleNSD)
1807 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1808 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1810 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1814 if (resultset->count + resultset->start > resultset->size)
1815 resultset->count = resultset->size - resultset->start;
1816 if (resultset->count < 0)
1817 resultset->count = 0;
1818 if (sr && sr->which == Z_Records_DBOSD)
1821 NMEM nmem = odr_extract_mem (c->odr_in);
1822 Z_NamePlusRecordList *p =
1823 sr->u.databaseOrSurDiagnostics;
1824 for (i = 0; i<p->num_records; i++)
1826 record_cache_add (resultset, p->records[i],
1827 i+ resultset->start);
1829 /* transfer our response to search_nmem .. we need it later */
1830 nmem_transfer (resultset->odr->mem, nmem);
1831 nmem_destroy (nmem);
1832 if (present_phase && p->num_records == 0)
1834 /* present response and we didn't get any records! */
1835 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1838 else if (present_phase)
1840 /* present response and we didn't get any records! */
1841 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1846 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1848 handle_records (c, pr->records, 1);
1851 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1853 ZOOM_resultset resultset;
1856 yaz_log (LOG_DEBUG, "got search response");
1858 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1861 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
1862 ZOOM_connection_put_event(c, event);
1864 resultset = c->tasks->u.search.resultset;
1866 resultset->size = *sr->resultCount;
1867 handle_records (c, sr->records, 0);
1870 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1872 if (res->diagnostics && res->num_diagnostics > 0)
1873 response_diag (c, res->diagnostics[0]);
1876 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1878 NMEM nmem = odr_extract_mem (c->odr_in);
1881 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1883 scan = c->tasks->u.scan.scan;
1885 if (res->entries && res->entries->nonsurrogateDiagnostics)
1886 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1887 scan->scan_response = res;
1888 nmem_transfer (scan->odr->mem, nmem);
1890 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1891 if (res->positionOfTerm)
1892 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1893 if (res->scanStatus)
1894 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1895 if (res->numberOfEntriesReturned)
1896 ZOOM_options_set_int (scan->options, "number",
1897 *res->numberOfEntriesReturned);
1898 nmem_destroy (nmem);
1902 static zoom_ret send_sort (ZOOM_connection c)
1904 ZOOM_resultset resultset;
1906 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1907 return zoom_complete;
1909 resultset = c->tasks->u.search.resultset;
1913 resultset->r_sort_spec = 0;
1914 return zoom_complete;
1916 if (resultset->r_sort_spec)
1918 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1919 Z_SortRequest *req = apdu->u.sortRequest;
1921 req->num_inputResultSetNames = 1;
1922 req->inputResultSetNames = (Z_InternationalString **)
1923 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1924 req->inputResultSetNames[0] =
1925 odr_strdup (c->odr_out, resultset->setname);
1926 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1927 req->sortSequence = resultset->r_sort_spec;
1928 resultset->r_sort_spec = 0;
1929 return send_APDU (c, apdu);
1931 return zoom_complete;
1934 static zoom_ret send_present (ZOOM_connection c)
1937 Z_PresentRequest *req = 0;
1939 const char *syntax = 0;
1940 const char *elementSetName = 0;
1941 ZOOM_resultset resultset;
1944 return zoom_complete;
1946 switch (c->tasks->which)
1948 case ZOOM_TASK_SEARCH:
1949 resultset = c->tasks->u.search.resultset;
1951 case ZOOM_TASK_RETRIEVE:
1952 resultset = c->tasks->u.retrieve.resultset;
1953 resultset->start = c->tasks->u.retrieve.start;
1954 resultset->count = c->tasks->u.retrieve.count;
1956 if (resultset->start >= resultset->size)
1957 return zoom_complete;
1958 if (resultset->start + resultset->count > resultset->size)
1959 resultset->count = resultset->size - resultset->start;
1962 return zoom_complete;
1965 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1966 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1968 if (c->error) /* don't continue on error */
1969 return zoom_complete;
1970 if (resultset->start < 0)
1971 return zoom_complete;
1972 for (i = 0; i<resultset->count; i++)
1975 record_cache_lookup (resultset, i + resultset->start);
1979 if (i == resultset->count)
1980 return zoom_complete;
1982 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1983 req = apdu->u.presentRequest;
1985 resultset->start += i;
1986 resultset->count -= i;
1987 *req->resultSetStartPoint = resultset->start + 1;
1988 *req->numberOfRecordsRequested = resultset->step>0 ?
1989 resultset->step : resultset->count;
1990 assert (*req->numberOfRecordsRequested > 0);
1992 if (syntax && *syntax)
1993 req->preferredRecordSyntax =
1994 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1996 if (resultset->schema && *resultset->schema)
1998 Z_RecordComposition *compo = (Z_RecordComposition *)
1999 odr_malloc (c->odr_out, sizeof(*compo));
2001 req->recordComposition = compo;
2002 compo->which = Z_RecordComp_complex;
2003 compo->u.complex = (Z_CompSpec *)
2004 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2005 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2006 odr_malloc(c->odr_out, sizeof(bool_t));
2007 *compo->u.complex->selectAlternativeSyntax = 0;
2009 compo->u.complex->generic = (Z_Specification *)
2010 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2012 compo->u.complex->generic->which = Z_Schema_oid;
2013 compo->u.complex->generic->schema.oid = (Odr_oid *)
2014 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, resultset->schema);
2016 if (!compo->u.complex->generic->schema.oid)
2018 /* OID wasn't a schema! Try record syntax instead. */
2020 compo->u.complex->generic->schema.oid = (Odr_oid *)
2021 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, resultset->schema);
2023 if (elementSetName && *elementSetName)
2025 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2026 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2027 compo->u.complex->generic->elementSpec->which =
2028 Z_ElementSpec_elementSetName;
2029 compo->u.complex->generic->elementSpec->u.elementSetName =
2030 odr_strdup (c->odr_out, elementSetName);
2033 compo->u.complex->generic->elementSpec = 0;
2034 compo->u.complex->num_dbSpecific = 0;
2035 compo->u.complex->dbSpecific = 0;
2036 compo->u.complex->num_recordSyntax = 0;
2037 compo->u.complex->recordSyntax = 0;
2039 else if (elementSetName && *elementSetName)
2041 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2042 odr_malloc (c->odr_out, sizeof(*esn));
2043 Z_RecordComposition *compo = (Z_RecordComposition *)
2044 odr_malloc (c->odr_out, sizeof(*compo));
2046 esn->which = Z_ElementSetNames_generic;
2047 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
2048 compo->which = Z_RecordComp_simple;
2049 compo->u.simple = esn;
2050 req->recordComposition = compo;
2052 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2053 return send_APDU (c, apdu);
2056 ZOOM_API(ZOOM_scanset)
2057 ZOOM_connection_scan (ZOOM_connection c, const char *start)
2059 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
2061 scan->connection = c;
2062 scan->odr = odr_createmem (ODR_DECODE);
2063 scan->options = ZOOM_options_create_with_parent (c->options);
2065 scan->scan_response = 0;
2067 if ((scan->termListAndStartPoint =
2068 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
2071 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
2072 task->u.scan.scan = scan;
2077 while (ZOOM_event (1, &c))
2085 ZOOM_scanset_destroy (ZOOM_scanset scan)
2090 if (scan->refcount == 0)
2092 odr_destroy (scan->odr);
2094 ZOOM_options_destroy (scan->options);
2099 static zoom_ret send_package (ZOOM_connection c)
2103 return zoom_complete;
2104 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2106 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
2107 ZOOM_connection_put_event (c, event);
2109 return do_write_ex (c, c->tasks->u.package->buf_out,
2110 c->tasks->u.package->len_out);
2113 static zoom_ret send_scan (ZOOM_connection c)
2116 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2117 Z_ScanRequest *req = apdu->u.scanRequest;
2119 return zoom_complete;
2120 assert (c->tasks->which == ZOOM_TASK_SCAN);
2121 scan = c->tasks->u.scan.scan;
2123 req->termListAndStartPoint = scan->termListAndStartPoint;
2124 req->attributeSet = scan->attributeSet;
2126 *req->numberOfTermsRequested =
2127 ZOOM_options_get_int(scan->options, "number", 10);
2129 req->preferredPositionInResponse =
2130 odr_intdup (c->odr_out,
2131 ZOOM_options_get_int(scan->options, "position", 1));
2134 odr_intdup (c->odr_out,
2135 ZOOM_options_get_int(scan->options, "stepSize", 0));
2137 req->databaseNames = set_DatabaseNames (c, scan->options,
2138 &req->num_databaseNames);
2140 return send_APDU (c, apdu);
2144 ZOOM_scanset_size (ZOOM_scanset scan)
2146 if (!scan || !scan->scan_response || !scan->scan_response->entries)
2148 return scan->scan_response->entries->num_entries;
2151 ZOOM_API(const char *)
2152 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
2155 const char *term = 0;
2156 size_t noent = ZOOM_scanset_size (scan);
2157 Z_ScanResponse *res = scan->scan_response;
2163 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2165 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2167 if (t->term->which == Z_Term_general)
2169 term = (const char *) t->term->u.general->buf;
2170 *len = t->term->u.general->len;
2172 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2177 ZOOM_API(const char *)
2178 ZOOM_scanset_display_term (ZOOM_scanset scan, size_t pos,
2181 const char *term = 0;
2182 size_t noent = ZOOM_scanset_size (scan);
2183 Z_ScanResponse *res = scan->scan_response;
2189 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2191 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2195 term = (const char *) t->term->u.general->buf;
2196 *len = strlen(term);
2198 else if (t->term->which == Z_Term_general)
2200 term = (const char *) t->term->u.general->buf;
2201 *len = t->term->u.general->len;
2203 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2208 ZOOM_API(const char *)
2209 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
2211 return ZOOM_options_get (scan->options, key);
2215 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
2218 ZOOM_options_set (scan->options, key, val);
2221 static Z_APDU *create_es_package (ZOOM_package p, int type)
2224 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
2225 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2227 *req->function = Z_ExtendedServicesRequest_create;
2229 str = ZOOM_options_get(p->options, "package-name");
2231 req->packageName = nmem_strdup (p->odr_out->mem, str);
2233 str = ZOOM_options_get(p->options, "user-id");
2235 req->userId = nmem_strdup (p->odr_out->mem, str);
2237 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
2240 str = ZOOM_options_get(p->options, "function");
2243 if (!strcmp (str, "create"))
2245 if (!strcmp (str, "delete"))
2247 if (!strcmp (str, "modify"))
2253 static const char *ill_array_lookup (void *clientData, const char *idx)
2255 ZOOM_package p = (ZOOM_package) clientData;
2256 return ZOOM_options_get (p->options, idx+4);
2259 static Z_External *encode_ill_request (ZOOM_package p)
2261 ODR out = p->odr_out;
2264 struct ill_get_ctl ctl;
2266 ctl.odr = p->odr_out;
2268 ctl.f = ill_array_lookup;
2270 req = ill_get_ILLRequest(&ctl, "ill", 0);
2272 if (!ill_Request (out, &req, 0, 0))
2274 int ill_request_size;
2275 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
2276 if (ill_request_buf)
2277 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
2283 int illRequest_size = 0;
2284 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
2286 oid.proto = PROTO_GENERAL;
2287 oid.oclass = CLASS_GENERAL;
2288 oid.value = VAL_ISO_ILL_1;
2290 r = (Z_External *) odr_malloc (out, sizeof(*r));
2291 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
2292 r->indirect_reference = 0;
2294 r->which = Z_External_single;
2296 r->u.single_ASN1_type = (Odr_oct *)
2297 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
2298 r->u.single_ASN1_type->buf = (unsigned char*)
2299 odr_malloc (out, illRequest_size);
2300 r->u.single_ASN1_type->len = illRequest_size;
2301 r->u.single_ASN1_type->size = illRequest_size;
2302 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
2307 static Z_ItemOrder *encode_item_order(ZOOM_package p)
2309 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
2312 req->which=Z_IOItemOrder_esRequest;
2313 req->u.esRequest = (Z_IORequest *)
2314 odr_malloc(p->odr_out,sizeof(Z_IORequest));
2316 /* to keep part ... */
2317 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
2318 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
2319 req->u.esRequest->toKeep->supplDescription = 0;
2320 req->u.esRequest->toKeep->contact = (Z_IOContact *)
2321 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
2323 str = ZOOM_options_get(p->options, "contact-name");
2324 req->u.esRequest->toKeep->contact->name = str ?
2325 nmem_strdup (p->odr_out->mem, str) : 0;
2327 str = ZOOM_options_get(p->options, "contact-phone");
2328 req->u.esRequest->toKeep->contact->phone = str ?
2329 nmem_strdup (p->odr_out->mem, str) : 0;
2331 str = ZOOM_options_get(p->options, "contact-email");
2332 req->u.esRequest->toKeep->contact->email = str ?
2333 nmem_strdup (p->odr_out->mem, str) : 0;
2335 req->u.esRequest->toKeep->addlBilling = 0;
2337 /* not to keep part ... */
2338 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
2339 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
2341 str = ZOOM_options_get(p->options, "itemorder-setname");
2346 req->u.esRequest->notToKeep->resultSetItem = 0;
2349 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
2350 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
2352 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
2353 nmem_strdup (p->odr_out->mem, str);
2354 req->u.esRequest->notToKeep->resultSetItem->item =
2355 (int *) odr_malloc(p->odr_out, sizeof(int));
2357 str = ZOOM_options_get(p->options, "itemorder-item");
2358 *req->u.esRequest->notToKeep->resultSetItem->item =
2359 (str ? atoi(str) : 1);
2361 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
2367 ZOOM_package_send (ZOOM_package p, const char *type)
2374 odr_reset (p->odr_out);
2377 if (!strcmp(type, "itemorder"))
2380 apdu = create_es_package (p, VAL_ITEMORDER);
2383 r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
2385 r->direct_reference =
2386 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
2389 r->which = Z_External_itemOrder;
2390 r->indirect_reference = 0;
2391 r->u.itemOrder = encode_item_order (p);
2393 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
2398 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
2402 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
2403 task->u.package = p;
2404 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
2405 p->buf_out = (char *) xmalloc (p->len_out);
2406 memcpy (p->buf_out, buf, p->len_out);
2411 while (ZOOM_event (1, &c))
2418 ZOOM_API(ZOOM_package)
2419 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
2421 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
2424 p->odr_out = odr_createmem (ODR_ENCODE);
2425 p->options = ZOOM_options_create_with_parent2 (options, c->options);
2433 ZOOM_package_destroy(ZOOM_package p)
2438 if (p->refcount == 0)
2440 odr_destroy (p->odr_out);
2443 ZOOM_options_destroy (p->options);
2448 ZOOM_API(const char *)
2449 ZOOM_package_option_get (ZOOM_package p, const char *key)
2451 return ZOOM_options_get (p->options, key);
2456 ZOOM_package_option_set (ZOOM_package p, const char *key,
2459 ZOOM_options_set (p->options, key, val);
2462 static int ZOOM_connection_exec_task (ZOOM_connection c)
2464 ZOOM_task task = c->tasks;
2465 zoom_ret ret = zoom_complete;
2469 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
2472 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
2473 task->which, task->running);
2474 if (c->error != ZOOM_ERROR_NONE)
2476 yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
2477 ZOOM_connection_remove_tasks (c);
2482 yaz_log (LOG_DEBUG, "task already running");
2486 ret = zoom_complete;
2487 if (c->cs || task->which == ZOOM_TASK_CONNECT)
2489 switch (task->which)
2491 case ZOOM_TASK_SEARCH:
2492 if (c->proto == PROTO_HTTP)
2493 ret = ZOOM_connection_srw_send_search(c);
2495 ret = ZOOM_connection_send_search(c);
2497 case ZOOM_TASK_RETRIEVE:
2498 if (c->proto == PROTO_HTTP)
2499 ret = ZOOM_connection_srw_send_search(c);
2501 ret = send_present (c);
2503 case ZOOM_TASK_CONNECT:
2504 ret = do_connect(c);
2506 case ZOOM_TASK_SCAN:
2509 case ZOOM_TASK_PACKAGE:
2510 ret = send_package(c);
2516 yaz_log (LOG_DEBUG, "remove tasks because no connection exist");
2517 ZOOM_connection_remove_tasks (c);
2519 if (ret == zoom_complete)
2521 yaz_log (LOG_DEBUG, "task removed (complete)");
2522 ZOOM_connection_remove_task (c);
2525 yaz_log (LOG_DEBUG, "task pending");
2529 static zoom_ret send_sort_present (ZOOM_connection c)
2531 zoom_ret r = send_sort (c);
2532 if (r == zoom_complete)
2533 r = send_present (c);
2537 static int es_response (ZOOM_connection c,
2538 Z_ExtendedServicesResponse *res)
2540 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
2542 if (res->diagnostics && res->num_diagnostics > 0)
2543 response_diag(c, res->diagnostics[0]);
2544 if (res->taskPackage &&
2545 res->taskPackage->which == Z_External_extendedService)
2547 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
2548 Odr_oct *id = taskPackage->targetReference;
2551 ZOOM_options_setl (c->tasks->u.package->options,
2552 "targetReference", (char*) id->buf, id->len);
2558 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
2560 Z_InitResponse *initrs;
2563 yaz_log (LOG_DEBUG, "recv APDU type=%d", apdu->which);
2566 case Z_APDU_initResponse:
2567 initrs = apdu->u.initResponse;
2568 ZOOM_connection_option_set(c, "serverImplementationId",
2569 initrs->implementationId ?
2570 initrs->implementationId : "");
2571 ZOOM_connection_option_set(c, "serverImplementationName",
2572 initrs->implementationName ?
2573 initrs->implementationName : "");
2574 ZOOM_connection_option_set(c, "serverImplementationVersion",
2575 initrs->implementationVersion ?
2576 initrs->implementationVersion : "");
2577 /* Set the three old options too, for old applications */
2578 ZOOM_connection_option_set(c, "targetImplementationId",
2579 initrs->implementationId ?
2580 initrs->implementationId : "");
2581 ZOOM_connection_option_set(c, "targetImplementationName",
2582 initrs->implementationName ?
2583 initrs->implementationName : "");
2584 ZOOM_connection_option_set(c, "targetImplementationVersion",
2585 initrs->implementationVersion ?
2586 initrs->implementationVersion : "");
2587 if (!*initrs->result)
2589 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0);
2594 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
2596 xfree (c->cookie_in);
2599 c->cookie_in = xstrdup(cookie);
2600 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2601 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2602 c->support_named_resultsets = 1;
2605 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2606 ZOOM_connection_remove_task (c);
2608 ZOOM_connection_exec_task (c);
2610 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2612 NMEM tmpmem = nmem_create();
2613 Z_CharSetandLanguageNegotiation *p =
2614 yaz_get_charneg_record(initrs->otherInfo);
2618 char *charset=NULL, *lang=NULL;
2621 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
2622 yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
2623 "language %s, select %d",
2624 charset ? charset : "none", lang ? lang : "none", sel);
2626 ZOOM_connection_option_set (c, "negotiation-charset",
2629 ZOOM_connection_option_set (c, "negotiation-lang",
2631 nmem_destroy(tmpmem);
2635 case Z_APDU_searchResponse:
2636 handle_search_response (c, apdu->u.searchResponse);
2637 if (send_sort_present (c) == zoom_complete)
2638 ZOOM_connection_remove_task (c);
2640 case Z_APDU_presentResponse:
2641 handle_present_response (c, apdu->u.presentResponse);
2642 if (send_present (c) == zoom_complete)
2643 ZOOM_connection_remove_task (c);
2645 case Z_APDU_sortResponse:
2646 sort_response (c, apdu->u.sortResponse);
2647 if (send_present (c) == zoom_complete)
2648 ZOOM_connection_remove_task (c);
2650 case Z_APDU_scanResponse:
2651 scan_response (c, apdu->u.scanResponse);
2652 ZOOM_connection_remove_task (c);
2654 case Z_APDU_extendedServicesResponse:
2655 es_response (c, apdu->u.extendedServicesResponse);
2656 ZOOM_connection_remove_task (c);
2659 if (c->reconnect_ok)
2662 c->tasks->running = 0;
2663 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2667 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2672 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2678 static void handle_srw_response(ZOOM_connection c,
2679 Z_SRW_searchRetrieveResponse *res)
2681 ZOOM_resultset resultset = 0;
2689 if (c->tasks->which == ZOOM_TASK_SEARCH)
2690 resultset = c->tasks->u.search.resultset;
2691 else if (c->tasks->which == ZOOM_TASK_RETRIEVE)
2692 resultset = c->tasks->u.retrieve.resultset;
2696 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2697 ZOOM_connection_put_event(c, event);
2699 resultset->size = 0;
2701 yaz_log(LOG_DEBUG, "got SRW response OK");
2703 if (res->numberOfRecords)
2704 resultset->size = *res->numberOfRecords;
2706 for (i = 0; i<res->num_records; i++)
2710 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
2711 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
2713 if (res->records[i].recordPosition &&
2714 *res->records[i].recordPosition > 0)
2715 pos = *res->records[i].recordPosition - 1;
2717 pos = resultset->start + i;
2719 npr->databaseName = 0;
2720 npr->which = Z_NamePlusRecord_databaseRecord;
2721 npr->u.databaseRecord = (Z_External *)
2722 odr_malloc(c->odr_in, sizeof(Z_External));
2723 npr->u.databaseRecord->descriptor = 0;
2724 npr->u.databaseRecord->direct_reference =
2725 yaz_oidval_to_z3950oid(c->odr_in, CLASS_RECSYN, VAL_TEXT_XML);
2726 npr->u.databaseRecord->which = Z_External_octet;
2727 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
2728 odr_malloc(c->odr_in, sizeof(Odr_oct));
2729 npr->u.databaseRecord->u.octet_aligned->buf =
2730 res->records[i].recordData_buf;
2731 npr->u.databaseRecord->u.octet_aligned->len =
2732 npr->u.databaseRecord->u.octet_aligned->size =
2733 res->records[i].recordData_len;
2734 record_cache_add (resultset, npr, pos);
2736 if (res->num_diagnostics > 0)
2738 set_dset_error(c, *res->diagnostics[0].code, "SRW",
2739 res->diagnostics[0].details, 0);
2741 nmem = odr_extract_mem(c->odr_in);
2742 nmem_transfer(resultset->odr->mem, nmem);
2748 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
2751 const char *content_type = z_HTTP_header_lookup(hres->headers,
2753 const char *connection_head = z_HTTP_header_lookup(hres->headers,
2756 yaz_log (LOG_DEBUG, "handle_http");
2758 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
2760 Z_SOAP *soap_package = 0;
2761 ODR o = odr_createmem(ODR_DECODE);
2762 Z_SOAP_Handler soap_handlers[2] = {
2763 {"http://www.loc.gov/zing/srw/v1.0/", 0,
2764 (Z_SOAP_fun) yaz_srw_codec},
2767 ret = z_soap_codec(o, &soap_package,
2768 &hres->content_buf, &hres->content_len,
2770 if (!ret && soap_package->which == Z_SOAP_generic &&
2771 soap_package->u.generic->no == 0)
2773 Z_SRW_PDU *sr = soap_package->u.generic->p;
2774 if (sr->which == Z_SRW_searchRetrieve_response)
2775 handle_srw_response(c, sr->u.response);
2779 else if (!ret && (soap_package->which == Z_SOAP_fault
2780 || soap_package->which == Z_SOAP_error))
2782 set_HTTP_error(c, hres->code,
2783 soap_package->u.fault->fault_code,
2784 soap_package->u.fault->fault_string);
2792 if (hres->code != 200)
2793 set_HTTP_error(c, hres->code, 0, 0);
2795 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2798 ZOOM_connection_remove_task(c);
2799 if (!strcmp(hres->version, "1.0"))
2801 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
2802 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
2807 /* HTTP 1.1: only if no close we stay alive .. */
2808 if (connection_head && !strcmp(connection_head, "close"))
2814 static int do_read (ZOOM_connection c)
2819 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2820 ZOOM_connection_put_event (c, event);
2823 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2824 more = cs_more(c->cs);
2825 yaz_log (LOG_DEBUG, "do_read len=%d more=%d", r, more);
2830 if (c->reconnect_ok)
2833 c->reconnect_ok = 0;
2834 yaz_log (LOG_DEBUG, "reconnect read");
2835 c->tasks->running = 0;
2836 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2840 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2849 odr_reset (c->odr_in);
2850 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2851 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2852 ZOOM_connection_put_event (c, event);
2854 if (!z_GDU (c->odr_in, &gdu, 0, 0))
2856 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2859 else if (gdu->which == Z_GDU_Z3950)
2860 handle_apdu (c, gdu->u.z3950);
2861 else if (gdu->which == Z_GDU_HTTP_Response)
2864 handle_http (c, gdu->u.HTTP_Response);
2866 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2870 c->reconnect_ok = 0;
2875 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2880 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2881 ZOOM_connection_put_event (c, event);
2883 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2884 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2886 if (c->reconnect_ok)
2889 c->reconnect_ok = 0;
2890 yaz_log (LOG_DEBUG, "reconnect write");
2891 c->tasks->running = 0;
2892 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2893 return zoom_pending;
2895 if (c->state == STATE_CONNECTING)
2896 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
2898 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2900 return zoom_complete;
2904 c->mask = ZOOM_SELECT_EXCEPT;
2905 if (c->cs->io_pending & CS_WANT_WRITE)
2906 c->mask += ZOOM_SELECT_WRITE;
2907 if (c->cs->io_pending & CS_WANT_READ)
2908 c->mask += ZOOM_SELECT_READ;
2909 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2913 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2914 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2916 return zoom_pending;
2919 static zoom_ret do_write(ZOOM_connection c)
2921 return do_write_ex (c, c->buf_out, c->len_out);
2925 ZOOM_API(const char *)
2926 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2928 return ZOOM_options_get (c->options, key);
2932 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2935 ZOOM_options_set (c->options, key, val);
2938 ZOOM_API(const char *)
2939 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2941 return ZOOM_options_get (r->options, key);
2945 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2948 ZOOM_options_set (r->options, key, val);
2953 ZOOM_connection_errcode (ZOOM_connection c)
2955 return ZOOM_connection_error (c, 0, 0);
2958 ZOOM_API(const char *)
2959 ZOOM_connection_errmsg (ZOOM_connection c)
2962 ZOOM_connection_error (c, &msg, 0);
2966 ZOOM_API(const char *)
2967 ZOOM_connection_addinfo (ZOOM_connection c)
2969 const char *addinfo;
2970 ZOOM_connection_error (c, 0, &addinfo);
2974 ZOOM_API(const char *)
2975 ZOOM_diag_str (int error)
2979 case ZOOM_ERROR_NONE:
2981 case ZOOM_ERROR_CONNECT:
2982 return "Connect failed";
2983 case ZOOM_ERROR_MEMORY:
2984 return "Out of memory";
2985 case ZOOM_ERROR_ENCODE:
2986 return "Encoding failed";
2987 case ZOOM_ERROR_DECODE:
2988 return "Decoding failed";
2989 case ZOOM_ERROR_CONNECTION_LOST:
2990 return "Connection lost";
2991 case ZOOM_ERROR_INIT:
2992 return "Init rejected";
2993 case ZOOM_ERROR_INTERNAL:
2994 return "Internal failure";
2995 case ZOOM_ERROR_TIMEOUT:
2997 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
2998 return "Unsupported protocol";
2999 case ZOOM_ERROR_UNSUPPORTED_QUERY:
3000 return "Unsupported query type";
3002 return diagbib1_str (error);
3007 ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
3008 const char **addinfo, const char **diagset)
3010 int error = c->error;
3013 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
3014 *cp = ZOOM_diag_str(error);
3015 else if (!strcmp(c->diagset, "HTTP"))
3016 *cp = z_HTTP_errmsg(c->error);
3017 else if (!strcmp(c->diagset, "Bib-1"))
3018 *cp = ZOOM_diag_str(error);
3019 else if (!strcmp(c->diagset, "SRW"))
3020 *cp = yaz_diag_srw_str(c->error);
3022 *cp = "Unknown error and diagnostic set";
3025 *addinfo = c->addinfo ? c->addinfo : "";
3027 *diagset = c->diagset ? c->diagset : "";
3032 ZOOM_connection_error (ZOOM_connection c, const char **cp,
3033 const char **addinfo)
3035 return ZOOM_connection_error_x(c, cp, addinfo, 0);
3038 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
3040 ZOOM_Event event = 0;
3041 int r = cs_look(c->cs);
3042 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
3047 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
3048 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
3050 ZOOM_connection_put_event (c, event);
3052 else if (r == CS_CONNECT)
3055 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
3057 ret = cs_rcvconnect (c->cs);
3058 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
3061 c->mask = ZOOM_SELECT_EXCEPT;
3062 if (c->cs->io_pending & CS_WANT_WRITE)
3063 c->mask += ZOOM_SELECT_WRITE;
3064 if (c->cs->io_pending & CS_WANT_READ)
3065 c->mask += ZOOM_SELECT_READ;
3066 ZOOM_connection_put_event (c, event);
3070 ZOOM_connection_put_event (c, event);
3071 if (c->proto == PROTO_Z3950)
3072 ZOOM_connection_send_init(c);
3075 /* no init request for SRW .. */
3076 assert (c->tasks->which == ZOOM_TASK_CONNECT);
3077 ZOOM_connection_remove_task (c);
3079 ZOOM_connection_exec_task (c);
3081 c->state = STATE_ESTABLISHED;
3085 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
3087 ZOOM_connection_put_event (c, event);
3092 if (mask & ZOOM_SELECT_READ)
3094 if (c->cs && (mask & ZOOM_SELECT_WRITE))
3101 ZOOM_connection_last_event(ZOOM_connection cs)
3104 return ZOOM_EVENT_NONE;
3105 return cs->last_event;
3109 ZOOM_event (int no, ZOOM_connection *cs)
3113 struct pollfd pollfds[1024];
3114 ZOOM_connection poll_cs[1024];
3117 fd_set input, output, except;
3122 for (i = 0; i<no; i++)
3124 ZOOM_connection c = cs[i];
3126 if (c && (event = ZOOM_connection_get_event(c)))
3128 ZOOM_Event_destroy (event);
3132 for (i = 0; i<no; i++)
3134 ZOOM_connection c = cs[i];
3136 if (c && ZOOM_connection_exec_task (c))
3138 if ((event = ZOOM_connection_get_event(c)))
3140 ZOOM_Event_destroy (event);
3153 for (i = 0; i<no; i++)
3155 ZOOM_connection c = cs[i];
3161 fd = z3950_connection_socket(c);
3162 mask = z3950_connection_mask(c);
3169 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
3170 if (this_timeout != -1 && this_timeout < timeout)
3171 timeout = this_timeout;
3175 short poll_events = 0;
3177 if (mask & ZOOM_SELECT_READ)
3178 poll_events += POLLIN;
3179 if (mask & ZOOM_SELECT_WRITE)
3180 poll_events += POLLOUT;
3181 if (mask & ZOOM_SELECT_EXCEPT)
3182 poll_events += POLLERR;
3183 pollfds[nfds].fd = fd;
3184 pollfds[nfds].events = poll_events;
3185 pollfds[nfds].revents = 0;
3190 if (mask & ZOOM_SELECT_READ)
3192 FD_SET (fd, &input);
3195 if (mask & ZOOM_SELECT_WRITE)
3197 FD_SET (fd, &output);
3200 if (mask & ZOOM_SELECT_EXCEPT)
3202 FD_SET (fd, &except);
3207 if (timeout >= 5000)
3214 r = poll (pollfds, nfds, timeout * 1000);
3215 for (i = 0; i<nfds; i++)
3217 ZOOM_connection c = poll_cs[i];
3221 if (pollfds[i].revents & POLLIN)
3222 mask += ZOOM_SELECT_READ;
3223 if (pollfds[i].revents & POLLOUT)
3224 mask += ZOOM_SELECT_WRITE;
3225 if (pollfds[i].revents & POLLERR)
3226 mask += ZOOM_SELECT_EXCEPT;
3228 ZOOM_connection_do_io(c, mask);
3230 else if (r == 0 && c->mask)
3232 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
3233 /* timeout and this connection was waiting */
3234 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
3236 ZOOM_connection_put_event(c, event);
3240 tv.tv_sec = timeout;
3242 yaz_log (LOG_DEBUG, "select start");
3243 r = select (max_fd+1, &input, &output, &except, &tv);
3244 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
3245 for (i = 0; i<no; i++)
3247 ZOOM_connection c = cs[i];
3252 fd = z3950_connection_socket(c);
3256 /* no timeout and real socket */
3257 if (FD_ISSET(fd, &input))
3258 mask += ZOOM_SELECT_READ;
3259 if (FD_ISSET(fd, &output))
3260 mask += ZOOM_SELECT_WRITE;
3261 if (FD_ISSET(fd, &except))
3262 mask += ZOOM_SELECT_EXCEPT;
3264 ZOOM_connection_do_io(c, mask);
3266 if (r == 0 && c->mask)
3268 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
3269 /* timeout and this connection was waiting */
3270 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
3272 yaz_log (LOG_DEBUG, "timeout");
3273 ZOOM_connection_put_event(c, event);
3277 for (i = 0; i<no; i++)
3279 ZOOM_connection c = cs[i];
3281 if (c && (event = ZOOM_connection_get_event(c)))
3283 ZOOM_Event_destroy (event);