2 * Copyright (c) 2000-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.6 2003-11-25 09:50:35 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);
548 /* If "presentChunk" is defined use that; otherwise "step" */
549 const char *cp = ZOOM_options_get (r->options, "presentChunk");
550 r->step = ZOOM_options_get_int(r->options,
551 (cp != 0 ? "presentChunk": "step"), 0);
553 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
554 cp = ZOOM_options_get (r->options, "setname");
556 r->setname = xstrdup(cp);
557 cp = ZOOM_options_get (r->options, "schema");
559 r->schema = xstrdup(cp);
563 r->next = c->resultsets;
566 if (c->host_port && c->proto == PROTO_HTTP)
570 yaz_log(LOG_DEBUG, "NO COMSTACK");
571 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
575 yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
580 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
581 task->u.search.resultset = r;
582 ZOOM_resultset_addref (r);
588 while (ZOOM_event (1, &c))
595 ZOOM_resultset_destroy(ZOOM_resultset r)
600 yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
602 if (r->refcount == 0)
604 ZOOM_record_cache rc;
606 for (rc = r->record_cache; rc; rc = rc->next)
608 if (rc->rec.wrbuf_marc)
609 wrbuf_free (rc->rec.wrbuf_marc, 1);
610 if (rc->rec.wrbuf_iconv)
611 wrbuf_free (rc->rec.wrbuf_iconv, 1);
612 if (rc->rec.wrbuf_opac)
613 wrbuf_free (rc->rec.wrbuf_opac, 1);
617 /* remove ourselves from the resultsets in connection */
618 ZOOM_resultset *rp = &r->connection->resultsets;
621 assert (*rp); /* we must be in this list!! */
623 { /* OK, we're here - take us out of it */
630 ZOOM_query_destroy (r->query);
631 ZOOM_options_destroy (r->options);
632 odr_destroy (r->odr);
640 ZOOM_resultset_size (ZOOM_resultset r)
645 static void do_close (ZOOM_connection c)
651 c->state = STATE_IDLE;
654 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
655 int force_sync, int start, int count)
667 if (c->host_port && c->proto == PROTO_HTTP)
671 yaz_log(LOG_DEBUG, "NO COMSTACK");
672 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
676 yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
680 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
681 task->u.retrieve.resultset = r;
682 task->u.retrieve.start = start;
683 task->u.retrieve.count = count;
685 cp = ZOOM_options_get (r->options, "schema");
688 if (!r->schema || strcmp(r->schema, cp))
691 r->schema = xstrdup(cp);
695 ZOOM_resultset_addref (r);
697 if (!r->connection->async || force_sync)
698 while (r->connection && ZOOM_event (1, &r->connection))
703 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
704 size_t start, size_t count)
706 int force_present = 0;
712 ZOOM_resultset_retrieve (r, force_present, start, count);
716 for (i = 0; i< count; i++)
717 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
721 static zoom_ret do_connect (ZOOM_connection c)
724 const char *effective_host;
727 effective_host = c->proxy;
729 effective_host = c->host_port;
731 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
734 c->cs = cs_create_host (effective_host, 0, &add);
736 if (c->cs && c->cs->protocol == PROTO_HTTP)
739 const char *path = 0;
741 c->proto = PROTO_HTTP;
742 cs_get_host_args(c->host_port, &path);
744 c->path = xmalloc(strlen(path)+2);
746 strcpy (c->path+1, path);
748 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
750 return zoom_complete;
755 int ret = cs_connect (c->cs, add);
758 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
759 ZOOM_connection_put_event(c, event);
760 if (c->proto == PROTO_Z3950)
761 ZOOM_connection_send_init(c);
764 /* no init request for SRW .. */
765 assert (c->tasks->which == ZOOM_TASK_CONNECT);
766 ZOOM_connection_remove_task (c);
768 ZOOM_connection_exec_task (c);
770 c->state = STATE_ESTABLISHED;
775 c->state = STATE_CONNECTING;
776 c->mask = ZOOM_SELECT_EXCEPT;
777 if (c->cs->io_pending & CS_WANT_WRITE)
778 c->mask += ZOOM_SELECT_WRITE;
779 if (c->cs->io_pending & CS_WANT_READ)
780 c->mask += ZOOM_SELECT_READ;
784 c->state = STATE_IDLE;
785 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, effective_host);
786 return zoom_complete;
789 int z3950_connection_socket(ZOOM_connection c)
792 return cs_fileno(c->cs);
796 int z3950_connection_mask(ZOOM_connection c)
803 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
806 for (i = 0; i<200; i++)
809 Z_OtherInformation **oi;
815 sprintf (buf, "otherInfo%d", i);
816 val = ZOOM_options_get (c->options, buf);
819 cp = strchr (val, ':');
823 if (len >= sizeof(buf))
825 memcpy (buf, val, len);
827 oidval = oid_getvalbyname (buf);
828 if (oidval == VAL_NONE)
832 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
836 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
841 Z_OtherInformation **oi;
843 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
847 Z_OtherInformation **oi;
849 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
851 otherInfo_attach (c, a, out);
852 if (!z_APDU(out, &a, 0, 0))
854 FILE *outf = fopen("/tmp/apdu.txt", "a");
857 ODR odr_pr = odr_createmem(ODR_PRINT);
858 fprintf (outf, "a=%p\n", a);
859 odr_setprint(odr_pr, outf);
860 z_APDU(odr_pr, &a, 0, 0);
863 yaz_log (LOG_DEBUG, "encoding failed");
864 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
872 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
876 if (encode_APDU(c, a, c->odr_out))
877 return zoom_complete;
878 yaz_log(LOG_DEBUG, "send APDU type=%d", a->which);
879 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
880 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
881 ZOOM_connection_put_event (c, event);
882 odr_reset(c->odr_out);
886 /* returns 1 if PDU was sent OK (still pending )
887 0 if PDU was not sent OK (nothing to wait for)
890 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
892 const char *impid, *impname, *impver;
893 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
894 Z_InitRequest *ireq = apdu->u.initRequest;
895 Z_IdAuthentication *auth = (Z_IdAuthentication *)
896 odr_malloc(c->odr_out, sizeof(*auth));
897 const char *auth_groupId = ZOOM_options_get (c->options, "group");
898 const char *auth_userId = ZOOM_options_get (c->options, "user");
899 const char *auth_password = ZOOM_options_get (c->options, "pass");
901 ODR_MASK_SET(ireq->options, Z_Options_search);
902 ODR_MASK_SET(ireq->options, Z_Options_present);
903 ODR_MASK_SET(ireq->options, Z_Options_scan);
904 ODR_MASK_SET(ireq->options, Z_Options_sort);
905 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
906 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
908 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
909 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
910 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
912 impid = ZOOM_options_get (c->options, "implementationId");
913 ireq->implementationId =
914 (char *) odr_malloc (c->odr_out, 15 + (impid ? strlen(impid) : 0));
915 strcpy (ireq->implementationId, "");
918 strcat (ireq->implementationId, impid);
919 strcat (ireq->implementationId, "/");
921 strcat (ireq->implementationId, "81"); /* Index's implementor ID */
923 impname = ZOOM_options_get (c->options, "implementationName");
924 ireq->implementationName =
925 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
926 strcpy (ireq->implementationName, "");
929 strcat (ireq->implementationName, impname);
930 strcat (ireq->implementationName, "/");
932 strcat (ireq->implementationName, "ZOOM-C/YAZ");
934 impver = ZOOM_options_get (c->options, "implementationVersion");
935 ireq->implementationVersion =
936 (char *) odr_malloc (c->odr_out, strlen("$Revision: 1.6 $") + 2 +
937 (impver ? strlen(impver) : 0));
938 strcpy (ireq->implementationVersion, "");
941 strcat (ireq->implementationVersion, impver);
942 strcat (ireq->implementationVersion, "/");
944 strcat (ireq->implementationVersion, "$Revision: 1.6 $");
946 *ireq->maximumRecordSize =
947 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
948 *ireq->preferredMessageSize =
949 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
951 if (auth_groupId || auth_password)
953 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
956 if (auth_groupId && *auth_groupId)
958 pass->groupId = (char *)
959 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
960 strcpy(pass->groupId, auth_groupId);
964 if (auth_userId && *auth_userId)
966 pass->userId = (char *)
967 odr_malloc(c->odr_out, strlen(auth_userId)+1);
968 strcpy(pass->userId, auth_userId);
972 if (auth_password && *auth_password)
974 pass->password = (char *)
975 odr_malloc(c->odr_out, strlen(auth_password)+1);
976 strcpy(pass->password, auth_password);
981 auth->which = Z_IdAuthentication_idPass;
982 auth->u.idPass = pass;
983 ireq->idAuthentication = auth;
986 else if (auth_userId)
988 auth->which = Z_IdAuthentication_open;
989 auth->u.open = (char *)
990 odr_malloc(c->odr_out, strlen(auth_userId)+1);
991 strcpy(auth->u.open, auth_userId);
992 ireq->idAuthentication = auth;
995 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
996 VAL_PROXY, 1, c->host_port);
997 if (c->charset||c->lang)
999 Z_OtherInformation **oi;
1000 Z_OtherInformationUnit *oi_unit;
1002 yaz_oi_APDU(apdu, &oi);
1004 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1006 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1008 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1009 oi_unit->information.externallyDefinedInfo =
1010 yaz_set_proposal_charneg
1012 (const char **)&c->charset, (c->charset) ? 1:0,
1013 (const char **)&c->lang, (c->lang) ? 1:0, 1);
1017 return send_APDU (c, apdu);
1021 static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr)
1024 Z_SOAP_Handler h[2] = {
1025 {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
1028 ODR o = odr_createmem(ODR_ENCODE);
1030 Z_SOAP *p = odr_malloc(o, sizeof(*p));
1034 gdu = z_get_HTTP_Request(c->odr_out);
1035 gdu->u.HTTP_Request->path = c->path;
1039 const char *cp0 = strstr(c->host_port, "://");
1040 const char *cp1 = 0;
1046 cp1 = strchr(cp0, '/');
1048 cp1 = cp0+strlen(cp0);
1052 char *h = odr_malloc(c->odr_out, cp1 - cp0 + 1);
1053 memcpy (h, cp0, cp1 - cp0);
1055 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1060 strcpy(ctype, "text/xml");
1061 if (c->charset && strlen(c->charset) < 20)
1063 strcat(ctype, "; charset=");
1064 strcat(ctype, c->charset);
1066 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1067 "Content-Type", ctype);
1068 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1069 "SOAPAction", "\"\"");
1070 p->which = Z_SOAP_generic;
1071 p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
1072 p->u.generic->no = 0;
1073 p->u.generic->ns = 0;
1074 p->u.generic->p = sr;
1075 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1077 ret = z_soap_codec_enc(o, &p,
1078 &gdu->u.HTTP_Request->content_buf,
1079 &gdu->u.HTTP_Request->content_len, h,
1082 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1083 return zoom_complete;
1084 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1088 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1089 ZOOM_connection_put_event (c, event);
1090 odr_reset(c->odr_out);
1091 return do_write (c);
1096 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1099 ZOOM_resultset resultset = 0;
1101 const char *recordPacking = 0;
1103 if (c->error) /* don't continue on error */
1104 return zoom_complete;
1106 if (c->tasks->which == ZOOM_TASK_SEARCH)
1108 resultset = c->tasks->u.search.resultset;
1109 resultset->setname = xstrdup ("default");
1110 ZOOM_options_set (resultset->options, "setname", resultset->setname);
1112 else if(c->tasks->which == ZOOM_TASK_RETRIEVE)
1114 resultset = c->tasks->u.retrieve.resultset;
1116 resultset->start = c->tasks->u.retrieve.start;
1117 resultset->count = c->tasks->u.retrieve.count;
1119 if (resultset->start >= resultset->size)
1120 return zoom_complete;
1121 if (resultset->start + resultset->count > resultset->size)
1122 resultset->count = resultset->size - resultset->start;
1124 for (i = 0; i<resultset->count; i++)
1127 record_cache_lookup (resultset, i + resultset->start);
1131 if (i == resultset->count)
1132 return zoom_complete;
1134 assert(resultset->query);
1136 sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
1138 if (resultset->query->z_query->which == Z_Query_type_104
1139 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1142 sr->u.request->query_type = Z_SRW_query_type_cql;
1143 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1145 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1146 resultset->query->z_query->u.type_1)
1148 sr->u.request->query_type = Z_SRW_query_type_pqf;
1149 sr->u.request->query.pqf = resultset->query->query_string;
1153 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1154 return zoom_complete;
1156 sr->u.request->startRecord = odr_intdup (c->odr_out, resultset->start + 1);
1157 sr->u.request->maximumRecords = odr_intdup (
1158 c->odr_out, resultset->step>0 ? resultset->step : resultset->count);
1159 sr->u.request->recordSchema = resultset->schema;
1161 recordPacking = ZOOM_resultset_option_get (resultset, "recordPacking");
1164 sr->u.request->recordPacking = odr_strdup(c->odr_out, recordPacking);
1166 return send_srw(c, sr);
1169 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1171 return zoom_complete;
1175 static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
1178 int lslb, ssub, mspn;
1180 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1181 Z_SearchRequest *search_req = apdu->u.searchRequest;
1182 const char *elementSetName;
1183 const char *smallSetElementSetName;
1184 const char *mediumSetElementSetName;
1187 assert (c->tasks->which == ZOOM_TASK_SEARCH);
1189 r = c->tasks->u.search.resultset;
1192 ZOOM_options_get (r->options, "elementSetName");
1193 smallSetElementSetName =
1194 ZOOM_options_get (r->options, "smallSetElementSetName");
1195 mediumSetElementSetName =
1196 ZOOM_options_get (r->options, "mediumSetElementSetName");
1198 if (!smallSetElementSetName)
1199 smallSetElementSetName = elementSetName;
1201 if (!mediumSetElementSetName)
1202 mediumSetElementSetName = elementSetName;
1207 /* prepare query for the search request */
1208 search_req->query = r->query->z_query;
1210 search_req->databaseNames =
1211 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
1213 /* get syntax (no need to provide unless piggyback is in effect) */
1214 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
1216 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
1217 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
1218 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
1219 if (lslb != -1 && ssub != -1 && mspn != -1)
1221 /* So're a Z39.50 expert? Let's hope you don't do sort */
1222 *search_req->largeSetLowerBound = lslb;
1223 *search_req->smallSetUpperBound = ssub;
1224 *search_req->mediumSetPresentNumber = mspn;
1226 else if (r->start == 0 && r->count > 0
1227 && r->piggyback && !r->r_sort_spec && !r->schema)
1229 /* Regular piggyback - do it unless we're going to do sort */
1230 *search_req->largeSetLowerBound = 2000000000;
1231 *search_req->smallSetUpperBound = 1;
1232 *search_req->mediumSetPresentNumber = r->step>0 ? r->step : r->count;
1236 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1237 smallSetElementSetName = 0;
1238 mediumSetElementSetName = 0;
1241 if (smallSetElementSetName && *smallSetElementSetName)
1243 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1244 odr_malloc (c->odr_out, sizeof(*esn));
1246 esn->which = Z_ElementSetNames_generic;
1247 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
1248 search_req->smallSetElementSetNames = esn;
1250 if (mediumSetElementSetName && *mediumSetElementSetName)
1252 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1253 odr_malloc (c->odr_out, sizeof(*esn));
1255 esn->which = Z_ElementSetNames_generic;
1256 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
1257 search_req->mediumSetElementSetNames = esn;
1260 search_req->preferredRecordSyntax =
1261 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1265 if (c->support_named_resultsets)
1269 /* find the lowest unused ordinal so that we re-use
1270 result sets on the server. */
1271 for (ord = 1; ; ord++)
1274 sprintf (setname, "%d", ord);
1275 for (rp = c->resultsets; rp; rp = rp->next)
1276 if (rp->setname && !strcmp (rp->setname, setname))
1281 r->setname = xstrdup (setname);
1282 yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
1285 r->setname = xstrdup ("default");
1286 ZOOM_options_set (r->options, "setname", r->setname);
1288 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1289 /* send search request */
1290 return send_APDU (c, apdu);
1293 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
1296 Z_DefaultDiagFormat *r;
1301 if (p->which != Z_DiagRec_defaultFormat)
1303 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1306 r = p->u.defaultFormat;
1309 case Z_DefaultDiagFormat_v2Addinfo:
1310 addinfo = r->u.v2Addinfo;
1312 case Z_DefaultDiagFormat_v3Addinfo:
1313 addinfo = r->u.v3Addinfo;
1316 set_dset_error(c, *r->condition,
1317 yaz_z3950oid_to_str(r->diagnosticSetId, &oclass),
1321 ZOOM_API(ZOOM_record)
1322 ZOOM_record_clone (ZOOM_record srec)
1329 odr_enc = odr_createmem(ODR_ENCODE);
1330 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
1332 buf = odr_getbuf (odr_enc, &size, 0);
1334 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
1335 nrec->odr = odr_createmem(ODR_DECODE);
1336 nrec->wrbuf_marc = 0;
1337 nrec->wrbuf_iconv = 0;
1338 nrec->wrbuf_opac = 0;
1339 odr_setbuf (nrec->odr, buf, size, 0);
1340 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
1342 odr_destroy (odr_enc);
1346 ZOOM_API(ZOOM_record)
1347 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1349 return record_cache_lookup (s, pos);
1352 ZOOM_API(ZOOM_record)
1353 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1355 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1359 ZOOM_resultset_retrieve (r, 1, pos, 1);
1360 rec = ZOOM_resultset_record_immediate (r, pos);
1366 ZOOM_record_destroy (ZOOM_record rec)
1370 if (rec->wrbuf_marc)
1371 wrbuf_free (rec->wrbuf_marc, 1);
1372 if (rec->wrbuf_iconv)
1373 wrbuf_free (rec->wrbuf_iconv, 1);
1374 if (rec->wrbuf_opac)
1375 wrbuf_free (rec->wrbuf_opac, 1);
1376 odr_destroy (rec->odr);
1380 static const char *record_iconv_return(ZOOM_record rec, int *len,
1381 const char *buf, int sz,
1382 const char *record_charset)
1389 strcpy(to, "UTF-8");
1390 if (record_charset && *record_charset)
1392 /* Use "from,to" or just "from" */
1393 const char *cp =strchr(record_charset, ',');
1394 int clen = strlen(record_charset);
1397 strncpy( to, cp+1, sizeof(to)-1);
1398 to[sizeof(to)-1] = '\0';
1399 clen = cp - record_charset;
1401 if (clen > sizeof(from)-1)
1402 clen = sizeof(from)-1;
1405 strncpy(from, record_charset, clen);
1409 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1412 size_t inbytesleft = sz;
1413 const char *inp = buf;
1415 if (!rec->wrbuf_iconv)
1416 rec->wrbuf_iconv = wrbuf_alloc();
1418 wrbuf_rewind(rec->wrbuf_iconv);
1422 size_t outbytesleft = sizeof(outbuf);
1423 char *outp = outbuf;
1424 size_t r = yaz_iconv (cd, (char**) &inp,
1426 &outp, &outbytesleft);
1427 if (r == (size_t) (-1))
1429 int e = yaz_iconv_error(cd);
1430 if (e != YAZ_ICONV_E2BIG)
1433 wrbuf_write(rec->wrbuf_iconv, outbuf, outp - outbuf);
1435 wrbuf_puts(rec->wrbuf_iconv, "");
1436 buf = wrbuf_buf(rec->wrbuf_iconv);
1437 sz = wrbuf_len(rec->wrbuf_iconv);
1438 yaz_iconv_close(cd);
1445 ZOOM_API(const char *)
1446 ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
1452 Z_NamePlusRecord *npr;
1455 *len = 0; /* default return */
1464 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
1466 if (cp[i] == ';' || cp[i] == ' ')
1472 if (type_spec[i] == ';')
1475 while (type_spec[i] == ' ')
1477 if (!strncmp(type_spec+i, "charset=", 8))
1480 for (i = 0; cp[i] && i < sizeof(charset)-1; i++)
1482 if (cp[i] == ';' || cp[i] == ' ')
1490 if (!strcmp (type, "database"))
1493 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1494 return npr->databaseName;
1496 else if (!strcmp (type, "syntax"))
1498 const char *desc = 0;
1499 if (npr->which == Z_NamePlusRecord_databaseRecord)
1501 Z_External *r = (Z_External *) npr->u.databaseRecord;
1502 oident *ent = oid_getentbyoid(r->direct_reference);
1509 *len = strlen(desc);
1512 else if (!strcmp (type, "render") &&
1513 npr->which == Z_NamePlusRecord_databaseRecord)
1515 Z_External *r = (Z_External *) npr->u.databaseRecord;
1516 oident *ent = oid_getentbyoid(r->direct_reference);
1518 /* render bibliographic record .. */
1519 if (r->which == Z_External_OPAC)
1521 r = r->u.opac->bibliographicRecord;
1524 ent = oid_getentbyoid(r->direct_reference);
1526 if (r->which == Z_External_sutrs)
1527 return record_iconv_return(rec, len,
1528 r->u.sutrs->buf, r->u.sutrs->len,
1530 else if (r->which == Z_External_octet)
1540 case VAL_APPLICATION_XML:
1543 if (!rec->wrbuf_marc)
1544 rec->wrbuf_marc = wrbuf_alloc();
1546 mt = yaz_marc_create();
1547 wrbuf_rewind (rec->wrbuf_marc);
1548 if (yaz_marc_decode_wrbuf (
1549 mt, (const char *) r->u.octet_aligned->buf,
1550 r->u.octet_aligned->len,
1551 rec->wrbuf_marc) > 0)
1553 yaz_marc_destroy(mt);
1554 return record_iconv_return(rec, len,
1555 wrbuf_buf(rec->wrbuf_marc),
1556 wrbuf_len(rec->wrbuf_marc),
1559 yaz_marc_destroy(mt);
1561 return record_iconv_return(rec, len,
1562 (const char *) r->u.octet_aligned->buf,
1563 r->u.octet_aligned->len,
1566 else if (r->which == Z_External_grs1)
1568 if (!rec->wrbuf_marc)
1569 rec->wrbuf_marc = wrbuf_alloc();
1570 wrbuf_rewind (rec->wrbuf_marc);
1571 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
1572 return record_iconv_return(rec, len,
1573 wrbuf_buf(rec->wrbuf_marc),
1574 wrbuf_len(rec->wrbuf_marc),
1579 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1580 (!strcmp (type, "xml") || !strcmp(type, "oai")))
1582 Z_External *r = (Z_External *) npr->u.databaseRecord;
1583 oident *ent = oid_getentbyoid(r->direct_reference);
1585 /* render bibliographic record .. */
1586 if (r->which == Z_External_OPAC)
1588 r = r->u.opac->bibliographicRecord;
1591 ent = oid_getentbyoid(r->direct_reference);
1594 if (r->which == Z_External_sutrs)
1595 return record_iconv_return(rec, len,
1596 (const char *) r->u.sutrs->buf,
1599 else if (r->which == Z_External_octet)
1602 int marc_decode_type = YAZ_MARC_MARCXML;
1604 if (!strcmp(type, "oai"))
1605 marc_decode_type = YAZ_MARC_OAIMARC;
1613 case VAL_APPLICATION_XML:
1616 if (!rec->wrbuf_marc)
1617 rec->wrbuf_marc = wrbuf_alloc();
1618 wrbuf_rewind (rec->wrbuf_marc);
1619 mt = yaz_marc_create();
1621 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
1622 if (yaz_marc_decode_wrbuf (
1623 mt, (const char *) r->u.octet_aligned->buf,
1624 r->u.octet_aligned->len,
1625 rec->wrbuf_marc) > 0)
1627 yaz_marc_destroy(mt);
1628 return record_iconv_return(rec, len,
1629 wrbuf_buf(rec->wrbuf_marc),
1630 wrbuf_len(rec->wrbuf_marc),
1633 yaz_marc_destroy(mt);
1635 return record_iconv_return(rec, len,
1636 (const char *) r->u.octet_aligned->buf,
1637 r->u.octet_aligned->len,
1640 else if (r->which == Z_External_grs1)
1647 else if (!strcmp (type, "raw"))
1649 if (npr->which == Z_NamePlusRecord_databaseRecord)
1651 Z_External *r = (Z_External *) npr->u.databaseRecord;
1653 if (r->which == Z_External_sutrs)
1655 if (len) *len = r->u.sutrs->len;
1656 return (const char *) r->u.sutrs->buf;
1658 else if (r->which == Z_External_octet)
1660 if (len) *len = r->u.octet_aligned->len;
1661 return (const char *) r->u.octet_aligned->buf;
1663 else /* grs-1, explain, OPAC, ... */
1666 return (const char *) npr->u.databaseRecord;
1671 else if (!strcmp (type, "ext"))
1673 if (npr->which == Z_NamePlusRecord_databaseRecord)
1674 return (const char *) npr->u.databaseRecord;
1677 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1678 !strcmp (type, "opac"))
1681 Z_External *r = (Z_External *) npr->u.databaseRecord;
1682 if (r->which == Z_External_OPAC)
1684 if (!rec->wrbuf_opac)
1685 rec->wrbuf_opac = wrbuf_alloc();
1686 wrbuf_rewind (rec->wrbuf_opac);
1687 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
1688 return record_iconv_return(rec, len,
1689 wrbuf_buf(rec->wrbuf_opac),
1690 wrbuf_len(rec->wrbuf_opac),
1697 static int strcmp_null(const char *v1, const char *v2)
1703 return strcmp(v1, v2);
1706 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr,
1709 ZOOM_record_cache rc;
1710 const char *elementSetName =
1711 ZOOM_resultset_option_get (r, "elementSetName");
1712 const char *syntax =
1713 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1715 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1716 ZOOM_connection_put_event(r->connection, event);
1718 for (rc = r->record_cache; rc; rc = rc->next)
1722 if (strcmp_null(r->schema, rc->schema))
1724 if (strcmp_null(elementSetName,rc->elementSetName))
1726 if (strcmp_null(syntax, rc->syntax))
1728 /* not destroying rc->npr (it's handled by nmem )*/
1730 /* keeping wrbuf_marc too */
1734 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1737 rc->rec.wrbuf_marc = 0;
1738 rc->rec.wrbuf_iconv = 0;
1739 rc->rec.wrbuf_opac = 0;
1741 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1743 rc->elementSetName = 0;
1746 rc->syntax = odr_strdup (r->odr, syntax);
1751 rc->schema = odr_strdup (r->odr, r->schema);
1756 rc->next = r->record_cache;
1757 r->record_cache = rc;
1760 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1762 ZOOM_record_cache rc;
1763 const char *elementSetName =
1764 ZOOM_resultset_option_get (r, "elementSetName");
1765 const char *syntax =
1766 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1768 for (rc = r->record_cache; rc; rc = rc->next)
1772 if (strcmp_null(r->schema, rc->schema))
1774 if (strcmp_null(elementSetName,rc->elementSetName))
1776 if (strcmp_null(syntax, rc->syntax))
1784 static void handle_records (ZOOM_connection c, Z_Records *sr,
1787 ZOOM_resultset resultset;
1791 switch (c->tasks->which)
1793 case ZOOM_TASK_SEARCH:
1794 resultset = c->tasks->u.search.resultset;
1796 case ZOOM_TASK_RETRIEVE:
1797 resultset = c->tasks->u.retrieve.resultset;
1802 if (sr && sr->which == Z_Records_NSD)
1804 Z_DiagRec dr, *dr_p = &dr;
1805 dr.which = Z_DiagRec_defaultFormat;
1806 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1808 response_diag (c, dr_p);
1810 else if (sr && sr->which == Z_Records_multipleNSD)
1812 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1813 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1815 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1819 if (resultset->count + resultset->start > resultset->size)
1820 resultset->count = resultset->size - resultset->start;
1821 if (resultset->count < 0)
1822 resultset->count = 0;
1823 if (sr && sr->which == Z_Records_DBOSD)
1826 NMEM nmem = odr_extract_mem (c->odr_in);
1827 Z_NamePlusRecordList *p =
1828 sr->u.databaseOrSurDiagnostics;
1829 for (i = 0; i<p->num_records; i++)
1831 record_cache_add (resultset, p->records[i],
1832 i+ resultset->start);
1834 /* transfer our response to search_nmem .. we need it later */
1835 nmem_transfer (resultset->odr->mem, nmem);
1836 nmem_destroy (nmem);
1837 if (present_phase && p->num_records == 0)
1839 /* present response and we didn't get any records! */
1840 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1843 else if (present_phase)
1845 /* present response and we didn't get any records! */
1846 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1851 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1853 handle_records (c, pr->records, 1);
1856 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1858 ZOOM_resultset resultset;
1861 yaz_log (LOG_DEBUG, "got search response");
1863 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1866 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
1867 ZOOM_connection_put_event(c, event);
1869 resultset = c->tasks->u.search.resultset;
1871 resultset->size = *sr->resultCount;
1872 handle_records (c, sr->records, 0);
1875 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1877 if (res->diagnostics && res->num_diagnostics > 0)
1878 response_diag (c, res->diagnostics[0]);
1881 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1883 NMEM nmem = odr_extract_mem (c->odr_in);
1886 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1888 scan = c->tasks->u.scan.scan;
1890 if (res->entries && res->entries->nonsurrogateDiagnostics)
1891 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1892 scan->scan_response = res;
1893 nmem_transfer (scan->odr->mem, nmem);
1895 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1896 if (res->positionOfTerm)
1897 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1898 if (res->scanStatus)
1899 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1900 if (res->numberOfEntriesReturned)
1901 ZOOM_options_set_int (scan->options, "number",
1902 *res->numberOfEntriesReturned);
1903 nmem_destroy (nmem);
1907 static zoom_ret send_sort (ZOOM_connection c)
1909 ZOOM_resultset resultset;
1911 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1912 return zoom_complete;
1914 resultset = c->tasks->u.search.resultset;
1918 resultset->r_sort_spec = 0;
1919 return zoom_complete;
1921 if (resultset->r_sort_spec)
1923 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1924 Z_SortRequest *req = apdu->u.sortRequest;
1926 req->num_inputResultSetNames = 1;
1927 req->inputResultSetNames = (Z_InternationalString **)
1928 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1929 req->inputResultSetNames[0] =
1930 odr_strdup (c->odr_out, resultset->setname);
1931 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1932 req->sortSequence = resultset->r_sort_spec;
1933 resultset->r_sort_spec = 0;
1934 return send_APDU (c, apdu);
1936 return zoom_complete;
1939 static zoom_ret send_present (ZOOM_connection c)
1942 Z_PresentRequest *req = 0;
1944 const char *syntax = 0;
1945 const char *elementSetName = 0;
1946 ZOOM_resultset resultset;
1949 return zoom_complete;
1951 switch (c->tasks->which)
1953 case ZOOM_TASK_SEARCH:
1954 resultset = c->tasks->u.search.resultset;
1956 case ZOOM_TASK_RETRIEVE:
1957 resultset = c->tasks->u.retrieve.resultset;
1958 resultset->start = c->tasks->u.retrieve.start;
1959 resultset->count = c->tasks->u.retrieve.count;
1961 if (resultset->start >= resultset->size)
1962 return zoom_complete;
1963 if (resultset->start + resultset->count > resultset->size)
1964 resultset->count = resultset->size - resultset->start;
1967 return zoom_complete;
1970 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1971 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1973 if (c->error) /* don't continue on error */
1974 return zoom_complete;
1975 if (resultset->start < 0)
1976 return zoom_complete;
1977 for (i = 0; i<resultset->count; i++)
1980 record_cache_lookup (resultset, i + resultset->start);
1984 if (i == resultset->count)
1985 return zoom_complete;
1987 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1988 req = apdu->u.presentRequest;
1990 resultset->start += i;
1991 resultset->count -= i;
1992 *req->resultSetStartPoint = resultset->start + 1;
1993 *req->numberOfRecordsRequested = resultset->step>0 ?
1994 resultset->step : resultset->count;
1995 assert (*req->numberOfRecordsRequested > 0);
1997 if (syntax && *syntax)
1998 req->preferredRecordSyntax =
1999 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
2001 if (resultset->schema && *resultset->schema)
2003 Z_RecordComposition *compo = (Z_RecordComposition *)
2004 odr_malloc (c->odr_out, sizeof(*compo));
2006 req->recordComposition = compo;
2007 compo->which = Z_RecordComp_complex;
2008 compo->u.complex = (Z_CompSpec *)
2009 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2010 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2011 odr_malloc(c->odr_out, sizeof(bool_t));
2012 *compo->u.complex->selectAlternativeSyntax = 0;
2014 compo->u.complex->generic = (Z_Specification *)
2015 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2017 compo->u.complex->generic->which = Z_Schema_oid;
2018 compo->u.complex->generic->schema.oid = (Odr_oid *)
2019 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, resultset->schema);
2021 if (!compo->u.complex->generic->schema.oid)
2023 /* OID wasn't a schema! Try record syntax instead. */
2025 compo->u.complex->generic->schema.oid = (Odr_oid *)
2026 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, resultset->schema);
2028 if (elementSetName && *elementSetName)
2030 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2031 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2032 compo->u.complex->generic->elementSpec->which =
2033 Z_ElementSpec_elementSetName;
2034 compo->u.complex->generic->elementSpec->u.elementSetName =
2035 odr_strdup (c->odr_out, elementSetName);
2038 compo->u.complex->generic->elementSpec = 0;
2039 compo->u.complex->num_dbSpecific = 0;
2040 compo->u.complex->dbSpecific = 0;
2041 compo->u.complex->num_recordSyntax = 0;
2042 compo->u.complex->recordSyntax = 0;
2044 else if (elementSetName && *elementSetName)
2046 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2047 odr_malloc (c->odr_out, sizeof(*esn));
2048 Z_RecordComposition *compo = (Z_RecordComposition *)
2049 odr_malloc (c->odr_out, sizeof(*compo));
2051 esn->which = Z_ElementSetNames_generic;
2052 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
2053 compo->which = Z_RecordComp_simple;
2054 compo->u.simple = esn;
2055 req->recordComposition = compo;
2057 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2058 return send_APDU (c, apdu);
2061 ZOOM_API(ZOOM_scanset)
2062 ZOOM_connection_scan (ZOOM_connection c, const char *start)
2064 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
2066 scan->connection = c;
2067 scan->odr = odr_createmem (ODR_DECODE);
2068 scan->options = ZOOM_options_create_with_parent (c->options);
2070 scan->scan_response = 0;
2072 if ((scan->termListAndStartPoint =
2073 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
2076 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
2077 task->u.scan.scan = scan;
2082 while (ZOOM_event (1, &c))
2090 ZOOM_scanset_destroy (ZOOM_scanset scan)
2095 if (scan->refcount == 0)
2097 odr_destroy (scan->odr);
2099 ZOOM_options_destroy (scan->options);
2104 static zoom_ret send_package (ZOOM_connection c)
2108 return zoom_complete;
2109 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2111 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
2112 ZOOM_connection_put_event (c, event);
2114 return do_write_ex (c, c->tasks->u.package->buf_out,
2115 c->tasks->u.package->len_out);
2118 static zoom_ret send_scan (ZOOM_connection c)
2121 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2122 Z_ScanRequest *req = apdu->u.scanRequest;
2124 return zoom_complete;
2125 assert (c->tasks->which == ZOOM_TASK_SCAN);
2126 scan = c->tasks->u.scan.scan;
2128 req->termListAndStartPoint = scan->termListAndStartPoint;
2129 req->attributeSet = scan->attributeSet;
2131 *req->numberOfTermsRequested =
2132 ZOOM_options_get_int(scan->options, "number", 10);
2134 req->preferredPositionInResponse =
2135 odr_intdup (c->odr_out,
2136 ZOOM_options_get_int(scan->options, "position", 1));
2139 odr_intdup (c->odr_out,
2140 ZOOM_options_get_int(scan->options, "stepSize", 0));
2142 req->databaseNames = set_DatabaseNames (c, scan->options,
2143 &req->num_databaseNames);
2145 return send_APDU (c, apdu);
2149 ZOOM_scanset_size (ZOOM_scanset scan)
2151 if (!scan || !scan->scan_response || !scan->scan_response->entries)
2153 return scan->scan_response->entries->num_entries;
2156 ZOOM_API(const char *)
2157 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
2160 const char *term = 0;
2161 size_t noent = ZOOM_scanset_size (scan);
2162 Z_ScanResponse *res = scan->scan_response;
2168 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2170 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2172 if (t->term->which == Z_Term_general)
2174 term = (const char *) t->term->u.general->buf;
2175 *len = t->term->u.general->len;
2177 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2182 ZOOM_API(const char *)
2183 ZOOM_scanset_display_term (ZOOM_scanset scan, size_t pos,
2186 const char *term = 0;
2187 size_t noent = ZOOM_scanset_size (scan);
2188 Z_ScanResponse *res = scan->scan_response;
2194 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2196 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2200 term = (const char *) t->term->u.general->buf;
2201 *len = strlen(term);
2203 else if (t->term->which == Z_Term_general)
2205 term = (const char *) t->term->u.general->buf;
2206 *len = t->term->u.general->len;
2208 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2213 ZOOM_API(const char *)
2214 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
2216 return ZOOM_options_get (scan->options, key);
2220 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
2223 ZOOM_options_set (scan->options, key, val);
2226 static Z_APDU *create_es_package (ZOOM_package p, int type)
2229 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
2230 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2232 *req->function = Z_ExtendedServicesRequest_create;
2234 str = ZOOM_options_get(p->options, "package-name");
2236 req->packageName = nmem_strdup (p->odr_out->mem, str);
2238 str = ZOOM_options_get(p->options, "user-id");
2240 req->userId = nmem_strdup (p->odr_out->mem, str);
2242 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
2245 str = ZOOM_options_get(p->options, "function");
2248 if (!strcmp (str, "create"))
2250 if (!strcmp (str, "delete"))
2252 if (!strcmp (str, "modify"))
2258 static const char *ill_array_lookup (void *clientData, const char *idx)
2260 ZOOM_package p = (ZOOM_package) clientData;
2261 return ZOOM_options_get (p->options, idx+4);
2264 static Z_External *encode_ill_request (ZOOM_package p)
2266 ODR out = p->odr_out;
2269 struct ill_get_ctl ctl;
2271 ctl.odr = p->odr_out;
2273 ctl.f = ill_array_lookup;
2275 req = ill_get_ILLRequest(&ctl, "ill", 0);
2277 if (!ill_Request (out, &req, 0, 0))
2279 int ill_request_size;
2280 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
2281 if (ill_request_buf)
2282 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
2288 int illRequest_size = 0;
2289 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
2291 oid.proto = PROTO_GENERAL;
2292 oid.oclass = CLASS_GENERAL;
2293 oid.value = VAL_ISO_ILL_1;
2295 r = (Z_External *) odr_malloc (out, sizeof(*r));
2296 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
2297 r->indirect_reference = 0;
2299 r->which = Z_External_single;
2301 r->u.single_ASN1_type = (Odr_oct *)
2302 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
2303 r->u.single_ASN1_type->buf = (unsigned char*)
2304 odr_malloc (out, illRequest_size);
2305 r->u.single_ASN1_type->len = illRequest_size;
2306 r->u.single_ASN1_type->size = illRequest_size;
2307 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
2312 static Z_ItemOrder *encode_item_order(ZOOM_package p)
2314 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
2317 req->which=Z_IOItemOrder_esRequest;
2318 req->u.esRequest = (Z_IORequest *)
2319 odr_malloc(p->odr_out,sizeof(Z_IORequest));
2321 /* to keep part ... */
2322 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
2323 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
2324 req->u.esRequest->toKeep->supplDescription = 0;
2325 req->u.esRequest->toKeep->contact = (Z_IOContact *)
2326 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
2328 str = ZOOM_options_get(p->options, "contact-name");
2329 req->u.esRequest->toKeep->contact->name = str ?
2330 nmem_strdup (p->odr_out->mem, str) : 0;
2332 str = ZOOM_options_get(p->options, "contact-phone");
2333 req->u.esRequest->toKeep->contact->phone = str ?
2334 nmem_strdup (p->odr_out->mem, str) : 0;
2336 str = ZOOM_options_get(p->options, "contact-email");
2337 req->u.esRequest->toKeep->contact->email = str ?
2338 nmem_strdup (p->odr_out->mem, str) : 0;
2340 req->u.esRequest->toKeep->addlBilling = 0;
2342 /* not to keep part ... */
2343 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
2344 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
2346 str = ZOOM_options_get(p->options, "itemorder-setname");
2351 req->u.esRequest->notToKeep->resultSetItem = 0;
2354 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
2355 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
2357 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
2358 nmem_strdup (p->odr_out->mem, str);
2359 req->u.esRequest->notToKeep->resultSetItem->item =
2360 (int *) odr_malloc(p->odr_out, sizeof(int));
2362 str = ZOOM_options_get(p->options, "itemorder-item");
2363 *req->u.esRequest->notToKeep->resultSetItem->item =
2364 (str ? atoi(str) : 1);
2366 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
2372 ZOOM_package_send (ZOOM_package p, const char *type)
2379 odr_reset (p->odr_out);
2382 if (!strcmp(type, "itemorder"))
2385 apdu = create_es_package (p, VAL_ITEMORDER);
2388 r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
2390 r->direct_reference =
2391 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
2394 r->which = Z_External_itemOrder;
2395 r->indirect_reference = 0;
2396 r->u.itemOrder = encode_item_order (p);
2398 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
2403 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
2407 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
2408 task->u.package = p;
2409 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
2410 p->buf_out = (char *) xmalloc (p->len_out);
2411 memcpy (p->buf_out, buf, p->len_out);
2416 while (ZOOM_event (1, &c))
2423 ZOOM_API(ZOOM_package)
2424 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
2426 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
2429 p->odr_out = odr_createmem (ODR_ENCODE);
2430 p->options = ZOOM_options_create_with_parent2 (options, c->options);
2438 ZOOM_package_destroy(ZOOM_package p)
2443 if (p->refcount == 0)
2445 odr_destroy (p->odr_out);
2448 ZOOM_options_destroy (p->options);
2453 ZOOM_API(const char *)
2454 ZOOM_package_option_get (ZOOM_package p, const char *key)
2456 return ZOOM_options_get (p->options, key);
2461 ZOOM_package_option_set (ZOOM_package p, const char *key,
2464 ZOOM_options_set (p->options, key, val);
2467 static int ZOOM_connection_exec_task (ZOOM_connection c)
2469 ZOOM_task task = c->tasks;
2470 zoom_ret ret = zoom_complete;
2474 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
2477 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
2478 task->which, task->running);
2479 if (c->error != ZOOM_ERROR_NONE)
2481 yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
2482 ZOOM_connection_remove_tasks (c);
2487 yaz_log (LOG_DEBUG, "task already running");
2491 ret = zoom_complete;
2492 if (c->cs || task->which == ZOOM_TASK_CONNECT)
2494 switch (task->which)
2496 case ZOOM_TASK_SEARCH:
2497 if (c->proto == PROTO_HTTP)
2498 ret = ZOOM_connection_srw_send_search(c);
2500 ret = ZOOM_connection_send_search(c);
2502 case ZOOM_TASK_RETRIEVE:
2503 if (c->proto == PROTO_HTTP)
2504 ret = ZOOM_connection_srw_send_search(c);
2506 ret = send_present (c);
2508 case ZOOM_TASK_CONNECT:
2509 ret = do_connect(c);
2511 case ZOOM_TASK_SCAN:
2514 case ZOOM_TASK_PACKAGE:
2515 ret = send_package(c);
2521 yaz_log (LOG_DEBUG, "remove tasks because no connection exist");
2522 ZOOM_connection_remove_tasks (c);
2524 if (ret == zoom_complete)
2526 yaz_log (LOG_DEBUG, "task removed (complete)");
2527 ZOOM_connection_remove_task (c);
2530 yaz_log (LOG_DEBUG, "task pending");
2534 static zoom_ret send_sort_present (ZOOM_connection c)
2536 zoom_ret r = send_sort (c);
2537 if (r == zoom_complete)
2538 r = send_present (c);
2542 static int es_response (ZOOM_connection c,
2543 Z_ExtendedServicesResponse *res)
2545 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
2547 if (res->diagnostics && res->num_diagnostics > 0)
2548 response_diag(c, res->diagnostics[0]);
2549 if (res->taskPackage &&
2550 res->taskPackage->which == Z_External_extendedService)
2552 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
2553 Odr_oct *id = taskPackage->targetReference;
2556 ZOOM_options_setl (c->tasks->u.package->options,
2557 "targetReference", (char*) id->buf, id->len);
2563 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
2565 Z_InitResponse *initrs;
2568 yaz_log (LOG_DEBUG, "recv APDU type=%d", apdu->which);
2571 case Z_APDU_initResponse:
2572 initrs = apdu->u.initResponse;
2573 ZOOM_connection_option_set(c, "serverImplementationId",
2574 initrs->implementationId ?
2575 initrs->implementationId : "");
2576 ZOOM_connection_option_set(c, "serverImplementationName",
2577 initrs->implementationName ?
2578 initrs->implementationName : "");
2579 ZOOM_connection_option_set(c, "serverImplementationVersion",
2580 initrs->implementationVersion ?
2581 initrs->implementationVersion : "");
2582 /* Set the three old options too, for old applications */
2583 ZOOM_connection_option_set(c, "targetImplementationId",
2584 initrs->implementationId ?
2585 initrs->implementationId : "");
2586 ZOOM_connection_option_set(c, "targetImplementationName",
2587 initrs->implementationName ?
2588 initrs->implementationName : "");
2589 ZOOM_connection_option_set(c, "targetImplementationVersion",
2590 initrs->implementationVersion ?
2591 initrs->implementationVersion : "");
2592 if (!*initrs->result)
2594 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0);
2599 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
2601 xfree (c->cookie_in);
2604 c->cookie_in = xstrdup(cookie);
2605 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2606 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2607 c->support_named_resultsets = 1;
2610 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2611 ZOOM_connection_remove_task (c);
2613 ZOOM_connection_exec_task (c);
2615 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2617 NMEM tmpmem = nmem_create();
2618 Z_CharSetandLanguageNegotiation *p =
2619 yaz_get_charneg_record(initrs->otherInfo);
2623 char *charset=NULL, *lang=NULL;
2626 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
2627 yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
2628 "language %s, select %d",
2629 charset ? charset : "none", lang ? lang : "none", sel);
2631 ZOOM_connection_option_set (c, "negotiation-charset",
2634 ZOOM_connection_option_set (c, "negotiation-lang",
2636 nmem_destroy(tmpmem);
2640 case Z_APDU_searchResponse:
2641 handle_search_response (c, apdu->u.searchResponse);
2642 if (send_sort_present (c) == zoom_complete)
2643 ZOOM_connection_remove_task (c);
2645 case Z_APDU_presentResponse:
2646 handle_present_response (c, apdu->u.presentResponse);
2647 if (send_present (c) == zoom_complete)
2648 ZOOM_connection_remove_task (c);
2650 case Z_APDU_sortResponse:
2651 sort_response (c, apdu->u.sortResponse);
2652 if (send_present (c) == zoom_complete)
2653 ZOOM_connection_remove_task (c);
2655 case Z_APDU_scanResponse:
2656 scan_response (c, apdu->u.scanResponse);
2657 ZOOM_connection_remove_task (c);
2659 case Z_APDU_extendedServicesResponse:
2660 es_response (c, apdu->u.extendedServicesResponse);
2661 ZOOM_connection_remove_task (c);
2664 if (c->reconnect_ok)
2667 c->tasks->running = 0;
2668 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2672 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2677 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2683 static void handle_srw_response(ZOOM_connection c,
2684 Z_SRW_searchRetrieveResponse *res)
2686 ZOOM_resultset resultset = 0;
2694 if (c->tasks->which == ZOOM_TASK_SEARCH)
2695 resultset = c->tasks->u.search.resultset;
2696 else if (c->tasks->which == ZOOM_TASK_RETRIEVE)
2697 resultset = c->tasks->u.retrieve.resultset;
2701 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2702 ZOOM_connection_put_event(c, event);
2704 resultset->size = 0;
2706 yaz_log(LOG_DEBUG, "got SRW response OK");
2708 if (res->numberOfRecords)
2709 resultset->size = *res->numberOfRecords;
2711 for (i = 0; i<res->num_records; i++)
2715 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
2716 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
2718 if (res->records[i].recordPosition &&
2719 *res->records[i].recordPosition > 0)
2720 pos = *res->records[i].recordPosition - 1;
2722 pos = resultset->start + i;
2724 npr->databaseName = 0;
2725 npr->which = Z_NamePlusRecord_databaseRecord;
2726 npr->u.databaseRecord = (Z_External *)
2727 odr_malloc(c->odr_in, sizeof(Z_External));
2728 npr->u.databaseRecord->descriptor = 0;
2729 npr->u.databaseRecord->direct_reference =
2730 yaz_oidval_to_z3950oid(c->odr_in, CLASS_RECSYN, VAL_TEXT_XML);
2731 npr->u.databaseRecord->which = Z_External_octet;
2732 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
2733 odr_malloc(c->odr_in, sizeof(Odr_oct));
2734 npr->u.databaseRecord->u.octet_aligned->buf =
2735 res->records[i].recordData_buf;
2736 npr->u.databaseRecord->u.octet_aligned->len =
2737 npr->u.databaseRecord->u.octet_aligned->size =
2738 res->records[i].recordData_len;
2739 record_cache_add (resultset, npr, pos);
2741 if (res->num_diagnostics > 0)
2743 set_dset_error(c, *res->diagnostics[0].code, "SRW",
2744 res->diagnostics[0].details, 0);
2746 nmem = odr_extract_mem(c->odr_in);
2747 nmem_transfer(resultset->odr->mem, nmem);
2753 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
2756 const char *content_type = z_HTTP_header_lookup(hres->headers,
2758 const char *connection_head = z_HTTP_header_lookup(hres->headers,
2761 yaz_log (LOG_DEBUG, "handle_http");
2763 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
2765 Z_SOAP *soap_package = 0;
2766 ODR o = odr_createmem(ODR_DECODE);
2767 Z_SOAP_Handler soap_handlers[2] = {
2768 {"http://www.loc.gov/zing/srw/v1.0/", 0,
2769 (Z_SOAP_fun) yaz_srw_codec},
2772 ret = z_soap_codec(o, &soap_package,
2773 &hres->content_buf, &hres->content_len,
2775 if (!ret && soap_package->which == Z_SOAP_generic &&
2776 soap_package->u.generic->no == 0)
2778 Z_SRW_PDU *sr = soap_package->u.generic->p;
2779 if (sr->which == Z_SRW_searchRetrieve_response)
2780 handle_srw_response(c, sr->u.response);
2784 else if (!ret && (soap_package->which == Z_SOAP_fault
2785 || soap_package->which == Z_SOAP_error))
2787 set_HTTP_error(c, hres->code,
2788 soap_package->u.fault->fault_code,
2789 soap_package->u.fault->fault_string);
2797 if (hres->code != 200)
2798 set_HTTP_error(c, hres->code, 0, 0);
2800 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2803 ZOOM_connection_remove_task(c);
2804 if (!strcmp(hres->version, "1.0"))
2806 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
2807 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
2812 /* HTTP 1.1: only if no close we stay alive .. */
2813 if (connection_head && !strcmp(connection_head, "close"))
2819 static int do_read (ZOOM_connection c)
2824 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2825 ZOOM_connection_put_event (c, event);
2828 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2829 more = cs_more(c->cs);
2830 yaz_log (LOG_DEBUG, "do_read len=%d more=%d", r, more);
2835 if (c->reconnect_ok)
2838 c->reconnect_ok = 0;
2839 yaz_log (LOG_DEBUG, "reconnect read");
2840 c->tasks->running = 0;
2841 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2845 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2854 odr_reset (c->odr_in);
2855 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2856 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2857 ZOOM_connection_put_event (c, event);
2859 if (!z_GDU (c->odr_in, &gdu, 0, 0))
2861 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2864 else if (gdu->which == Z_GDU_Z3950)
2865 handle_apdu (c, gdu->u.z3950);
2866 else if (gdu->which == Z_GDU_HTTP_Response)
2869 handle_http (c, gdu->u.HTTP_Response);
2871 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2875 c->reconnect_ok = 0;
2880 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2885 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2886 ZOOM_connection_put_event (c, event);
2888 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2889 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2891 if (c->reconnect_ok)
2894 c->reconnect_ok = 0;
2895 yaz_log (LOG_DEBUG, "reconnect write");
2896 c->tasks->running = 0;
2897 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2898 return zoom_pending;
2900 if (c->state == STATE_CONNECTING)
2901 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
2903 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2905 return zoom_complete;
2909 c->mask = ZOOM_SELECT_EXCEPT;
2910 if (c->cs->io_pending & CS_WANT_WRITE)
2911 c->mask += ZOOM_SELECT_WRITE;
2912 if (c->cs->io_pending & CS_WANT_READ)
2913 c->mask += ZOOM_SELECT_READ;
2914 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2918 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2919 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2921 return zoom_pending;
2924 static zoom_ret do_write(ZOOM_connection c)
2926 return do_write_ex (c, c->buf_out, c->len_out);
2930 ZOOM_API(const char *)
2931 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2933 return ZOOM_options_get (c->options, key);
2937 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2940 ZOOM_options_set (c->options, key, val);
2943 ZOOM_API(const char *)
2944 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2946 return ZOOM_options_get (r->options, key);
2950 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2953 ZOOM_options_set (r->options, key, val);
2958 ZOOM_connection_errcode (ZOOM_connection c)
2960 return ZOOM_connection_error (c, 0, 0);
2963 ZOOM_API(const char *)
2964 ZOOM_connection_errmsg (ZOOM_connection c)
2967 ZOOM_connection_error (c, &msg, 0);
2971 ZOOM_API(const char *)
2972 ZOOM_connection_addinfo (ZOOM_connection c)
2974 const char *addinfo;
2975 ZOOM_connection_error (c, 0, &addinfo);
2979 ZOOM_API(const char *)
2980 ZOOM_diag_str (int error)
2984 case ZOOM_ERROR_NONE:
2986 case ZOOM_ERROR_CONNECT:
2987 return "Connect failed";
2988 case ZOOM_ERROR_MEMORY:
2989 return "Out of memory";
2990 case ZOOM_ERROR_ENCODE:
2991 return "Encoding failed";
2992 case ZOOM_ERROR_DECODE:
2993 return "Decoding failed";
2994 case ZOOM_ERROR_CONNECTION_LOST:
2995 return "Connection lost";
2996 case ZOOM_ERROR_INIT:
2997 return "Init rejected";
2998 case ZOOM_ERROR_INTERNAL:
2999 return "Internal failure";
3000 case ZOOM_ERROR_TIMEOUT:
3002 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
3003 return "Unsupported protocol";
3004 case ZOOM_ERROR_UNSUPPORTED_QUERY:
3005 return "Unsupported query type";
3007 return diagbib1_str (error);
3012 ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
3013 const char **addinfo, const char **diagset)
3015 int error = c->error;
3018 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
3019 *cp = ZOOM_diag_str(error);
3020 else if (!strcmp(c->diagset, "HTTP"))
3021 *cp = z_HTTP_errmsg(c->error);
3022 else if (!strcmp(c->diagset, "Bib-1"))
3023 *cp = ZOOM_diag_str(error);
3024 else if (!strcmp(c->diagset, "SRW"))
3025 *cp = yaz_diag_srw_str(c->error);
3027 *cp = "Unknown error and diagnostic set";
3030 *addinfo = c->addinfo ? c->addinfo : "";
3032 *diagset = c->diagset ? c->diagset : "";
3037 ZOOM_connection_error (ZOOM_connection c, const char **cp,
3038 const char **addinfo)
3040 return ZOOM_connection_error_x(c, cp, addinfo, 0);
3043 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
3045 ZOOM_Event event = 0;
3046 int r = cs_look(c->cs);
3047 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
3052 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
3053 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
3055 ZOOM_connection_put_event (c, event);
3057 else if (r == CS_CONNECT)
3060 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
3062 ret = cs_rcvconnect (c->cs);
3063 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
3066 c->mask = ZOOM_SELECT_EXCEPT;
3067 if (c->cs->io_pending & CS_WANT_WRITE)
3068 c->mask += ZOOM_SELECT_WRITE;
3069 if (c->cs->io_pending & CS_WANT_READ)
3070 c->mask += ZOOM_SELECT_READ;
3071 ZOOM_connection_put_event (c, event);
3075 ZOOM_connection_put_event (c, event);
3076 if (c->proto == PROTO_Z3950)
3077 ZOOM_connection_send_init(c);
3080 /* no init request for SRW .. */
3081 assert (c->tasks->which == ZOOM_TASK_CONNECT);
3082 ZOOM_connection_remove_task (c);
3084 ZOOM_connection_exec_task (c);
3086 c->state = STATE_ESTABLISHED;
3090 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
3092 ZOOM_connection_put_event (c, event);
3097 if (mask & ZOOM_SELECT_READ)
3099 if (c->cs && (mask & ZOOM_SELECT_WRITE))
3106 ZOOM_connection_last_event(ZOOM_connection cs)
3109 return ZOOM_EVENT_NONE;
3110 return cs->last_event;
3114 ZOOM_event (int no, ZOOM_connection *cs)
3118 struct pollfd pollfds[1024];
3119 ZOOM_connection poll_cs[1024];
3122 fd_set input, output, except;
3127 for (i = 0; i<no; i++)
3129 ZOOM_connection c = cs[i];
3131 if (c && (event = ZOOM_connection_get_event(c)))
3133 ZOOM_Event_destroy (event);
3137 for (i = 0; i<no; i++)
3139 ZOOM_connection c = cs[i];
3141 if (c && ZOOM_connection_exec_task (c))
3143 if ((event = ZOOM_connection_get_event(c)))
3145 ZOOM_Event_destroy (event);
3158 for (i = 0; i<no; i++)
3160 ZOOM_connection c = cs[i];
3166 fd = z3950_connection_socket(c);
3167 mask = z3950_connection_mask(c);
3174 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
3175 if (this_timeout != -1 && this_timeout < timeout)
3176 timeout = this_timeout;
3180 short poll_events = 0;
3182 if (mask & ZOOM_SELECT_READ)
3183 poll_events += POLLIN;
3184 if (mask & ZOOM_SELECT_WRITE)
3185 poll_events += POLLOUT;
3186 if (mask & ZOOM_SELECT_EXCEPT)
3187 poll_events += POLLERR;
3188 pollfds[nfds].fd = fd;
3189 pollfds[nfds].events = poll_events;
3190 pollfds[nfds].revents = 0;
3195 if (mask & ZOOM_SELECT_READ)
3197 FD_SET (fd, &input);
3200 if (mask & ZOOM_SELECT_WRITE)
3202 FD_SET (fd, &output);
3205 if (mask & ZOOM_SELECT_EXCEPT)
3207 FD_SET (fd, &except);
3212 if (timeout >= 5000)
3219 r = poll (pollfds, nfds, timeout * 1000);
3220 for (i = 0; i<nfds; i++)
3222 ZOOM_connection c = poll_cs[i];
3226 if (pollfds[i].revents & POLLIN)
3227 mask += ZOOM_SELECT_READ;
3228 if (pollfds[i].revents & POLLOUT)
3229 mask += ZOOM_SELECT_WRITE;
3230 if (pollfds[i].revents & POLLERR)
3231 mask += ZOOM_SELECT_EXCEPT;
3233 ZOOM_connection_do_io(c, mask);
3235 else if (r == 0 && c->mask)
3237 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
3238 /* timeout and this connection was waiting */
3239 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
3241 ZOOM_connection_put_event(c, event);
3245 tv.tv_sec = timeout;
3247 yaz_log (LOG_DEBUG, "select start");
3248 r = select (max_fd+1, &input, &output, &except, &tv);
3249 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
3250 for (i = 0; i<no; i++)
3252 ZOOM_connection c = cs[i];
3257 fd = z3950_connection_socket(c);
3261 /* no timeout and real socket */
3262 if (FD_ISSET(fd, &input))
3263 mask += ZOOM_SELECT_READ;
3264 if (FD_ISSET(fd, &output))
3265 mask += ZOOM_SELECT_WRITE;
3266 if (FD_ISSET(fd, &except))
3267 mask += ZOOM_SELECT_EXCEPT;
3269 ZOOM_connection_do_io(c, mask);
3271 if (r == 0 && c->mask)
3273 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
3274 /* timeout and this connection was waiting */
3275 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
3277 yaz_log (LOG_DEBUG, "timeout");
3278 ZOOM_connection_put_event(c, event);
3282 for (i = 0; i<no; i++)
3284 ZOOM_connection c = cs[i];
3286 if (c && (event = ZOOM_connection_get_event(c)))
3288 ZOOM_Event_destroy (event);