2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.130 2007-05-08 08:22:36 adam Exp $
9 * \brief Implements ZOOM C interface.
17 #include <yaz/yaz-util.h>
18 #include <yaz/xmalloc.h>
19 #include <yaz/otherinfo.h>
21 #include <yaz/pquery.h>
22 #include <yaz/marcdisp.h>
23 #include <yaz/diagbib1.h>
24 #include <yaz/charneg.h>
29 #include <yaz/query-charset.h>
30 #include <yaz/copy_types.h>
32 static int log_api = 0;
33 static int log_details = 0;
40 static void resultset_destroy(ZOOM_resultset r);
41 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
42 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
43 static char *cql2pqf(ZOOM_connection c, const char *cql);
47 * This wrapper is just for logging failed lookups. It would be nicer
48 * if it could cause failure when a lookup fails, but that's hard.
50 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
51 int oid_class, const char *str) {
52 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
55 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
56 c, (int) oid_class, str);
61 static void initlog(void)
63 static int log_level_initialized = 0;
64 if (!log_level_initialized)
66 log_api = yaz_log_module_level("zoom");
67 log_details = yaz_log_module_level("zoomdetails");
68 log_level_initialized = 1;
72 static ZOOM_Event ZOOM_Event_create(int kind)
74 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
78 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
82 static void ZOOM_Event_destroy(ZOOM_Event event)
87 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
91 c->m_queue_back->prev = event;
92 assert(c->m_queue_front);
96 assert(!c->m_queue_front);
97 c->m_queue_front = event;
99 event->next = c->m_queue_back;
101 c->m_queue_back = event;
104 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
106 ZOOM_Event event = c->m_queue_front;
109 c->last_event = ZOOM_EVENT_NONE;
112 assert(c->m_queue_back);
113 c->m_queue_front = event->prev;
114 if (c->m_queue_front)
116 assert(c->m_queue_back);
117 c->m_queue_front->next = 0;
121 c->last_event = event->kind;
125 static void ZOOM_connection_remove_events(ZOOM_connection c)
128 while ((event = ZOOM_connection_get_event(c)))
129 ZOOM_Event_destroy(event);
132 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
134 ZOOM_Event event = c->m_queue_front;
136 return event ? event->kind : ZOOM_EVENT_NONE;
139 void ZOOM_connection_remove_tasks(ZOOM_connection c);
141 static void set_dset_error(ZOOM_connection c, int error,
143 const char *addinfo, const char *addinfo2)
150 if (!c->diagset || strcmp(dset, c->diagset))
153 c->diagset = xstrdup(dset);
154 /* remove integer part from SRW diagset .. */
155 if ((cp = strrchr(c->diagset, '/')))
158 if (addinfo && addinfo2)
160 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
161 strcpy(c->addinfo, addinfo);
162 strcat(c->addinfo, addinfo2);
165 c->addinfo = xstrdup(addinfo);
166 if (error != ZOOM_ERROR_NONE)
168 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
169 c, c->host_port ? c->host_port : "<>", dset, error,
170 addinfo ? addinfo : "",
171 addinfo2 ? addinfo2 : "");
172 ZOOM_connection_remove_tasks(c);
177 static void set_HTTP_error(ZOOM_connection c, int error,
178 const char *addinfo, const char *addinfo2)
180 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
185 static void set_ZOOM_error(ZOOM_connection c, int error,
188 set_dset_error(c, error, "ZOOM", addinfo, 0);
191 static void clear_error(ZOOM_connection c)
194 * If an error is tied to an operation then it's ok to clear: for
195 * example, a diagnostic returned from a search is cleared by a
196 * subsequent search. However, problems such as Connection Lost
197 * or Init Refused are not cleared, because they are not
198 * recoverable: doing another search doesn't help.
201 ZOOM_connection_remove_events(c);
204 case ZOOM_ERROR_CONNECT:
205 case ZOOM_ERROR_MEMORY:
206 case ZOOM_ERROR_DECODE:
207 case ZOOM_ERROR_CONNECTION_LOST:
208 case ZOOM_ERROR_INIT:
209 case ZOOM_ERROR_INTERNAL:
210 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
213 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
217 void ZOOM_connection_show_task(ZOOM_task task)
221 case ZOOM_TASK_SEARCH:
222 yaz_log(YLOG_LOG, "search p=%p", task);
224 case ZOOM_TASK_RETRIEVE:
225 yaz_log(YLOG_LOG, "retrieve p=%p", task);
227 case ZOOM_TASK_CONNECT:
228 yaz_log(YLOG_LOG, "connect p=%p", task);
231 yaz_log(YLOG_LOG, "scant p=%p", task);
236 void ZOOM_connection_show_tasks(ZOOM_connection c)
239 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
240 for (task = c->tasks; task; task = task->next)
241 ZOOM_connection_show_task(task);
244 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
246 ZOOM_task *taskp = &c->tasks;
248 taskp = &(*taskp)->next;
249 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
250 (*taskp)->running = 0;
251 (*taskp)->which = which;
257 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
259 return c->tasks ? 0 : 1;
262 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
264 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
266 task->next = c->tasks;
275 void ZOOM_connection_remove_task(ZOOM_connection c)
277 ZOOM_task task = c->tasks;
281 c->tasks = task->next;
284 case ZOOM_TASK_SEARCH:
285 resultset_destroy(task->u.search.resultset);
286 xfree(task->u.search.syntax);
287 xfree(task->u.search.elementSetName);
289 case ZOOM_TASK_RETRIEVE:
290 resultset_destroy(task->u.retrieve.resultset);
291 xfree(task->u.retrieve.syntax);
292 xfree(task->u.retrieve.elementSetName);
294 case ZOOM_TASK_CONNECT:
297 ZOOM_scanset_destroy(task->u.scan.scan);
299 case ZOOM_TASK_PACKAGE:
300 ZOOM_package_destroy(task->u.package);
303 resultset_destroy(task->u.sort.resultset);
304 ZOOM_query_destroy(task->u.sort.q);
313 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
314 ZOOM_connection_put_event(c, event);
319 static int ZOOM_connection_exec_task(ZOOM_connection c);
321 void ZOOM_connection_remove_tasks(ZOOM_connection c)
324 ZOOM_connection_remove_task(c);
327 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
329 const char *elementSetName);
331 ZOOM_API(ZOOM_connection)
332 ZOOM_connection_create(ZOOM_options options)
334 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
338 yaz_log(log_api, "%p ZOOM_connection_create", c);
340 c->proto = PROTO_Z3950;
342 ZOOM_connection_set_mask(c, 0);
344 c->state = STATE_IDLE;
347 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
354 c->options = ZOOM_options_create_with_parent(options);
360 c->charset = c->lang = 0;
371 c->maximum_record_size = 0;
372 c->preferred_message_size = 0;
374 c->odr_in = odr_createmem(ODR_DECODE);
375 c->odr_out = odr_createmem(ODR_ENCODE);
378 c->support_named_resultsets = 0;
379 c->last_event = ZOOM_EVENT_NONE;
381 c->m_queue_front = 0;
387 /* set database names. Take local databases (if set); otherwise
388 take databases given in ZURL (if set); otherwise use Default */
389 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
392 char **databaseNames;
393 const char *cp = ZOOM_options_get(options, "databaseName");
395 if ((!cp || !*cp) && con->host_port)
397 if (strncmp(con->host_port, "unix:", 5) == 0)
398 cp = strchr(con->host_port+5, ':');
400 cp = strchr(con->host_port, '/');
406 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
407 return databaseNames;
410 ZOOM_API(ZOOM_connection)
411 ZOOM_connection_new(const char *host, int portnum)
413 ZOOM_connection c = ZOOM_connection_create(0);
415 ZOOM_connection_connect(c, host, portnum);
419 static zoom_sru_mode get_sru_mode_from_string(const char *s)
422 return zoom_sru_soap;
423 if (!yaz_matchstr(s, "soap"))
424 return zoom_sru_soap;
425 else if (!yaz_matchstr(s, "get"))
427 else if (!yaz_matchstr(s, "post"))
428 return zoom_sru_post;
429 return zoom_sru_error;
433 ZOOM_connection_connect(ZOOM_connection c,
434 const char *host, int portnum)
441 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
442 c, host ? host : "null", portnum);
444 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
445 ZOOM_connection_remove_tasks(c);
449 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
453 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
456 val = ZOOM_options_get(c->options, "proxy");
459 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
460 c->proxy = xstrdup(val);
465 val = ZOOM_options_get(c->options, "charset");
468 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
469 c->charset = xstrdup(val);
473 val = ZOOM_options_get(c->options, "lang");
476 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
477 c->lang = xstrdup(val);
488 sprintf(hostn, "%.80s:%d", host, portnum);
489 c->host_port = xstrdup(hostn);
492 c->host_port = xstrdup(host);
497 * If the "<scheme>:" part of the host string is preceded by one
498 * or more comma-separated <name>=<value> pairs, these are taken
499 * to be options to be set on the connection object. Among other
500 * applications, this facility can be used to embed authentication
502 * user=admin,password=secret,tcp:localhost:9999
504 char *remainder = c->host_port;
505 char *pcolon = strchr(remainder, ':');
508 while ((pcomma = strchr(remainder, ',')) != 0 &&
509 (pcolon == 0 || pcomma < pcolon)) {
511 if ((pequals = strchr(remainder, '=')) != 0) {
513 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
514 ZOOM_connection_option_set(c, remainder, pequals+1);
516 remainder = pcomma+1;
519 if (remainder != c->host_port) {
521 c->host_port = xstrdup(remainder);
522 /*printf("# reset hp='%s'\n", remainder);*/
526 val = ZOOM_options_get(c->options, "sru");
527 c->sru_mode = get_sru_mode_from_string(val);
529 ZOOM_options_set(c->options, "host", c->host_port);
531 xfree(c->cookie_out);
533 val = ZOOM_options_get(c->options, "cookie");
536 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
537 c->cookie_out = xstrdup(val);
542 val = ZOOM_options_get(c->options, "clientIP");
545 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
547 c->client_IP = xstrdup(val);
552 val = ZOOM_options_get(c->options, "group");
554 c->group = xstrdup(val);
558 val = ZOOM_options_get(c->options, "user");
560 c->user = xstrdup(val);
564 val = ZOOM_options_get(c->options, "password");
566 val = ZOOM_options_get(c->options, "pass");
569 c->password = xstrdup(val);
571 c->maximum_record_size =
572 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
573 c->preferred_message_size =
574 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
576 c->async = ZOOM_options_get_bool(c->options, "async", 0);
577 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
579 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
583 while (ZOOM_event(1, &c))
589 ZOOM_query_create(void)
591 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
593 yaz_log(log_details, "%p ZOOM_query_create", s);
597 s->odr = odr_createmem(ODR_ENCODE);
604 ZOOM_query_destroy(ZOOM_query s)
610 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
611 if (s->refcount == 0)
619 ZOOM_query_prefix(ZOOM_query s, const char *str)
621 s->query_string = odr_strdup(s->odr, str);
622 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
623 s->z_query->which = Z_Query_type_1;
624 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
625 if (!s->z_query->u.type_1)
627 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
631 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
636 ZOOM_query_cql(ZOOM_query s, const char *str)
640 s->query_string = odr_strdup(s->odr, str);
642 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
643 ext->direct_reference = odr_getoidbystr(s->odr, "1.2.840.10003.16.2");
644 ext->indirect_reference = 0;
646 ext->which = Z_External_CQL;
647 ext->u.cql = s->query_string;
649 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
650 s->z_query->which = Z_Query_type_104;
651 s->z_query->u.type_104 = ext;
653 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
659 * Translate the CQL string client-side into RPN which is passed to
660 * the server. This is useful for server's that don't themselves
661 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
662 * only as a place to stash diagnostics if compilation fails; if this
663 * information is not needed, a null pointer may be used.
666 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
670 ZOOM_connection freeme = 0;
672 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
674 conn = freeme = ZOOM_connection_create(0);
676 rpn = cql2pqf(conn, str);
678 ZOOM_connection_destroy(freeme);
682 ret = ZOOM_query_prefix(s, rpn);
688 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
689 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
690 * to the server, as the YAZ GFS doesn't know how to handle this.
693 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
694 int *ccl_error, const char **error_string,
698 struct ccl_rpn_node *rpn;
699 CCL_bibset bibset = ccl_qual_mk();
702 ccl_qual_buf(bibset, config);
704 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
707 *error_string = ccl_err_msg(*ccl_error);
712 WRBUF wr = wrbuf_alloc();
715 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
718 ccl_qual_rm(&bibset);
723 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
725 s->sort_spec = yaz_sort_spec(s->odr, criteria);
728 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
732 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
736 static zoom_ret do_write(ZOOM_connection c);
739 ZOOM_connection_destroy(ZOOM_connection c)
744 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
747 for (r = c->resultsets; r; r = r->next)
753 odr_destroy(c->odr_in);
754 odr_destroy(c->odr_out);
755 ZOOM_options_destroy(c->options);
756 ZOOM_connection_remove_tasks(c);
757 ZOOM_connection_remove_events(c);
763 xfree(c->cookie_out);
772 void ZOOM_resultset_addref(ZOOM_resultset r)
777 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
782 ZOOM_resultset ZOOM_resultset_create(void)
785 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
789 yaz_log(log_details, "%p ZOOM_resultset_create", r);
792 r->odr = odr_createmem(ODR_ENCODE);
797 for (i = 0; i<RECORD_HASH_SIZE; i++)
798 r->record_hash[i] = 0;
803 r->databaseNames = 0;
804 r->num_databaseNames = 0;
809 ZOOM_API(ZOOM_resultset)
810 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
813 ZOOM_query s = ZOOM_query_create();
815 ZOOM_query_prefix(s, q);
817 r = ZOOM_connection_search(c, s);
818 ZOOM_query_destroy(s);
822 ZOOM_API(ZOOM_resultset)
823 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
825 ZOOM_resultset r = ZOOM_resultset_create();
829 const char *syntax, *elementSetName;
831 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
832 r->r_sort_spec = q->sort_spec;
835 r->options = ZOOM_options_create_with_parent(c->options);
838 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
840 r->rpn_iconv = yaz_iconv_open(cp, "UTF-8");
844 start = ZOOM_options_get_int(r->options, "start", 0);
845 count = ZOOM_options_get_int(r->options, "count", 0);
847 /* If "presentChunk" is defined use that; otherwise "step" */
848 const char *cp = ZOOM_options_get(r->options, "presentChunk");
849 r->step = ZOOM_options_get_int(r->options,
850 (cp != 0 ? "presentChunk": "step"), 0);
852 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
853 cp = ZOOM_options_get(r->options, "setname");
855 r->setname = xstrdup(cp);
856 cp = ZOOM_options_get(r->options, "schema");
858 r->schema = xstrdup(cp);
860 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
865 r->next = c->resultsets;
870 if (c->host_port && c->proto == PROTO_HTTP)
874 yaz_log(log_details, "ZOOM_connection_search: no comstack");
875 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
879 yaz_log(log_details, "ZOOM_connection_search: reconnect");
884 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
885 task->u.search.resultset = r;
886 task->u.search.start = start;
887 task->u.search.count = count;
889 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
890 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
891 elementSetName = ZOOM_options_get(r->options, "elementSetName");
892 task->u.search.elementSetName = elementSetName
893 ? xstrdup(elementSetName) : 0;
895 ZOOM_resultset_addref(r);
901 while (ZOOM_event(1, &c))
908 ZOOM_resultset_sort(ZOOM_resultset r,
909 const char *sort_type, const char *sort_spec)
911 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
915 ZOOM_resultset_sort1(ZOOM_resultset r,
916 const char *sort_type, const char *sort_spec)
918 ZOOM_connection c = r->connection;
922 newq = ZOOM_query_create();
923 if (ZOOM_query_sortby(newq, sort_spec) < 0)
926 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
927 r, r, sort_type, sort_spec);
931 if (c->host_port && c->proto == PROTO_HTTP)
935 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
936 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
940 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
946 ZOOM_resultset_cache_reset(r);
947 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
948 task->u.sort.resultset = r;
949 task->u.sort.q = newq;
951 ZOOM_resultset_addref(r);
955 while (ZOOM_event(1, &c))
963 ZOOM_resultset_cache_reset(ZOOM_resultset r)
966 for (i = 0; i<RECORD_HASH_SIZE; i++)
968 ZOOM_record_cache rc;
969 for (rc = r->record_hash[i]; rc; rc = rc->next)
971 if (rc->rec.wrbuf_marc)
972 wrbuf_destroy(rc->rec.wrbuf_marc);
973 if (rc->rec.wrbuf_iconv)
974 wrbuf_destroy(rc->rec.wrbuf_iconv);
975 if (rc->rec.wrbuf_opac)
976 wrbuf_destroy(rc->rec.wrbuf_opac);
978 r->record_hash[i] = 0;
983 ZOOM_resultset_destroy(ZOOM_resultset r)
985 resultset_destroy(r);
988 static void resultset_destroy(ZOOM_resultset r)
993 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
995 if (r->refcount == 0)
997 ZOOM_resultset_cache_reset(r);
1001 /* remove ourselves from the resultsets in connection */
1002 ZOOM_resultset *rp = &r->connection->resultsets;
1005 assert(*rp); /* we must be in this list!! */
1007 { /* OK, we're here - take us out of it */
1014 ZOOM_query_destroy(r->query);
1015 ZOOM_options_destroy(r->options);
1017 yaz_iconv_close(r->rpn_iconv);
1018 odr_destroy(r->odr);
1026 ZOOM_resultset_size(ZOOM_resultset r)
1028 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
1033 static void do_close(ZOOM_connection c)
1038 ZOOM_connection_set_mask(c, 0);
1039 c->state = STATE_IDLE;
1042 static int ZOOM_test_reconnect(ZOOM_connection c)
1046 if (!c->reconnect_ok)
1049 c->reconnect_ok = 0;
1050 c->tasks->running = 0;
1051 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1053 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1054 ZOOM_connection_put_event(c, event);
1059 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1060 int force_sync, int start, int count)
1065 const char *syntax, *elementSetName;
1069 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1070 " count=%d", r, force_sync, start, count);
1075 if (c->host_port && c->proto == PROTO_HTTP)
1079 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1080 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1084 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1086 c->reconnect_ok = 1;
1089 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1090 task->u.retrieve.resultset = r;
1091 task->u.retrieve.start = start;
1092 task->u.retrieve.count = count;
1094 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1095 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1096 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1097 task->u.retrieve.elementSetName = elementSetName
1098 ? xstrdup(elementSetName) : 0;
1100 cp = ZOOM_options_get(r->options, "schema");
1103 if (!r->schema || strcmp(r->schema, cp))
1106 r->schema = xstrdup(cp);
1110 ZOOM_resultset_addref(r);
1112 if (!r->connection->async || force_sync)
1113 while (r->connection && ZOOM_event(1, &r->connection))
1118 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1119 size_t start, size_t count)
1121 int force_present = 0;
1125 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1126 r, r, (long) start, (long) count);
1129 ZOOM_resultset_retrieve(r, force_present, start, count);
1133 for (i = 0; i< count; i++)
1134 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1138 static void get_cert(ZOOM_connection c)
1143 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1145 ZOOM_connection_option_setl(c, "sslPeerCert",
1146 cert_buf, cert_len);
1151 static zoom_ret do_connect(ZOOM_connection c)
1154 const char *effective_host;
1157 effective_host = c->proxy;
1159 effective_host = c->host_port;
1161 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1165 c->cs = cs_create_host(effective_host, 0, &add);
1167 if (c->cs && c->cs->protocol == PROTO_HTTP)
1170 const char *path = 0;
1172 c->proto = PROTO_HTTP;
1173 cs_get_host_args(c->host_port, &path);
1175 c->path = (char*) xmalloc(strlen(path)+2);
1177 strcpy(c->path+1, path);
1179 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1181 return zoom_complete;
1186 int ret = cs_connect(c->cs, add);
1189 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1190 ZOOM_connection_put_event(c, event);
1192 if (c->proto == PROTO_Z3950)
1193 ZOOM_connection_send_init(c);
1196 /* no init request for SRW .. */
1197 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1198 ZOOM_connection_remove_task(c);
1199 ZOOM_connection_set_mask(c, 0);
1200 ZOOM_connection_exec_task(c);
1202 c->state = STATE_ESTABLISHED;
1203 return zoom_pending;
1207 int mask = ZOOM_SELECT_EXCEPT;
1208 if (c->cs->io_pending & CS_WANT_WRITE)
1209 mask += ZOOM_SELECT_WRITE;
1210 if (c->cs->io_pending & CS_WANT_READ)
1211 mask += ZOOM_SELECT_READ;
1212 ZOOM_connection_set_mask(c, mask);
1213 c->state = STATE_CONNECTING;
1214 return zoom_pending;
1217 c->state = STATE_IDLE;
1218 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1219 return zoom_complete;
1222 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1225 for (i = 0; i<200; i++)
1229 Z_OtherInformation **oi;
1234 sprintf(buf, "otherInfo%d", i);
1235 val = ZOOM_options_get(c->options, buf);
1238 cp = strchr(val, ':');
1242 if (len >= sizeof(buf))
1243 len = sizeof(buf)-1;
1244 memcpy(buf, val, len);
1247 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1252 yaz_oi_APDU(a, &oi);
1253 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1257 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1262 Z_OtherInformation **oi;
1263 yaz_oi_APDU(a, &oi);
1264 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1269 Z_OtherInformation **oi;
1270 yaz_oi_APDU(a, &oi);
1271 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1274 otherInfo_attach(c, a, out);
1275 if (!z_APDU(out, &a, 0, 0))
1277 FILE *outf = fopen("/tmp/apdu.txt", "a");
1280 ODR odr_pr = odr_createmem(ODR_PRINT);
1281 fprintf(outf, "a=%p\n", a);
1282 odr_setprint(odr_pr, outf);
1283 z_APDU(odr_pr, &a, 0, 0);
1284 odr_destroy(odr_pr);
1286 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1287 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1291 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1295 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1299 if (encode_APDU(c, a, c->odr_out))
1300 return zoom_complete;
1301 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1302 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1303 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1304 ZOOM_connection_put_event(c, event);
1305 odr_reset(c->odr_out);
1309 /* returns 1 if PDU was sent OK (still pending )
1310 0 if PDU was not sent OK (nothing to wait for)
1313 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1315 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1316 Z_InitRequest *ireq = apdu->u.initRequest;
1317 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1318 odr_malloc(c->odr_out, sizeof(*auth));
1321 ODR_MASK_SET(ireq->options, Z_Options_search);
1322 ODR_MASK_SET(ireq->options, Z_Options_present);
1323 ODR_MASK_SET(ireq->options, Z_Options_scan);
1324 ODR_MASK_SET(ireq->options, Z_Options_sort);
1325 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1326 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1328 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1329 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1330 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1332 /* Index Data's Z39.50 Implementor Id is 81 */
1333 ireq->implementationId =
1334 odr_prepend(c->odr_out,
1335 ZOOM_options_get(c->options, "implementationId"),
1336 odr_prepend(c->odr_out, "81", ireq->implementationId));
1338 ireq->implementationName =
1339 odr_prepend(c->odr_out,
1340 ZOOM_options_get(c->options, "implementationName"),
1341 odr_prepend(c->odr_out, "ZOOM-C",
1342 ireq->implementationName));
1344 version = odr_strdup(c->odr_out, "$Revision: 1.130 $");
1345 if (strlen(version) > 10) /* check for unexpanded CVS strings */
1346 version[strlen(version)-2] = '\0';
1347 ireq->implementationVersion =
1348 odr_prepend(c->odr_out,
1349 ZOOM_options_get(c->options, "implementationVersion"),
1350 odr_prepend(c->odr_out, &version[11],
1351 ireq->implementationVersion));
1353 *ireq->maximumRecordSize = c->maximum_record_size;
1354 *ireq->preferredMessageSize = c->preferred_message_size;
1356 if (c->group || c->password)
1358 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1361 pass->groupId = odr_strdup(c->odr_out, c->group);
1364 pass->userId = odr_strdup(c->odr_out, c->user);
1367 pass->password = odr_strdup(c->odr_out, c->password);
1368 auth->which = Z_IdAuthentication_idPass;
1369 auth->u.idPass = pass;
1370 ireq->idAuthentication = auth;
1374 auth->which = Z_IdAuthentication_open;
1375 auth->u.open = odr_strdup(c->odr_out, c->user);
1376 ireq->idAuthentication = auth;
1380 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1381 yaz_oid_userinfo_proxy, 1, c->host_port);
1383 if (c->charset || c->lang)
1385 Z_OtherInformation **oi;
1386 Z_OtherInformationUnit *oi_unit;
1388 yaz_oi_APDU(apdu, &oi);
1390 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1392 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1393 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1394 oi_unit->information.externallyDefinedInfo =
1395 yaz_set_proposal_charneg_list(c->odr_out, " ",
1396 c->charset, c->lang, 1);
1400 return send_APDU(c, apdu);
1404 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1409 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
1411 if (c->sru_mode == zoom_sru_get)
1413 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1415 else if (c->sru_mode == zoom_sru_post)
1417 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1419 else if (c->sru_mode == zoom_sru_soap)
1421 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1423 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1424 return zoom_complete;
1425 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1427 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1428 ZOOM_connection_put_event(c, event);
1429 odr_reset(c->odr_out);
1435 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1439 ZOOM_resultset resultset = 0;
1441 const char *option_val = 0;
1443 if (c->error) /* don't continue on error */
1444 return zoom_complete;
1446 switch(c->tasks->which)
1448 case ZOOM_TASK_SEARCH:
1449 resultset = c->tasks->u.search.resultset;
1450 resultset->setname = xstrdup("default");
1451 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1452 start = &c->tasks->u.search.start;
1453 count = &c->tasks->u.search.count;
1455 case ZOOM_TASK_RETRIEVE:
1456 resultset = c->tasks->u.retrieve.resultset;
1458 start = &c->tasks->u.retrieve.start;
1459 count = &c->tasks->u.retrieve.count;
1461 if (*start >= resultset->size)
1462 return zoom_complete;
1463 if (*start + *count > resultset->size)
1464 *count = resultset->size - *start;
1466 for (i = 0; i < *count; i++)
1469 record_cache_lookup(resultset, i + *start,
1470 c->tasks->u.retrieve.syntax,
1471 c->tasks->u.retrieve.elementSetName);
1476 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1477 ZOOM_connection_put_event(c, event);
1484 return zoom_complete;
1487 return zoom_complete;
1489 assert(resultset->query);
1491 sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
1493 if (resultset->query->z_query->which == Z_Query_type_104
1494 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1496 sr->u.request->query_type = Z_SRW_query_type_cql;
1497 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1499 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1500 resultset->query->z_query->u.type_1)
1502 sr->u.request->query_type = Z_SRW_query_type_pqf;
1503 sr->u.request->query.pqf = resultset->query->query_string;
1507 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1508 return zoom_complete;
1510 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1511 sr->u.request->maximumRecords = odr_intdup(
1512 c->odr_out, resultset->step>0 ? resultset->step : *count);
1513 sr->u.request->recordSchema = resultset->schema;
1515 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1517 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1519 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1521 sr->extra_args = odr_strdup(c->odr_out, option_val);
1522 return send_srw(c, sr);
1525 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1527 return zoom_complete;
1531 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1534 int lslb, ssub, mspn;
1536 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1537 Z_SearchRequest *search_req = apdu->u.searchRequest;
1538 const char *elementSetName;
1539 const char *smallSetElementSetName;
1540 const char *mediumSetElementSetName;
1543 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1545 r = c->tasks->u.search.resultset;
1547 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1550 ZOOM_options_get(r->options, "elementSetName");
1551 smallSetElementSetName =
1552 ZOOM_options_get(r->options, "smallSetElementSetName");
1553 mediumSetElementSetName =
1554 ZOOM_options_get(r->options, "mediumSetElementSetName");
1556 if (!smallSetElementSetName)
1557 smallSetElementSetName = elementSetName;
1559 if (!mediumSetElementSetName)
1560 mediumSetElementSetName = elementSetName;
1565 /* prepare query for the search request */
1566 search_req->query = r->query->z_query;
1567 if (!search_req->query)
1569 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1570 return zoom_complete;
1572 if (r->query->z_query->which == Z_Query_type_1 && r->rpn_iconv)
1574 search_req->query = yaz_copy_Z_Query(r->query->z_query, c->odr_out);
1576 yaz_query_charset_convert_rpnquery(search_req->query->u.type_1,
1577 c->odr_out, r->rpn_iconv);
1579 search_req->databaseNames = r->databaseNames;
1580 search_req->num_databaseNames = r->num_databaseNames;
1582 /* get syntax (no need to provide unless piggyback is in effect) */
1583 syntax = c->tasks->u.search.syntax;
1585 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1586 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1587 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1588 if (lslb != -1 && ssub != -1 && mspn != -1)
1590 /* So're a Z39.50 expert? Let's hope you don't do sort */
1591 *search_req->largeSetLowerBound = lslb;
1592 *search_req->smallSetUpperBound = ssub;
1593 *search_req->mediumSetPresentNumber = mspn;
1595 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1596 && r->piggyback && !r->r_sort_spec && !r->schema)
1598 /* Regular piggyback - do it unless we're going to do sort */
1599 *search_req->largeSetLowerBound = 2000000000;
1600 *search_req->smallSetUpperBound = 1;
1601 *search_req->mediumSetPresentNumber =
1602 r->step>0 ? r->step : c->tasks->u.search.count;
1606 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1607 smallSetElementSetName = 0;
1608 mediumSetElementSetName = 0;
1611 if (smallSetElementSetName && *smallSetElementSetName)
1613 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1614 odr_malloc(c->odr_out, sizeof(*esn));
1616 esn->which = Z_ElementSetNames_generic;
1617 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1618 search_req->smallSetElementSetNames = esn;
1620 if (mediumSetElementSetName && *mediumSetElementSetName)
1622 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1623 odr_malloc(c->odr_out, sizeof(*esn));
1625 esn->which = Z_ElementSetNames_generic;
1626 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1627 search_req->mediumSetElementSetNames = esn;
1630 search_req->preferredRecordSyntax =
1631 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1635 if (c->support_named_resultsets)
1639 /* find the lowest unused ordinal so that we re-use
1640 result sets on the server. */
1641 for (ord = 1; ; ord++)
1644 sprintf(setname, "%d", ord);
1645 for (rp = c->resultsets; rp; rp = rp->next)
1646 if (rp->setname && !strcmp(rp->setname, setname))
1651 r->setname = xstrdup(setname);
1652 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1653 "set %s", c, r->setname);
1657 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1659 r->setname = xstrdup("default");
1661 ZOOM_options_set(r->options, "setname", r->setname);
1663 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1664 return send_APDU(c, apdu);
1667 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1674 case Z_DefaultDiagFormat_v2Addinfo:
1675 addinfo = r->u.v2Addinfo;
1677 case Z_DefaultDiagFormat_v3Addinfo:
1678 addinfo = r->u.v3Addinfo;
1683 set_dset_error(c, *r->condition,
1684 yaz_oid_to_string(yaz_oid_std(),
1685 r->diagnosticSetId, &oclass),
1689 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1691 if (p->which != Z_DiagRec_defaultFormat)
1692 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1694 response_default_diag(c, p->u.defaultFormat);
1697 ZOOM_API(ZOOM_record)
1698 ZOOM_record_clone(ZOOM_record srec)
1705 odr_enc = odr_createmem(ODR_ENCODE);
1706 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1708 buf = odr_getbuf(odr_enc, &size, 0);
1710 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1711 nrec->odr = odr_createmem(ODR_DECODE);
1712 nrec->wrbuf_marc = 0;
1713 nrec->wrbuf_iconv = 0;
1714 nrec->wrbuf_opac = 0;
1715 odr_setbuf(nrec->odr, buf, size, 0);
1716 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1718 odr_destroy(odr_enc);
1722 ZOOM_API(ZOOM_record)
1723 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1725 const char *syntax =
1726 ZOOM_options_get(s->options, "preferredRecordSyntax");
1727 const char *elementSetName =
1728 ZOOM_options_get(s->options, "elementSetName");
1730 return record_cache_lookup(s, pos, syntax, elementSetName);
1733 ZOOM_API(ZOOM_record)
1734 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1736 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1741 * MIKE: I think force_sync should always be zero, but I don't
1742 * want to make this change until I get the go-ahead from
1743 * Adam, in case something depends on the old synchronous
1747 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1748 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1749 rec = ZOOM_resultset_record_immediate(r, pos);
1755 ZOOM_record_destroy(ZOOM_record rec)
1759 if (rec->wrbuf_marc)
1760 wrbuf_destroy(rec->wrbuf_marc);
1761 if (rec->wrbuf_iconv)
1762 wrbuf_destroy(rec->wrbuf_iconv);
1763 if (rec->wrbuf_opac)
1764 wrbuf_destroy(rec->wrbuf_opac);
1765 odr_destroy(rec->odr);
1769 static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
1771 const char *buf, int sz,
1772 const char *record_charset)
1777 yaz_marc_t mt = yaz_marc_create();
1780 strcpy(to, "UTF-8");
1781 if (record_charset && *record_charset)
1783 /* Use "from,to" or just "from" */
1784 const char *cp = strchr(record_charset, ',');
1785 int clen = strlen(record_charset);
1788 strncpy( to, cp+1, sizeof(to)-1);
1789 to[sizeof(to)-1] = '\0';
1790 clen = cp - record_charset;
1792 if (clen > sizeof(from)-1)
1793 clen = sizeof(from)-1;
1796 strncpy(from, record_charset, clen);
1802 cd = yaz_iconv_open(to, from);
1803 yaz_marc_iconv(mt, cd);
1806 yaz_marc_xml(mt, marc_type);
1807 if (!rec->wrbuf_marc)
1808 rec->wrbuf_marc = wrbuf_alloc();
1809 wrbuf_rewind(rec->wrbuf_marc);
1810 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
1812 yaz_marc_destroy(mt);
1814 yaz_iconv_close(cd);
1816 *len = wrbuf_len(rec->wrbuf_marc);
1817 return wrbuf_cstr(rec->wrbuf_marc);
1819 yaz_marc_destroy(mt);
1821 yaz_iconv_close(cd);
1825 static const char *record_iconv_return(ZOOM_record rec, int *len,
1826 const char *buf, int sz,
1827 const char *record_charset)
1834 strcpy(to, "UTF-8");
1836 if (record_charset && *record_charset)
1838 /* Use "from,to" or just "from" */
1839 const char *cp = strchr(record_charset, ',');
1840 int clen = strlen(record_charset);
1843 strncpy( to, cp+1, sizeof(to)-1);
1844 to[sizeof(to)-1] = '\0';
1845 clen = cp - record_charset;
1847 if (clen > sizeof(from)-1)
1848 clen = sizeof(from)-1;
1851 strncpy(from, record_charset, clen);
1855 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1857 if (!rec->wrbuf_iconv)
1858 rec->wrbuf_iconv = wrbuf_alloc();
1860 wrbuf_rewind(rec->wrbuf_iconv);
1862 wrbuf_iconv_write(rec->wrbuf_iconv, cd, buf, sz);
1863 wrbuf_iconv_reset(rec->wrbuf_iconv, cd);
1865 buf = wrbuf_cstr(rec->wrbuf_iconv);
1866 sz = wrbuf_len(rec->wrbuf_iconv);
1867 yaz_iconv_close(cd);
1875 ZOOM_record_error(ZOOM_record rec, const char **cp,
1876 const char **addinfo, const char **diagset)
1878 Z_NamePlusRecord *npr;
1883 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
1885 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
1886 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
1887 const char *add = 0;
1889 if (diag_rec->which == Z_DiagRec_defaultFormat)
1891 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
1894 error = *ddf->condition;
1897 case Z_DefaultDiagFormat_v2Addinfo:
1898 add = ddf->u.v2Addinfo;
1900 case Z_DefaultDiagFormat_v3Addinfo:
1901 add = ddf->u.v3Addinfo;
1906 yaz_oid_to_string(yaz_oid_std(),
1907 ddf->diagnosticSetId, &oclass);
1915 *addinfo = add ? add : "";
1917 *cp = diagbib1_str(error);
1923 ZOOM_API(const char *)
1924 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
1931 Z_NamePlusRecord *npr;
1934 *len = 0; /* default return */
1943 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
1945 if (cp[i] == ';' || cp[i] == ' ')
1951 while (type_spec[i] == ';')
1954 while (type_spec[i] == ' ')
1956 if (!strncmp(type_spec+i, "charset=", 8))
1959 i = i + 8; /* skip charset= */
1960 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
1962 if (type_spec[i] == ';' || type_spec[i] == ' ')
1968 else if (!strncmp(type_spec+i, "xpath=", 6))
1972 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
1976 while (type_spec[i] == ' ')
1979 if (!strcmp(type, "database"))
1982 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1983 return npr->databaseName;
1985 else if (!strcmp(type, "syntax"))
1987 const char *desc = 0;
1988 if (npr->which == Z_NamePlusRecord_databaseRecord)
1990 Z_External *r = (Z_External *) npr->u.databaseRecord;
1991 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
1996 *len = strlen(desc);
1999 if (npr->which != Z_NamePlusRecord_databaseRecord)
2002 /* from now on - we have a database record .. */
2003 if (!strcmp(type, "render"))
2005 Z_External *r = (Z_External *) npr->u.databaseRecord;
2006 const Odr_oid *oid = r->direct_reference;
2008 /* render bibliographic record .. */
2009 if (r->which == Z_External_OPAC)
2011 r = r->u.opac->bibliographicRecord;
2014 oid = r->direct_reference;
2016 if (r->which == Z_External_sutrs)
2017 return record_iconv_return(rec, len,
2018 (char*) r->u.sutrs->buf,
2021 else if (r->which == Z_External_octet)
2023 if (yaz_oid_is_iso2709(oid))
2025 const char *ret_buf = marc_iconv_return(
2026 rec, YAZ_MARC_LINE, len,
2027 (const char *) r->u.octet_aligned->buf,
2028 r->u.octet_aligned->len,
2033 return record_iconv_return(rec, len,
2034 (const char *) r->u.octet_aligned->buf,
2035 r->u.octet_aligned->len,
2038 else if (r->which == Z_External_grs1)
2040 if (!rec->wrbuf_marc)
2041 rec->wrbuf_marc = wrbuf_alloc();
2042 wrbuf_rewind(rec->wrbuf_marc);
2043 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
2044 return record_iconv_return(rec, len,
2045 wrbuf_buf(rec->wrbuf_marc),
2046 wrbuf_len(rec->wrbuf_marc),
2051 else if (!strcmp(type, "xml"))
2053 Z_External *r = (Z_External *) npr->u.databaseRecord;
2054 const Odr_oid *oid = r->direct_reference;
2056 /* render bibliographic record .. */
2057 if (r->which == Z_External_OPAC)
2059 r = r->u.opac->bibliographicRecord;
2062 oid = r->direct_reference;
2065 if (r->which == Z_External_sutrs)
2066 return record_iconv_return(rec, len,
2067 (const char *) r->u.sutrs->buf,
2070 else if (r->which == Z_External_octet)
2072 int marc_decode_type = YAZ_MARC_MARCXML;
2073 if (yaz_oid_is_iso2709(oid))
2075 const char *ret_buf = marc_iconv_return(
2076 rec, marc_decode_type, len,
2077 (const char *) r->u.octet_aligned->buf,
2078 r->u.octet_aligned->len,
2083 return record_iconv_return(rec, len,
2084 (const char *) r->u.octet_aligned->buf,
2085 r->u.octet_aligned->len,
2088 else if (r->which == Z_External_grs1)
2095 else if (!strcmp(type, "raw"))
2097 Z_External *r = (Z_External *) npr->u.databaseRecord;
2099 if (r->which == Z_External_sutrs)
2101 if (len) *len = r->u.sutrs->len;
2102 return (const char *) r->u.sutrs->buf;
2104 else if (r->which == Z_External_octet)
2106 if (len) *len = r->u.octet_aligned->len;
2107 return (const char *) r->u.octet_aligned->buf;
2109 else /* grs-1, explain, OPAC, ... */
2112 return (const char *) npr->u.databaseRecord;
2116 else if (!strcmp (type, "ext"))
2119 return (const char *) npr->u.databaseRecord;
2121 else if (!strcmp (type, "opac"))
2124 Z_External *r = (Z_External *) npr->u.databaseRecord;
2125 if (r->which == Z_External_OPAC)
2127 if (!rec->wrbuf_opac)
2128 rec->wrbuf_opac = wrbuf_alloc();
2129 wrbuf_rewind(rec->wrbuf_opac);
2130 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
2131 return record_iconv_return(rec, len,
2132 wrbuf_buf(rec->wrbuf_opac),
2133 wrbuf_len(rec->wrbuf_opac),
2140 static int strcmp_null(const char *v1, const char *v2)
2146 return strcmp(v1, v2);
2149 static size_t record_hash(int pos)
2153 return pos % RECORD_HASH_SIZE;
2156 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2158 const char *syntax, const char *elementSetName)
2160 ZOOM_record_cache rc;
2162 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2163 ZOOM_connection_put_event(r->connection, event);
2165 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2169 if (strcmp_null(r->schema, rc->schema))
2171 if (strcmp_null(elementSetName,rc->elementSetName))
2173 if (strcmp_null(syntax, rc->syntax))
2175 /* not destroying rc->npr (it's handled by nmem )*/
2177 /* keeping wrbuf_marc too */
2181 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2184 rc->rec.wrbuf_marc = 0;
2185 rc->rec.wrbuf_iconv = 0;
2186 rc->rec.wrbuf_opac = 0;
2188 rc->elementSetName = odr_strdup(r->odr, elementSetName);
2190 rc->elementSetName = 0;
2193 rc->syntax = odr_strdup(r->odr, syntax);
2198 rc->schema = odr_strdup(r->odr, r->schema);
2203 rc->next = r->record_hash[record_hash(pos)];
2204 r->record_hash[record_hash(pos)] = rc;
2207 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2209 const char *elementSetName)
2211 ZOOM_record_cache rc;
2213 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2217 if (strcmp_null(r->schema, rc->schema))
2219 if (strcmp_null(elementSetName,rc->elementSetName))
2221 if (strcmp_null(syntax, rc->syntax))
2229 static void handle_records(ZOOM_connection c, Z_Records *sr,
2232 ZOOM_resultset resultset;
2234 const char *syntax = 0, *elementSetName = 0;
2238 switch (c->tasks->which)
2240 case ZOOM_TASK_SEARCH:
2241 resultset = c->tasks->u.search.resultset;
2242 start = &c->tasks->u.search.start;
2243 count = &c->tasks->u.search.count;
2244 syntax = c->tasks->u.search.syntax;
2245 elementSetName = c->tasks->u.search.elementSetName;
2247 case ZOOM_TASK_RETRIEVE:
2248 resultset = c->tasks->u.retrieve.resultset;
2249 start = &c->tasks->u.retrieve.start;
2250 count = &c->tasks->u.retrieve.count;
2251 syntax = c->tasks->u.retrieve.syntax;
2252 elementSetName = c->tasks->u.retrieve.elementSetName;
2257 if (sr && sr->which == Z_Records_NSD)
2258 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2259 else if (sr && sr->which == Z_Records_multipleNSD)
2261 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2262 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2264 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2268 if (*count + *start > resultset->size)
2269 *count = resultset->size - *start;
2272 if (sr && sr->which == Z_Records_DBOSD)
2275 NMEM nmem = odr_extract_mem(c->odr_in);
2276 Z_NamePlusRecordList *p =
2277 sr->u.databaseOrSurDiagnostics;
2278 for (i = 0; i<p->num_records; i++)
2280 record_cache_add(resultset, p->records[i], i + *start,
2281 syntax, elementSetName);
2287 yaz_log(log_details,
2288 "handle_records resultset=%p start=%d count=%d",
2289 resultset, *start, *count);
2291 /* transfer our response to search_nmem .. we need it later */
2292 nmem_transfer(odr_getmem(resultset->odr), nmem);
2294 if (present_phase && p->num_records == 0)
2296 /* present response and we didn't get any records! */
2297 Z_NamePlusRecord *myrec =
2298 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2299 record_cache_add(resultset, myrec, *start,
2300 syntax, elementSetName);
2303 else if (present_phase)
2305 /* present response and we didn't get any records! */
2306 Z_NamePlusRecord *myrec =
2307 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2308 record_cache_add(resultset, myrec, *start, syntax, elementSetName);
2313 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2315 handle_records(c, pr->records, 1);
2318 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2321 switch (term->which)
2323 case Z_Term_general:
2324 ZOOM_options_setl(opt, name,
2325 (const char *)(term->u.general->buf),
2326 term->u.general->len);
2328 case Z_Term_characterString:
2329 ZOOM_options_set(opt, name, term->u.characterString);
2331 case Z_Term_numeric:
2332 ZOOM_options_set_int(opt, name, *term->u.numeric);
2337 static void handle_queryExpression(ZOOM_options opt, const char *name,
2338 Z_QueryExpression *exp)
2344 case Z_QueryExpression_term:
2345 if (exp->u.term && exp->u.term->queryTerm)
2347 sprintf(opt_name, "%s.term", name);
2348 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2351 case Z_QueryExpression_query:
2356 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2357 Z_OtherInformation *o)
2360 for (i = 0; o && i < o->num_elements; i++)
2362 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2364 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2366 if (ext->which == Z_External_searchResult1)
2369 Z_SearchInfoReport *sr = ext->u.searchResult1;
2372 ZOOM_options_set_int(
2373 resultset->options, "searchresult.size", sr->num);
2375 for (j = 0; j < sr->num; j++)
2377 Z_SearchInfoReport_s *ent =
2378 ext->u.searchResult1->elements[j];
2381 sprintf(pref, "searchresult.%d", j);
2383 if (ent->subqueryId)
2386 sprintf(opt_name, "%s.id", pref);
2387 ZOOM_options_set(resultset->options, opt_name,
2390 if (ent->subqueryExpression)
2393 sprintf(opt_name, "%s.subquery", pref);
2394 handle_queryExpression(resultset->options, opt_name,
2395 ent->subqueryExpression);
2397 if (ent->subqueryInterpretation)
2400 sprintf(opt_name, "%s.interpretation", pref);
2401 handle_queryExpression(resultset->options, opt_name,
2402 ent->subqueryInterpretation);
2404 if (ent->subqueryRecommendation)
2407 sprintf(opt_name, "%s.recommendation", pref);
2408 handle_queryExpression(resultset->options, opt_name,
2409 ent->subqueryRecommendation);
2411 if (ent->subqueryCount)
2414 sprintf(opt_name, "%s.count", pref);
2415 ZOOM_options_set_int(resultset->options, opt_name,
2416 *ent->subqueryCount);
2424 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2426 ZOOM_resultset resultset;
2429 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2432 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2433 ZOOM_connection_put_event(c, event);
2435 resultset = c->tasks->u.search.resultset;
2437 if (sr->resultSetStatus)
2439 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2440 *sr->resultSetStatus);
2442 if (sr->presentStatus)
2444 ZOOM_options_set_int(resultset->options, "presentStatus",
2445 *sr->presentStatus);
2447 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2449 resultset->size = *sr->resultCount;
2450 handle_records(c, sr->records, 0);
2453 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2455 if (res->diagnostics && res->num_diagnostics > 0)
2456 response_diag(c, res->diagnostics[0]);
2459 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2461 NMEM nmem = odr_extract_mem(c->odr_in);
2464 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2466 scan = c->tasks->u.scan.scan;
2468 if (res->entries && res->entries->nonsurrogateDiagnostics)
2469 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2470 scan->scan_response = res;
2471 nmem_transfer(odr_getmem(scan->odr), nmem);
2473 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2474 if (res->positionOfTerm)
2475 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2476 if (res->scanStatus)
2477 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2478 if (res->numberOfEntriesReturned)
2479 ZOOM_options_set_int(scan->options, "number",
2480 *res->numberOfEntriesReturned);
2485 static zoom_ret send_sort(ZOOM_connection c,
2486 ZOOM_resultset resultset)
2489 resultset->r_sort_spec = 0;
2490 if (resultset->r_sort_spec)
2492 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2493 Z_SortRequest *req = apdu->u.sortRequest;
2495 req->num_inputResultSetNames = 1;
2496 req->inputResultSetNames = (Z_InternationalString **)
2497 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2498 req->inputResultSetNames[0] =
2499 odr_strdup(c->odr_out, resultset->setname);
2500 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2501 req->sortSequence = resultset->r_sort_spec;
2502 resultset->r_sort_spec = 0;
2503 return send_APDU(c, apdu);
2505 return zoom_complete;
2508 static zoom_ret send_present(ZOOM_connection c)
2511 Z_PresentRequest *req = 0;
2513 const char *syntax = 0;
2514 const char *elementSetName = 0;
2515 ZOOM_resultset resultset;
2520 yaz_log(log_details, "%p send_present no tasks", c);
2521 return zoom_complete;
2524 switch (c->tasks->which)
2526 case ZOOM_TASK_SEARCH:
2527 resultset = c->tasks->u.search.resultset;
2528 start = &c->tasks->u.search.start;
2529 count = &c->tasks->u.search.count;
2530 syntax = c->tasks->u.search.syntax;
2531 elementSetName = c->tasks->u.search.elementSetName;
2533 case ZOOM_TASK_RETRIEVE:
2534 resultset = c->tasks->u.retrieve.resultset;
2535 start = &c->tasks->u.retrieve.start;
2536 count = &c->tasks->u.retrieve.count;
2537 syntax = c->tasks->u.retrieve.syntax;
2538 elementSetName = c->tasks->u.retrieve.elementSetName;
2541 return zoom_complete;
2543 yaz_log(log_details, "%p send_present start=%d count=%d",
2546 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2548 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2551 if (c->error) /* don't continue on error */
2552 return zoom_complete;
2553 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2554 resultset, *start, *count);
2556 for (i = 0; i < *count; i++)
2559 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2564 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2565 ZOOM_connection_put_event(c, event);
2573 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2574 return zoom_complete;
2577 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2578 req = apdu->u.presentRequest;
2581 yaz_log(log_details, "%p send_present skip=%d", c, i);
2583 *req->resultSetStartPoint = *start + 1;
2584 *req->numberOfRecordsRequested = resultset->step>0 ?
2585 resultset->step : *count;
2586 if (*req->numberOfRecordsRequested + *start > resultset->size)
2587 *req->numberOfRecordsRequested = resultset->size - *start;
2588 assert(*req->numberOfRecordsRequested > 0);
2590 if (syntax && *syntax)
2591 req->preferredRecordSyntax =
2592 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2594 if (resultset->schema && *resultset->schema)
2596 Z_RecordComposition *compo = (Z_RecordComposition *)
2597 odr_malloc(c->odr_out, sizeof(*compo));
2599 req->recordComposition = compo;
2600 compo->which = Z_RecordComp_complex;
2601 compo->u.complex = (Z_CompSpec *)
2602 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2603 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2604 odr_malloc(c->odr_out, sizeof(bool_t));
2605 *compo->u.complex->selectAlternativeSyntax = 0;
2607 compo->u.complex->generic = (Z_Specification *)
2608 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2610 compo->u.complex->generic->which = Z_Schema_oid;
2611 compo->u.complex->generic->schema.oid = (Odr_oid *)
2612 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2614 if (!compo->u.complex->generic->schema.oid)
2616 /* OID wasn't a schema! Try record syntax instead. */
2618 compo->u.complex->generic->schema.oid = (Odr_oid *)
2619 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2621 if (elementSetName && *elementSetName)
2623 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2624 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2625 compo->u.complex->generic->elementSpec->which =
2626 Z_ElementSpec_elementSetName;
2627 compo->u.complex->generic->elementSpec->u.elementSetName =
2628 odr_strdup(c->odr_out, elementSetName);
2631 compo->u.complex->generic->elementSpec = 0;
2632 compo->u.complex->num_dbSpecific = 0;
2633 compo->u.complex->dbSpecific = 0;
2634 compo->u.complex->num_recordSyntax = 0;
2635 compo->u.complex->recordSyntax = 0;
2637 else if (elementSetName && *elementSetName)
2639 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2640 odr_malloc(c->odr_out, sizeof(*esn));
2641 Z_RecordComposition *compo = (Z_RecordComposition *)
2642 odr_malloc(c->odr_out, sizeof(*compo));
2644 esn->which = Z_ElementSetNames_generic;
2645 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2646 compo->which = Z_RecordComp_simple;
2647 compo->u.simple = esn;
2648 req->recordComposition = compo;
2650 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2651 return send_APDU(c, apdu);
2654 ZOOM_API(ZOOM_scanset)
2655 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2658 ZOOM_query q = ZOOM_query_create();
2660 ZOOM_query_prefix(q, start);
2662 s = ZOOM_connection_scan1(c, q);
2663 ZOOM_query_destroy(q);
2668 ZOOM_API(ZOOM_scanset)
2669 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2673 ZOOM_scanset scan = 0;
2676 * We need to check the query-type, so we can recognise CQL and
2677 * CCL and compile them into a form that we can use here. The
2678 * ZOOM_query structure has no explicit `type' member, but
2679 * inspection of the ZOOM_query_prefix() and ZOOM_query_cql()
2680 * functions shows how the structure is set up in each case.
2684 else if (q->z_query->which == Z_Query_type_1)
2686 yaz_log(log_api, "%p ZOOM_connection_scan1 q=%p PQF '%s'",
2687 c, q, q->query_string);
2688 start = q->query_string;
2690 else if (q->z_query->which == Z_Query_type_104)
2692 yaz_log(log_api, "%p ZOOM_connection_scan1 q=%p CQL '%s'",
2693 c, q, q->query_string);
2694 start = freeme = cql2pqf(c, q->query_string);
2700 yaz_log(YLOG_FATAL, "%p ZOOM_connection_scan1 q=%p unknown type '%s'",
2701 c, q, q->query_string);
2705 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2706 scan->connection = c;
2707 scan->odr = odr_createmem(ODR_DECODE);
2708 scan->options = ZOOM_options_create_with_parent(c->options);
2710 scan->scan_response = 0;
2711 scan->termListAndStartPoint =
2712 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet, start);
2716 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2719 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2722 yaz_query_charset_convert_apt(scan->termListAndStartPoint,
2729 scan->databaseNames = set_DatabaseNames(c, c->options,
2730 &scan->num_databaseNames,
2732 if (scan->termListAndStartPoint != 0)
2734 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2735 task->u.scan.scan = scan;
2740 while (ZOOM_event(1, &c))
2748 ZOOM_scanset_destroy(ZOOM_scanset scan)
2753 if (scan->refcount == 0)
2755 odr_destroy(scan->odr);
2757 ZOOM_options_destroy(scan->options);
2762 static zoom_ret send_package(ZOOM_connection c)
2766 yaz_log(log_details, "%p send_package", c);
2768 return zoom_complete;
2769 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2771 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2772 ZOOM_connection_put_event(c, event);
2774 c->buf_out = c->tasks->u.package->buf_out;
2775 c->len_out = c->tasks->u.package->len_out;
2780 static zoom_ret send_scan(ZOOM_connection c)
2783 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2784 Z_ScanRequest *req = apdu->u.scanRequest;
2786 yaz_log(log_details, "%p send_scan", c);
2788 return zoom_complete;
2789 assert (c->tasks->which == ZOOM_TASK_SCAN);
2790 scan = c->tasks->u.scan.scan;
2792 req->termListAndStartPoint = scan->termListAndStartPoint;
2793 req->attributeSet = scan->attributeSet;
2795 *req->numberOfTermsRequested =
2796 ZOOM_options_get_int(scan->options, "number", 10);
2798 req->preferredPositionInResponse =
2799 odr_intdup(c->odr_out,
2800 ZOOM_options_get_int(scan->options, "position", 1));
2803 odr_intdup(c->odr_out,
2804 ZOOM_options_get_int(scan->options, "stepSize", 0));
2806 req->databaseNames = scan->databaseNames;
2807 req->num_databaseNames = scan->num_databaseNames;
2809 return send_APDU(c, apdu);
2813 ZOOM_scanset_size(ZOOM_scanset scan)
2815 if (!scan || !scan->scan_response || !scan->scan_response->entries)
2817 return scan->scan_response->entries->num_entries;
2820 ZOOM_API(const char *)
2821 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
2824 const char *term = 0;
2825 size_t noent = ZOOM_scanset_size(scan);
2826 Z_ScanResponse *res = scan->scan_response;
2832 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2834 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2836 if (t->term->which == Z_Term_general)
2838 term = (const char *) t->term->u.general->buf;
2839 *len = t->term->u.general->len;
2841 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2846 ZOOM_API(const char *)
2847 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
2850 const char *term = 0;
2851 size_t noent = ZOOM_scanset_size(scan);
2852 Z_ScanResponse *res = scan->scan_response;
2858 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2860 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2864 term = t->displayTerm;
2865 *len = strlen(term);
2867 else if (t->term->which == Z_Term_general)
2869 term = (const char *) t->term->u.general->buf;
2870 *len = t->term->u.general->len;
2872 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2877 ZOOM_API(const char *)
2878 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
2880 return ZOOM_options_get(scan->options, key);
2884 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
2887 ZOOM_options_set(scan->options, key, val);
2890 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
2893 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
2894 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2896 *req->function = Z_ExtendedServicesRequest_create;
2898 str = ZOOM_options_get(p->options, "package-name");
2900 req->packageName = odr_strdup(p->odr_out, str);
2902 str = ZOOM_options_get(p->options, "user-id");
2904 req->userId = odr_strdup(p->odr_out, str);
2906 req->packageType = odr_oiddup(p->odr_out, oid);
2908 str = ZOOM_options_get(p->options, "function");
2911 if (!strcmp (str, "create"))
2913 if (!strcmp (str, "delete"))
2915 if (!strcmp (str, "modify"))
2921 static const char *ill_array_lookup(void *clientData, const char *idx)
2923 ZOOM_package p = (ZOOM_package) clientData;
2924 return ZOOM_options_get(p->options, idx+4);
2927 static Z_External *encode_ill_request(ZOOM_package p)
2929 ODR out = p->odr_out;
2932 struct ill_get_ctl ctl;
2934 ctl.odr = p->odr_out;
2936 ctl.f = ill_array_lookup;
2938 req = ill_get_ILLRequest(&ctl, "ill", 0);
2940 if (!ill_Request(out, &req, 0, 0))
2942 int ill_request_size;
2943 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
2944 if (ill_request_buf)
2945 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
2950 int illRequest_size = 0;
2951 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
2953 r = (Z_External *) odr_malloc(out, sizeof(*r));
2954 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
2955 r->indirect_reference = 0;
2957 r->which = Z_External_single;
2959 r->u.single_ASN1_type =
2960 odr_create_Odr_oct(out,
2961 (unsigned char *)illRequest_buf,
2967 static Z_ItemOrder *encode_item_order(ZOOM_package p)
2969 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
2972 req->which = Z_IOItemOrder_esRequest;
2973 req->u.esRequest = (Z_IORequest *)
2974 odr_malloc(p->odr_out,sizeof(Z_IORequest));
2976 /* to keep part ... */
2977 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
2978 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
2979 req->u.esRequest->toKeep->supplDescription = 0;
2980 req->u.esRequest->toKeep->contact = (Z_IOContact *)
2981 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
2983 str = ZOOM_options_get(p->options, "contact-name");
2984 req->u.esRequest->toKeep->contact->name = str ?
2985 odr_strdup(p->odr_out, str) : 0;
2987 str = ZOOM_options_get(p->options, "contact-phone");
2988 req->u.esRequest->toKeep->contact->phone = str ?
2989 odr_strdup(p->odr_out, str) : 0;
2991 str = ZOOM_options_get(p->options, "contact-email");
2992 req->u.esRequest->toKeep->contact->email = str ?
2993 odr_strdup(p->odr_out, str) : 0;
2995 req->u.esRequest->toKeep->addlBilling = 0;
2997 /* not to keep part ... */
2998 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
2999 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3001 str = ZOOM_options_get(p->options, "itemorder-setname");
3006 req->u.esRequest->notToKeep->resultSetItem = 0;
3009 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3010 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3012 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3013 odr_strdup(p->odr_out, str);
3014 req->u.esRequest->notToKeep->resultSetItem->item =
3015 (int *) odr_malloc(p->odr_out, sizeof(int));
3017 str = ZOOM_options_get(p->options, "itemorder-item");
3018 *req->u.esRequest->notToKeep->resultSetItem->item =
3019 (str ? atoi(str) : 1);
3022 str = ZOOM_options_get(p->options, "doc");
3025 req->u.esRequest->notToKeep->itemRequest =
3026 z_ext_record_xml(p->odr_out, str, strlen(str));
3029 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3034 Z_APDU *create_admin_package(ZOOM_package p, int type,
3035 Z_ESAdminOriginPartToKeep **toKeepP,
3036 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3038 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3041 Z_ESAdminOriginPartToKeep *toKeep;
3042 Z_ESAdminOriginPartNotToKeep *notToKeep;
3043 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3044 const char *first_db = "Default";
3046 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3051 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3053 r->indirect_reference = 0;
3054 r->which = Z_External_ESAdmin;
3056 r->u.adminService = (Z_Admin *)
3057 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3058 r->u.adminService->which = Z_Admin_esRequest;
3059 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3060 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3062 toKeep = r->u.adminService->u.esRequest->toKeep =
3063 (Z_ESAdminOriginPartToKeep *)
3064 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3065 toKeep->which = type;
3066 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3067 toKeep->u.create = odr_nullval();
3068 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3070 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3071 (Z_ESAdminOriginPartNotToKeep *)
3072 odr_malloc(p->odr_out,
3073 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3074 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3075 notToKeep->u.recordsWillFollow = odr_nullval();
3079 *notToKeepP = notToKeep;
3084 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3086 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3087 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3088 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3089 const char *doc = ZOOM_options_get(p->options, "doc");
3094 req->taskSpecificParameters = ext;
3095 ext->direct_reference = req->packageType;
3096 ext->descriptor = 0;
3097 ext->indirect_reference = 0;
3099 ext->which = Z_External_octet;
3100 ext->u.single_ASN1_type =
3101 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc,
3106 static Z_APDU *create_update_package(ZOOM_package p)
3109 const char *first_db = "Default";
3111 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3112 const char *action = ZOOM_options_get(p->options, "action");
3113 const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque");
3114 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3115 const char *record_buf = ZOOM_options_get(p->options, "record");
3116 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3118 Odr_oid *syntax_oid = 0;
3124 record_buf = "void";
3125 syntax_str = "SUTRS";
3130 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3131 CLASS_RECSYN, syntax_str,
3141 action = "specialUpdate";
3143 if (!strcmp(action, "recordInsert"))
3144 action_no = Z_IUOriginPartToKeep_recordInsert;
3145 else if (!strcmp(action, "recordReplace"))
3146 action_no = Z_IUOriginPartToKeep_recordReplace;
3147 else if (!strcmp(action, "recordDelete"))
3148 action_no = Z_IUOriginPartToKeep_recordDelete;
3149 else if (!strcmp(action, "elementUpdate"))
3150 action_no = Z_IUOriginPartToKeep_elementUpdate;
3151 else if (!strcmp(action, "specialUpdate"))
3152 action_no = Z_IUOriginPartToKeep_specialUpdate;
3156 apdu = create_es_package(p, yaz_oid_extserv_database_update);
3159 Z_IUOriginPartToKeep *toKeep;
3160 Z_IUSuppliedRecords *notToKeep;
3161 Z_External *r = (Z_External *)
3162 odr_malloc(p->odr_out, sizeof(*r));
3164 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3167 r->direct_reference = odr_oiddup(p->odr_out,
3168 yaz_oid_extserv_database_update);
3170 r->which = Z_External_update;
3171 r->indirect_reference = 0;
3172 r->u.update = (Z_IUUpdate *)
3173 odr_malloc(p->odr_out, sizeof(*r->u.update));
3175 r->u.update->which = Z_IUUpdate_esRequest;
3176 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3177 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3178 toKeep = r->u.update->u.esRequest->toKeep =
3179 (Z_IUOriginPartToKeep *)
3180 odr_malloc(p->odr_out, sizeof(*toKeep));
3182 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3184 toKeep->elementSetName = 0;
3185 toKeep->actionQualifier = 0;
3186 toKeep->action = odr_intdup(p->odr_out, action_no);
3188 notToKeep = r->u.update->u.esRequest->notToKeep =
3189 (Z_IUSuppliedRecords *)
3190 odr_malloc(p->odr_out, sizeof(*notToKeep));
3192 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3193 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3194 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3195 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3196 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3199 notToKeep->elements[0]->u.opaque =
3200 odr_create_Odr_oct(p->odr_out,
3201 (const unsigned char *) recordIdOpaque,
3202 strlen(recordIdOpaque));
3204 else if (recordIdNumber)
3206 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3208 notToKeep->elements[0]->u.number =
3209 odr_intdup(p->odr_out, atoi(recordIdNumber));
3212 notToKeep->elements[0]->u.opaque = 0;
3213 notToKeep->elements[0]->supplementalId = 0;
3214 notToKeep->elements[0]->correlationInfo = 0;
3215 notToKeep->elements[0]->record =
3216 z_ext_record_oid(p->odr_out, syntax_oid,
3217 record_buf, strlen(record_buf));
3221 ODR print = odr_createmem(ODR_PRINT);
3223 z_APDU(print, &apdu, 0, 0);
3230 ZOOM_package_send(ZOOM_package p, const char *type)
3237 odr_reset(p->odr_out);
3240 if (!strcmp(type, "itemorder"))
3242 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3245 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3247 r->direct_reference =
3248 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3250 r->which = Z_External_itemOrder;
3251 r->indirect_reference = 0;
3252 r->u.itemOrder = encode_item_order(p);
3254 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3257 else if (!strcmp(type, "create")) /* create database */
3259 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3262 else if (!strcmp(type, "drop")) /* drop database */
3264 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3267 else if (!strcmp(type, "commit")) /* commit changes */
3269 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3272 else if (!strcmp(type, "update")) /* update record(s) */
3274 apdu = create_update_package(p);
3276 else if (!strcmp(type, "xmlupdate"))
3278 apdu = create_xmlupdate_package(p);
3282 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3286 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3287 task->u.package = p;
3288 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3289 p->buf_out = (char *) xmalloc(p->len_out);
3290 memcpy(p->buf_out, buf, p->len_out);
3295 while (ZOOM_event(1, &c))
3302 ZOOM_API(ZOOM_package)
3303 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3305 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3308 p->odr_out = odr_createmem(ODR_ENCODE);
3309 p->options = ZOOM_options_create_with_parent2(options, c->options);
3317 ZOOM_package_destroy(ZOOM_package p)
3322 if (p->refcount == 0)
3324 odr_destroy(p->odr_out);
3327 ZOOM_options_destroy(p->options);
3332 ZOOM_API(const char *)
3333 ZOOM_package_option_get(ZOOM_package p, const char *key)
3335 return ZOOM_options_get(p->options, key);
3340 ZOOM_package_option_set(ZOOM_package p, const char *key,
3343 ZOOM_options_set(p->options, key, val);
3346 static int ZOOM_connection_exec_task(ZOOM_connection c)
3348 ZOOM_task task = c->tasks;
3349 zoom_ret ret = zoom_complete;
3353 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3354 c, task->which, task->running);
3355 if (c->error != ZOOM_ERROR_NONE)
3357 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3358 "removing tasks because of error = %d", c, c->error);
3359 ZOOM_connection_remove_tasks(c);
3364 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3365 "task already running", c);
3369 ret = zoom_complete;
3370 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3372 switch (task->which)
3374 case ZOOM_TASK_SEARCH:
3375 if (c->proto == PROTO_HTTP)
3376 ret = ZOOM_connection_srw_send_search(c);
3378 ret = ZOOM_connection_send_search(c);
3380 case ZOOM_TASK_RETRIEVE:
3381 if (c->proto == PROTO_HTTP)
3382 ret = ZOOM_connection_srw_send_search(c);
3384 ret = send_present(c);
3386 case ZOOM_TASK_CONNECT:
3387 ret = do_connect(c);
3389 case ZOOM_TASK_SCAN:
3392 case ZOOM_TASK_PACKAGE:
3393 ret = send_package(c);
3395 case ZOOM_TASK_SORT:
3396 c->tasks->u.sort.resultset->r_sort_spec =
3397 c->tasks->u.sort.q->sort_spec;
3398 ret = send_sort(c, c->tasks->u.sort.resultset);
3404 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3405 "remove tasks because no connection exist", c);
3406 ZOOM_connection_remove_tasks(c);
3408 if (ret == zoom_complete)
3410 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3411 "task removed (complete)", c);
3412 ZOOM_connection_remove_task(c);
3415 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3420 static zoom_ret send_sort_present(ZOOM_connection c)
3422 zoom_ret r = zoom_complete;
3424 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3425 r = send_sort(c, c->tasks->u.search.resultset);
3426 if (r == zoom_complete)
3427 r = send_present(c);
3431 static int es_response(ZOOM_connection c,
3432 Z_ExtendedServicesResponse *res)
3434 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3436 if (res->diagnostics && res->num_diagnostics > 0)
3437 response_diag(c, res->diagnostics[0]);
3438 if (res->taskPackage &&
3439 res->taskPackage->which == Z_External_extendedService)
3441 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3442 Odr_oct *id = taskPackage->targetReference;
3445 ZOOM_options_setl(c->tasks->u.package->options,
3446 "targetReference", (char*) id->buf, id->len);
3448 if (res->taskPackage &&
3449 res->taskPackage->which == Z_External_octet)
3451 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3452 ZOOM_options_setl(c->tasks->u.package->options,
3453 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3458 static void interpret_init_diag(ZOOM_connection c,
3459 Z_DiagnosticFormat *diag)
3463 Z_DiagnosticFormat_s *ds = diag->elements[0];
3464 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3465 response_default_diag(c, ds->u.defaultDiagRec);
3470 static void interpret_otherinformation_field(ZOOM_connection c,
3471 Z_OtherInformation *ui)
3474 for (i = 0; i < ui->num_elements; i++)
3476 Z_OtherInformationUnit *unit = ui->list[i];
3477 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3478 unit->information.externallyDefinedInfo &&
3479 unit->information.externallyDefinedInfo->which ==
3482 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3488 static void set_init_option(const char *name, void *clientData) {
3489 ZOOM_connection c = (ZOOM_connection) clientData;
3492 sprintf(buf, "init_opt_%.70s", name);
3493 ZOOM_connection_option_set(c, buf, "1");
3497 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3499 Z_InitResponse *initrs;
3501 ZOOM_connection_set_mask(c, 0);
3502 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3505 case Z_APDU_initResponse:
3506 yaz_log(log_api, "%p recv_apd: Received Init response", c);
3507 initrs = apdu->u.initResponse;
3508 ZOOM_connection_option_set(c, "serverImplementationId",
3509 initrs->implementationId ?
3510 initrs->implementationId : "");
3511 ZOOM_connection_option_set(c, "serverImplementationName",
3512 initrs->implementationName ?
3513 initrs->implementationName : "");
3514 ZOOM_connection_option_set(c, "serverImplementationVersion",
3515 initrs->implementationVersion ?
3516 initrs->implementationVersion : "");
3517 /* Set the three old options too, for old applications */
3518 ZOOM_connection_option_set(c, "targetImplementationId",
3519 initrs->implementationId ?
3520 initrs->implementationId : "");
3521 ZOOM_connection_option_set(c, "targetImplementationName",
3522 initrs->implementationName ?
3523 initrs->implementationName : "");
3524 ZOOM_connection_option_set(c, "targetImplementationVersion",
3525 initrs->implementationVersion ?
3526 initrs->implementationVersion : "");
3528 /* Make initrs->options available as ZOOM-level options */
3529 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3531 if (!*initrs->result)
3533 Z_External *uif = initrs->userInformationField;
3535 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3537 if (uif && uif->which == Z_External_userInfo1)
3538 interpret_otherinformation_field(c, uif->u.userInfo1);
3543 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3544 yaz_oid_userinfo_cookie, 1, 0);
3545 xfree(c->cookie_in);
3548 c->cookie_in = xstrdup(cookie);
3549 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3550 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3551 c->support_named_resultsets = 1;
3554 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3555 ZOOM_connection_remove_task(c);
3557 ZOOM_connection_exec_task(c);
3559 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3561 NMEM tmpmem = nmem_create();
3562 Z_CharSetandLanguageNegotiation *p =
3563 yaz_get_charneg_record(initrs->otherInfo);
3567 char *charset = NULL, *lang = NULL;
3570 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3571 yaz_log(log_details, "%p recv_apdu target accepted: "
3572 "charset %s, language %s, select %d",
3574 charset ? charset : "none", lang ? lang : "none", sel);
3576 ZOOM_connection_option_set(c, "negotiation-charset",
3579 ZOOM_connection_option_set(c, "negotiation-lang",
3582 ZOOM_connection_option_set(
3583 c, "negotiation-charset-in-effect-for-records",
3584 (sel != 0) ? "1" : "0");
3585 nmem_destroy(tmpmem);
3589 case Z_APDU_searchResponse:
3590 yaz_log(log_api, "%p recv_apdu Search response", c);
3591 handle_search_response(c, apdu->u.searchResponse);
3592 if (send_sort_present(c) == zoom_complete)
3593 ZOOM_connection_remove_task(c);
3595 case Z_APDU_presentResponse:
3596 yaz_log(log_api, "%p recv_apdu Present response", c);
3597 handle_present_response(c, apdu->u.presentResponse);
3598 if (send_present(c) == zoom_complete)
3599 ZOOM_connection_remove_task(c);
3601 case Z_APDU_sortResponse:
3602 yaz_log(log_api, "%p recv_apdu Sort response", c);
3603 sort_response(c, apdu->u.sortResponse);
3604 if (send_present(c) == zoom_complete)
3605 ZOOM_connection_remove_task(c);
3607 case Z_APDU_scanResponse:
3608 yaz_log(log_api, "%p recv_apdu Scan response", c);
3609 scan_response(c, apdu->u.scanResponse);
3610 ZOOM_connection_remove_task(c);
3612 case Z_APDU_extendedServicesResponse:
3613 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3614 es_response(c, apdu->u.extendedServicesResponse);
3615 ZOOM_connection_remove_task(c);
3618 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3619 if (!ZOOM_test_reconnect(c))
3621 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3626 yaz_log(log_api, "%p Received unknown PDU", c);
3627 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3633 static void handle_srw_response(ZOOM_connection c,
3634 Z_SRW_searchRetrieveResponse *res)
3636 ZOOM_resultset resultset = 0;
3641 const char *syntax, *elementSetName;
3646 switch(c->tasks->which)
3648 case ZOOM_TASK_SEARCH:
3649 resultset = c->tasks->u.search.resultset;
3650 start = &c->tasks->u.search.start;
3651 syntax = c->tasks->u.search.syntax;
3652 elementSetName = c->tasks->u.search.elementSetName;
3654 case ZOOM_TASK_RETRIEVE:
3655 resultset = c->tasks->u.retrieve.resultset;
3656 start = &c->tasks->u.retrieve.start;
3657 syntax = c->tasks->u.retrieve.syntax;
3658 elementSetName = c->tasks->u.retrieve.elementSetName;
3663 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3664 ZOOM_connection_put_event(c, event);
3666 resultset->size = 0;
3668 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
3670 if (res->numberOfRecords)
3671 resultset->size = *res->numberOfRecords;
3673 for (i = 0; i<res->num_records; i++)
3677 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
3678 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
3680 if (res->records[i].recordPosition &&
3681 *res->records[i].recordPosition > 0)
3682 pos = *res->records[i].recordPosition - 1;
3686 npr->databaseName = 0;
3687 npr->which = Z_NamePlusRecord_databaseRecord;
3688 npr->u.databaseRecord = (Z_External *)
3689 odr_malloc(c->odr_in, sizeof(Z_External));
3690 npr->u.databaseRecord->descriptor = 0;
3691 npr->u.databaseRecord->direct_reference =
3692 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
3693 npr->u.databaseRecord->which = Z_External_octet;
3695 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
3696 odr_malloc(c->odr_in, sizeof(Odr_oct));
3697 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
3698 res->records[i].recordData_buf;
3699 npr->u.databaseRecord->u.octet_aligned->len =
3700 npr->u.databaseRecord->u.octet_aligned->size =
3701 res->records[i].recordData_len;
3702 record_cache_add(resultset, npr, pos, syntax, elementSetName);
3704 if (res->num_diagnostics > 0)
3706 const char *uri = res->diagnostics[0].uri;
3711 if ((cp = strrchr(uri, '/')))
3713 set_dset_error(c, code, uri,
3714 res->diagnostics[0].details, 0);
3717 nmem = odr_extract_mem(c->odr_in);
3718 nmem_transfer(odr_getmem(resultset->odr), nmem);
3724 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
3727 const char *content_type = z_HTTP_header_lookup(hres->headers,
3729 const char *connection_head = z_HTTP_header_lookup(hres->headers,
3731 ZOOM_connection_set_mask(c, 0);
3732 yaz_log(log_details, "%p handle_http", c);
3734 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
3736 Z_SOAP *soap_package = 0;
3738 Z_SOAP_Handler soap_handlers[2] = {
3739 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
3742 ret = z_soap_codec(o, &soap_package,
3743 &hres->content_buf, &hres->content_len,
3745 if (!ret && soap_package->which == Z_SOAP_generic &&
3746 soap_package->u.generic->no == 0)
3748 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
3749 if (sr->which == Z_SRW_searchRetrieve_response)
3750 handle_srw_response(c, sr->u.response);
3754 else if (!ret && (soap_package->which == Z_SOAP_fault
3755 || soap_package->which == Z_SOAP_error))
3757 set_HTTP_error(c, hres->code,
3758 soap_package->u.fault->fault_code,
3759 soap_package->u.fault->fault_string);
3766 if (hres->code != 200)
3767 set_HTTP_error(c, hres->code, 0, 0);
3769 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3772 ZOOM_connection_remove_task(c);
3773 if (!strcmp(hres->version, "1.0"))
3775 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
3776 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
3781 /* HTTP 1.1: only if no close we stay alive .. */
3782 if (connection_head && !strcmp(connection_head, "close"))
3788 static int do_read(ZOOM_connection c)
3793 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
3794 ZOOM_connection_put_event(c, event);
3796 r = cs_get(c->cs, &c->buf_in, &c->len_in);
3797 more = cs_more(c->cs);
3798 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
3803 if (!ZOOM_test_reconnect(c))
3805 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3814 odr_reset(c->odr_in);
3815 odr_setbuf(c->odr_in, c->buf_in, r, 0);
3816 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
3817 ZOOM_connection_put_event(c, event);
3819 if (!z_GDU(c->odr_in, &gdu, 0, 0))
3822 int err = odr_geterrorx(c->odr_in, &x);
3824 const char *element = odr_getelement(c->odr_in);
3825 sprintf(msg, "ODR code %d:%d element=%-20s",
3826 err, x, element ? element : "<unknown>");
3827 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
3830 else if (gdu->which == Z_GDU_Z3950)
3831 recv_apdu(c, gdu->u.z3950);
3832 else if (gdu->which == Z_GDU_HTTP_Response)
3835 handle_http(c, gdu->u.HTTP_Response);
3837 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3841 c->reconnect_ok = 0;
3846 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
3851 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
3852 ZOOM_connection_put_event(c, event);
3854 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
3855 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
3857 yaz_log(log_details, "%p do_write_ex write failed", c);
3858 if (ZOOM_test_reconnect(c))
3860 return zoom_pending;
3862 if (c->state == STATE_CONNECTING)
3863 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
3865 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3867 return zoom_complete;
3871 int mask = ZOOM_SELECT_EXCEPT;
3872 if (c->cs->io_pending & CS_WANT_WRITE)
3873 mask += ZOOM_SELECT_WRITE;
3874 if (c->cs->io_pending & CS_WANT_READ)
3875 mask += ZOOM_SELECT_READ;
3876 ZOOM_connection_set_mask(c, mask);
3877 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
3882 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
3883 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
3886 return zoom_pending;
3889 static zoom_ret do_write(ZOOM_connection c)
3891 return do_write_ex(c, c->buf_out, c->len_out);
3895 ZOOM_API(const char *)
3896 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
3898 return ZOOM_options_get(c->options, key);
3901 ZOOM_API(const char *)
3902 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
3904 return ZOOM_options_getl(c->options, key, lenp);
3908 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
3911 ZOOM_options_set(c->options, key, val);
3915 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
3916 const char *val, int len)
3918 ZOOM_options_setl(c->options, key, val, len);
3921 ZOOM_API(const char *)
3922 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
3924 return ZOOM_options_get(r->options, key);
3928 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
3931 ZOOM_options_set(r->options, key, val);
3936 ZOOM_connection_errcode(ZOOM_connection c)
3938 return ZOOM_connection_error(c, 0, 0);
3941 ZOOM_API(const char *)
3942 ZOOM_connection_errmsg(ZOOM_connection c)
3945 ZOOM_connection_error(c, &msg, 0);
3949 ZOOM_API(const char *)
3950 ZOOM_connection_addinfo(ZOOM_connection c)
3952 const char *addinfo;
3953 ZOOM_connection_error(c, 0, &addinfo);
3957 ZOOM_API(const char *)
3958 ZOOM_connection_diagset(ZOOM_connection c)
3960 const char *diagset;
3961 ZOOM_connection_error_x(c, 0, 0, &diagset);
3965 ZOOM_API(const char *)
3966 ZOOM_diag_str(int error)
3970 case ZOOM_ERROR_NONE:
3972 case ZOOM_ERROR_CONNECT:
3973 return "Connect failed";
3974 case ZOOM_ERROR_MEMORY:
3975 return "Out of memory";
3976 case ZOOM_ERROR_ENCODE:
3977 return "Encoding failed";
3978 case ZOOM_ERROR_DECODE:
3979 return "Decoding failed";
3980 case ZOOM_ERROR_CONNECTION_LOST:
3981 return "Connection lost";
3982 case ZOOM_ERROR_INIT:
3983 return "Init rejected";
3984 case ZOOM_ERROR_INTERNAL:
3985 return "Internal failure";
3986 case ZOOM_ERROR_TIMEOUT:
3988 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
3989 return "Unsupported protocol";
3990 case ZOOM_ERROR_UNSUPPORTED_QUERY:
3991 return "Unsupported query type";
3992 case ZOOM_ERROR_INVALID_QUERY:
3993 return "Invalid query";
3994 case ZOOM_ERROR_CQL_PARSE:
3995 return "CQL parsing error";
3996 case ZOOM_ERROR_CQL_TRANSFORM:
3997 return "CQL transformation error";
3998 case ZOOM_ERROR_CCL_CONFIG:
3999 return "CCL configuration error";
4000 case ZOOM_ERROR_CCL_PARSE:
4001 return "CCL parsing error";
4003 return diagbib1_str(error);
4008 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4009 const char **addinfo, const char **diagset)
4011 int error = c->error;
4014 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4015 *cp = ZOOM_diag_str(error);
4016 else if (!strcmp(c->diagset, "HTTP"))
4017 *cp = z_HTTP_errmsg(c->error);
4018 else if (!strcmp(c->diagset, "Bib-1"))
4019 *cp = ZOOM_diag_str(error);
4020 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4021 *cp = yaz_diag_srw_str(c->error);
4023 *cp = "Unknown error and diagnostic set";
4026 *addinfo = c->addinfo ? c->addinfo : "";
4028 *diagset = c->diagset ? c->diagset : "";
4033 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4034 const char **addinfo)
4036 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4039 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4041 ZOOM_Event event = 0;
4042 int r = cs_look(c->cs);
4043 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4048 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4049 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4051 ZOOM_connection_put_event(c, event);
4053 else if (r == CS_CONNECT)
4055 int ret = ret = cs_rcvconnect(c->cs);
4056 yaz_log(log_details, "%p ZOOM_connection_do_io "
4057 "cs_rcvconnect returned %d", c, ret);
4060 int mask = ZOOM_SELECT_EXCEPT;
4061 if (c->cs->io_pending & CS_WANT_WRITE)
4062 mask += ZOOM_SELECT_WRITE;
4063 if (c->cs->io_pending & CS_WANT_READ)
4064 mask += ZOOM_SELECT_READ;
4065 ZOOM_connection_set_mask(c, mask);
4069 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4070 ZOOM_connection_put_event(c, event);
4072 if (c->proto == PROTO_Z3950)
4073 ZOOM_connection_send_init(c);
4076 /* no init request for SRW .. */
4077 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4078 ZOOM_connection_remove_task(c);
4079 ZOOM_connection_set_mask(c, 0);
4080 ZOOM_connection_exec_task(c);
4082 c->state = STATE_ESTABLISHED;
4086 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4092 if (mask & ZOOM_SELECT_EXCEPT)
4094 if (!ZOOM_test_reconnect(c))
4096 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4101 if (mask & ZOOM_SELECT_READ)
4103 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4109 ZOOM_connection_last_event(ZOOM_connection cs)
4112 return ZOOM_EVENT_NONE;
4113 return cs->last_event;
4117 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4119 WRBUF wrbuf = (WRBUF) client_data;
4120 wrbuf_puts(wrbuf, buf);
4124 * Returns an xmalloc()d string containing RPN that corresponds to the
4125 * CQL passed in. On error, sets the Connection object's error state
4126 * and returns a null pointer.
4127 * ### We could cache CQL parser and/or transformer in Connection.
4129 static char *cql2pqf(ZOOM_connection c, const char *cql)
4133 const char *cqlfile;
4134 cql_transform_t trans;
4137 parser = cql_parser_create();
4138 if ((error = cql_parser_string(parser, cql)) != 0) {
4139 cql_parser_destroy(parser);
4140 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4144 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4147 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4149 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4152 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4153 cqlfile, strerror(errno));
4154 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4158 WRBUF wrbuf_result = wrbuf_alloc();
4159 error = cql_transform(trans, cql_parser_result(parser),
4160 cql2pqf_wrbuf_puts, wrbuf_result);
4163 const char *addinfo;
4164 error = cql_transform_error(trans, &addinfo);
4165 sprintf(buf, "%.200s (addinfo=%.200s)",
4166 cql_strerror(error), addinfo);
4167 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4171 result = xstrdup(wrbuf_cstr(wrbuf_result));
4173 cql_transform_close(trans);
4174 wrbuf_destroy(wrbuf_result);
4176 cql_parser_destroy(parser);
4180 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4184 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4185 /* timeout and this connection was waiting */
4186 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4188 ZOOM_connection_put_event(c, event);
4194 ZOOM_connection_process(ZOOM_connection c)
4200 event = ZOOM_connection_get_event(c);
4203 ZOOM_Event_destroy(event);
4206 ZOOM_connection_exec_task(c);
4207 event = ZOOM_connection_get_event(c);
4210 ZOOM_Event_destroy(event);
4217 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4221 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4223 for (i = 0; i<no; i++)
4225 ZOOM_connection c = cs[i];
4227 if (c && ZOOM_connection_process(c))
4233 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4235 if (c->mask && mask)
4236 ZOOM_connection_do_io(c, mask);
4240 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4243 return cs_fileno(c->cs);
4247 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4255 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4262 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4264 return ZOOM_options_get_int(c->options, "timeout", 30);
4270 * indent-tabs-mode: nil
4272 * vim: shiftwidth=4 tabstop=8 expandtab