1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM C interface.
15 #include <yaz/yaz-util.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/otherinfo.h>
19 #include <yaz/pquery.h>
20 #include <yaz/marcdisp.h>
21 #include <yaz/diagbib1.h>
22 #include <yaz/charneg.h>
27 #include <yaz/query-charset.h>
28 #include <yaz/copy_types.h>
29 #include <yaz/snprintf.h>
31 static int log_api = 0;
32 static int log_details = 0;
39 static void resultset_destroy(ZOOM_resultset r);
40 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
41 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
42 static char *cql2pqf(ZOOM_connection c, const char *cql);
44 ZOOM_API(const char *) ZOOM_get_event_str(int event)
46 static const char *ar[] = {
63 * This wrapper is just for logging failed lookups. It would be nicer
64 * if it could cause failure when a lookup fails, but that's hard.
66 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
67 oid_class oid_class, const char *str) {
68 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
71 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
72 c, (int) oid_class, str);
77 static void initlog(void)
79 static int log_level_initialized = 0;
80 if (!log_level_initialized)
82 log_api = yaz_log_module_level("zoom");
83 log_details = yaz_log_module_level("zoomdetails");
84 log_level_initialized = 1;
88 static ZOOM_Event ZOOM_Event_create(int kind)
90 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
94 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
98 static void ZOOM_Event_destroy(ZOOM_Event event)
103 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
107 c->m_queue_back->prev = event;
108 assert(c->m_queue_front);
112 assert(!c->m_queue_front);
113 c->m_queue_front = event;
115 event->next = c->m_queue_back;
117 c->m_queue_back = event;
120 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
122 ZOOM_Event event = c->m_queue_front;
125 c->last_event = ZOOM_EVENT_NONE;
128 assert(c->m_queue_back);
129 c->m_queue_front = event->prev;
130 if (c->m_queue_front)
132 assert(c->m_queue_back);
133 c->m_queue_front->next = 0;
137 c->last_event = event->kind;
141 static void ZOOM_connection_remove_events(ZOOM_connection c)
144 while ((event = ZOOM_connection_get_event(c)))
145 ZOOM_Event_destroy(event);
148 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
150 ZOOM_Event event = c->m_queue_front;
152 return event ? event->kind : ZOOM_EVENT_NONE;
155 void ZOOM_connection_remove_tasks(ZOOM_connection c);
157 static void set_dset_error(ZOOM_connection c, int error,
159 const char *addinfo, const char *addinfo2)
166 if (!c->diagset || strcmp(dset, c->diagset))
169 c->diagset = xstrdup(dset);
170 /* remove integer part from SRW diagset .. */
171 if ((cp = strrchr(c->diagset, '/')))
174 if (addinfo && addinfo2)
176 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
177 strcpy(c->addinfo, addinfo);
178 strcat(c->addinfo, addinfo2);
181 c->addinfo = xstrdup(addinfo);
182 if (error != ZOOM_ERROR_NONE)
184 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
185 c, c->host_port ? c->host_port : "<>", dset, error,
186 addinfo ? addinfo : "",
187 addinfo2 ? addinfo2 : "");
188 ZOOM_connection_remove_tasks(c);
192 static int uri_to_code(const char *uri)
196 if ((cp = strrchr(uri, '/')))
202 static void set_HTTP_error(ZOOM_connection c, int error,
203 const char *addinfo, const char *addinfo2)
205 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
208 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
210 const char *uri = d->uri;
212 set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
218 static void set_ZOOM_error(ZOOM_connection c, int error,
221 set_dset_error(c, error, "ZOOM", addinfo, 0);
224 static void clear_error(ZOOM_connection c)
227 * If an error is tied to an operation then it's ok to clear: for
228 * example, a diagnostic returned from a search is cleared by a
229 * subsequent search. However, problems such as Connection Lost
230 * or Init Refused are not cleared, because they are not
231 * recoverable: doing another search doesn't help.
234 ZOOM_connection_remove_events(c);
237 case ZOOM_ERROR_CONNECT:
238 case ZOOM_ERROR_MEMORY:
239 case ZOOM_ERROR_DECODE:
240 case ZOOM_ERROR_CONNECTION_LOST:
241 case ZOOM_ERROR_INIT:
242 case ZOOM_ERROR_INTERNAL:
243 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
246 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
250 void ZOOM_connection_show_task(ZOOM_task task)
254 case ZOOM_TASK_SEARCH:
255 yaz_log(YLOG_LOG, "search p=%p", task);
257 case ZOOM_TASK_RETRIEVE:
258 yaz_log(YLOG_LOG, "retrieve p=%p", task);
260 case ZOOM_TASK_CONNECT:
261 yaz_log(YLOG_LOG, "connect p=%p", task);
264 yaz_log(YLOG_LOG, "scan p=%p", task);
269 void ZOOM_connection_show_tasks(ZOOM_connection c)
272 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
273 for (task = c->tasks; task; task = task->next)
274 ZOOM_connection_show_task(task);
277 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
279 ZOOM_task *taskp = &c->tasks;
281 taskp = &(*taskp)->next;
282 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
283 (*taskp)->running = 0;
284 (*taskp)->which = which;
290 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
292 return c->tasks ? 0 : 1;
295 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
297 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
299 task->next = c->tasks;
308 void ZOOM_connection_remove_task(ZOOM_connection c)
310 ZOOM_task task = c->tasks;
314 c->tasks = task->next;
317 case ZOOM_TASK_SEARCH:
318 resultset_destroy(task->u.search.resultset);
319 xfree(task->u.search.syntax);
320 xfree(task->u.search.elementSetName);
322 case ZOOM_TASK_RETRIEVE:
323 resultset_destroy(task->u.retrieve.resultset);
324 xfree(task->u.retrieve.syntax);
325 xfree(task->u.retrieve.elementSetName);
327 case ZOOM_TASK_CONNECT:
330 ZOOM_scanset_destroy(task->u.scan.scan);
332 case ZOOM_TASK_PACKAGE:
333 ZOOM_package_destroy(task->u.package);
336 resultset_destroy(task->u.sort.resultset);
337 ZOOM_query_destroy(task->u.sort.q);
346 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
347 ZOOM_connection_put_event(c, event);
352 static int ZOOM_connection_exec_task(ZOOM_connection c);
354 void ZOOM_connection_remove_tasks(ZOOM_connection c)
357 ZOOM_connection_remove_task(c);
360 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
362 const char *elementSetName);
364 ZOOM_API(ZOOM_connection)
365 ZOOM_connection_create(ZOOM_options options)
367 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
371 yaz_log(log_api, "%p ZOOM_connection_create", c);
373 c->proto = PROTO_Z3950;
375 ZOOM_connection_set_mask(c, 0);
377 c->state = STATE_IDLE;
380 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
387 c->options = ZOOM_options_create_with_parent(options);
393 c->charset = c->lang = 0;
404 c->maximum_record_size = 0;
405 c->preferred_message_size = 0;
407 c->odr_in = odr_createmem(ODR_DECODE);
408 c->odr_out = odr_createmem(ODR_ENCODE);
411 c->support_named_resultsets = 0;
412 c->last_event = ZOOM_EVENT_NONE;
414 c->m_queue_front = 0;
422 /* set database names. Take local databases (if set); otherwise
423 take databases given in ZURL (if set); otherwise use Default */
424 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
427 char **databaseNames;
428 const char *cp = ZOOM_options_get(options, "databaseName");
430 if ((!cp || !*cp) && con->host_port)
432 if (strncmp(con->host_port, "unix:", 5) == 0)
433 cp = strchr(con->host_port+5, ':');
435 cp = strchr(con->host_port, '/');
441 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
442 return databaseNames;
445 ZOOM_API(ZOOM_connection)
446 ZOOM_connection_new(const char *host, int portnum)
448 ZOOM_connection c = ZOOM_connection_create(0);
450 ZOOM_connection_connect(c, host, portnum);
454 static zoom_sru_mode get_sru_mode_from_string(const char *s)
457 return zoom_sru_soap;
458 if (!yaz_matchstr(s, "soap"))
459 return zoom_sru_soap;
460 else if (!yaz_matchstr(s, "get"))
462 else if (!yaz_matchstr(s, "post"))
463 return zoom_sru_post;
464 return zoom_sru_error;
468 ZOOM_connection_connect(ZOOM_connection c,
469 const char *host, int portnum)
476 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
477 c, host ? host : "null", portnum);
479 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
480 ZOOM_connection_remove_tasks(c);
482 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
484 c->odr_print = odr_createmem(ODR_PRINT);
485 odr_setprint(c->odr_print, yaz_log_file());
492 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
496 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
499 val = ZOOM_options_get(c->options, "proxy");
502 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
503 c->proxy = xstrdup(val);
508 val = ZOOM_options_get(c->options, "charset");
511 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
512 c->charset = xstrdup(val);
516 val = ZOOM_options_get(c->options, "lang");
519 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
520 c->lang = xstrdup(val);
531 sprintf(hostn, "%.80s:%d", host, portnum);
532 c->host_port = xstrdup(hostn);
535 c->host_port = xstrdup(host);
540 * If the "<scheme>:" part of the host string is preceded by one
541 * or more comma-separated <name>=<value> pairs, these are taken
542 * to be options to be set on the connection object. Among other
543 * applications, this facility can be used to embed authentication
545 * user=admin,password=secret,tcp:localhost:9999
547 char *remainder = c->host_port;
548 char *pcolon = strchr(remainder, ':');
551 while ((pcomma = strchr(remainder, ',')) != 0 &&
552 (pcolon == 0 || pcomma < pcolon)) {
554 if ((pequals = strchr(remainder, '=')) != 0) {
556 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
557 ZOOM_connection_option_set(c, remainder, pequals+1);
559 remainder = pcomma+1;
562 if (remainder != c->host_port) {
564 c->host_port = xstrdup(remainder);
565 /*printf("# reset hp='%s'\n", remainder);*/
569 val = ZOOM_options_get(c->options, "sru");
570 c->sru_mode = get_sru_mode_from_string(val);
572 xfree(c->sru_version);
573 val = ZOOM_options_get(c->options, "sru_version");
574 c->sru_version = xstrdup(val ? val : "1.2");
576 ZOOM_options_set(c->options, "host", c->host_port);
578 xfree(c->cookie_out);
580 val = ZOOM_options_get(c->options, "cookie");
583 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
584 c->cookie_out = xstrdup(val);
589 val = ZOOM_options_get(c->options, "clientIP");
592 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
594 c->client_IP = xstrdup(val);
599 val = ZOOM_options_get(c->options, "group");
601 c->group = xstrdup(val);
605 val = ZOOM_options_get(c->options, "user");
607 c->user = xstrdup(val);
611 val = ZOOM_options_get(c->options, "password");
613 val = ZOOM_options_get(c->options, "pass");
616 c->password = xstrdup(val);
618 c->maximum_record_size =
619 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
620 c->preferred_message_size =
621 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
623 c->async = ZOOM_options_get_bool(c->options, "async", 0);
624 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
626 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
630 while (ZOOM_event(1, &c))
636 ZOOM_query_create(void)
638 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
640 yaz_log(log_details, "%p ZOOM_query_create", s);
644 s->odr = odr_createmem(ODR_ENCODE);
651 ZOOM_query_destroy(ZOOM_query s)
657 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
658 if (s->refcount == 0)
666 ZOOM_query_prefix(ZOOM_query s, const char *str)
668 s->query_string = odr_strdup(s->odr, str);
669 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
670 s->z_query->which = Z_Query_type_1;
671 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
672 if (!s->z_query->u.type_1)
674 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
678 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
683 ZOOM_query_cql(ZOOM_query s, const char *str)
687 s->query_string = odr_strdup(s->odr, str);
689 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
690 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
691 ext->indirect_reference = 0;
693 ext->which = Z_External_CQL;
694 ext->u.cql = s->query_string;
696 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
697 s->z_query->which = Z_Query_type_104;
698 s->z_query->u.type_104 = ext;
700 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
706 * Translate the CQL string client-side into RPN which is passed to
707 * the server. This is useful for server's that don't themselves
708 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
709 * only as a place to stash diagnostics if compilation fails; if this
710 * information is not needed, a null pointer may be used.
713 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
717 ZOOM_connection freeme = 0;
719 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
721 conn = freeme = ZOOM_connection_create(0);
723 rpn = cql2pqf(conn, str);
725 ZOOM_connection_destroy(freeme);
729 ret = ZOOM_query_prefix(s, rpn);
735 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
736 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
737 * to the server, as the YAZ GFS doesn't know how to handle this.
740 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
741 int *ccl_error, const char **error_string,
745 struct ccl_rpn_node *rpn;
746 CCL_bibset bibset = ccl_qual_mk();
749 ccl_qual_buf(bibset, config);
751 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
754 *error_string = ccl_err_msg(*ccl_error);
759 WRBUF wr = wrbuf_alloc();
762 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
765 ccl_qual_rm(&bibset);
770 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
772 s->sort_spec = yaz_sort_spec(s->odr, criteria);
775 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
779 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
783 static zoom_ret do_write(ZOOM_connection c);
786 ZOOM_connection_destroy(ZOOM_connection c)
791 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
794 for (r = c->resultsets; r; r = r->next)
800 odr_destroy(c->odr_in);
801 odr_destroy(c->odr_out);
804 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
805 odr_destroy(c->odr_print);
807 ZOOM_options_destroy(c->options);
808 ZOOM_connection_remove_tasks(c);
809 ZOOM_connection_remove_events(c);
815 xfree(c->cookie_out);
821 xfree(c->sru_version);
825 void ZOOM_resultset_addref(ZOOM_resultset r)
830 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
835 ZOOM_resultset ZOOM_resultset_create(void)
838 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
842 yaz_log(log_details, "%p ZOOM_resultset_create", r);
845 r->odr = odr_createmem(ODR_ENCODE);
850 for (i = 0; i<RECORD_HASH_SIZE; i++)
851 r->record_hash[i] = 0;
856 r->databaseNames = 0;
857 r->num_databaseNames = 0;
861 ZOOM_API(ZOOM_resultset)
862 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
865 ZOOM_query s = ZOOM_query_create();
867 ZOOM_query_prefix(s, q);
869 r = ZOOM_connection_search(c, s);
870 ZOOM_query_destroy(s);
874 ZOOM_API(ZOOM_resultset)
875 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
877 ZOOM_resultset r = ZOOM_resultset_create();
881 const char *syntax, *elementSetName;
883 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
884 r->r_sort_spec = q->sort_spec;
887 r->options = ZOOM_options_create_with_parent(c->options);
889 start = ZOOM_options_get_int(r->options, "start", 0);
890 count = ZOOM_options_get_int(r->options, "count", 0);
892 /* If "presentChunk" is defined use that; otherwise "step" */
893 const char *cp = ZOOM_options_get(r->options, "presentChunk");
894 r->step = ZOOM_options_get_int(r->options,
895 (cp != 0 ? "presentChunk": "step"), 0);
897 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
898 cp = ZOOM_options_get(r->options, "setname");
900 r->setname = xstrdup(cp);
901 cp = ZOOM_options_get(r->options, "schema");
903 r->schema = xstrdup(cp);
905 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
910 r->next = c->resultsets;
915 if (c->host_port && c->proto == PROTO_HTTP)
919 yaz_log(log_details, "ZOOM_connection_search: no comstack");
920 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
924 yaz_log(log_details, "ZOOM_connection_search: reconnect");
929 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
930 task->u.search.resultset = r;
931 task->u.search.start = start;
932 task->u.search.count = count;
933 task->u.search.recv_search_fired = 0;
935 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
936 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
937 elementSetName = ZOOM_options_get(r->options, "elementSetName");
938 task->u.search.elementSetName = elementSetName
939 ? xstrdup(elementSetName) : 0;
941 ZOOM_resultset_addref(r);
947 while (ZOOM_event(1, &c))
954 ZOOM_resultset_sort(ZOOM_resultset r,
955 const char *sort_type, const char *sort_spec)
957 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
961 ZOOM_resultset_sort1(ZOOM_resultset r,
962 const char *sort_type, const char *sort_spec)
964 ZOOM_connection c = r->connection;
968 newq = ZOOM_query_create();
969 if (ZOOM_query_sortby(newq, sort_spec) < 0)
972 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
973 r, r, sort_type, sort_spec);
977 if (c->host_port && c->proto == PROTO_HTTP)
981 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
982 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
986 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
992 ZOOM_resultset_cache_reset(r);
993 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
994 task->u.sort.resultset = r;
995 task->u.sort.q = newq;
997 ZOOM_resultset_addref(r);
1001 while (ZOOM_event(1, &c))
1009 ZOOM_resultset_cache_reset(ZOOM_resultset r)
1012 for (i = 0; i<RECORD_HASH_SIZE; i++)
1014 ZOOM_record_cache rc;
1015 for (rc = r->record_hash[i]; rc; rc = rc->next)
1018 wrbuf_destroy(rc->rec.wrbuf);
1020 r->record_hash[i] = 0;
1025 ZOOM_resultset_destroy(ZOOM_resultset r)
1027 resultset_destroy(r);
1030 static void resultset_destroy(ZOOM_resultset r)
1035 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1037 if (r->refcount == 0)
1039 ZOOM_resultset_cache_reset(r);
1043 /* remove ourselves from the resultsets in connection */
1044 ZOOM_resultset *rp = &r->connection->resultsets;
1047 assert(*rp); /* we must be in this list!! */
1049 { /* OK, we're here - take us out of it */
1056 ZOOM_query_destroy(r->query);
1057 ZOOM_options_destroy(r->options);
1058 odr_destroy(r->odr);
1066 ZOOM_resultset_size(ZOOM_resultset r)
1068 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
1073 static void do_close(ZOOM_connection c)
1078 ZOOM_connection_set_mask(c, 0);
1079 c->state = STATE_IDLE;
1082 static int ZOOM_test_reconnect(ZOOM_connection c)
1086 if (!c->reconnect_ok)
1089 c->reconnect_ok = 0;
1090 c->tasks->running = 0;
1091 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1093 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1094 ZOOM_connection_put_event(c, event);
1099 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1100 int force_sync, int start, int count)
1105 const char *syntax, *elementSetName;
1109 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1110 " count=%d", r, force_sync, start, count);
1115 if (c->host_port && c->proto == PROTO_HTTP)
1119 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1120 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1124 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1126 c->reconnect_ok = 1;
1129 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1130 task->u.retrieve.resultset = r;
1131 task->u.retrieve.start = start;
1132 task->u.retrieve.count = count;
1134 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1135 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1136 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1137 task->u.retrieve.elementSetName = elementSetName
1138 ? xstrdup(elementSetName) : 0;
1140 cp = ZOOM_options_get(r->options, "schema");
1143 if (!r->schema || strcmp(r->schema, cp))
1146 r->schema = xstrdup(cp);
1150 ZOOM_resultset_addref(r);
1152 if (!r->connection->async || force_sync)
1153 while (r->connection && ZOOM_event(1, &r->connection))
1158 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1159 size_t start, size_t count)
1161 int force_present = 0;
1165 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1166 r, r, (long) start, (long) count);
1169 ZOOM_resultset_retrieve(r, force_present, start, count);
1173 for (i = 0; i< count; i++)
1174 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1178 static void get_cert(ZOOM_connection c)
1183 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1185 ZOOM_connection_option_setl(c, "sslPeerCert",
1186 cert_buf, cert_len);
1191 static zoom_ret do_connect(ZOOM_connection c)
1194 const char *effective_host;
1197 effective_host = c->proxy;
1199 effective_host = c->host_port;
1201 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1205 c->cs = cs_create_host(effective_host, 0, &add);
1207 if (c->cs && c->cs->protocol == PROTO_HTTP)
1210 const char *path = 0;
1212 c->proto = PROTO_HTTP;
1213 cs_get_host_args(c->host_port, &path);
1215 c->path = (char*) xmalloc(strlen(path)+2);
1217 strcpy(c->path+1, path);
1219 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1221 return zoom_complete;
1226 int ret = cs_connect(c->cs, add);
1229 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1230 ZOOM_connection_put_event(c, event);
1232 if (c->proto == PROTO_Z3950)
1233 ZOOM_connection_send_init(c);
1236 /* no init request for SRW .. */
1237 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1238 ZOOM_connection_remove_task(c);
1239 ZOOM_connection_set_mask(c, 0);
1240 ZOOM_connection_exec_task(c);
1242 c->state = STATE_ESTABLISHED;
1243 return zoom_pending;
1247 int mask = ZOOM_SELECT_EXCEPT;
1248 if (c->cs->io_pending & CS_WANT_WRITE)
1249 mask += ZOOM_SELECT_WRITE;
1250 if (c->cs->io_pending & CS_WANT_READ)
1251 mask += ZOOM_SELECT_READ;
1252 ZOOM_connection_set_mask(c, mask);
1253 c->state = STATE_CONNECTING;
1254 return zoom_pending;
1257 c->state = STATE_IDLE;
1258 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1259 return zoom_complete;
1262 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1265 for (i = 0; i<200; i++)
1269 Z_OtherInformation **oi;
1274 sprintf(buf, "otherInfo%d", i);
1275 val = ZOOM_options_get(c->options, buf);
1278 cp = strchr(val, ':');
1282 if (len >= sizeof(buf))
1283 len = sizeof(buf)-1;
1284 memcpy(buf, val, len);
1287 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1292 yaz_oi_APDU(a, &oi);
1293 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1297 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1302 Z_OtherInformation **oi;
1303 yaz_oi_APDU(a, &oi);
1304 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1309 Z_OtherInformation **oi;
1310 yaz_oi_APDU(a, &oi);
1311 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1314 otherInfo_attach(c, a, out);
1315 if (!z_APDU(out, &a, 0, 0))
1317 FILE *outf = fopen("/tmp/apdu.txt", "a");
1320 ODR odr_pr = odr_createmem(ODR_PRINT);
1321 fprintf(outf, "a=%p\n", a);
1322 odr_setprint(odr_pr, outf);
1323 z_APDU(odr_pr, &a, 0, 0);
1324 odr_destroy(odr_pr);
1326 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1327 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1332 z_APDU(c->odr_print, &a, 0, 0);
1333 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1337 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1341 if (encode_APDU(c, a, c->odr_out))
1342 return zoom_complete;
1343 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1344 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1345 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1346 ZOOM_connection_put_event(c, event);
1347 odr_reset(c->odr_out);
1351 /* returns 1 if PDU was sent OK (still pending )
1352 0 if PDU was not sent OK (nothing to wait for)
1355 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1357 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1358 Z_InitRequest *ireq = apdu->u.initRequest;
1359 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1360 odr_malloc(c->odr_out, sizeof(*auth));
1362 ODR_MASK_SET(ireq->options, Z_Options_search);
1363 ODR_MASK_SET(ireq->options, Z_Options_present);
1364 ODR_MASK_SET(ireq->options, Z_Options_scan);
1365 ODR_MASK_SET(ireq->options, Z_Options_sort);
1366 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1367 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1369 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1370 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1371 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1373 ireq->implementationId =
1374 odr_prepend(c->odr_out,
1375 ZOOM_options_get(c->options, "implementationId"),
1376 ireq->implementationId);
1378 ireq->implementationName =
1379 odr_prepend(c->odr_out,
1380 ZOOM_options_get(c->options, "implementationName"),
1381 odr_prepend(c->odr_out, "ZOOM-C",
1382 ireq->implementationName));
1384 ireq->implementationVersion =
1385 odr_prepend(c->odr_out,
1386 ZOOM_options_get(c->options, "implementationVersion"),
1387 ireq->implementationVersion);
1389 *ireq->maximumRecordSize = c->maximum_record_size;
1390 *ireq->preferredMessageSize = c->preferred_message_size;
1392 if (c->group || c->password)
1394 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1395 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1396 pass->userId = odr_strdup_null(c->odr_out, c->user);
1397 pass->password = odr_strdup_null(c->odr_out, c->password);
1398 auth->which = Z_IdAuthentication_idPass;
1399 auth->u.idPass = pass;
1400 ireq->idAuthentication = auth;
1404 auth->which = Z_IdAuthentication_open;
1405 auth->u.open = odr_strdup(c->odr_out, c->user);
1406 ireq->idAuthentication = auth;
1410 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1411 yaz_oid_userinfo_proxy, 1, c->host_port);
1413 if (c->charset || c->lang)
1415 Z_OtherInformation **oi;
1416 Z_OtherInformationUnit *oi_unit;
1418 yaz_oi_APDU(apdu, &oi);
1420 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1422 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1423 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1424 oi_unit->information.externallyDefinedInfo =
1425 yaz_set_proposal_charneg_list(c->odr_out, " ",
1426 c->charset, c->lang, 1);
1430 return send_APDU(c, apdu);
1434 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1438 const char *database = ZOOM_options_get(c->options, "databaseName");
1439 char *fdatabase = 0;
1443 fdatabase = (char *) odr_malloc(c->odr_out, strlen(database)+2);
1444 strcpy(fdatabase, "/");
1445 strcat(fdatabase, database);
1447 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
1448 fdatabase ? fdatabase : c->path);
1450 if (c->sru_mode == zoom_sru_get)
1452 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1454 else if (c->sru_mode == zoom_sru_post)
1456 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1458 else if (c->sru_mode == zoom_sru_soap)
1460 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1462 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1463 return zoom_complete;
1465 z_GDU(c->odr_print, &gdu, 0, 0);
1466 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1468 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1469 ZOOM_connection_put_event(c, event);
1470 odr_reset(c->odr_out);
1476 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1478 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1479 sr->username = c->user;
1480 sr->password = c->password;
1486 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1490 ZOOM_resultset resultset = 0;
1492 const char *option_val = 0;
1494 if (c->error) /* don't continue on error */
1495 return zoom_complete;
1497 switch(c->tasks->which)
1499 case ZOOM_TASK_SEARCH:
1500 resultset = c->tasks->u.search.resultset;
1501 if (!resultset->setname)
1502 resultset->setname = xstrdup("default");
1503 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1504 start = &c->tasks->u.search.start;
1505 count = &c->tasks->u.search.count;
1507 case ZOOM_TASK_RETRIEVE:
1508 resultset = c->tasks->u.retrieve.resultset;
1510 start = &c->tasks->u.retrieve.start;
1511 count = &c->tasks->u.retrieve.count;
1513 if (*start >= resultset->size)
1514 return zoom_complete;
1515 if (*start + *count > resultset->size)
1516 *count = resultset->size - *start;
1518 for (i = 0; i < *count; i++)
1521 record_cache_lookup(resultset, i + *start,
1522 c->tasks->u.retrieve.syntax,
1523 c->tasks->u.retrieve.elementSetName);
1528 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1529 ZOOM_connection_put_event(c, event);
1536 return zoom_complete;
1539 return zoom_complete;
1541 assert(resultset->query);
1543 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1544 if (resultset->query->z_query->which == Z_Query_type_104
1545 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1547 sr->u.request->query_type = Z_SRW_query_type_cql;
1548 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1550 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1551 resultset->query->z_query->u.type_1)
1553 sr->u.request->query_type = Z_SRW_query_type_pqf;
1554 sr->u.request->query.pqf = resultset->query->query_string;
1558 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1559 return zoom_complete;
1561 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1562 sr->u.request->maximumRecords = odr_intdup(
1563 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
1564 resultset->step : *count);
1565 sr->u.request->recordSchema = resultset->schema;
1567 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1569 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1571 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1572 yaz_encode_sru_extra(sr, c->odr_out, option_val);
1573 return send_srw(c, sr);
1576 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1578 return zoom_complete;
1582 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1585 int lslb, ssub, mspn;
1587 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1588 Z_SearchRequest *search_req = apdu->u.searchRequest;
1589 const char *elementSetName;
1590 const char *smallSetElementSetName;
1591 const char *mediumSetElementSetName;
1594 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1596 r = c->tasks->u.search.resultset;
1598 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1601 ZOOM_options_get(r->options, "elementSetName");
1602 smallSetElementSetName =
1603 ZOOM_options_get(r->options, "smallSetElementSetName");
1604 mediumSetElementSetName =
1605 ZOOM_options_get(r->options, "mediumSetElementSetName");
1607 if (!smallSetElementSetName)
1608 smallSetElementSetName = elementSetName;
1610 if (!mediumSetElementSetName)
1611 mediumSetElementSetName = elementSetName;
1616 /* prepare query for the search request */
1617 search_req->query = r->query->z_query;
1618 if (!search_req->query)
1620 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1621 return zoom_complete;
1623 if (r->query->z_query->which == Z_Query_type_1 ||
1624 r->query->z_query->which == Z_Query_type_101)
1626 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1629 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1632 search_req->query = yaz_copy_Z_Query(search_req->query,
1635 yaz_query_charset_convert_rpnquery(search_req->query->u.type_1,
1637 yaz_iconv_close(cd);
1641 search_req->databaseNames = r->databaseNames;
1642 search_req->num_databaseNames = r->num_databaseNames;
1644 /* get syntax (no need to provide unless piggyback is in effect) */
1645 syntax = c->tasks->u.search.syntax;
1647 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1648 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1649 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1650 if (lslb != -1 && ssub != -1 && mspn != -1)
1652 /* So're a Z39.50 expert? Let's hope you don't do sort */
1653 *search_req->largeSetLowerBound = lslb;
1654 *search_req->smallSetUpperBound = ssub;
1655 *search_req->mediumSetPresentNumber = mspn;
1657 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1658 && r->piggyback && !r->r_sort_spec && !r->schema)
1660 /* Regular piggyback - do it unless we're going to do sort */
1661 *search_req->largeSetLowerBound = 2000000000;
1662 *search_req->smallSetUpperBound = 1;
1663 *search_req->mediumSetPresentNumber =
1664 r->step>0 ? r->step : c->tasks->u.search.count;
1668 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1669 smallSetElementSetName = 0;
1670 mediumSetElementSetName = 0;
1673 if (smallSetElementSetName && *smallSetElementSetName)
1675 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1676 odr_malloc(c->odr_out, sizeof(*esn));
1678 esn->which = Z_ElementSetNames_generic;
1679 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1680 search_req->smallSetElementSetNames = esn;
1682 if (mediumSetElementSetName && *mediumSetElementSetName)
1684 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1685 odr_malloc(c->odr_out, sizeof(*esn));
1687 esn->which = Z_ElementSetNames_generic;
1688 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1689 search_req->mediumSetElementSetNames = esn;
1692 search_req->preferredRecordSyntax =
1693 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1697 if (c->support_named_resultsets)
1701 /* find the lowest unused ordinal so that we re-use
1702 result sets on the server. */
1703 for (ord = 1; ; ord++)
1706 sprintf(setname, "%d", ord);
1707 for (rp = c->resultsets; rp; rp = rp->next)
1708 if (rp->setname && !strcmp(rp->setname, setname))
1713 r->setname = xstrdup(setname);
1714 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1715 "set %s", c, r->setname);
1719 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1721 r->setname = xstrdup("default");
1723 ZOOM_options_set(r->options, "setname", r->setname);
1725 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1726 return send_APDU(c, apdu);
1729 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1731 char oid_name_buf[OID_STR_MAX];
1732 const char *oid_name;
1735 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1738 case Z_DefaultDiagFormat_v2Addinfo:
1739 addinfo = r->u.v2Addinfo;
1741 case Z_DefaultDiagFormat_v3Addinfo:
1742 addinfo = r->u.v3Addinfo;
1747 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1750 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1752 if (p->which != Z_DiagRec_defaultFormat)
1753 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1755 response_default_diag(c, p->u.defaultFormat);
1758 ZOOM_API(ZOOM_record)
1759 ZOOM_record_clone(ZOOM_record srec)
1766 odr_enc = odr_createmem(ODR_ENCODE);
1767 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1769 buf = odr_getbuf(odr_enc, &size, 0);
1771 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1772 nrec->odr = odr_createmem(ODR_DECODE);
1774 odr_setbuf(nrec->odr, buf, size, 0);
1775 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1777 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1778 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1779 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1780 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1781 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1782 odr_destroy(odr_enc);
1786 ZOOM_API(ZOOM_record)
1787 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1789 const char *syntax =
1790 ZOOM_options_get(s->options, "preferredRecordSyntax");
1791 const char *elementSetName =
1792 ZOOM_options_get(s->options, "elementSetName");
1794 return record_cache_lookup(s, pos, syntax, elementSetName);
1797 ZOOM_API(ZOOM_record)
1798 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1800 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1805 * MIKE: I think force_sync should always be zero, but I don't
1806 * want to make this change until I get the go-ahead from
1807 * Adam, in case something depends on the old synchronous
1811 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1812 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1813 rec = ZOOM_resultset_record_immediate(r, pos);
1819 ZOOM_record_destroy(ZOOM_record rec)
1824 wrbuf_destroy(rec->wrbuf);
1825 odr_destroy(rec->odr);
1830 static yaz_iconv_t iconv_create_charset(const char *record_charset)
1837 strcpy(to, "UTF-8");
1838 if (record_charset && *record_charset)
1840 /* Use "from,to" or just "from" */
1841 const char *cp = strchr(record_charset, ',');
1842 int clen = strlen(record_charset);
1845 strncpy( to, cp+1, sizeof(to)-1);
1846 to[sizeof(to)-1] = '\0';
1847 clen = cp - record_charset;
1849 if (clen > sizeof(from)-1)
1850 clen = sizeof(from)-1;
1853 strncpy(from, record_charset, clen);
1857 cd = yaz_iconv_open(to, from);
1861 static const char *return_marc_record(ZOOM_record rec, int marc_type,
1863 const char *buf, int sz,
1864 const char *record_charset)
1866 yaz_iconv_t cd = iconv_create_charset(record_charset);
1867 yaz_marc_t mt = yaz_marc_create();
1868 const char *ret_string = 0;
1871 yaz_marc_iconv(mt, cd);
1872 yaz_marc_xml(mt, marc_type);
1874 rec->wrbuf = wrbuf_alloc();
1875 wrbuf_rewind(rec->wrbuf);
1876 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf) > 0)
1879 *len = wrbuf_len(rec->wrbuf);
1880 ret_string = wrbuf_cstr(rec->wrbuf);
1882 yaz_marc_destroy(mt);
1884 yaz_iconv_close(cd);
1888 static const char *return_opac_record(ZOOM_record rec, int marc_type,
1890 Z_OPACRecord *opac_rec,
1891 const char *record_charset)
1893 yaz_iconv_t cd = iconv_create_charset(record_charset);
1894 yaz_marc_t mt = yaz_marc_create();
1897 yaz_marc_iconv(mt, cd);
1898 yaz_marc_xml(mt, marc_type);
1901 rec->wrbuf = wrbuf_alloc();
1902 wrbuf_rewind(rec->wrbuf);
1904 yaz_opac_decode_wrbuf(mt, opac_rec, rec->wrbuf);
1905 yaz_marc_destroy(mt);
1908 yaz_iconv_close(cd);
1910 *len = wrbuf_len(rec->wrbuf);
1911 return wrbuf_cstr(rec->wrbuf);
1914 static const char *return_string_record(ZOOM_record rec, int *len,
1915 const char *buf, int sz,
1916 const char *record_charset)
1918 yaz_iconv_t cd = iconv_create_charset(record_charset);
1923 rec->wrbuf = wrbuf_alloc();
1925 wrbuf_rewind(rec->wrbuf);
1927 wrbuf_iconv_write(rec->wrbuf, cd, buf, sz);
1928 wrbuf_iconv_reset(rec->wrbuf, cd);
1930 buf = wrbuf_cstr(rec->wrbuf);
1931 sz = wrbuf_len(rec->wrbuf);
1932 yaz_iconv_close(cd);
1939 static const char *return_record(ZOOM_record rec, int *len,
1940 Z_NamePlusRecord *npr,
1941 int marctype, const char *charset)
1943 Z_External *r = (Z_External *) npr->u.databaseRecord;
1944 const Odr_oid *oid = r->direct_reference;
1946 /* render bibliographic record .. */
1947 if (r->which == Z_External_OPAC)
1949 return return_opac_record(rec, marctype, len,
1950 r->u.opac, charset);
1952 if (r->which == Z_External_sutrs)
1953 return return_string_record(rec, len,
1954 (char*) r->u.sutrs->buf,
1957 else if (r->which == Z_External_octet)
1959 if (yaz_oid_is_iso2709(oid))
1961 const char *ret_buf = return_marc_record(
1963 (const char *) r->u.octet_aligned->buf,
1964 r->u.octet_aligned->len,
1969 return return_string_record(rec, len,
1970 (const char *) r->u.octet_aligned->buf,
1971 r->u.octet_aligned->len,
1974 else if (r->which == Z_External_grs1)
1977 rec->wrbuf = wrbuf_alloc();
1978 wrbuf_rewind(rec->wrbuf);
1979 yaz_display_grs1(rec->wrbuf, r->u.grs1, 0);
1980 return return_string_record(rec, len,
1981 wrbuf_buf(rec->wrbuf),
1982 wrbuf_len(rec->wrbuf),
1990 ZOOM_record_error(ZOOM_record rec, const char **cp,
1991 const char **addinfo, const char **diagset)
1993 Z_NamePlusRecord *npr;
2002 *cp = rec->diag_message;
2004 *addinfo = rec->diag_details;
2006 *diagset = rec->diag_set;
2007 return uri_to_code(rec->diag_uri);
2009 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
2011 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
2012 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
2013 const char *add = 0;
2015 if (diag_rec->which == Z_DiagRec_defaultFormat)
2017 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
2020 error = *ddf->condition;
2023 case Z_DefaultDiagFormat_v2Addinfo:
2024 add = ddf->u.v2Addinfo;
2026 case Z_DefaultDiagFormat_v3Addinfo:
2027 add = ddf->u.v3Addinfo;
2032 yaz_oid_to_string(yaz_oid_std(),
2033 ddf->diagnosticSetId, &oclass);
2041 *addinfo = add ? add : "";
2043 *cp = diagbib1_str(error);
2049 ZOOM_API(const char *)
2050 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
2057 Z_NamePlusRecord *npr;
2060 *len = 0; /* default return */
2069 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
2071 if (cp[i] == ';' || cp[i] == ' ')
2077 while (type_spec[i] == ';')
2080 while (type_spec[i] == ' ')
2082 if (!strncmp(type_spec+i, "charset=", 8))
2085 i = i + 8; /* skip charset= */
2086 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
2088 if (type_spec[i] == ';' || type_spec[i] == ' ')
2094 else if (!strncmp(type_spec+i, "xpath=", 6))
2098 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
2102 while (type_spec[i] == ' ')
2105 if (!strcmp(type, "database"))
2108 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2109 return npr->databaseName;
2111 else if (!strcmp(type, "schema"))
2114 *len = rec->schema ? strlen(rec->schema) : 0;
2117 else if (!strcmp(type, "syntax"))
2119 const char *desc = 0;
2120 if (npr->which == Z_NamePlusRecord_databaseRecord)
2122 Z_External *r = (Z_External *) npr->u.databaseRecord;
2123 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2128 *len = strlen(desc);
2131 if (npr->which != Z_NamePlusRecord_databaseRecord)
2134 /* from now on - we have a database record .. */
2135 if (!strcmp(type, "render"))
2137 return return_record(rec, len, npr, YAZ_MARC_LINE, charset);
2139 else if (!strcmp(type, "xml"))
2141 return return_record(rec, len, npr, YAZ_MARC_MARCXML, charset);
2143 else if (!strcmp(type, "raw"))
2145 return return_record(rec, len, npr, YAZ_MARC_ISO2709, charset);
2147 else if (!strcmp(type, "ext"))
2150 return (const char *) npr->u.databaseRecord;
2152 else if (!strcmp(type, "opac"))
2154 if (npr->u.databaseRecord->which == Z_External_OPAC)
2155 return return_record(rec, len, npr, YAZ_MARC_MARCXML, charset);
2160 static int strcmp_null(const char *v1, const char *v2)
2166 return strcmp(v1, v2);
2169 static size_t record_hash(int pos)
2173 return pos % RECORD_HASH_SIZE;
2176 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2178 const char *syntax, const char *elementSetName,
2180 Z_SRW_diagnostic *diag)
2182 ZOOM_record_cache rc = 0;
2184 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2185 ZOOM_connection_put_event(r->connection, event);
2187 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2190 && strcmp_null(r->schema, rc->schema) == 0
2191 && strcmp_null(elementSetName,rc->elementSetName) == 0
2192 && strcmp_null(syntax, rc->syntax) == 0)
2197 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2200 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2202 rc->syntax = odr_strdup_null(r->odr, syntax);
2204 rc->schema = odr_strdup_null(r->odr, r->schema);
2207 rc->next = r->record_hash[record_hash(pos)];
2208 r->record_hash[record_hash(pos)] = rc;
2211 rc->rec.schema = odr_strdup_null(r->odr, schema);
2212 rc->rec.diag_set = 0;
2213 rc->rec.diag_uri = 0;
2214 rc->rec.diag_message = 0;
2215 rc->rec.diag_details = 0;
2221 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2222 if ((cp = strrchr(rc->rec.diag_set, '/')))
2224 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2226 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2227 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2231 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2233 const char *elementSetName)
2235 ZOOM_record_cache rc;
2237 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2241 if (strcmp_null(r->schema, rc->schema))
2243 if (strcmp_null(elementSetName,rc->elementSetName))
2245 if (strcmp_null(syntax, rc->syntax))
2253 static void handle_records(ZOOM_connection c, Z_Records *sr,
2256 ZOOM_resultset resultset;
2258 const char *syntax = 0, *elementSetName = 0;
2262 switch (c->tasks->which)
2264 case ZOOM_TASK_SEARCH:
2265 resultset = c->tasks->u.search.resultset;
2266 start = &c->tasks->u.search.start;
2267 count = &c->tasks->u.search.count;
2268 syntax = c->tasks->u.search.syntax;
2269 elementSetName = c->tasks->u.search.elementSetName;
2271 case ZOOM_TASK_RETRIEVE:
2272 resultset = c->tasks->u.retrieve.resultset;
2273 start = &c->tasks->u.retrieve.start;
2274 count = &c->tasks->u.retrieve.count;
2275 syntax = c->tasks->u.retrieve.syntax;
2276 elementSetName = c->tasks->u.retrieve.elementSetName;
2281 if (sr && sr->which == Z_Records_NSD)
2282 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2283 else if (sr && sr->which == Z_Records_multipleNSD)
2285 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2286 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2288 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2292 if (*count + *start > resultset->size)
2293 *count = resultset->size - *start;
2296 if (sr && sr->which == Z_Records_DBOSD)
2299 NMEM nmem = odr_extract_mem(c->odr_in);
2300 Z_NamePlusRecordList *p =
2301 sr->u.databaseOrSurDiagnostics;
2302 for (i = 0; i<p->num_records; i++)
2304 record_cache_add(resultset, p->records[i], i + *start,
2305 syntax, elementSetName,
2312 yaz_log(log_details,
2313 "handle_records resultset=%p start=%d count=%d",
2314 resultset, *start, *count);
2316 /* transfer our response to search_nmem .. we need it later */
2317 nmem_transfer(odr_getmem(resultset->odr), nmem);
2319 if (present_phase && p->num_records == 0)
2321 /* present response and we didn't get any records! */
2322 Z_NamePlusRecord *myrec =
2323 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2324 record_cache_add(resultset, myrec, *start,
2325 syntax, elementSetName, 0, 0);
2328 else if (present_phase)
2330 /* present response and we didn't get any records! */
2331 Z_NamePlusRecord *myrec =
2332 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2333 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2339 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2341 handle_records(c, pr->records, 1);
2344 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2347 switch (term->which)
2349 case Z_Term_general:
2350 ZOOM_options_setl(opt, name,
2351 (const char *)(term->u.general->buf),
2352 term->u.general->len);
2354 case Z_Term_characterString:
2355 ZOOM_options_set(opt, name, term->u.characterString);
2357 case Z_Term_numeric:
2358 ZOOM_options_set_int(opt, name, *term->u.numeric);
2363 static void handle_queryExpression(ZOOM_options opt, const char *name,
2364 Z_QueryExpression *exp)
2370 case Z_QueryExpression_term:
2371 if (exp->u.term && exp->u.term->queryTerm)
2373 sprintf(opt_name, "%s.term", name);
2374 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2377 case Z_QueryExpression_query:
2382 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2383 Z_OtherInformation *o)
2386 for (i = 0; o && i < o->num_elements; i++)
2388 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2390 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2392 if (ext->which == Z_External_searchResult1)
2395 Z_SearchInfoReport *sr = ext->u.searchResult1;
2398 ZOOM_options_set_int(
2399 resultset->options, "searchresult.size", sr->num);
2401 for (j = 0; j < sr->num; j++)
2403 Z_SearchInfoReport_s *ent =
2404 ext->u.searchResult1->elements[j];
2407 sprintf(pref, "searchresult.%d", j);
2409 if (ent->subqueryId)
2412 sprintf(opt_name, "%s.id", pref);
2413 ZOOM_options_set(resultset->options, opt_name,
2416 if (ent->subqueryExpression)
2419 sprintf(opt_name, "%s.subquery", pref);
2420 handle_queryExpression(resultset->options, opt_name,
2421 ent->subqueryExpression);
2423 if (ent->subqueryInterpretation)
2426 sprintf(opt_name, "%s.interpretation", pref);
2427 handle_queryExpression(resultset->options, opt_name,
2428 ent->subqueryInterpretation);
2430 if (ent->subqueryRecommendation)
2433 sprintf(opt_name, "%s.recommendation", pref);
2434 handle_queryExpression(resultset->options, opt_name,
2435 ent->subqueryRecommendation);
2437 if (ent->subqueryCount)
2440 sprintf(opt_name, "%s.count", pref);
2441 ZOOM_options_set_int(resultset->options, opt_name,
2442 *ent->subqueryCount);
2450 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2452 ZOOM_resultset resultset;
2455 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2458 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2459 ZOOM_connection_put_event(c, event);
2461 resultset = c->tasks->u.search.resultset;
2463 if (sr->resultSetStatus)
2465 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2466 *sr->resultSetStatus);
2468 if (sr->presentStatus)
2470 ZOOM_options_set_int(resultset->options, "presentStatus",
2471 *sr->presentStatus);
2473 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2475 resultset->size = *sr->resultCount;
2476 handle_records(c, sr->records, 0);
2479 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2481 if (res->diagnostics && res->num_diagnostics > 0)
2482 response_diag(c, res->diagnostics[0]);
2485 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2487 NMEM nmem = odr_extract_mem(c->odr_in);
2490 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2492 scan = c->tasks->u.scan.scan;
2494 if (res->entries && res->entries->nonsurrogateDiagnostics)
2495 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2496 scan->scan_response = res;
2497 scan->srw_scan_response = 0;
2498 nmem_transfer(odr_getmem(scan->odr), nmem);
2500 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2501 if (res->positionOfTerm)
2502 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2503 if (res->scanStatus)
2504 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2505 if (res->numberOfEntriesReturned)
2506 ZOOM_options_set_int(scan->options, "number",
2507 *res->numberOfEntriesReturned);
2512 static zoom_ret send_sort(ZOOM_connection c,
2513 ZOOM_resultset resultset)
2516 resultset->r_sort_spec = 0;
2517 if (resultset->r_sort_spec)
2519 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2520 Z_SortRequest *req = apdu->u.sortRequest;
2522 req->num_inputResultSetNames = 1;
2523 req->inputResultSetNames = (Z_InternationalString **)
2524 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2525 req->inputResultSetNames[0] =
2526 odr_strdup(c->odr_out, resultset->setname);
2527 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2528 req->sortSequence = resultset->r_sort_spec;
2529 resultset->r_sort_spec = 0;
2530 return send_APDU(c, apdu);
2532 return zoom_complete;
2535 static zoom_ret send_present(ZOOM_connection c)
2538 Z_PresentRequest *req = 0;
2540 const char *syntax = 0;
2541 const char *elementSetName = 0;
2542 ZOOM_resultset resultset;
2547 yaz_log(log_details, "%p send_present no tasks", c);
2548 return zoom_complete;
2551 switch (c->tasks->which)
2553 case ZOOM_TASK_SEARCH:
2554 resultset = c->tasks->u.search.resultset;
2555 start = &c->tasks->u.search.start;
2556 count = &c->tasks->u.search.count;
2557 syntax = c->tasks->u.search.syntax;
2558 elementSetName = c->tasks->u.search.elementSetName;
2560 case ZOOM_TASK_RETRIEVE:
2561 resultset = c->tasks->u.retrieve.resultset;
2562 start = &c->tasks->u.retrieve.start;
2563 count = &c->tasks->u.retrieve.count;
2564 syntax = c->tasks->u.retrieve.syntax;
2565 elementSetName = c->tasks->u.retrieve.elementSetName;
2568 return zoom_complete;
2570 yaz_log(log_details, "%p send_present start=%d count=%d",
2573 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2575 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2578 if (c->error) /* don't continue on error */
2579 return zoom_complete;
2580 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2581 resultset, *start, *count);
2583 for (i = 0; i < *count; i++)
2586 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2591 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2592 ZOOM_connection_put_event(c, event);
2600 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2601 return zoom_complete;
2604 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2605 req = apdu->u.presentRequest;
2608 yaz_log(log_details, "%p send_present skip=%d", c, i);
2610 *req->resultSetStartPoint = *start + 1;
2612 if (resultset->step > 0 && resultset->step < *count)
2613 *req->numberOfRecordsRequested = resultset->step;
2615 *req->numberOfRecordsRequested = *count;
2617 if (*req->numberOfRecordsRequested + *start > resultset->size)
2618 *req->numberOfRecordsRequested = resultset->size - *start;
2619 assert(*req->numberOfRecordsRequested > 0);
2621 if (syntax && *syntax)
2622 req->preferredRecordSyntax =
2623 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2625 if (resultset->schema && *resultset->schema)
2627 Z_RecordComposition *compo = (Z_RecordComposition *)
2628 odr_malloc(c->odr_out, sizeof(*compo));
2630 req->recordComposition = compo;
2631 compo->which = Z_RecordComp_complex;
2632 compo->u.complex = (Z_CompSpec *)
2633 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2634 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2635 odr_malloc(c->odr_out, sizeof(bool_t));
2636 *compo->u.complex->selectAlternativeSyntax = 0;
2638 compo->u.complex->generic = (Z_Specification *)
2639 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2641 compo->u.complex->generic->which = Z_Schema_oid;
2642 compo->u.complex->generic->schema.oid = (Odr_oid *)
2643 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2645 if (!compo->u.complex->generic->schema.oid)
2647 /* OID wasn't a schema! Try record syntax instead. */
2649 compo->u.complex->generic->schema.oid = (Odr_oid *)
2650 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2652 if (elementSetName && *elementSetName)
2654 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2655 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2656 compo->u.complex->generic->elementSpec->which =
2657 Z_ElementSpec_elementSetName;
2658 compo->u.complex->generic->elementSpec->u.elementSetName =
2659 odr_strdup(c->odr_out, elementSetName);
2662 compo->u.complex->generic->elementSpec = 0;
2663 compo->u.complex->num_dbSpecific = 0;
2664 compo->u.complex->dbSpecific = 0;
2665 compo->u.complex->num_recordSyntax = 0;
2666 compo->u.complex->recordSyntax = 0;
2668 else if (elementSetName && *elementSetName)
2670 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2671 odr_malloc(c->odr_out, sizeof(*esn));
2672 Z_RecordComposition *compo = (Z_RecordComposition *)
2673 odr_malloc(c->odr_out, sizeof(*compo));
2675 esn->which = Z_ElementSetNames_generic;
2676 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2677 compo->which = Z_RecordComp_simple;
2678 compo->u.simple = esn;
2679 req->recordComposition = compo;
2681 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2682 return send_APDU(c, apdu);
2685 ZOOM_API(ZOOM_scanset)
2686 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2689 ZOOM_query q = ZOOM_query_create();
2691 ZOOM_query_prefix(q, start);
2693 s = ZOOM_connection_scan1(c, q);
2694 ZOOM_query_destroy(q);
2699 ZOOM_API(ZOOM_scanset)
2700 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2702 ZOOM_scanset scan = 0;
2706 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2707 scan->connection = c;
2708 scan->odr = odr_createmem(ODR_DECODE);
2709 scan->options = ZOOM_options_create_with_parent(c->options);
2711 scan->scan_response = 0;
2712 scan->srw_scan_response = 0;
2716 scan->databaseNames = set_DatabaseNames(c, c->options,
2717 &scan->num_databaseNames,
2722 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2723 task->u.scan.scan = scan;
2728 while (ZOOM_event(1, &c))
2736 ZOOM_scanset_destroy(ZOOM_scanset scan)
2741 if (scan->refcount == 0)
2743 ZOOM_query_destroy(scan->query);
2745 odr_destroy(scan->odr);
2747 ZOOM_options_destroy(scan->options);
2752 static zoom_ret send_package(ZOOM_connection c)
2756 yaz_log(log_details, "%p send_package", c);
2758 return zoom_complete;
2759 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2761 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2762 ZOOM_connection_put_event(c, event);
2764 c->buf_out = c->tasks->u.package->buf_out;
2765 c->len_out = c->tasks->u.package->len_out;
2770 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2773 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2774 Z_ScanRequest *req = apdu->u.scanRequest;
2776 yaz_log(log_details, "%p send_scan", c);
2778 return zoom_complete;
2779 assert (c->tasks->which == ZOOM_TASK_SCAN);
2780 scan = c->tasks->u.scan.scan;
2782 /* Z39.50 scan can only carry RPN */
2783 if (scan->query->z_query->which == Z_Query_type_1 ||
2784 scan->query->z_query->which == Z_Query_type_101)
2786 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2787 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2790 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2793 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2795 yaz_query_charset_convert_rpnquery(
2796 rpn, c->odr_out, cd);
2797 yaz_iconv_close(cd);
2800 req->attributeSet = rpn->attributeSetId;
2801 if (!req->attributeSet)
2802 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2803 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2804 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2806 req->termListAndStartPoint =
2807 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2811 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2812 return zoom_complete;
2817 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2818 return zoom_complete;
2821 *req->numberOfTermsRequested =
2822 ZOOM_options_get_int(scan->options, "number", 10);
2824 req->preferredPositionInResponse =
2825 odr_intdup(c->odr_out,
2826 ZOOM_options_get_int(scan->options, "position", 1));
2829 odr_intdup(c->odr_out,
2830 ZOOM_options_get_int(scan->options, "stepSize", 0));
2832 req->databaseNames = scan->databaseNames;
2833 req->num_databaseNames = scan->num_databaseNames;
2835 return send_APDU(c, apdu);
2839 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2843 const char *option_val = 0;
2846 return zoom_complete;
2847 assert (c->tasks->which == ZOOM_TASK_SCAN);
2848 scan = c->tasks->u.scan.scan;
2850 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2852 /* SRU scan can only carry CQL and PQF */
2853 if (scan->query->z_query->which == Z_Query_type_104)
2855 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2856 sr->u.scan_request->scanClause.cql = scan->query->query_string;
2858 else if (scan->query->z_query->which == Z_Query_type_1
2859 || scan->query->z_query->which == Z_Query_type_101)
2861 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2862 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2866 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2867 return zoom_complete;
2870 sr->u.scan_request->maximumTerms = odr_intdup(
2871 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2873 sr->u.scan_request->responsePosition = odr_intdup(
2874 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2876 option_val = ZOOM_options_get(scan->options, "extraArgs");
2877 yaz_encode_sru_extra(sr, c->odr_out, option_val);
2878 return send_srw(c, sr);
2881 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2883 return zoom_complete;
2889 ZOOM_scanset_size(ZOOM_scanset scan)
2894 if (scan->scan_response && scan->scan_response->entries)
2895 return scan->scan_response->entries->num_entries;
2896 else if (scan->srw_scan_response)
2897 return scan->srw_scan_response->num_terms;
2901 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2903 const char **value_term, size_t *value_len,
2904 const char **disp_term, size_t *disp_len)
2906 size_t noent = ZOOM_scanset_size(scan);
2915 if (pos >= noent || pos < 0)
2917 if (scan->scan_response)
2919 Z_ScanResponse *res = scan->scan_response;
2920 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2922 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2924 *value_term = (const char *) t->term->u.general->buf;
2925 *value_len = t->term->u.general->len;
2928 *disp_term = t->displayTerm;
2929 *disp_len = strlen(*disp_term);
2931 else if (t->term->which == Z_Term_general)
2933 *disp_term = (const char *) t->term->u.general->buf;
2934 *disp_len = t->term->u.general->len;
2936 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2939 if (scan->srw_scan_response)
2941 Z_SRW_scanResponse *res = scan->srw_scan_response;
2942 Z_SRW_scanTerm *t = res->terms + pos;
2945 *value_term = t->value;
2946 *value_len = strlen(*value_term);
2949 *disp_term = t->displayTerm;
2951 *disp_term = t->value;
2952 *disp_len = strlen(*disp_term);
2953 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
2958 ZOOM_API(const char *)
2959 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
2962 const char *value_term = 0;
2963 size_t value_len = 0;
2964 const char *disp_term = 0;
2965 size_t disp_len = 0;
2967 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
2968 &disp_term, &disp_len);
2974 ZOOM_API(const char *)
2975 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
2978 const char *value_term = 0;
2979 size_t value_len = 0;
2980 const char *disp_term = 0;
2981 size_t disp_len = 0;
2983 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
2984 &disp_term, &disp_len);
2990 ZOOM_API(const char *)
2991 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
2993 return ZOOM_options_get(scan->options, key);
2997 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3000 ZOOM_options_set(scan->options, key, val);
3003 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3006 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3007 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3009 str = ZOOM_options_get(p->options, "package-name");
3011 req->packageName = odr_strdup(p->odr_out, str);
3013 str = ZOOM_options_get(p->options, "user-id");
3015 req->userId = odr_strdup_null(p->odr_out, str);
3017 req->packageType = odr_oiddup(p->odr_out, oid);
3019 str = ZOOM_options_get(p->options, "function");
3022 if (!strcmp (str, "create"))
3023 *req->function = Z_ExtendedServicesRequest_create;
3024 if (!strcmp (str, "delete"))
3025 *req->function = Z_ExtendedServicesRequest_delete;
3026 if (!strcmp (str, "modify"))
3027 *req->function = Z_ExtendedServicesRequest_modify;
3030 str = ZOOM_options_get(p->options, "waitAction");
3033 if (!strcmp (str, "wait"))
3034 *req->waitAction = Z_ExtendedServicesRequest_wait;
3035 if (!strcmp (str, "waitIfPossible"))
3036 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3037 if (!strcmp (str, "dontWait"))
3038 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3039 if (!strcmp (str, "dontReturnPackage"))
3040 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3045 static const char *ill_array_lookup(void *clientData, const char *idx)
3047 ZOOM_package p = (ZOOM_package) clientData;
3048 return ZOOM_options_get(p->options, idx+4);
3051 static Z_External *encode_ill_request(ZOOM_package p)
3053 ODR out = p->odr_out;
3056 struct ill_get_ctl ctl;
3058 ctl.odr = p->odr_out;
3060 ctl.f = ill_array_lookup;
3062 req = ill_get_ILLRequest(&ctl, "ill", 0);
3064 if (!ill_Request(out, &req, 0, 0))
3066 int ill_request_size;
3067 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3068 if (ill_request_buf)
3069 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3074 int illRequest_size = 0;
3075 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3077 r = (Z_External *) odr_malloc(out, sizeof(*r));
3078 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3079 r->indirect_reference = 0;
3081 r->which = Z_External_single;
3083 r->u.single_ASN1_type =
3084 odr_create_Odr_oct(out,
3085 (unsigned char *)illRequest_buf,
3091 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3093 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3097 req->which = Z_IOItemOrder_esRequest;
3098 req->u.esRequest = (Z_IORequest *)
3099 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3101 /* to keep part ... */
3102 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3103 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3104 req->u.esRequest->toKeep->supplDescription = 0;
3105 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3106 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3108 str = ZOOM_options_get(p->options, "contact-name");
3109 req->u.esRequest->toKeep->contact->name =
3110 odr_strdup_null(p->odr_out, str);
3112 str = ZOOM_options_get(p->options, "contact-phone");
3113 req->u.esRequest->toKeep->contact->phone =
3114 odr_strdup_null(p->odr_out, str);
3116 str = ZOOM_options_get(p->options, "contact-email");
3117 req->u.esRequest->toKeep->contact->email =
3118 odr_strdup_null(p->odr_out, str);
3120 req->u.esRequest->toKeep->addlBilling = 0;
3122 /* not to keep part ... */
3123 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3124 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3126 str = ZOOM_options_get(p->options, "itemorder-setname");
3131 req->u.esRequest->notToKeep->resultSetItem = 0;
3134 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3135 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3137 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3138 odr_strdup(p->odr_out, str);
3139 req->u.esRequest->notToKeep->resultSetItem->item =
3140 odr_intdup(p->odr_out, 0);
3142 str = ZOOM_options_get(p->options, "itemorder-item");
3143 *req->u.esRequest->notToKeep->resultSetItem->item =
3144 (str ? atoi(str) : 1);
3147 str = ZOOM_options_getl(p->options, "doc", &len);
3150 req->u.esRequest->notToKeep->itemRequest =
3151 z_ext_record_xml(p->odr_out, str, len);
3154 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3159 Z_APDU *create_admin_package(ZOOM_package p, int type,
3160 Z_ESAdminOriginPartToKeep **toKeepP,
3161 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3163 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3166 Z_ESAdminOriginPartToKeep *toKeep;
3167 Z_ESAdminOriginPartNotToKeep *notToKeep;
3168 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3169 const char *first_db = "Default";
3171 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3176 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3178 r->indirect_reference = 0;
3179 r->which = Z_External_ESAdmin;
3181 r->u.adminService = (Z_Admin *)
3182 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3183 r->u.adminService->which = Z_Admin_esRequest;
3184 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3185 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3187 toKeep = r->u.adminService->u.esRequest->toKeep =
3188 (Z_ESAdminOriginPartToKeep *)
3189 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3190 toKeep->which = type;
3191 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3192 toKeep->u.create = odr_nullval();
3193 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3195 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3196 (Z_ESAdminOriginPartNotToKeep *)
3197 odr_malloc(p->odr_out,
3198 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3199 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3200 notToKeep->u.recordsWillFollow = odr_nullval();
3204 *notToKeepP = notToKeep;
3209 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3211 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3212 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3213 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3215 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3223 req->taskSpecificParameters = ext;
3224 ext->direct_reference = req->packageType;
3225 ext->descriptor = 0;
3226 ext->indirect_reference = 0;
3228 ext->which = Z_External_octet;
3229 ext->u.single_ASN1_type =
3230 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3234 static Z_APDU *create_update_package(ZOOM_package p)
3237 const char *first_db = "Default";
3239 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3240 const char *action = ZOOM_options_get(p->options, "action");
3241 int recordIdOpaque_len;
3242 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3243 &recordIdOpaque_len);
3244 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3246 const char *record_buf = ZOOM_options_getl(p->options, "record",
3248 int recordOpaque_len;
3249 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3251 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3252 const char *version = ZOOM_options_get(p->options, "updateVersion");
3254 const char *correlationInfo_note =
3255 ZOOM_options_get(p->options, "correlationInfo.note");
3256 const char *correlationInfo_id =
3257 ZOOM_options_get(p->options, "correlationInfo.id");
3259 Odr_oid *syntax_oid = 0;
3260 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3266 if (!record_buf && !recordOpaque_buf)
3268 record_buf = "void";
3270 syntax_str = "SUTRS";
3275 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3276 CLASS_RECSYN, syntax_str,
3288 package_oid = yaz_oid_extserv_database_update_first_version;
3289 /* old update does not support specialUpdate */
3291 action = "recordInsert";
3295 action = "specialUpdate";
3296 package_oid = yaz_oid_extserv_database_update_second_version;
3300 action = "specialUpdate";
3301 package_oid = yaz_oid_extserv_database_update;
3307 if (!strcmp(action, "recordInsert"))
3308 action_no = Z_IUOriginPartToKeep_recordInsert;
3309 else if (!strcmp(action, "recordReplace"))
3310 action_no = Z_IUOriginPartToKeep_recordReplace;
3311 else if (!strcmp(action, "recordDelete"))
3312 action_no = Z_IUOriginPartToKeep_recordDelete;
3313 else if (!strcmp(action, "elementUpdate"))
3314 action_no = Z_IUOriginPartToKeep_elementUpdate;
3315 else if (!strcmp(action, "specialUpdate"))
3316 action_no = Z_IUOriginPartToKeep_specialUpdate;
3320 apdu = create_es_package(p, package_oid);
3323 Z_IUOriginPartToKeep *toKeep;
3324 Z_IUSuppliedRecords *notToKeep;
3325 Z_External *r = (Z_External *)
3326 odr_malloc(p->odr_out, sizeof(*r));
3327 const char *elementSetName =
3328 ZOOM_options_get(p->options, "elementSetName");
3330 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3332 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3334 r->which = Z_External_update;
3335 r->indirect_reference = 0;
3336 r->u.update = (Z_IUUpdate *)
3337 odr_malloc(p->odr_out, sizeof(*r->u.update));
3339 r->u.update->which = Z_IUUpdate_esRequest;
3340 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3341 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3342 toKeep = r->u.update->u.esRequest->toKeep =
3343 (Z_IUOriginPartToKeep *)
3344 odr_malloc(p->odr_out, sizeof(*toKeep));
3346 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3349 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3351 toKeep->actionQualifier = 0;
3352 toKeep->action = odr_intdup(p->odr_out, action_no);
3354 notToKeep = r->u.update->u.esRequest->notToKeep =
3355 (Z_IUSuppliedRecords *)
3356 odr_malloc(p->odr_out, sizeof(*notToKeep));
3358 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3359 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3360 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3361 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3362 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3365 notToKeep->elements[0]->u.opaque =
3366 odr_create_Odr_oct(p->odr_out,
3367 (const unsigned char *) recordIdOpaque,
3368 recordIdOpaque_len);
3370 else if (recordIdNumber)
3372 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3374 notToKeep->elements[0]->u.number =
3375 odr_intdup(p->odr_out, atoi(recordIdNumber));
3378 notToKeep->elements[0]->u.opaque = 0;
3379 notToKeep->elements[0]->supplementalId = 0;
3380 if (correlationInfo_note || correlationInfo_id)
3382 Z_IUCorrelationInfo *ci;
3383 ci = notToKeep->elements[0]->correlationInfo =
3384 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3385 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3386 ci->id = correlationInfo_id ?
3387 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3390 notToKeep->elements[0]->correlationInfo = 0;
3391 if (recordOpaque_buf)
3393 notToKeep->elements[0]->record =
3394 z_ext_record_oid_any(p->odr_out, syntax_oid,
3395 recordOpaque_buf, recordOpaque_len);
3399 notToKeep->elements[0]->record =
3400 z_ext_record_oid(p->odr_out, syntax_oid,
3401 record_buf, record_len);
3406 ODR print = odr_createmem(ODR_PRINT);
3408 z_APDU(print, &apdu, 0, 0);
3415 ZOOM_package_send(ZOOM_package p, const char *type)
3422 odr_reset(p->odr_out);
3425 if (!strcmp(type, "itemorder"))
3427 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3430 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3432 r->direct_reference =
3433 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3435 r->which = Z_External_itemOrder;
3436 r->indirect_reference = 0;
3437 r->u.itemOrder = encode_item_order(p);
3439 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3442 else if (!strcmp(type, "create")) /* create database */
3444 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3447 else if (!strcmp(type, "drop")) /* drop database */
3449 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3452 else if (!strcmp(type, "commit")) /* commit changes */
3454 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3457 else if (!strcmp(type, "update")) /* update record(s) */
3459 apdu = create_update_package(p);
3461 else if (!strcmp(type, "xmlupdate"))
3463 apdu = create_xmlupdate_package(p);
3467 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3471 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3472 task->u.package = p;
3473 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3474 p->buf_out = (char *) xmalloc(p->len_out);
3475 memcpy(p->buf_out, buf, p->len_out);
3480 while (ZOOM_event(1, &c))
3487 ZOOM_API(ZOOM_package)
3488 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3490 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3493 p->odr_out = odr_createmem(ODR_ENCODE);
3494 p->options = ZOOM_options_create_with_parent2(options, c->options);
3502 ZOOM_package_destroy(ZOOM_package p)
3507 if (p->refcount == 0)
3509 odr_destroy(p->odr_out);
3512 ZOOM_options_destroy(p->options);
3517 ZOOM_API(const char *)
3518 ZOOM_package_option_get(ZOOM_package p, const char *key)
3520 return ZOOM_options_get(p->options, key);
3523 ZOOM_API(const char *)
3524 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3526 return ZOOM_options_getl(p->options, key, lenp);
3530 ZOOM_package_option_set(ZOOM_package p, const char *key,
3533 ZOOM_options_set(p->options, key, val);
3537 ZOOM_package_option_setl(ZOOM_package p, const char *key,
3538 const char *val, int len)
3540 ZOOM_options_setl(p->options, key, val, len);
3543 static int ZOOM_connection_exec_task(ZOOM_connection c)
3545 ZOOM_task task = c->tasks;
3546 zoom_ret ret = zoom_complete;
3550 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3551 c, task->which, task->running);
3552 if (c->error != ZOOM_ERROR_NONE)
3554 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3555 "removing tasks because of error = %d", c, c->error);
3556 ZOOM_connection_remove_tasks(c);
3561 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3562 "task already running", c);
3566 ret = zoom_complete;
3567 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3569 switch (task->which)
3571 case ZOOM_TASK_SEARCH:
3572 if (c->proto == PROTO_HTTP)
3573 ret = ZOOM_connection_srw_send_search(c);
3575 ret = ZOOM_connection_send_search(c);
3577 case ZOOM_TASK_RETRIEVE:
3578 if (c->proto == PROTO_HTTP)
3579 ret = ZOOM_connection_srw_send_search(c);
3581 ret = send_present(c);
3583 case ZOOM_TASK_CONNECT:
3584 ret = do_connect(c);
3586 case ZOOM_TASK_SCAN:
3587 if (c->proto == PROTO_HTTP)
3588 ret = ZOOM_connection_srw_send_scan(c);
3590 ret = ZOOM_connection_send_scan(c);
3592 case ZOOM_TASK_PACKAGE:
3593 ret = send_package(c);
3595 case ZOOM_TASK_SORT:
3596 c->tasks->u.sort.resultset->r_sort_spec =
3597 c->tasks->u.sort.q->sort_spec;
3598 ret = send_sort(c, c->tasks->u.sort.resultset);
3604 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3605 "remove tasks because no connection exist", c);
3606 ZOOM_connection_remove_tasks(c);
3608 if (ret == zoom_complete)
3610 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3611 "task removed (complete)", c);
3612 ZOOM_connection_remove_task(c);
3615 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3620 static zoom_ret send_sort_present(ZOOM_connection c)
3622 zoom_ret r = zoom_complete;
3624 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3625 r = send_sort(c, c->tasks->u.search.resultset);
3626 if (r == zoom_complete)
3627 r = send_present(c);
3631 static int es_response_taskpackage_update(ZOOM_connection c,
3632 Z_IUUpdateTaskPackage *utp)
3634 if (utp && utp->targetPart)
3636 Z_IUTargetPart *targetPart = utp->targetPart;
3637 switch ( *targetPart->updateStatus ) {
3638 case Z_IUTargetPart_success:
3639 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3641 case Z_IUTargetPart_partial:
3642 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3644 case Z_IUTargetPart_failure:
3645 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3646 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3647 response_diag(c, targetPart->globalDiagnostics[0]);
3650 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3655 static int es_response_taskpackage(ZOOM_connection c,
3656 Z_TaskPackage *taskPackage)
3659 Odr_oct *id = taskPackage->targetReference;
3661 ZOOM_options_setl(c->tasks->u.package->options,
3662 "targetReference", (char*) id->buf, id->len);
3665 switch ( *taskPackage->taskStatus ) {
3666 case Z_TaskPackage_pending:
3667 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3669 case Z_TaskPackage_active:
3670 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3672 case Z_TaskPackage_complete:
3673 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3675 case Z_TaskPackage_aborted:
3676 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3677 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3678 response_diag(c, taskPackage->packageDiagnostics[0]);
3682 // taskSpecificParameters
3683 // NOTE: Only Update implemented, no others.
3684 if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3685 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3686 es_response_taskpackage_update(c, utp);
3692 static int es_response(ZOOM_connection c,
3693 Z_ExtendedServicesResponse *res)
3695 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3697 switch (*res->operationStatus) {
3698 case Z_ExtendedServicesResponse_done:
3699 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3701 case Z_ExtendedServicesResponse_accepted:
3702 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3704 case Z_ExtendedServicesResponse_failure:
3705 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3706 if (res->diagnostics && res->num_diagnostics > 0)
3707 response_diag(c, res->diagnostics[0]);
3710 if (res->taskPackage &&
3711 res->taskPackage->which == Z_External_extendedService)
3713 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3714 es_response_taskpackage(c, taskPackage);
3716 if (res->taskPackage &&
3717 res->taskPackage->which == Z_External_octet)
3719 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3720 ZOOM_options_setl(c->tasks->u.package->options,
3721 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3726 static void interpret_init_diag(ZOOM_connection c,
3727 Z_DiagnosticFormat *diag)
3731 Z_DiagnosticFormat_s *ds = diag->elements[0];
3732 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3733 response_default_diag(c, ds->u.defaultDiagRec);
3738 static void interpret_otherinformation_field(ZOOM_connection c,
3739 Z_OtherInformation *ui)
3742 for (i = 0; i < ui->num_elements; i++)
3744 Z_OtherInformationUnit *unit = ui->list[i];
3745 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3746 unit->information.externallyDefinedInfo &&
3747 unit->information.externallyDefinedInfo->which ==
3750 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3756 static void set_init_option(const char *name, void *clientData) {
3757 ZOOM_connection c = (ZOOM_connection) clientData;
3760 sprintf(buf, "init_opt_%.70s", name);
3761 ZOOM_connection_option_set(c, buf, "1");
3765 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3767 Z_InitResponse *initrs;
3769 ZOOM_connection_set_mask(c, 0);
3770 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3773 case Z_APDU_initResponse:
3774 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3775 initrs = apdu->u.initResponse;
3776 ZOOM_connection_option_set(c, "serverImplementationId",
3777 initrs->implementationId ?
3778 initrs->implementationId : "");
3779 ZOOM_connection_option_set(c, "serverImplementationName",
3780 initrs->implementationName ?
3781 initrs->implementationName : "");
3782 ZOOM_connection_option_set(c, "serverImplementationVersion",
3783 initrs->implementationVersion ?
3784 initrs->implementationVersion : "");
3785 /* Set the three old options too, for old applications */
3786 ZOOM_connection_option_set(c, "targetImplementationId",
3787 initrs->implementationId ?
3788 initrs->implementationId : "");
3789 ZOOM_connection_option_set(c, "targetImplementationName",
3790 initrs->implementationName ?
3791 initrs->implementationName : "");
3792 ZOOM_connection_option_set(c, "targetImplementationVersion",
3793 initrs->implementationVersion ?
3794 initrs->implementationVersion : "");
3796 /* Make initrs->options available as ZOOM-level options */
3797 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3799 if (!*initrs->result)
3801 Z_External *uif = initrs->userInformationField;
3803 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3805 if (uif && uif->which == Z_External_userInfo1)
3806 interpret_otherinformation_field(c, uif->u.userInfo1);
3811 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3812 yaz_oid_userinfo_cookie, 1, 0);
3813 xfree(c->cookie_in);
3816 c->cookie_in = xstrdup(cookie);
3817 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3818 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3819 c->support_named_resultsets = 1;
3822 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3823 ZOOM_connection_remove_task(c);
3825 ZOOM_connection_exec_task(c);
3827 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3829 NMEM tmpmem = nmem_create();
3830 Z_CharSetandLanguageNegotiation *p =
3831 yaz_get_charneg_record(initrs->otherInfo);
3835 char *charset = NULL, *lang = NULL;
3838 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3839 yaz_log(log_details, "%p recv_apdu target accepted: "
3840 "charset %s, language %s, select %d",
3842 charset ? charset : "none", lang ? lang : "none", sel);
3844 ZOOM_connection_option_set(c, "negotiation-charset",
3847 ZOOM_connection_option_set(c, "negotiation-lang",
3850 ZOOM_connection_option_set(
3851 c, "negotiation-charset-in-effect-for-records",
3852 (sel != 0) ? "1" : "0");
3853 nmem_destroy(tmpmem);
3857 case Z_APDU_searchResponse:
3858 yaz_log(log_api, "%p recv_apdu Search response", c);
3859 handle_search_response(c, apdu->u.searchResponse);
3860 if (send_sort_present(c) == zoom_complete)
3861 ZOOM_connection_remove_task(c);
3863 case Z_APDU_presentResponse:
3864 yaz_log(log_api, "%p recv_apdu Present response", c);
3865 handle_present_response(c, apdu->u.presentResponse);
3866 if (send_present(c) == zoom_complete)
3867 ZOOM_connection_remove_task(c);
3869 case Z_APDU_sortResponse:
3870 yaz_log(log_api, "%p recv_apdu Sort response", c);
3871 sort_response(c, apdu->u.sortResponse);
3872 if (send_present(c) == zoom_complete)
3873 ZOOM_connection_remove_task(c);
3875 case Z_APDU_scanResponse:
3876 yaz_log(log_api, "%p recv_apdu Scan response", c);
3877 scan_response(c, apdu->u.scanResponse);
3878 ZOOM_connection_remove_task(c);
3880 case Z_APDU_extendedServicesResponse:
3881 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3882 es_response(c, apdu->u.extendedServicesResponse);
3883 ZOOM_connection_remove_task(c);
3886 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3887 if (!ZOOM_test_reconnect(c))
3889 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3894 yaz_log(log_api, "%p Received unknown PDU", c);
3895 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3901 static zoom_ret handle_srw_response(ZOOM_connection c,
3902 Z_SRW_searchRetrieveResponse *res)
3904 ZOOM_resultset resultset = 0;
3909 const char *syntax, *elementSetName;
3912 return zoom_complete;
3914 switch(c->tasks->which)
3916 case ZOOM_TASK_SEARCH:
3917 resultset = c->tasks->u.search.resultset;
3918 start = &c->tasks->u.search.start;
3919 count = &c->tasks->u.search.count;
3920 syntax = c->tasks->u.search.syntax;
3921 elementSetName = c->tasks->u.search.elementSetName;
3923 if (!c->tasks->u.search.recv_search_fired)
3925 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3926 ZOOM_connection_put_event(c, event);
3927 c->tasks->u.search.recv_search_fired = 1;
3930 case ZOOM_TASK_RETRIEVE:
3931 resultset = c->tasks->u.retrieve.resultset;
3932 start = &c->tasks->u.retrieve.start;
3933 count = &c->tasks->u.retrieve.count;
3934 syntax = c->tasks->u.retrieve.syntax;
3935 elementSetName = c->tasks->u.retrieve.elementSetName;
3938 return zoom_complete;
3941 resultset->size = 0;
3943 if (res->resultSetId)
3944 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
3946 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
3948 if (res->num_diagnostics > 0)
3950 set_SRU_error(c, &res->diagnostics[0]);
3954 if (res->numberOfRecords)
3955 resultset->size = *res->numberOfRecords;
3956 for (i = 0; i<res->num_records; i++)
3959 Z_SRW_record *sru_rec;
3960 Z_SRW_diagnostic *diag = 0;
3963 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
3964 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
3966 if (res->records[i].recordPosition &&
3967 *res->records[i].recordPosition > 0)
3968 pos = *res->records[i].recordPosition - 1;
3972 sru_rec = &res->records[i];
3974 npr->databaseName = 0;
3975 npr->which = Z_NamePlusRecord_databaseRecord;
3976 npr->u.databaseRecord = (Z_External *)
3977 odr_malloc(c->odr_in, sizeof(Z_External));
3978 npr->u.databaseRecord->descriptor = 0;
3979 npr->u.databaseRecord->direct_reference =
3980 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
3981 npr->u.databaseRecord->which = Z_External_octet;
3983 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
3984 odr_malloc(c->odr_in, sizeof(Odr_oct));
3985 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
3986 sru_rec->recordData_buf;
3987 npr->u.databaseRecord->u.octet_aligned->len =
3988 npr->u.databaseRecord->u.octet_aligned->size =
3989 sru_rec->recordData_len;
3991 if (sru_rec->recordSchema
3992 && !strcmp(sru_rec->recordSchema,
3993 "info:srw/schema/1/diagnostics-v1.1"))
3995 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
3996 sru_rec->recordData_len,
4000 record_cache_add(resultset, npr, pos, syntax, elementSetName,
4001 sru_rec->recordSchema, diag);
4005 if (*count + *start > resultset->size)
4006 *count = resultset->size - *start;
4010 nmem = odr_extract_mem(c->odr_in);
4011 nmem_transfer(odr_getmem(resultset->odr), nmem);
4015 return ZOOM_connection_srw_send_search(c);
4017 return zoom_complete;
4022 static void handle_srw_scan_response(ZOOM_connection c,
4023 Z_SRW_scanResponse *res)
4025 NMEM nmem = odr_extract_mem(c->odr_in);
4028 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4030 scan = c->tasks->u.scan.scan;
4032 if (res->num_diagnostics > 0)
4033 set_SRU_error(c, &res->diagnostics[0]);
4035 scan->scan_response = 0;
4036 scan->srw_scan_response = res;
4037 nmem_transfer(odr_getmem(scan->odr), nmem);
4039 ZOOM_options_set_int(scan->options, "number", res->num_terms);
4045 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4047 zoom_ret cret = zoom_complete;
4049 const char *addinfo = 0;
4050 const char *connection_head = z_HTTP_header_lookup(hres->headers,
4052 ZOOM_connection_set_mask(c, 0);
4053 yaz_log(log_details, "%p handle_http", c);
4055 if (!yaz_srw_check_content_type(hres))
4056 addinfo = "content-type";
4059 Z_SOAP *soap_package = 0;
4061 Z_SOAP_Handler soap_handlers[2] = {
4062 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4065 ret = z_soap_codec(o, &soap_package,
4066 &hres->content_buf, &hres->content_len,
4068 if (!ret && soap_package->which == Z_SOAP_generic &&
4069 soap_package->u.generic->no == 0)
4071 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4073 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4074 if (sr->which == Z_SRW_searchRetrieve_response)
4075 cret = handle_srw_response(c, sr->u.response);
4076 else if (sr->which == Z_SRW_scan_response)
4077 handle_srw_scan_response(c, sr->u.scan_response);
4081 else if (!ret && (soap_package->which == Z_SOAP_fault
4082 || soap_package->which == Z_SOAP_error))
4084 set_HTTP_error(c, hres->code,
4085 soap_package->u.fault->fault_code,
4086 soap_package->u.fault->fault_string);
4093 if (hres->code != 200)
4094 set_HTTP_error(c, hres->code, 0, 0);
4096 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4099 if (cret == zoom_complete)
4100 ZOOM_connection_remove_task(c);
4101 if (!strcmp(hres->version, "1.0"))
4103 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4104 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4109 /* HTTP 1.1: only if no close we stay alive .. */
4110 if (connection_head && !strcmp(connection_head, "close"))
4116 static int do_read(ZOOM_connection c)
4121 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4122 ZOOM_connection_put_event(c, event);
4124 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4125 more = cs_more(c->cs);
4126 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4131 if (!ZOOM_test_reconnect(c))
4133 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4142 odr_reset(c->odr_in);
4143 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4144 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4145 ZOOM_connection_put_event(c, event);
4147 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4150 int err = odr_geterrorx(c->odr_in, &x);
4152 const char *element = odr_getelement(c->odr_in);
4153 yaz_snprintf(msg, sizeof(msg),
4154 "ODR code %d:%d element=%s offset=%d",
4155 err, x, element ? element : "<unknown>",
4156 odr_offset(c->odr_in));
4157 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4160 FILE *ber_file = yaz_log_file();
4162 odr_dumpBER(ber_file, c->buf_in, r);
4169 z_GDU(c->odr_print, &gdu, 0, 0);
4170 if (gdu->which == Z_GDU_Z3950)
4171 recv_apdu(c, gdu->u.z3950);
4172 else if (gdu->which == Z_GDU_HTTP_Response)
4175 handle_http(c, gdu->u.HTTP_Response);
4177 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4182 c->reconnect_ok = 0;
4187 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4192 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4193 ZOOM_connection_put_event(c, event);
4195 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4196 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4198 yaz_log(log_details, "%p do_write_ex write failed", c);
4199 if (ZOOM_test_reconnect(c))
4201 return zoom_pending;
4203 if (c->state == STATE_CONNECTING)
4204 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4206 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4208 return zoom_complete;
4212 int mask = ZOOM_SELECT_EXCEPT;
4213 if (c->cs->io_pending & CS_WANT_WRITE)
4214 mask += ZOOM_SELECT_WRITE;
4215 if (c->cs->io_pending & CS_WANT_READ)
4216 mask += ZOOM_SELECT_READ;
4217 ZOOM_connection_set_mask(c, mask);
4218 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4223 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4224 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4227 return zoom_pending;
4230 static zoom_ret do_write(ZOOM_connection c)
4232 return do_write_ex(c, c->buf_out, c->len_out);
4236 ZOOM_API(const char *)
4237 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4239 return ZOOM_options_get(c->options, key);
4242 ZOOM_API(const char *)
4243 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4245 return ZOOM_options_getl(c->options, key, lenp);
4249 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4252 ZOOM_options_set(c->options, key, val);
4256 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4257 const char *val, int len)
4259 ZOOM_options_setl(c->options, key, val, len);
4262 ZOOM_API(const char *)
4263 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4265 return ZOOM_options_get(r->options, key);
4269 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4272 ZOOM_options_set(r->options, key, val);
4277 ZOOM_connection_errcode(ZOOM_connection c)
4279 return ZOOM_connection_error(c, 0, 0);
4282 ZOOM_API(const char *)
4283 ZOOM_connection_errmsg(ZOOM_connection c)
4286 ZOOM_connection_error(c, &msg, 0);
4290 ZOOM_API(const char *)
4291 ZOOM_connection_addinfo(ZOOM_connection c)
4293 const char *addinfo;
4294 ZOOM_connection_error(c, 0, &addinfo);
4298 ZOOM_API(const char *)
4299 ZOOM_connection_diagset(ZOOM_connection c)
4301 const char *diagset;
4302 ZOOM_connection_error_x(c, 0, 0, &diagset);
4306 ZOOM_API(const char *)
4307 ZOOM_diag_str(int error)
4311 case ZOOM_ERROR_NONE:
4313 case ZOOM_ERROR_CONNECT:
4314 return "Connect failed";
4315 case ZOOM_ERROR_MEMORY:
4316 return "Out of memory";
4317 case ZOOM_ERROR_ENCODE:
4318 return "Encoding failed";
4319 case ZOOM_ERROR_DECODE:
4320 return "Decoding failed";
4321 case ZOOM_ERROR_CONNECTION_LOST:
4322 return "Connection lost";
4323 case ZOOM_ERROR_INIT:
4324 return "Init rejected";
4325 case ZOOM_ERROR_INTERNAL:
4326 return "Internal failure";
4327 case ZOOM_ERROR_TIMEOUT:
4329 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4330 return "Unsupported protocol";
4331 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4332 return "Unsupported query type";
4333 case ZOOM_ERROR_INVALID_QUERY:
4334 return "Invalid query";
4335 case ZOOM_ERROR_CQL_PARSE:
4336 return "CQL parsing error";
4337 case ZOOM_ERROR_CQL_TRANSFORM:
4338 return "CQL transformation error";
4339 case ZOOM_ERROR_CCL_CONFIG:
4340 return "CCL configuration error";
4341 case ZOOM_ERROR_CCL_PARSE:
4342 return "CCL parsing error";
4344 return diagbib1_str(error);
4349 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4350 const char **addinfo, const char **diagset)
4352 int error = c->error;
4355 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4356 *cp = ZOOM_diag_str(error);
4357 else if (!strcmp(c->diagset, "HTTP"))
4358 *cp = z_HTTP_errmsg(c->error);
4359 else if (!strcmp(c->diagset, "Bib-1"))
4360 *cp = ZOOM_diag_str(error);
4361 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4362 *cp = yaz_diag_srw_str(c->error);
4364 *cp = "Unknown error and diagnostic set";
4367 *addinfo = c->addinfo ? c->addinfo : "";
4369 *diagset = c->diagset ? c->diagset : "";
4374 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4375 const char **addinfo)
4377 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4380 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4382 ZOOM_Event event = 0;
4383 int r = cs_look(c->cs);
4384 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4389 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4390 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4392 ZOOM_connection_put_event(c, event);
4394 else if (r == CS_CONNECT)
4396 int ret = ret = cs_rcvconnect(c->cs);
4397 yaz_log(log_details, "%p ZOOM_connection_do_io "
4398 "cs_rcvconnect returned %d", c, ret);
4401 int mask = ZOOM_SELECT_EXCEPT;
4402 if (c->cs->io_pending & CS_WANT_WRITE)
4403 mask += ZOOM_SELECT_WRITE;
4404 if (c->cs->io_pending & CS_WANT_READ)
4405 mask += ZOOM_SELECT_READ;
4406 ZOOM_connection_set_mask(c, mask);
4407 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4408 ZOOM_connection_put_event(c, event);
4412 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4413 ZOOM_connection_put_event(c, event);
4415 if (c->proto == PROTO_Z3950)
4416 ZOOM_connection_send_init(c);
4419 /* no init request for SRW .. */
4420 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4421 ZOOM_connection_remove_task(c);
4422 ZOOM_connection_set_mask(c, 0);
4423 ZOOM_connection_exec_task(c);
4425 c->state = STATE_ESTABLISHED;
4429 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4435 if (mask & ZOOM_SELECT_EXCEPT)
4437 if (!ZOOM_test_reconnect(c))
4439 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4444 if (mask & ZOOM_SELECT_READ)
4446 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4452 ZOOM_connection_last_event(ZOOM_connection cs)
4455 return ZOOM_EVENT_NONE;
4456 return cs->last_event;
4460 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4462 WRBUF wrbuf = (WRBUF) client_data;
4463 wrbuf_puts(wrbuf, buf);
4467 * Returns an xmalloc()d string containing RPN that corresponds to the
4468 * CQL passed in. On error, sets the Connection object's error state
4469 * and returns a null pointer.
4470 * ### We could cache CQL parser and/or transformer in Connection.
4472 static char *cql2pqf(ZOOM_connection c, const char *cql)
4476 const char *cqlfile;
4477 cql_transform_t trans;
4480 parser = cql_parser_create();
4481 if ((error = cql_parser_string(parser, cql)) != 0) {
4482 cql_parser_destroy(parser);
4483 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4487 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4490 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4492 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4495 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4496 cqlfile, strerror(errno));
4497 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4501 WRBUF wrbuf_result = wrbuf_alloc();
4502 error = cql_transform(trans, cql_parser_result(parser),
4503 cql2pqf_wrbuf_puts, wrbuf_result);
4506 const char *addinfo;
4507 error = cql_transform_error(trans, &addinfo);
4508 sprintf(buf, "%.200s (addinfo=%.200s)",
4509 cql_strerror(error), addinfo);
4510 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4514 result = xstrdup(wrbuf_cstr(wrbuf_result));
4516 cql_transform_close(trans);
4517 wrbuf_destroy(wrbuf_result);
4519 cql_parser_destroy(parser);
4523 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4527 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4528 /* timeout and this connection was waiting */
4529 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4531 ZOOM_connection_put_event(c, event);
4537 ZOOM_connection_process(ZOOM_connection c)
4543 event = ZOOM_connection_get_event(c);
4546 ZOOM_Event_destroy(event);
4549 ZOOM_connection_exec_task(c);
4550 event = ZOOM_connection_get_event(c);
4553 ZOOM_Event_destroy(event);
4560 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4564 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4566 for (i = 0; i<no; i++)
4568 ZOOM_connection c = cs[i];
4570 if (c && ZOOM_connection_process(c))
4576 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4578 if (c->mask && mask)
4579 ZOOM_connection_do_io(c, mask);
4583 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4586 return cs_fileno(c->cs);
4590 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4598 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4605 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4607 return ZOOM_options_get_int(c->options, "timeout", 30);
4613 * c-file-style: "Stroustrup"
4614 * indent-tabs-mode: nil
4616 * vim: shiftwidth=4 tabstop=8 expandtab