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 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, "scant 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);
484 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
488 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
491 val = ZOOM_options_get(c->options, "proxy");
494 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
495 c->proxy = xstrdup(val);
500 val = ZOOM_options_get(c->options, "charset");
503 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
504 c->charset = xstrdup(val);
508 val = ZOOM_options_get(c->options, "lang");
511 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
512 c->lang = xstrdup(val);
523 sprintf(hostn, "%.80s:%d", host, portnum);
524 c->host_port = xstrdup(hostn);
527 c->host_port = xstrdup(host);
532 * If the "<scheme>:" part of the host string is preceded by one
533 * or more comma-separated <name>=<value> pairs, these are taken
534 * to be options to be set on the connection object. Among other
535 * applications, this facility can be used to embed authentication
537 * user=admin,password=secret,tcp:localhost:9999
539 char *remainder = c->host_port;
540 char *pcolon = strchr(remainder, ':');
543 while ((pcomma = strchr(remainder, ',')) != 0 &&
544 (pcolon == 0 || pcomma < pcolon)) {
546 if ((pequals = strchr(remainder, '=')) != 0) {
548 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
549 ZOOM_connection_option_set(c, remainder, pequals+1);
551 remainder = pcomma+1;
554 if (remainder != c->host_port) {
556 c->host_port = xstrdup(remainder);
557 /*printf("# reset hp='%s'\n", remainder);*/
561 val = ZOOM_options_get(c->options, "sru");
562 c->sru_mode = get_sru_mode_from_string(val);
564 xfree(c->sru_version);
565 val = ZOOM_options_get(c->options, "sru_version");
566 c->sru_version = xstrdup(val ? val : "1.2");
568 ZOOM_options_set(c->options, "host", c->host_port);
570 xfree(c->cookie_out);
572 val = ZOOM_options_get(c->options, "cookie");
575 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
576 c->cookie_out = xstrdup(val);
581 val = ZOOM_options_get(c->options, "clientIP");
584 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
586 c->client_IP = xstrdup(val);
591 val = ZOOM_options_get(c->options, "group");
593 c->group = xstrdup(val);
597 val = ZOOM_options_get(c->options, "user");
599 c->user = xstrdup(val);
603 val = ZOOM_options_get(c->options, "password");
605 val = ZOOM_options_get(c->options, "pass");
608 c->password = xstrdup(val);
610 c->maximum_record_size =
611 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
612 c->preferred_message_size =
613 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
615 c->async = ZOOM_options_get_bool(c->options, "async", 0);
616 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
618 c->odr_print = odr_createmem(ODR_PRINT);
619 odr_setprint(c->odr_print, yaz_log_file());
623 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
625 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
629 while (ZOOM_event(1, &c))
635 ZOOM_query_create(void)
637 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
639 yaz_log(log_details, "%p ZOOM_query_create", s);
643 s->odr = odr_createmem(ODR_ENCODE);
650 ZOOM_query_destroy(ZOOM_query s)
656 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
657 if (s->refcount == 0)
665 ZOOM_query_prefix(ZOOM_query s, const char *str)
667 s->query_string = odr_strdup(s->odr, str);
668 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
669 s->z_query->which = Z_Query_type_1;
670 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
671 if (!s->z_query->u.type_1)
673 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
677 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
682 ZOOM_query_cql(ZOOM_query s, const char *str)
686 s->query_string = odr_strdup(s->odr, str);
688 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
689 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
690 ext->indirect_reference = 0;
692 ext->which = Z_External_CQL;
693 ext->u.cql = s->query_string;
695 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
696 s->z_query->which = Z_Query_type_104;
697 s->z_query->u.type_104 = ext;
699 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
705 * Translate the CQL string client-side into RPN which is passed to
706 * the server. This is useful for server's that don't themselves
707 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
708 * only as a place to stash diagnostics if compilation fails; if this
709 * information is not needed, a null pointer may be used.
712 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
716 ZOOM_connection freeme = 0;
718 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
720 conn = freeme = ZOOM_connection_create(0);
722 rpn = cql2pqf(conn, str);
724 ZOOM_connection_destroy(freeme);
728 ret = ZOOM_query_prefix(s, rpn);
734 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
735 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
736 * to the server, as the YAZ GFS doesn't know how to handle this.
739 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
740 int *ccl_error, const char **error_string,
744 struct ccl_rpn_node *rpn;
745 CCL_bibset bibset = ccl_qual_mk();
748 ccl_qual_buf(bibset, config);
750 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
753 *error_string = ccl_err_msg(*ccl_error);
758 WRBUF wr = wrbuf_alloc();
761 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
764 ccl_qual_rm(&bibset);
769 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
771 s->sort_spec = yaz_sort_spec(s->odr, criteria);
774 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
778 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
782 static zoom_ret do_write(ZOOM_connection c);
785 ZOOM_connection_destroy(ZOOM_connection c)
790 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
793 for (r = c->resultsets; r; r = r->next)
799 odr_destroy(c->odr_in);
800 odr_destroy(c->odr_out);
803 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
804 odr_destroy(c->odr_print);
806 ZOOM_options_destroy(c->options);
807 ZOOM_connection_remove_tasks(c);
808 ZOOM_connection_remove_events(c);
814 xfree(c->cookie_out);
820 xfree(c->sru_version);
824 void ZOOM_resultset_addref(ZOOM_resultset r)
829 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
834 ZOOM_resultset ZOOM_resultset_create(void)
837 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
841 yaz_log(log_details, "%p ZOOM_resultset_create", r);
844 r->odr = odr_createmem(ODR_ENCODE);
849 for (i = 0; i<RECORD_HASH_SIZE; i++)
850 r->record_hash[i] = 0;
855 r->databaseNames = 0;
856 r->num_databaseNames = 0;
860 ZOOM_API(ZOOM_resultset)
861 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
864 ZOOM_query s = ZOOM_query_create();
866 ZOOM_query_prefix(s, q);
868 r = ZOOM_connection_search(c, s);
869 ZOOM_query_destroy(s);
873 ZOOM_API(ZOOM_resultset)
874 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
876 ZOOM_resultset r = ZOOM_resultset_create();
880 const char *syntax, *elementSetName;
882 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
883 r->r_sort_spec = q->sort_spec;
886 r->options = ZOOM_options_create_with_parent(c->options);
888 start = ZOOM_options_get_int(r->options, "start", 0);
889 count = ZOOM_options_get_int(r->options, "count", 0);
891 /* If "presentChunk" is defined use that; otherwise "step" */
892 const char *cp = ZOOM_options_get(r->options, "presentChunk");
893 r->step = ZOOM_options_get_int(r->options,
894 (cp != 0 ? "presentChunk": "step"), 0);
896 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
897 cp = ZOOM_options_get(r->options, "setname");
899 r->setname = xstrdup(cp);
900 cp = ZOOM_options_get(r->options, "schema");
902 r->schema = xstrdup(cp);
904 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
909 r->next = c->resultsets;
914 if (c->host_port && c->proto == PROTO_HTTP)
918 yaz_log(log_details, "ZOOM_connection_search: no comstack");
919 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
923 yaz_log(log_details, "ZOOM_connection_search: reconnect");
928 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
929 task->u.search.resultset = r;
930 task->u.search.start = start;
931 task->u.search.count = count;
933 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
934 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
935 elementSetName = ZOOM_options_get(r->options, "elementSetName");
936 task->u.search.elementSetName = elementSetName
937 ? xstrdup(elementSetName) : 0;
939 ZOOM_resultset_addref(r);
945 while (ZOOM_event(1, &c))
952 ZOOM_resultset_sort(ZOOM_resultset r,
953 const char *sort_type, const char *sort_spec)
955 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
959 ZOOM_resultset_sort1(ZOOM_resultset r,
960 const char *sort_type, const char *sort_spec)
962 ZOOM_connection c = r->connection;
966 newq = ZOOM_query_create();
967 if (ZOOM_query_sortby(newq, sort_spec) < 0)
970 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
971 r, r, sort_type, sort_spec);
975 if (c->host_port && c->proto == PROTO_HTTP)
979 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
980 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
984 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
990 ZOOM_resultset_cache_reset(r);
991 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
992 task->u.sort.resultset = r;
993 task->u.sort.q = newq;
995 ZOOM_resultset_addref(r);
999 while (ZOOM_event(1, &c))
1007 ZOOM_resultset_cache_reset(ZOOM_resultset r)
1010 for (i = 0; i<RECORD_HASH_SIZE; i++)
1012 ZOOM_record_cache rc;
1013 for (rc = r->record_hash[i]; rc; rc = rc->next)
1015 if (rc->rec.wrbuf_marc)
1016 wrbuf_destroy(rc->rec.wrbuf_marc);
1017 if (rc->rec.wrbuf_iconv)
1018 wrbuf_destroy(rc->rec.wrbuf_iconv);
1019 if (rc->rec.wrbuf_opac)
1020 wrbuf_destroy(rc->rec.wrbuf_opac);
1022 r->record_hash[i] = 0;
1027 ZOOM_resultset_destroy(ZOOM_resultset r)
1029 resultset_destroy(r);
1032 static void resultset_destroy(ZOOM_resultset r)
1037 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1039 if (r->refcount == 0)
1041 ZOOM_resultset_cache_reset(r);
1045 /* remove ourselves from the resultsets in connection */
1046 ZOOM_resultset *rp = &r->connection->resultsets;
1049 assert(*rp); /* we must be in this list!! */
1051 { /* OK, we're here - take us out of it */
1058 ZOOM_query_destroy(r->query);
1059 ZOOM_options_destroy(r->options);
1060 odr_destroy(r->odr);
1068 ZOOM_resultset_size(ZOOM_resultset r)
1070 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
1075 static void do_close(ZOOM_connection c)
1080 ZOOM_connection_set_mask(c, 0);
1081 c->state = STATE_IDLE;
1084 static int ZOOM_test_reconnect(ZOOM_connection c)
1088 if (!c->reconnect_ok)
1091 c->reconnect_ok = 0;
1092 c->tasks->running = 0;
1093 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1095 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1096 ZOOM_connection_put_event(c, event);
1101 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1102 int force_sync, int start, int count)
1107 const char *syntax, *elementSetName;
1111 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1112 " count=%d", r, force_sync, start, count);
1117 if (c->host_port && c->proto == PROTO_HTTP)
1121 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1122 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1126 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1128 c->reconnect_ok = 1;
1131 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1132 task->u.retrieve.resultset = r;
1133 task->u.retrieve.start = start;
1134 task->u.retrieve.count = count;
1136 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1137 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1138 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1139 task->u.retrieve.elementSetName = elementSetName
1140 ? xstrdup(elementSetName) : 0;
1142 cp = ZOOM_options_get(r->options, "schema");
1145 if (!r->schema || strcmp(r->schema, cp))
1148 r->schema = xstrdup(cp);
1152 ZOOM_resultset_addref(r);
1154 if (!r->connection->async || force_sync)
1155 while (r->connection && ZOOM_event(1, &r->connection))
1160 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1161 size_t start, size_t count)
1163 int force_present = 0;
1167 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1168 r, r, (long) start, (long) count);
1171 ZOOM_resultset_retrieve(r, force_present, start, count);
1175 for (i = 0; i< count; i++)
1176 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1180 static void get_cert(ZOOM_connection c)
1185 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1187 ZOOM_connection_option_setl(c, "sslPeerCert",
1188 cert_buf, cert_len);
1193 static zoom_ret do_connect(ZOOM_connection c)
1196 const char *effective_host;
1199 effective_host = c->proxy;
1201 effective_host = c->host_port;
1203 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1207 c->cs = cs_create_host(effective_host, 0, &add);
1209 if (c->cs && c->cs->protocol == PROTO_HTTP)
1212 const char *path = 0;
1214 c->proto = PROTO_HTTP;
1215 cs_get_host_args(c->host_port, &path);
1217 c->path = (char*) xmalloc(strlen(path)+2);
1219 strcpy(c->path+1, path);
1221 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1223 return zoom_complete;
1228 int ret = cs_connect(c->cs, add);
1231 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1232 ZOOM_connection_put_event(c, event);
1234 if (c->proto == PROTO_Z3950)
1235 ZOOM_connection_send_init(c);
1238 /* no init request for SRW .. */
1239 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1240 ZOOM_connection_remove_task(c);
1241 ZOOM_connection_set_mask(c, 0);
1242 ZOOM_connection_exec_task(c);
1244 c->state = STATE_ESTABLISHED;
1245 return zoom_pending;
1249 int mask = ZOOM_SELECT_EXCEPT;
1250 if (c->cs->io_pending & CS_WANT_WRITE)
1251 mask += ZOOM_SELECT_WRITE;
1252 if (c->cs->io_pending & CS_WANT_READ)
1253 mask += ZOOM_SELECT_READ;
1254 ZOOM_connection_set_mask(c, mask);
1255 c->state = STATE_CONNECTING;
1256 return zoom_pending;
1259 c->state = STATE_IDLE;
1260 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1261 return zoom_complete;
1264 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1267 for (i = 0; i<200; i++)
1271 Z_OtherInformation **oi;
1276 sprintf(buf, "otherInfo%d", i);
1277 val = ZOOM_options_get(c->options, buf);
1280 cp = strchr(val, ':');
1284 if (len >= sizeof(buf))
1285 len = sizeof(buf)-1;
1286 memcpy(buf, val, len);
1289 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1294 yaz_oi_APDU(a, &oi);
1295 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1299 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1304 Z_OtherInformation **oi;
1305 yaz_oi_APDU(a, &oi);
1306 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1311 Z_OtherInformation **oi;
1312 yaz_oi_APDU(a, &oi);
1313 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1316 otherInfo_attach(c, a, out);
1317 if (!z_APDU(out, &a, 0, 0))
1319 FILE *outf = fopen("/tmp/apdu.txt", "a");
1322 ODR odr_pr = odr_createmem(ODR_PRINT);
1323 fprintf(outf, "a=%p\n", a);
1324 odr_setprint(odr_pr, outf);
1325 z_APDU(odr_pr, &a, 0, 0);
1326 odr_destroy(odr_pr);
1328 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1329 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1334 z_APDU(c->odr_print, &a, 0, 0);
1335 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1339 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1343 if (encode_APDU(c, a, c->odr_out))
1344 return zoom_complete;
1345 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1346 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1347 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1348 ZOOM_connection_put_event(c, event);
1349 odr_reset(c->odr_out);
1353 /* returns 1 if PDU was sent OK (still pending )
1354 0 if PDU was not sent OK (nothing to wait for)
1357 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1359 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1360 Z_InitRequest *ireq = apdu->u.initRequest;
1361 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1362 odr_malloc(c->odr_out, sizeof(*auth));
1365 ODR_MASK_SET(ireq->options, Z_Options_search);
1366 ODR_MASK_SET(ireq->options, Z_Options_present);
1367 ODR_MASK_SET(ireq->options, Z_Options_scan);
1368 ODR_MASK_SET(ireq->options, Z_Options_sort);
1369 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1370 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1372 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1373 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1374 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1376 /* Index Data's Z39.50 Implementor Id is 81 */
1377 ireq->implementationId =
1378 odr_prepend(c->odr_out,
1379 ZOOM_options_get(c->options, "implementationId"),
1380 odr_prepend(c->odr_out, "81", ireq->implementationId));
1382 ireq->implementationName =
1383 odr_prepend(c->odr_out,
1384 ZOOM_options_get(c->options, "implementationName"),
1385 odr_prepend(c->odr_out, "ZOOM-C",
1386 ireq->implementationName));
1388 version = odr_strdup(c->odr_out, "$Revision: 1.154 $");
1389 if (strlen(version) > 10) /* check for unexpanded CVS strings */
1390 version[strlen(version)-2] = '\0';
1391 ireq->implementationVersion =
1392 odr_prepend(c->odr_out,
1393 ZOOM_options_get(c->options, "implementationVersion"),
1394 odr_prepend(c->odr_out, &version[11],
1395 ireq->implementationVersion));
1397 *ireq->maximumRecordSize = c->maximum_record_size;
1398 *ireq->preferredMessageSize = c->preferred_message_size;
1400 if (c->group || c->password)
1402 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1403 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1404 pass->userId = odr_strdup_null(c->odr_out, c->user);
1405 pass->password = odr_strdup_null(c->odr_out, c->password);
1406 auth->which = Z_IdAuthentication_idPass;
1407 auth->u.idPass = pass;
1408 ireq->idAuthentication = auth;
1412 auth->which = Z_IdAuthentication_open;
1413 auth->u.open = odr_strdup(c->odr_out, c->user);
1414 ireq->idAuthentication = auth;
1418 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1419 yaz_oid_userinfo_proxy, 1, c->host_port);
1421 if (c->charset || c->lang)
1423 Z_OtherInformation **oi;
1424 Z_OtherInformationUnit *oi_unit;
1426 yaz_oi_APDU(apdu, &oi);
1428 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1430 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1431 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1432 oi_unit->information.externallyDefinedInfo =
1433 yaz_set_proposal_charneg_list(c->odr_out, " ",
1434 c->charset, c->lang, 1);
1438 return send_APDU(c, apdu);
1442 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1447 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
1449 if (c->sru_mode == zoom_sru_get)
1451 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1453 else if (c->sru_mode == zoom_sru_post)
1455 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1457 else if (c->sru_mode == zoom_sru_soap)
1459 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1461 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1462 return zoom_complete;
1464 z_GDU(c->odr_print, &gdu, 0, 0);
1465 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1467 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1468 ZOOM_connection_put_event(c, event);
1469 odr_reset(c->odr_out);
1475 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1477 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1478 sr->username = c->user;
1479 sr->password = c->password;
1485 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1489 ZOOM_resultset resultset = 0;
1491 const char *option_val = 0;
1493 if (c->error) /* don't continue on error */
1494 return zoom_complete;
1496 switch(c->tasks->which)
1498 case ZOOM_TASK_SEARCH:
1499 resultset = c->tasks->u.search.resultset;
1500 if (!resultset->setname)
1501 resultset->setname = xstrdup("default");
1502 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1503 start = &c->tasks->u.search.start;
1504 count = &c->tasks->u.search.count;
1506 case ZOOM_TASK_RETRIEVE:
1507 resultset = c->tasks->u.retrieve.resultset;
1509 start = &c->tasks->u.retrieve.start;
1510 count = &c->tasks->u.retrieve.count;
1512 if (*start >= resultset->size)
1513 return zoom_complete;
1514 if (*start + *count > resultset->size)
1515 *count = resultset->size - *start;
1517 for (i = 0; i < *count; i++)
1520 record_cache_lookup(resultset, i + *start,
1521 c->tasks->u.retrieve.syntax,
1522 c->tasks->u.retrieve.elementSetName);
1527 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1528 ZOOM_connection_put_event(c, event);
1535 return zoom_complete;
1538 return zoom_complete;
1540 assert(resultset->query);
1542 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1543 if (resultset->query->z_query->which == Z_Query_type_104
1544 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1546 sr->u.request->query_type = Z_SRW_query_type_cql;
1547 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1549 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1550 resultset->query->z_query->u.type_1)
1552 sr->u.request->query_type = Z_SRW_query_type_pqf;
1553 sr->u.request->query.pqf = resultset->query->query_string;
1557 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1558 return zoom_complete;
1560 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1561 sr->u.request->maximumRecords = odr_intdup(
1562 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
1563 resultset->step : *count);
1564 sr->u.request->recordSchema = resultset->schema;
1566 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1568 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1570 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1571 yaz_encode_sru_extra(sr, c->odr_out, option_val);
1572 return send_srw(c, sr);
1575 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1577 return zoom_complete;
1581 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1584 int lslb, ssub, mspn;
1586 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1587 Z_SearchRequest *search_req = apdu->u.searchRequest;
1588 const char *elementSetName;
1589 const char *smallSetElementSetName;
1590 const char *mediumSetElementSetName;
1593 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1595 r = c->tasks->u.search.resultset;
1597 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1600 ZOOM_options_get(r->options, "elementSetName");
1601 smallSetElementSetName =
1602 ZOOM_options_get(r->options, "smallSetElementSetName");
1603 mediumSetElementSetName =
1604 ZOOM_options_get(r->options, "mediumSetElementSetName");
1606 if (!smallSetElementSetName)
1607 smallSetElementSetName = elementSetName;
1609 if (!mediumSetElementSetName)
1610 mediumSetElementSetName = elementSetName;
1615 /* prepare query for the search request */
1616 search_req->query = r->query->z_query;
1617 if (!search_req->query)
1619 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1620 return zoom_complete;
1622 if (r->query->z_query->which == Z_Query_type_1 ||
1623 r->query->z_query->which == Z_Query_type_101)
1625 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1628 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1631 search_req->query = yaz_copy_Z_Query(search_req->query,
1634 yaz_query_charset_convert_rpnquery(search_req->query->u.type_1,
1636 yaz_iconv_close(cd);
1640 search_req->databaseNames = r->databaseNames;
1641 search_req->num_databaseNames = r->num_databaseNames;
1643 /* get syntax (no need to provide unless piggyback is in effect) */
1644 syntax = c->tasks->u.search.syntax;
1646 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1647 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1648 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1649 if (lslb != -1 && ssub != -1 && mspn != -1)
1651 /* So're a Z39.50 expert? Let's hope you don't do sort */
1652 *search_req->largeSetLowerBound = lslb;
1653 *search_req->smallSetUpperBound = ssub;
1654 *search_req->mediumSetPresentNumber = mspn;
1656 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1657 && r->piggyback && !r->r_sort_spec && !r->schema)
1659 /* Regular piggyback - do it unless we're going to do sort */
1660 *search_req->largeSetLowerBound = 2000000000;
1661 *search_req->smallSetUpperBound = 1;
1662 *search_req->mediumSetPresentNumber =
1663 r->step>0 ? r->step : c->tasks->u.search.count;
1667 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1668 smallSetElementSetName = 0;
1669 mediumSetElementSetName = 0;
1672 if (smallSetElementSetName && *smallSetElementSetName)
1674 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1675 odr_malloc(c->odr_out, sizeof(*esn));
1677 esn->which = Z_ElementSetNames_generic;
1678 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1679 search_req->smallSetElementSetNames = esn;
1681 if (mediumSetElementSetName && *mediumSetElementSetName)
1683 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1684 odr_malloc(c->odr_out, sizeof(*esn));
1686 esn->which = Z_ElementSetNames_generic;
1687 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1688 search_req->mediumSetElementSetNames = esn;
1691 search_req->preferredRecordSyntax =
1692 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1696 if (c->support_named_resultsets)
1700 /* find the lowest unused ordinal so that we re-use
1701 result sets on the server. */
1702 for (ord = 1; ; ord++)
1705 sprintf(setname, "%d", ord);
1706 for (rp = c->resultsets; rp; rp = rp->next)
1707 if (rp->setname && !strcmp(rp->setname, setname))
1712 r->setname = xstrdup(setname);
1713 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1714 "set %s", c, r->setname);
1718 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1720 r->setname = xstrdup("default");
1722 ZOOM_options_set(r->options, "setname", r->setname);
1724 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1725 return send_APDU(c, apdu);
1728 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1730 char oid_name_buf[OID_STR_MAX];
1731 const char *oid_name;
1734 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1737 case Z_DefaultDiagFormat_v2Addinfo:
1738 addinfo = r->u.v2Addinfo;
1740 case Z_DefaultDiagFormat_v3Addinfo:
1741 addinfo = r->u.v3Addinfo;
1746 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1749 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1751 if (p->which != Z_DiagRec_defaultFormat)
1752 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1754 response_default_diag(c, p->u.defaultFormat);
1757 ZOOM_API(ZOOM_record)
1758 ZOOM_record_clone(ZOOM_record srec)
1765 odr_enc = odr_createmem(ODR_ENCODE);
1766 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1768 buf = odr_getbuf(odr_enc, &size, 0);
1770 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1771 nrec->odr = odr_createmem(ODR_DECODE);
1772 nrec->wrbuf_marc = 0;
1773 nrec->wrbuf_iconv = 0;
1774 nrec->wrbuf_opac = 0;
1775 odr_setbuf(nrec->odr, buf, size, 0);
1776 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1778 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1779 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1780 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1781 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1782 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1783 odr_destroy(odr_enc);
1787 ZOOM_API(ZOOM_record)
1788 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1790 const char *syntax =
1791 ZOOM_options_get(s->options, "preferredRecordSyntax");
1792 const char *elementSetName =
1793 ZOOM_options_get(s->options, "elementSetName");
1795 return record_cache_lookup(s, pos, syntax, elementSetName);
1798 ZOOM_API(ZOOM_record)
1799 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1801 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1806 * MIKE: I think force_sync should always be zero, but I don't
1807 * want to make this change until I get the go-ahead from
1808 * Adam, in case something depends on the old synchronous
1812 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1813 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1814 rec = ZOOM_resultset_record_immediate(r, pos);
1820 ZOOM_record_destroy(ZOOM_record rec)
1824 if (rec->wrbuf_marc)
1825 wrbuf_destroy(rec->wrbuf_marc);
1826 if (rec->wrbuf_iconv)
1827 wrbuf_destroy(rec->wrbuf_iconv);
1828 if (rec->wrbuf_opac)
1829 wrbuf_destroy(rec->wrbuf_opac);
1830 odr_destroy(rec->odr);
1834 static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
1836 const char *buf, int sz,
1837 const char *record_charset)
1842 yaz_marc_t mt = yaz_marc_create();
1845 strcpy(to, "UTF-8");
1846 if (record_charset && *record_charset)
1848 /* Use "from,to" or just "from" */
1849 const char *cp = strchr(record_charset, ',');
1850 int clen = strlen(record_charset);
1853 strncpy( to, cp+1, sizeof(to)-1);
1854 to[sizeof(to)-1] = '\0';
1855 clen = cp - record_charset;
1857 if (clen > sizeof(from)-1)
1858 clen = sizeof(from)-1;
1861 strncpy(from, record_charset, clen);
1867 cd = yaz_iconv_open(to, from);
1868 yaz_marc_iconv(mt, cd);
1871 yaz_marc_xml(mt, marc_type);
1872 if (!rec->wrbuf_marc)
1873 rec->wrbuf_marc = wrbuf_alloc();
1874 wrbuf_rewind(rec->wrbuf_marc);
1875 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
1877 yaz_marc_destroy(mt);
1879 yaz_iconv_close(cd);
1881 *len = wrbuf_len(rec->wrbuf_marc);
1882 return wrbuf_cstr(rec->wrbuf_marc);
1884 yaz_marc_destroy(mt);
1886 yaz_iconv_close(cd);
1890 static const char *record_iconv_return(ZOOM_record rec, int *len,
1891 const char *buf, int sz,
1892 const char *record_charset)
1899 strcpy(to, "UTF-8");
1901 if (record_charset && *record_charset)
1903 /* Use "from,to" or just "from" */
1904 const char *cp = strchr(record_charset, ',');
1905 int clen = strlen(record_charset);
1908 strncpy( to, cp+1, sizeof(to)-1);
1909 to[sizeof(to)-1] = '\0';
1910 clen = cp - record_charset;
1912 if (clen > sizeof(from)-1)
1913 clen = sizeof(from)-1;
1916 strncpy(from, record_charset, clen);
1920 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1922 if (!rec->wrbuf_iconv)
1923 rec->wrbuf_iconv = wrbuf_alloc();
1925 wrbuf_rewind(rec->wrbuf_iconv);
1927 wrbuf_iconv_write(rec->wrbuf_iconv, cd, buf, sz);
1928 wrbuf_iconv_reset(rec->wrbuf_iconv, cd);
1930 buf = wrbuf_cstr(rec->wrbuf_iconv);
1931 sz = wrbuf_len(rec->wrbuf_iconv);
1932 yaz_iconv_close(cd);
1941 ZOOM_record_error(ZOOM_record rec, const char **cp,
1942 const char **addinfo, const char **diagset)
1944 Z_NamePlusRecord *npr;
1953 *cp = rec->diag_message;
1955 *addinfo = rec->diag_details;
1957 *diagset = rec->diag_set;
1958 return uri_to_code(rec->diag_uri);
1960 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
1962 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
1963 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
1964 const char *add = 0;
1966 if (diag_rec->which == Z_DiagRec_defaultFormat)
1968 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
1971 error = *ddf->condition;
1974 case Z_DefaultDiagFormat_v2Addinfo:
1975 add = ddf->u.v2Addinfo;
1977 case Z_DefaultDiagFormat_v3Addinfo:
1978 add = ddf->u.v3Addinfo;
1983 yaz_oid_to_string(yaz_oid_std(),
1984 ddf->diagnosticSetId, &oclass);
1992 *addinfo = add ? add : "";
1994 *cp = diagbib1_str(error);
2000 ZOOM_API(const char *)
2001 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
2008 Z_NamePlusRecord *npr;
2011 *len = 0; /* default return */
2020 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
2022 if (cp[i] == ';' || cp[i] == ' ')
2028 while (type_spec[i] == ';')
2031 while (type_spec[i] == ' ')
2033 if (!strncmp(type_spec+i, "charset=", 8))
2036 i = i + 8; /* skip charset= */
2037 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
2039 if (type_spec[i] == ';' || type_spec[i] == ' ')
2045 else if (!strncmp(type_spec+i, "xpath=", 6))
2049 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
2053 while (type_spec[i] == ' ')
2056 if (!strcmp(type, "database"))
2059 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2060 return npr->databaseName;
2062 else if (!strcmp(type, "schema"))
2065 *len = rec->schema ? strlen(rec->schema) : 0;
2068 else if (!strcmp(type, "syntax"))
2070 const char *desc = 0;
2071 if (npr->which == Z_NamePlusRecord_databaseRecord)
2073 Z_External *r = (Z_External *) npr->u.databaseRecord;
2074 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2079 *len = strlen(desc);
2082 if (npr->which != Z_NamePlusRecord_databaseRecord)
2085 /* from now on - we have a database record .. */
2086 if (!strcmp(type, "render"))
2088 Z_External *r = (Z_External *) npr->u.databaseRecord;
2089 const Odr_oid *oid = r->direct_reference;
2091 /* render bibliographic record .. */
2092 if (r->which == Z_External_OPAC)
2094 r = r->u.opac->bibliographicRecord;
2097 oid = r->direct_reference;
2099 if (r->which == Z_External_sutrs)
2100 return record_iconv_return(rec, len,
2101 (char*) r->u.sutrs->buf,
2104 else if (r->which == Z_External_octet)
2106 if (yaz_oid_is_iso2709(oid))
2108 const char *ret_buf = marc_iconv_return(
2109 rec, YAZ_MARC_LINE, len,
2110 (const char *) r->u.octet_aligned->buf,
2111 r->u.octet_aligned->len,
2116 return record_iconv_return(rec, len,
2117 (const char *) r->u.octet_aligned->buf,
2118 r->u.octet_aligned->len,
2121 else if (r->which == Z_External_grs1)
2123 if (!rec->wrbuf_marc)
2124 rec->wrbuf_marc = wrbuf_alloc();
2125 wrbuf_rewind(rec->wrbuf_marc);
2126 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
2127 return record_iconv_return(rec, len,
2128 wrbuf_buf(rec->wrbuf_marc),
2129 wrbuf_len(rec->wrbuf_marc),
2134 else if (!strcmp(type, "xml"))
2136 Z_External *r = (Z_External *) npr->u.databaseRecord;
2137 const Odr_oid *oid = r->direct_reference;
2139 /* render bibliographic record .. */
2140 if (r->which == Z_External_OPAC)
2142 r = r->u.opac->bibliographicRecord;
2145 oid = r->direct_reference;
2148 if (r->which == Z_External_sutrs)
2149 return record_iconv_return(rec, len,
2150 (const char *) r->u.sutrs->buf,
2153 else if (r->which == Z_External_octet)
2155 int marc_decode_type = YAZ_MARC_MARCXML;
2156 if (yaz_oid_is_iso2709(oid))
2158 const char *ret_buf = marc_iconv_return(
2159 rec, marc_decode_type, len,
2160 (const char *) r->u.octet_aligned->buf,
2161 r->u.octet_aligned->len,
2166 return record_iconv_return(rec, len,
2167 (const char *) r->u.octet_aligned->buf,
2168 r->u.octet_aligned->len,
2171 else if (r->which == Z_External_grs1)
2178 else if (!strcmp(type, "raw"))
2180 Z_External *r = (Z_External *) npr->u.databaseRecord;
2182 if (r->which == Z_External_sutrs)
2184 if (len) *len = r->u.sutrs->len;
2185 return (const char *) r->u.sutrs->buf;
2187 else if (r->which == Z_External_octet)
2189 if (len) *len = r->u.octet_aligned->len;
2190 return (const char *) r->u.octet_aligned->buf;
2192 else /* grs-1, explain, OPAC, ... */
2195 return (const char *) npr->u.databaseRecord;
2199 else if (!strcmp (type, "ext"))
2202 return (const char *) npr->u.databaseRecord;
2204 else if (!strcmp (type, "opac"))
2207 Z_External *r = (Z_External *) npr->u.databaseRecord;
2208 if (r->which == Z_External_OPAC)
2210 if (!rec->wrbuf_opac)
2211 rec->wrbuf_opac = wrbuf_alloc();
2212 wrbuf_rewind(rec->wrbuf_opac);
2213 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
2214 return record_iconv_return(rec, len,
2215 wrbuf_buf(rec->wrbuf_opac),
2216 wrbuf_len(rec->wrbuf_opac),
2223 static int strcmp_null(const char *v1, const char *v2)
2229 return strcmp(v1, v2);
2232 static size_t record_hash(int pos)
2236 return pos % RECORD_HASH_SIZE;
2239 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2241 const char *syntax, const char *elementSetName,
2243 Z_SRW_diagnostic *diag)
2245 ZOOM_record_cache rc = 0;
2247 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2248 ZOOM_connection_put_event(r->connection, event);
2250 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2253 && strcmp_null(r->schema, rc->schema) == 0
2254 && strcmp_null(elementSetName,rc->elementSetName) == 0
2255 && strcmp_null(syntax, rc->syntax) == 0)
2260 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2262 rc->rec.wrbuf_marc = 0;
2263 rc->rec.wrbuf_iconv = 0;
2264 rc->rec.wrbuf_opac = 0;
2265 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2267 rc->syntax = odr_strdup_null(r->odr, syntax);
2269 rc->schema = odr_strdup_null(r->odr, r->schema);
2272 rc->next = r->record_hash[record_hash(pos)];
2273 r->record_hash[record_hash(pos)] = rc;
2276 rc->rec.schema = odr_strdup_null(r->odr, schema);
2277 rc->rec.diag_set = 0;
2278 rc->rec.diag_uri = 0;
2279 rc->rec.diag_message = 0;
2280 rc->rec.diag_details = 0;
2286 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2287 if ((cp = strrchr(rc->rec.diag_set, '/')))
2289 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2291 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2292 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2296 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2298 const char *elementSetName)
2300 ZOOM_record_cache rc;
2302 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2306 if (strcmp_null(r->schema, rc->schema))
2308 if (strcmp_null(elementSetName,rc->elementSetName))
2310 if (strcmp_null(syntax, rc->syntax))
2318 static void handle_records(ZOOM_connection c, Z_Records *sr,
2321 ZOOM_resultset resultset;
2323 const char *syntax = 0, *elementSetName = 0;
2327 switch (c->tasks->which)
2329 case ZOOM_TASK_SEARCH:
2330 resultset = c->tasks->u.search.resultset;
2331 start = &c->tasks->u.search.start;
2332 count = &c->tasks->u.search.count;
2333 syntax = c->tasks->u.search.syntax;
2334 elementSetName = c->tasks->u.search.elementSetName;
2336 case ZOOM_TASK_RETRIEVE:
2337 resultset = c->tasks->u.retrieve.resultset;
2338 start = &c->tasks->u.retrieve.start;
2339 count = &c->tasks->u.retrieve.count;
2340 syntax = c->tasks->u.retrieve.syntax;
2341 elementSetName = c->tasks->u.retrieve.elementSetName;
2346 if (sr && sr->which == Z_Records_NSD)
2347 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2348 else if (sr && sr->which == Z_Records_multipleNSD)
2350 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2351 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2353 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2357 if (*count + *start > resultset->size)
2358 *count = resultset->size - *start;
2361 if (sr && sr->which == Z_Records_DBOSD)
2364 NMEM nmem = odr_extract_mem(c->odr_in);
2365 Z_NamePlusRecordList *p =
2366 sr->u.databaseOrSurDiagnostics;
2367 for (i = 0; i<p->num_records; i++)
2369 record_cache_add(resultset, p->records[i], i + *start,
2370 syntax, elementSetName,
2377 yaz_log(log_details,
2378 "handle_records resultset=%p start=%d count=%d",
2379 resultset, *start, *count);
2381 /* transfer our response to search_nmem .. we need it later */
2382 nmem_transfer(odr_getmem(resultset->odr), nmem);
2384 if (present_phase && p->num_records == 0)
2386 /* present response and we didn't get any records! */
2387 Z_NamePlusRecord *myrec =
2388 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2389 record_cache_add(resultset, myrec, *start,
2390 syntax, elementSetName, 0, 0);
2393 else if (present_phase)
2395 /* present response and we didn't get any records! */
2396 Z_NamePlusRecord *myrec =
2397 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2398 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2404 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2406 handle_records(c, pr->records, 1);
2409 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2412 switch (term->which)
2414 case Z_Term_general:
2415 ZOOM_options_setl(opt, name,
2416 (const char *)(term->u.general->buf),
2417 term->u.general->len);
2419 case Z_Term_characterString:
2420 ZOOM_options_set(opt, name, term->u.characterString);
2422 case Z_Term_numeric:
2423 ZOOM_options_set_int(opt, name, *term->u.numeric);
2428 static void handle_queryExpression(ZOOM_options opt, const char *name,
2429 Z_QueryExpression *exp)
2435 case Z_QueryExpression_term:
2436 if (exp->u.term && exp->u.term->queryTerm)
2438 sprintf(opt_name, "%s.term", name);
2439 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2442 case Z_QueryExpression_query:
2447 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2448 Z_OtherInformation *o)
2451 for (i = 0; o && i < o->num_elements; i++)
2453 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2455 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2457 if (ext->which == Z_External_searchResult1)
2460 Z_SearchInfoReport *sr = ext->u.searchResult1;
2463 ZOOM_options_set_int(
2464 resultset->options, "searchresult.size", sr->num);
2466 for (j = 0; j < sr->num; j++)
2468 Z_SearchInfoReport_s *ent =
2469 ext->u.searchResult1->elements[j];
2472 sprintf(pref, "searchresult.%d", j);
2474 if (ent->subqueryId)
2477 sprintf(opt_name, "%s.id", pref);
2478 ZOOM_options_set(resultset->options, opt_name,
2481 if (ent->subqueryExpression)
2484 sprintf(opt_name, "%s.subquery", pref);
2485 handle_queryExpression(resultset->options, opt_name,
2486 ent->subqueryExpression);
2488 if (ent->subqueryInterpretation)
2491 sprintf(opt_name, "%s.interpretation", pref);
2492 handle_queryExpression(resultset->options, opt_name,
2493 ent->subqueryInterpretation);
2495 if (ent->subqueryRecommendation)
2498 sprintf(opt_name, "%s.recommendation", pref);
2499 handle_queryExpression(resultset->options, opt_name,
2500 ent->subqueryRecommendation);
2502 if (ent->subqueryCount)
2505 sprintf(opt_name, "%s.count", pref);
2506 ZOOM_options_set_int(resultset->options, opt_name,
2507 *ent->subqueryCount);
2515 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2517 ZOOM_resultset resultset;
2520 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2523 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2524 ZOOM_connection_put_event(c, event);
2526 resultset = c->tasks->u.search.resultset;
2528 if (sr->resultSetStatus)
2530 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2531 *sr->resultSetStatus);
2533 if (sr->presentStatus)
2535 ZOOM_options_set_int(resultset->options, "presentStatus",
2536 *sr->presentStatus);
2538 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2540 resultset->size = *sr->resultCount;
2541 handle_records(c, sr->records, 0);
2544 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2546 if (res->diagnostics && res->num_diagnostics > 0)
2547 response_diag(c, res->diagnostics[0]);
2550 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2552 NMEM nmem = odr_extract_mem(c->odr_in);
2555 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2557 scan = c->tasks->u.scan.scan;
2559 if (res->entries && res->entries->nonsurrogateDiagnostics)
2560 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2561 scan->scan_response = res;
2562 scan->srw_scan_response = 0;
2563 nmem_transfer(odr_getmem(scan->odr), nmem);
2565 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2566 if (res->positionOfTerm)
2567 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2568 if (res->scanStatus)
2569 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2570 if (res->numberOfEntriesReturned)
2571 ZOOM_options_set_int(scan->options, "number",
2572 *res->numberOfEntriesReturned);
2577 static zoom_ret send_sort(ZOOM_connection c,
2578 ZOOM_resultset resultset)
2581 resultset->r_sort_spec = 0;
2582 if (resultset->r_sort_spec)
2584 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2585 Z_SortRequest *req = apdu->u.sortRequest;
2587 req->num_inputResultSetNames = 1;
2588 req->inputResultSetNames = (Z_InternationalString **)
2589 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2590 req->inputResultSetNames[0] =
2591 odr_strdup(c->odr_out, resultset->setname);
2592 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2593 req->sortSequence = resultset->r_sort_spec;
2594 resultset->r_sort_spec = 0;
2595 return send_APDU(c, apdu);
2597 return zoom_complete;
2600 static zoom_ret send_present(ZOOM_connection c)
2603 Z_PresentRequest *req = 0;
2605 const char *syntax = 0;
2606 const char *elementSetName = 0;
2607 ZOOM_resultset resultset;
2612 yaz_log(log_details, "%p send_present no tasks", c);
2613 return zoom_complete;
2616 switch (c->tasks->which)
2618 case ZOOM_TASK_SEARCH:
2619 resultset = c->tasks->u.search.resultset;
2620 start = &c->tasks->u.search.start;
2621 count = &c->tasks->u.search.count;
2622 syntax = c->tasks->u.search.syntax;
2623 elementSetName = c->tasks->u.search.elementSetName;
2625 case ZOOM_TASK_RETRIEVE:
2626 resultset = c->tasks->u.retrieve.resultset;
2627 start = &c->tasks->u.retrieve.start;
2628 count = &c->tasks->u.retrieve.count;
2629 syntax = c->tasks->u.retrieve.syntax;
2630 elementSetName = c->tasks->u.retrieve.elementSetName;
2633 return zoom_complete;
2635 yaz_log(log_details, "%p send_present start=%d count=%d",
2638 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2640 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2643 if (c->error) /* don't continue on error */
2644 return zoom_complete;
2645 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2646 resultset, *start, *count);
2648 for (i = 0; i < *count; i++)
2651 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2656 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2657 ZOOM_connection_put_event(c, event);
2665 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2666 return zoom_complete;
2669 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2670 req = apdu->u.presentRequest;
2673 yaz_log(log_details, "%p send_present skip=%d", c, i);
2675 *req->resultSetStartPoint = *start + 1;
2677 if (resultset->step > 0 && resultset->step < *count)
2678 *req->numberOfRecordsRequested = resultset->step;
2680 *req->numberOfRecordsRequested = *count;
2682 if (*req->numberOfRecordsRequested + *start > resultset->size)
2683 *req->numberOfRecordsRequested = resultset->size - *start;
2684 assert(*req->numberOfRecordsRequested > 0);
2686 if (syntax && *syntax)
2687 req->preferredRecordSyntax =
2688 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2690 if (resultset->schema && *resultset->schema)
2692 Z_RecordComposition *compo = (Z_RecordComposition *)
2693 odr_malloc(c->odr_out, sizeof(*compo));
2695 req->recordComposition = compo;
2696 compo->which = Z_RecordComp_complex;
2697 compo->u.complex = (Z_CompSpec *)
2698 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2699 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2700 odr_malloc(c->odr_out, sizeof(bool_t));
2701 *compo->u.complex->selectAlternativeSyntax = 0;
2703 compo->u.complex->generic = (Z_Specification *)
2704 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2706 compo->u.complex->generic->which = Z_Schema_oid;
2707 compo->u.complex->generic->schema.oid = (Odr_oid *)
2708 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2710 if (!compo->u.complex->generic->schema.oid)
2712 /* OID wasn't a schema! Try record syntax instead. */
2714 compo->u.complex->generic->schema.oid = (Odr_oid *)
2715 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2717 if (elementSetName && *elementSetName)
2719 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2720 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2721 compo->u.complex->generic->elementSpec->which =
2722 Z_ElementSpec_elementSetName;
2723 compo->u.complex->generic->elementSpec->u.elementSetName =
2724 odr_strdup(c->odr_out, elementSetName);
2727 compo->u.complex->generic->elementSpec = 0;
2728 compo->u.complex->num_dbSpecific = 0;
2729 compo->u.complex->dbSpecific = 0;
2730 compo->u.complex->num_recordSyntax = 0;
2731 compo->u.complex->recordSyntax = 0;
2733 else if (elementSetName && *elementSetName)
2735 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2736 odr_malloc(c->odr_out, sizeof(*esn));
2737 Z_RecordComposition *compo = (Z_RecordComposition *)
2738 odr_malloc(c->odr_out, sizeof(*compo));
2740 esn->which = Z_ElementSetNames_generic;
2741 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2742 compo->which = Z_RecordComp_simple;
2743 compo->u.simple = esn;
2744 req->recordComposition = compo;
2746 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2747 return send_APDU(c, apdu);
2750 ZOOM_API(ZOOM_scanset)
2751 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2754 ZOOM_query q = ZOOM_query_create();
2756 ZOOM_query_prefix(q, start);
2758 s = ZOOM_connection_scan1(c, q);
2759 ZOOM_query_destroy(q);
2764 ZOOM_API(ZOOM_scanset)
2765 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2767 ZOOM_scanset scan = 0;
2771 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2772 scan->connection = c;
2773 scan->odr = odr_createmem(ODR_DECODE);
2774 scan->options = ZOOM_options_create_with_parent(c->options);
2776 scan->scan_response = 0;
2777 scan->srw_scan_response = 0;
2781 scan->databaseNames = set_DatabaseNames(c, c->options,
2782 &scan->num_databaseNames,
2787 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2788 task->u.scan.scan = scan;
2793 while (ZOOM_event(1, &c))
2801 ZOOM_scanset_destroy(ZOOM_scanset scan)
2806 if (scan->refcount == 0)
2808 ZOOM_query_destroy(scan->query);
2810 odr_destroy(scan->odr);
2812 ZOOM_options_destroy(scan->options);
2817 static zoom_ret send_package(ZOOM_connection c)
2821 yaz_log(log_details, "%p send_package", c);
2823 return zoom_complete;
2824 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2826 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2827 ZOOM_connection_put_event(c, event);
2829 c->buf_out = c->tasks->u.package->buf_out;
2830 c->len_out = c->tasks->u.package->len_out;
2835 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2838 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2839 Z_ScanRequest *req = apdu->u.scanRequest;
2841 yaz_log(log_details, "%p send_scan", c);
2843 return zoom_complete;
2844 assert (c->tasks->which == ZOOM_TASK_SCAN);
2845 scan = c->tasks->u.scan.scan;
2847 /* Z39.50 scan can only carry RPN */
2848 if (scan->query->z_query->which == Z_Query_type_1 ||
2849 scan->query->z_query->which == Z_Query_type_101)
2851 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2852 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2855 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2858 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2860 yaz_query_charset_convert_rpnquery(
2861 rpn, c->odr_out, cd);
2862 yaz_iconv_close(cd);
2865 req->attributeSet = rpn->attributeSetId;
2866 if (!req->attributeSet)
2867 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2868 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2869 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2871 req->termListAndStartPoint =
2872 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2876 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2877 return zoom_complete;
2882 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2883 return zoom_complete;
2886 *req->numberOfTermsRequested =
2887 ZOOM_options_get_int(scan->options, "number", 10);
2889 req->preferredPositionInResponse =
2890 odr_intdup(c->odr_out,
2891 ZOOM_options_get_int(scan->options, "position", 1));
2894 odr_intdup(c->odr_out,
2895 ZOOM_options_get_int(scan->options, "stepSize", 0));
2897 req->databaseNames = scan->databaseNames;
2898 req->num_databaseNames = scan->num_databaseNames;
2900 return send_APDU(c, apdu);
2904 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2908 const char *option_val = 0;
2911 return zoom_complete;
2912 assert (c->tasks->which == ZOOM_TASK_SCAN);
2913 scan = c->tasks->u.scan.scan;
2915 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2917 /* SRU scan can only carry CQL and PQF */
2918 if (scan->query->z_query->which == Z_Query_type_104)
2920 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2921 sr->u.scan_request->scanClause.cql = scan->query->query_string;
2923 else if (scan->query->z_query->which == Z_Query_type_1
2924 || scan->query->z_query->which == Z_Query_type_101)
2926 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2927 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2931 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2932 return zoom_complete;
2935 sr->u.scan_request->maximumTerms = odr_intdup(
2936 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2938 sr->u.scan_request->responsePosition = odr_intdup(
2939 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2941 option_val = ZOOM_options_get(scan->options, "extraArgs");
2942 yaz_encode_sru_extra(sr, c->odr_out, option_val);
2943 return send_srw(c, sr);
2946 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2948 return zoom_complete;
2954 ZOOM_scanset_size(ZOOM_scanset scan)
2959 if (scan->scan_response && scan->scan_response->entries)
2960 return scan->scan_response->entries->num_entries;
2961 else if (scan->srw_scan_response)
2962 return scan->srw_scan_response->num_terms;
2966 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2968 const char **value_term, size_t *value_len,
2969 const char **disp_term, size_t *disp_len)
2971 size_t noent = ZOOM_scanset_size(scan);
2980 if (pos >= noent || pos < 0)
2982 if (scan->scan_response)
2984 Z_ScanResponse *res = scan->scan_response;
2985 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2987 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2989 *value_term = (const char *) t->term->u.general->buf;
2990 *value_len = t->term->u.general->len;
2993 *disp_term = t->displayTerm;
2994 *disp_len = strlen(*disp_term);
2996 else if (t->term->which == Z_Term_general)
2998 *disp_term = (const char *) t->term->u.general->buf;
2999 *disp_len = t->term->u.general->len;
3001 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
3004 if (scan->srw_scan_response)
3006 Z_SRW_scanResponse *res = scan->srw_scan_response;
3007 Z_SRW_scanTerm *t = res->terms + pos;
3010 *value_term = t->value;
3011 *value_len = strlen(*value_term);
3014 *disp_term = t->displayTerm;
3016 *disp_term = t->value;
3017 *disp_len = strlen(*disp_term);
3018 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
3023 ZOOM_API(const char *)
3024 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
3027 const char *value_term = 0;
3028 size_t value_len = 0;
3029 const char *disp_term = 0;
3030 size_t disp_len = 0;
3032 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3033 &disp_term, &disp_len);
3039 ZOOM_API(const char *)
3040 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3043 const char *value_term = 0;
3044 size_t value_len = 0;
3045 const char *disp_term = 0;
3046 size_t disp_len = 0;
3048 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3049 &disp_term, &disp_len);
3055 ZOOM_API(const char *)
3056 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3058 return ZOOM_options_get(scan->options, key);
3062 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3065 ZOOM_options_set(scan->options, key, val);
3068 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3071 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3072 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3074 str = ZOOM_options_get(p->options, "package-name");
3076 req->packageName = odr_strdup(p->odr_out, str);
3078 str = ZOOM_options_get(p->options, "user-id");
3080 req->userId = odr_strdup_null(p->odr_out, str);
3082 req->packageType = odr_oiddup(p->odr_out, oid);
3084 str = ZOOM_options_get(p->options, "function");
3087 if (!strcmp (str, "create"))
3088 *req->function = Z_ExtendedServicesRequest_create;
3089 if (!strcmp (str, "delete"))
3090 *req->function = Z_ExtendedServicesRequest_delete;
3091 if (!strcmp (str, "modify"))
3092 *req->function = Z_ExtendedServicesRequest_modify;
3095 str = ZOOM_options_get(p->options, "waitAction");
3098 if (!strcmp (str, "wait"))
3099 *req->waitAction = Z_ExtendedServicesRequest_wait;
3100 if (!strcmp (str, "waitIfPossible"))
3101 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3102 if (!strcmp (str, "dontWait"))
3103 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3104 if (!strcmp (str, "dontReturnPackage"))
3105 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3110 static const char *ill_array_lookup(void *clientData, const char *idx)
3112 ZOOM_package p = (ZOOM_package) clientData;
3113 return ZOOM_options_get(p->options, idx+4);
3116 static Z_External *encode_ill_request(ZOOM_package p)
3118 ODR out = p->odr_out;
3121 struct ill_get_ctl ctl;
3123 ctl.odr = p->odr_out;
3125 ctl.f = ill_array_lookup;
3127 req = ill_get_ILLRequest(&ctl, "ill", 0);
3129 if (!ill_Request(out, &req, 0, 0))
3131 int ill_request_size;
3132 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3133 if (ill_request_buf)
3134 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3139 int illRequest_size = 0;
3140 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3142 r = (Z_External *) odr_malloc(out, sizeof(*r));
3143 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3144 r->indirect_reference = 0;
3146 r->which = Z_External_single;
3148 r->u.single_ASN1_type =
3149 odr_create_Odr_oct(out,
3150 (unsigned char *)illRequest_buf,
3156 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3158 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3162 req->which = Z_IOItemOrder_esRequest;
3163 req->u.esRequest = (Z_IORequest *)
3164 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3166 /* to keep part ... */
3167 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3168 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3169 req->u.esRequest->toKeep->supplDescription = 0;
3170 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3171 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3173 str = ZOOM_options_get(p->options, "contact-name");
3174 req->u.esRequest->toKeep->contact->name =
3175 odr_strdup_null(p->odr_out, str);
3177 str = ZOOM_options_get(p->options, "contact-phone");
3178 req->u.esRequest->toKeep->contact->phone =
3179 odr_strdup_null(p->odr_out, str);
3181 str = ZOOM_options_get(p->options, "contact-email");
3182 req->u.esRequest->toKeep->contact->email =
3183 odr_strdup_null(p->odr_out, str);
3185 req->u.esRequest->toKeep->addlBilling = 0;
3187 /* not to keep part ... */
3188 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3189 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3191 str = ZOOM_options_get(p->options, "itemorder-setname");
3196 req->u.esRequest->notToKeep->resultSetItem = 0;
3199 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3200 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3202 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3203 odr_strdup(p->odr_out, str);
3204 req->u.esRequest->notToKeep->resultSetItem->item =
3205 (int *) odr_malloc(p->odr_out, sizeof(int));
3207 str = ZOOM_options_get(p->options, "itemorder-item");
3208 *req->u.esRequest->notToKeep->resultSetItem->item =
3209 (str ? atoi(str) : 1);
3212 str = ZOOM_options_getl(p->options, "doc", &len);
3215 req->u.esRequest->notToKeep->itemRequest =
3216 z_ext_record_xml(p->odr_out, str, len);
3219 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3224 Z_APDU *create_admin_package(ZOOM_package p, int type,
3225 Z_ESAdminOriginPartToKeep **toKeepP,
3226 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3228 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3231 Z_ESAdminOriginPartToKeep *toKeep;
3232 Z_ESAdminOriginPartNotToKeep *notToKeep;
3233 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3234 const char *first_db = "Default";
3236 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3241 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3243 r->indirect_reference = 0;
3244 r->which = Z_External_ESAdmin;
3246 r->u.adminService = (Z_Admin *)
3247 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3248 r->u.adminService->which = Z_Admin_esRequest;
3249 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3250 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3252 toKeep = r->u.adminService->u.esRequest->toKeep =
3253 (Z_ESAdminOriginPartToKeep *)
3254 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3255 toKeep->which = type;
3256 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3257 toKeep->u.create = odr_nullval();
3258 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3260 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3261 (Z_ESAdminOriginPartNotToKeep *)
3262 odr_malloc(p->odr_out,
3263 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3264 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3265 notToKeep->u.recordsWillFollow = odr_nullval();
3269 *notToKeepP = notToKeep;
3274 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3276 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3277 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3278 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3280 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3288 req->taskSpecificParameters = ext;
3289 ext->direct_reference = req->packageType;
3290 ext->descriptor = 0;
3291 ext->indirect_reference = 0;
3293 ext->which = Z_External_octet;
3294 ext->u.single_ASN1_type =
3295 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3299 static Z_APDU *create_update_package(ZOOM_package p)
3302 const char *first_db = "Default";
3304 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3305 const char *action = ZOOM_options_get(p->options, "action");
3306 int recordIdOpaque_len;
3307 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3308 &recordIdOpaque_len);
3309 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3311 const char *record_buf = ZOOM_options_getl(p->options, "record",
3313 int recordOpaque_len;
3314 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3316 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3317 const char *version = ZOOM_options_get(p->options, "updateVersion");
3319 const char *correlationInfo_note =
3320 ZOOM_options_get(p->options, "correlationInfo.note");
3321 const char *correlationInfo_id =
3322 ZOOM_options_get(p->options, "correlationInfo.id");
3324 Odr_oid *syntax_oid = 0;
3325 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3331 if (!record_buf && !recordOpaque_buf)
3333 record_buf = "void";
3335 syntax_str = "SUTRS";
3340 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3341 CLASS_RECSYN, syntax_str,
3353 package_oid = yaz_oid_extserv_database_update_first_version;
3354 /* old update does not support specialUpdate */
3356 action = "recordInsert";
3360 action = "specialUpdate";
3361 package_oid = yaz_oid_extserv_database_update_second_version;
3365 action = "specialUpdate";
3366 package_oid = yaz_oid_extserv_database_update;
3372 if (!strcmp(action, "recordInsert"))
3373 action_no = Z_IUOriginPartToKeep_recordInsert;
3374 else if (!strcmp(action, "recordReplace"))
3375 action_no = Z_IUOriginPartToKeep_recordReplace;
3376 else if (!strcmp(action, "recordDelete"))
3377 action_no = Z_IUOriginPartToKeep_recordDelete;
3378 else if (!strcmp(action, "elementUpdate"))
3379 action_no = Z_IUOriginPartToKeep_elementUpdate;
3380 else if (!strcmp(action, "specialUpdate"))
3381 action_no = Z_IUOriginPartToKeep_specialUpdate;
3385 apdu = create_es_package(p, package_oid);
3388 Z_IUOriginPartToKeep *toKeep;
3389 Z_IUSuppliedRecords *notToKeep;
3390 Z_External *r = (Z_External *)
3391 odr_malloc(p->odr_out, sizeof(*r));
3392 const char *elementSetName =
3393 ZOOM_options_get(p->options, "elementSetName");
3395 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3397 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3399 r->which = Z_External_update;
3400 r->indirect_reference = 0;
3401 r->u.update = (Z_IUUpdate *)
3402 odr_malloc(p->odr_out, sizeof(*r->u.update));
3404 r->u.update->which = Z_IUUpdate_esRequest;
3405 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3406 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3407 toKeep = r->u.update->u.esRequest->toKeep =
3408 (Z_IUOriginPartToKeep *)
3409 odr_malloc(p->odr_out, sizeof(*toKeep));
3411 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3414 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3416 toKeep->actionQualifier = 0;
3417 toKeep->action = odr_intdup(p->odr_out, action_no);
3419 notToKeep = r->u.update->u.esRequest->notToKeep =
3420 (Z_IUSuppliedRecords *)
3421 odr_malloc(p->odr_out, sizeof(*notToKeep));
3423 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3424 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3425 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3426 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3427 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3430 notToKeep->elements[0]->u.opaque =
3431 odr_create_Odr_oct(p->odr_out,
3432 (const unsigned char *) recordIdOpaque,
3433 recordIdOpaque_len);
3435 else if (recordIdNumber)
3437 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3439 notToKeep->elements[0]->u.number =
3440 odr_intdup(p->odr_out, atoi(recordIdNumber));
3443 notToKeep->elements[0]->u.opaque = 0;
3444 notToKeep->elements[0]->supplementalId = 0;
3445 if (correlationInfo_note || correlationInfo_id)
3447 Z_IUCorrelationInfo *ci;
3448 ci = notToKeep->elements[0]->correlationInfo =
3449 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3450 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3451 ci->id = correlationInfo_id ?
3452 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3455 notToKeep->elements[0]->correlationInfo = 0;
3456 if (recordOpaque_buf)
3458 notToKeep->elements[0]->record =
3459 z_ext_record_oid_any(p->odr_out, syntax_oid,
3460 recordOpaque_buf, recordOpaque_len);
3464 notToKeep->elements[0]->record =
3465 z_ext_record_oid(p->odr_out, syntax_oid,
3466 record_buf, record_len);
3471 ODR print = odr_createmem(ODR_PRINT);
3473 z_APDU(print, &apdu, 0, 0);
3480 ZOOM_package_send(ZOOM_package p, const char *type)
3487 odr_reset(p->odr_out);
3490 if (!strcmp(type, "itemorder"))
3492 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3495 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3497 r->direct_reference =
3498 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3500 r->which = Z_External_itemOrder;
3501 r->indirect_reference = 0;
3502 r->u.itemOrder = encode_item_order(p);
3504 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3507 else if (!strcmp(type, "create")) /* create database */
3509 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3512 else if (!strcmp(type, "drop")) /* drop database */
3514 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3517 else if (!strcmp(type, "commit")) /* commit changes */
3519 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3522 else if (!strcmp(type, "update")) /* update record(s) */
3524 apdu = create_update_package(p);
3526 else if (!strcmp(type, "xmlupdate"))
3528 apdu = create_xmlupdate_package(p);
3532 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3536 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3537 task->u.package = p;
3538 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3539 p->buf_out = (char *) xmalloc(p->len_out);
3540 memcpy(p->buf_out, buf, p->len_out);
3545 while (ZOOM_event(1, &c))
3552 ZOOM_API(ZOOM_package)
3553 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3555 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3558 p->odr_out = odr_createmem(ODR_ENCODE);
3559 p->options = ZOOM_options_create_with_parent2(options, c->options);
3567 ZOOM_package_destroy(ZOOM_package p)
3572 if (p->refcount == 0)
3574 odr_destroy(p->odr_out);
3577 ZOOM_options_destroy(p->options);
3582 ZOOM_API(const char *)
3583 ZOOM_package_option_get(ZOOM_package p, const char *key)
3585 return ZOOM_options_get(p->options, key);
3588 ZOOM_API(const char *)
3589 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3591 return ZOOM_options_getl(p->options, key, lenp);
3595 ZOOM_package_option_set(ZOOM_package p, const char *key,
3598 ZOOM_options_set(p->options, key, val);
3602 ZOOM_package_option_setl(ZOOM_package p, const char *key,
3603 const char *val, int len)
3605 ZOOM_options_setl(p->options, key, val, len);
3608 static int ZOOM_connection_exec_task(ZOOM_connection c)
3610 ZOOM_task task = c->tasks;
3611 zoom_ret ret = zoom_complete;
3615 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3616 c, task->which, task->running);
3617 if (c->error != ZOOM_ERROR_NONE)
3619 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3620 "removing tasks because of error = %d", c, c->error);
3621 ZOOM_connection_remove_tasks(c);
3626 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3627 "task already running", c);
3631 ret = zoom_complete;
3632 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3634 switch (task->which)
3636 case ZOOM_TASK_SEARCH:
3637 if (c->proto == PROTO_HTTP)
3638 ret = ZOOM_connection_srw_send_search(c);
3640 ret = ZOOM_connection_send_search(c);
3642 case ZOOM_TASK_RETRIEVE:
3643 if (c->proto == PROTO_HTTP)
3644 ret = ZOOM_connection_srw_send_search(c);
3646 ret = send_present(c);
3648 case ZOOM_TASK_CONNECT:
3649 ret = do_connect(c);
3651 case ZOOM_TASK_SCAN:
3652 if (c->proto == PROTO_HTTP)
3653 ret = ZOOM_connection_srw_send_scan(c);
3655 ret = ZOOM_connection_send_scan(c);
3657 case ZOOM_TASK_PACKAGE:
3658 ret = send_package(c);
3660 case ZOOM_TASK_SORT:
3661 c->tasks->u.sort.resultset->r_sort_spec =
3662 c->tasks->u.sort.q->sort_spec;
3663 ret = send_sort(c, c->tasks->u.sort.resultset);
3669 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3670 "remove tasks because no connection exist", c);
3671 ZOOM_connection_remove_tasks(c);
3673 if (ret == zoom_complete)
3675 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3676 "task removed (complete)", c);
3677 ZOOM_connection_remove_task(c);
3680 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3685 static zoom_ret send_sort_present(ZOOM_connection c)
3687 zoom_ret r = zoom_complete;
3689 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3690 r = send_sort(c, c->tasks->u.search.resultset);
3691 if (r == zoom_complete)
3692 r = send_present(c);
3696 static int es_response_taskpackage_update(ZOOM_connection c,
3697 Z_IUUpdateTaskPackage *utp)
3699 if (utp && utp->targetPart)
3701 Z_IUTargetPart *targetPart = utp->targetPart;
3702 switch ( *targetPart->updateStatus ) {
3703 case Z_IUTargetPart_success:
3704 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3706 case Z_IUTargetPart_partial:
3707 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3709 case Z_IUTargetPart_failure:
3710 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3711 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3712 response_diag(c, targetPart->globalDiagnostics[0]);
3715 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3720 static int es_response_taskpackage(ZOOM_connection c,
3721 Z_TaskPackage *taskPackage)
3724 Odr_oct *id = taskPackage->targetReference;
3726 ZOOM_options_setl(c->tasks->u.package->options,
3727 "targetReference", (char*) id->buf, id->len);
3730 switch ( *taskPackage->taskStatus ) {
3731 case Z_TaskPackage_pending:
3732 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3734 case Z_TaskPackage_active:
3735 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3737 case Z_TaskPackage_complete:
3738 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3740 case Z_TaskPackage_aborted:
3741 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3742 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3743 response_diag(c, taskPackage->packageDiagnostics[0]);
3747 // taskSpecificParameters
3748 // NOTE: Only Update implemented, no others.
3749 if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3750 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3751 es_response_taskpackage_update(c, utp);
3757 static int es_response(ZOOM_connection c,
3758 Z_ExtendedServicesResponse *res)
3760 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3762 switch (*res->operationStatus) {
3763 case Z_ExtendedServicesResponse_done:
3764 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3766 case Z_ExtendedServicesResponse_accepted:
3767 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3769 case Z_ExtendedServicesResponse_failure:
3770 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3771 if (res->diagnostics && res->num_diagnostics > 0)
3772 response_diag(c, res->diagnostics[0]);
3775 if (res->taskPackage &&
3776 res->taskPackage->which == Z_External_extendedService)
3778 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3779 es_response_taskpackage(c, taskPackage);
3781 if (res->taskPackage &&
3782 res->taskPackage->which == Z_External_octet)
3784 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3785 ZOOM_options_setl(c->tasks->u.package->options,
3786 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3791 static void interpret_init_diag(ZOOM_connection c,
3792 Z_DiagnosticFormat *diag)
3796 Z_DiagnosticFormat_s *ds = diag->elements[0];
3797 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3798 response_default_diag(c, ds->u.defaultDiagRec);
3803 static void interpret_otherinformation_field(ZOOM_connection c,
3804 Z_OtherInformation *ui)
3807 for (i = 0; i < ui->num_elements; i++)
3809 Z_OtherInformationUnit *unit = ui->list[i];
3810 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3811 unit->information.externallyDefinedInfo &&
3812 unit->information.externallyDefinedInfo->which ==
3815 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3821 static void set_init_option(const char *name, void *clientData) {
3822 ZOOM_connection c = (ZOOM_connection) clientData;
3825 sprintf(buf, "init_opt_%.70s", name);
3826 ZOOM_connection_option_set(c, buf, "1");
3830 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3832 Z_InitResponse *initrs;
3834 ZOOM_connection_set_mask(c, 0);
3835 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3838 case Z_APDU_initResponse:
3839 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3840 initrs = apdu->u.initResponse;
3841 ZOOM_connection_option_set(c, "serverImplementationId",
3842 initrs->implementationId ?
3843 initrs->implementationId : "");
3844 ZOOM_connection_option_set(c, "serverImplementationName",
3845 initrs->implementationName ?
3846 initrs->implementationName : "");
3847 ZOOM_connection_option_set(c, "serverImplementationVersion",
3848 initrs->implementationVersion ?
3849 initrs->implementationVersion : "");
3850 /* Set the three old options too, for old applications */
3851 ZOOM_connection_option_set(c, "targetImplementationId",
3852 initrs->implementationId ?
3853 initrs->implementationId : "");
3854 ZOOM_connection_option_set(c, "targetImplementationName",
3855 initrs->implementationName ?
3856 initrs->implementationName : "");
3857 ZOOM_connection_option_set(c, "targetImplementationVersion",
3858 initrs->implementationVersion ?
3859 initrs->implementationVersion : "");
3861 /* Make initrs->options available as ZOOM-level options */
3862 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3864 if (!*initrs->result)
3866 Z_External *uif = initrs->userInformationField;
3868 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3870 if (uif && uif->which == Z_External_userInfo1)
3871 interpret_otherinformation_field(c, uif->u.userInfo1);
3876 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3877 yaz_oid_userinfo_cookie, 1, 0);
3878 xfree(c->cookie_in);
3881 c->cookie_in = xstrdup(cookie);
3882 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3883 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3884 c->support_named_resultsets = 1;
3887 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3888 ZOOM_connection_remove_task(c);
3890 ZOOM_connection_exec_task(c);
3892 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3894 NMEM tmpmem = nmem_create();
3895 Z_CharSetandLanguageNegotiation *p =
3896 yaz_get_charneg_record(initrs->otherInfo);
3900 char *charset = NULL, *lang = NULL;
3903 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3904 yaz_log(log_details, "%p recv_apdu target accepted: "
3905 "charset %s, language %s, select %d",
3907 charset ? charset : "none", lang ? lang : "none", sel);
3909 ZOOM_connection_option_set(c, "negotiation-charset",
3912 ZOOM_connection_option_set(c, "negotiation-lang",
3915 ZOOM_connection_option_set(
3916 c, "negotiation-charset-in-effect-for-records",
3917 (sel != 0) ? "1" : "0");
3918 nmem_destroy(tmpmem);
3922 case Z_APDU_searchResponse:
3923 yaz_log(log_api, "%p recv_apdu Search response", c);
3924 handle_search_response(c, apdu->u.searchResponse);
3925 if (send_sort_present(c) == zoom_complete)
3926 ZOOM_connection_remove_task(c);
3928 case Z_APDU_presentResponse:
3929 yaz_log(log_api, "%p recv_apdu Present response", c);
3930 handle_present_response(c, apdu->u.presentResponse);
3931 if (send_present(c) == zoom_complete)
3932 ZOOM_connection_remove_task(c);
3934 case Z_APDU_sortResponse:
3935 yaz_log(log_api, "%p recv_apdu Sort response", c);
3936 sort_response(c, apdu->u.sortResponse);
3937 if (send_present(c) == zoom_complete)
3938 ZOOM_connection_remove_task(c);
3940 case Z_APDU_scanResponse:
3941 yaz_log(log_api, "%p recv_apdu Scan response", c);
3942 scan_response(c, apdu->u.scanResponse);
3943 ZOOM_connection_remove_task(c);
3945 case Z_APDU_extendedServicesResponse:
3946 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3947 es_response(c, apdu->u.extendedServicesResponse);
3948 ZOOM_connection_remove_task(c);
3951 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3952 if (!ZOOM_test_reconnect(c))
3954 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3959 yaz_log(log_api, "%p Received unknown PDU", c);
3960 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3966 static zoom_ret handle_srw_response(ZOOM_connection c,
3967 Z_SRW_searchRetrieveResponse *res)
3969 ZOOM_resultset resultset = 0;
3974 const char *syntax, *elementSetName;
3977 return zoom_complete;
3979 switch(c->tasks->which)
3981 case ZOOM_TASK_SEARCH:
3982 resultset = c->tasks->u.search.resultset;
3983 start = &c->tasks->u.search.start;
3984 count = &c->tasks->u.search.count;
3985 syntax = c->tasks->u.search.syntax;
3986 elementSetName = c->tasks->u.search.elementSetName;
3988 case ZOOM_TASK_RETRIEVE:
3989 resultset = c->tasks->u.retrieve.resultset;
3990 start = &c->tasks->u.retrieve.start;
3991 count = &c->tasks->u.retrieve.count;
3992 syntax = c->tasks->u.retrieve.syntax;
3993 elementSetName = c->tasks->u.retrieve.elementSetName;
3996 return zoom_complete;
3998 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3999 ZOOM_connection_put_event(c, event);
4001 resultset->size = 0;
4003 if (res->resultSetId)
4004 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
4006 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
4008 if (res->numberOfRecords)
4009 resultset->size = *res->numberOfRecords;
4011 for (i = 0; i<res->num_records; i++)
4014 Z_SRW_record *sru_rec;
4015 Z_SRW_diagnostic *diag = 0;
4018 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
4019 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
4021 if (res->records[i].recordPosition &&
4022 *res->records[i].recordPosition > 0)
4023 pos = *res->records[i].recordPosition - 1;
4027 sru_rec = &res->records[i];
4029 npr->databaseName = 0;
4030 npr->which = Z_NamePlusRecord_databaseRecord;
4031 npr->u.databaseRecord = (Z_External *)
4032 odr_malloc(c->odr_in, sizeof(Z_External));
4033 npr->u.databaseRecord->descriptor = 0;
4034 npr->u.databaseRecord->direct_reference =
4035 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
4036 npr->u.databaseRecord->which = Z_External_octet;
4038 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
4039 odr_malloc(c->odr_in, sizeof(Odr_oct));
4040 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
4041 sru_rec->recordData_buf;
4042 npr->u.databaseRecord->u.octet_aligned->len =
4043 npr->u.databaseRecord->u.octet_aligned->size =
4044 sru_rec->recordData_len;
4046 if (sru_rec->recordSchema
4047 && !strcmp(sru_rec->recordSchema,
4048 "info:srw/schema/1/diagnostics-v1.1"))
4050 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
4051 sru_rec->recordData_len,
4055 record_cache_add(resultset, npr, pos, syntax, elementSetName,
4056 sru_rec->recordSchema, diag);
4060 if (*count + *start > resultset->size)
4061 *count = resultset->size - *start;
4065 nmem = odr_extract_mem(c->odr_in);
4066 nmem_transfer(odr_getmem(resultset->odr), nmem);
4069 if (res->num_diagnostics > 0)
4070 set_SRU_error(c, &res->diagnostics[0]);
4071 else if (*count > 0)
4072 return ZOOM_connection_srw_send_search(c);
4073 return zoom_complete;
4078 static void handle_srw_scan_response(ZOOM_connection c,
4079 Z_SRW_scanResponse *res)
4081 NMEM nmem = odr_extract_mem(c->odr_in);
4084 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4086 scan = c->tasks->u.scan.scan;
4088 if (res->num_diagnostics > 0)
4089 set_SRU_error(c, &res->diagnostics[0]);
4091 scan->scan_response = 0;
4092 scan->srw_scan_response = res;
4093 nmem_transfer(odr_getmem(scan->odr), nmem);
4095 ZOOM_options_set_int(scan->options, "number", res->num_terms);
4101 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4103 zoom_ret cret = zoom_complete;
4105 const char *addinfo = 0;
4106 const char *connection_head = z_HTTP_header_lookup(hres->headers,
4108 ZOOM_connection_set_mask(c, 0);
4109 yaz_log(log_details, "%p handle_http", c);
4111 if (!yaz_srw_check_content_type(hres))
4112 addinfo = "content-type";
4115 Z_SOAP *soap_package = 0;
4117 Z_SOAP_Handler soap_handlers[2] = {
4118 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4121 ret = z_soap_codec(o, &soap_package,
4122 &hres->content_buf, &hres->content_len,
4124 if (!ret && soap_package->which == Z_SOAP_generic &&
4125 soap_package->u.generic->no == 0)
4127 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4129 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4130 if (sr->which == Z_SRW_searchRetrieve_response)
4131 cret = handle_srw_response(c, sr->u.response);
4132 else if (sr->which == Z_SRW_scan_response)
4133 handle_srw_scan_response(c, sr->u.scan_response);
4137 else if (!ret && (soap_package->which == Z_SOAP_fault
4138 || soap_package->which == Z_SOAP_error))
4140 set_HTTP_error(c, hres->code,
4141 soap_package->u.fault->fault_code,
4142 soap_package->u.fault->fault_string);
4149 if (hres->code != 200)
4150 set_HTTP_error(c, hres->code, 0, 0);
4152 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4155 if (cret == zoom_complete)
4156 ZOOM_connection_remove_task(c);
4157 if (!strcmp(hres->version, "1.0"))
4159 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4160 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4165 /* HTTP 1.1: only if no close we stay alive .. */
4166 if (connection_head && !strcmp(connection_head, "close"))
4172 static int do_read(ZOOM_connection c)
4177 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4178 ZOOM_connection_put_event(c, event);
4180 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4181 more = cs_more(c->cs);
4182 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4187 if (!ZOOM_test_reconnect(c))
4189 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4198 odr_reset(c->odr_in);
4199 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4200 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4201 ZOOM_connection_put_event(c, event);
4203 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4206 int err = odr_geterrorx(c->odr_in, &x);
4208 const char *element = odr_getelement(c->odr_in);
4209 yaz_snprintf(msg, sizeof(msg),
4210 "ODR code %d:%d element=%s offset=%d",
4211 err, x, element ? element : "<unknown>",
4212 odr_offset(c->odr_in));
4213 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4216 FILE *ber_file = yaz_log_file();
4218 odr_dumpBER(ber_file, c->buf_in, r);
4225 z_GDU(c->odr_print, &gdu, 0, 0);
4226 if (gdu->which == Z_GDU_Z3950)
4227 recv_apdu(c, gdu->u.z3950);
4228 else if (gdu->which == Z_GDU_HTTP_Response)
4231 handle_http(c, gdu->u.HTTP_Response);
4233 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4238 c->reconnect_ok = 0;
4243 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4248 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4249 ZOOM_connection_put_event(c, event);
4251 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4252 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4254 yaz_log(log_details, "%p do_write_ex write failed", c);
4255 if (ZOOM_test_reconnect(c))
4257 return zoom_pending;
4259 if (c->state == STATE_CONNECTING)
4260 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4262 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4264 return zoom_complete;
4268 int mask = ZOOM_SELECT_EXCEPT;
4269 if (c->cs->io_pending & CS_WANT_WRITE)
4270 mask += ZOOM_SELECT_WRITE;
4271 if (c->cs->io_pending & CS_WANT_READ)
4272 mask += ZOOM_SELECT_READ;
4273 ZOOM_connection_set_mask(c, mask);
4274 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4279 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4280 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4283 return zoom_pending;
4286 static zoom_ret do_write(ZOOM_connection c)
4288 return do_write_ex(c, c->buf_out, c->len_out);
4292 ZOOM_API(const char *)
4293 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4295 return ZOOM_options_get(c->options, key);
4298 ZOOM_API(const char *)
4299 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4301 return ZOOM_options_getl(c->options, key, lenp);
4305 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4308 ZOOM_options_set(c->options, key, val);
4312 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4313 const char *val, int len)
4315 ZOOM_options_setl(c->options, key, val, len);
4318 ZOOM_API(const char *)
4319 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4321 return ZOOM_options_get(r->options, key);
4325 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4328 ZOOM_options_set(r->options, key, val);
4333 ZOOM_connection_errcode(ZOOM_connection c)
4335 return ZOOM_connection_error(c, 0, 0);
4338 ZOOM_API(const char *)
4339 ZOOM_connection_errmsg(ZOOM_connection c)
4342 ZOOM_connection_error(c, &msg, 0);
4346 ZOOM_API(const char *)
4347 ZOOM_connection_addinfo(ZOOM_connection c)
4349 const char *addinfo;
4350 ZOOM_connection_error(c, 0, &addinfo);
4354 ZOOM_API(const char *)
4355 ZOOM_connection_diagset(ZOOM_connection c)
4357 const char *diagset;
4358 ZOOM_connection_error_x(c, 0, 0, &diagset);
4362 ZOOM_API(const char *)
4363 ZOOM_diag_str(int error)
4367 case ZOOM_ERROR_NONE:
4369 case ZOOM_ERROR_CONNECT:
4370 return "Connect failed";
4371 case ZOOM_ERROR_MEMORY:
4372 return "Out of memory";
4373 case ZOOM_ERROR_ENCODE:
4374 return "Encoding failed";
4375 case ZOOM_ERROR_DECODE:
4376 return "Decoding failed";
4377 case ZOOM_ERROR_CONNECTION_LOST:
4378 return "Connection lost";
4379 case ZOOM_ERROR_INIT:
4380 return "Init rejected";
4381 case ZOOM_ERROR_INTERNAL:
4382 return "Internal failure";
4383 case ZOOM_ERROR_TIMEOUT:
4385 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4386 return "Unsupported protocol";
4387 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4388 return "Unsupported query type";
4389 case ZOOM_ERROR_INVALID_QUERY:
4390 return "Invalid query";
4391 case ZOOM_ERROR_CQL_PARSE:
4392 return "CQL parsing error";
4393 case ZOOM_ERROR_CQL_TRANSFORM:
4394 return "CQL transformation error";
4395 case ZOOM_ERROR_CCL_CONFIG:
4396 return "CCL configuration error";
4397 case ZOOM_ERROR_CCL_PARSE:
4398 return "CCL parsing error";
4400 return diagbib1_str(error);
4405 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4406 const char **addinfo, const char **diagset)
4408 int error = c->error;
4411 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4412 *cp = ZOOM_diag_str(error);
4413 else if (!strcmp(c->diagset, "HTTP"))
4414 *cp = z_HTTP_errmsg(c->error);
4415 else if (!strcmp(c->diagset, "Bib-1"))
4416 *cp = ZOOM_diag_str(error);
4417 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4418 *cp = yaz_diag_srw_str(c->error);
4420 *cp = "Unknown error and diagnostic set";
4423 *addinfo = c->addinfo ? c->addinfo : "";
4425 *diagset = c->diagset ? c->diagset : "";
4430 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4431 const char **addinfo)
4433 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4436 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4438 ZOOM_Event event = 0;
4439 int r = cs_look(c->cs);
4440 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4445 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4446 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4448 ZOOM_connection_put_event(c, event);
4450 else if (r == CS_CONNECT)
4452 int ret = ret = cs_rcvconnect(c->cs);
4453 yaz_log(log_details, "%p ZOOM_connection_do_io "
4454 "cs_rcvconnect returned %d", c, ret);
4457 int mask = ZOOM_SELECT_EXCEPT;
4458 if (c->cs->io_pending & CS_WANT_WRITE)
4459 mask += ZOOM_SELECT_WRITE;
4460 if (c->cs->io_pending & CS_WANT_READ)
4461 mask += ZOOM_SELECT_READ;
4462 ZOOM_connection_set_mask(c, mask);
4463 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4464 ZOOM_connection_put_event(c, event);
4468 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4469 ZOOM_connection_put_event(c, event);
4471 if (c->proto == PROTO_Z3950)
4472 ZOOM_connection_send_init(c);
4475 /* no init request for SRW .. */
4476 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4477 ZOOM_connection_remove_task(c);
4478 ZOOM_connection_set_mask(c, 0);
4479 ZOOM_connection_exec_task(c);
4481 c->state = STATE_ESTABLISHED;
4485 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4491 if (mask & ZOOM_SELECT_EXCEPT)
4493 if (!ZOOM_test_reconnect(c))
4495 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4500 if (mask & ZOOM_SELECT_READ)
4502 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4508 ZOOM_connection_last_event(ZOOM_connection cs)
4511 return ZOOM_EVENT_NONE;
4512 return cs->last_event;
4516 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4518 WRBUF wrbuf = (WRBUF) client_data;
4519 wrbuf_puts(wrbuf, buf);
4523 * Returns an xmalloc()d string containing RPN that corresponds to the
4524 * CQL passed in. On error, sets the Connection object's error state
4525 * and returns a null pointer.
4526 * ### We could cache CQL parser and/or transformer in Connection.
4528 static char *cql2pqf(ZOOM_connection c, const char *cql)
4532 const char *cqlfile;
4533 cql_transform_t trans;
4536 parser = cql_parser_create();
4537 if ((error = cql_parser_string(parser, cql)) != 0) {
4538 cql_parser_destroy(parser);
4539 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4543 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4546 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4548 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4551 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4552 cqlfile, strerror(errno));
4553 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4557 WRBUF wrbuf_result = wrbuf_alloc();
4558 error = cql_transform(trans, cql_parser_result(parser),
4559 cql2pqf_wrbuf_puts, wrbuf_result);
4562 const char *addinfo;
4563 error = cql_transform_error(trans, &addinfo);
4564 sprintf(buf, "%.200s (addinfo=%.200s)",
4565 cql_strerror(error), addinfo);
4566 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4570 result = xstrdup(wrbuf_cstr(wrbuf_result));
4572 cql_transform_close(trans);
4573 wrbuf_destroy(wrbuf_result);
4575 cql_parser_destroy(parser);
4579 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4583 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4584 /* timeout and this connection was waiting */
4585 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4587 ZOOM_connection_put_event(c, event);
4593 ZOOM_connection_process(ZOOM_connection c)
4599 event = ZOOM_connection_get_event(c);
4602 ZOOM_Event_destroy(event);
4605 ZOOM_connection_exec_task(c);
4606 event = ZOOM_connection_get_event(c);
4609 ZOOM_Event_destroy(event);
4616 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4620 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4622 for (i = 0; i<no; i++)
4624 ZOOM_connection c = cs[i];
4626 if (c && ZOOM_connection_process(c))
4632 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4634 if (c->mask && mask)
4635 ZOOM_connection_do_io(c, mask);
4639 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4642 return cs_fileno(c->cs);
4646 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4654 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4661 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4663 return ZOOM_options_get_int(c->options, "timeout", 30);
4669 * indent-tabs-mode: nil
4671 * vim: shiftwidth=4 tabstop=8 expandtab