1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 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;
934 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
935 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
936 elementSetName = ZOOM_options_get(r->options, "elementSetName");
937 task->u.search.elementSetName = elementSetName
938 ? xstrdup(elementSetName) : 0;
940 ZOOM_resultset_addref(r);
946 while (ZOOM_event(1, &c))
953 ZOOM_resultset_sort(ZOOM_resultset r,
954 const char *sort_type, const char *sort_spec)
956 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
960 ZOOM_resultset_sort1(ZOOM_resultset r,
961 const char *sort_type, const char *sort_spec)
963 ZOOM_connection c = r->connection;
967 newq = ZOOM_query_create();
968 if (ZOOM_query_sortby(newq, sort_spec) < 0)
971 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
972 r, r, sort_type, sort_spec);
976 if (c->host_port && c->proto == PROTO_HTTP)
980 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
981 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
985 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
991 ZOOM_resultset_cache_reset(r);
992 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
993 task->u.sort.resultset = r;
994 task->u.sort.q = newq;
996 ZOOM_resultset_addref(r);
1000 while (ZOOM_event(1, &c))
1008 ZOOM_resultset_cache_reset(ZOOM_resultset r)
1011 for (i = 0; i<RECORD_HASH_SIZE; i++)
1013 ZOOM_record_cache rc;
1014 for (rc = r->record_hash[i]; rc; rc = rc->next)
1016 if (rc->rec.wrbuf_marc)
1017 wrbuf_destroy(rc->rec.wrbuf_marc);
1018 if (rc->rec.wrbuf_iconv)
1019 wrbuf_destroy(rc->rec.wrbuf_iconv);
1020 if (rc->rec.wrbuf_opac)
1021 wrbuf_destroy(rc->rec.wrbuf_opac);
1023 r->record_hash[i] = 0;
1028 ZOOM_resultset_destroy(ZOOM_resultset r)
1030 resultset_destroy(r);
1033 static void resultset_destroy(ZOOM_resultset r)
1038 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1040 if (r->refcount == 0)
1042 ZOOM_resultset_cache_reset(r);
1046 /* remove ourselves from the resultsets in connection */
1047 ZOOM_resultset *rp = &r->connection->resultsets;
1050 assert(*rp); /* we must be in this list!! */
1052 { /* OK, we're here - take us out of it */
1059 ZOOM_query_destroy(r->query);
1060 ZOOM_options_destroy(r->options);
1061 odr_destroy(r->odr);
1069 ZOOM_resultset_size(ZOOM_resultset r)
1071 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
1076 static void do_close(ZOOM_connection c)
1081 ZOOM_connection_set_mask(c, 0);
1082 c->state = STATE_IDLE;
1085 static int ZOOM_test_reconnect(ZOOM_connection c)
1089 if (!c->reconnect_ok)
1092 c->reconnect_ok = 0;
1093 c->tasks->running = 0;
1094 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1096 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1097 ZOOM_connection_put_event(c, event);
1102 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1103 int force_sync, int start, int count)
1108 const char *syntax, *elementSetName;
1112 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1113 " count=%d", r, force_sync, start, count);
1118 if (c->host_port && c->proto == PROTO_HTTP)
1122 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1123 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1127 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1129 c->reconnect_ok = 1;
1132 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1133 task->u.retrieve.resultset = r;
1134 task->u.retrieve.start = start;
1135 task->u.retrieve.count = count;
1137 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1138 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1139 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1140 task->u.retrieve.elementSetName = elementSetName
1141 ? xstrdup(elementSetName) : 0;
1143 cp = ZOOM_options_get(r->options, "schema");
1146 if (!r->schema || strcmp(r->schema, cp))
1149 r->schema = xstrdup(cp);
1153 ZOOM_resultset_addref(r);
1155 if (!r->connection->async || force_sync)
1156 while (r->connection && ZOOM_event(1, &r->connection))
1161 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1162 size_t start, size_t count)
1164 int force_present = 0;
1168 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1169 r, r, (long) start, (long) count);
1172 ZOOM_resultset_retrieve(r, force_present, start, count);
1176 for (i = 0; i< count; i++)
1177 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1181 static void get_cert(ZOOM_connection c)
1186 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1188 ZOOM_connection_option_setl(c, "sslPeerCert",
1189 cert_buf, cert_len);
1194 static zoom_ret do_connect(ZOOM_connection c)
1197 const char *effective_host;
1200 effective_host = c->proxy;
1202 effective_host = c->host_port;
1204 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1208 c->cs = cs_create_host(effective_host, 0, &add);
1210 if (c->cs && c->cs->protocol == PROTO_HTTP)
1213 const char *path = 0;
1215 c->proto = PROTO_HTTP;
1216 cs_get_host_args(c->host_port, &path);
1218 c->path = (char*) xmalloc(strlen(path)+2);
1220 strcpy(c->path+1, path);
1222 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1224 return zoom_complete;
1229 int ret = cs_connect(c->cs, add);
1232 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1233 ZOOM_connection_put_event(c, event);
1235 if (c->proto == PROTO_Z3950)
1236 ZOOM_connection_send_init(c);
1239 /* no init request for SRW .. */
1240 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1241 ZOOM_connection_remove_task(c);
1242 ZOOM_connection_set_mask(c, 0);
1243 ZOOM_connection_exec_task(c);
1245 c->state = STATE_ESTABLISHED;
1246 return zoom_pending;
1250 int mask = ZOOM_SELECT_EXCEPT;
1251 if (c->cs->io_pending & CS_WANT_WRITE)
1252 mask += ZOOM_SELECT_WRITE;
1253 if (c->cs->io_pending & CS_WANT_READ)
1254 mask += ZOOM_SELECT_READ;
1255 ZOOM_connection_set_mask(c, mask);
1256 c->state = STATE_CONNECTING;
1257 return zoom_pending;
1260 c->state = STATE_IDLE;
1261 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1262 return zoom_complete;
1265 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1268 for (i = 0; i<200; i++)
1272 Z_OtherInformation **oi;
1277 sprintf(buf, "otherInfo%d", i);
1278 val = ZOOM_options_get(c->options, buf);
1281 cp = strchr(val, ':');
1285 if (len >= sizeof(buf))
1286 len = sizeof(buf)-1;
1287 memcpy(buf, val, len);
1290 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1295 yaz_oi_APDU(a, &oi);
1296 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1300 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1305 Z_OtherInformation **oi;
1306 yaz_oi_APDU(a, &oi);
1307 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1312 Z_OtherInformation **oi;
1313 yaz_oi_APDU(a, &oi);
1314 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1317 otherInfo_attach(c, a, out);
1318 if (!z_APDU(out, &a, 0, 0))
1320 FILE *outf = fopen("/tmp/apdu.txt", "a");
1323 ODR odr_pr = odr_createmem(ODR_PRINT);
1324 fprintf(outf, "a=%p\n", a);
1325 odr_setprint(odr_pr, outf);
1326 z_APDU(odr_pr, &a, 0, 0);
1327 odr_destroy(odr_pr);
1329 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1330 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1335 z_APDU(c->odr_print, &a, 0, 0);
1336 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1340 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1344 if (encode_APDU(c, a, c->odr_out))
1345 return zoom_complete;
1346 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1347 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1348 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1349 ZOOM_connection_put_event(c, event);
1350 odr_reset(c->odr_out);
1354 /* returns 1 if PDU was sent OK (still pending )
1355 0 if PDU was not sent OK (nothing to wait for)
1358 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1360 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1361 Z_InitRequest *ireq = apdu->u.initRequest;
1362 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1363 odr_malloc(c->odr_out, sizeof(*auth));
1366 ODR_MASK_SET(ireq->options, Z_Options_search);
1367 ODR_MASK_SET(ireq->options, Z_Options_present);
1368 ODR_MASK_SET(ireq->options, Z_Options_scan);
1369 ODR_MASK_SET(ireq->options, Z_Options_sort);
1370 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1371 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1373 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1374 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1375 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1377 /* Index Data's Z39.50 Implementor Id is 81 */
1378 ireq->implementationId =
1379 odr_prepend(c->odr_out,
1380 ZOOM_options_get(c->options, "implementationId"),
1381 odr_prepend(c->odr_out, "81", ireq->implementationId));
1383 ireq->implementationName =
1384 odr_prepend(c->odr_out,
1385 ZOOM_options_get(c->options, "implementationName"),
1386 odr_prepend(c->odr_out, "ZOOM-C",
1387 ireq->implementationName));
1389 version = odr_strdup(c->odr_out, "$Revision: 1.154 $");
1390 if (strlen(version) > 10) /* check for unexpanded CVS strings */
1391 version[strlen(version)-2] = '\0';
1392 ireq->implementationVersion =
1393 odr_prepend(c->odr_out,
1394 ZOOM_options_get(c->options, "implementationVersion"),
1395 odr_prepend(c->odr_out, &version[11],
1396 ireq->implementationVersion));
1398 *ireq->maximumRecordSize = c->maximum_record_size;
1399 *ireq->preferredMessageSize = c->preferred_message_size;
1401 if (c->group || c->password)
1403 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1404 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1405 pass->userId = odr_strdup_null(c->odr_out, c->user);
1406 pass->password = odr_strdup_null(c->odr_out, c->password);
1407 auth->which = Z_IdAuthentication_idPass;
1408 auth->u.idPass = pass;
1409 ireq->idAuthentication = auth;
1413 auth->which = Z_IdAuthentication_open;
1414 auth->u.open = odr_strdup(c->odr_out, c->user);
1415 ireq->idAuthentication = auth;
1419 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1420 yaz_oid_userinfo_proxy, 1, c->host_port);
1422 if (c->charset || c->lang)
1424 Z_OtherInformation **oi;
1425 Z_OtherInformationUnit *oi_unit;
1427 yaz_oi_APDU(apdu, &oi);
1429 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1431 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1432 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1433 oi_unit->information.externallyDefinedInfo =
1434 yaz_set_proposal_charneg_list(c->odr_out, " ",
1435 c->charset, c->lang, 1);
1439 return send_APDU(c, apdu);
1443 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1447 const char *database = ZOOM_options_get(c->options, "databaseName");
1448 char *fdatabase = 0;
1452 fdatabase = (char *) odr_malloc(c->odr_out, strlen(database)+2);
1453 strcpy(fdatabase, "/");
1454 strcat(fdatabase, database);
1456 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
1457 fdatabase ? fdatabase : c->path);
1459 if (c->sru_mode == zoom_sru_get)
1461 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1463 else if (c->sru_mode == zoom_sru_post)
1465 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1467 else if (c->sru_mode == zoom_sru_soap)
1469 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1471 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1472 return zoom_complete;
1474 z_GDU(c->odr_print, &gdu, 0, 0);
1475 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1477 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1478 ZOOM_connection_put_event(c, event);
1479 odr_reset(c->odr_out);
1485 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1487 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1488 sr->username = c->user;
1489 sr->password = c->password;
1495 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1499 ZOOM_resultset resultset = 0;
1501 const char *option_val = 0;
1503 if (c->error) /* don't continue on error */
1504 return zoom_complete;
1506 switch(c->tasks->which)
1508 case ZOOM_TASK_SEARCH:
1509 resultset = c->tasks->u.search.resultset;
1510 if (!resultset->setname)
1511 resultset->setname = xstrdup("default");
1512 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1513 start = &c->tasks->u.search.start;
1514 count = &c->tasks->u.search.count;
1516 case ZOOM_TASK_RETRIEVE:
1517 resultset = c->tasks->u.retrieve.resultset;
1519 start = &c->tasks->u.retrieve.start;
1520 count = &c->tasks->u.retrieve.count;
1522 if (*start >= resultset->size)
1523 return zoom_complete;
1524 if (*start + *count > resultset->size)
1525 *count = resultset->size - *start;
1527 for (i = 0; i < *count; i++)
1530 record_cache_lookup(resultset, i + *start,
1531 c->tasks->u.retrieve.syntax,
1532 c->tasks->u.retrieve.elementSetName);
1537 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1538 ZOOM_connection_put_event(c, event);
1545 return zoom_complete;
1548 return zoom_complete;
1550 assert(resultset->query);
1552 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1553 if (resultset->query->z_query->which == Z_Query_type_104
1554 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1556 sr->u.request->query_type = Z_SRW_query_type_cql;
1557 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1559 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1560 resultset->query->z_query->u.type_1)
1562 sr->u.request->query_type = Z_SRW_query_type_pqf;
1563 sr->u.request->query.pqf = resultset->query->query_string;
1567 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1568 return zoom_complete;
1570 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1571 sr->u.request->maximumRecords = odr_intdup(
1572 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
1573 resultset->step : *count);
1574 sr->u.request->recordSchema = resultset->schema;
1576 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1578 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1580 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1581 yaz_encode_sru_extra(sr, c->odr_out, option_val);
1582 return send_srw(c, sr);
1585 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1587 return zoom_complete;
1591 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1594 int lslb, ssub, mspn;
1596 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1597 Z_SearchRequest *search_req = apdu->u.searchRequest;
1598 const char *elementSetName;
1599 const char *smallSetElementSetName;
1600 const char *mediumSetElementSetName;
1603 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1605 r = c->tasks->u.search.resultset;
1607 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1610 ZOOM_options_get(r->options, "elementSetName");
1611 smallSetElementSetName =
1612 ZOOM_options_get(r->options, "smallSetElementSetName");
1613 mediumSetElementSetName =
1614 ZOOM_options_get(r->options, "mediumSetElementSetName");
1616 if (!smallSetElementSetName)
1617 smallSetElementSetName = elementSetName;
1619 if (!mediumSetElementSetName)
1620 mediumSetElementSetName = elementSetName;
1625 /* prepare query for the search request */
1626 search_req->query = r->query->z_query;
1627 if (!search_req->query)
1629 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1630 return zoom_complete;
1632 if (r->query->z_query->which == Z_Query_type_1 ||
1633 r->query->z_query->which == Z_Query_type_101)
1635 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1638 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1641 search_req->query = yaz_copy_Z_Query(search_req->query,
1644 yaz_query_charset_convert_rpnquery(search_req->query->u.type_1,
1646 yaz_iconv_close(cd);
1650 search_req->databaseNames = r->databaseNames;
1651 search_req->num_databaseNames = r->num_databaseNames;
1653 /* get syntax (no need to provide unless piggyback is in effect) */
1654 syntax = c->tasks->u.search.syntax;
1656 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1657 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1658 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1659 if (lslb != -1 && ssub != -1 && mspn != -1)
1661 /* So're a Z39.50 expert? Let's hope you don't do sort */
1662 *search_req->largeSetLowerBound = lslb;
1663 *search_req->smallSetUpperBound = ssub;
1664 *search_req->mediumSetPresentNumber = mspn;
1666 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1667 && r->piggyback && !r->r_sort_spec && !r->schema)
1669 /* Regular piggyback - do it unless we're going to do sort */
1670 *search_req->largeSetLowerBound = 2000000000;
1671 *search_req->smallSetUpperBound = 1;
1672 *search_req->mediumSetPresentNumber =
1673 r->step>0 ? r->step : c->tasks->u.search.count;
1677 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1678 smallSetElementSetName = 0;
1679 mediumSetElementSetName = 0;
1682 if (smallSetElementSetName && *smallSetElementSetName)
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, smallSetElementSetName);
1689 search_req->smallSetElementSetNames = esn;
1691 if (mediumSetElementSetName && *mediumSetElementSetName)
1693 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1694 odr_malloc(c->odr_out, sizeof(*esn));
1696 esn->which = Z_ElementSetNames_generic;
1697 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1698 search_req->mediumSetElementSetNames = esn;
1701 search_req->preferredRecordSyntax =
1702 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1706 if (c->support_named_resultsets)
1710 /* find the lowest unused ordinal so that we re-use
1711 result sets on the server. */
1712 for (ord = 1; ; ord++)
1715 sprintf(setname, "%d", ord);
1716 for (rp = c->resultsets; rp; rp = rp->next)
1717 if (rp->setname && !strcmp(rp->setname, setname))
1722 r->setname = xstrdup(setname);
1723 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1724 "set %s", c, r->setname);
1728 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1730 r->setname = xstrdup("default");
1732 ZOOM_options_set(r->options, "setname", r->setname);
1734 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1735 return send_APDU(c, apdu);
1738 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1740 char oid_name_buf[OID_STR_MAX];
1741 const char *oid_name;
1744 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1747 case Z_DefaultDiagFormat_v2Addinfo:
1748 addinfo = r->u.v2Addinfo;
1750 case Z_DefaultDiagFormat_v3Addinfo:
1751 addinfo = r->u.v3Addinfo;
1756 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1759 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1761 if (p->which != Z_DiagRec_defaultFormat)
1762 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1764 response_default_diag(c, p->u.defaultFormat);
1767 ZOOM_API(ZOOM_record)
1768 ZOOM_record_clone(ZOOM_record srec)
1775 odr_enc = odr_createmem(ODR_ENCODE);
1776 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1778 buf = odr_getbuf(odr_enc, &size, 0);
1780 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1781 nrec->odr = odr_createmem(ODR_DECODE);
1782 nrec->wrbuf_marc = 0;
1783 nrec->wrbuf_iconv = 0;
1784 nrec->wrbuf_opac = 0;
1785 odr_setbuf(nrec->odr, buf, size, 0);
1786 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1788 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1789 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1790 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1791 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1792 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1793 odr_destroy(odr_enc);
1797 ZOOM_API(ZOOM_record)
1798 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1800 const char *syntax =
1801 ZOOM_options_get(s->options, "preferredRecordSyntax");
1802 const char *elementSetName =
1803 ZOOM_options_get(s->options, "elementSetName");
1805 return record_cache_lookup(s, pos, syntax, elementSetName);
1808 ZOOM_API(ZOOM_record)
1809 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1811 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1816 * MIKE: I think force_sync should always be zero, but I don't
1817 * want to make this change until I get the go-ahead from
1818 * Adam, in case something depends on the old synchronous
1822 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1823 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1824 rec = ZOOM_resultset_record_immediate(r, pos);
1830 ZOOM_record_destroy(ZOOM_record rec)
1834 if (rec->wrbuf_marc)
1835 wrbuf_destroy(rec->wrbuf_marc);
1836 if (rec->wrbuf_iconv)
1837 wrbuf_destroy(rec->wrbuf_iconv);
1838 if (rec->wrbuf_opac)
1839 wrbuf_destroy(rec->wrbuf_opac);
1840 odr_destroy(rec->odr);
1844 static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
1846 const char *buf, int sz,
1847 const char *record_charset)
1852 yaz_marc_t mt = yaz_marc_create();
1855 strcpy(to, "UTF-8");
1856 if (record_charset && *record_charset)
1858 /* Use "from,to" or just "from" */
1859 const char *cp = strchr(record_charset, ',');
1860 int clen = strlen(record_charset);
1863 strncpy( to, cp+1, sizeof(to)-1);
1864 to[sizeof(to)-1] = '\0';
1865 clen = cp - record_charset;
1867 if (clen > sizeof(from)-1)
1868 clen = sizeof(from)-1;
1871 strncpy(from, record_charset, clen);
1877 cd = yaz_iconv_open(to, from);
1878 yaz_marc_iconv(mt, cd);
1881 yaz_marc_xml(mt, marc_type);
1882 if (!rec->wrbuf_marc)
1883 rec->wrbuf_marc = wrbuf_alloc();
1884 wrbuf_rewind(rec->wrbuf_marc);
1885 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
1887 yaz_marc_destroy(mt);
1889 yaz_iconv_close(cd);
1891 *len = wrbuf_len(rec->wrbuf_marc);
1892 return wrbuf_cstr(rec->wrbuf_marc);
1894 yaz_marc_destroy(mt);
1896 yaz_iconv_close(cd);
1900 static const char *record_iconv_return(ZOOM_record rec, int *len,
1901 const char *buf, int sz,
1902 const char *record_charset)
1909 strcpy(to, "UTF-8");
1911 if (record_charset && *record_charset)
1913 /* Use "from,to" or just "from" */
1914 const char *cp = strchr(record_charset, ',');
1915 int clen = strlen(record_charset);
1918 strncpy( to, cp+1, sizeof(to)-1);
1919 to[sizeof(to)-1] = '\0';
1920 clen = cp - record_charset;
1922 if (clen > sizeof(from)-1)
1923 clen = sizeof(from)-1;
1926 strncpy(from, record_charset, clen);
1930 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1932 if (!rec->wrbuf_iconv)
1933 rec->wrbuf_iconv = wrbuf_alloc();
1935 wrbuf_rewind(rec->wrbuf_iconv);
1937 wrbuf_iconv_write(rec->wrbuf_iconv, cd, buf, sz);
1938 wrbuf_iconv_reset(rec->wrbuf_iconv, cd);
1940 buf = wrbuf_cstr(rec->wrbuf_iconv);
1941 sz = wrbuf_len(rec->wrbuf_iconv);
1942 yaz_iconv_close(cd);
1951 ZOOM_record_error(ZOOM_record rec, const char **cp,
1952 const char **addinfo, const char **diagset)
1954 Z_NamePlusRecord *npr;
1963 *cp = rec->diag_message;
1965 *addinfo = rec->diag_details;
1967 *diagset = rec->diag_set;
1968 return uri_to_code(rec->diag_uri);
1970 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
1972 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
1973 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
1974 const char *add = 0;
1976 if (diag_rec->which == Z_DiagRec_defaultFormat)
1978 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
1981 error = *ddf->condition;
1984 case Z_DefaultDiagFormat_v2Addinfo:
1985 add = ddf->u.v2Addinfo;
1987 case Z_DefaultDiagFormat_v3Addinfo:
1988 add = ddf->u.v3Addinfo;
1993 yaz_oid_to_string(yaz_oid_std(),
1994 ddf->diagnosticSetId, &oclass);
2002 *addinfo = add ? add : "";
2004 *cp = diagbib1_str(error);
2010 ZOOM_API(const char *)
2011 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
2018 Z_NamePlusRecord *npr;
2021 *len = 0; /* default return */
2030 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
2032 if (cp[i] == ';' || cp[i] == ' ')
2038 while (type_spec[i] == ';')
2041 while (type_spec[i] == ' ')
2043 if (!strncmp(type_spec+i, "charset=", 8))
2046 i = i + 8; /* skip charset= */
2047 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
2049 if (type_spec[i] == ';' || type_spec[i] == ' ')
2055 else if (!strncmp(type_spec+i, "xpath=", 6))
2059 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
2063 while (type_spec[i] == ' ')
2066 if (!strcmp(type, "database"))
2069 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2070 return npr->databaseName;
2072 else if (!strcmp(type, "schema"))
2075 *len = rec->schema ? strlen(rec->schema) : 0;
2078 else if (!strcmp(type, "syntax"))
2080 const char *desc = 0;
2081 if (npr->which == Z_NamePlusRecord_databaseRecord)
2083 Z_External *r = (Z_External *) npr->u.databaseRecord;
2084 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2089 *len = strlen(desc);
2092 if (npr->which != Z_NamePlusRecord_databaseRecord)
2095 /* from now on - we have a database record .. */
2096 if (!strcmp(type, "render"))
2098 Z_External *r = (Z_External *) npr->u.databaseRecord;
2099 const Odr_oid *oid = r->direct_reference;
2101 /* render bibliographic record .. */
2102 if (r->which == Z_External_OPAC)
2104 r = r->u.opac->bibliographicRecord;
2107 oid = r->direct_reference;
2109 if (r->which == Z_External_sutrs)
2110 return record_iconv_return(rec, len,
2111 (char*) r->u.sutrs->buf,
2114 else if (r->which == Z_External_octet)
2116 if (yaz_oid_is_iso2709(oid))
2118 const char *ret_buf = marc_iconv_return(
2119 rec, YAZ_MARC_LINE, len,
2120 (const char *) r->u.octet_aligned->buf,
2121 r->u.octet_aligned->len,
2126 return record_iconv_return(rec, len,
2127 (const char *) r->u.octet_aligned->buf,
2128 r->u.octet_aligned->len,
2131 else if (r->which == Z_External_grs1)
2133 if (!rec->wrbuf_marc)
2134 rec->wrbuf_marc = wrbuf_alloc();
2135 wrbuf_rewind(rec->wrbuf_marc);
2136 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
2137 return record_iconv_return(rec, len,
2138 wrbuf_buf(rec->wrbuf_marc),
2139 wrbuf_len(rec->wrbuf_marc),
2144 else if (!strcmp(type, "xml"))
2146 Z_External *r = (Z_External *) npr->u.databaseRecord;
2147 const Odr_oid *oid = r->direct_reference;
2149 /* render bibliographic record .. */
2150 if (r->which == Z_External_OPAC)
2152 r = r->u.opac->bibliographicRecord;
2155 oid = r->direct_reference;
2158 if (r->which == Z_External_sutrs)
2159 return record_iconv_return(rec, len,
2160 (const char *) r->u.sutrs->buf,
2163 else if (r->which == Z_External_octet)
2165 int marc_decode_type = YAZ_MARC_MARCXML;
2166 if (yaz_oid_is_iso2709(oid))
2168 const char *ret_buf = marc_iconv_return(
2169 rec, marc_decode_type, len,
2170 (const char *) r->u.octet_aligned->buf,
2171 r->u.octet_aligned->len,
2176 return record_iconv_return(rec, len,
2177 (const char *) r->u.octet_aligned->buf,
2178 r->u.octet_aligned->len,
2181 else if (r->which == Z_External_grs1)
2188 else if (!strcmp(type, "raw"))
2190 Z_External *r = (Z_External *) npr->u.databaseRecord;
2192 if (r->which == Z_External_sutrs)
2194 if (len) *len = r->u.sutrs->len;
2195 return (const char *) r->u.sutrs->buf;
2197 else if (r->which == Z_External_octet)
2199 if (len) *len = r->u.octet_aligned->len;
2200 return (const char *) r->u.octet_aligned->buf;
2202 else /* grs-1, explain, OPAC, ... */
2205 return (const char *) npr->u.databaseRecord;
2209 else if (!strcmp (type, "ext"))
2212 return (const char *) npr->u.databaseRecord;
2214 else if (!strcmp (type, "opac"))
2217 Z_External *r = (Z_External *) npr->u.databaseRecord;
2218 if (r->which == Z_External_OPAC)
2220 if (!rec->wrbuf_opac)
2221 rec->wrbuf_opac = wrbuf_alloc();
2222 wrbuf_rewind(rec->wrbuf_opac);
2223 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
2224 return record_iconv_return(rec, len,
2225 wrbuf_buf(rec->wrbuf_opac),
2226 wrbuf_len(rec->wrbuf_opac),
2233 static int strcmp_null(const char *v1, const char *v2)
2239 return strcmp(v1, v2);
2242 static size_t record_hash(int pos)
2246 return pos % RECORD_HASH_SIZE;
2249 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2251 const char *syntax, const char *elementSetName,
2253 Z_SRW_diagnostic *diag)
2255 ZOOM_record_cache rc = 0;
2257 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2258 ZOOM_connection_put_event(r->connection, event);
2260 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2263 && strcmp_null(r->schema, rc->schema) == 0
2264 && strcmp_null(elementSetName,rc->elementSetName) == 0
2265 && strcmp_null(syntax, rc->syntax) == 0)
2270 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2272 rc->rec.wrbuf_marc = 0;
2273 rc->rec.wrbuf_iconv = 0;
2274 rc->rec.wrbuf_opac = 0;
2275 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2277 rc->syntax = odr_strdup_null(r->odr, syntax);
2279 rc->schema = odr_strdup_null(r->odr, r->schema);
2282 rc->next = r->record_hash[record_hash(pos)];
2283 r->record_hash[record_hash(pos)] = rc;
2286 rc->rec.schema = odr_strdup_null(r->odr, schema);
2287 rc->rec.diag_set = 0;
2288 rc->rec.diag_uri = 0;
2289 rc->rec.diag_message = 0;
2290 rc->rec.diag_details = 0;
2296 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2297 if ((cp = strrchr(rc->rec.diag_set, '/')))
2299 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2301 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2302 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2306 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2308 const char *elementSetName)
2310 ZOOM_record_cache rc;
2312 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2316 if (strcmp_null(r->schema, rc->schema))
2318 if (strcmp_null(elementSetName,rc->elementSetName))
2320 if (strcmp_null(syntax, rc->syntax))
2328 static void handle_records(ZOOM_connection c, Z_Records *sr,
2331 ZOOM_resultset resultset;
2333 const char *syntax = 0, *elementSetName = 0;
2337 switch (c->tasks->which)
2339 case ZOOM_TASK_SEARCH:
2340 resultset = c->tasks->u.search.resultset;
2341 start = &c->tasks->u.search.start;
2342 count = &c->tasks->u.search.count;
2343 syntax = c->tasks->u.search.syntax;
2344 elementSetName = c->tasks->u.search.elementSetName;
2346 case ZOOM_TASK_RETRIEVE:
2347 resultset = c->tasks->u.retrieve.resultset;
2348 start = &c->tasks->u.retrieve.start;
2349 count = &c->tasks->u.retrieve.count;
2350 syntax = c->tasks->u.retrieve.syntax;
2351 elementSetName = c->tasks->u.retrieve.elementSetName;
2356 if (sr && sr->which == Z_Records_NSD)
2357 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2358 else if (sr && sr->which == Z_Records_multipleNSD)
2360 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2361 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2363 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2367 if (*count + *start > resultset->size)
2368 *count = resultset->size - *start;
2371 if (sr && sr->which == Z_Records_DBOSD)
2374 NMEM nmem = odr_extract_mem(c->odr_in);
2375 Z_NamePlusRecordList *p =
2376 sr->u.databaseOrSurDiagnostics;
2377 for (i = 0; i<p->num_records; i++)
2379 record_cache_add(resultset, p->records[i], i + *start,
2380 syntax, elementSetName,
2387 yaz_log(log_details,
2388 "handle_records resultset=%p start=%d count=%d",
2389 resultset, *start, *count);
2391 /* transfer our response to search_nmem .. we need it later */
2392 nmem_transfer(odr_getmem(resultset->odr), nmem);
2394 if (present_phase && p->num_records == 0)
2396 /* present response and we didn't get any records! */
2397 Z_NamePlusRecord *myrec =
2398 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2399 record_cache_add(resultset, myrec, *start,
2400 syntax, elementSetName, 0, 0);
2403 else if (present_phase)
2405 /* present response and we didn't get any records! */
2406 Z_NamePlusRecord *myrec =
2407 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2408 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2414 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2416 handle_records(c, pr->records, 1);
2419 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2422 switch (term->which)
2424 case Z_Term_general:
2425 ZOOM_options_setl(opt, name,
2426 (const char *)(term->u.general->buf),
2427 term->u.general->len);
2429 case Z_Term_characterString:
2430 ZOOM_options_set(opt, name, term->u.characterString);
2432 case Z_Term_numeric:
2433 ZOOM_options_set_int(opt, name, *term->u.numeric);
2438 static void handle_queryExpression(ZOOM_options opt, const char *name,
2439 Z_QueryExpression *exp)
2445 case Z_QueryExpression_term:
2446 if (exp->u.term && exp->u.term->queryTerm)
2448 sprintf(opt_name, "%s.term", name);
2449 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2452 case Z_QueryExpression_query:
2457 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2458 Z_OtherInformation *o)
2461 for (i = 0; o && i < o->num_elements; i++)
2463 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2465 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2467 if (ext->which == Z_External_searchResult1)
2470 Z_SearchInfoReport *sr = ext->u.searchResult1;
2473 ZOOM_options_set_int(
2474 resultset->options, "searchresult.size", sr->num);
2476 for (j = 0; j < sr->num; j++)
2478 Z_SearchInfoReport_s *ent =
2479 ext->u.searchResult1->elements[j];
2482 sprintf(pref, "searchresult.%d", j);
2484 if (ent->subqueryId)
2487 sprintf(opt_name, "%s.id", pref);
2488 ZOOM_options_set(resultset->options, opt_name,
2491 if (ent->subqueryExpression)
2494 sprintf(opt_name, "%s.subquery", pref);
2495 handle_queryExpression(resultset->options, opt_name,
2496 ent->subqueryExpression);
2498 if (ent->subqueryInterpretation)
2501 sprintf(opt_name, "%s.interpretation", pref);
2502 handle_queryExpression(resultset->options, opt_name,
2503 ent->subqueryInterpretation);
2505 if (ent->subqueryRecommendation)
2508 sprintf(opt_name, "%s.recommendation", pref);
2509 handle_queryExpression(resultset->options, opt_name,
2510 ent->subqueryRecommendation);
2512 if (ent->subqueryCount)
2515 sprintf(opt_name, "%s.count", pref);
2516 ZOOM_options_set_int(resultset->options, opt_name,
2517 *ent->subqueryCount);
2525 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2527 ZOOM_resultset resultset;
2530 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2533 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2534 ZOOM_connection_put_event(c, event);
2536 resultset = c->tasks->u.search.resultset;
2538 if (sr->resultSetStatus)
2540 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2541 *sr->resultSetStatus);
2543 if (sr->presentStatus)
2545 ZOOM_options_set_int(resultset->options, "presentStatus",
2546 *sr->presentStatus);
2548 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2550 resultset->size = *sr->resultCount;
2551 handle_records(c, sr->records, 0);
2554 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2556 if (res->diagnostics && res->num_diagnostics > 0)
2557 response_diag(c, res->diagnostics[0]);
2560 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2562 NMEM nmem = odr_extract_mem(c->odr_in);
2565 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2567 scan = c->tasks->u.scan.scan;
2569 if (res->entries && res->entries->nonsurrogateDiagnostics)
2570 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2571 scan->scan_response = res;
2572 scan->srw_scan_response = 0;
2573 nmem_transfer(odr_getmem(scan->odr), nmem);
2575 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2576 if (res->positionOfTerm)
2577 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2578 if (res->scanStatus)
2579 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2580 if (res->numberOfEntriesReturned)
2581 ZOOM_options_set_int(scan->options, "number",
2582 *res->numberOfEntriesReturned);
2587 static zoom_ret send_sort(ZOOM_connection c,
2588 ZOOM_resultset resultset)
2591 resultset->r_sort_spec = 0;
2592 if (resultset->r_sort_spec)
2594 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2595 Z_SortRequest *req = apdu->u.sortRequest;
2597 req->num_inputResultSetNames = 1;
2598 req->inputResultSetNames = (Z_InternationalString **)
2599 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2600 req->inputResultSetNames[0] =
2601 odr_strdup(c->odr_out, resultset->setname);
2602 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2603 req->sortSequence = resultset->r_sort_spec;
2604 resultset->r_sort_spec = 0;
2605 return send_APDU(c, apdu);
2607 return zoom_complete;
2610 static zoom_ret send_present(ZOOM_connection c)
2613 Z_PresentRequest *req = 0;
2615 const char *syntax = 0;
2616 const char *elementSetName = 0;
2617 ZOOM_resultset resultset;
2622 yaz_log(log_details, "%p send_present no tasks", c);
2623 return zoom_complete;
2626 switch (c->tasks->which)
2628 case ZOOM_TASK_SEARCH:
2629 resultset = c->tasks->u.search.resultset;
2630 start = &c->tasks->u.search.start;
2631 count = &c->tasks->u.search.count;
2632 syntax = c->tasks->u.search.syntax;
2633 elementSetName = c->tasks->u.search.elementSetName;
2635 case ZOOM_TASK_RETRIEVE:
2636 resultset = c->tasks->u.retrieve.resultset;
2637 start = &c->tasks->u.retrieve.start;
2638 count = &c->tasks->u.retrieve.count;
2639 syntax = c->tasks->u.retrieve.syntax;
2640 elementSetName = c->tasks->u.retrieve.elementSetName;
2643 return zoom_complete;
2645 yaz_log(log_details, "%p send_present start=%d count=%d",
2648 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2650 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2653 if (c->error) /* don't continue on error */
2654 return zoom_complete;
2655 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2656 resultset, *start, *count);
2658 for (i = 0; i < *count; i++)
2661 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2666 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2667 ZOOM_connection_put_event(c, event);
2675 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2676 return zoom_complete;
2679 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2680 req = apdu->u.presentRequest;
2683 yaz_log(log_details, "%p send_present skip=%d", c, i);
2685 *req->resultSetStartPoint = *start + 1;
2687 if (resultset->step > 0 && resultset->step < *count)
2688 *req->numberOfRecordsRequested = resultset->step;
2690 *req->numberOfRecordsRequested = *count;
2692 if (*req->numberOfRecordsRequested + *start > resultset->size)
2693 *req->numberOfRecordsRequested = resultset->size - *start;
2694 assert(*req->numberOfRecordsRequested > 0);
2696 if (syntax && *syntax)
2697 req->preferredRecordSyntax =
2698 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2700 if (resultset->schema && *resultset->schema)
2702 Z_RecordComposition *compo = (Z_RecordComposition *)
2703 odr_malloc(c->odr_out, sizeof(*compo));
2705 req->recordComposition = compo;
2706 compo->which = Z_RecordComp_complex;
2707 compo->u.complex = (Z_CompSpec *)
2708 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2709 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2710 odr_malloc(c->odr_out, sizeof(bool_t));
2711 *compo->u.complex->selectAlternativeSyntax = 0;
2713 compo->u.complex->generic = (Z_Specification *)
2714 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2716 compo->u.complex->generic->which = Z_Schema_oid;
2717 compo->u.complex->generic->schema.oid = (Odr_oid *)
2718 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2720 if (!compo->u.complex->generic->schema.oid)
2722 /* OID wasn't a schema! Try record syntax instead. */
2724 compo->u.complex->generic->schema.oid = (Odr_oid *)
2725 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2727 if (elementSetName && *elementSetName)
2729 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2730 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2731 compo->u.complex->generic->elementSpec->which =
2732 Z_ElementSpec_elementSetName;
2733 compo->u.complex->generic->elementSpec->u.elementSetName =
2734 odr_strdup(c->odr_out, elementSetName);
2737 compo->u.complex->generic->elementSpec = 0;
2738 compo->u.complex->num_dbSpecific = 0;
2739 compo->u.complex->dbSpecific = 0;
2740 compo->u.complex->num_recordSyntax = 0;
2741 compo->u.complex->recordSyntax = 0;
2743 else if (elementSetName && *elementSetName)
2745 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2746 odr_malloc(c->odr_out, sizeof(*esn));
2747 Z_RecordComposition *compo = (Z_RecordComposition *)
2748 odr_malloc(c->odr_out, sizeof(*compo));
2750 esn->which = Z_ElementSetNames_generic;
2751 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2752 compo->which = Z_RecordComp_simple;
2753 compo->u.simple = esn;
2754 req->recordComposition = compo;
2756 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2757 return send_APDU(c, apdu);
2760 ZOOM_API(ZOOM_scanset)
2761 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2764 ZOOM_query q = ZOOM_query_create();
2766 ZOOM_query_prefix(q, start);
2768 s = ZOOM_connection_scan1(c, q);
2769 ZOOM_query_destroy(q);
2774 ZOOM_API(ZOOM_scanset)
2775 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2777 ZOOM_scanset scan = 0;
2781 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2782 scan->connection = c;
2783 scan->odr = odr_createmem(ODR_DECODE);
2784 scan->options = ZOOM_options_create_with_parent(c->options);
2786 scan->scan_response = 0;
2787 scan->srw_scan_response = 0;
2791 scan->databaseNames = set_DatabaseNames(c, c->options,
2792 &scan->num_databaseNames,
2797 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2798 task->u.scan.scan = scan;
2803 while (ZOOM_event(1, &c))
2811 ZOOM_scanset_destroy(ZOOM_scanset scan)
2816 if (scan->refcount == 0)
2818 ZOOM_query_destroy(scan->query);
2820 odr_destroy(scan->odr);
2822 ZOOM_options_destroy(scan->options);
2827 static zoom_ret send_package(ZOOM_connection c)
2831 yaz_log(log_details, "%p send_package", c);
2833 return zoom_complete;
2834 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2836 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2837 ZOOM_connection_put_event(c, event);
2839 c->buf_out = c->tasks->u.package->buf_out;
2840 c->len_out = c->tasks->u.package->len_out;
2845 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2848 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2849 Z_ScanRequest *req = apdu->u.scanRequest;
2851 yaz_log(log_details, "%p send_scan", c);
2853 return zoom_complete;
2854 assert (c->tasks->which == ZOOM_TASK_SCAN);
2855 scan = c->tasks->u.scan.scan;
2857 /* Z39.50 scan can only carry RPN */
2858 if (scan->query->z_query->which == Z_Query_type_1 ||
2859 scan->query->z_query->which == Z_Query_type_101)
2861 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2862 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2865 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2868 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2870 yaz_query_charset_convert_rpnquery(
2871 rpn, c->odr_out, cd);
2872 yaz_iconv_close(cd);
2875 req->attributeSet = rpn->attributeSetId;
2876 if (!req->attributeSet)
2877 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2878 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2879 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2881 req->termListAndStartPoint =
2882 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2886 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2887 return zoom_complete;
2892 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2893 return zoom_complete;
2896 *req->numberOfTermsRequested =
2897 ZOOM_options_get_int(scan->options, "number", 10);
2899 req->preferredPositionInResponse =
2900 odr_intdup(c->odr_out,
2901 ZOOM_options_get_int(scan->options, "position", 1));
2904 odr_intdup(c->odr_out,
2905 ZOOM_options_get_int(scan->options, "stepSize", 0));
2907 req->databaseNames = scan->databaseNames;
2908 req->num_databaseNames = scan->num_databaseNames;
2910 return send_APDU(c, apdu);
2914 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2918 const char *option_val = 0;
2921 return zoom_complete;
2922 assert (c->tasks->which == ZOOM_TASK_SCAN);
2923 scan = c->tasks->u.scan.scan;
2925 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2927 /* SRU scan can only carry CQL and PQF */
2928 if (scan->query->z_query->which == Z_Query_type_104)
2930 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2931 sr->u.scan_request->scanClause.cql = scan->query->query_string;
2933 else if (scan->query->z_query->which == Z_Query_type_1
2934 || scan->query->z_query->which == Z_Query_type_101)
2936 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2937 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2941 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2942 return zoom_complete;
2945 sr->u.scan_request->maximumTerms = odr_intdup(
2946 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2948 sr->u.scan_request->responsePosition = odr_intdup(
2949 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2951 option_val = ZOOM_options_get(scan->options, "extraArgs");
2952 yaz_encode_sru_extra(sr, c->odr_out, option_val);
2953 return send_srw(c, sr);
2956 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2958 return zoom_complete;
2964 ZOOM_scanset_size(ZOOM_scanset scan)
2969 if (scan->scan_response && scan->scan_response->entries)
2970 return scan->scan_response->entries->num_entries;
2971 else if (scan->srw_scan_response)
2972 return scan->srw_scan_response->num_terms;
2976 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2978 const char **value_term, size_t *value_len,
2979 const char **disp_term, size_t *disp_len)
2981 size_t noent = ZOOM_scanset_size(scan);
2990 if (pos >= noent || pos < 0)
2992 if (scan->scan_response)
2994 Z_ScanResponse *res = scan->scan_response;
2995 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2997 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2999 *value_term = (const char *) t->term->u.general->buf;
3000 *value_len = t->term->u.general->len;
3003 *disp_term = t->displayTerm;
3004 *disp_len = strlen(*disp_term);
3006 else if (t->term->which == Z_Term_general)
3008 *disp_term = (const char *) t->term->u.general->buf;
3009 *disp_len = t->term->u.general->len;
3011 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
3014 if (scan->srw_scan_response)
3016 Z_SRW_scanResponse *res = scan->srw_scan_response;
3017 Z_SRW_scanTerm *t = res->terms + pos;
3020 *value_term = t->value;
3021 *value_len = strlen(*value_term);
3024 *disp_term = t->displayTerm;
3026 *disp_term = t->value;
3027 *disp_len = strlen(*disp_term);
3028 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
3033 ZOOM_API(const char *)
3034 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
3037 const char *value_term = 0;
3038 size_t value_len = 0;
3039 const char *disp_term = 0;
3040 size_t disp_len = 0;
3042 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3043 &disp_term, &disp_len);
3049 ZOOM_API(const char *)
3050 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3053 const char *value_term = 0;
3054 size_t value_len = 0;
3055 const char *disp_term = 0;
3056 size_t disp_len = 0;
3058 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3059 &disp_term, &disp_len);
3065 ZOOM_API(const char *)
3066 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3068 return ZOOM_options_get(scan->options, key);
3072 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3075 ZOOM_options_set(scan->options, key, val);
3078 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3081 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3082 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3084 str = ZOOM_options_get(p->options, "package-name");
3086 req->packageName = odr_strdup(p->odr_out, str);
3088 str = ZOOM_options_get(p->options, "user-id");
3090 req->userId = odr_strdup_null(p->odr_out, str);
3092 req->packageType = odr_oiddup(p->odr_out, oid);
3094 str = ZOOM_options_get(p->options, "function");
3097 if (!strcmp (str, "create"))
3098 *req->function = Z_ExtendedServicesRequest_create;
3099 if (!strcmp (str, "delete"))
3100 *req->function = Z_ExtendedServicesRequest_delete;
3101 if (!strcmp (str, "modify"))
3102 *req->function = Z_ExtendedServicesRequest_modify;
3105 str = ZOOM_options_get(p->options, "waitAction");
3108 if (!strcmp (str, "wait"))
3109 *req->waitAction = Z_ExtendedServicesRequest_wait;
3110 if (!strcmp (str, "waitIfPossible"))
3111 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3112 if (!strcmp (str, "dontWait"))
3113 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3114 if (!strcmp (str, "dontReturnPackage"))
3115 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3120 static const char *ill_array_lookup(void *clientData, const char *idx)
3122 ZOOM_package p = (ZOOM_package) clientData;
3123 return ZOOM_options_get(p->options, idx+4);
3126 static Z_External *encode_ill_request(ZOOM_package p)
3128 ODR out = p->odr_out;
3131 struct ill_get_ctl ctl;
3133 ctl.odr = p->odr_out;
3135 ctl.f = ill_array_lookup;
3137 req = ill_get_ILLRequest(&ctl, "ill", 0);
3139 if (!ill_Request(out, &req, 0, 0))
3141 int ill_request_size;
3142 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3143 if (ill_request_buf)
3144 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3149 int illRequest_size = 0;
3150 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3152 r = (Z_External *) odr_malloc(out, sizeof(*r));
3153 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3154 r->indirect_reference = 0;
3156 r->which = Z_External_single;
3158 r->u.single_ASN1_type =
3159 odr_create_Odr_oct(out,
3160 (unsigned char *)illRequest_buf,
3166 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3168 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3172 req->which = Z_IOItemOrder_esRequest;
3173 req->u.esRequest = (Z_IORequest *)
3174 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3176 /* to keep part ... */
3177 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3178 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3179 req->u.esRequest->toKeep->supplDescription = 0;
3180 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3181 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3183 str = ZOOM_options_get(p->options, "contact-name");
3184 req->u.esRequest->toKeep->contact->name =
3185 odr_strdup_null(p->odr_out, str);
3187 str = ZOOM_options_get(p->options, "contact-phone");
3188 req->u.esRequest->toKeep->contact->phone =
3189 odr_strdup_null(p->odr_out, str);
3191 str = ZOOM_options_get(p->options, "contact-email");
3192 req->u.esRequest->toKeep->contact->email =
3193 odr_strdup_null(p->odr_out, str);
3195 req->u.esRequest->toKeep->addlBilling = 0;
3197 /* not to keep part ... */
3198 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3199 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3201 str = ZOOM_options_get(p->options, "itemorder-setname");
3206 req->u.esRequest->notToKeep->resultSetItem = 0;
3209 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3210 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3212 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3213 odr_strdup(p->odr_out, str);
3214 req->u.esRequest->notToKeep->resultSetItem->item =
3215 (int *) odr_malloc(p->odr_out, sizeof(int));
3217 str = ZOOM_options_get(p->options, "itemorder-item");
3218 *req->u.esRequest->notToKeep->resultSetItem->item =
3219 (str ? atoi(str) : 1);
3222 str = ZOOM_options_getl(p->options, "doc", &len);
3225 req->u.esRequest->notToKeep->itemRequest =
3226 z_ext_record_xml(p->odr_out, str, len);
3229 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3234 Z_APDU *create_admin_package(ZOOM_package p, int type,
3235 Z_ESAdminOriginPartToKeep **toKeepP,
3236 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3238 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3241 Z_ESAdminOriginPartToKeep *toKeep;
3242 Z_ESAdminOriginPartNotToKeep *notToKeep;
3243 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3244 const char *first_db = "Default";
3246 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3251 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3253 r->indirect_reference = 0;
3254 r->which = Z_External_ESAdmin;
3256 r->u.adminService = (Z_Admin *)
3257 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3258 r->u.adminService->which = Z_Admin_esRequest;
3259 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3260 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3262 toKeep = r->u.adminService->u.esRequest->toKeep =
3263 (Z_ESAdminOriginPartToKeep *)
3264 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3265 toKeep->which = type;
3266 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3267 toKeep->u.create = odr_nullval();
3268 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3270 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3271 (Z_ESAdminOriginPartNotToKeep *)
3272 odr_malloc(p->odr_out,
3273 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3274 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3275 notToKeep->u.recordsWillFollow = odr_nullval();
3279 *notToKeepP = notToKeep;
3284 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3286 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3287 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3288 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3290 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3298 req->taskSpecificParameters = ext;
3299 ext->direct_reference = req->packageType;
3300 ext->descriptor = 0;
3301 ext->indirect_reference = 0;
3303 ext->which = Z_External_octet;
3304 ext->u.single_ASN1_type =
3305 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3309 static Z_APDU *create_update_package(ZOOM_package p)
3312 const char *first_db = "Default";
3314 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3315 const char *action = ZOOM_options_get(p->options, "action");
3316 int recordIdOpaque_len;
3317 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3318 &recordIdOpaque_len);
3319 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3321 const char *record_buf = ZOOM_options_getl(p->options, "record",
3323 int recordOpaque_len;
3324 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3326 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3327 const char *version = ZOOM_options_get(p->options, "updateVersion");
3329 const char *correlationInfo_note =
3330 ZOOM_options_get(p->options, "correlationInfo.note");
3331 const char *correlationInfo_id =
3332 ZOOM_options_get(p->options, "correlationInfo.id");
3334 Odr_oid *syntax_oid = 0;
3335 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3341 if (!record_buf && !recordOpaque_buf)
3343 record_buf = "void";
3345 syntax_str = "SUTRS";
3350 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3351 CLASS_RECSYN, syntax_str,
3363 package_oid = yaz_oid_extserv_database_update_first_version;
3364 /* old update does not support specialUpdate */
3366 action = "recordInsert";
3370 action = "specialUpdate";
3371 package_oid = yaz_oid_extserv_database_update_second_version;
3375 action = "specialUpdate";
3376 package_oid = yaz_oid_extserv_database_update;
3382 if (!strcmp(action, "recordInsert"))
3383 action_no = Z_IUOriginPartToKeep_recordInsert;
3384 else if (!strcmp(action, "recordReplace"))
3385 action_no = Z_IUOriginPartToKeep_recordReplace;
3386 else if (!strcmp(action, "recordDelete"))
3387 action_no = Z_IUOriginPartToKeep_recordDelete;
3388 else if (!strcmp(action, "elementUpdate"))
3389 action_no = Z_IUOriginPartToKeep_elementUpdate;
3390 else if (!strcmp(action, "specialUpdate"))
3391 action_no = Z_IUOriginPartToKeep_specialUpdate;
3395 apdu = create_es_package(p, package_oid);
3398 Z_IUOriginPartToKeep *toKeep;
3399 Z_IUSuppliedRecords *notToKeep;
3400 Z_External *r = (Z_External *)
3401 odr_malloc(p->odr_out, sizeof(*r));
3402 const char *elementSetName =
3403 ZOOM_options_get(p->options, "elementSetName");
3405 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3407 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3409 r->which = Z_External_update;
3410 r->indirect_reference = 0;
3411 r->u.update = (Z_IUUpdate *)
3412 odr_malloc(p->odr_out, sizeof(*r->u.update));
3414 r->u.update->which = Z_IUUpdate_esRequest;
3415 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3416 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3417 toKeep = r->u.update->u.esRequest->toKeep =
3418 (Z_IUOriginPartToKeep *)
3419 odr_malloc(p->odr_out, sizeof(*toKeep));
3421 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3424 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3426 toKeep->actionQualifier = 0;
3427 toKeep->action = odr_intdup(p->odr_out, action_no);
3429 notToKeep = r->u.update->u.esRequest->notToKeep =
3430 (Z_IUSuppliedRecords *)
3431 odr_malloc(p->odr_out, sizeof(*notToKeep));
3433 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3434 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3435 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3436 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3437 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3440 notToKeep->elements[0]->u.opaque =
3441 odr_create_Odr_oct(p->odr_out,
3442 (const unsigned char *) recordIdOpaque,
3443 recordIdOpaque_len);
3445 else if (recordIdNumber)
3447 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3449 notToKeep->elements[0]->u.number =
3450 odr_intdup(p->odr_out, atoi(recordIdNumber));
3453 notToKeep->elements[0]->u.opaque = 0;
3454 notToKeep->elements[0]->supplementalId = 0;
3455 if (correlationInfo_note || correlationInfo_id)
3457 Z_IUCorrelationInfo *ci;
3458 ci = notToKeep->elements[0]->correlationInfo =
3459 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3460 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3461 ci->id = correlationInfo_id ?
3462 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3465 notToKeep->elements[0]->correlationInfo = 0;
3466 if (recordOpaque_buf)
3468 notToKeep->elements[0]->record =
3469 z_ext_record_oid_any(p->odr_out, syntax_oid,
3470 recordOpaque_buf, recordOpaque_len);
3474 notToKeep->elements[0]->record =
3475 z_ext_record_oid(p->odr_out, syntax_oid,
3476 record_buf, record_len);
3481 ODR print = odr_createmem(ODR_PRINT);
3483 z_APDU(print, &apdu, 0, 0);
3490 ZOOM_package_send(ZOOM_package p, const char *type)
3497 odr_reset(p->odr_out);
3500 if (!strcmp(type, "itemorder"))
3502 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3505 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3507 r->direct_reference =
3508 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3510 r->which = Z_External_itemOrder;
3511 r->indirect_reference = 0;
3512 r->u.itemOrder = encode_item_order(p);
3514 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3517 else if (!strcmp(type, "create")) /* create database */
3519 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3522 else if (!strcmp(type, "drop")) /* drop database */
3524 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3527 else if (!strcmp(type, "commit")) /* commit changes */
3529 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3532 else if (!strcmp(type, "update")) /* update record(s) */
3534 apdu = create_update_package(p);
3536 else if (!strcmp(type, "xmlupdate"))
3538 apdu = create_xmlupdate_package(p);
3542 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3546 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3547 task->u.package = p;
3548 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3549 p->buf_out = (char *) xmalloc(p->len_out);
3550 memcpy(p->buf_out, buf, p->len_out);
3555 while (ZOOM_event(1, &c))
3562 ZOOM_API(ZOOM_package)
3563 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3565 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3568 p->odr_out = odr_createmem(ODR_ENCODE);
3569 p->options = ZOOM_options_create_with_parent2(options, c->options);
3577 ZOOM_package_destroy(ZOOM_package p)
3582 if (p->refcount == 0)
3584 odr_destroy(p->odr_out);
3587 ZOOM_options_destroy(p->options);
3592 ZOOM_API(const char *)
3593 ZOOM_package_option_get(ZOOM_package p, const char *key)
3595 return ZOOM_options_get(p->options, key);
3598 ZOOM_API(const char *)
3599 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3601 return ZOOM_options_getl(p->options, key, lenp);
3605 ZOOM_package_option_set(ZOOM_package p, const char *key,
3608 ZOOM_options_set(p->options, key, val);
3612 ZOOM_package_option_setl(ZOOM_package p, const char *key,
3613 const char *val, int len)
3615 ZOOM_options_setl(p->options, key, val, len);
3618 static int ZOOM_connection_exec_task(ZOOM_connection c)
3620 ZOOM_task task = c->tasks;
3621 zoom_ret ret = zoom_complete;
3625 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3626 c, task->which, task->running);
3627 if (c->error != ZOOM_ERROR_NONE)
3629 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3630 "removing tasks because of error = %d", c, c->error);
3631 ZOOM_connection_remove_tasks(c);
3636 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3637 "task already running", c);
3641 ret = zoom_complete;
3642 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3644 switch (task->which)
3646 case ZOOM_TASK_SEARCH:
3647 if (c->proto == PROTO_HTTP)
3648 ret = ZOOM_connection_srw_send_search(c);
3650 ret = ZOOM_connection_send_search(c);
3652 case ZOOM_TASK_RETRIEVE:
3653 if (c->proto == PROTO_HTTP)
3654 ret = ZOOM_connection_srw_send_search(c);
3656 ret = send_present(c);
3658 case ZOOM_TASK_CONNECT:
3659 ret = do_connect(c);
3661 case ZOOM_TASK_SCAN:
3662 if (c->proto == PROTO_HTTP)
3663 ret = ZOOM_connection_srw_send_scan(c);
3665 ret = ZOOM_connection_send_scan(c);
3667 case ZOOM_TASK_PACKAGE:
3668 ret = send_package(c);
3670 case ZOOM_TASK_SORT:
3671 c->tasks->u.sort.resultset->r_sort_spec =
3672 c->tasks->u.sort.q->sort_spec;
3673 ret = send_sort(c, c->tasks->u.sort.resultset);
3679 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3680 "remove tasks because no connection exist", c);
3681 ZOOM_connection_remove_tasks(c);
3683 if (ret == zoom_complete)
3685 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3686 "task removed (complete)", c);
3687 ZOOM_connection_remove_task(c);
3690 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3695 static zoom_ret send_sort_present(ZOOM_connection c)
3697 zoom_ret r = zoom_complete;
3699 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3700 r = send_sort(c, c->tasks->u.search.resultset);
3701 if (r == zoom_complete)
3702 r = send_present(c);
3706 static int es_response_taskpackage_update(ZOOM_connection c,
3707 Z_IUUpdateTaskPackage *utp)
3709 if (utp && utp->targetPart)
3711 Z_IUTargetPart *targetPart = utp->targetPart;
3712 switch ( *targetPart->updateStatus ) {
3713 case Z_IUTargetPart_success:
3714 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3716 case Z_IUTargetPart_partial:
3717 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3719 case Z_IUTargetPart_failure:
3720 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3721 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3722 response_diag(c, targetPart->globalDiagnostics[0]);
3725 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3730 static int es_response_taskpackage(ZOOM_connection c,
3731 Z_TaskPackage *taskPackage)
3734 Odr_oct *id = taskPackage->targetReference;
3736 ZOOM_options_setl(c->tasks->u.package->options,
3737 "targetReference", (char*) id->buf, id->len);
3740 switch ( *taskPackage->taskStatus ) {
3741 case Z_TaskPackage_pending:
3742 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3744 case Z_TaskPackage_active:
3745 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3747 case Z_TaskPackage_complete:
3748 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3750 case Z_TaskPackage_aborted:
3751 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3752 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3753 response_diag(c, taskPackage->packageDiagnostics[0]);
3757 // taskSpecificParameters
3758 // NOTE: Only Update implemented, no others.
3759 if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3760 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3761 es_response_taskpackage_update(c, utp);
3767 static int es_response(ZOOM_connection c,
3768 Z_ExtendedServicesResponse *res)
3770 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3772 switch (*res->operationStatus) {
3773 case Z_ExtendedServicesResponse_done:
3774 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3776 case Z_ExtendedServicesResponse_accepted:
3777 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3779 case Z_ExtendedServicesResponse_failure:
3780 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3781 if (res->diagnostics && res->num_diagnostics > 0)
3782 response_diag(c, res->diagnostics[0]);
3785 if (res->taskPackage &&
3786 res->taskPackage->which == Z_External_extendedService)
3788 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3789 es_response_taskpackage(c, taskPackage);
3791 if (res->taskPackage &&
3792 res->taskPackage->which == Z_External_octet)
3794 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3795 ZOOM_options_setl(c->tasks->u.package->options,
3796 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3801 static void interpret_init_diag(ZOOM_connection c,
3802 Z_DiagnosticFormat *diag)
3806 Z_DiagnosticFormat_s *ds = diag->elements[0];
3807 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3808 response_default_diag(c, ds->u.defaultDiagRec);
3813 static void interpret_otherinformation_field(ZOOM_connection c,
3814 Z_OtherInformation *ui)
3817 for (i = 0; i < ui->num_elements; i++)
3819 Z_OtherInformationUnit *unit = ui->list[i];
3820 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3821 unit->information.externallyDefinedInfo &&
3822 unit->information.externallyDefinedInfo->which ==
3825 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3831 static void set_init_option(const char *name, void *clientData) {
3832 ZOOM_connection c = (ZOOM_connection) clientData;
3835 sprintf(buf, "init_opt_%.70s", name);
3836 ZOOM_connection_option_set(c, buf, "1");
3840 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3842 Z_InitResponse *initrs;
3844 ZOOM_connection_set_mask(c, 0);
3845 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3848 case Z_APDU_initResponse:
3849 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3850 initrs = apdu->u.initResponse;
3851 ZOOM_connection_option_set(c, "serverImplementationId",
3852 initrs->implementationId ?
3853 initrs->implementationId : "");
3854 ZOOM_connection_option_set(c, "serverImplementationName",
3855 initrs->implementationName ?
3856 initrs->implementationName : "");
3857 ZOOM_connection_option_set(c, "serverImplementationVersion",
3858 initrs->implementationVersion ?
3859 initrs->implementationVersion : "");
3860 /* Set the three old options too, for old applications */
3861 ZOOM_connection_option_set(c, "targetImplementationId",
3862 initrs->implementationId ?
3863 initrs->implementationId : "");
3864 ZOOM_connection_option_set(c, "targetImplementationName",
3865 initrs->implementationName ?
3866 initrs->implementationName : "");
3867 ZOOM_connection_option_set(c, "targetImplementationVersion",
3868 initrs->implementationVersion ?
3869 initrs->implementationVersion : "");
3871 /* Make initrs->options available as ZOOM-level options */
3872 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3874 if (!*initrs->result)
3876 Z_External *uif = initrs->userInformationField;
3878 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3880 if (uif && uif->which == Z_External_userInfo1)
3881 interpret_otherinformation_field(c, uif->u.userInfo1);
3886 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3887 yaz_oid_userinfo_cookie, 1, 0);
3888 xfree(c->cookie_in);
3891 c->cookie_in = xstrdup(cookie);
3892 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3893 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3894 c->support_named_resultsets = 1;
3897 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3898 ZOOM_connection_remove_task(c);
3900 ZOOM_connection_exec_task(c);
3902 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3904 NMEM tmpmem = nmem_create();
3905 Z_CharSetandLanguageNegotiation *p =
3906 yaz_get_charneg_record(initrs->otherInfo);
3910 char *charset = NULL, *lang = NULL;
3913 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3914 yaz_log(log_details, "%p recv_apdu target accepted: "
3915 "charset %s, language %s, select %d",
3917 charset ? charset : "none", lang ? lang : "none", sel);
3919 ZOOM_connection_option_set(c, "negotiation-charset",
3922 ZOOM_connection_option_set(c, "negotiation-lang",
3925 ZOOM_connection_option_set(
3926 c, "negotiation-charset-in-effect-for-records",
3927 (sel != 0) ? "1" : "0");
3928 nmem_destroy(tmpmem);
3932 case Z_APDU_searchResponse:
3933 yaz_log(log_api, "%p recv_apdu Search response", c);
3934 handle_search_response(c, apdu->u.searchResponse);
3935 if (send_sort_present(c) == zoom_complete)
3936 ZOOM_connection_remove_task(c);
3938 case Z_APDU_presentResponse:
3939 yaz_log(log_api, "%p recv_apdu Present response", c);
3940 handle_present_response(c, apdu->u.presentResponse);
3941 if (send_present(c) == zoom_complete)
3942 ZOOM_connection_remove_task(c);
3944 case Z_APDU_sortResponse:
3945 yaz_log(log_api, "%p recv_apdu Sort response", c);
3946 sort_response(c, apdu->u.sortResponse);
3947 if (send_present(c) == zoom_complete)
3948 ZOOM_connection_remove_task(c);
3950 case Z_APDU_scanResponse:
3951 yaz_log(log_api, "%p recv_apdu Scan response", c);
3952 scan_response(c, apdu->u.scanResponse);
3953 ZOOM_connection_remove_task(c);
3955 case Z_APDU_extendedServicesResponse:
3956 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3957 es_response(c, apdu->u.extendedServicesResponse);
3958 ZOOM_connection_remove_task(c);
3961 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3962 if (!ZOOM_test_reconnect(c))
3964 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3969 yaz_log(log_api, "%p Received unknown PDU", c);
3970 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3976 static zoom_ret handle_srw_response(ZOOM_connection c,
3977 Z_SRW_searchRetrieveResponse *res)
3979 ZOOM_resultset resultset = 0;
3984 const char *syntax, *elementSetName;
3987 return zoom_complete;
3989 switch(c->tasks->which)
3991 case ZOOM_TASK_SEARCH:
3992 resultset = c->tasks->u.search.resultset;
3993 start = &c->tasks->u.search.start;
3994 count = &c->tasks->u.search.count;
3995 syntax = c->tasks->u.search.syntax;
3996 elementSetName = c->tasks->u.search.elementSetName;
3998 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3999 ZOOM_connection_put_event(c, event);
4002 case ZOOM_TASK_RETRIEVE:
4003 resultset = c->tasks->u.retrieve.resultset;
4004 start = &c->tasks->u.retrieve.start;
4005 count = &c->tasks->u.retrieve.count;
4006 syntax = c->tasks->u.retrieve.syntax;
4007 elementSetName = c->tasks->u.retrieve.elementSetName;
4010 return zoom_complete;
4013 resultset->size = 0;
4015 if (res->resultSetId)
4016 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
4018 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
4020 if (res->num_diagnostics > 0)
4022 set_SRU_error(c, &res->diagnostics[0]);
4026 if (res->numberOfRecords)
4027 resultset->size = *res->numberOfRecords;
4028 for (i = 0; i<res->num_records; i++)
4031 Z_SRW_record *sru_rec;
4032 Z_SRW_diagnostic *diag = 0;
4035 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
4036 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
4038 if (res->records[i].recordPosition &&
4039 *res->records[i].recordPosition > 0)
4040 pos = *res->records[i].recordPosition - 1;
4044 sru_rec = &res->records[i];
4046 npr->databaseName = 0;
4047 npr->which = Z_NamePlusRecord_databaseRecord;
4048 npr->u.databaseRecord = (Z_External *)
4049 odr_malloc(c->odr_in, sizeof(Z_External));
4050 npr->u.databaseRecord->descriptor = 0;
4051 npr->u.databaseRecord->direct_reference =
4052 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
4053 npr->u.databaseRecord->which = Z_External_octet;
4055 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
4056 odr_malloc(c->odr_in, sizeof(Odr_oct));
4057 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
4058 sru_rec->recordData_buf;
4059 npr->u.databaseRecord->u.octet_aligned->len =
4060 npr->u.databaseRecord->u.octet_aligned->size =
4061 sru_rec->recordData_len;
4063 if (sru_rec->recordSchema
4064 && !strcmp(sru_rec->recordSchema,
4065 "info:srw/schema/1/diagnostics-v1.1"))
4067 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
4068 sru_rec->recordData_len,
4072 record_cache_add(resultset, npr, pos, syntax, elementSetName,
4073 sru_rec->recordSchema, diag);
4077 if (*count + *start > resultset->size)
4078 *count = resultset->size - *start;
4082 nmem = odr_extract_mem(c->odr_in);
4083 nmem_transfer(odr_getmem(resultset->odr), nmem);
4087 return ZOOM_connection_srw_send_search(c);
4089 return zoom_complete;
4094 static void handle_srw_scan_response(ZOOM_connection c,
4095 Z_SRW_scanResponse *res)
4097 NMEM nmem = odr_extract_mem(c->odr_in);
4100 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4102 scan = c->tasks->u.scan.scan;
4104 if (res->num_diagnostics > 0)
4105 set_SRU_error(c, &res->diagnostics[0]);
4107 scan->scan_response = 0;
4108 scan->srw_scan_response = res;
4109 nmem_transfer(odr_getmem(scan->odr), nmem);
4111 ZOOM_options_set_int(scan->options, "number", res->num_terms);
4117 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4119 zoom_ret cret = zoom_complete;
4121 const char *addinfo = 0;
4122 const char *connection_head = z_HTTP_header_lookup(hres->headers,
4124 ZOOM_connection_set_mask(c, 0);
4125 yaz_log(log_details, "%p handle_http", c);
4127 if (!yaz_srw_check_content_type(hres))
4128 addinfo = "content-type";
4131 Z_SOAP *soap_package = 0;
4133 Z_SOAP_Handler soap_handlers[2] = {
4134 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4137 ret = z_soap_codec(o, &soap_package,
4138 &hres->content_buf, &hres->content_len,
4140 if (!ret && soap_package->which == Z_SOAP_generic &&
4141 soap_package->u.generic->no == 0)
4143 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4145 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4146 if (sr->which == Z_SRW_searchRetrieve_response)
4147 cret = handle_srw_response(c, sr->u.response);
4148 else if (sr->which == Z_SRW_scan_response)
4149 handle_srw_scan_response(c, sr->u.scan_response);
4153 else if (!ret && (soap_package->which == Z_SOAP_fault
4154 || soap_package->which == Z_SOAP_error))
4156 set_HTTP_error(c, hres->code,
4157 soap_package->u.fault->fault_code,
4158 soap_package->u.fault->fault_string);
4165 if (hres->code != 200)
4166 set_HTTP_error(c, hres->code, 0, 0);
4168 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4171 if (cret == zoom_complete)
4172 ZOOM_connection_remove_task(c);
4173 if (!strcmp(hres->version, "1.0"))
4175 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4176 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4181 /* HTTP 1.1: only if no close we stay alive .. */
4182 if (connection_head && !strcmp(connection_head, "close"))
4188 static int do_read(ZOOM_connection c)
4193 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4194 ZOOM_connection_put_event(c, event);
4196 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4197 more = cs_more(c->cs);
4198 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4203 if (!ZOOM_test_reconnect(c))
4205 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4214 odr_reset(c->odr_in);
4215 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4216 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4217 ZOOM_connection_put_event(c, event);
4219 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4222 int err = odr_geterrorx(c->odr_in, &x);
4224 const char *element = odr_getelement(c->odr_in);
4225 yaz_snprintf(msg, sizeof(msg),
4226 "ODR code %d:%d element=%s offset=%d",
4227 err, x, element ? element : "<unknown>",
4228 odr_offset(c->odr_in));
4229 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4232 FILE *ber_file = yaz_log_file();
4234 odr_dumpBER(ber_file, c->buf_in, r);
4241 z_GDU(c->odr_print, &gdu, 0, 0);
4242 if (gdu->which == Z_GDU_Z3950)
4243 recv_apdu(c, gdu->u.z3950);
4244 else if (gdu->which == Z_GDU_HTTP_Response)
4247 handle_http(c, gdu->u.HTTP_Response);
4249 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4254 c->reconnect_ok = 0;
4259 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4264 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4265 ZOOM_connection_put_event(c, event);
4267 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4268 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4270 yaz_log(log_details, "%p do_write_ex write failed", c);
4271 if (ZOOM_test_reconnect(c))
4273 return zoom_pending;
4275 if (c->state == STATE_CONNECTING)
4276 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4278 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4280 return zoom_complete;
4284 int mask = ZOOM_SELECT_EXCEPT;
4285 if (c->cs->io_pending & CS_WANT_WRITE)
4286 mask += ZOOM_SELECT_WRITE;
4287 if (c->cs->io_pending & CS_WANT_READ)
4288 mask += ZOOM_SELECT_READ;
4289 ZOOM_connection_set_mask(c, mask);
4290 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4295 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4296 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4299 return zoom_pending;
4302 static zoom_ret do_write(ZOOM_connection c)
4304 return do_write_ex(c, c->buf_out, c->len_out);
4308 ZOOM_API(const char *)
4309 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4311 return ZOOM_options_get(c->options, key);
4314 ZOOM_API(const char *)
4315 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4317 return ZOOM_options_getl(c->options, key, lenp);
4321 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4324 ZOOM_options_set(c->options, key, val);
4328 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4329 const char *val, int len)
4331 ZOOM_options_setl(c->options, key, val, len);
4334 ZOOM_API(const char *)
4335 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4337 return ZOOM_options_get(r->options, key);
4341 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4344 ZOOM_options_set(r->options, key, val);
4349 ZOOM_connection_errcode(ZOOM_connection c)
4351 return ZOOM_connection_error(c, 0, 0);
4354 ZOOM_API(const char *)
4355 ZOOM_connection_errmsg(ZOOM_connection c)
4358 ZOOM_connection_error(c, &msg, 0);
4362 ZOOM_API(const char *)
4363 ZOOM_connection_addinfo(ZOOM_connection c)
4365 const char *addinfo;
4366 ZOOM_connection_error(c, 0, &addinfo);
4370 ZOOM_API(const char *)
4371 ZOOM_connection_diagset(ZOOM_connection c)
4373 const char *diagset;
4374 ZOOM_connection_error_x(c, 0, 0, &diagset);
4378 ZOOM_API(const char *)
4379 ZOOM_diag_str(int error)
4383 case ZOOM_ERROR_NONE:
4385 case ZOOM_ERROR_CONNECT:
4386 return "Connect failed";
4387 case ZOOM_ERROR_MEMORY:
4388 return "Out of memory";
4389 case ZOOM_ERROR_ENCODE:
4390 return "Encoding failed";
4391 case ZOOM_ERROR_DECODE:
4392 return "Decoding failed";
4393 case ZOOM_ERROR_CONNECTION_LOST:
4394 return "Connection lost";
4395 case ZOOM_ERROR_INIT:
4396 return "Init rejected";
4397 case ZOOM_ERROR_INTERNAL:
4398 return "Internal failure";
4399 case ZOOM_ERROR_TIMEOUT:
4401 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4402 return "Unsupported protocol";
4403 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4404 return "Unsupported query type";
4405 case ZOOM_ERROR_INVALID_QUERY:
4406 return "Invalid query";
4407 case ZOOM_ERROR_CQL_PARSE:
4408 return "CQL parsing error";
4409 case ZOOM_ERROR_CQL_TRANSFORM:
4410 return "CQL transformation error";
4411 case ZOOM_ERROR_CCL_CONFIG:
4412 return "CCL configuration error";
4413 case ZOOM_ERROR_CCL_PARSE:
4414 return "CCL parsing error";
4416 return diagbib1_str(error);
4421 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4422 const char **addinfo, const char **diagset)
4424 int error = c->error;
4427 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4428 *cp = ZOOM_diag_str(error);
4429 else if (!strcmp(c->diagset, "HTTP"))
4430 *cp = z_HTTP_errmsg(c->error);
4431 else if (!strcmp(c->diagset, "Bib-1"))
4432 *cp = ZOOM_diag_str(error);
4433 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4434 *cp = yaz_diag_srw_str(c->error);
4436 *cp = "Unknown error and diagnostic set";
4439 *addinfo = c->addinfo ? c->addinfo : "";
4441 *diagset = c->diagset ? c->diagset : "";
4446 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4447 const char **addinfo)
4449 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4452 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4454 ZOOM_Event event = 0;
4455 int r = cs_look(c->cs);
4456 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4461 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4462 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4464 ZOOM_connection_put_event(c, event);
4466 else if (r == CS_CONNECT)
4468 int ret = ret = cs_rcvconnect(c->cs);
4469 yaz_log(log_details, "%p ZOOM_connection_do_io "
4470 "cs_rcvconnect returned %d", c, ret);
4473 int mask = ZOOM_SELECT_EXCEPT;
4474 if (c->cs->io_pending & CS_WANT_WRITE)
4475 mask += ZOOM_SELECT_WRITE;
4476 if (c->cs->io_pending & CS_WANT_READ)
4477 mask += ZOOM_SELECT_READ;
4478 ZOOM_connection_set_mask(c, mask);
4479 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4480 ZOOM_connection_put_event(c, event);
4484 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4485 ZOOM_connection_put_event(c, event);
4487 if (c->proto == PROTO_Z3950)
4488 ZOOM_connection_send_init(c);
4491 /* no init request for SRW .. */
4492 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4493 ZOOM_connection_remove_task(c);
4494 ZOOM_connection_set_mask(c, 0);
4495 ZOOM_connection_exec_task(c);
4497 c->state = STATE_ESTABLISHED;
4501 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4507 if (mask & ZOOM_SELECT_EXCEPT)
4509 if (!ZOOM_test_reconnect(c))
4511 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4516 if (mask & ZOOM_SELECT_READ)
4518 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4524 ZOOM_connection_last_event(ZOOM_connection cs)
4527 return ZOOM_EVENT_NONE;
4528 return cs->last_event;
4532 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4534 WRBUF wrbuf = (WRBUF) client_data;
4535 wrbuf_puts(wrbuf, buf);
4539 * Returns an xmalloc()d string containing RPN that corresponds to the
4540 * CQL passed in. On error, sets the Connection object's error state
4541 * and returns a null pointer.
4542 * ### We could cache CQL parser and/or transformer in Connection.
4544 static char *cql2pqf(ZOOM_connection c, const char *cql)
4548 const char *cqlfile;
4549 cql_transform_t trans;
4552 parser = cql_parser_create();
4553 if ((error = cql_parser_string(parser, cql)) != 0) {
4554 cql_parser_destroy(parser);
4555 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4559 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4562 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4564 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4567 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4568 cqlfile, strerror(errno));
4569 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4573 WRBUF wrbuf_result = wrbuf_alloc();
4574 error = cql_transform(trans, cql_parser_result(parser),
4575 cql2pqf_wrbuf_puts, wrbuf_result);
4578 const char *addinfo;
4579 error = cql_transform_error(trans, &addinfo);
4580 sprintf(buf, "%.200s (addinfo=%.200s)",
4581 cql_strerror(error), addinfo);
4582 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4586 result = xstrdup(wrbuf_cstr(wrbuf_result));
4588 cql_transform_close(trans);
4589 wrbuf_destroy(wrbuf_result);
4591 cql_parser_destroy(parser);
4595 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4599 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4600 /* timeout and this connection was waiting */
4601 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4603 ZOOM_connection_put_event(c, event);
4609 ZOOM_connection_process(ZOOM_connection c)
4615 event = ZOOM_connection_get_event(c);
4618 ZOOM_Event_destroy(event);
4621 ZOOM_connection_exec_task(c);
4622 event = ZOOM_connection_get_event(c);
4625 ZOOM_Event_destroy(event);
4632 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4636 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4638 for (i = 0; i<no; i++)
4640 ZOOM_connection c = cs[i];
4642 if (c && ZOOM_connection_process(c))
4648 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4650 if (c->mask && mask)
4651 ZOOM_connection_do_io(c, mask);
4655 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4658 return cs_fileno(c->cs);
4662 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4670 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4677 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4679 return ZOOM_options_get_int(c->options, "timeout", 30);
4685 * indent-tabs-mode: nil
4687 * vim: shiftwidth=4 tabstop=8 expandtab