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);
46 * This wrapper is just for logging failed lookups. It would be nicer
47 * if it could cause failure when a lookup fails, but that's hard.
49 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
50 oid_class oid_class, const char *str) {
51 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
54 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
55 c, (int) oid_class, str);
60 static void initlog(void)
62 static int log_level_initialized = 0;
63 if (!log_level_initialized)
65 log_api = yaz_log_module_level("zoom");
66 log_details = yaz_log_module_level("zoomdetails");
67 log_level_initialized = 1;
71 static ZOOM_Event ZOOM_Event_create(int kind)
73 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
77 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
81 static void ZOOM_Event_destroy(ZOOM_Event event)
86 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
90 c->m_queue_back->prev = event;
91 assert(c->m_queue_front);
95 assert(!c->m_queue_front);
96 c->m_queue_front = event;
98 event->next = c->m_queue_back;
100 c->m_queue_back = event;
103 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
105 ZOOM_Event event = c->m_queue_front;
108 c->last_event = ZOOM_EVENT_NONE;
111 assert(c->m_queue_back);
112 c->m_queue_front = event->prev;
113 if (c->m_queue_front)
115 assert(c->m_queue_back);
116 c->m_queue_front->next = 0;
120 c->last_event = event->kind;
124 static void ZOOM_connection_remove_events(ZOOM_connection c)
127 while ((event = ZOOM_connection_get_event(c)))
128 ZOOM_Event_destroy(event);
131 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
133 ZOOM_Event event = c->m_queue_front;
135 return event ? event->kind : ZOOM_EVENT_NONE;
138 void ZOOM_connection_remove_tasks(ZOOM_connection c);
140 static void set_dset_error(ZOOM_connection c, int error,
142 const char *addinfo, const char *addinfo2)
149 if (!c->diagset || strcmp(dset, c->diagset))
152 c->diagset = xstrdup(dset);
153 /* remove integer part from SRW diagset .. */
154 if ((cp = strrchr(c->diagset, '/')))
157 if (addinfo && addinfo2)
159 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
160 strcpy(c->addinfo, addinfo);
161 strcat(c->addinfo, addinfo2);
164 c->addinfo = xstrdup(addinfo);
165 if (error != ZOOM_ERROR_NONE)
167 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
168 c, c->host_port ? c->host_port : "<>", dset, error,
169 addinfo ? addinfo : "",
170 addinfo2 ? addinfo2 : "");
171 ZOOM_connection_remove_tasks(c);
175 static int uri_to_code(const char *uri)
179 if ((cp = strrchr(uri, '/')))
185 static void set_HTTP_error(ZOOM_connection c, int error,
186 const char *addinfo, const char *addinfo2)
188 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
191 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
193 const char *uri = d->uri;
195 set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
201 static void set_ZOOM_error(ZOOM_connection c, int error,
204 set_dset_error(c, error, "ZOOM", addinfo, 0);
207 static void clear_error(ZOOM_connection c)
210 * If an error is tied to an operation then it's ok to clear: for
211 * example, a diagnostic returned from a search is cleared by a
212 * subsequent search. However, problems such as Connection Lost
213 * or Init Refused are not cleared, because they are not
214 * recoverable: doing another search doesn't help.
217 ZOOM_connection_remove_events(c);
220 case ZOOM_ERROR_CONNECT:
221 case ZOOM_ERROR_MEMORY:
222 case ZOOM_ERROR_DECODE:
223 case ZOOM_ERROR_CONNECTION_LOST:
224 case ZOOM_ERROR_INIT:
225 case ZOOM_ERROR_INTERNAL:
226 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
229 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
233 void ZOOM_connection_show_task(ZOOM_task task)
237 case ZOOM_TASK_SEARCH:
238 yaz_log(YLOG_LOG, "search p=%p", task);
240 case ZOOM_TASK_RETRIEVE:
241 yaz_log(YLOG_LOG, "retrieve p=%p", task);
243 case ZOOM_TASK_CONNECT:
244 yaz_log(YLOG_LOG, "connect p=%p", task);
247 yaz_log(YLOG_LOG, "scant p=%p", task);
252 void ZOOM_connection_show_tasks(ZOOM_connection c)
255 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
256 for (task = c->tasks; task; task = task->next)
257 ZOOM_connection_show_task(task);
260 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
262 ZOOM_task *taskp = &c->tasks;
264 taskp = &(*taskp)->next;
265 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
266 (*taskp)->running = 0;
267 (*taskp)->which = which;
273 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
275 return c->tasks ? 0 : 1;
278 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
280 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
282 task->next = c->tasks;
291 void ZOOM_connection_remove_task(ZOOM_connection c)
293 ZOOM_task task = c->tasks;
297 c->tasks = task->next;
300 case ZOOM_TASK_SEARCH:
301 resultset_destroy(task->u.search.resultset);
302 xfree(task->u.search.syntax);
303 xfree(task->u.search.elementSetName);
305 case ZOOM_TASK_RETRIEVE:
306 resultset_destroy(task->u.retrieve.resultset);
307 xfree(task->u.retrieve.syntax);
308 xfree(task->u.retrieve.elementSetName);
310 case ZOOM_TASK_CONNECT:
313 ZOOM_scanset_destroy(task->u.scan.scan);
315 case ZOOM_TASK_PACKAGE:
316 ZOOM_package_destroy(task->u.package);
319 resultset_destroy(task->u.sort.resultset);
320 ZOOM_query_destroy(task->u.sort.q);
329 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
330 ZOOM_connection_put_event(c, event);
335 static int ZOOM_connection_exec_task(ZOOM_connection c);
337 void ZOOM_connection_remove_tasks(ZOOM_connection c)
340 ZOOM_connection_remove_task(c);
343 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
345 const char *elementSetName);
347 ZOOM_API(ZOOM_connection)
348 ZOOM_connection_create(ZOOM_options options)
350 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
354 yaz_log(log_api, "%p ZOOM_connection_create", c);
356 c->proto = PROTO_Z3950;
358 ZOOM_connection_set_mask(c, 0);
360 c->state = STATE_IDLE;
363 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
370 c->options = ZOOM_options_create_with_parent(options);
376 c->charset = c->lang = 0;
387 c->maximum_record_size = 0;
388 c->preferred_message_size = 0;
390 c->odr_in = odr_createmem(ODR_DECODE);
391 c->odr_out = odr_createmem(ODR_ENCODE);
394 c->support_named_resultsets = 0;
395 c->last_event = ZOOM_EVENT_NONE;
397 c->m_queue_front = 0;
405 /* set database names. Take local databases (if set); otherwise
406 take databases given in ZURL (if set); otherwise use Default */
407 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
410 char **databaseNames;
411 const char *cp = ZOOM_options_get(options, "databaseName");
413 if ((!cp || !*cp) && con->host_port)
415 if (strncmp(con->host_port, "unix:", 5) == 0)
416 cp = strchr(con->host_port+5, ':');
418 cp = strchr(con->host_port, '/');
424 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
425 return databaseNames;
428 ZOOM_API(ZOOM_connection)
429 ZOOM_connection_new(const char *host, int portnum)
431 ZOOM_connection c = ZOOM_connection_create(0);
433 ZOOM_connection_connect(c, host, portnum);
437 static zoom_sru_mode get_sru_mode_from_string(const char *s)
440 return zoom_sru_soap;
441 if (!yaz_matchstr(s, "soap"))
442 return zoom_sru_soap;
443 else if (!yaz_matchstr(s, "get"))
445 else if (!yaz_matchstr(s, "post"))
446 return zoom_sru_post;
447 return zoom_sru_error;
451 ZOOM_connection_connect(ZOOM_connection c,
452 const char *host, int portnum)
459 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
460 c, host ? host : "null", portnum);
462 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
463 ZOOM_connection_remove_tasks(c);
467 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
471 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
474 val = ZOOM_options_get(c->options, "proxy");
477 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
478 c->proxy = xstrdup(val);
483 val = ZOOM_options_get(c->options, "charset");
486 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
487 c->charset = xstrdup(val);
491 val = ZOOM_options_get(c->options, "lang");
494 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
495 c->lang = xstrdup(val);
506 sprintf(hostn, "%.80s:%d", host, portnum);
507 c->host_port = xstrdup(hostn);
510 c->host_port = xstrdup(host);
515 * If the "<scheme>:" part of the host string is preceded by one
516 * or more comma-separated <name>=<value> pairs, these are taken
517 * to be options to be set on the connection object. Among other
518 * applications, this facility can be used to embed authentication
520 * user=admin,password=secret,tcp:localhost:9999
522 char *remainder = c->host_port;
523 char *pcolon = strchr(remainder, ':');
526 while ((pcomma = strchr(remainder, ',')) != 0 &&
527 (pcolon == 0 || pcomma < pcolon)) {
529 if ((pequals = strchr(remainder, '=')) != 0) {
531 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
532 ZOOM_connection_option_set(c, remainder, pequals+1);
534 remainder = pcomma+1;
537 if (remainder != c->host_port) {
539 c->host_port = xstrdup(remainder);
540 /*printf("# reset hp='%s'\n", remainder);*/
544 val = ZOOM_options_get(c->options, "sru");
545 c->sru_mode = get_sru_mode_from_string(val);
547 xfree(c->sru_version);
548 val = ZOOM_options_get(c->options, "sru_version");
549 c->sru_version = xstrdup(val ? val : "1.2");
551 ZOOM_options_set(c->options, "host", c->host_port);
553 xfree(c->cookie_out);
555 val = ZOOM_options_get(c->options, "cookie");
558 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
559 c->cookie_out = xstrdup(val);
564 val = ZOOM_options_get(c->options, "clientIP");
567 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
569 c->client_IP = xstrdup(val);
574 val = ZOOM_options_get(c->options, "group");
576 c->group = xstrdup(val);
580 val = ZOOM_options_get(c->options, "user");
582 c->user = xstrdup(val);
586 val = ZOOM_options_get(c->options, "password");
588 val = ZOOM_options_get(c->options, "pass");
591 c->password = xstrdup(val);
593 c->maximum_record_size =
594 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
595 c->preferred_message_size =
596 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
598 c->async = ZOOM_options_get_bool(c->options, "async", 0);
599 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
601 c->odr_print = odr_createmem(ODR_PRINT);
602 odr_setprint(c->odr_print, yaz_log_file());
606 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
608 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
612 while (ZOOM_event(1, &c))
618 ZOOM_query_create(void)
620 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
622 yaz_log(log_details, "%p ZOOM_query_create", s);
626 s->odr = odr_createmem(ODR_ENCODE);
633 ZOOM_query_destroy(ZOOM_query s)
639 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
640 if (s->refcount == 0)
648 ZOOM_query_prefix(ZOOM_query s, const char *str)
650 s->query_string = odr_strdup(s->odr, str);
651 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
652 s->z_query->which = Z_Query_type_1;
653 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
654 if (!s->z_query->u.type_1)
656 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
660 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
665 ZOOM_query_cql(ZOOM_query s, const char *str)
669 s->query_string = odr_strdup(s->odr, str);
671 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
672 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
673 ext->indirect_reference = 0;
675 ext->which = Z_External_CQL;
676 ext->u.cql = s->query_string;
678 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
679 s->z_query->which = Z_Query_type_104;
680 s->z_query->u.type_104 = ext;
682 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
688 * Translate the CQL string client-side into RPN which is passed to
689 * the server. This is useful for server's that don't themselves
690 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
691 * only as a place to stash diagnostics if compilation fails; if this
692 * information is not needed, a null pointer may be used.
695 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
699 ZOOM_connection freeme = 0;
701 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
703 conn = freeme = ZOOM_connection_create(0);
705 rpn = cql2pqf(conn, str);
707 ZOOM_connection_destroy(freeme);
711 ret = ZOOM_query_prefix(s, rpn);
717 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
718 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
719 * to the server, as the YAZ GFS doesn't know how to handle this.
722 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
723 int *ccl_error, const char **error_string,
727 struct ccl_rpn_node *rpn;
728 CCL_bibset bibset = ccl_qual_mk();
731 ccl_qual_buf(bibset, config);
733 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
736 *error_string = ccl_err_msg(*ccl_error);
741 WRBUF wr = wrbuf_alloc();
744 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
747 ccl_qual_rm(&bibset);
752 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
754 s->sort_spec = yaz_sort_spec(s->odr, criteria);
757 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
761 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
765 static zoom_ret do_write(ZOOM_connection c);
768 ZOOM_connection_destroy(ZOOM_connection c)
773 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
776 for (r = c->resultsets; r; r = r->next)
782 odr_destroy(c->odr_in);
783 odr_destroy(c->odr_out);
786 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
787 odr_destroy(c->odr_print);
789 ZOOM_options_destroy(c->options);
790 ZOOM_connection_remove_tasks(c);
791 ZOOM_connection_remove_events(c);
797 xfree(c->cookie_out);
803 xfree(c->sru_version);
807 void ZOOM_resultset_addref(ZOOM_resultset r)
812 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
817 ZOOM_resultset ZOOM_resultset_create(void)
820 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
824 yaz_log(log_details, "%p ZOOM_resultset_create", r);
827 r->odr = odr_createmem(ODR_ENCODE);
832 for (i = 0; i<RECORD_HASH_SIZE; i++)
833 r->record_hash[i] = 0;
838 r->databaseNames = 0;
839 r->num_databaseNames = 0;
843 ZOOM_API(ZOOM_resultset)
844 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
847 ZOOM_query s = ZOOM_query_create();
849 ZOOM_query_prefix(s, q);
851 r = ZOOM_connection_search(c, s);
852 ZOOM_query_destroy(s);
856 ZOOM_API(ZOOM_resultset)
857 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
859 ZOOM_resultset r = ZOOM_resultset_create();
863 const char *syntax, *elementSetName;
865 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
866 r->r_sort_spec = q->sort_spec;
869 r->options = ZOOM_options_create_with_parent(c->options);
871 start = ZOOM_options_get_int(r->options, "start", 0);
872 count = ZOOM_options_get_int(r->options, "count", 0);
874 /* If "presentChunk" is defined use that; otherwise "step" */
875 const char *cp = ZOOM_options_get(r->options, "presentChunk");
876 r->step = ZOOM_options_get_int(r->options,
877 (cp != 0 ? "presentChunk": "step"), 0);
879 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
880 cp = ZOOM_options_get(r->options, "setname");
882 r->setname = xstrdup(cp);
883 cp = ZOOM_options_get(r->options, "schema");
885 r->schema = xstrdup(cp);
887 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
892 r->next = c->resultsets;
897 if (c->host_port && c->proto == PROTO_HTTP)
901 yaz_log(log_details, "ZOOM_connection_search: no comstack");
902 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
906 yaz_log(log_details, "ZOOM_connection_search: reconnect");
911 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
912 task->u.search.resultset = r;
913 task->u.search.start = start;
914 task->u.search.count = count;
916 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
917 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
918 elementSetName = ZOOM_options_get(r->options, "elementSetName");
919 task->u.search.elementSetName = elementSetName
920 ? xstrdup(elementSetName) : 0;
922 ZOOM_resultset_addref(r);
928 while (ZOOM_event(1, &c))
935 ZOOM_resultset_sort(ZOOM_resultset r,
936 const char *sort_type, const char *sort_spec)
938 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
942 ZOOM_resultset_sort1(ZOOM_resultset r,
943 const char *sort_type, const char *sort_spec)
945 ZOOM_connection c = r->connection;
949 newq = ZOOM_query_create();
950 if (ZOOM_query_sortby(newq, sort_spec) < 0)
953 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
954 r, r, sort_type, sort_spec);
958 if (c->host_port && c->proto == PROTO_HTTP)
962 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
963 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
967 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
973 ZOOM_resultset_cache_reset(r);
974 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
975 task->u.sort.resultset = r;
976 task->u.sort.q = newq;
978 ZOOM_resultset_addref(r);
982 while (ZOOM_event(1, &c))
990 ZOOM_resultset_cache_reset(ZOOM_resultset r)
993 for (i = 0; i<RECORD_HASH_SIZE; i++)
995 ZOOM_record_cache rc;
996 for (rc = r->record_hash[i]; rc; rc = rc->next)
998 if (rc->rec.wrbuf_marc)
999 wrbuf_destroy(rc->rec.wrbuf_marc);
1000 if (rc->rec.wrbuf_iconv)
1001 wrbuf_destroy(rc->rec.wrbuf_iconv);
1002 if (rc->rec.wrbuf_opac)
1003 wrbuf_destroy(rc->rec.wrbuf_opac);
1005 r->record_hash[i] = 0;
1010 ZOOM_resultset_destroy(ZOOM_resultset r)
1012 resultset_destroy(r);
1015 static void resultset_destroy(ZOOM_resultset r)
1020 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1022 if (r->refcount == 0)
1024 ZOOM_resultset_cache_reset(r);
1028 /* remove ourselves from the resultsets in connection */
1029 ZOOM_resultset *rp = &r->connection->resultsets;
1032 assert(*rp); /* we must be in this list!! */
1034 { /* OK, we're here - take us out of it */
1041 ZOOM_query_destroy(r->query);
1042 ZOOM_options_destroy(r->options);
1043 odr_destroy(r->odr);
1051 ZOOM_resultset_size(ZOOM_resultset r)
1053 yaz_log(log_details, "ZOOM_resultset_size r=%p count=%d",
1058 static void do_close(ZOOM_connection c)
1063 ZOOM_connection_set_mask(c, 0);
1064 c->state = STATE_IDLE;
1067 static int ZOOM_test_reconnect(ZOOM_connection c)
1071 if (!c->reconnect_ok)
1074 c->reconnect_ok = 0;
1075 c->tasks->running = 0;
1076 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1078 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1079 ZOOM_connection_put_event(c, event);
1084 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1085 int force_sync, int start, int count)
1090 const char *syntax, *elementSetName;
1094 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1095 " count=%d", r, force_sync, start, count);
1100 if (c->host_port && c->proto == PROTO_HTTP)
1104 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1105 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1109 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1111 c->reconnect_ok = 1;
1114 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1115 task->u.retrieve.resultset = r;
1116 task->u.retrieve.start = start;
1117 task->u.retrieve.count = count;
1119 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1120 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1121 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1122 task->u.retrieve.elementSetName = elementSetName
1123 ? xstrdup(elementSetName) : 0;
1125 cp = ZOOM_options_get(r->options, "schema");
1128 if (!r->schema || strcmp(r->schema, cp))
1131 r->schema = xstrdup(cp);
1135 ZOOM_resultset_addref(r);
1137 if (!r->connection->async || force_sync)
1138 while (r->connection && ZOOM_event(1, &r->connection))
1143 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1144 size_t start, size_t count)
1146 int force_present = 0;
1150 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1151 r, r, (long) start, (long) count);
1154 ZOOM_resultset_retrieve(r, force_present, start, count);
1158 for (i = 0; i< count; i++)
1159 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1163 static void get_cert(ZOOM_connection c)
1168 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1170 ZOOM_connection_option_setl(c, "sslPeerCert",
1171 cert_buf, cert_len);
1176 static zoom_ret do_connect(ZOOM_connection c)
1179 const char *effective_host;
1182 effective_host = c->proxy;
1184 effective_host = c->host_port;
1186 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1190 c->cs = cs_create_host(effective_host, 0, &add);
1192 if (c->cs && c->cs->protocol == PROTO_HTTP)
1195 const char *path = 0;
1197 c->proto = PROTO_HTTP;
1198 cs_get_host_args(c->host_port, &path);
1200 c->path = (char*) xmalloc(strlen(path)+2);
1202 strcpy(c->path+1, path);
1204 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1206 return zoom_complete;
1211 int ret = cs_connect(c->cs, add);
1214 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1215 ZOOM_connection_put_event(c, event);
1217 if (c->proto == PROTO_Z3950)
1218 ZOOM_connection_send_init(c);
1221 /* no init request for SRW .. */
1222 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1223 ZOOM_connection_remove_task(c);
1224 ZOOM_connection_set_mask(c, 0);
1225 ZOOM_connection_exec_task(c);
1227 c->state = STATE_ESTABLISHED;
1228 return zoom_pending;
1232 int mask = ZOOM_SELECT_EXCEPT;
1233 if (c->cs->io_pending & CS_WANT_WRITE)
1234 mask += ZOOM_SELECT_WRITE;
1235 if (c->cs->io_pending & CS_WANT_READ)
1236 mask += ZOOM_SELECT_READ;
1237 ZOOM_connection_set_mask(c, mask);
1238 c->state = STATE_CONNECTING;
1239 return zoom_pending;
1242 c->state = STATE_IDLE;
1243 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1244 return zoom_complete;
1247 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1250 for (i = 0; i<200; i++)
1254 Z_OtherInformation **oi;
1259 sprintf(buf, "otherInfo%d", i);
1260 val = ZOOM_options_get(c->options, buf);
1263 cp = strchr(val, ':');
1267 if (len >= sizeof(buf))
1268 len = sizeof(buf)-1;
1269 memcpy(buf, val, len);
1272 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1277 yaz_oi_APDU(a, &oi);
1278 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1282 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1287 Z_OtherInformation **oi;
1288 yaz_oi_APDU(a, &oi);
1289 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1294 Z_OtherInformation **oi;
1295 yaz_oi_APDU(a, &oi);
1296 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1299 otherInfo_attach(c, a, out);
1300 if (!z_APDU(out, &a, 0, 0))
1302 FILE *outf = fopen("/tmp/apdu.txt", "a");
1305 ODR odr_pr = odr_createmem(ODR_PRINT);
1306 fprintf(outf, "a=%p\n", a);
1307 odr_setprint(odr_pr, outf);
1308 z_APDU(odr_pr, &a, 0, 0);
1309 odr_destroy(odr_pr);
1311 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1312 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1317 z_APDU(c->odr_print, &a, 0, 0);
1318 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1322 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1326 if (encode_APDU(c, a, c->odr_out))
1327 return zoom_complete;
1328 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1329 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1330 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1331 ZOOM_connection_put_event(c, event);
1332 odr_reset(c->odr_out);
1336 /* returns 1 if PDU was sent OK (still pending )
1337 0 if PDU was not sent OK (nothing to wait for)
1340 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1342 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1343 Z_InitRequest *ireq = apdu->u.initRequest;
1344 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1345 odr_malloc(c->odr_out, sizeof(*auth));
1348 ODR_MASK_SET(ireq->options, Z_Options_search);
1349 ODR_MASK_SET(ireq->options, Z_Options_present);
1350 ODR_MASK_SET(ireq->options, Z_Options_scan);
1351 ODR_MASK_SET(ireq->options, Z_Options_sort);
1352 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1353 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1355 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1356 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1357 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1359 /* Index Data's Z39.50 Implementor Id is 81 */
1360 ireq->implementationId =
1361 odr_prepend(c->odr_out,
1362 ZOOM_options_get(c->options, "implementationId"),
1363 odr_prepend(c->odr_out, "81", ireq->implementationId));
1365 ireq->implementationName =
1366 odr_prepend(c->odr_out,
1367 ZOOM_options_get(c->options, "implementationName"),
1368 odr_prepend(c->odr_out, "ZOOM-C",
1369 ireq->implementationName));
1371 version = odr_strdup(c->odr_out, "$Revision: 1.154 $");
1372 if (strlen(version) > 10) /* check for unexpanded CVS strings */
1373 version[strlen(version)-2] = '\0';
1374 ireq->implementationVersion =
1375 odr_prepend(c->odr_out,
1376 ZOOM_options_get(c->options, "implementationVersion"),
1377 odr_prepend(c->odr_out, &version[11],
1378 ireq->implementationVersion));
1380 *ireq->maximumRecordSize = c->maximum_record_size;
1381 *ireq->preferredMessageSize = c->preferred_message_size;
1383 if (c->group || c->password)
1385 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1386 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1387 pass->userId = odr_strdup_null(c->odr_out, c->user);
1388 pass->password = odr_strdup_null(c->odr_out, c->password);
1389 auth->which = Z_IdAuthentication_idPass;
1390 auth->u.idPass = pass;
1391 ireq->idAuthentication = auth;
1395 auth->which = Z_IdAuthentication_open;
1396 auth->u.open = odr_strdup(c->odr_out, c->user);
1397 ireq->idAuthentication = auth;
1401 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1402 yaz_oid_userinfo_proxy, 1, c->host_port);
1404 if (c->charset || c->lang)
1406 Z_OtherInformation **oi;
1407 Z_OtherInformationUnit *oi_unit;
1409 yaz_oi_APDU(apdu, &oi);
1411 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1413 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1414 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1415 oi_unit->information.externallyDefinedInfo =
1416 yaz_set_proposal_charneg_list(c->odr_out, " ",
1417 c->charset, c->lang, 1);
1421 return send_APDU(c, apdu);
1425 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1430 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
1432 if (c->sru_mode == zoom_sru_get)
1434 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1436 else if (c->sru_mode == zoom_sru_post)
1438 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1440 else if (c->sru_mode == zoom_sru_soap)
1442 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1444 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1445 return zoom_complete;
1447 z_GDU(c->odr_print, &gdu, 0, 0);
1448 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1450 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1451 ZOOM_connection_put_event(c, event);
1452 odr_reset(c->odr_out);
1458 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1460 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1461 sr->username = c->user;
1462 sr->password = c->password;
1468 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1472 ZOOM_resultset resultset = 0;
1474 const char *option_val = 0;
1476 if (c->error) /* don't continue on error */
1477 return zoom_complete;
1479 switch(c->tasks->which)
1481 case ZOOM_TASK_SEARCH:
1482 resultset = c->tasks->u.search.resultset;
1483 resultset->setname = xstrdup("default");
1484 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1485 start = &c->tasks->u.search.start;
1486 count = &c->tasks->u.search.count;
1488 case ZOOM_TASK_RETRIEVE:
1489 resultset = c->tasks->u.retrieve.resultset;
1491 start = &c->tasks->u.retrieve.start;
1492 count = &c->tasks->u.retrieve.count;
1494 if (*start >= resultset->size)
1495 return zoom_complete;
1496 if (*start + *count > resultset->size)
1497 *count = resultset->size - *start;
1499 for (i = 0; i < *count; i++)
1502 record_cache_lookup(resultset, i + *start,
1503 c->tasks->u.retrieve.syntax,
1504 c->tasks->u.retrieve.elementSetName);
1509 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1510 ZOOM_connection_put_event(c, event);
1517 return zoom_complete;
1520 return zoom_complete;
1522 assert(resultset->query);
1524 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1525 if (resultset->query->z_query->which == Z_Query_type_104
1526 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1528 sr->u.request->query_type = Z_SRW_query_type_cql;
1529 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1531 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1532 resultset->query->z_query->u.type_1)
1534 sr->u.request->query_type = Z_SRW_query_type_pqf;
1535 sr->u.request->query.pqf = resultset->query->query_string;
1539 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1540 return zoom_complete;
1542 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1543 sr->u.request->maximumRecords = odr_intdup(
1544 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
1545 resultset->step : *count);
1546 sr->u.request->recordSchema = resultset->schema;
1548 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1550 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1552 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1553 yaz_encode_sru_extra(sr, c->odr_out, option_val);
1554 return send_srw(c, sr);
1557 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1559 return zoom_complete;
1563 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1566 int lslb, ssub, mspn;
1568 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1569 Z_SearchRequest *search_req = apdu->u.searchRequest;
1570 const char *elementSetName;
1571 const char *smallSetElementSetName;
1572 const char *mediumSetElementSetName;
1575 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1577 r = c->tasks->u.search.resultset;
1579 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1582 ZOOM_options_get(r->options, "elementSetName");
1583 smallSetElementSetName =
1584 ZOOM_options_get(r->options, "smallSetElementSetName");
1585 mediumSetElementSetName =
1586 ZOOM_options_get(r->options, "mediumSetElementSetName");
1588 if (!smallSetElementSetName)
1589 smallSetElementSetName = elementSetName;
1591 if (!mediumSetElementSetName)
1592 mediumSetElementSetName = elementSetName;
1597 /* prepare query for the search request */
1598 search_req->query = r->query->z_query;
1599 if (!search_req->query)
1601 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1602 return zoom_complete;
1604 if (r->query->z_query->which == Z_Query_type_1 ||
1605 r->query->z_query->which == Z_Query_type_101)
1607 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1610 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1613 search_req->query = yaz_copy_Z_Query(search_req->query,
1616 yaz_query_charset_convert_rpnquery(search_req->query->u.type_1,
1618 yaz_iconv_close(cd);
1622 search_req->databaseNames = r->databaseNames;
1623 search_req->num_databaseNames = r->num_databaseNames;
1625 /* get syntax (no need to provide unless piggyback is in effect) */
1626 syntax = c->tasks->u.search.syntax;
1628 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1629 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1630 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1631 if (lslb != -1 && ssub != -1 && mspn != -1)
1633 /* So're a Z39.50 expert? Let's hope you don't do sort */
1634 *search_req->largeSetLowerBound = lslb;
1635 *search_req->smallSetUpperBound = ssub;
1636 *search_req->mediumSetPresentNumber = mspn;
1638 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1639 && r->piggyback && !r->r_sort_spec && !r->schema)
1641 /* Regular piggyback - do it unless we're going to do sort */
1642 *search_req->largeSetLowerBound = 2000000000;
1643 *search_req->smallSetUpperBound = 1;
1644 *search_req->mediumSetPresentNumber =
1645 r->step>0 ? r->step : c->tasks->u.search.count;
1649 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1650 smallSetElementSetName = 0;
1651 mediumSetElementSetName = 0;
1654 if (smallSetElementSetName && *smallSetElementSetName)
1656 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1657 odr_malloc(c->odr_out, sizeof(*esn));
1659 esn->which = Z_ElementSetNames_generic;
1660 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1661 search_req->smallSetElementSetNames = esn;
1663 if (mediumSetElementSetName && *mediumSetElementSetName)
1665 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1666 odr_malloc(c->odr_out, sizeof(*esn));
1668 esn->which = Z_ElementSetNames_generic;
1669 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1670 search_req->mediumSetElementSetNames = esn;
1673 search_req->preferredRecordSyntax =
1674 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1678 if (c->support_named_resultsets)
1682 /* find the lowest unused ordinal so that we re-use
1683 result sets on the server. */
1684 for (ord = 1; ; ord++)
1687 sprintf(setname, "%d", ord);
1688 for (rp = c->resultsets; rp; rp = rp->next)
1689 if (rp->setname && !strcmp(rp->setname, setname))
1694 r->setname = xstrdup(setname);
1695 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1696 "set %s", c, r->setname);
1700 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1702 r->setname = xstrdup("default");
1704 ZOOM_options_set(r->options, "setname", r->setname);
1706 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1707 return send_APDU(c, apdu);
1710 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1712 char oid_name_buf[OID_STR_MAX];
1713 const char *oid_name;
1716 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1719 case Z_DefaultDiagFormat_v2Addinfo:
1720 addinfo = r->u.v2Addinfo;
1722 case Z_DefaultDiagFormat_v3Addinfo:
1723 addinfo = r->u.v3Addinfo;
1728 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1731 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1733 if (p->which != Z_DiagRec_defaultFormat)
1734 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1736 response_default_diag(c, p->u.defaultFormat);
1739 ZOOM_API(ZOOM_record)
1740 ZOOM_record_clone(ZOOM_record srec)
1747 odr_enc = odr_createmem(ODR_ENCODE);
1748 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1750 buf = odr_getbuf(odr_enc, &size, 0);
1752 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1753 nrec->odr = odr_createmem(ODR_DECODE);
1754 nrec->wrbuf_marc = 0;
1755 nrec->wrbuf_iconv = 0;
1756 nrec->wrbuf_opac = 0;
1757 odr_setbuf(nrec->odr, buf, size, 0);
1758 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1760 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1761 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1762 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1763 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1764 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1765 odr_destroy(odr_enc);
1769 ZOOM_API(ZOOM_record)
1770 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1772 const char *syntax =
1773 ZOOM_options_get(s->options, "preferredRecordSyntax");
1774 const char *elementSetName =
1775 ZOOM_options_get(s->options, "elementSetName");
1777 return record_cache_lookup(s, pos, syntax, elementSetName);
1780 ZOOM_API(ZOOM_record)
1781 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1783 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1788 * MIKE: I think force_sync should always be zero, but I don't
1789 * want to make this change until I get the go-ahead from
1790 * Adam, in case something depends on the old synchronous
1794 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1795 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1796 rec = ZOOM_resultset_record_immediate(r, pos);
1802 ZOOM_record_destroy(ZOOM_record rec)
1806 if (rec->wrbuf_marc)
1807 wrbuf_destroy(rec->wrbuf_marc);
1808 if (rec->wrbuf_iconv)
1809 wrbuf_destroy(rec->wrbuf_iconv);
1810 if (rec->wrbuf_opac)
1811 wrbuf_destroy(rec->wrbuf_opac);
1812 odr_destroy(rec->odr);
1816 static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
1818 const char *buf, int sz,
1819 const char *record_charset)
1824 yaz_marc_t mt = yaz_marc_create();
1827 strcpy(to, "UTF-8");
1828 if (record_charset && *record_charset)
1830 /* Use "from,to" or just "from" */
1831 const char *cp = strchr(record_charset, ',');
1832 int clen = strlen(record_charset);
1835 strncpy( to, cp+1, sizeof(to)-1);
1836 to[sizeof(to)-1] = '\0';
1837 clen = cp - record_charset;
1839 if (clen > sizeof(from)-1)
1840 clen = sizeof(from)-1;
1843 strncpy(from, record_charset, clen);
1849 cd = yaz_iconv_open(to, from);
1850 yaz_marc_iconv(mt, cd);
1853 yaz_marc_xml(mt, marc_type);
1854 if (!rec->wrbuf_marc)
1855 rec->wrbuf_marc = wrbuf_alloc();
1856 wrbuf_rewind(rec->wrbuf_marc);
1857 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf_marc) > 0)
1859 yaz_marc_destroy(mt);
1861 yaz_iconv_close(cd);
1863 *len = wrbuf_len(rec->wrbuf_marc);
1864 return wrbuf_cstr(rec->wrbuf_marc);
1866 yaz_marc_destroy(mt);
1868 yaz_iconv_close(cd);
1872 static const char *record_iconv_return(ZOOM_record rec, int *len,
1873 const char *buf, int sz,
1874 const char *record_charset)
1881 strcpy(to, "UTF-8");
1883 if (record_charset && *record_charset)
1885 /* Use "from,to" or just "from" */
1886 const char *cp = strchr(record_charset, ',');
1887 int clen = strlen(record_charset);
1890 strncpy( to, cp+1, sizeof(to)-1);
1891 to[sizeof(to)-1] = '\0';
1892 clen = cp - record_charset;
1894 if (clen > sizeof(from)-1)
1895 clen = sizeof(from)-1;
1898 strncpy(from, record_charset, clen);
1902 if (*from && *to && (cd = yaz_iconv_open(to, from)))
1904 if (!rec->wrbuf_iconv)
1905 rec->wrbuf_iconv = wrbuf_alloc();
1907 wrbuf_rewind(rec->wrbuf_iconv);
1909 wrbuf_iconv_write(rec->wrbuf_iconv, cd, buf, sz);
1910 wrbuf_iconv_reset(rec->wrbuf_iconv, cd);
1912 buf = wrbuf_cstr(rec->wrbuf_iconv);
1913 sz = wrbuf_len(rec->wrbuf_iconv);
1914 yaz_iconv_close(cd);
1923 ZOOM_record_error(ZOOM_record rec, const char **cp,
1924 const char **addinfo, const char **diagset)
1926 Z_NamePlusRecord *npr;
1935 *cp = rec->diag_message;
1937 *addinfo = rec->diag_details;
1939 *diagset = rec->diag_set;
1940 return uri_to_code(rec->diag_uri);
1942 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
1944 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
1945 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
1946 const char *add = 0;
1948 if (diag_rec->which == Z_DiagRec_defaultFormat)
1950 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
1953 error = *ddf->condition;
1956 case Z_DefaultDiagFormat_v2Addinfo:
1957 add = ddf->u.v2Addinfo;
1959 case Z_DefaultDiagFormat_v3Addinfo:
1960 add = ddf->u.v3Addinfo;
1965 yaz_oid_to_string(yaz_oid_std(),
1966 ddf->diagnosticSetId, &oclass);
1974 *addinfo = add ? add : "";
1976 *cp = diagbib1_str(error);
1982 ZOOM_API(const char *)
1983 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
1990 Z_NamePlusRecord *npr;
1993 *len = 0; /* default return */
2002 for (i = 0; cp[i] && i < sizeof(type)-1; i++)
2004 if (cp[i] == ';' || cp[i] == ' ')
2010 while (type_spec[i] == ';')
2013 while (type_spec[i] == ' ')
2015 if (!strncmp(type_spec+i, "charset=", 8))
2018 i = i + 8; /* skip charset= */
2019 for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++)
2021 if (type_spec[i] == ';' || type_spec[i] == ' ')
2027 else if (!strncmp(type_spec+i, "xpath=", 6))
2031 for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
2035 while (type_spec[i] == ' ')
2038 if (!strcmp(type, "database"))
2041 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2042 return npr->databaseName;
2044 else if (!strcmp(type, "schema"))
2047 *len = rec->schema ? strlen(rec->schema) : 0;
2050 else if (!strcmp(type, "syntax"))
2052 const char *desc = 0;
2053 if (npr->which == Z_NamePlusRecord_databaseRecord)
2055 Z_External *r = (Z_External *) npr->u.databaseRecord;
2056 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2061 *len = strlen(desc);
2064 if (npr->which != Z_NamePlusRecord_databaseRecord)
2067 /* from now on - we have a database record .. */
2068 if (!strcmp(type, "render"))
2070 Z_External *r = (Z_External *) npr->u.databaseRecord;
2071 const Odr_oid *oid = r->direct_reference;
2073 /* render bibliographic record .. */
2074 if (r->which == Z_External_OPAC)
2076 r = r->u.opac->bibliographicRecord;
2079 oid = r->direct_reference;
2081 if (r->which == Z_External_sutrs)
2082 return record_iconv_return(rec, len,
2083 (char*) r->u.sutrs->buf,
2086 else if (r->which == Z_External_octet)
2088 if (yaz_oid_is_iso2709(oid))
2090 const char *ret_buf = marc_iconv_return(
2091 rec, YAZ_MARC_LINE, len,
2092 (const char *) r->u.octet_aligned->buf,
2093 r->u.octet_aligned->len,
2098 return record_iconv_return(rec, len,
2099 (const char *) r->u.octet_aligned->buf,
2100 r->u.octet_aligned->len,
2103 else if (r->which == Z_External_grs1)
2105 if (!rec->wrbuf_marc)
2106 rec->wrbuf_marc = wrbuf_alloc();
2107 wrbuf_rewind(rec->wrbuf_marc);
2108 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
2109 return record_iconv_return(rec, len,
2110 wrbuf_buf(rec->wrbuf_marc),
2111 wrbuf_len(rec->wrbuf_marc),
2116 else if (!strcmp(type, "xml"))
2118 Z_External *r = (Z_External *) npr->u.databaseRecord;
2119 const Odr_oid *oid = r->direct_reference;
2121 /* render bibliographic record .. */
2122 if (r->which == Z_External_OPAC)
2124 r = r->u.opac->bibliographicRecord;
2127 oid = r->direct_reference;
2130 if (r->which == Z_External_sutrs)
2131 return record_iconv_return(rec, len,
2132 (const char *) r->u.sutrs->buf,
2135 else if (r->which == Z_External_octet)
2137 int marc_decode_type = YAZ_MARC_MARCXML;
2138 if (yaz_oid_is_iso2709(oid))
2140 const char *ret_buf = marc_iconv_return(
2141 rec, marc_decode_type, len,
2142 (const char *) r->u.octet_aligned->buf,
2143 r->u.octet_aligned->len,
2148 return record_iconv_return(rec, len,
2149 (const char *) r->u.octet_aligned->buf,
2150 r->u.octet_aligned->len,
2153 else if (r->which == Z_External_grs1)
2160 else if (!strcmp(type, "raw"))
2162 Z_External *r = (Z_External *) npr->u.databaseRecord;
2164 if (r->which == Z_External_sutrs)
2166 if (len) *len = r->u.sutrs->len;
2167 return (const char *) r->u.sutrs->buf;
2169 else if (r->which == Z_External_octet)
2171 if (len) *len = r->u.octet_aligned->len;
2172 return (const char *) r->u.octet_aligned->buf;
2174 else /* grs-1, explain, OPAC, ... */
2177 return (const char *) npr->u.databaseRecord;
2181 else if (!strcmp (type, "ext"))
2184 return (const char *) npr->u.databaseRecord;
2186 else if (!strcmp (type, "opac"))
2189 Z_External *r = (Z_External *) npr->u.databaseRecord;
2190 if (r->which == Z_External_OPAC)
2192 if (!rec->wrbuf_opac)
2193 rec->wrbuf_opac = wrbuf_alloc();
2194 wrbuf_rewind(rec->wrbuf_opac);
2195 yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
2196 return record_iconv_return(rec, len,
2197 wrbuf_buf(rec->wrbuf_opac),
2198 wrbuf_len(rec->wrbuf_opac),
2205 static int strcmp_null(const char *v1, const char *v2)
2211 return strcmp(v1, v2);
2214 static size_t record_hash(int pos)
2218 return pos % RECORD_HASH_SIZE;
2221 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2223 const char *syntax, const char *elementSetName,
2225 Z_SRW_diagnostic *diag)
2227 ZOOM_record_cache rc = 0;
2229 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2230 ZOOM_connection_put_event(r->connection, event);
2232 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2235 && strcmp_null(r->schema, rc->schema) == 0
2236 && strcmp_null(elementSetName,rc->elementSetName) == 0
2237 && strcmp_null(syntax, rc->syntax) == 0)
2242 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2244 rc->rec.wrbuf_marc = 0;
2245 rc->rec.wrbuf_iconv = 0;
2246 rc->rec.wrbuf_opac = 0;
2247 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2249 rc->syntax = odr_strdup_null(r->odr, syntax);
2251 rc->schema = odr_strdup_null(r->odr, r->schema);
2254 rc->next = r->record_hash[record_hash(pos)];
2255 r->record_hash[record_hash(pos)] = rc;
2258 rc->rec.schema = odr_strdup_null(r->odr, schema);
2259 rc->rec.diag_set = 0;
2260 rc->rec.diag_uri = 0;
2261 rc->rec.diag_message = 0;
2262 rc->rec.diag_details = 0;
2268 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2269 if ((cp = strrchr(rc->rec.diag_set, '/')))
2271 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2273 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2274 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2278 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2280 const char *elementSetName)
2282 ZOOM_record_cache rc;
2284 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2288 if (strcmp_null(r->schema, rc->schema))
2290 if (strcmp_null(elementSetName,rc->elementSetName))
2292 if (strcmp_null(syntax, rc->syntax))
2300 static void handle_records(ZOOM_connection c, Z_Records *sr,
2303 ZOOM_resultset resultset;
2305 const char *syntax = 0, *elementSetName = 0;
2309 switch (c->tasks->which)
2311 case ZOOM_TASK_SEARCH:
2312 resultset = c->tasks->u.search.resultset;
2313 start = &c->tasks->u.search.start;
2314 count = &c->tasks->u.search.count;
2315 syntax = c->tasks->u.search.syntax;
2316 elementSetName = c->tasks->u.search.elementSetName;
2318 case ZOOM_TASK_RETRIEVE:
2319 resultset = c->tasks->u.retrieve.resultset;
2320 start = &c->tasks->u.retrieve.start;
2321 count = &c->tasks->u.retrieve.count;
2322 syntax = c->tasks->u.retrieve.syntax;
2323 elementSetName = c->tasks->u.retrieve.elementSetName;
2328 if (sr && sr->which == Z_Records_NSD)
2329 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2330 else if (sr && sr->which == Z_Records_multipleNSD)
2332 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2333 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2335 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2339 if (*count + *start > resultset->size)
2340 *count = resultset->size - *start;
2343 if (sr && sr->which == Z_Records_DBOSD)
2346 NMEM nmem = odr_extract_mem(c->odr_in);
2347 Z_NamePlusRecordList *p =
2348 sr->u.databaseOrSurDiagnostics;
2349 for (i = 0; i<p->num_records; i++)
2351 record_cache_add(resultset, p->records[i], i + *start,
2352 syntax, elementSetName,
2359 yaz_log(log_details,
2360 "handle_records resultset=%p start=%d count=%d",
2361 resultset, *start, *count);
2363 /* transfer our response to search_nmem .. we need it later */
2364 nmem_transfer(odr_getmem(resultset->odr), nmem);
2366 if (present_phase && p->num_records == 0)
2368 /* present response and we didn't get any records! */
2369 Z_NamePlusRecord *myrec =
2370 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2371 record_cache_add(resultset, myrec, *start,
2372 syntax, elementSetName, 0, 0);
2375 else if (present_phase)
2377 /* present response and we didn't get any records! */
2378 Z_NamePlusRecord *myrec =
2379 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2380 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2386 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2388 handle_records(c, pr->records, 1);
2391 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2394 switch (term->which)
2396 case Z_Term_general:
2397 ZOOM_options_setl(opt, name,
2398 (const char *)(term->u.general->buf),
2399 term->u.general->len);
2401 case Z_Term_characterString:
2402 ZOOM_options_set(opt, name, term->u.characterString);
2404 case Z_Term_numeric:
2405 ZOOM_options_set_int(opt, name, *term->u.numeric);
2410 static void handle_queryExpression(ZOOM_options opt, const char *name,
2411 Z_QueryExpression *exp)
2417 case Z_QueryExpression_term:
2418 if (exp->u.term && exp->u.term->queryTerm)
2420 sprintf(opt_name, "%s.term", name);
2421 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2424 case Z_QueryExpression_query:
2429 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2430 Z_OtherInformation *o)
2433 for (i = 0; o && i < o->num_elements; i++)
2435 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2437 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2439 if (ext->which == Z_External_searchResult1)
2442 Z_SearchInfoReport *sr = ext->u.searchResult1;
2445 ZOOM_options_set_int(
2446 resultset->options, "searchresult.size", sr->num);
2448 for (j = 0; j < sr->num; j++)
2450 Z_SearchInfoReport_s *ent =
2451 ext->u.searchResult1->elements[j];
2454 sprintf(pref, "searchresult.%d", j);
2456 if (ent->subqueryId)
2459 sprintf(opt_name, "%s.id", pref);
2460 ZOOM_options_set(resultset->options, opt_name,
2463 if (ent->subqueryExpression)
2466 sprintf(opt_name, "%s.subquery", pref);
2467 handle_queryExpression(resultset->options, opt_name,
2468 ent->subqueryExpression);
2470 if (ent->subqueryInterpretation)
2473 sprintf(opt_name, "%s.interpretation", pref);
2474 handle_queryExpression(resultset->options, opt_name,
2475 ent->subqueryInterpretation);
2477 if (ent->subqueryRecommendation)
2480 sprintf(opt_name, "%s.recommendation", pref);
2481 handle_queryExpression(resultset->options, opt_name,
2482 ent->subqueryRecommendation);
2484 if (ent->subqueryCount)
2487 sprintf(opt_name, "%s.count", pref);
2488 ZOOM_options_set_int(resultset->options, opt_name,
2489 *ent->subqueryCount);
2497 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2499 ZOOM_resultset resultset;
2502 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2505 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2506 ZOOM_connection_put_event(c, event);
2508 resultset = c->tasks->u.search.resultset;
2510 if (sr->resultSetStatus)
2512 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2513 *sr->resultSetStatus);
2515 if (sr->presentStatus)
2517 ZOOM_options_set_int(resultset->options, "presentStatus",
2518 *sr->presentStatus);
2520 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2522 resultset->size = *sr->resultCount;
2523 handle_records(c, sr->records, 0);
2526 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2528 if (res->diagnostics && res->num_diagnostics > 0)
2529 response_diag(c, res->diagnostics[0]);
2532 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2534 NMEM nmem = odr_extract_mem(c->odr_in);
2537 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2539 scan = c->tasks->u.scan.scan;
2541 if (res->entries && res->entries->nonsurrogateDiagnostics)
2542 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2543 scan->scan_response = res;
2544 scan->srw_scan_response = 0;
2545 nmem_transfer(odr_getmem(scan->odr), nmem);
2547 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2548 if (res->positionOfTerm)
2549 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2550 if (res->scanStatus)
2551 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2552 if (res->numberOfEntriesReturned)
2553 ZOOM_options_set_int(scan->options, "number",
2554 *res->numberOfEntriesReturned);
2559 static zoom_ret send_sort(ZOOM_connection c,
2560 ZOOM_resultset resultset)
2563 resultset->r_sort_spec = 0;
2564 if (resultset->r_sort_spec)
2566 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2567 Z_SortRequest *req = apdu->u.sortRequest;
2569 req->num_inputResultSetNames = 1;
2570 req->inputResultSetNames = (Z_InternationalString **)
2571 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2572 req->inputResultSetNames[0] =
2573 odr_strdup(c->odr_out, resultset->setname);
2574 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2575 req->sortSequence = resultset->r_sort_spec;
2576 resultset->r_sort_spec = 0;
2577 return send_APDU(c, apdu);
2579 return zoom_complete;
2582 static zoom_ret send_present(ZOOM_connection c)
2585 Z_PresentRequest *req = 0;
2587 const char *syntax = 0;
2588 const char *elementSetName = 0;
2589 ZOOM_resultset resultset;
2594 yaz_log(log_details, "%p send_present no tasks", c);
2595 return zoom_complete;
2598 switch (c->tasks->which)
2600 case ZOOM_TASK_SEARCH:
2601 resultset = c->tasks->u.search.resultset;
2602 start = &c->tasks->u.search.start;
2603 count = &c->tasks->u.search.count;
2604 syntax = c->tasks->u.search.syntax;
2605 elementSetName = c->tasks->u.search.elementSetName;
2607 case ZOOM_TASK_RETRIEVE:
2608 resultset = c->tasks->u.retrieve.resultset;
2609 start = &c->tasks->u.retrieve.start;
2610 count = &c->tasks->u.retrieve.count;
2611 syntax = c->tasks->u.retrieve.syntax;
2612 elementSetName = c->tasks->u.retrieve.elementSetName;
2615 return zoom_complete;
2617 yaz_log(log_details, "%p send_present start=%d count=%d",
2620 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2622 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2625 if (c->error) /* don't continue on error */
2626 return zoom_complete;
2627 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2628 resultset, *start, *count);
2630 for (i = 0; i < *count; i++)
2633 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2638 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2639 ZOOM_connection_put_event(c, event);
2647 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2648 return zoom_complete;
2651 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2652 req = apdu->u.presentRequest;
2655 yaz_log(log_details, "%p send_present skip=%d", c, i);
2657 *req->resultSetStartPoint = *start + 1;
2659 if (resultset->step > 0 && resultset->step < *count)
2660 *req->numberOfRecordsRequested = resultset->step;
2662 *req->numberOfRecordsRequested = *count;
2664 if (*req->numberOfRecordsRequested + *start > resultset->size)
2665 *req->numberOfRecordsRequested = resultset->size - *start;
2666 assert(*req->numberOfRecordsRequested > 0);
2668 if (syntax && *syntax)
2669 req->preferredRecordSyntax =
2670 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2672 if (resultset->schema && *resultset->schema)
2674 Z_RecordComposition *compo = (Z_RecordComposition *)
2675 odr_malloc(c->odr_out, sizeof(*compo));
2677 req->recordComposition = compo;
2678 compo->which = Z_RecordComp_complex;
2679 compo->u.complex = (Z_CompSpec *)
2680 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2681 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2682 odr_malloc(c->odr_out, sizeof(bool_t));
2683 *compo->u.complex->selectAlternativeSyntax = 0;
2685 compo->u.complex->generic = (Z_Specification *)
2686 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2688 compo->u.complex->generic->which = Z_Schema_oid;
2689 compo->u.complex->generic->schema.oid = (Odr_oid *)
2690 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2692 if (!compo->u.complex->generic->schema.oid)
2694 /* OID wasn't a schema! Try record syntax instead. */
2696 compo->u.complex->generic->schema.oid = (Odr_oid *)
2697 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2699 if (elementSetName && *elementSetName)
2701 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2702 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2703 compo->u.complex->generic->elementSpec->which =
2704 Z_ElementSpec_elementSetName;
2705 compo->u.complex->generic->elementSpec->u.elementSetName =
2706 odr_strdup(c->odr_out, elementSetName);
2709 compo->u.complex->generic->elementSpec = 0;
2710 compo->u.complex->num_dbSpecific = 0;
2711 compo->u.complex->dbSpecific = 0;
2712 compo->u.complex->num_recordSyntax = 0;
2713 compo->u.complex->recordSyntax = 0;
2715 else if (elementSetName && *elementSetName)
2717 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2718 odr_malloc(c->odr_out, sizeof(*esn));
2719 Z_RecordComposition *compo = (Z_RecordComposition *)
2720 odr_malloc(c->odr_out, sizeof(*compo));
2722 esn->which = Z_ElementSetNames_generic;
2723 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2724 compo->which = Z_RecordComp_simple;
2725 compo->u.simple = esn;
2726 req->recordComposition = compo;
2728 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2729 return send_APDU(c, apdu);
2732 ZOOM_API(ZOOM_scanset)
2733 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2736 ZOOM_query q = ZOOM_query_create();
2738 ZOOM_query_prefix(q, start);
2740 s = ZOOM_connection_scan1(c, q);
2741 ZOOM_query_destroy(q);
2746 ZOOM_API(ZOOM_scanset)
2747 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2749 ZOOM_scanset scan = 0;
2753 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2754 scan->connection = c;
2755 scan->odr = odr_createmem(ODR_DECODE);
2756 scan->options = ZOOM_options_create_with_parent(c->options);
2758 scan->scan_response = 0;
2759 scan->srw_scan_response = 0;
2763 scan->databaseNames = set_DatabaseNames(c, c->options,
2764 &scan->num_databaseNames,
2769 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2770 task->u.scan.scan = scan;
2775 while (ZOOM_event(1, &c))
2783 ZOOM_scanset_destroy(ZOOM_scanset scan)
2788 if (scan->refcount == 0)
2790 ZOOM_query_destroy(scan->query);
2792 odr_destroy(scan->odr);
2794 ZOOM_options_destroy(scan->options);
2799 static zoom_ret send_package(ZOOM_connection c)
2803 yaz_log(log_details, "%p send_package", c);
2805 return zoom_complete;
2806 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2808 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2809 ZOOM_connection_put_event(c, event);
2811 c->buf_out = c->tasks->u.package->buf_out;
2812 c->len_out = c->tasks->u.package->len_out;
2817 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2820 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2821 Z_ScanRequest *req = apdu->u.scanRequest;
2823 yaz_log(log_details, "%p send_scan", c);
2825 return zoom_complete;
2826 assert (c->tasks->which == ZOOM_TASK_SCAN);
2827 scan = c->tasks->u.scan.scan;
2829 /* Z39.50 scan can only carry RPN */
2830 if (scan->query->z_query->which == Z_Query_type_1 ||
2831 scan->query->z_query->which == Z_Query_type_101)
2833 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2834 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2837 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2840 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2842 yaz_query_charset_convert_rpnquery(
2843 rpn, c->odr_out, cd);
2844 yaz_iconv_close(cd);
2847 req->attributeSet = rpn->attributeSetId;
2848 if (!req->attributeSet)
2849 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2850 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2851 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2853 req->termListAndStartPoint =
2854 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2858 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2859 return zoom_complete;
2864 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2865 return zoom_complete;
2868 *req->numberOfTermsRequested =
2869 ZOOM_options_get_int(scan->options, "number", 10);
2871 req->preferredPositionInResponse =
2872 odr_intdup(c->odr_out,
2873 ZOOM_options_get_int(scan->options, "position", 1));
2876 odr_intdup(c->odr_out,
2877 ZOOM_options_get_int(scan->options, "stepSize", 0));
2879 req->databaseNames = scan->databaseNames;
2880 req->num_databaseNames = scan->num_databaseNames;
2882 return send_APDU(c, apdu);
2886 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2890 const char *option_val = 0;
2893 return zoom_complete;
2894 assert (c->tasks->which == ZOOM_TASK_SCAN);
2895 scan = c->tasks->u.scan.scan;
2897 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2899 /* SRU scan can only carry CQL and PQF */
2900 if (scan->query->z_query->which == Z_Query_type_104)
2902 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2903 sr->u.scan_request->scanClause.cql = scan->query->query_string;
2905 else if (scan->query->z_query->which == Z_Query_type_1
2906 || scan->query->z_query->which == Z_Query_type_101)
2908 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2909 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2913 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2914 return zoom_complete;
2917 sr->u.scan_request->maximumTerms = odr_intdup(
2918 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2920 sr->u.scan_request->responsePosition = odr_intdup(
2921 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2923 option_val = ZOOM_options_get(scan->options, "extraArgs");
2924 yaz_encode_sru_extra(sr, c->odr_out, option_val);
2925 return send_srw(c, sr);
2928 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2930 return zoom_complete;
2936 ZOOM_scanset_size(ZOOM_scanset scan)
2941 if (scan->scan_response && scan->scan_response->entries)
2942 return scan->scan_response->entries->num_entries;
2943 else if (scan->srw_scan_response)
2944 return scan->srw_scan_response->num_terms;
2948 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2950 const char **value_term, size_t *value_len,
2951 const char **disp_term, size_t *disp_len)
2953 size_t noent = ZOOM_scanset_size(scan);
2962 if (pos >= noent || pos < 0)
2964 if (scan->scan_response)
2966 Z_ScanResponse *res = scan->scan_response;
2967 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2969 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2971 *value_term = (const char *) t->term->u.general->buf;
2972 *value_len = t->term->u.general->len;
2975 *disp_term = t->displayTerm;
2976 *disp_len = strlen(*disp_term);
2978 else if (t->term->which == Z_Term_general)
2980 *disp_term = (const char *) t->term->u.general->buf;
2981 *disp_len = t->term->u.general->len;
2983 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2986 if (scan->srw_scan_response)
2988 Z_SRW_scanResponse *res = scan->srw_scan_response;
2989 Z_SRW_scanTerm *t = res->terms + pos;
2992 *value_term = t->value;
2993 *value_len = strlen(*value_term);
2996 *disp_term = t->displayTerm;
2998 *disp_term = t->value;
2999 *disp_len = strlen(*disp_term);
3000 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
3005 ZOOM_API(const char *)
3006 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
3009 const char *value_term = 0;
3010 size_t value_len = 0;
3011 const char *disp_term = 0;
3012 size_t disp_len = 0;
3014 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3015 &disp_term, &disp_len);
3021 ZOOM_API(const char *)
3022 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3025 const char *value_term = 0;
3026 size_t value_len = 0;
3027 const char *disp_term = 0;
3028 size_t disp_len = 0;
3030 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3031 &disp_term, &disp_len);
3037 ZOOM_API(const char *)
3038 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3040 return ZOOM_options_get(scan->options, key);
3044 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3047 ZOOM_options_set(scan->options, key, val);
3050 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3053 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3054 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3056 str = ZOOM_options_get(p->options, "package-name");
3058 req->packageName = odr_strdup(p->odr_out, str);
3060 str = ZOOM_options_get(p->options, "user-id");
3062 req->userId = odr_strdup_null(p->odr_out, str);
3064 req->packageType = odr_oiddup(p->odr_out, oid);
3066 str = ZOOM_options_get(p->options, "function");
3069 if (!strcmp (str, "create"))
3070 *req->function = Z_ExtendedServicesRequest_create;
3071 if (!strcmp (str, "delete"))
3072 *req->function = Z_ExtendedServicesRequest_delete;
3073 if (!strcmp (str, "modify"))
3074 *req->function = Z_ExtendedServicesRequest_modify;
3077 str = ZOOM_options_get(p->options, "waitAction");
3080 if (!strcmp (str, "wait"))
3081 *req->waitAction = Z_ExtendedServicesRequest_wait;
3082 if (!strcmp (str, "waitIfPossible"))
3083 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3084 if (!strcmp (str, "dontWait"))
3085 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3086 if (!strcmp (str, "dontReturnPackage"))
3087 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3092 static const char *ill_array_lookup(void *clientData, const char *idx)
3094 ZOOM_package p = (ZOOM_package) clientData;
3095 return ZOOM_options_get(p->options, idx+4);
3098 static Z_External *encode_ill_request(ZOOM_package p)
3100 ODR out = p->odr_out;
3103 struct ill_get_ctl ctl;
3105 ctl.odr = p->odr_out;
3107 ctl.f = ill_array_lookup;
3109 req = ill_get_ILLRequest(&ctl, "ill", 0);
3111 if (!ill_Request(out, &req, 0, 0))
3113 int ill_request_size;
3114 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3115 if (ill_request_buf)
3116 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3121 int illRequest_size = 0;
3122 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3124 r = (Z_External *) odr_malloc(out, sizeof(*r));
3125 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3126 r->indirect_reference = 0;
3128 r->which = Z_External_single;
3130 r->u.single_ASN1_type =
3131 odr_create_Odr_oct(out,
3132 (unsigned char *)illRequest_buf,
3138 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3140 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3144 req->which = Z_IOItemOrder_esRequest;
3145 req->u.esRequest = (Z_IORequest *)
3146 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3148 /* to keep part ... */
3149 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3150 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3151 req->u.esRequest->toKeep->supplDescription = 0;
3152 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3153 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3155 str = ZOOM_options_get(p->options, "contact-name");
3156 req->u.esRequest->toKeep->contact->name =
3157 odr_strdup_null(p->odr_out, str);
3159 str = ZOOM_options_get(p->options, "contact-phone");
3160 req->u.esRequest->toKeep->contact->phone =
3161 odr_strdup_null(p->odr_out, str);
3163 str = ZOOM_options_get(p->options, "contact-email");
3164 req->u.esRequest->toKeep->contact->email =
3165 odr_strdup_null(p->odr_out, str);
3167 req->u.esRequest->toKeep->addlBilling = 0;
3169 /* not to keep part ... */
3170 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3171 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3173 str = ZOOM_options_get(p->options, "itemorder-setname");
3178 req->u.esRequest->notToKeep->resultSetItem = 0;
3181 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3182 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3184 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3185 odr_strdup(p->odr_out, str);
3186 req->u.esRequest->notToKeep->resultSetItem->item =
3187 (int *) odr_malloc(p->odr_out, sizeof(int));
3189 str = ZOOM_options_get(p->options, "itemorder-item");
3190 *req->u.esRequest->notToKeep->resultSetItem->item =
3191 (str ? atoi(str) : 1);
3194 str = ZOOM_options_getl(p->options, "doc", &len);
3197 req->u.esRequest->notToKeep->itemRequest =
3198 z_ext_record_xml(p->odr_out, str, len);
3201 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3206 Z_APDU *create_admin_package(ZOOM_package p, int type,
3207 Z_ESAdminOriginPartToKeep **toKeepP,
3208 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3210 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3213 Z_ESAdminOriginPartToKeep *toKeep;
3214 Z_ESAdminOriginPartNotToKeep *notToKeep;
3215 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3216 const char *first_db = "Default";
3218 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3223 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3225 r->indirect_reference = 0;
3226 r->which = Z_External_ESAdmin;
3228 r->u.adminService = (Z_Admin *)
3229 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3230 r->u.adminService->which = Z_Admin_esRequest;
3231 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3232 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3234 toKeep = r->u.adminService->u.esRequest->toKeep =
3235 (Z_ESAdminOriginPartToKeep *)
3236 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3237 toKeep->which = type;
3238 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3239 toKeep->u.create = odr_nullval();
3240 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3242 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3243 (Z_ESAdminOriginPartNotToKeep *)
3244 odr_malloc(p->odr_out,
3245 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3246 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3247 notToKeep->u.recordsWillFollow = odr_nullval();
3251 *notToKeepP = notToKeep;
3256 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3258 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3259 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3260 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3262 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3270 req->taskSpecificParameters = ext;
3271 ext->direct_reference = req->packageType;
3272 ext->descriptor = 0;
3273 ext->indirect_reference = 0;
3275 ext->which = Z_External_octet;
3276 ext->u.single_ASN1_type =
3277 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3281 static Z_APDU *create_update_package(ZOOM_package p)
3284 const char *first_db = "Default";
3286 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3287 const char *action = ZOOM_options_get(p->options, "action");
3288 int recordIdOpaque_len;
3289 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3290 &recordIdOpaque_len);
3291 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3293 const char *record_buf = ZOOM_options_getl(p->options, "record",
3295 int recordOpaque_len;
3296 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3298 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3299 const char *version = ZOOM_options_get(p->options, "updateVersion");
3301 const char *correlationInfo_note =
3302 ZOOM_options_get(p->options, "correlationInfo.note");
3303 const char *correlationInfo_id =
3304 ZOOM_options_get(p->options, "correlationInfo.id");
3306 Odr_oid *syntax_oid = 0;
3307 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3313 if (!record_buf && !recordOpaque_buf)
3315 record_buf = "void";
3317 syntax_str = "SUTRS";
3322 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3323 CLASS_RECSYN, syntax_str,
3335 package_oid = yaz_oid_extserv_database_update_first_version;
3336 /* old update does not support specialUpdate */
3338 action = "recordInsert";
3342 action = "specialUpdate";
3343 package_oid = yaz_oid_extserv_database_update_second_version;
3347 action = "specialUpdate";
3348 package_oid = yaz_oid_extserv_database_update;
3354 if (!strcmp(action, "recordInsert"))
3355 action_no = Z_IUOriginPartToKeep_recordInsert;
3356 else if (!strcmp(action, "recordReplace"))
3357 action_no = Z_IUOriginPartToKeep_recordReplace;
3358 else if (!strcmp(action, "recordDelete"))
3359 action_no = Z_IUOriginPartToKeep_recordDelete;
3360 else if (!strcmp(action, "elementUpdate"))
3361 action_no = Z_IUOriginPartToKeep_elementUpdate;
3362 else if (!strcmp(action, "specialUpdate"))
3363 action_no = Z_IUOriginPartToKeep_specialUpdate;
3367 apdu = create_es_package(p, package_oid);
3370 Z_IUOriginPartToKeep *toKeep;
3371 Z_IUSuppliedRecords *notToKeep;
3372 Z_External *r = (Z_External *)
3373 odr_malloc(p->odr_out, sizeof(*r));
3374 const char *elementSetName =
3375 ZOOM_options_get(p->options, "elementSetName");
3377 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3379 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3381 r->which = Z_External_update;
3382 r->indirect_reference = 0;
3383 r->u.update = (Z_IUUpdate *)
3384 odr_malloc(p->odr_out, sizeof(*r->u.update));
3386 r->u.update->which = Z_IUUpdate_esRequest;
3387 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3388 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3389 toKeep = r->u.update->u.esRequest->toKeep =
3390 (Z_IUOriginPartToKeep *)
3391 odr_malloc(p->odr_out, sizeof(*toKeep));
3393 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3396 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3398 toKeep->actionQualifier = 0;
3399 toKeep->action = odr_intdup(p->odr_out, action_no);
3401 notToKeep = r->u.update->u.esRequest->notToKeep =
3402 (Z_IUSuppliedRecords *)
3403 odr_malloc(p->odr_out, sizeof(*notToKeep));
3405 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3406 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3407 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3408 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3409 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3412 notToKeep->elements[0]->u.opaque =
3413 odr_create_Odr_oct(p->odr_out,
3414 (const unsigned char *) recordIdOpaque,
3415 recordIdOpaque_len);
3417 else if (recordIdNumber)
3419 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3421 notToKeep->elements[0]->u.number =
3422 odr_intdup(p->odr_out, atoi(recordIdNumber));
3425 notToKeep->elements[0]->u.opaque = 0;
3426 notToKeep->elements[0]->supplementalId = 0;
3427 if (correlationInfo_note || correlationInfo_id)
3429 Z_IUCorrelationInfo *ci;
3430 ci = notToKeep->elements[0]->correlationInfo =
3431 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3432 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3433 ci->id = correlationInfo_id ?
3434 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3437 notToKeep->elements[0]->correlationInfo = 0;
3438 if (recordOpaque_buf)
3440 notToKeep->elements[0]->record =
3441 z_ext_record_oid_any(p->odr_out, syntax_oid,
3442 recordOpaque_buf, recordOpaque_len);
3446 notToKeep->elements[0]->record =
3447 z_ext_record_oid(p->odr_out, syntax_oid,
3448 record_buf, record_len);
3453 ODR print = odr_createmem(ODR_PRINT);
3455 z_APDU(print, &apdu, 0, 0);
3462 ZOOM_package_send(ZOOM_package p, const char *type)
3469 odr_reset(p->odr_out);
3472 if (!strcmp(type, "itemorder"))
3474 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3477 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3479 r->direct_reference =
3480 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3482 r->which = Z_External_itemOrder;
3483 r->indirect_reference = 0;
3484 r->u.itemOrder = encode_item_order(p);
3486 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3489 else if (!strcmp(type, "create")) /* create database */
3491 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3494 else if (!strcmp(type, "drop")) /* drop database */
3496 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3499 else if (!strcmp(type, "commit")) /* commit changes */
3501 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3504 else if (!strcmp(type, "update")) /* update record(s) */
3506 apdu = create_update_package(p);
3508 else if (!strcmp(type, "xmlupdate"))
3510 apdu = create_xmlupdate_package(p);
3514 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3518 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3519 task->u.package = p;
3520 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3521 p->buf_out = (char *) xmalloc(p->len_out);
3522 memcpy(p->buf_out, buf, p->len_out);
3527 while (ZOOM_event(1, &c))
3534 ZOOM_API(ZOOM_package)
3535 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3537 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3540 p->odr_out = odr_createmem(ODR_ENCODE);
3541 p->options = ZOOM_options_create_with_parent2(options, c->options);
3549 ZOOM_package_destroy(ZOOM_package p)
3554 if (p->refcount == 0)
3556 odr_destroy(p->odr_out);
3559 ZOOM_options_destroy(p->options);
3564 ZOOM_API(const char *)
3565 ZOOM_package_option_get(ZOOM_package p, const char *key)
3567 return ZOOM_options_get(p->options, key);
3570 ZOOM_API(const char *)
3571 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3573 return ZOOM_options_getl(p->options, key, lenp);
3577 ZOOM_package_option_set(ZOOM_package p, const char *key,
3580 ZOOM_options_set(p->options, key, val);
3584 ZOOM_package_option_setl(ZOOM_package p, const char *key,
3585 const char *val, int len)
3587 ZOOM_options_setl(p->options, key, val, len);
3590 static int ZOOM_connection_exec_task(ZOOM_connection c)
3592 ZOOM_task task = c->tasks;
3593 zoom_ret ret = zoom_complete;
3597 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3598 c, task->which, task->running);
3599 if (c->error != ZOOM_ERROR_NONE)
3601 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3602 "removing tasks because of error = %d", c, c->error);
3603 ZOOM_connection_remove_tasks(c);
3608 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3609 "task already running", c);
3613 ret = zoom_complete;
3614 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3616 switch (task->which)
3618 case ZOOM_TASK_SEARCH:
3619 if (c->proto == PROTO_HTTP)
3620 ret = ZOOM_connection_srw_send_search(c);
3622 ret = ZOOM_connection_send_search(c);
3624 case ZOOM_TASK_RETRIEVE:
3625 if (c->proto == PROTO_HTTP)
3626 ret = ZOOM_connection_srw_send_search(c);
3628 ret = send_present(c);
3630 case ZOOM_TASK_CONNECT:
3631 ret = do_connect(c);
3633 case ZOOM_TASK_SCAN:
3634 if (c->proto == PROTO_HTTP)
3635 ret = ZOOM_connection_srw_send_scan(c);
3637 ret = ZOOM_connection_send_scan(c);
3639 case ZOOM_TASK_PACKAGE:
3640 ret = send_package(c);
3642 case ZOOM_TASK_SORT:
3643 c->tasks->u.sort.resultset->r_sort_spec =
3644 c->tasks->u.sort.q->sort_spec;
3645 ret = send_sort(c, c->tasks->u.sort.resultset);
3651 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3652 "remove tasks because no connection exist", c);
3653 ZOOM_connection_remove_tasks(c);
3655 if (ret == zoom_complete)
3657 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3658 "task removed (complete)", c);
3659 ZOOM_connection_remove_task(c);
3662 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3667 static zoom_ret send_sort_present(ZOOM_connection c)
3669 zoom_ret r = zoom_complete;
3671 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3672 r = send_sort(c, c->tasks->u.search.resultset);
3673 if (r == zoom_complete)
3674 r = send_present(c);
3678 static int es_response_taskpackage_update(ZOOM_connection c,
3679 Z_IUUpdateTaskPackage *utp)
3681 if (utp && utp->targetPart)
3683 Z_IUTargetPart *targetPart = utp->targetPart;
3684 switch ( *targetPart->updateStatus ) {
3685 case Z_IUTargetPart_success:
3686 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3688 case Z_IUTargetPart_partial:
3689 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3691 case Z_IUTargetPart_failure:
3692 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3693 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3694 response_diag(c, targetPart->globalDiagnostics[0]);
3697 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3702 static int es_response_taskpackage(ZOOM_connection c,
3703 Z_TaskPackage *taskPackage)
3706 Odr_oct *id = taskPackage->targetReference;
3708 ZOOM_options_setl(c->tasks->u.package->options,
3709 "targetReference", (char*) id->buf, id->len);
3712 switch ( *taskPackage->taskStatus ) {
3713 case Z_TaskPackage_pending:
3714 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3716 case Z_TaskPackage_active:
3717 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3719 case Z_TaskPackage_complete:
3720 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3722 case Z_TaskPackage_aborted:
3723 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3724 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3725 response_diag(c, taskPackage->packageDiagnostics[0]);
3729 // taskSpecificParameters
3730 // NOTE: Only Update implemented, no others.
3731 if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3732 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3733 es_response_taskpackage_update(c, utp);
3739 static int es_response(ZOOM_connection c,
3740 Z_ExtendedServicesResponse *res)
3742 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3744 switch (*res->operationStatus) {
3745 case Z_ExtendedServicesResponse_done:
3746 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3748 case Z_ExtendedServicesResponse_accepted:
3749 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3751 case Z_ExtendedServicesResponse_failure:
3752 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3753 if (res->diagnostics && res->num_diagnostics > 0)
3754 response_diag(c, res->diagnostics[0]);
3757 if (res->taskPackage &&
3758 res->taskPackage->which == Z_External_extendedService)
3760 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3761 es_response_taskpackage(c, taskPackage);
3763 if (res->taskPackage &&
3764 res->taskPackage->which == Z_External_octet)
3766 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3767 ZOOM_options_setl(c->tasks->u.package->options,
3768 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3773 static void interpret_init_diag(ZOOM_connection c,
3774 Z_DiagnosticFormat *diag)
3778 Z_DiagnosticFormat_s *ds = diag->elements[0];
3779 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3780 response_default_diag(c, ds->u.defaultDiagRec);
3785 static void interpret_otherinformation_field(ZOOM_connection c,
3786 Z_OtherInformation *ui)
3789 for (i = 0; i < ui->num_elements; i++)
3791 Z_OtherInformationUnit *unit = ui->list[i];
3792 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3793 unit->information.externallyDefinedInfo &&
3794 unit->information.externallyDefinedInfo->which ==
3797 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3803 static void set_init_option(const char *name, void *clientData) {
3804 ZOOM_connection c = (ZOOM_connection) clientData;
3807 sprintf(buf, "init_opt_%.70s", name);
3808 ZOOM_connection_option_set(c, buf, "1");
3812 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3814 Z_InitResponse *initrs;
3816 ZOOM_connection_set_mask(c, 0);
3817 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3820 case Z_APDU_initResponse:
3821 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3822 initrs = apdu->u.initResponse;
3823 ZOOM_connection_option_set(c, "serverImplementationId",
3824 initrs->implementationId ?
3825 initrs->implementationId : "");
3826 ZOOM_connection_option_set(c, "serverImplementationName",
3827 initrs->implementationName ?
3828 initrs->implementationName : "");
3829 ZOOM_connection_option_set(c, "serverImplementationVersion",
3830 initrs->implementationVersion ?
3831 initrs->implementationVersion : "");
3832 /* Set the three old options too, for old applications */
3833 ZOOM_connection_option_set(c, "targetImplementationId",
3834 initrs->implementationId ?
3835 initrs->implementationId : "");
3836 ZOOM_connection_option_set(c, "targetImplementationName",
3837 initrs->implementationName ?
3838 initrs->implementationName : "");
3839 ZOOM_connection_option_set(c, "targetImplementationVersion",
3840 initrs->implementationVersion ?
3841 initrs->implementationVersion : "");
3843 /* Make initrs->options available as ZOOM-level options */
3844 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3846 if (!*initrs->result)
3848 Z_External *uif = initrs->userInformationField;
3850 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3852 if (uif && uif->which == Z_External_userInfo1)
3853 interpret_otherinformation_field(c, uif->u.userInfo1);
3858 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3859 yaz_oid_userinfo_cookie, 1, 0);
3860 xfree(c->cookie_in);
3863 c->cookie_in = xstrdup(cookie);
3864 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3865 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3866 c->support_named_resultsets = 1;
3869 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3870 ZOOM_connection_remove_task(c);
3872 ZOOM_connection_exec_task(c);
3874 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3876 NMEM tmpmem = nmem_create();
3877 Z_CharSetandLanguageNegotiation *p =
3878 yaz_get_charneg_record(initrs->otherInfo);
3882 char *charset = NULL, *lang = NULL;
3885 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3886 yaz_log(log_details, "%p recv_apdu target accepted: "
3887 "charset %s, language %s, select %d",
3889 charset ? charset : "none", lang ? lang : "none", sel);
3891 ZOOM_connection_option_set(c, "negotiation-charset",
3894 ZOOM_connection_option_set(c, "negotiation-lang",
3897 ZOOM_connection_option_set(
3898 c, "negotiation-charset-in-effect-for-records",
3899 (sel != 0) ? "1" : "0");
3900 nmem_destroy(tmpmem);
3904 case Z_APDU_searchResponse:
3905 yaz_log(log_api, "%p recv_apdu Search response", c);
3906 handle_search_response(c, apdu->u.searchResponse);
3907 if (send_sort_present(c) == zoom_complete)
3908 ZOOM_connection_remove_task(c);
3910 case Z_APDU_presentResponse:
3911 yaz_log(log_api, "%p recv_apdu Present response", c);
3912 handle_present_response(c, apdu->u.presentResponse);
3913 if (send_present(c) == zoom_complete)
3914 ZOOM_connection_remove_task(c);
3916 case Z_APDU_sortResponse:
3917 yaz_log(log_api, "%p recv_apdu Sort response", c);
3918 sort_response(c, apdu->u.sortResponse);
3919 if (send_present(c) == zoom_complete)
3920 ZOOM_connection_remove_task(c);
3922 case Z_APDU_scanResponse:
3923 yaz_log(log_api, "%p recv_apdu Scan response", c);
3924 scan_response(c, apdu->u.scanResponse);
3925 ZOOM_connection_remove_task(c);
3927 case Z_APDU_extendedServicesResponse:
3928 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3929 es_response(c, apdu->u.extendedServicesResponse);
3930 ZOOM_connection_remove_task(c);
3933 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3934 if (!ZOOM_test_reconnect(c))
3936 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3941 yaz_log(log_api, "%p Received unknown PDU", c);
3942 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3948 static zoom_ret handle_srw_response(ZOOM_connection c,
3949 Z_SRW_searchRetrieveResponse *res)
3951 ZOOM_resultset resultset = 0;
3956 const char *syntax, *elementSetName;
3959 return zoom_complete;
3961 switch(c->tasks->which)
3963 case ZOOM_TASK_SEARCH:
3964 resultset = c->tasks->u.search.resultset;
3965 start = &c->tasks->u.search.start;
3966 count = &c->tasks->u.search.count;
3967 syntax = c->tasks->u.search.syntax;
3968 elementSetName = c->tasks->u.search.elementSetName;
3970 case ZOOM_TASK_RETRIEVE:
3971 resultset = c->tasks->u.retrieve.resultset;
3972 start = &c->tasks->u.retrieve.start;
3973 count = &c->tasks->u.retrieve.count;
3974 syntax = c->tasks->u.retrieve.syntax;
3975 elementSetName = c->tasks->u.retrieve.elementSetName;
3978 return zoom_complete;
3980 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3981 ZOOM_connection_put_event(c, event);
3983 resultset->size = 0;
3985 if (res->resultSetId)
3986 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
3988 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
3990 if (res->numberOfRecords)
3991 resultset->size = *res->numberOfRecords;
3993 for (i = 0; i<res->num_records; i++)
3996 Z_SRW_record *sru_rec;
3997 Z_SRW_diagnostic *diag = 0;
4000 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
4001 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
4003 if (res->records[i].recordPosition &&
4004 *res->records[i].recordPosition > 0)
4005 pos = *res->records[i].recordPosition - 1;
4009 sru_rec = &res->records[i];
4011 npr->databaseName = 0;
4012 npr->which = Z_NamePlusRecord_databaseRecord;
4013 npr->u.databaseRecord = (Z_External *)
4014 odr_malloc(c->odr_in, sizeof(Z_External));
4015 npr->u.databaseRecord->descriptor = 0;
4016 npr->u.databaseRecord->direct_reference =
4017 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
4018 npr->u.databaseRecord->which = Z_External_octet;
4020 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
4021 odr_malloc(c->odr_in, sizeof(Odr_oct));
4022 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
4023 sru_rec->recordData_buf;
4024 npr->u.databaseRecord->u.octet_aligned->len =
4025 npr->u.databaseRecord->u.octet_aligned->size =
4026 sru_rec->recordData_len;
4028 if (sru_rec->recordSchema
4029 && !strcmp(sru_rec->recordSchema,
4030 "info:srw/schema/1/diagnostics-v1.1"))
4032 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
4033 sru_rec->recordData_len,
4037 record_cache_add(resultset, npr, pos, syntax, elementSetName,
4038 sru_rec->recordSchema, diag);
4042 if (*count + *start > resultset->size)
4043 *count = resultset->size - *start;
4047 nmem = odr_extract_mem(c->odr_in);
4048 nmem_transfer(odr_getmem(resultset->odr), nmem);
4051 if (res->num_diagnostics > 0)
4052 set_SRU_error(c, &res->diagnostics[0]);
4053 else if (*count > 0)
4054 return ZOOM_connection_srw_send_search(c);
4055 return zoom_complete;
4060 static void handle_srw_scan_response(ZOOM_connection c,
4061 Z_SRW_scanResponse *res)
4063 NMEM nmem = odr_extract_mem(c->odr_in);
4066 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4068 scan = c->tasks->u.scan.scan;
4070 if (res->num_diagnostics > 0)
4071 set_SRU_error(c, &res->diagnostics[0]);
4073 scan->scan_response = 0;
4074 scan->srw_scan_response = res;
4075 nmem_transfer(odr_getmem(scan->odr), nmem);
4077 ZOOM_options_set_int(scan->options, "number", res->num_terms);
4083 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4085 zoom_ret cret = zoom_complete;
4087 const char *addinfo = 0;
4088 const char *connection_head = z_HTTP_header_lookup(hres->headers,
4090 ZOOM_connection_set_mask(c, 0);
4091 yaz_log(log_details, "%p handle_http", c);
4093 if (!yaz_srw_check_content_type(hres))
4094 addinfo = "content-type";
4097 Z_SOAP *soap_package = 0;
4099 Z_SOAP_Handler soap_handlers[2] = {
4100 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4103 ret = z_soap_codec(o, &soap_package,
4104 &hres->content_buf, &hres->content_len,
4106 if (!ret && soap_package->which == Z_SOAP_generic &&
4107 soap_package->u.generic->no == 0)
4109 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4111 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4112 if (sr->which == Z_SRW_searchRetrieve_response)
4113 cret = handle_srw_response(c, sr->u.response);
4114 else if (sr->which == Z_SRW_scan_response)
4115 handle_srw_scan_response(c, sr->u.scan_response);
4119 else if (!ret && (soap_package->which == Z_SOAP_fault
4120 || soap_package->which == Z_SOAP_error))
4122 set_HTTP_error(c, hres->code,
4123 soap_package->u.fault->fault_code,
4124 soap_package->u.fault->fault_string);
4131 if (hres->code != 200)
4132 set_HTTP_error(c, hres->code, 0, 0);
4134 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4137 if (cret == zoom_complete)
4138 ZOOM_connection_remove_task(c);
4139 if (!strcmp(hres->version, "1.0"))
4141 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4142 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4147 /* HTTP 1.1: only if no close we stay alive .. */
4148 if (connection_head && !strcmp(connection_head, "close"))
4154 static int do_read(ZOOM_connection c)
4159 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4160 ZOOM_connection_put_event(c, event);
4162 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4163 more = cs_more(c->cs);
4164 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4169 if (!ZOOM_test_reconnect(c))
4171 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4180 odr_reset(c->odr_in);
4181 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4182 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4183 ZOOM_connection_put_event(c, event);
4185 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4188 int err = odr_geterrorx(c->odr_in, &x);
4190 const char *element = odr_getelement(c->odr_in);
4191 yaz_snprintf(msg, sizeof(msg),
4192 "ODR code %d:%d element=%s offset=%d",
4193 err, x, element ? element : "<unknown>",
4194 odr_offset(c->odr_in));
4195 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4198 FILE *ber_file = yaz_log_file();
4200 odr_dumpBER(ber_file, c->buf_in, r);
4207 z_GDU(c->odr_print, &gdu, 0, 0);
4208 if (gdu->which == Z_GDU_Z3950)
4209 recv_apdu(c, gdu->u.z3950);
4210 else if (gdu->which == Z_GDU_HTTP_Response)
4213 handle_http(c, gdu->u.HTTP_Response);
4215 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4220 c->reconnect_ok = 0;
4225 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4230 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4231 ZOOM_connection_put_event(c, event);
4233 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4234 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4236 yaz_log(log_details, "%p do_write_ex write failed", c);
4237 if (ZOOM_test_reconnect(c))
4239 return zoom_pending;
4241 if (c->state == STATE_CONNECTING)
4242 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4244 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4246 return zoom_complete;
4250 int mask = ZOOM_SELECT_EXCEPT;
4251 if (c->cs->io_pending & CS_WANT_WRITE)
4252 mask += ZOOM_SELECT_WRITE;
4253 if (c->cs->io_pending & CS_WANT_READ)
4254 mask += ZOOM_SELECT_READ;
4255 ZOOM_connection_set_mask(c, mask);
4256 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4261 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4262 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4265 return zoom_pending;
4268 static zoom_ret do_write(ZOOM_connection c)
4270 return do_write_ex(c, c->buf_out, c->len_out);
4274 ZOOM_API(const char *)
4275 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4277 return ZOOM_options_get(c->options, key);
4280 ZOOM_API(const char *)
4281 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4283 return ZOOM_options_getl(c->options, key, lenp);
4287 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4290 ZOOM_options_set(c->options, key, val);
4294 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4295 const char *val, int len)
4297 ZOOM_options_setl(c->options, key, val, len);
4300 ZOOM_API(const char *)
4301 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4303 return ZOOM_options_get(r->options, key);
4307 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4310 ZOOM_options_set(r->options, key, val);
4315 ZOOM_connection_errcode(ZOOM_connection c)
4317 return ZOOM_connection_error(c, 0, 0);
4320 ZOOM_API(const char *)
4321 ZOOM_connection_errmsg(ZOOM_connection c)
4324 ZOOM_connection_error(c, &msg, 0);
4328 ZOOM_API(const char *)
4329 ZOOM_connection_addinfo(ZOOM_connection c)
4331 const char *addinfo;
4332 ZOOM_connection_error(c, 0, &addinfo);
4336 ZOOM_API(const char *)
4337 ZOOM_connection_diagset(ZOOM_connection c)
4339 const char *diagset;
4340 ZOOM_connection_error_x(c, 0, 0, &diagset);
4344 ZOOM_API(const char *)
4345 ZOOM_diag_str(int error)
4349 case ZOOM_ERROR_NONE:
4351 case ZOOM_ERROR_CONNECT:
4352 return "Connect failed";
4353 case ZOOM_ERROR_MEMORY:
4354 return "Out of memory";
4355 case ZOOM_ERROR_ENCODE:
4356 return "Encoding failed";
4357 case ZOOM_ERROR_DECODE:
4358 return "Decoding failed";
4359 case ZOOM_ERROR_CONNECTION_LOST:
4360 return "Connection lost";
4361 case ZOOM_ERROR_INIT:
4362 return "Init rejected";
4363 case ZOOM_ERROR_INTERNAL:
4364 return "Internal failure";
4365 case ZOOM_ERROR_TIMEOUT:
4367 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4368 return "Unsupported protocol";
4369 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4370 return "Unsupported query type";
4371 case ZOOM_ERROR_INVALID_QUERY:
4372 return "Invalid query";
4373 case ZOOM_ERROR_CQL_PARSE:
4374 return "CQL parsing error";
4375 case ZOOM_ERROR_CQL_TRANSFORM:
4376 return "CQL transformation error";
4377 case ZOOM_ERROR_CCL_CONFIG:
4378 return "CCL configuration error";
4379 case ZOOM_ERROR_CCL_PARSE:
4380 return "CCL parsing error";
4382 return diagbib1_str(error);
4387 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4388 const char **addinfo, const char **diagset)
4390 int error = c->error;
4393 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4394 *cp = ZOOM_diag_str(error);
4395 else if (!strcmp(c->diagset, "HTTP"))
4396 *cp = z_HTTP_errmsg(c->error);
4397 else if (!strcmp(c->diagset, "Bib-1"))
4398 *cp = ZOOM_diag_str(error);
4399 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4400 *cp = yaz_diag_srw_str(c->error);
4402 *cp = "Unknown error and diagnostic set";
4405 *addinfo = c->addinfo ? c->addinfo : "";
4407 *diagset = c->diagset ? c->diagset : "";
4412 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4413 const char **addinfo)
4415 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4418 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4420 ZOOM_Event event = 0;
4421 int r = cs_look(c->cs);
4422 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4427 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4428 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4430 ZOOM_connection_put_event(c, event);
4432 else if (r == CS_CONNECT)
4434 int ret = ret = cs_rcvconnect(c->cs);
4435 yaz_log(log_details, "%p ZOOM_connection_do_io "
4436 "cs_rcvconnect returned %d", c, ret);
4439 int mask = ZOOM_SELECT_EXCEPT;
4440 if (c->cs->io_pending & CS_WANT_WRITE)
4441 mask += ZOOM_SELECT_WRITE;
4442 if (c->cs->io_pending & CS_WANT_READ)
4443 mask += ZOOM_SELECT_READ;
4444 ZOOM_connection_set_mask(c, mask);
4445 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4446 ZOOM_connection_put_event(c, event);
4450 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4451 ZOOM_connection_put_event(c, event);
4453 if (c->proto == PROTO_Z3950)
4454 ZOOM_connection_send_init(c);
4457 /* no init request for SRW .. */
4458 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4459 ZOOM_connection_remove_task(c);
4460 ZOOM_connection_set_mask(c, 0);
4461 ZOOM_connection_exec_task(c);
4463 c->state = STATE_ESTABLISHED;
4467 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4473 if (mask & ZOOM_SELECT_EXCEPT)
4475 if (!ZOOM_test_reconnect(c))
4477 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4482 if (mask & ZOOM_SELECT_READ)
4484 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4490 ZOOM_connection_last_event(ZOOM_connection cs)
4493 return ZOOM_EVENT_NONE;
4494 return cs->last_event;
4498 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4500 WRBUF wrbuf = (WRBUF) client_data;
4501 wrbuf_puts(wrbuf, buf);
4505 * Returns an xmalloc()d string containing RPN that corresponds to the
4506 * CQL passed in. On error, sets the Connection object's error state
4507 * and returns a null pointer.
4508 * ### We could cache CQL parser and/or transformer in Connection.
4510 static char *cql2pqf(ZOOM_connection c, const char *cql)
4514 const char *cqlfile;
4515 cql_transform_t trans;
4518 parser = cql_parser_create();
4519 if ((error = cql_parser_string(parser, cql)) != 0) {
4520 cql_parser_destroy(parser);
4521 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4525 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4528 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4530 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4533 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4534 cqlfile, strerror(errno));
4535 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4539 WRBUF wrbuf_result = wrbuf_alloc();
4540 error = cql_transform(trans, cql_parser_result(parser),
4541 cql2pqf_wrbuf_puts, wrbuf_result);
4544 const char *addinfo;
4545 error = cql_transform_error(trans, &addinfo);
4546 sprintf(buf, "%.200s (addinfo=%.200s)",
4547 cql_strerror(error), addinfo);
4548 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4552 result = xstrdup(wrbuf_cstr(wrbuf_result));
4554 cql_transform_close(trans);
4555 wrbuf_destroy(wrbuf_result);
4557 cql_parser_destroy(parser);
4561 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4565 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4566 /* timeout and this connection was waiting */
4567 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4569 ZOOM_connection_put_event(c, event);
4575 ZOOM_connection_process(ZOOM_connection c)
4581 event = ZOOM_connection_get_event(c);
4584 ZOOM_Event_destroy(event);
4587 ZOOM_connection_exec_task(c);
4588 event = ZOOM_connection_get_event(c);
4591 ZOOM_Event_destroy(event);
4598 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4602 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4604 for (i = 0; i<no; i++)
4606 ZOOM_connection c = cs[i];
4608 if (c && ZOOM_connection_process(c))
4614 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4616 if (c->mask && mask)
4617 ZOOM_connection_do_io(c, mask);
4621 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4624 return cs_fileno(c->cs);
4628 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4636 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4643 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4645 return ZOOM_options_get_int(c->options, "timeout", 30);
4651 * indent-tabs-mode: nil
4653 * vim: shiftwidth=4 tabstop=8 expandtab