2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.148 2007-09-11 08:40:28 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>
31 #include <yaz/snprintf.h>
33 static int log_api = 0;
34 static int log_details = 0;
41 static void resultset_destroy(ZOOM_resultset r);
42 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
43 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
44 static char *cql2pqf(ZOOM_connection c, const char *cql);
48 * This wrapper is just for logging failed lookups. It would be nicer
49 * if it could cause failure when a lookup fails, but that's hard.
51 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
52 int oid_class, const char *str) {
53 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
56 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
57 c, (int) oid_class, str);
62 static void initlog(void)
64 static int log_level_initialized = 0;
65 if (!log_level_initialized)
67 log_api = yaz_log_module_level("zoom");
68 log_details = yaz_log_module_level("zoomdetails");
69 log_level_initialized = 1;
73 static ZOOM_Event ZOOM_Event_create(int kind)
75 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
79 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
83 static void ZOOM_Event_destroy(ZOOM_Event event)
88 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
92 c->m_queue_back->prev = event;
93 assert(c->m_queue_front);
97 assert(!c->m_queue_front);
98 c->m_queue_front = event;
100 event->next = c->m_queue_back;
102 c->m_queue_back = event;
105 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
107 ZOOM_Event event = c->m_queue_front;
110 c->last_event = ZOOM_EVENT_NONE;
113 assert(c->m_queue_back);
114 c->m_queue_front = event->prev;
115 if (c->m_queue_front)
117 assert(c->m_queue_back);
118 c->m_queue_front->next = 0;
122 c->last_event = event->kind;
126 static void ZOOM_connection_remove_events(ZOOM_connection c)
129 while ((event = ZOOM_connection_get_event(c)))
130 ZOOM_Event_destroy(event);
133 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
135 ZOOM_Event event = c->m_queue_front;
137 return event ? event->kind : ZOOM_EVENT_NONE;
140 void ZOOM_connection_remove_tasks(ZOOM_connection c);
142 static void set_dset_error(ZOOM_connection c, int error,
144 const char *addinfo, const char *addinfo2)
151 if (!c->diagset || strcmp(dset, c->diagset))
154 c->diagset = xstrdup(dset);
155 /* remove integer part from SRW diagset .. */
156 if ((cp = strrchr(c->diagset, '/')))
159 if (addinfo && addinfo2)
161 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
162 strcpy(c->addinfo, addinfo);
163 strcat(c->addinfo, addinfo2);
166 c->addinfo = xstrdup(addinfo);
167 if (error != ZOOM_ERROR_NONE)
169 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
170 c, c->host_port ? c->host_port : "<>", dset, error,
171 addinfo ? addinfo : "",
172 addinfo2 ? addinfo2 : "");
173 ZOOM_connection_remove_tasks(c);
178 static void set_HTTP_error(ZOOM_connection c, int error,
179 const char *addinfo, const char *addinfo2)
181 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
184 static int uri_to_code(const char *uri)
188 if ((cp = strrchr(uri, '/')))
193 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
195 const char *uri = d->uri;
197 set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
203 static void set_ZOOM_error(ZOOM_connection c, int error,
206 set_dset_error(c, error, "ZOOM", addinfo, 0);
209 static void clear_error(ZOOM_connection c)
212 * If an error is tied to an operation then it's ok to clear: for
213 * example, a diagnostic returned from a search is cleared by a
214 * subsequent search. However, problems such as Connection Lost
215 * or Init Refused are not cleared, because they are not
216 * recoverable: doing another search doesn't help.
219 ZOOM_connection_remove_events(c);
222 case ZOOM_ERROR_CONNECT:
223 case ZOOM_ERROR_MEMORY:
224 case ZOOM_ERROR_DECODE:
225 case ZOOM_ERROR_CONNECTION_LOST:
226 case ZOOM_ERROR_INIT:
227 case ZOOM_ERROR_INTERNAL:
228 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
231 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
235 void ZOOM_connection_show_task(ZOOM_task task)
239 case ZOOM_TASK_SEARCH:
240 yaz_log(YLOG_LOG, "search p=%p", task);
242 case ZOOM_TASK_RETRIEVE:
243 yaz_log(YLOG_LOG, "retrieve p=%p", task);
245 case ZOOM_TASK_CONNECT:
246 yaz_log(YLOG_LOG, "connect p=%p", task);
249 yaz_log(YLOG_LOG, "scant p=%p", task);
254 void ZOOM_connection_show_tasks(ZOOM_connection c)
257 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
258 for (task = c->tasks; task; task = task->next)
259 ZOOM_connection_show_task(task);
262 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
264 ZOOM_task *taskp = &c->tasks;
266 taskp = &(*taskp)->next;
267 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
268 (*taskp)->running = 0;
269 (*taskp)->which = which;
275 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
277 return c->tasks ? 0 : 1;
280 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
282 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
284 task->next = c->tasks;
293 void ZOOM_connection_remove_task(ZOOM_connection c)
295 ZOOM_task task = c->tasks;
299 c->tasks = task->next;
302 case ZOOM_TASK_SEARCH:
303 resultset_destroy(task->u.search.resultset);
304 xfree(task->u.search.syntax);
305 xfree(task->u.search.elementSetName);
307 case ZOOM_TASK_RETRIEVE:
308 resultset_destroy(task->u.retrieve.resultset);
309 xfree(task->u.retrieve.syntax);
310 xfree(task->u.retrieve.elementSetName);
312 case ZOOM_TASK_CONNECT:
315 ZOOM_scanset_destroy(task->u.scan.scan);
317 case ZOOM_TASK_PACKAGE:
318 ZOOM_package_destroy(task->u.package);
321 resultset_destroy(task->u.sort.resultset);
322 ZOOM_query_destroy(task->u.sort.q);
331 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
332 ZOOM_connection_put_event(c, event);
337 static int ZOOM_connection_exec_task(ZOOM_connection c);
339 void ZOOM_connection_remove_tasks(ZOOM_connection c)
342 ZOOM_connection_remove_task(c);
345 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
347 const char *elementSetName);
349 ZOOM_API(ZOOM_connection)
350 ZOOM_connection_create(ZOOM_options options)
352 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
356 yaz_log(log_api, "%p ZOOM_connection_create", c);
358 c->proto = PROTO_Z3950;
360 ZOOM_connection_set_mask(c, 0);
362 c->state = STATE_IDLE;
365 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
372 c->options = ZOOM_options_create_with_parent(options);
378 c->charset = c->lang = 0;
389 c->maximum_record_size = 0;
390 c->preferred_message_size = 0;
392 c->odr_in = odr_createmem(ODR_DECODE);
393 c->odr_out = odr_createmem(ODR_ENCODE);
396 c->support_named_resultsets = 0;
397 c->last_event = ZOOM_EVENT_NONE;
399 c->m_queue_front = 0;
402 c->sru_version = xstrdup("1.1");
407 /* set database names. Take local databases (if set); otherwise
408 take databases given in ZURL (if set); otherwise use Default */
409 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
412 char **databaseNames;
413 const char *cp = ZOOM_options_get(options, "databaseName");
415 if ((!cp || !*cp) && con->host_port)
417 if (strncmp(con->host_port, "unix:", 5) == 0)
418 cp = strchr(con->host_port+5, ':');
420 cp = strchr(con->host_port, '/');
426 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
427 return databaseNames;
430 ZOOM_API(ZOOM_connection)
431 ZOOM_connection_new(const char *host, int portnum)
433 ZOOM_connection c = ZOOM_connection_create(0);
435 ZOOM_connection_connect(c, host, portnum);
439 static zoom_sru_mode get_sru_mode_from_string(const char *s)
442 return zoom_sru_soap;
443 if (!yaz_matchstr(s, "soap"))
444 return zoom_sru_soap;
445 else if (!yaz_matchstr(s, "get"))
447 else if (!yaz_matchstr(s, "post"))
448 return zoom_sru_post;
449 return zoom_sru_error;
453 ZOOM_connection_connect(ZOOM_connection c,
454 const char *host, int portnum)
461 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
462 c, host ? host : "null", portnum);
464 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
465 ZOOM_connection_remove_tasks(c);
469 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
473 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
476 val = ZOOM_options_get(c->options, "proxy");
479 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
480 c->proxy = xstrdup(val);
485 val = ZOOM_options_get(c->options, "charset");
488 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
489 c->charset = xstrdup(val);
493 val = ZOOM_options_get(c->options, "lang");
496 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
497 c->lang = xstrdup(val);
508 sprintf(hostn, "%.80s:%d", host, portnum);
509 c->host_port = xstrdup(hostn);
512 c->host_port = xstrdup(host);
517 * If the "<scheme>:" part of the host string is preceded by one
518 * or more comma-separated <name>=<value> pairs, these are taken
519 * to be options to be set on the connection object. Among other
520 * applications, this facility can be used to embed authentication
522 * user=admin,password=secret,tcp:localhost:9999
524 char *remainder = c->host_port;
525 char *pcolon = strchr(remainder, ':');
528 while ((pcomma = strchr(remainder, ',')) != 0 &&
529 (pcolon == 0 || pcomma < pcolon)) {
531 if ((pequals = strchr(remainder, '=')) != 0) {
533 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
534 ZOOM_connection_option_set(c, remainder, pequals+1);
536 remainder = pcomma+1;
539 if (remainder != c->host_port) {
541 c->host_port = xstrdup(remainder);
542 /*printf("# reset hp='%s'\n", remainder);*/
546 val = ZOOM_options_get(c->options, "sru");
547 c->sru_mode = get_sru_mode_from_string(val);
549 xfree(c->sru_version);
550 val = ZOOM_options_get(c->options, "sru_version");
551 c->sru_version = xstrdup(val ? val : "1.1");
553 ZOOM_options_set(c->options, "host", c->host_port);
555 xfree(c->cookie_out);
557 val = ZOOM_options_get(c->options, "cookie");
560 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
561 c->cookie_out = xstrdup(val);
566 val = ZOOM_options_get(c->options, "clientIP");
569 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
571 c->client_IP = xstrdup(val);
576 val = ZOOM_options_get(c->options, "group");
578 c->group = xstrdup(val);
582 val = ZOOM_options_get(c->options, "user");
584 c->user = xstrdup(val);
588 val = ZOOM_options_get(c->options, "password");
590 val = ZOOM_options_get(c->options, "pass");
593 c->password = xstrdup(val);
595 c->maximum_record_size =
596 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
597 c->preferred_message_size =
598 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
600 c->async = ZOOM_options_get_bool(c->options, "async", 0);
601 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
603 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
607 while (ZOOM_event(1, &c))
613 ZOOM_query_create(void)
615 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
617 yaz_log(log_details, "%p ZOOM_query_create", s);
621 s->odr = odr_createmem(ODR_ENCODE);
628 ZOOM_query_destroy(ZOOM_query s)
634 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
635 if (s->refcount == 0)
643 ZOOM_query_prefix(ZOOM_query s, const char *str)
645 s->query_string = odr_strdup(s->odr, str);
646 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
647 s->z_query->which = Z_Query_type_1;
648 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
649 if (!s->z_query->u.type_1)
651 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
655 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
660 ZOOM_query_cql(ZOOM_query s, const char *str)
664 s->query_string = odr_strdup(s->odr, str);
666 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
667 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
668 ext->indirect_reference = 0;
670 ext->which = Z_External_CQL;
671 ext->u.cql = s->query_string;
673 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
674 s->z_query->which = Z_Query_type_104;
675 s->z_query->u.type_104 = ext;
677 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
683 * Translate the CQL string client-side into RPN which is passed to
684 * the server. This is useful for server's that don't themselves
685 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
686 * only as a place to stash diagnostics if compilation fails; if this
687 * information is not needed, a null pointer may be used.
690 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
694 ZOOM_connection freeme = 0;
696 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
698 conn = freeme = ZOOM_connection_create(0);
700 rpn = cql2pqf(conn, str);
702 ZOOM_connection_destroy(freeme);
706 ret = ZOOM_query_prefix(s, rpn);
712 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
713 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
714 * to the server, as the YAZ GFS doesn't know how to handle this.
717 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
718 int *ccl_error, const char **error_string,
722 struct ccl_rpn_node *rpn;
723 CCL_bibset bibset = ccl_qual_mk();
726 ccl_qual_buf(bibset, config);
728 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
731 *error_string = ccl_err_msg(*ccl_error);
736 WRBUF wr = wrbuf_alloc();
739 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
742 ccl_qual_rm(&bibset);
747 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
749 s->sort_spec = yaz_sort_spec(s->odr, criteria);
752 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
756 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
760 static zoom_ret do_write(ZOOM_connection c);
763 ZOOM_connection_destroy(ZOOM_connection c)
768 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
771 for (r = c->resultsets; r; r = r->next)
777 odr_destroy(c->odr_in);
778 odr_destroy(c->odr_out);
779 ZOOM_options_destroy(c->options);
780 ZOOM_connection_remove_tasks(c);
781 ZOOM_connection_remove_events(c);
787 xfree(c->cookie_out);
793 xfree(c->sru_version);
797 void ZOOM_resultset_addref(ZOOM_resultset r)
802 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
807 ZOOM_resultset ZOOM_resultset_create(void)
810 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
814 yaz_log(log_details, "%p ZOOM_resultset_create", r);
817 r->odr = odr_createmem(ODR_ENCODE);
822 for (i = 0; i<RECORD_HASH_SIZE; i++)
823 r->record_hash[i] = 0;
828 r->databaseNames = 0;
829 r->num_databaseNames = 0;
833 ZOOM_API(ZOOM_resultset)
834 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
837 ZOOM_query s = ZOOM_query_create();
839 ZOOM_query_prefix(s, q);
841 r = ZOOM_connection_search(c, s);
842 ZOOM_query_destroy(s);
846 ZOOM_API(ZOOM_resultset)
847 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
849 ZOOM_resultset r = ZOOM_resultset_create();
853 const char *syntax, *elementSetName;
855 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
856 r->r_sort_spec = q->sort_spec;
859 r->options = ZOOM_options_create_with_parent(c->options);
861 start = ZOOM_options_get_int(r->options, "start", 0);
862 count = ZOOM_options_get_int(r->options, "count", 0);
864 /* If "presentChunk" is defined use that; otherwise "step" */
865 const char *cp = ZOOM_options_get(r->options, "presentChunk");
866 r->step = ZOOM_options_get_int(r->options,
867 (cp != 0 ? "presentChunk": "step"), 0);
869 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
870 cp = ZOOM_options_get(r->options, "setname");
872 r->setname = xstrdup(cp);
873 cp = ZOOM_options_get(r->options, "schema");
875 r->schema = xstrdup(cp);
877 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
882 r->next = c->resultsets;
887 if (c->host_port && c->proto == PROTO_HTTP)
891 yaz_log(log_details, "ZOOM_connection_search: no comstack");
892 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
896 yaz_log(log_details, "ZOOM_connection_search: reconnect");
901 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
902 task->u.search.resultset = r;
903 task->u.search.start = start;
904 task->u.search.count = count;
906 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
907 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
908 elementSetName = ZOOM_options_get(r->options, "elementSetName");
909 task->u.search.elementSetName = elementSetName
910 ? xstrdup(elementSetName) : 0;
912 ZOOM_resultset_addref(r);
918 while (ZOOM_event(1, &c))
925 ZOOM_resultset_sort(ZOOM_resultset r,
926 const char *sort_type, const char *sort_spec)
928 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
932 ZOOM_resultset_sort1(ZOOM_resultset r,
933 const char *sort_type, const char *sort_spec)
935 ZOOM_connection c = r->connection;
939 newq = ZOOM_query_create();
940 if (ZOOM_query_sortby(newq, sort_spec) < 0)
943 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
944 r, r, sort_type, sort_spec);
948 if (c->host_port && c->proto == PROTO_HTTP)
952 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
953 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
957 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
963 ZOOM_resultset_cache_reset(r);
964 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
965 task->u.sort.resultset = r;
966 task->u.sort.q = newq;
968 ZOOM_resultset_addref(r);
972 while (ZOOM_event(1, &c))
980 ZOOM_resultset_cache_reset(ZOOM_resultset r)
983 for (i = 0; i<RECORD_HASH_SIZE; i++)
985 ZOOM_record_cache rc;
986 for (rc = r->record_hash[i]; rc; rc = rc->next)
988 if (rc->rec.wrbuf_marc)
989 wrbuf_destroy(rc->rec.wrbuf_marc);
990 if (rc->rec.wrbuf_iconv)
991 wrbuf_destroy(rc->rec.wrbuf_iconv);
992 if (rc->rec.wrbuf_opac)
993 wrbuf_destroy(rc->rec.wrbuf_opac);
995 r->record_hash[i] = 0;
1000 ZOOM_resultset_destroy(ZOOM_resultset r)
1002 resultset_destroy(r);
1005 static void resultset_destroy(ZOOM_resultset r)
1010 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1012 if (r->refcount == 0)
1014 ZOOM_resultset_cache_reset(r);
1018 /* remove ourselves from the resultsets in connection */
1019 ZOOM_resultset *rp = &r->connection->resultsets;
1022 assert(*rp); /* we must be in this list!! */
1024 { /* OK, we're here - take us out of it */
1031 ZOOM_query_destroy(r->query);
1032 ZOOM_options_destroy(r->options);
1033 odr_destroy(r->odr);
1041 ZOOM_resultset_size(ZOOM_resultset r)
1043 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
1048 static void do_close(ZOOM_connection c)
1053 ZOOM_connection_set_mask(c, 0);
1054 c->state = STATE_IDLE;
1057 static int ZOOM_test_reconnect(ZOOM_connection c)
1061 if (!c->reconnect_ok)
1064 c->reconnect_ok = 0;
1065 c->tasks->running = 0;
1066 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1068 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1069 ZOOM_connection_put_event(c, event);
1074 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1075 int force_sync, int start, int count)
1080 const char *syntax, *elementSetName;
1084 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1085 " count=%d", r, force_sync, start, count);
1090 if (c->host_port && c->proto == PROTO_HTTP)
1094 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1095 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1099 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1101 c->reconnect_ok = 1;
1104 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1105 task->u.retrieve.resultset = r;
1106 task->u.retrieve.start = start;
1107 task->u.retrieve.count = count;
1109 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1110 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1111 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1112 task->u.retrieve.elementSetName = elementSetName
1113 ? xstrdup(elementSetName) : 0;
1115 cp = ZOOM_options_get(r->options, "schema");
1118 if (!r->schema || strcmp(r->schema, cp))
1121 r->schema = xstrdup(cp);
1125 ZOOM_resultset_addref(r);
1127 if (!r->connection->async || force_sync)
1128 while (r->connection && ZOOM_event(1, &r->connection))
1133 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1134 size_t start, size_t count)
1136 int force_present = 0;
1140 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1141 r, r, (long) start, (long) count);
1144 ZOOM_resultset_retrieve(r, force_present, start, count);
1148 for (i = 0; i< count; i++)
1149 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1153 static void get_cert(ZOOM_connection c)
1158 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1160 ZOOM_connection_option_setl(c, "sslPeerCert",
1161 cert_buf, cert_len);
1166 static zoom_ret do_connect(ZOOM_connection c)
1169 const char *effective_host;
1172 effective_host = c->proxy;
1174 effective_host = c->host_port;
1176 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1180 c->cs = cs_create_host(effective_host, 0, &add);
1182 if (c->cs && c->cs->protocol == PROTO_HTTP)
1185 const char *path = 0;
1187 c->proto = PROTO_HTTP;
1188 cs_get_host_args(c->host_port, &path);
1190 c->path = (char*) xmalloc(strlen(path)+2);
1192 strcpy(c->path+1, path);
1194 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1196 return zoom_complete;
1201 int ret = cs_connect(c->cs, add);
1204 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1205 ZOOM_connection_put_event(c, event);
1207 if (c->proto == PROTO_Z3950)
1208 ZOOM_connection_send_init(c);
1211 /* no init request for SRW .. */
1212 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1213 ZOOM_connection_remove_task(c);
1214 ZOOM_connection_set_mask(c, 0);
1215 ZOOM_connection_exec_task(c);
1217 c->state = STATE_ESTABLISHED;
1218 return zoom_pending;
1222 int mask = ZOOM_SELECT_EXCEPT;
1223 if (c->cs->io_pending & CS_WANT_WRITE)
1224 mask += ZOOM_SELECT_WRITE;
1225 if (c->cs->io_pending & CS_WANT_READ)
1226 mask += ZOOM_SELECT_READ;
1227 ZOOM_connection_set_mask(c, mask);
1228 c->state = STATE_CONNECTING;
1229 return zoom_pending;
1232 c->state = STATE_IDLE;
1233 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1234 return zoom_complete;
1237 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1240 for (i = 0; i<200; i++)
1244 Z_OtherInformation **oi;
1249 sprintf(buf, "otherInfo%d", i);
1250 val = ZOOM_options_get(c->options, buf);
1253 cp = strchr(val, ':');
1257 if (len >= sizeof(buf))
1258 len = sizeof(buf)-1;
1259 memcpy(buf, val, len);
1262 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1267 yaz_oi_APDU(a, &oi);
1268 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1272 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1277 Z_OtherInformation **oi;
1278 yaz_oi_APDU(a, &oi);
1279 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1284 Z_OtherInformation **oi;
1285 yaz_oi_APDU(a, &oi);
1286 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1289 otherInfo_attach(c, a, out);
1290 if (!z_APDU(out, &a, 0, 0))
1292 FILE *outf = fopen("/tmp/apdu.txt", "a");
1295 ODR odr_pr = odr_createmem(ODR_PRINT);
1296 fprintf(outf, "a=%p\n", a);
1297 odr_setprint(odr_pr, outf);
1298 z_APDU(odr_pr, &a, 0, 0);
1299 odr_destroy(odr_pr);
1301 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1302 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1306 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1310 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1314 if (encode_APDU(c, a, c->odr_out))
1315 return zoom_complete;
1316 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1317 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1318 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1319 ZOOM_connection_put_event(c, event);
1320 odr_reset(c->odr_out);
1324 /* returns 1 if PDU was sent OK (still pending )
1325 0 if PDU was not sent OK (nothing to wait for)
1328 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1330 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1331 Z_InitRequest *ireq = apdu->u.initRequest;
1332 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1333 odr_malloc(c->odr_out, sizeof(*auth));
1336 ODR_MASK_SET(ireq->options, Z_Options_search);
1337 ODR_MASK_SET(ireq->options, Z_Options_present);
1338 ODR_MASK_SET(ireq->options, Z_Options_scan);
1339 ODR_MASK_SET(ireq->options, Z_Options_sort);
1340 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1341 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1343 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1344 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1345 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1347 /* Index Data's Z39.50 Implementor Id is 81 */
1348 ireq->implementationId =
1349 odr_prepend(c->odr_out,
1350 ZOOM_options_get(c->options, "implementationId"),
1351 odr_prepend(c->odr_out, "81", ireq->implementationId));
1353 ireq->implementationName =
1354 odr_prepend(c->odr_out,
1355 ZOOM_options_get(c->options, "implementationName"),
1356 odr_prepend(c->odr_out, "ZOOM-C",
1357 ireq->implementationName));
1359 version = odr_strdup(c->odr_out, "$Revision: 1.148 $");
1360 if (strlen(version) > 10) /* check for unexpanded CVS strings */
1361 version[strlen(version)-2] = '\0';
1362 ireq->implementationVersion =
1363 odr_prepend(c->odr_out,
1364 ZOOM_options_get(c->options, "implementationVersion"),
1365 odr_prepend(c->odr_out, &version[11],
1366 ireq->implementationVersion));
1368 *ireq->maximumRecordSize = c->maximum_record_size;
1369 *ireq->preferredMessageSize = c->preferred_message_size;
1371 if (c->group || c->password)
1373 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1374 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1375 pass->userId = odr_strdup_null(c->odr_out, c->user);
1376 pass->password = odr_strdup_null(c->odr_out, c->password);
1377 auth->which = Z_IdAuthentication_idPass;
1378 auth->u.idPass = pass;
1379 ireq->idAuthentication = auth;
1383 auth->which = Z_IdAuthentication_open;
1384 auth->u.open = odr_strdup(c->odr_out, c->user);
1385 ireq->idAuthentication = auth;
1389 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1390 yaz_oid_userinfo_proxy, 1, c->host_port);
1392 if (c->charset || c->lang)
1394 Z_OtherInformation **oi;
1395 Z_OtherInformationUnit *oi_unit;
1397 yaz_oi_APDU(apdu, &oi);
1399 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1401 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1402 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1403 oi_unit->information.externallyDefinedInfo =
1404 yaz_set_proposal_charneg_list(c->odr_out, " ",
1405 c->charset, c->lang, 1);
1409 return send_APDU(c, apdu);
1413 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1418 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
1420 if (c->sru_mode == zoom_sru_get)
1422 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1424 else if (c->sru_mode == zoom_sru_post)
1426 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1428 else if (c->sru_mode == zoom_sru_soap)
1430 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1432 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1433 return zoom_complete;
1434 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1436 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1437 ZOOM_connection_put_event(c, event);
1438 odr_reset(c->odr_out);
1444 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type) {
1445 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1446 sr->username = c->user;
1447 sr->password = c->password;
1453 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1457 ZOOM_resultset resultset = 0;
1459 const char *option_val = 0;
1461 if (c->error) /* don't continue on error */
1462 return zoom_complete;
1464 switch(c->tasks->which)
1466 case ZOOM_TASK_SEARCH:
1467 resultset = c->tasks->u.search.resultset;
1468 resultset->setname = xstrdup("default");
1469 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1470 start = &c->tasks->u.search.start;
1471 count = &c->tasks->u.search.count;
1473 case ZOOM_TASK_RETRIEVE:
1474 resultset = c->tasks->u.retrieve.resultset;
1476 start = &c->tasks->u.retrieve.start;
1477 count = &c->tasks->u.retrieve.count;
1479 if (*start >= resultset->size)
1480 return zoom_complete;
1481 if (*start + *count > resultset->size)
1482 *count = resultset->size - *start;
1484 for (i = 0; i < *count; i++)
1487 record_cache_lookup(resultset, i + *start,
1488 c->tasks->u.retrieve.syntax,
1489 c->tasks->u.retrieve.elementSetName);
1494 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1495 ZOOM_connection_put_event(c, event);
1502 return zoom_complete;
1505 return zoom_complete;
1507 assert(resultset->query);
1509 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1510 if (resultset->query->z_query->which == Z_Query_type_104
1511 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1513 sr->u.request->query_type = Z_SRW_query_type_cql;
1514 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1516 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1517 resultset->query->z_query->u.type_1)
1519 sr->u.request->query_type = Z_SRW_query_type_pqf;
1520 sr->u.request->query.pqf = resultset->query->query_string;
1524 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1525 return zoom_complete;
1527 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1528 sr->u.request->maximumRecords = odr_intdup(
1529 c->odr_out, resultset->step>0 ? resultset->step : *count);
1530 sr->u.request->recordSchema = resultset->schema;
1532 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1534 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1536 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1538 sr->extra_args = odr_strdup(c->odr_out, option_val);
1539 return send_srw(c, sr);
1542 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1544 return zoom_complete;
1548 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1551 int lslb, ssub, mspn;
1553 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1554 Z_SearchRequest *search_req = apdu->u.searchRequest;
1555 const char *elementSetName;
1556 const char *smallSetElementSetName;
1557 const char *mediumSetElementSetName;
1560 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1562 r = c->tasks->u.search.resultset;
1564 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1567 ZOOM_options_get(r->options, "elementSetName");
1568 smallSetElementSetName =
1569 ZOOM_options_get(r->options, "smallSetElementSetName");
1570 mediumSetElementSetName =
1571 ZOOM_options_get(r->options, "mediumSetElementSetName");
1573 if (!smallSetElementSetName)
1574 smallSetElementSetName = elementSetName;
1576 if (!mediumSetElementSetName)
1577 mediumSetElementSetName = elementSetName;
1582 /* prepare query for the search request */
1583 search_req->query = r->query->z_query;
1584 if (!search_req->query)
1586 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1587 return zoom_complete;
1589 if (r->query->z_query->which == Z_Query_type_1 ||
1590 r->query->z_query->which == Z_Query_type_101)
1592 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1595 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1598 search_req->query = yaz_copy_Z_Query(search_req->query,
1601 yaz_query_charset_convert_rpnquery(search_req->query->u.type_1,
1603 yaz_iconv_close(cd);
1607 search_req->databaseNames = r->databaseNames;
1608 search_req->num_databaseNames = r->num_databaseNames;
1610 /* get syntax (no need to provide unless piggyback is in effect) */
1611 syntax = c->tasks->u.search.syntax;
1613 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1614 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1615 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1616 if (lslb != -1 && ssub != -1 && mspn != -1)
1618 /* So're a Z39.50 expert? Let's hope you don't do sort */
1619 *search_req->largeSetLowerBound = lslb;
1620 *search_req->smallSetUpperBound = ssub;
1621 *search_req->mediumSetPresentNumber = mspn;
1623 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1624 && r->piggyback && !r->r_sort_spec && !r->schema)
1626 /* Regular piggyback - do it unless we're going to do sort */
1627 *search_req->largeSetLowerBound = 2000000000;
1628 *search_req->smallSetUpperBound = 1;
1629 *search_req->mediumSetPresentNumber =
1630 r->step>0 ? r->step : c->tasks->u.search.count;
1634 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1635 smallSetElementSetName = 0;
1636 mediumSetElementSetName = 0;
1639 if (smallSetElementSetName && *smallSetElementSetName)
1641 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1642 odr_malloc(c->odr_out, sizeof(*esn));
1644 esn->which = Z_ElementSetNames_generic;
1645 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1646 search_req->smallSetElementSetNames = esn;
1648 if (mediumSetElementSetName && *mediumSetElementSetName)
1650 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1651 odr_malloc(c->odr_out, sizeof(*esn));
1653 esn->which = Z_ElementSetNames_generic;
1654 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1655 search_req->mediumSetElementSetNames = esn;
1658 search_req->preferredRecordSyntax =
1659 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1663 if (c->support_named_resultsets)
1667 /* find the lowest unused ordinal so that we re-use
1668 result sets on the server. */
1669 for (ord = 1; ; ord++)
1672 sprintf(setname, "%d", ord);
1673 for (rp = c->resultsets; rp; rp = rp->next)
1674 if (rp->setname && !strcmp(rp->setname, setname))
1679 r->setname = xstrdup(setname);
1680 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1681 "set %s", c, r->setname);
1685 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1687 r->setname = xstrdup("default");
1689 ZOOM_options_set(r->options, "setname", r->setname);
1691 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1692 return send_APDU(c, apdu);
1695 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1697 char oid_name_buf[OID_STR_MAX];
1698 const char *oid_name;
1701 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1704 case Z_DefaultDiagFormat_v2Addinfo:
1705 addinfo = r->u.v2Addinfo;
1707 case Z_DefaultDiagFormat_v3Addinfo:
1708 addinfo = r->u.v3Addinfo;
1713 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1716 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1718 if (p->which != Z_DiagRec_defaultFormat)
1719 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1721 response_default_diag(c, p->u.defaultFormat);
1724 ZOOM_API(ZOOM_record)
1725 ZOOM_record_clone(ZOOM_record srec)
1732 odr_enc = odr_createmem(ODR_ENCODE);
1733 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1735 buf = odr_getbuf(odr_enc, &size, 0);
1737 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1738 nrec->odr = odr_createmem(ODR_DECODE);
1739 nrec->wrbuf_marc = 0;
1740 nrec->wrbuf_iconv = 0;
1741 nrec->wrbuf_opac = 0;
1742 odr_setbuf(nrec->odr, buf, size, 0);
1743 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1745 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1746 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1747 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1748 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1749 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1750 odr_destroy(odr_enc);
1754 ZOOM_API(ZOOM_record)
1755 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1757 const char *syntax =
1758 ZOOM_options_get(s->options, "preferredRecordSyntax");
1759 const char *elementSetName =
1760 ZOOM_options_get(s->options, "elementSetName");
1762 return record_cache_lookup(s, pos, syntax, elementSetName);
1765 ZOOM_API(ZOOM_record)
1766 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1768 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1773 * MIKE: I think force_sync should always be zero, but I don't
1774 * want to make this change until I get the go-ahead from
1775 * Adam, in case something depends on the old synchronous
1779 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1780 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1781 rec = ZOOM_resultset_record_immediate(r, pos);
1787 ZOOM_record_destroy(ZOOM_record rec)
1791 if (rec->wrbuf_marc)
1792 wrbuf_destroy(rec->wrbuf_marc);
1793 if (rec->wrbuf_iconv)
1794 wrbuf_destroy(rec->wrbuf_iconv);
1795 if (rec->wrbuf_opac)
1796 wrbuf_destroy(rec->wrbuf_opac);
1797 odr_destroy(rec->odr);
1801 static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
1803 const char *buf, int sz,
1804 const char *record_charset)
1809 yaz_marc_t mt = yaz_marc_create();
1812 strcpy(to, "UTF-8");
1813 if (record_charset && *record_charset)
1815 /* Use "from,to" or just "from" */
1816 const char *cp = strchr(record_charset, ',');
1817 int clen = strlen(record_charset);
1820 strncpy( to, cp+1, sizeof(to)-1);
1821 to[sizeof(to)-1] = '\0';
1822 clen = cp - record_charset;
1824 if (clen > sizeof(from)-1)
1825 clen = sizeof(from)-1;
1828 strncpy(from, record_charset, clen);
1834 cd = yaz_iconv_open(to, from);
1835 yaz_marc_iconv(mt, cd);
1838 yaz_marc_xml(mt, marc_type);
1839 if (!rec->wrbuf_marc)
1840 rec->wrbuf_marc = wrbuf_alloc();
1841 wrbuf_rewind(rec->wrbuf_marc);
1842 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
1844 yaz_marc_destroy(mt);
1846 yaz_iconv_close(cd);
1848 *len = wrbuf_len(rec->wrbuf_marc);
1849 return wrbuf_cstr(rec->wrbuf_marc);
1851 yaz_marc_destroy(mt);
1853 yaz_iconv_close(cd);
1857 static const char *record_iconv_return(ZOOM_record rec, int *len,
1858 const char *buf, int sz,
1859 const char *record_charset)
1866 strcpy(to, "UTF-8");
1868 if (record_charset && *record_charset)
1870 /* Use "from,to" or just "from" */
1871 const char *cp = strchr(record_charset, ',');
1872 int clen = strlen(record_charset);
1875 strncpy( to, cp+1, sizeof(to)-1);
1876 to[sizeof(to)-1] = '\0';
1877 clen = cp - record_charset;
1879 if (clen > sizeof(from)-1)
1880 clen = sizeof(from)-1;
1883 strncpy(from, record_charset, clen);
1887 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1889 if (!rec->wrbuf_iconv)
1890 rec->wrbuf_iconv = wrbuf_alloc();
1892 wrbuf_rewind(rec->wrbuf_iconv);
1894 wrbuf_iconv_write(rec->wrbuf_iconv, cd, buf, sz);
1895 wrbuf_iconv_reset(rec->wrbuf_iconv, cd);
1897 buf = wrbuf_cstr(rec->wrbuf_iconv);
1898 sz = wrbuf_len(rec->wrbuf_iconv);
1899 yaz_iconv_close(cd);
1908 ZOOM_record_error(ZOOM_record rec, const char **cp,
1909 const char **addinfo, const char **diagset)
1911 Z_NamePlusRecord *npr;
1920 *cp = rec->diag_message;
1922 *addinfo = rec->diag_details;
1924 *diagset = rec->diag_set;
1925 return uri_to_code(rec->diag_uri);
1927 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
1929 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
1930 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
1931 const char *add = 0;
1933 if (diag_rec->which == Z_DiagRec_defaultFormat)
1935 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
1938 error = *ddf->condition;
1941 case Z_DefaultDiagFormat_v2Addinfo:
1942 add = ddf->u.v2Addinfo;
1944 case Z_DefaultDiagFormat_v3Addinfo:
1945 add = ddf->u.v3Addinfo;
1950 yaz_oid_to_string(yaz_oid_std(),
1951 ddf->diagnosticSetId, &oclass);
1959 *addinfo = add ? add : "";
1961 *cp = diagbib1_str(error);
1967 ZOOM_API(const char *)
1968 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
1975 Z_NamePlusRecord *npr;
1978 *len = 0; /* default return */
1987 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
1989 if (cp[i] == ';' || cp[i] == ' ')
1995 while (type_spec[i] == ';')
1998 while (type_spec[i] == ' ')
2000 if (!strncmp(type_spec+i, "charset=", 8))
2003 i = i + 8; /* skip charset= */
2004 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
2006 if (type_spec[i] == ';' || type_spec[i] == ' ')
2012 else if (!strncmp(type_spec+i, "xpath=", 6))
2016 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
2020 while (type_spec[i] == ' ')
2023 if (!strcmp(type, "database"))
2026 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2027 return npr->databaseName;
2029 else if (!strcmp(type, "schema"))
2032 *len = rec->schema ? strlen(rec->schema) : 0;
2035 else if (!strcmp(type, "syntax"))
2037 const char *desc = 0;
2038 if (npr->which == Z_NamePlusRecord_databaseRecord)
2040 Z_External *r = (Z_External *) npr->u.databaseRecord;
2041 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2046 *len = strlen(desc);
2049 if (npr->which != Z_NamePlusRecord_databaseRecord)
2052 /* from now on - we have a database record .. */
2053 if (!strcmp(type, "render"))
2055 Z_External *r = (Z_External *) npr->u.databaseRecord;
2056 const Odr_oid *oid = r->direct_reference;
2058 /* render bibliographic record .. */
2059 if (r->which == Z_External_OPAC)
2061 r = r->u.opac->bibliographicRecord;
2064 oid = r->direct_reference;
2066 if (r->which == Z_External_sutrs)
2067 return record_iconv_return(rec, len,
2068 (char*) r->u.sutrs->buf,
2071 else if (r->which == Z_External_octet)
2073 if (yaz_oid_is_iso2709(oid))
2075 const char *ret_buf = marc_iconv_return(
2076 rec, YAZ_MARC_LINE, 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)
2090 if (!rec->wrbuf_marc)
2091 rec->wrbuf_marc = wrbuf_alloc();
2092 wrbuf_rewind(rec->wrbuf_marc);
2093 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
2094 return record_iconv_return(rec, len,
2095 wrbuf_buf(rec->wrbuf_marc),
2096 wrbuf_len(rec->wrbuf_marc),
2101 else if (!strcmp(type, "xml"))
2103 Z_External *r = (Z_External *) npr->u.databaseRecord;
2104 const Odr_oid *oid = r->direct_reference;
2106 /* render bibliographic record .. */
2107 if (r->which == Z_External_OPAC)
2109 r = r->u.opac->bibliographicRecord;
2112 oid = r->direct_reference;
2115 if (r->which == Z_External_sutrs)
2116 return record_iconv_return(rec, len,
2117 (const char *) r->u.sutrs->buf,
2120 else if (r->which == Z_External_octet)
2122 int marc_decode_type = YAZ_MARC_MARCXML;
2123 if (yaz_oid_is_iso2709(oid))
2125 const char *ret_buf = marc_iconv_return(
2126 rec, marc_decode_type, len,
2127 (const char *) r->u.octet_aligned->buf,
2128 r->u.octet_aligned->len,
2133 return record_iconv_return(rec, len,
2134 (const char *) r->u.octet_aligned->buf,
2135 r->u.octet_aligned->len,
2138 else if (r->which == Z_External_grs1)
2145 else if (!strcmp(type, "raw"))
2147 Z_External *r = (Z_External *) npr->u.databaseRecord;
2149 if (r->which == Z_External_sutrs)
2151 if (len) *len = r->u.sutrs->len;
2152 return (const char *) r->u.sutrs->buf;
2154 else if (r->which == Z_External_octet)
2156 if (len) *len = r->u.octet_aligned->len;
2157 return (const char *) r->u.octet_aligned->buf;
2159 else /* grs-1, explain, OPAC, ... */
2162 return (const char *) npr->u.databaseRecord;
2166 else if (!strcmp (type, "ext"))
2169 return (const char *) npr->u.databaseRecord;
2171 else if (!strcmp (type, "opac"))
2174 Z_External *r = (Z_External *) npr->u.databaseRecord;
2175 if (r->which == Z_External_OPAC)
2177 if (!rec->wrbuf_opac)
2178 rec->wrbuf_opac = wrbuf_alloc();
2179 wrbuf_rewind(rec->wrbuf_opac);
2180 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
2181 return record_iconv_return(rec, len,
2182 wrbuf_buf(rec->wrbuf_opac),
2183 wrbuf_len(rec->wrbuf_opac),
2190 static int strcmp_null(const char *v1, const char *v2)
2196 return strcmp(v1, v2);
2199 static size_t record_hash(int pos)
2203 return pos % RECORD_HASH_SIZE;
2206 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2208 const char *syntax, const char *elementSetName,
2210 Z_SRW_diagnostic *diag)
2212 ZOOM_record_cache rc = 0;
2214 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2215 ZOOM_connection_put_event(r->connection, event);
2217 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2220 && strcmp_null(r->schema, rc->schema) == 0
2221 && strcmp_null(elementSetName,rc->elementSetName) == 0
2222 && strcmp_null(syntax, rc->syntax) == 0)
2227 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2229 rc->rec.wrbuf_marc = 0;
2230 rc->rec.wrbuf_iconv = 0;
2231 rc->rec.wrbuf_opac = 0;
2232 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2234 rc->syntax = odr_strdup_null(r->odr, syntax);
2236 rc->schema = odr_strdup_null(r->odr, r->schema);
2239 rc->next = r->record_hash[record_hash(pos)];
2240 r->record_hash[record_hash(pos)] = rc;
2243 rc->rec.schema = odr_strdup_null(r->odr, schema);
2244 rc->rec.diag_set = 0;
2245 rc->rec.diag_uri = 0;
2246 rc->rec.diag_details = 0;
2252 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2253 if ((cp = strrchr(rc->rec.diag_set, '/')))
2255 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2257 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2258 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2262 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2264 const char *elementSetName)
2266 ZOOM_record_cache rc;
2268 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2272 if (strcmp_null(r->schema, rc->schema))
2274 if (strcmp_null(elementSetName,rc->elementSetName))
2276 if (strcmp_null(syntax, rc->syntax))
2284 static void handle_records(ZOOM_connection c, Z_Records *sr,
2287 ZOOM_resultset resultset;
2289 const char *syntax = 0, *elementSetName = 0;
2293 switch (c->tasks->which)
2295 case ZOOM_TASK_SEARCH:
2296 resultset = c->tasks->u.search.resultset;
2297 start = &c->tasks->u.search.start;
2298 count = &c->tasks->u.search.count;
2299 syntax = c->tasks->u.search.syntax;
2300 elementSetName = c->tasks->u.search.elementSetName;
2302 case ZOOM_TASK_RETRIEVE:
2303 resultset = c->tasks->u.retrieve.resultset;
2304 start = &c->tasks->u.retrieve.start;
2305 count = &c->tasks->u.retrieve.count;
2306 syntax = c->tasks->u.retrieve.syntax;
2307 elementSetName = c->tasks->u.retrieve.elementSetName;
2312 if (sr && sr->which == Z_Records_NSD)
2313 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2314 else if (sr && sr->which == Z_Records_multipleNSD)
2316 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2317 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2319 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2323 if (*count + *start > resultset->size)
2324 *count = resultset->size - *start;
2327 if (sr && sr->which == Z_Records_DBOSD)
2330 NMEM nmem = odr_extract_mem(c->odr_in);
2331 Z_NamePlusRecordList *p =
2332 sr->u.databaseOrSurDiagnostics;
2333 for (i = 0; i<p->num_records; i++)
2335 record_cache_add(resultset, p->records[i], i + *start,
2336 syntax, elementSetName,
2343 yaz_log(log_details,
2344 "handle_records resultset=%p start=%d count=%d",
2345 resultset, *start, *count);
2347 /* transfer our response to search_nmem .. we need it later */
2348 nmem_transfer(odr_getmem(resultset->odr), nmem);
2350 if (present_phase && p->num_records == 0)
2352 /* present response and we didn't get any records! */
2353 Z_NamePlusRecord *myrec =
2354 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2355 record_cache_add(resultset, myrec, *start,
2356 syntax, elementSetName, 0, 0);
2359 else if (present_phase)
2361 /* present response and we didn't get any records! */
2362 Z_NamePlusRecord *myrec =
2363 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2364 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2370 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2372 handle_records(c, pr->records, 1);
2375 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2378 switch (term->which)
2380 case Z_Term_general:
2381 ZOOM_options_setl(opt, name,
2382 (const char *)(term->u.general->buf),
2383 term->u.general->len);
2385 case Z_Term_characterString:
2386 ZOOM_options_set(opt, name, term->u.characterString);
2388 case Z_Term_numeric:
2389 ZOOM_options_set_int(opt, name, *term->u.numeric);
2394 static void handle_queryExpression(ZOOM_options opt, const char *name,
2395 Z_QueryExpression *exp)
2401 case Z_QueryExpression_term:
2402 if (exp->u.term && exp->u.term->queryTerm)
2404 sprintf(opt_name, "%s.term", name);
2405 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2408 case Z_QueryExpression_query:
2413 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2414 Z_OtherInformation *o)
2417 for (i = 0; o && i < o->num_elements; i++)
2419 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2421 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2423 if (ext->which == Z_External_searchResult1)
2426 Z_SearchInfoReport *sr = ext->u.searchResult1;
2429 ZOOM_options_set_int(
2430 resultset->options, "searchresult.size", sr->num);
2432 for (j = 0; j < sr->num; j++)
2434 Z_SearchInfoReport_s *ent =
2435 ext->u.searchResult1->elements[j];
2438 sprintf(pref, "searchresult.%d", j);
2440 if (ent->subqueryId)
2443 sprintf(opt_name, "%s.id", pref);
2444 ZOOM_options_set(resultset->options, opt_name,
2447 if (ent->subqueryExpression)
2450 sprintf(opt_name, "%s.subquery", pref);
2451 handle_queryExpression(resultset->options, opt_name,
2452 ent->subqueryExpression);
2454 if (ent->subqueryInterpretation)
2457 sprintf(opt_name, "%s.interpretation", pref);
2458 handle_queryExpression(resultset->options, opt_name,
2459 ent->subqueryInterpretation);
2461 if (ent->subqueryRecommendation)
2464 sprintf(opt_name, "%s.recommendation", pref);
2465 handle_queryExpression(resultset->options, opt_name,
2466 ent->subqueryRecommendation);
2468 if (ent->subqueryCount)
2471 sprintf(opt_name, "%s.count", pref);
2472 ZOOM_options_set_int(resultset->options, opt_name,
2473 *ent->subqueryCount);
2481 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2483 ZOOM_resultset resultset;
2486 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2489 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2490 ZOOM_connection_put_event(c, event);
2492 resultset = c->tasks->u.search.resultset;
2494 if (sr->resultSetStatus)
2496 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2497 *sr->resultSetStatus);
2499 if (sr->presentStatus)
2501 ZOOM_options_set_int(resultset->options, "presentStatus",
2502 *sr->presentStatus);
2504 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2506 resultset->size = *sr->resultCount;
2507 handle_records(c, sr->records, 0);
2510 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2512 if (res->diagnostics && res->num_diagnostics > 0)
2513 response_diag(c, res->diagnostics[0]);
2516 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2518 NMEM nmem = odr_extract_mem(c->odr_in);
2521 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2523 scan = c->tasks->u.scan.scan;
2525 if (res->entries && res->entries->nonsurrogateDiagnostics)
2526 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2527 scan->scan_response = res;
2528 scan->srw_scan_response = 0;
2529 nmem_transfer(odr_getmem(scan->odr), nmem);
2531 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2532 if (res->positionOfTerm)
2533 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2534 if (res->scanStatus)
2535 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2536 if (res->numberOfEntriesReturned)
2537 ZOOM_options_set_int(scan->options, "number",
2538 *res->numberOfEntriesReturned);
2543 static zoom_ret send_sort(ZOOM_connection c,
2544 ZOOM_resultset resultset)
2547 resultset->r_sort_spec = 0;
2548 if (resultset->r_sort_spec)
2550 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2551 Z_SortRequest *req = apdu->u.sortRequest;
2553 req->num_inputResultSetNames = 1;
2554 req->inputResultSetNames = (Z_InternationalString **)
2555 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2556 req->inputResultSetNames[0] =
2557 odr_strdup(c->odr_out, resultset->setname);
2558 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2559 req->sortSequence = resultset->r_sort_spec;
2560 resultset->r_sort_spec = 0;
2561 return send_APDU(c, apdu);
2563 return zoom_complete;
2566 static zoom_ret send_present(ZOOM_connection c)
2569 Z_PresentRequest *req = 0;
2571 const char *syntax = 0;
2572 const char *elementSetName = 0;
2573 ZOOM_resultset resultset;
2578 yaz_log(log_details, "%p send_present no tasks", c);
2579 return zoom_complete;
2582 switch (c->tasks->which)
2584 case ZOOM_TASK_SEARCH:
2585 resultset = c->tasks->u.search.resultset;
2586 start = &c->tasks->u.search.start;
2587 count = &c->tasks->u.search.count;
2588 syntax = c->tasks->u.search.syntax;
2589 elementSetName = c->tasks->u.search.elementSetName;
2591 case ZOOM_TASK_RETRIEVE:
2592 resultset = c->tasks->u.retrieve.resultset;
2593 start = &c->tasks->u.retrieve.start;
2594 count = &c->tasks->u.retrieve.count;
2595 syntax = c->tasks->u.retrieve.syntax;
2596 elementSetName = c->tasks->u.retrieve.elementSetName;
2599 return zoom_complete;
2601 yaz_log(log_details, "%p send_present start=%d count=%d",
2604 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2606 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2609 if (c->error) /* don't continue on error */
2610 return zoom_complete;
2611 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2612 resultset, *start, *count);
2614 for (i = 0; i < *count; i++)
2617 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2622 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2623 ZOOM_connection_put_event(c, event);
2631 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2632 return zoom_complete;
2635 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2636 req = apdu->u.presentRequest;
2639 yaz_log(log_details, "%p send_present skip=%d", c, i);
2641 *req->resultSetStartPoint = *start + 1;
2642 *req->numberOfRecordsRequested = resultset->step>0 ?
2643 resultset->step : *count;
2644 if (*req->numberOfRecordsRequested + *start > resultset->size)
2645 *req->numberOfRecordsRequested = resultset->size - *start;
2646 assert(*req->numberOfRecordsRequested > 0);
2648 if (syntax && *syntax)
2649 req->preferredRecordSyntax =
2650 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2652 if (resultset->schema && *resultset->schema)
2654 Z_RecordComposition *compo = (Z_RecordComposition *)
2655 odr_malloc(c->odr_out, sizeof(*compo));
2657 req->recordComposition = compo;
2658 compo->which = Z_RecordComp_complex;
2659 compo->u.complex = (Z_CompSpec *)
2660 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2661 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2662 odr_malloc(c->odr_out, sizeof(bool_t));
2663 *compo->u.complex->selectAlternativeSyntax = 0;
2665 compo->u.complex->generic = (Z_Specification *)
2666 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2668 compo->u.complex->generic->which = Z_Schema_oid;
2669 compo->u.complex->generic->schema.oid = (Odr_oid *)
2670 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2672 if (!compo->u.complex->generic->schema.oid)
2674 /* OID wasn't a schema! Try record syntax instead. */
2676 compo->u.complex->generic->schema.oid = (Odr_oid *)
2677 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2679 if (elementSetName && *elementSetName)
2681 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2682 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2683 compo->u.complex->generic->elementSpec->which =
2684 Z_ElementSpec_elementSetName;
2685 compo->u.complex->generic->elementSpec->u.elementSetName =
2686 odr_strdup(c->odr_out, elementSetName);
2689 compo->u.complex->generic->elementSpec = 0;
2690 compo->u.complex->num_dbSpecific = 0;
2691 compo->u.complex->dbSpecific = 0;
2692 compo->u.complex->num_recordSyntax = 0;
2693 compo->u.complex->recordSyntax = 0;
2695 else if (elementSetName && *elementSetName)
2697 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2698 odr_malloc(c->odr_out, sizeof(*esn));
2699 Z_RecordComposition *compo = (Z_RecordComposition *)
2700 odr_malloc(c->odr_out, sizeof(*compo));
2702 esn->which = Z_ElementSetNames_generic;
2703 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2704 compo->which = Z_RecordComp_simple;
2705 compo->u.simple = esn;
2706 req->recordComposition = compo;
2708 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2709 return send_APDU(c, apdu);
2712 ZOOM_API(ZOOM_scanset)
2713 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2716 ZOOM_query q = ZOOM_query_create();
2718 ZOOM_query_prefix(q, start);
2720 s = ZOOM_connection_scan1(c, q);
2721 ZOOM_query_destroy(q);
2726 ZOOM_API(ZOOM_scanset)
2727 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2729 ZOOM_scanset scan = 0;
2733 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2734 scan->connection = c;
2735 scan->odr = odr_createmem(ODR_DECODE);
2736 scan->options = ZOOM_options_create_with_parent(c->options);
2738 scan->scan_response = 0;
2739 scan->srw_scan_response = 0;
2743 scan->databaseNames = set_DatabaseNames(c, c->options,
2744 &scan->num_databaseNames,
2749 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2750 task->u.scan.scan = scan;
2755 while (ZOOM_event(1, &c))
2763 ZOOM_scanset_destroy(ZOOM_scanset scan)
2768 if (scan->refcount == 0)
2770 ZOOM_query_destroy(scan->query);
2772 odr_destroy(scan->odr);
2774 ZOOM_options_destroy(scan->options);
2779 static zoom_ret send_package(ZOOM_connection c)
2783 yaz_log(log_details, "%p send_package", c);
2785 return zoom_complete;
2786 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2788 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2789 ZOOM_connection_put_event(c, event);
2791 c->buf_out = c->tasks->u.package->buf_out;
2792 c->len_out = c->tasks->u.package->len_out;
2797 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2800 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2801 Z_ScanRequest *req = apdu->u.scanRequest;
2803 yaz_log(log_details, "%p send_scan", c);
2805 return zoom_complete;
2806 assert (c->tasks->which == ZOOM_TASK_SCAN);
2807 scan = c->tasks->u.scan.scan;
2809 /* Z39.50 scan can only carry RPN */
2810 if (scan->query->z_query->which == Z_Query_type_1 ||
2811 scan->query->z_query->which == Z_Query_type_101)
2813 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2814 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2817 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2820 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2822 yaz_query_charset_convert_rpnquery(
2823 rpn, c->odr_out, cd);
2824 yaz_iconv_close(cd);
2827 req->attributeSet = rpn->attributeSetId;
2828 if (!req->attributeSet)
2829 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2830 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2831 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2833 req->termListAndStartPoint =
2834 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2838 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2839 return zoom_complete;
2844 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2845 return zoom_complete;
2848 *req->numberOfTermsRequested =
2849 ZOOM_options_get_int(scan->options, "number", 10);
2851 req->preferredPositionInResponse =
2852 odr_intdup(c->odr_out,
2853 ZOOM_options_get_int(scan->options, "position", 1));
2856 odr_intdup(c->odr_out,
2857 ZOOM_options_get_int(scan->options, "stepSize", 0));
2859 req->databaseNames = scan->databaseNames;
2860 req->num_databaseNames = scan->num_databaseNames;
2862 return send_APDU(c, apdu);
2866 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2870 const char *option_val = 0;
2873 return zoom_complete;
2874 assert (c->tasks->which == ZOOM_TASK_SCAN);
2875 scan = c->tasks->u.scan.scan;
2877 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2879 /* SRU scan can only carry CQL and PQF */
2880 if (scan->query->z_query->which == Z_Query_type_104)
2882 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2883 sr->u.scan_request->scanClause.cql = scan->query->query_string;
2885 else if (scan->query->z_query->which == Z_Query_type_1
2886 || scan->query->z_query->which == Z_Query_type_101)
2888 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2889 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2893 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2894 return zoom_complete;
2897 sr->u.scan_request->maximumTerms = odr_intdup(
2898 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2900 sr->u.scan_request->responsePosition = odr_intdup(
2901 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2903 option_val = ZOOM_options_get(scan->options, "extraArgs");
2905 sr->extra_args = odr_strdup(c->odr_out, option_val);
2906 return send_srw(c, sr);
2909 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2911 return zoom_complete;
2917 ZOOM_scanset_size(ZOOM_scanset scan)
2922 if (scan->scan_response && scan->scan_response->entries)
2923 return scan->scan_response->entries->num_entries;
2924 else if (scan->srw_scan_response)
2925 return scan->srw_scan_response->num_terms;
2929 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2931 const char **value_term, size_t *value_len,
2932 const char **disp_term, size_t *disp_len)
2934 size_t noent = ZOOM_scanset_size(scan);
2943 if (pos >= noent || pos < 0)
2945 if (scan->scan_response)
2947 Z_ScanResponse *res = scan->scan_response;
2948 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2950 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2952 *value_term = (const char *) t->term->u.general->buf;
2953 *value_len = t->term->u.general->len;
2956 *disp_term = t->displayTerm;
2957 *disp_len = strlen(*disp_term);
2959 else if (t->term->which == Z_Term_general)
2961 *disp_term = (const char *) t->term->u.general->buf;
2962 *disp_len = t->term->u.general->len;
2964 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2967 if (scan->srw_scan_response)
2969 Z_SRW_scanResponse *res = scan->srw_scan_response;
2970 Z_SRW_scanTerm *t = res->terms + pos;
2973 *value_term = t->value;
2974 *value_len = strlen(*value_term);
2977 *disp_term = t->displayTerm;
2979 *disp_term = t->value;
2980 *disp_len = strlen(*disp_term);
2981 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
2986 ZOOM_API(const char *)
2987 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
2990 const char *value_term = 0;
2991 size_t value_len = 0;
2992 const char *disp_term = 0;
2993 size_t disp_len = 0;
2995 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
2996 &disp_term, &disp_len);
3002 ZOOM_API(const char *)
3003 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3006 const char *value_term = 0;
3007 size_t value_len = 0;
3008 const char *disp_term = 0;
3009 size_t disp_len = 0;
3011 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3012 &disp_term, &disp_len);
3018 ZOOM_API(const char *)
3019 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3021 return ZOOM_options_get(scan->options, key);
3025 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3028 ZOOM_options_set(scan->options, key, val);
3031 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3034 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3035 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3037 str = ZOOM_options_get(p->options, "package-name");
3039 req->packageName = odr_strdup(p->odr_out, str);
3041 str = ZOOM_options_get(p->options, "user-id");
3043 req->userId = odr_strdup_null(p->odr_out, str);
3045 req->packageType = odr_oiddup(p->odr_out, oid);
3047 str = ZOOM_options_get(p->options, "function");
3050 if (!strcmp (str, "create"))
3051 *req->function = Z_ExtendedServicesRequest_create;
3052 if (!strcmp (str, "delete"))
3053 *req->function = Z_ExtendedServicesRequest_delete;
3054 if (!strcmp (str, "modify"))
3055 *req->function = Z_ExtendedServicesRequest_modify;
3058 str = ZOOM_options_get(p->options, "waitAction");
3061 if (!strcmp (str, "wait"))
3062 *req->waitAction = Z_ExtendedServicesRequest_wait;
3063 if (!strcmp (str, "waitIfPossible"))
3064 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3065 if (!strcmp (str, "dontWait"))
3066 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3067 if (!strcmp (str, "dontReturnPackage"))
3068 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3073 static const char *ill_array_lookup(void *clientData, const char *idx)
3075 ZOOM_package p = (ZOOM_package) clientData;
3076 return ZOOM_options_get(p->options, idx+4);
3079 static Z_External *encode_ill_request(ZOOM_package p)
3081 ODR out = p->odr_out;
3084 struct ill_get_ctl ctl;
3086 ctl.odr = p->odr_out;
3088 ctl.f = ill_array_lookup;
3090 req = ill_get_ILLRequest(&ctl, "ill", 0);
3092 if (!ill_Request(out, &req, 0, 0))
3094 int ill_request_size;
3095 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3096 if (ill_request_buf)
3097 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3102 int illRequest_size = 0;
3103 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3105 r = (Z_External *) odr_malloc(out, sizeof(*r));
3106 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3107 r->indirect_reference = 0;
3109 r->which = Z_External_single;
3111 r->u.single_ASN1_type =
3112 odr_create_Odr_oct(out,
3113 (unsigned char *)illRequest_buf,
3119 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3121 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3124 req->which = Z_IOItemOrder_esRequest;
3125 req->u.esRequest = (Z_IORequest *)
3126 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3128 /* to keep part ... */
3129 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3130 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3131 req->u.esRequest->toKeep->supplDescription = 0;
3132 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3133 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3135 str = ZOOM_options_get(p->options, "contact-name");
3136 req->u.esRequest->toKeep->contact->name =
3137 odr_strdup_null(p->odr_out, str);
3139 str = ZOOM_options_get(p->options, "contact-phone");
3140 req->u.esRequest->toKeep->contact->phone =
3141 odr_strdup_null(p->odr_out, str);
3143 str = ZOOM_options_get(p->options, "contact-email");
3144 req->u.esRequest->toKeep->contact->email =
3145 odr_strdup_null(p->odr_out, str);
3147 req->u.esRequest->toKeep->addlBilling = 0;
3149 /* not to keep part ... */
3150 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3151 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3153 str = ZOOM_options_get(p->options, "itemorder-setname");
3158 req->u.esRequest->notToKeep->resultSetItem = 0;
3161 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3162 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3164 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3165 odr_strdup(p->odr_out, str);
3166 req->u.esRequest->notToKeep->resultSetItem->item =
3167 (int *) odr_malloc(p->odr_out, sizeof(int));
3169 str = ZOOM_options_get(p->options, "itemorder-item");
3170 *req->u.esRequest->notToKeep->resultSetItem->item =
3171 (str ? atoi(str) : 1);
3174 str = ZOOM_options_get(p->options, "doc");
3177 req->u.esRequest->notToKeep->itemRequest =
3178 z_ext_record_xml(p->odr_out, str, strlen(str));
3181 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3186 Z_APDU *create_admin_package(ZOOM_package p, int type,
3187 Z_ESAdminOriginPartToKeep **toKeepP,
3188 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3190 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3193 Z_ESAdminOriginPartToKeep *toKeep;
3194 Z_ESAdminOriginPartNotToKeep *notToKeep;
3195 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3196 const char *first_db = "Default";
3198 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3203 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3205 r->indirect_reference = 0;
3206 r->which = Z_External_ESAdmin;
3208 r->u.adminService = (Z_Admin *)
3209 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3210 r->u.adminService->which = Z_Admin_esRequest;
3211 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3212 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3214 toKeep = r->u.adminService->u.esRequest->toKeep =
3215 (Z_ESAdminOriginPartToKeep *)
3216 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3217 toKeep->which = type;
3218 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3219 toKeep->u.create = odr_nullval();
3220 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3222 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3223 (Z_ESAdminOriginPartNotToKeep *)
3224 odr_malloc(p->odr_out,
3225 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3226 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3227 notToKeep->u.recordsWillFollow = odr_nullval();
3231 *notToKeepP = notToKeep;
3236 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3238 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3239 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3240 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3241 const char *doc = ZOOM_options_get(p->options, "doc");
3246 req->taskSpecificParameters = ext;
3247 ext->direct_reference = req->packageType;
3248 ext->descriptor = 0;
3249 ext->indirect_reference = 0;
3251 ext->which = Z_External_octet;
3252 ext->u.single_ASN1_type =
3253 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc,
3258 static Z_APDU *create_update_package(ZOOM_package p)
3261 const char *first_db = "Default";
3263 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3264 const char *action = ZOOM_options_get(p->options, "action");
3265 const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque");
3266 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3267 const char *record_buf = ZOOM_options_get(p->options, "record");
3268 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3269 const char *version = ZOOM_options_get(p->options, "updateVersion");
3271 const char *correlationInfo_note =
3272 ZOOM_options_get(p->options, "correlationInfo.note");
3273 const char *correlationInfo_id =
3274 ZOOM_options_get(p->options, "correlationInfo.id");
3276 Odr_oid *syntax_oid = 0;
3277 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3285 record_buf = "void";
3286 syntax_str = "SUTRS";
3291 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3292 CLASS_RECSYN, syntax_str,
3304 package_oid = yaz_oid_extserv_database_update_first_version;
3305 /* old update does not support specialUpdate */
3307 action = "recordInsert";
3311 action = "specialUpdate";
3312 package_oid = yaz_oid_extserv_database_update_second_version;
3316 action = "specialUpdate";
3317 package_oid = yaz_oid_extserv_database_update;
3323 if (!strcmp(action, "recordInsert"))
3324 action_no = Z_IUOriginPartToKeep_recordInsert;
3325 else if (!strcmp(action, "recordReplace"))
3326 action_no = Z_IUOriginPartToKeep_recordReplace;
3327 else if (!strcmp(action, "recordDelete"))
3328 action_no = Z_IUOriginPartToKeep_recordDelete;
3329 else if (!strcmp(action, "elementUpdate"))
3330 action_no = Z_IUOriginPartToKeep_elementUpdate;
3331 else if (!strcmp(action, "specialUpdate"))
3332 action_no = Z_IUOriginPartToKeep_specialUpdate;
3336 apdu = create_es_package(p, package_oid);
3339 Z_IUOriginPartToKeep *toKeep;
3340 Z_IUSuppliedRecords *notToKeep;
3341 Z_External *r = (Z_External *)
3342 odr_malloc(p->odr_out, sizeof(*r));
3343 const char *elementSetName =
3344 ZOOM_options_get(p->options, "elementSetName");
3346 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3348 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3350 r->which = Z_External_update;
3351 r->indirect_reference = 0;
3352 r->u.update = (Z_IUUpdate *)
3353 odr_malloc(p->odr_out, sizeof(*r->u.update));
3355 r->u.update->which = Z_IUUpdate_esRequest;
3356 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3357 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3358 toKeep = r->u.update->u.esRequest->toKeep =
3359 (Z_IUOriginPartToKeep *)
3360 odr_malloc(p->odr_out, sizeof(*toKeep));
3362 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3365 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3367 toKeep->actionQualifier = 0;
3368 toKeep->action = odr_intdup(p->odr_out, action_no);
3370 notToKeep = r->u.update->u.esRequest->notToKeep =
3371 (Z_IUSuppliedRecords *)
3372 odr_malloc(p->odr_out, sizeof(*notToKeep));
3374 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3375 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3376 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3377 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3378 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3381 notToKeep->elements[0]->u.opaque =
3382 odr_create_Odr_oct(p->odr_out,
3383 (const unsigned char *) recordIdOpaque,
3384 strlen(recordIdOpaque));
3386 else if (recordIdNumber)
3388 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3390 notToKeep->elements[0]->u.number =
3391 odr_intdup(p->odr_out, atoi(recordIdNumber));
3394 notToKeep->elements[0]->u.opaque = 0;
3395 notToKeep->elements[0]->supplementalId = 0;
3396 if (correlationInfo_note || correlationInfo_id)
3398 Z_IUCorrelationInfo *ci;
3399 ci = notToKeep->elements[0]->correlationInfo =
3400 odr_malloc(p->odr_out, sizeof(*ci));
3401 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3402 ci->id = correlationInfo_id ?
3403 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3406 notToKeep->elements[0]->correlationInfo = 0;
3407 notToKeep->elements[0]->record =
3408 z_ext_record_oid(p->odr_out, syntax_oid,
3409 record_buf, strlen(record_buf));
3413 ODR print = odr_createmem(ODR_PRINT);
3415 z_APDU(print, &apdu, 0, 0);
3422 ZOOM_package_send(ZOOM_package p, const char *type)
3429 odr_reset(p->odr_out);
3432 if (!strcmp(type, "itemorder"))
3434 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3437 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3439 r->direct_reference =
3440 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3442 r->which = Z_External_itemOrder;
3443 r->indirect_reference = 0;
3444 r->u.itemOrder = encode_item_order(p);
3446 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3449 else if (!strcmp(type, "create")) /* create database */
3451 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3454 else if (!strcmp(type, "drop")) /* drop database */
3456 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3459 else if (!strcmp(type, "commit")) /* commit changes */
3461 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3464 else if (!strcmp(type, "update")) /* update record(s) */
3466 apdu = create_update_package(p);
3468 else if (!strcmp(type, "xmlupdate"))
3470 apdu = create_xmlupdate_package(p);
3474 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3478 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3479 task->u.package = p;
3480 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3481 p->buf_out = (char *) xmalloc(p->len_out);
3482 memcpy(p->buf_out, buf, p->len_out);
3487 while (ZOOM_event(1, &c))
3494 ZOOM_API(ZOOM_package)
3495 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3497 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3500 p->odr_out = odr_createmem(ODR_ENCODE);
3501 p->options = ZOOM_options_create_with_parent2(options, c->options);
3509 ZOOM_package_destroy(ZOOM_package p)
3514 if (p->refcount == 0)
3516 odr_destroy(p->odr_out);
3519 ZOOM_options_destroy(p->options);
3524 ZOOM_API(const char *)
3525 ZOOM_package_option_get(ZOOM_package p, const char *key)
3527 return ZOOM_options_get(p->options, key);
3532 ZOOM_package_option_set(ZOOM_package p, const char *key,
3535 ZOOM_options_set(p->options, key, val);
3538 static int ZOOM_connection_exec_task(ZOOM_connection c)
3540 ZOOM_task task = c->tasks;
3541 zoom_ret ret = zoom_complete;
3545 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3546 c, task->which, task->running);
3547 if (c->error != ZOOM_ERROR_NONE)
3549 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3550 "removing tasks because of error = %d", c, c->error);
3551 ZOOM_connection_remove_tasks(c);
3556 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3557 "task already running", c);
3561 ret = zoom_complete;
3562 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3564 switch (task->which)
3566 case ZOOM_TASK_SEARCH:
3567 if (c->proto == PROTO_HTTP)
3568 ret = ZOOM_connection_srw_send_search(c);
3570 ret = ZOOM_connection_send_search(c);
3572 case ZOOM_TASK_RETRIEVE:
3573 if (c->proto == PROTO_HTTP)
3574 ret = ZOOM_connection_srw_send_search(c);
3576 ret = send_present(c);
3578 case ZOOM_TASK_CONNECT:
3579 ret = do_connect(c);
3581 case ZOOM_TASK_SCAN:
3582 if (c->proto == PROTO_HTTP)
3583 ret = ZOOM_connection_srw_send_scan(c);
3585 ret = ZOOM_connection_send_scan(c);
3587 case ZOOM_TASK_PACKAGE:
3588 ret = send_package(c);
3590 case ZOOM_TASK_SORT:
3591 c->tasks->u.sort.resultset->r_sort_spec =
3592 c->tasks->u.sort.q->sort_spec;
3593 ret = send_sort(c, c->tasks->u.sort.resultset);
3599 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3600 "remove tasks because no connection exist", c);
3601 ZOOM_connection_remove_tasks(c);
3603 if (ret == zoom_complete)
3605 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3606 "task removed (complete)", c);
3607 ZOOM_connection_remove_task(c);
3610 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3615 static zoom_ret send_sort_present(ZOOM_connection c)
3617 zoom_ret r = zoom_complete;
3619 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3620 r = send_sort(c, c->tasks->u.search.resultset);
3621 if (r == zoom_complete)
3622 r = send_present(c);
3626 static int es_response(ZOOM_connection c,
3627 Z_ExtendedServicesResponse *res)
3629 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3631 if (res->diagnostics && res->num_diagnostics > 0)
3632 response_diag(c, res->diagnostics[0]);
3633 if (res->taskPackage &&
3634 res->taskPackage->which == Z_External_extendedService)
3636 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3637 Odr_oct *id = taskPackage->targetReference;
3640 ZOOM_options_setl(c->tasks->u.package->options,
3641 "targetReference", (char*) id->buf, id->len);
3643 if (res->taskPackage &&
3644 res->taskPackage->which == Z_External_octet)
3646 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3647 ZOOM_options_setl(c->tasks->u.package->options,
3648 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3653 static void interpret_init_diag(ZOOM_connection c,
3654 Z_DiagnosticFormat *diag)
3658 Z_DiagnosticFormat_s *ds = diag->elements[0];
3659 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3660 response_default_diag(c, ds->u.defaultDiagRec);
3665 static void interpret_otherinformation_field(ZOOM_connection c,
3666 Z_OtherInformation *ui)
3669 for (i = 0; i < ui->num_elements; i++)
3671 Z_OtherInformationUnit *unit = ui->list[i];
3672 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3673 unit->information.externallyDefinedInfo &&
3674 unit->information.externallyDefinedInfo->which ==
3677 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3683 static void set_init_option(const char *name, void *clientData) {
3684 ZOOM_connection c = (ZOOM_connection) clientData;
3687 sprintf(buf, "init_opt_%.70s", name);
3688 ZOOM_connection_option_set(c, buf, "1");
3692 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3694 Z_InitResponse *initrs;
3696 ZOOM_connection_set_mask(c, 0);
3697 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3700 case Z_APDU_initResponse:
3701 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3702 initrs = apdu->u.initResponse;
3703 ZOOM_connection_option_set(c, "serverImplementationId",
3704 initrs->implementationId ?
3705 initrs->implementationId : "");
3706 ZOOM_connection_option_set(c, "serverImplementationName",
3707 initrs->implementationName ?
3708 initrs->implementationName : "");
3709 ZOOM_connection_option_set(c, "serverImplementationVersion",
3710 initrs->implementationVersion ?
3711 initrs->implementationVersion : "");
3712 /* Set the three old options too, for old applications */
3713 ZOOM_connection_option_set(c, "targetImplementationId",
3714 initrs->implementationId ?
3715 initrs->implementationId : "");
3716 ZOOM_connection_option_set(c, "targetImplementationName",
3717 initrs->implementationName ?
3718 initrs->implementationName : "");
3719 ZOOM_connection_option_set(c, "targetImplementationVersion",
3720 initrs->implementationVersion ?
3721 initrs->implementationVersion : "");
3723 /* Make initrs->options available as ZOOM-level options */
3724 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3726 if (!*initrs->result)
3728 Z_External *uif = initrs->userInformationField;
3730 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3732 if (uif && uif->which == Z_External_userInfo1)
3733 interpret_otherinformation_field(c, uif->u.userInfo1);
3738 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3739 yaz_oid_userinfo_cookie, 1, 0);
3740 xfree(c->cookie_in);
3743 c->cookie_in = xstrdup(cookie);
3744 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3745 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3746 c->support_named_resultsets = 1;
3749 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3750 ZOOM_connection_remove_task(c);
3752 ZOOM_connection_exec_task(c);
3754 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3756 NMEM tmpmem = nmem_create();
3757 Z_CharSetandLanguageNegotiation *p =
3758 yaz_get_charneg_record(initrs->otherInfo);
3762 char *charset = NULL, *lang = NULL;
3765 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3766 yaz_log(log_details, "%p recv_apdu target accepted: "
3767 "charset %s, language %s, select %d",
3769 charset ? charset : "none", lang ? lang : "none", sel);
3771 ZOOM_connection_option_set(c, "negotiation-charset",
3774 ZOOM_connection_option_set(c, "negotiation-lang",
3777 ZOOM_connection_option_set(
3778 c, "negotiation-charset-in-effect-for-records",
3779 (sel != 0) ? "1" : "0");
3780 nmem_destroy(tmpmem);
3784 case Z_APDU_searchResponse:
3785 yaz_log(log_api, "%p recv_apdu Search response", c);
3786 handle_search_response(c, apdu->u.searchResponse);
3787 if (send_sort_present(c) == zoom_complete)
3788 ZOOM_connection_remove_task(c);
3790 case Z_APDU_presentResponse:
3791 yaz_log(log_api, "%p recv_apdu Present response", c);
3792 handle_present_response(c, apdu->u.presentResponse);
3793 if (send_present(c) == zoom_complete)
3794 ZOOM_connection_remove_task(c);
3796 case Z_APDU_sortResponse:
3797 yaz_log(log_api, "%p recv_apdu Sort response", c);
3798 sort_response(c, apdu->u.sortResponse);
3799 if (send_present(c) == zoom_complete)
3800 ZOOM_connection_remove_task(c);
3802 case Z_APDU_scanResponse:
3803 yaz_log(log_api, "%p recv_apdu Scan response", c);
3804 scan_response(c, apdu->u.scanResponse);
3805 ZOOM_connection_remove_task(c);
3807 case Z_APDU_extendedServicesResponse:
3808 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3809 es_response(c, apdu->u.extendedServicesResponse);
3810 ZOOM_connection_remove_task(c);
3813 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3814 if (!ZOOM_test_reconnect(c))
3816 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3821 yaz_log(log_api, "%p Received unknown PDU", c);
3822 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3828 static void handle_srw_response(ZOOM_connection c,
3829 Z_SRW_searchRetrieveResponse *res)
3831 ZOOM_resultset resultset = 0;
3836 const char *syntax, *elementSetName;
3841 switch(c->tasks->which)
3843 case ZOOM_TASK_SEARCH:
3844 resultset = c->tasks->u.search.resultset;
3845 start = &c->tasks->u.search.start;
3846 syntax = c->tasks->u.search.syntax;
3847 elementSetName = c->tasks->u.search.elementSetName;
3849 case ZOOM_TASK_RETRIEVE:
3850 resultset = c->tasks->u.retrieve.resultset;
3851 start = &c->tasks->u.retrieve.start;
3852 syntax = c->tasks->u.retrieve.syntax;
3853 elementSetName = c->tasks->u.retrieve.elementSetName;
3858 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3859 ZOOM_connection_put_event(c, event);
3861 resultset->size = 0;
3863 if (res->resultSetId)
3864 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
3866 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
3868 if (res->numberOfRecords)
3869 resultset->size = *res->numberOfRecords;
3871 for (i = 0; i<res->num_records; i++)
3874 Z_SRW_record *sru_rec;
3875 Z_SRW_diagnostic *diag = 0;
3878 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
3879 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
3881 if (res->records[i].recordPosition &&
3882 *res->records[i].recordPosition > 0)
3883 pos = *res->records[i].recordPosition - 1;
3887 sru_rec = &res->records[i];
3889 npr->databaseName = 0;
3890 npr->which = Z_NamePlusRecord_databaseRecord;
3891 npr->u.databaseRecord = (Z_External *)
3892 odr_malloc(c->odr_in, sizeof(Z_External));
3893 npr->u.databaseRecord->descriptor = 0;
3894 npr->u.databaseRecord->direct_reference =
3895 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
3896 npr->u.databaseRecord->which = Z_External_octet;
3898 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
3899 odr_malloc(c->odr_in, sizeof(Odr_oct));
3900 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
3901 sru_rec->recordData_buf;
3902 npr->u.databaseRecord->u.octet_aligned->len =
3903 npr->u.databaseRecord->u.octet_aligned->size =
3904 sru_rec->recordData_len;
3906 if (sru_rec->recordSchema
3907 && !strcmp(sru_rec->recordSchema,
3908 "info:srw/schema/1/diagnostics-v1.1"))
3910 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
3911 sru_rec->recordData_len,
3915 record_cache_add(resultset, npr, pos, syntax, elementSetName,
3916 sru_rec->recordSchema, diag);
3918 if (res->num_diagnostics > 0)
3919 set_SRU_error(c, &res->diagnostics[0]);
3920 nmem = odr_extract_mem(c->odr_in);
3921 nmem_transfer(odr_getmem(resultset->odr), nmem);
3927 static void handle_srw_scan_response(ZOOM_connection c,
3928 Z_SRW_scanResponse *res)
3930 NMEM nmem = odr_extract_mem(c->odr_in);
3933 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
3935 scan = c->tasks->u.scan.scan;
3937 if (res->num_diagnostics > 0)
3938 set_SRU_error(c, &res->diagnostics[0]);
3940 scan->scan_response = 0;
3941 scan->srw_scan_response = res;
3942 nmem_transfer(odr_getmem(scan->odr), nmem);
3944 ZOOM_options_set_int(scan->options, "number", res->num_terms);
3950 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
3953 const char *addinfo = 0;
3954 const char *connection_head = z_HTTP_header_lookup(hres->headers,
3956 ZOOM_connection_set_mask(c, 0);
3957 yaz_log(log_details, "%p handle_http", c);
3959 if (!yaz_srw_check_content_type(hres))
3960 addinfo = "content-type";
3963 Z_SOAP *soap_package = 0;
3965 Z_SOAP_Handler soap_handlers[2] = {
3966 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
3969 ret = z_soap_codec(o, &soap_package,
3970 &hres->content_buf, &hres->content_len,
3972 if (!ret && soap_package->which == Z_SOAP_generic &&
3973 soap_package->u.generic->no == 0)
3975 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
3977 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
3978 if (sr->which == Z_SRW_searchRetrieve_response)
3979 handle_srw_response(c, sr->u.response);
3980 else if (sr->which == Z_SRW_scan_response)
3981 handle_srw_scan_response(c, sr->u.scan_response);
3985 else if (!ret && (soap_package->which == Z_SOAP_fault
3986 || soap_package->which == Z_SOAP_error))
3988 set_HTTP_error(c, hres->code,
3989 soap_package->u.fault->fault_code,
3990 soap_package->u.fault->fault_string);
3997 if (hres->code != 200)
3998 set_HTTP_error(c, hres->code, 0, 0);
4000 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4003 ZOOM_connection_remove_task(c);
4004 if (!strcmp(hres->version, "1.0"))
4006 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4007 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4012 /* HTTP 1.1: only if no close we stay alive .. */
4013 if (connection_head && !strcmp(connection_head, "close"))
4019 static int do_read(ZOOM_connection c)
4024 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4025 ZOOM_connection_put_event(c, event);
4027 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4028 more = cs_more(c->cs);
4029 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4034 if (!ZOOM_test_reconnect(c))
4036 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4045 odr_reset(c->odr_in);
4046 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4047 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4048 ZOOM_connection_put_event(c, event);
4050 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4053 int err = odr_geterrorx(c->odr_in, &x);
4055 const char *element = odr_getelement(c->odr_in);
4056 yaz_snprintf(msg, sizeof(msg),
4057 "ODR code %d:%d element=%s offset=%d",
4058 err, x, element ? element : "<unknown>",
4059 odr_offset(c->odr_in));
4060 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4063 FILE *ber_file = yaz_log_file();
4065 odr_dumpBER(ber_file, c->buf_in, r);
4069 else if (gdu->which == Z_GDU_Z3950)
4070 recv_apdu(c, gdu->u.z3950);
4071 else if (gdu->which == Z_GDU_HTTP_Response)
4074 handle_http(c, gdu->u.HTTP_Response);
4076 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4080 c->reconnect_ok = 0;
4085 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4090 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4091 ZOOM_connection_put_event(c, event);
4093 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4094 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4096 yaz_log(log_details, "%p do_write_ex write failed", c);
4097 if (ZOOM_test_reconnect(c))
4099 return zoom_pending;
4101 if (c->state == STATE_CONNECTING)
4102 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4104 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4106 return zoom_complete;
4110 int mask = ZOOM_SELECT_EXCEPT;
4111 if (c->cs->io_pending & CS_WANT_WRITE)
4112 mask += ZOOM_SELECT_WRITE;
4113 if (c->cs->io_pending & CS_WANT_READ)
4114 mask += ZOOM_SELECT_READ;
4115 ZOOM_connection_set_mask(c, mask);
4116 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4121 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4122 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4125 return zoom_pending;
4128 static zoom_ret do_write(ZOOM_connection c)
4130 return do_write_ex(c, c->buf_out, c->len_out);
4134 ZOOM_API(const char *)
4135 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4137 return ZOOM_options_get(c->options, key);
4140 ZOOM_API(const char *)
4141 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4143 return ZOOM_options_getl(c->options, key, lenp);
4147 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4150 ZOOM_options_set(c->options, key, val);
4154 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4155 const char *val, int len)
4157 ZOOM_options_setl(c->options, key, val, len);
4160 ZOOM_API(const char *)
4161 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4163 return ZOOM_options_get(r->options, key);
4167 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4170 ZOOM_options_set(r->options, key, val);
4175 ZOOM_connection_errcode(ZOOM_connection c)
4177 return ZOOM_connection_error(c, 0, 0);
4180 ZOOM_API(const char *)
4181 ZOOM_connection_errmsg(ZOOM_connection c)
4184 ZOOM_connection_error(c, &msg, 0);
4188 ZOOM_API(const char *)
4189 ZOOM_connection_addinfo(ZOOM_connection c)
4191 const char *addinfo;
4192 ZOOM_connection_error(c, 0, &addinfo);
4196 ZOOM_API(const char *)
4197 ZOOM_connection_diagset(ZOOM_connection c)
4199 const char *diagset;
4200 ZOOM_connection_error_x(c, 0, 0, &diagset);
4204 ZOOM_API(const char *)
4205 ZOOM_diag_str(int error)
4209 case ZOOM_ERROR_NONE:
4211 case ZOOM_ERROR_CONNECT:
4212 return "Connect failed";
4213 case ZOOM_ERROR_MEMORY:
4214 return "Out of memory";
4215 case ZOOM_ERROR_ENCODE:
4216 return "Encoding failed";
4217 case ZOOM_ERROR_DECODE:
4218 return "Decoding failed";
4219 case ZOOM_ERROR_CONNECTION_LOST:
4220 return "Connection lost";
4221 case ZOOM_ERROR_INIT:
4222 return "Init rejected";
4223 case ZOOM_ERROR_INTERNAL:
4224 return "Internal failure";
4225 case ZOOM_ERROR_TIMEOUT:
4227 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4228 return "Unsupported protocol";
4229 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4230 return "Unsupported query type";
4231 case ZOOM_ERROR_INVALID_QUERY:
4232 return "Invalid query";
4233 case ZOOM_ERROR_CQL_PARSE:
4234 return "CQL parsing error";
4235 case ZOOM_ERROR_CQL_TRANSFORM:
4236 return "CQL transformation error";
4237 case ZOOM_ERROR_CCL_CONFIG:
4238 return "CCL configuration error";
4239 case ZOOM_ERROR_CCL_PARSE:
4240 return "CCL parsing error";
4242 return diagbib1_str(error);
4247 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4248 const char **addinfo, const char **diagset)
4250 int error = c->error;
4253 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4254 *cp = ZOOM_diag_str(error);
4255 else if (!strcmp(c->diagset, "HTTP"))
4256 *cp = z_HTTP_errmsg(c->error);
4257 else if (!strcmp(c->diagset, "Bib-1"))
4258 *cp = ZOOM_diag_str(error);
4259 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4260 *cp = yaz_diag_srw_str(c->error);
4262 *cp = "Unknown error and diagnostic set";
4265 *addinfo = c->addinfo ? c->addinfo : "";
4267 *diagset = c->diagset ? c->diagset : "";
4272 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4273 const char **addinfo)
4275 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4278 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4280 ZOOM_Event event = 0;
4281 int r = cs_look(c->cs);
4282 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4287 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4288 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4290 ZOOM_connection_put_event(c, event);
4292 else if (r == CS_CONNECT)
4294 int ret = ret = cs_rcvconnect(c->cs);
4295 yaz_log(log_details, "%p ZOOM_connection_do_io "
4296 "cs_rcvconnect returned %d", c, ret);
4299 int mask = ZOOM_SELECT_EXCEPT;
4300 if (c->cs->io_pending & CS_WANT_WRITE)
4301 mask += ZOOM_SELECT_WRITE;
4302 if (c->cs->io_pending & CS_WANT_READ)
4303 mask += ZOOM_SELECT_READ;
4304 ZOOM_connection_set_mask(c, mask);
4305 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4306 ZOOM_connection_put_event(c, event);
4310 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4311 ZOOM_connection_put_event(c, event);
4313 if (c->proto == PROTO_Z3950)
4314 ZOOM_connection_send_init(c);
4317 /* no init request for SRW .. */
4318 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4319 ZOOM_connection_remove_task(c);
4320 ZOOM_connection_set_mask(c, 0);
4321 ZOOM_connection_exec_task(c);
4323 c->state = STATE_ESTABLISHED;
4327 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4333 if (mask & ZOOM_SELECT_EXCEPT)
4335 if (!ZOOM_test_reconnect(c))
4337 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4342 if (mask & ZOOM_SELECT_READ)
4344 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4350 ZOOM_connection_last_event(ZOOM_connection cs)
4353 return ZOOM_EVENT_NONE;
4354 return cs->last_event;
4358 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4360 WRBUF wrbuf = (WRBUF) client_data;
4361 wrbuf_puts(wrbuf, buf);
4365 * Returns an xmalloc()d string containing RPN that corresponds to the
4366 * CQL passed in. On error, sets the Connection object's error state
4367 * and returns a null pointer.
4368 * ### We could cache CQL parser and/or transformer in Connection.
4370 static char *cql2pqf(ZOOM_connection c, const char *cql)
4374 const char *cqlfile;
4375 cql_transform_t trans;
4378 parser = cql_parser_create();
4379 if ((error = cql_parser_string(parser, cql)) != 0) {
4380 cql_parser_destroy(parser);
4381 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4385 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4388 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4390 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4393 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4394 cqlfile, strerror(errno));
4395 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4399 WRBUF wrbuf_result = wrbuf_alloc();
4400 error = cql_transform(trans, cql_parser_result(parser),
4401 cql2pqf_wrbuf_puts, wrbuf_result);
4404 const char *addinfo;
4405 error = cql_transform_error(trans, &addinfo);
4406 sprintf(buf, "%.200s (addinfo=%.200s)",
4407 cql_strerror(error), addinfo);
4408 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4412 result = xstrdup(wrbuf_cstr(wrbuf_result));
4414 cql_transform_close(trans);
4415 wrbuf_destroy(wrbuf_result);
4417 cql_parser_destroy(parser);
4421 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4425 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4426 /* timeout and this connection was waiting */
4427 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4429 ZOOM_connection_put_event(c, event);
4435 ZOOM_connection_process(ZOOM_connection c)
4441 event = ZOOM_connection_get_event(c);
4444 ZOOM_Event_destroy(event);
4447 ZOOM_connection_exec_task(c);
4448 event = ZOOM_connection_get_event(c);
4451 ZOOM_Event_destroy(event);
4458 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4462 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4464 for (i = 0; i<no; i++)
4466 ZOOM_connection c = cs[i];
4468 if (c && ZOOM_connection_process(c))
4474 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4476 if (c->mask && mask)
4477 ZOOM_connection_do_io(c, mask);
4481 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4484 return cs_fileno(c->cs);
4488 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4496 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4503 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4505 return ZOOM_options_get_int(c->options, "timeout", 30);
4511 * indent-tabs-mode: nil
4513 * vim: shiftwidth=4 tabstop=8 expandtab