1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM C interface.
15 #include <yaz/yaz-util.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/otherinfo.h>
19 #include <yaz/pquery.h>
20 #include <yaz/marcdisp.h>
21 #include <yaz/diagbib1.h>
22 #include <yaz/charneg.h>
27 #include <yaz/query-charset.h>
28 #include <yaz/copy_types.h>
29 #include <yaz/snprintf.h>
31 static int log_api = 0;
32 static int log_details = 0;
39 static void resultset_destroy(ZOOM_resultset r);
40 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c);
41 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
42 static char *cql2pqf(ZOOM_connection c, const char *cql);
44 ZOOM_API(const char *) ZOOM_get_event_str(int event)
46 static const char *ar[] = {
63 * This wrapper is just for logging failed lookups. It would be nicer
64 * if it could cause failure when a lookup fails, but that's hard.
66 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
67 oid_class oid_class, const char *str) {
68 Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
71 yaz_log(YLOG_WARN, "%p OID lookup (%d, '%s') failed",
72 c, (int) oid_class, str);
77 static void initlog(void)
79 static int log_level_initialized = 0;
80 if (!log_level_initialized)
82 log_api = yaz_log_module_level("zoom");
83 log_details = yaz_log_module_level("zoomdetails");
84 log_level_initialized = 1;
88 static ZOOM_Event ZOOM_Event_create(int kind)
90 ZOOM_Event event = (ZOOM_Event) xmalloc(sizeof(*event));
94 yaz_log(log_details, "ZOOM_Event_create(kind=%d)", kind);
98 static void ZOOM_Event_destroy(ZOOM_Event event)
103 static void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
107 c->m_queue_back->prev = event;
108 assert(c->m_queue_front);
112 assert(!c->m_queue_front);
113 c->m_queue_front = event;
115 event->next = c->m_queue_back;
117 c->m_queue_back = event;
120 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
122 ZOOM_Event event = c->m_queue_front;
125 c->last_event = ZOOM_EVENT_NONE;
128 assert(c->m_queue_back);
129 c->m_queue_front = event->prev;
130 if (c->m_queue_front)
132 assert(c->m_queue_back);
133 c->m_queue_front->next = 0;
137 c->last_event = event->kind;
141 static void ZOOM_connection_remove_events(ZOOM_connection c)
144 while ((event = ZOOM_connection_get_event(c)))
145 ZOOM_Event_destroy(event);
148 ZOOM_API(int) ZOOM_connection_peek_event(ZOOM_connection c)
150 ZOOM_Event event = c->m_queue_front;
152 return event ? event->kind : ZOOM_EVENT_NONE;
155 void ZOOM_connection_remove_tasks(ZOOM_connection c);
157 static void set_dset_error(ZOOM_connection c, int error,
159 const char *addinfo, const char *addinfo2)
166 if (!c->diagset || strcmp(dset, c->diagset))
169 c->diagset = xstrdup(dset);
170 /* remove integer part from SRW diagset .. */
171 if ((cp = strrchr(c->diagset, '/')))
174 if (addinfo && addinfo2)
176 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
177 strcpy(c->addinfo, addinfo);
178 strcat(c->addinfo, addinfo2);
181 c->addinfo = xstrdup(addinfo);
182 if (error != ZOOM_ERROR_NONE)
184 yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s",
185 c, c->host_port ? c->host_port : "<>", dset, error,
186 addinfo ? addinfo : "",
187 addinfo2 ? addinfo2 : "");
188 ZOOM_connection_remove_tasks(c);
192 static int uri_to_code(const char *uri)
196 if ((cp = strrchr(uri, '/')))
202 static void set_HTTP_error(ZOOM_connection c, int error,
203 const char *addinfo, const char *addinfo2)
205 set_dset_error(c, error, "HTTP", addinfo, addinfo2);
208 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
210 const char *uri = d->uri;
212 set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
218 static void set_ZOOM_error(ZOOM_connection c, int error,
221 set_dset_error(c, error, "ZOOM", addinfo, 0);
224 static void clear_error(ZOOM_connection c)
227 * If an error is tied to an operation then it's ok to clear: for
228 * example, a diagnostic returned from a search is cleared by a
229 * subsequent search. However, problems such as Connection Lost
230 * or Init Refused are not cleared, because they are not
231 * recoverable: doing another search doesn't help.
234 ZOOM_connection_remove_events(c);
237 case ZOOM_ERROR_CONNECT:
238 case ZOOM_ERROR_MEMORY:
239 case ZOOM_ERROR_DECODE:
240 case ZOOM_ERROR_CONNECTION_LOST:
241 case ZOOM_ERROR_INIT:
242 case ZOOM_ERROR_INTERNAL:
243 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
246 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
250 void ZOOM_connection_show_task(ZOOM_task task)
254 case ZOOM_TASK_SEARCH:
255 yaz_log(YLOG_LOG, "search p=%p", task);
257 case ZOOM_TASK_RETRIEVE:
258 yaz_log(YLOG_LOG, "retrieve p=%p", task);
260 case ZOOM_TASK_CONNECT:
261 yaz_log(YLOG_LOG, "connect p=%p", task);
264 yaz_log(YLOG_LOG, "scan p=%p", task);
269 void ZOOM_connection_show_tasks(ZOOM_connection c)
272 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
273 for (task = c->tasks; task; task = task->next)
274 ZOOM_connection_show_task(task);
277 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
279 ZOOM_task *taskp = &c->tasks;
281 taskp = &(*taskp)->next;
282 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
283 (*taskp)->running = 0;
284 (*taskp)->which = which;
290 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
292 return c->tasks ? 0 : 1;
295 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
297 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
299 task->next = c->tasks;
308 void ZOOM_connection_remove_task(ZOOM_connection c)
310 ZOOM_task task = c->tasks;
314 c->tasks = task->next;
317 case ZOOM_TASK_SEARCH:
318 resultset_destroy(task->u.search.resultset);
319 xfree(task->u.search.syntax);
320 xfree(task->u.search.elementSetName);
322 case ZOOM_TASK_RETRIEVE:
323 resultset_destroy(task->u.retrieve.resultset);
324 xfree(task->u.retrieve.syntax);
325 xfree(task->u.retrieve.elementSetName);
327 case ZOOM_TASK_CONNECT:
330 ZOOM_scanset_destroy(task->u.scan.scan);
332 case ZOOM_TASK_PACKAGE:
333 ZOOM_package_destroy(task->u.package);
336 resultset_destroy(task->u.sort.resultset);
337 ZOOM_query_destroy(task->u.sort.q);
346 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
347 ZOOM_connection_put_event(c, event);
352 static int ZOOM_connection_exec_task(ZOOM_connection c);
354 void ZOOM_connection_remove_tasks(ZOOM_connection c)
357 ZOOM_connection_remove_task(c);
360 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
362 const char *elementSetName);
364 ZOOM_API(ZOOM_connection)
365 ZOOM_connection_create(ZOOM_options options)
367 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
371 yaz_log(log_api, "%p ZOOM_connection_create", c);
373 c->proto = PROTO_Z3950;
375 ZOOM_connection_set_mask(c, 0);
377 c->state = STATE_IDLE;
380 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
387 c->options = ZOOM_options_create_with_parent(options);
393 c->charset = c->lang = 0;
404 c->maximum_record_size = 0;
405 c->preferred_message_size = 0;
407 c->odr_in = odr_createmem(ODR_DECODE);
408 c->odr_out = odr_createmem(ODR_ENCODE);
412 c->support_named_resultsets = 0;
413 c->last_event = ZOOM_EVENT_NONE;
415 c->m_queue_front = 0;
423 /* set database names. Take local databases (if set); otherwise
424 take databases given in ZURL (if set); otherwise use Default */
425 static char **set_DatabaseNames(ZOOM_connection con, ZOOM_options options,
428 char **databaseNames;
429 const char *cp = ZOOM_options_get(options, "databaseName");
431 if ((!cp || !*cp) && con->host_port)
433 if (strncmp(con->host_port, "unix:", 5) == 0)
434 cp = strchr(con->host_port+5, ':');
436 cp = strchr(con->host_port, '/');
442 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
443 return databaseNames;
446 ZOOM_API(ZOOM_connection)
447 ZOOM_connection_new(const char *host, int portnum)
449 ZOOM_connection c = ZOOM_connection_create(0);
451 ZOOM_connection_connect(c, host, portnum);
455 static zoom_sru_mode get_sru_mode_from_string(const char *s)
458 return zoom_sru_soap;
459 if (!yaz_matchstr(s, "soap"))
460 return zoom_sru_soap;
461 else if (!yaz_matchstr(s, "get"))
463 else if (!yaz_matchstr(s, "post"))
464 return zoom_sru_post;
465 return zoom_sru_error;
469 ZOOM_connection_connect(ZOOM_connection c,
470 const char *host, int portnum)
477 yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
478 c, host ? host : "null", portnum);
480 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
481 ZOOM_connection_remove_tasks(c);
485 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
486 odr_destroy(c->odr_print);
488 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
490 c->odr_print = odr_createmem(ODR_PRINT);
491 odr_setprint(c->odr_print, yaz_log_file());
498 yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
502 yaz_log(log_details, "%p ZOOM_connection_connect connect", c);
505 val = ZOOM_options_get(c->options, "proxy");
508 yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
509 c->proxy = xstrdup(val);
514 val = ZOOM_options_get(c->options, "charset");
517 yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val);
518 c->charset = xstrdup(val);
522 val = ZOOM_options_get(c->options, "lang");
525 yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val);
526 c->lang = xstrdup(val);
537 sprintf(hostn, "%.80s:%d", host, portnum);
538 c->host_port = xstrdup(hostn);
541 c->host_port = xstrdup(host);
546 * If the "<scheme>:" part of the host string is preceded by one
547 * or more comma-separated <name>=<value> pairs, these are taken
548 * to be options to be set on the connection object. Among other
549 * applications, this facility can be used to embed authentication
551 * user=admin,password=secret,tcp:localhost:9999
553 char *remainder = c->host_port;
554 char *pcolon = strchr(remainder, ':');
557 while ((pcomma = strchr(remainder, ',')) != 0 &&
558 (pcolon == 0 || pcomma < pcolon)) {
560 if ((pequals = strchr(remainder, '=')) != 0) {
562 /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
563 ZOOM_connection_option_set(c, remainder, pequals+1);
565 remainder = pcomma+1;
568 if (remainder != c->host_port) {
570 c->host_port = xstrdup(remainder);
571 /*printf("# reset hp='%s'\n", remainder);*/
575 val = ZOOM_options_get(c->options, "sru");
576 c->sru_mode = get_sru_mode_from_string(val);
578 xfree(c->sru_version);
579 val = ZOOM_options_get(c->options, "sru_version");
580 c->sru_version = xstrdup(val ? val : "1.2");
582 ZOOM_options_set(c->options, "host", c->host_port);
584 xfree(c->cookie_out);
586 val = ZOOM_options_get(c->options, "cookie");
589 yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
590 c->cookie_out = xstrdup(val);
595 val = ZOOM_options_get(c->options, "clientIP");
598 yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s",
600 c->client_IP = xstrdup(val);
605 val = ZOOM_options_get(c->options, "group");
607 c->group = xstrdup(val);
611 val = ZOOM_options_get(c->options, "user");
613 c->user = xstrdup(val);
617 val = ZOOM_options_get(c->options, "password");
619 val = ZOOM_options_get(c->options, "pass");
622 c->password = xstrdup(val);
624 c->maximum_record_size =
625 ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
626 c->preferred_message_size =
627 ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
629 c->async = ZOOM_options_get_bool(c->options, "async", 0);
630 yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
632 task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
636 while (ZOOM_event(1, &c))
642 ZOOM_query_create(void)
644 ZOOM_query s = (ZOOM_query) xmalloc(sizeof(*s));
646 yaz_log(log_details, "%p ZOOM_query_create", s);
650 s->odr = odr_createmem(ODR_ENCODE);
657 ZOOM_query_destroy(ZOOM_query s)
663 yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount);
664 if (s->refcount == 0)
672 ZOOM_query_prefix(ZOOM_query s, const char *str)
674 s->query_string = odr_strdup(s->odr, str);
675 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
676 s->z_query->which = Z_Query_type_1;
677 s->z_query->u.type_1 = p_query_rpn(s->odr, str);
678 if (!s->z_query->u.type_1)
680 yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str);
684 yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str);
689 ZOOM_query_cql(ZOOM_query s, const char *str)
693 s->query_string = odr_strdup(s->odr, str);
695 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
696 ext->direct_reference = odr_oiddup(s->odr, yaz_oid_userinfo_cql);
697 ext->indirect_reference = 0;
699 ext->which = Z_External_CQL;
700 ext->u.cql = s->query_string;
702 s->z_query = (Z_Query *) odr_malloc(s->odr, sizeof(*s->z_query));
703 s->z_query->which = Z_Query_type_104;
704 s->z_query->u.type_104 = ext;
706 yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str);
712 * Translate the CQL string client-side into RPN which is passed to
713 * the server. This is useful for server's that don't themselves
714 * support CQL, for which ZOOM_query_cql() is useless. `conn' is used
715 * only as a place to stash diagnostics if compilation fails; if this
716 * information is not needed, a null pointer may be used.
719 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn)
723 ZOOM_connection freeme = 0;
725 yaz_log(log_details, "%p ZOOM_query_cql2rpn str=%s conn=%p", s, str, conn);
727 conn = freeme = ZOOM_connection_create(0);
729 rpn = cql2pqf(conn, str);
731 ZOOM_connection_destroy(freeme);
735 ret = ZOOM_query_prefix(s, rpn);
741 * Analogous in every way to ZOOM_query_cql2rpn(), except that there
742 * is no analogous ZOOM_query_ccl() that just sends uninterpreted CCL
743 * to the server, as the YAZ GFS doesn't know how to handle this.
746 ZOOM_query_ccl2rpn(ZOOM_query s, const char *str, const char *config,
747 int *ccl_error, const char **error_string,
751 struct ccl_rpn_node *rpn;
752 CCL_bibset bibset = ccl_qual_mk();
755 ccl_qual_buf(bibset, config);
757 rpn = ccl_find_str(bibset, str, ccl_error, error_pos);
760 *error_string = ccl_err_msg(*ccl_error);
765 WRBUF wr = wrbuf_alloc();
768 ret = ZOOM_query_prefix(s, wrbuf_cstr(wr));
771 ccl_qual_rm(&bibset);
776 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
778 s->sort_spec = yaz_sort_spec(s->odr, criteria);
781 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed",
785 yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria);
789 static zoom_ret do_write(ZOOM_connection c);
792 ZOOM_connection_destroy(ZOOM_connection c)
797 yaz_log(log_api, "%p ZOOM_connection_destroy", c);
800 for (r = c->resultsets; r; r = r->next)
806 odr_destroy(c->odr_in);
807 odr_destroy(c->odr_out);
810 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
811 odr_destroy(c->odr_print);
813 ZOOM_options_destroy(c->options);
814 ZOOM_connection_remove_tasks(c);
815 ZOOM_connection_remove_events(c);
821 xfree(c->cookie_out);
827 xfree(c->sru_version);
831 void ZOOM_resultset_addref(ZOOM_resultset r)
836 yaz_log(log_details, "%p ZOOM_resultset_addref count=%d",
841 ZOOM_resultset ZOOM_resultset_create(void)
844 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
848 yaz_log(log_details, "%p ZOOM_resultset_create", r);
851 r->odr = odr_createmem(ODR_ENCODE);
856 for (i = 0; i<RECORD_HASH_SIZE; i++)
857 r->record_hash[i] = 0;
862 r->databaseNames = 0;
863 r->num_databaseNames = 0;
867 ZOOM_API(ZOOM_resultset)
868 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
871 ZOOM_query s = ZOOM_query_create();
873 ZOOM_query_prefix(s, q);
875 r = ZOOM_connection_search(c, s);
876 ZOOM_query_destroy(s);
880 ZOOM_API(ZOOM_resultset)
881 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
883 ZOOM_resultset r = ZOOM_resultset_create();
887 const char *syntax, *elementSetName;
889 yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
890 r->r_sort_spec = q->sort_spec;
893 r->options = ZOOM_options_create_with_parent(c->options);
895 start = ZOOM_options_get_int(r->options, "start", 0);
896 count = ZOOM_options_get_int(r->options, "count", 0);
898 /* If "presentChunk" is defined use that; otherwise "step" */
899 const char *cp = ZOOM_options_get(r->options, "presentChunk");
900 r->step = ZOOM_options_get_int(r->options,
901 (cp != 0 ? "presentChunk": "step"), 0);
903 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
904 cp = ZOOM_options_get(r->options, "setname");
906 r->setname = xstrdup(cp);
907 cp = ZOOM_options_get(r->options, "schema");
909 r->schema = xstrdup(cp);
911 r->databaseNames = set_DatabaseNames(c, c->options, &r->num_databaseNames,
916 r->next = c->resultsets;
921 if (c->host_port && c->proto == PROTO_HTTP)
925 yaz_log(log_details, "ZOOM_connection_search: no comstack");
926 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
930 yaz_log(log_details, "ZOOM_connection_search: reconnect");
935 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
936 task->u.search.resultset = r;
937 task->u.search.start = start;
938 task->u.search.count = count;
939 task->u.search.recv_search_fired = 0;
941 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
942 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
943 elementSetName = ZOOM_options_get(r->options, "elementSetName");
944 task->u.search.elementSetName = elementSetName
945 ? xstrdup(elementSetName) : 0;
947 ZOOM_resultset_addref(r);
953 while (ZOOM_event(1, &c))
960 ZOOM_resultset_sort(ZOOM_resultset r,
961 const char *sort_type, const char *sort_spec)
963 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
967 ZOOM_resultset_sort1(ZOOM_resultset r,
968 const char *sort_type, const char *sort_spec)
970 ZOOM_connection c = r->connection;
974 newq = ZOOM_query_create();
975 if (ZOOM_query_sortby(newq, sort_spec) < 0)
978 yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
979 r, r, sort_type, sort_spec);
983 if (c->host_port && c->proto == PROTO_HTTP)
987 yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r);
988 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
992 yaz_log(log_details, "%p ZOOM_resultset_sort: prepare reconnect",
998 ZOOM_resultset_cache_reset(r);
999 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
1000 task->u.sort.resultset = r;
1001 task->u.sort.q = newq;
1003 ZOOM_resultset_addref(r);
1007 while (ZOOM_event(1, &c))
1014 static void ZOOM_record_release(ZOOM_record rec)
1019 wrbuf_destroy(rec->wrbuf);
1022 xmlFree(rec->xml_mem);
1025 odr_destroy(rec->odr);
1029 ZOOM_resultset_cache_reset(ZOOM_resultset r)
1032 for (i = 0; i<RECORD_HASH_SIZE; i++)
1034 ZOOM_record_cache rc;
1035 for (rc = r->record_hash[i]; rc; rc = rc->next)
1037 ZOOM_record_release(&rc->rec);
1039 r->record_hash[i] = 0;
1044 ZOOM_resultset_destroy(ZOOM_resultset r)
1046 resultset_destroy(r);
1049 static void resultset_destroy(ZOOM_resultset r)
1054 yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d",
1056 if (r->refcount == 0)
1058 ZOOM_resultset_cache_reset(r);
1062 /* remove ourselves from the resultsets in connection */
1063 ZOOM_resultset *rp = &r->connection->resultsets;
1066 assert(*rp); /* we must be in this list!! */
1068 { /* OK, we're here - take us out of it */
1075 ZOOM_query_destroy(r->query);
1076 ZOOM_options_destroy(r->options);
1077 odr_destroy(r->odr);
1085 ZOOM_resultset_size(ZOOM_resultset r)
1087 yaz_log(log_details, "ZOOM_resultset_size r=%p count=" ODR_INT_PRINTF,
1092 static void do_close(ZOOM_connection c)
1097 ZOOM_connection_set_mask(c, 0);
1098 c->state = STATE_IDLE;
1101 static int ZOOM_test_reconnect(ZOOM_connection c)
1105 if (!c->reconnect_ok)
1108 c->reconnect_ok = 0;
1109 c->tasks->running = 0;
1110 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1112 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1113 ZOOM_connection_put_event(c, event);
1118 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
1119 int force_sync, int start, int count)
1124 const char *syntax, *elementSetName;
1128 yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
1129 " count=%d", r, force_sync, start, count);
1134 if (c->host_port && c->proto == PROTO_HTTP)
1138 yaz_log(log_details, "%p ZOOM_resultset_retrieve: no comstack", r);
1139 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1143 yaz_log(log_details, "%p ZOOM_resultset_retrieve: prepare "
1145 c->reconnect_ok = 1;
1148 task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
1149 task->u.retrieve.resultset = r;
1150 task->u.retrieve.start = start;
1151 task->u.retrieve.count = count;
1153 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1154 task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
1155 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1156 task->u.retrieve.elementSetName = elementSetName
1157 ? xstrdup(elementSetName) : 0;
1159 cp = ZOOM_options_get(r->options, "schema");
1162 if (!r->schema || strcmp(r->schema, cp))
1165 r->schema = xstrdup(cp);
1169 ZOOM_resultset_addref(r);
1171 if (!r->connection->async || force_sync)
1172 while (r->connection && ZOOM_event(1, &r->connection))
1177 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1178 size_t start, size_t count)
1180 int force_present = 0;
1184 yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1185 r, r, (long) start, (long) count);
1188 ZOOM_resultset_retrieve(r, force_present, start, count);
1192 for (i = 0; i< count; i++)
1193 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1197 static void get_cert(ZOOM_connection c)
1202 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1204 ZOOM_connection_option_setl(c, "sslPeerCert",
1205 cert_buf, cert_len);
1210 static zoom_ret do_connect(ZOOM_connection c)
1213 const char *effective_host;
1216 effective_host = c->proxy;
1218 effective_host = c->host_port;
1220 yaz_log(log_details, "%p do_connect effective_host=%s", c, effective_host);
1224 c->cs = cs_create_host(effective_host, 0, &add);
1226 if (c->cs && c->cs->protocol == PROTO_HTTP)
1231 c->proto = PROTO_HTTP;
1232 cs_get_host_args(c->host_port, &db);
1235 c->path = xmalloc(strlen(db) * 3 + 2);
1236 yaz_encode_sru_dbpath_buf(c->path, db);
1238 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1240 return zoom_complete;
1245 int ret = cs_connect(c->cs, add);
1248 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1249 ZOOM_connection_put_event(c, event);
1251 if (c->proto == PROTO_Z3950)
1252 ZOOM_connection_send_init(c);
1255 /* no init request for SRW .. */
1256 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1257 ZOOM_connection_remove_task(c);
1258 ZOOM_connection_set_mask(c, 0);
1259 ZOOM_connection_exec_task(c);
1261 c->state = STATE_ESTABLISHED;
1262 return zoom_pending;
1266 int mask = ZOOM_SELECT_EXCEPT;
1267 if (c->cs->io_pending & CS_WANT_WRITE)
1268 mask += ZOOM_SELECT_WRITE;
1269 if (c->cs->io_pending & CS_WANT_READ)
1270 mask += ZOOM_SELECT_READ;
1271 ZOOM_connection_set_mask(c, mask);
1272 c->state = STATE_CONNECTING;
1273 return zoom_pending;
1276 c->state = STATE_IDLE;
1277 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1278 return zoom_complete;
1281 static void otherInfo_attach(ZOOM_connection c, Z_APDU *a, ODR out)
1284 for (i = 0; i<200; i++)
1288 Z_OtherInformation **oi;
1293 sprintf(buf, "otherInfo%d", i);
1294 val = ZOOM_options_get(c->options, buf);
1297 cp = strchr(val, ':');
1301 if (len >= sizeof(buf))
1302 len = sizeof(buf)-1;
1303 memcpy(buf, val, len);
1306 oid = yaz_string_to_oid_odr(yaz_oid_std(), CLASS_USERINFO,
1311 yaz_oi_APDU(a, &oi);
1312 yaz_oi_set_string_oid(oi, out, oid, 1, cp+1);
1316 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
1321 Z_OtherInformation **oi;
1322 yaz_oi_APDU(a, &oi);
1323 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_cookie,
1328 Z_OtherInformation **oi;
1329 yaz_oi_APDU(a, &oi);
1330 yaz_oi_set_string_oid(oi, out, yaz_oid_userinfo_client_ip,
1333 otherInfo_attach(c, a, out);
1334 if (!z_APDU(out, &a, 0, 0))
1336 FILE *outf = fopen("/tmp/apdu.txt", "a");
1339 ODR odr_pr = odr_createmem(ODR_PRINT);
1340 fprintf(outf, "a=%p\n", a);
1341 odr_setprint(odr_pr, outf);
1342 z_APDU(odr_pr, &a, 0, 0);
1343 odr_destroy(odr_pr);
1345 yaz_log(log_api, "%p encoding_APDU: encoding failed", c);
1346 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
1351 z_APDU(c->odr_print, &a, 0, 0);
1352 yaz_log(log_details, "%p encoding_APDU encoding OK", c);
1356 static zoom_ret send_APDU(ZOOM_connection c, Z_APDU *a)
1360 if (encode_APDU(c, a, c->odr_out))
1361 return zoom_complete;
1362 yaz_log(log_details, "%p send APDU type=%d", c, a->which);
1363 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1364 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1365 ZOOM_connection_put_event(c, event);
1366 odr_reset(c->odr_out);
1370 /* returns 1 if PDU was sent OK (still pending )
1371 0 if PDU was not sent OK (nothing to wait for)
1374 static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
1376 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
1377 Z_InitRequest *ireq = apdu->u.initRequest;
1378 Z_IdAuthentication *auth = (Z_IdAuthentication *)
1379 odr_malloc(c->odr_out, sizeof(*auth));
1381 ODR_MASK_SET(ireq->options, Z_Options_search);
1382 ODR_MASK_SET(ireq->options, Z_Options_present);
1383 ODR_MASK_SET(ireq->options, Z_Options_scan);
1384 ODR_MASK_SET(ireq->options, Z_Options_sort);
1385 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
1386 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
1388 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
1389 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
1390 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
1392 ireq->implementationId =
1393 odr_prepend(c->odr_out,
1394 ZOOM_options_get(c->options, "implementationId"),
1395 ireq->implementationId);
1397 ireq->implementationName =
1398 odr_prepend(c->odr_out,
1399 ZOOM_options_get(c->options, "implementationName"),
1400 odr_prepend(c->odr_out, "ZOOM-C",
1401 ireq->implementationName));
1403 ireq->implementationVersion =
1404 odr_prepend(c->odr_out,
1405 ZOOM_options_get(c->options, "implementationVersion"),
1406 ireq->implementationVersion);
1408 *ireq->maximumRecordSize = c->maximum_record_size;
1409 *ireq->preferredMessageSize = c->preferred_message_size;
1411 if (c->group || c->password)
1413 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
1414 pass->groupId = odr_strdup_null(c->odr_out, c->group);
1415 pass->userId = odr_strdup_null(c->odr_out, c->user);
1416 pass->password = odr_strdup_null(c->odr_out, c->password);
1417 auth->which = Z_IdAuthentication_idPass;
1418 auth->u.idPass = pass;
1419 ireq->idAuthentication = auth;
1423 auth->which = Z_IdAuthentication_open;
1424 auth->u.open = odr_strdup(c->odr_out, c->user);
1425 ireq->idAuthentication = auth;
1429 yaz_oi_set_string_oid(&ireq->otherInfo, c->odr_out,
1430 yaz_oid_userinfo_proxy, 1, c->host_port);
1432 if (c->charset || c->lang)
1434 Z_OtherInformation **oi;
1435 Z_OtherInformationUnit *oi_unit;
1437 yaz_oi_APDU(apdu, &oi);
1439 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
1441 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
1442 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
1443 oi_unit->information.externallyDefinedInfo =
1444 yaz_set_proposal_charneg_list(c->odr_out, " ",
1445 c->charset, c->lang, 1);
1449 return send_APDU(c, apdu);
1453 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
1457 const char *database = ZOOM_options_get(c->options, "databaseName");
1458 char *fdatabase = 0;
1461 fdatabase = yaz_encode_sru_dbpath_odr(c->odr_out, database);
1462 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
1463 fdatabase ? fdatabase : c->path);
1465 if (c->sru_mode == zoom_sru_get)
1467 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1469 else if (c->sru_mode == zoom_sru_post)
1471 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1473 else if (c->sru_mode == zoom_sru_soap)
1475 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
1477 if (!z_GDU(c->odr_out, &gdu, 0, 0))
1478 return zoom_complete;
1480 z_GDU(c->odr_print, &gdu, 0, 0);
1481 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1483 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1484 ZOOM_connection_put_event(c, event);
1485 odr_reset(c->odr_out);
1491 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
1493 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
1494 sr->username = c->user;
1495 sr->password = c->password;
1501 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1505 ZOOM_resultset resultset = 0;
1507 const char *option_val = 0;
1509 if (c->error) /* don't continue on error */
1510 return zoom_complete;
1512 switch(c->tasks->which)
1514 case ZOOM_TASK_SEARCH:
1515 resultset = c->tasks->u.search.resultset;
1516 if (!resultset->setname)
1517 resultset->setname = xstrdup("default");
1518 ZOOM_options_set(resultset->options, "setname", resultset->setname);
1519 start = &c->tasks->u.search.start;
1520 count = &c->tasks->u.search.count;
1522 case ZOOM_TASK_RETRIEVE:
1523 resultset = c->tasks->u.retrieve.resultset;
1525 start = &c->tasks->u.retrieve.start;
1526 count = &c->tasks->u.retrieve.count;
1528 if (*start >= resultset->size)
1529 return zoom_complete;
1530 if (*start + *count > resultset->size)
1531 *count = resultset->size - *start;
1533 for (i = 0; i < *count; i++)
1536 record_cache_lookup(resultset, i + *start,
1537 c->tasks->u.retrieve.syntax,
1538 c->tasks->u.retrieve.elementSetName);
1543 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
1544 ZOOM_connection_put_event(c, event);
1551 return zoom_complete;
1554 return zoom_complete;
1556 assert(resultset->query);
1558 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
1559 if (resultset->query->z_query->which == Z_Query_type_104
1560 && resultset->query->z_query->u.type_104->which == Z_External_CQL)
1562 sr->u.request->query_type = Z_SRW_query_type_cql;
1563 sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
1565 else if (resultset->query->z_query->which == Z_Query_type_1 &&
1566 resultset->query->z_query->u.type_1)
1568 sr->u.request->query_type = Z_SRW_query_type_pqf;
1569 sr->u.request->query.pqf = resultset->query->query_string;
1573 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
1574 return zoom_complete;
1576 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
1577 sr->u.request->maximumRecords = odr_intdup(
1578 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
1579 resultset->step : *count);
1580 sr->u.request->recordSchema = resultset->schema;
1582 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
1584 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
1586 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
1587 yaz_encode_sru_extra(sr, c->odr_out, option_val);
1588 return send_srw(c, sr);
1591 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
1593 return zoom_complete;
1597 static zoom_ret ZOOM_connection_send_search(ZOOM_connection c)
1600 int lslb, ssub, mspn;
1602 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
1603 Z_SearchRequest *search_req = apdu->u.searchRequest;
1604 const char *elementSetName;
1605 const char *smallSetElementSetName;
1606 const char *mediumSetElementSetName;
1609 assert(c->tasks->which == ZOOM_TASK_SEARCH);
1611 r = c->tasks->u.search.resultset;
1613 yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r);
1616 ZOOM_options_get(r->options, "elementSetName");
1617 smallSetElementSetName =
1618 ZOOM_options_get(r->options, "smallSetElementSetName");
1619 mediumSetElementSetName =
1620 ZOOM_options_get(r->options, "mediumSetElementSetName");
1622 if (!smallSetElementSetName)
1623 smallSetElementSetName = elementSetName;
1625 if (!mediumSetElementSetName)
1626 mediumSetElementSetName = elementSetName;
1631 /* prepare query for the search request */
1632 search_req->query = r->query->z_query;
1633 if (!search_req->query)
1635 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1636 return zoom_complete;
1638 if (r->query->z_query->which == Z_Query_type_1 ||
1639 r->query->z_query->which == Z_Query_type_101)
1641 const char *cp = ZOOM_options_get(r->options, "rpnCharset");
1644 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
1648 search_req->query = yaz_copy_Z_Query(search_req->query,
1651 r = yaz_query_charset_convert_rpnquery_check(
1652 search_req->query->u.type_1,
1654 yaz_iconv_close(cd);
1656 { /* query could not be char converted */
1657 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
1658 return zoom_complete;
1663 search_req->databaseNames = r->databaseNames;
1664 search_req->num_databaseNames = r->num_databaseNames;
1666 /* get syntax (no need to provide unless piggyback is in effect) */
1667 syntax = c->tasks->u.search.syntax;
1669 lslb = ZOOM_options_get_int(r->options, "largeSetLowerBound", -1);
1670 ssub = ZOOM_options_get_int(r->options, "smallSetUpperBound", -1);
1671 mspn = ZOOM_options_get_int(r->options, "mediumSetPresentNumber", -1);
1672 if (lslb != -1 && ssub != -1 && mspn != -1)
1674 /* So're a Z39.50 expert? Let's hope you don't do sort */
1675 *search_req->largeSetLowerBound = lslb;
1676 *search_req->smallSetUpperBound = ssub;
1677 *search_req->mediumSetPresentNumber = mspn;
1679 else if (c->tasks->u.search.start == 0 && c->tasks->u.search.count > 0
1680 && r->piggyback && !r->r_sort_spec && !r->schema)
1682 /* Regular piggyback - do it unless we're going to do sort */
1683 *search_req->largeSetLowerBound = 2000000000;
1684 *search_req->smallSetUpperBound = 1;
1685 *search_req->mediumSetPresentNumber =
1686 r->step>0 ? r->step : c->tasks->u.search.count;
1690 /* non-piggyback. Need not provide elementsets or syntaxes .. */
1691 smallSetElementSetName = 0;
1692 mediumSetElementSetName = 0;
1695 if (smallSetElementSetName && *smallSetElementSetName)
1697 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1698 odr_malloc(c->odr_out, sizeof(*esn));
1700 esn->which = Z_ElementSetNames_generic;
1701 esn->u.generic = odr_strdup(c->odr_out, smallSetElementSetName);
1702 search_req->smallSetElementSetNames = esn;
1704 if (mediumSetElementSetName && *mediumSetElementSetName)
1706 Z_ElementSetNames *esn =(Z_ElementSetNames *)
1707 odr_malloc(c->odr_out, sizeof(*esn));
1709 esn->which = Z_ElementSetNames_generic;
1710 esn->u.generic = odr_strdup(c->odr_out, mediumSetElementSetName);
1711 search_req->mediumSetElementSetNames = esn;
1714 search_req->preferredRecordSyntax =
1715 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
1719 if (c->support_named_resultsets)
1723 /* find the lowest unused ordinal so that we re-use
1724 result sets on the server. */
1725 for (ord = 1; ; ord++)
1728 sprintf(setname, "%d", ord);
1729 for (rp = c->resultsets; rp; rp = rp->next)
1730 if (rp->setname && !strcmp(rp->setname, setname))
1735 r->setname = xstrdup(setname);
1736 yaz_log(log_details, "%p ZOOM_connection_send_search: allocating "
1737 "set %s", c, r->setname);
1741 yaz_log(log_details, "%p ZOOM_connection_send_search: using "
1743 r->setname = xstrdup("default");
1745 ZOOM_options_set(r->options, "setname", r->setname);
1747 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1748 return send_APDU(c, apdu);
1751 static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r)
1753 char oid_name_buf[OID_STR_MAX];
1754 const char *oid_name;
1757 oid_name = yaz_oid_to_string_buf(r->diagnosticSetId, 0, oid_name_buf);
1760 case Z_DefaultDiagFormat_v2Addinfo:
1761 addinfo = r->u.v2Addinfo;
1763 case Z_DefaultDiagFormat_v3Addinfo:
1764 addinfo = r->u.v3Addinfo;
1769 set_dset_error(c, *r->condition, oid_name, addinfo, 0);
1772 static void response_diag(ZOOM_connection c, Z_DiagRec *p)
1774 if (p->which != Z_DiagRec_defaultFormat)
1775 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1777 response_default_diag(c, p->u.defaultFormat);
1780 ZOOM_API(ZOOM_record)
1781 ZOOM_record_clone(ZOOM_record srec)
1788 odr_enc = odr_createmem(ODR_ENCODE);
1789 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
1791 buf = odr_getbuf(odr_enc, &size, 0);
1793 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
1794 nrec->odr = odr_createmem(ODR_DECODE);
1800 odr_setbuf(nrec->odr, buf, size, 0);
1801 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
1803 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
1804 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
1805 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
1806 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
1807 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
1808 odr_destroy(odr_enc);
1812 ZOOM_API(ZOOM_record)
1813 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1815 const char *syntax =
1816 ZOOM_options_get(s->options, "preferredRecordSyntax");
1817 const char *elementSetName =
1818 ZOOM_options_get(s->options, "elementSetName");
1820 return record_cache_lookup(s, pos, syntax, elementSetName);
1823 ZOOM_API(ZOOM_record)
1824 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1826 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1831 * MIKE: I think force_sync should always be zero, but I don't
1832 * want to make this change until I get the go-ahead from
1833 * Adam, in case something depends on the old synchronous
1837 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1838 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1839 rec = ZOOM_resultset_record_immediate(r, pos);
1845 ZOOM_record_destroy(ZOOM_record rec)
1847 ZOOM_record_release(rec);
1852 static yaz_iconv_t iconv_create_charset(const char *record_charset)
1859 strcpy(to, "UTF-8");
1860 if (record_charset && *record_charset)
1862 /* Use "from,to" or just "from" */
1863 const char *cp = strchr(record_charset, ',');
1864 size_t clen = strlen(record_charset);
1867 strncpy( to, cp+1, sizeof(to)-1);
1868 to[sizeof(to)-1] = '\0';
1869 clen = cp - record_charset;
1871 if (clen > sizeof(from)-1)
1872 clen = sizeof(from)-1;
1875 strncpy(from, record_charset, clen);
1879 cd = yaz_iconv_open(to, from);
1883 static const char *return_marc_record(ZOOM_record rec, int marc_type,
1885 const char *buf, int sz,
1886 const char *record_charset)
1888 yaz_iconv_t cd = iconv_create_charset(record_charset);
1889 yaz_marc_t mt = yaz_marc_create();
1890 const char *ret_string = 0;
1893 yaz_marc_iconv(mt, cd);
1894 yaz_marc_xml(mt, marc_type);
1896 rec->wrbuf = wrbuf_alloc();
1897 wrbuf_rewind(rec->wrbuf);
1898 if (yaz_marc_decode_wrbuf(mt, buf, sz, rec->wrbuf) > 0)
1901 *len = wrbuf_len(rec->wrbuf);
1902 ret_string = wrbuf_cstr(rec->wrbuf);
1904 yaz_marc_destroy(mt);
1906 yaz_iconv_close(cd);
1910 static const char *return_opac_record(ZOOM_record rec, int marc_type,
1912 Z_OPACRecord *opac_rec,
1913 const char *record_charset)
1915 yaz_iconv_t cd = iconv_create_charset(record_charset);
1916 yaz_marc_t mt = yaz_marc_create();
1919 yaz_marc_iconv(mt, cd);
1920 yaz_marc_xml(mt, marc_type);
1923 rec->wrbuf = wrbuf_alloc();
1924 wrbuf_rewind(rec->wrbuf);
1926 yaz_opac_decode_wrbuf(mt, opac_rec, rec->wrbuf);
1927 yaz_marc_destroy(mt);
1930 yaz_iconv_close(cd);
1932 *len = wrbuf_len(rec->wrbuf);
1933 return wrbuf_cstr(rec->wrbuf);
1936 static const char *return_string_record(ZOOM_record rec, int *len,
1937 const char *buf, int sz,
1938 const char *record_charset)
1940 yaz_iconv_t cd = iconv_create_charset(record_charset);
1945 rec->wrbuf = wrbuf_alloc();
1947 wrbuf_rewind(rec->wrbuf);
1949 wrbuf_iconv_write(rec->wrbuf, cd, buf, sz);
1950 wrbuf_iconv_reset(rec->wrbuf, cd);
1952 buf = wrbuf_cstr(rec->wrbuf);
1953 sz = wrbuf_len(rec->wrbuf);
1954 yaz_iconv_close(cd);
1961 static const char *return_record(ZOOM_record rec, int *len,
1962 Z_NamePlusRecord *npr,
1963 int marctype, const char *charset)
1965 Z_External *r = (Z_External *) npr->u.databaseRecord;
1966 const Odr_oid *oid = r->direct_reference;
1968 /* render bibliographic record .. */
1969 if (r->which == Z_External_OPAC)
1971 return return_opac_record(rec, marctype, len,
1972 r->u.opac, charset);
1974 if (r->which == Z_External_sutrs)
1975 return return_string_record(rec, len,
1976 (char*) r->u.sutrs->buf,
1979 else if (r->which == Z_External_octet)
1981 if (yaz_oid_is_iso2709(oid))
1983 const char *ret_buf = return_marc_record(
1985 (const char *) r->u.octet_aligned->buf,
1986 r->u.octet_aligned->len,
1990 /* bad ISO2709. Return fail unless raw (ISO2709) is wanted */
1991 if (marctype != YAZ_MARC_ISO2709)
1994 return return_string_record(rec, len,
1995 (const char *) r->u.octet_aligned->buf,
1996 r->u.octet_aligned->len,
1999 else if (r->which == Z_External_grs1)
2002 rec->wrbuf = wrbuf_alloc();
2003 wrbuf_rewind(rec->wrbuf);
2004 yaz_display_grs1(rec->wrbuf, r->u.grs1, 0);
2005 return return_string_record(rec, len,
2006 wrbuf_buf(rec->wrbuf),
2007 wrbuf_len(rec->wrbuf),
2015 ZOOM_record_error(ZOOM_record rec, const char **cp,
2016 const char **addinfo, const char **diagset)
2018 Z_NamePlusRecord *npr;
2027 *cp = rec->diag_message;
2029 *addinfo = rec->diag_details;
2031 *diagset = rec->diag_set;
2032 return uri_to_code(rec->diag_uri);
2034 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
2036 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
2037 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
2038 const char *add = 0;
2040 if (diag_rec->which == Z_DiagRec_defaultFormat)
2042 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
2045 error = *ddf->condition;
2048 case Z_DefaultDiagFormat_v2Addinfo:
2049 add = ddf->u.v2Addinfo;
2051 case Z_DefaultDiagFormat_v3Addinfo:
2052 add = ddf->u.v3Addinfo;
2057 yaz_oid_to_string(yaz_oid_std(),
2058 ddf->diagnosticSetId, &oclass);
2066 *addinfo = add ? add : "";
2068 *cp = diagbib1_str(error);
2074 static const char *get_record_format(ZOOM_record rec, int *len,
2075 Z_NamePlusRecord *npr,
2076 int marctype, const char *charset,
2079 const char *res = return_record(rec, len, npr, marctype, charset);
2081 if (*format == '1' && len)
2083 /* try to XML format res */
2084 xmlKeepBlanksDefault(0);
2085 xmlDocPtr doc = xmlParseMemory(res, *len);
2089 xmlFree(rec->xml_mem);
2090 xmlDocDumpFormatMemory(doc, &rec->xml_mem, &rec->xml_size, 1);
2092 res = (char *) rec->xml_mem;
2093 *len = rec->xml_size;
2101 ZOOM_API(const char *)
2102 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
2109 Z_NamePlusRecord *npr;
2112 *len = 0; /* default return */
2121 for (i = 0; cp[i] && cp[i] != ';' && cp[i] != ' ' && i < sizeof(type)-1;
2129 while (cp[i] == ' ')
2134 while (cp[i] == ' ')
2136 if (!strncmp(cp + i, "charset=", 8))
2139 i = i + 8; /* skip charset= */
2140 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
2142 if (j < sizeof(charset)-1)
2143 charset[j++] = cp[i];
2147 else if (!strncmp(cp + i, "format=", 7))
2151 for (j = 0; cp[i] && cp[i] != ';' && cp[i] != ' '; i++)
2153 if (j < sizeof(format)-1)
2154 format[j++] = cp[i];
2159 if (!strcmp(type, "database"))
2162 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
2163 return npr->databaseName;
2165 else if (!strcmp(type, "schema"))
2168 *len = rec->schema ? strlen(rec->schema) : 0;
2171 else if (!strcmp(type, "syntax"))
2173 const char *desc = 0;
2174 if (npr->which == Z_NamePlusRecord_databaseRecord)
2176 Z_External *r = (Z_External *) npr->u.databaseRecord;
2177 desc = yaz_oid_to_string(yaz_oid_std(), r->direct_reference, 0);
2182 *len = strlen(desc);
2185 if (npr->which != Z_NamePlusRecord_databaseRecord)
2188 /* from now on - we have a database record .. */
2189 if (!strcmp(type, "render"))
2191 return get_record_format(rec, len, npr, YAZ_MARC_LINE, charset, format);
2193 else if (!strcmp(type, "xml"))
2195 return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset,
2198 else if (!strcmp(type, "raw"))
2200 return get_record_format(rec, len, npr, YAZ_MARC_ISO2709, charset,
2203 else if (!strcmp(type, "ext"))
2206 return (const char *) npr->u.databaseRecord;
2208 else if (!strcmp(type, "opac"))
2210 if (npr->u.databaseRecord->which == Z_External_OPAC)
2211 return get_record_format(rec, len, npr, YAZ_MARC_MARCXML, charset,
2217 static int strcmp_null(const char *v1, const char *v2)
2223 return strcmp(v1, v2);
2226 static size_t record_hash(int pos)
2230 return pos % RECORD_HASH_SIZE;
2233 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
2235 const char *syntax, const char *elementSetName,
2237 Z_SRW_diagnostic *diag)
2239 ZOOM_record_cache rc = 0;
2241 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2242 ZOOM_connection_put_event(r->connection, event);
2244 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2247 && strcmp_null(r->schema, rc->schema) == 0
2248 && strcmp_null(elementSetName,rc->elementSetName) == 0
2249 && strcmp_null(syntax, rc->syntax) == 0)
2254 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
2258 rc->rec.xml_mem = 0;
2260 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
2262 rc->syntax = odr_strdup_null(r->odr, syntax);
2264 rc->schema = odr_strdup_null(r->odr, r->schema);
2267 rc->next = r->record_hash[record_hash(pos)];
2268 r->record_hash[record_hash(pos)] = rc;
2271 rc->rec.schema = odr_strdup_null(r->odr, schema);
2272 rc->rec.diag_set = 0;
2273 rc->rec.diag_uri = 0;
2274 rc->rec.diag_message = 0;
2275 rc->rec.diag_details = 0;
2281 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
2282 if ((cp = strrchr(rc->rec.diag_set, '/')))
2284 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
2286 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
2287 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
2291 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
2293 const char *elementSetName)
2295 ZOOM_record_cache rc;
2297 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
2301 if (strcmp_null(r->schema, rc->schema))
2303 if (strcmp_null(elementSetName,rc->elementSetName))
2305 if (strcmp_null(syntax, rc->syntax))
2313 static void handle_records(ZOOM_connection c, Z_Records *sr,
2316 ZOOM_resultset resultset;
2318 const char *syntax = 0, *elementSetName = 0;
2322 switch (c->tasks->which)
2324 case ZOOM_TASK_SEARCH:
2325 resultset = c->tasks->u.search.resultset;
2326 start = &c->tasks->u.search.start;
2327 count = &c->tasks->u.search.count;
2328 syntax = c->tasks->u.search.syntax;
2329 elementSetName = c->tasks->u.search.elementSetName;
2331 case ZOOM_TASK_RETRIEVE:
2332 resultset = c->tasks->u.retrieve.resultset;
2333 start = &c->tasks->u.retrieve.start;
2334 count = &c->tasks->u.retrieve.count;
2335 syntax = c->tasks->u.retrieve.syntax;
2336 elementSetName = c->tasks->u.retrieve.elementSetName;
2341 if (sr && sr->which == Z_Records_NSD)
2342 response_default_diag(c, sr->u.nonSurrogateDiagnostic);
2343 else if (sr && sr->which == Z_Records_multipleNSD)
2345 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
2346 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
2348 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2352 if (*count + *start > resultset->size)
2353 *count = resultset->size - *start;
2356 if (sr && sr->which == Z_Records_DBOSD)
2359 NMEM nmem = odr_extract_mem(c->odr_in);
2360 Z_NamePlusRecordList *p =
2361 sr->u.databaseOrSurDiagnostics;
2362 for (i = 0; i<p->num_records; i++)
2364 record_cache_add(resultset, p->records[i], i + *start,
2365 syntax, elementSetName,
2372 yaz_log(log_details,
2373 "handle_records resultset=%p start=%d count=%d",
2374 resultset, *start, *count);
2376 /* transfer our response to search_nmem .. we need it later */
2377 nmem_transfer(odr_getmem(resultset->odr), nmem);
2379 if (present_phase && p->num_records == 0)
2381 /* present response and we didn't get any records! */
2382 Z_NamePlusRecord *myrec =
2383 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2384 record_cache_add(resultset, myrec, *start,
2385 syntax, elementSetName, 0, 0);
2388 else if (present_phase)
2390 /* present response and we didn't get any records! */
2391 Z_NamePlusRecord *myrec =
2392 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
2393 record_cache_add(resultset, myrec, *start, syntax, elementSetName,
2399 static void handle_present_response(ZOOM_connection c, Z_PresentResponse *pr)
2401 handle_records(c, pr->records, 1);
2404 static void handle_queryExpressionTerm(ZOOM_options opt, const char *name,
2407 switch (term->which)
2409 case Z_Term_general:
2410 ZOOM_options_setl(opt, name,
2411 (const char *)(term->u.general->buf),
2412 term->u.general->len);
2414 case Z_Term_characterString:
2415 ZOOM_options_set(opt, name, term->u.characterString);
2417 case Z_Term_numeric:
2418 ZOOM_options_set_int(opt, name, *term->u.numeric);
2423 static void handle_queryExpression(ZOOM_options opt, const char *name,
2424 Z_QueryExpression *exp)
2430 case Z_QueryExpression_term:
2431 if (exp->u.term && exp->u.term->queryTerm)
2433 sprintf(opt_name, "%s.term", name);
2434 handle_queryExpressionTerm(opt, opt_name, exp->u.term->queryTerm);
2437 case Z_QueryExpression_query:
2442 static void handle_searchResult(ZOOM_connection c, ZOOM_resultset resultset,
2443 Z_OtherInformation *o)
2446 for (i = 0; o && i < o->num_elements; i++)
2448 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
2450 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
2452 if (ext->which == Z_External_searchResult1)
2455 Z_SearchInfoReport *sr = ext->u.searchResult1;
2458 ZOOM_options_set_int(
2459 resultset->options, "searchresult.size", sr->num);
2461 for (j = 0; j < sr->num; j++)
2463 Z_SearchInfoReport_s *ent =
2464 ext->u.searchResult1->elements[j];
2467 sprintf(pref, "searchresult.%d", j);
2469 if (ent->subqueryId)
2472 sprintf(opt_name, "%s.id", pref);
2473 ZOOM_options_set(resultset->options, opt_name,
2476 if (ent->subqueryExpression)
2479 sprintf(opt_name, "%s.subquery", pref);
2480 handle_queryExpression(resultset->options, opt_name,
2481 ent->subqueryExpression);
2483 if (ent->subqueryInterpretation)
2486 sprintf(opt_name, "%s.interpretation", pref);
2487 handle_queryExpression(resultset->options, opt_name,
2488 ent->subqueryInterpretation);
2490 if (ent->subqueryRecommendation)
2493 sprintf(opt_name, "%s.recommendation", pref);
2494 handle_queryExpression(resultset->options, opt_name,
2495 ent->subqueryRecommendation);
2497 if (ent->subqueryCount)
2500 sprintf(opt_name, "%s.count", pref);
2501 ZOOM_options_set_int(resultset->options, opt_name,
2502 *ent->subqueryCount);
2510 static void handle_search_response(ZOOM_connection c, Z_SearchResponse *sr)
2512 ZOOM_resultset resultset;
2515 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
2518 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
2519 ZOOM_connection_put_event(c, event);
2521 resultset = c->tasks->u.search.resultset;
2523 if (sr->resultSetStatus)
2525 ZOOM_options_set_int(resultset->options, "resultSetStatus",
2526 *sr->resultSetStatus);
2528 if (sr->presentStatus)
2530 ZOOM_options_set_int(resultset->options, "presentStatus",
2531 *sr->presentStatus);
2533 handle_searchResult(c, resultset, sr->additionalSearchInfo);
2535 resultset->size = *sr->resultCount;
2536 handle_records(c, sr->records, 0);
2539 static void sort_response(ZOOM_connection c, Z_SortResponse *res)
2541 if (res->diagnostics && res->num_diagnostics > 0)
2542 response_diag(c, res->diagnostics[0]);
2545 static int scan_response(ZOOM_connection c, Z_ScanResponse *res)
2547 NMEM nmem = odr_extract_mem(c->odr_in);
2550 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
2552 scan = c->tasks->u.scan.scan;
2554 if (res->entries && res->entries->nonsurrogateDiagnostics)
2555 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
2556 scan->scan_response = res;
2557 scan->srw_scan_response = 0;
2558 nmem_transfer(odr_getmem(scan->odr), nmem);
2560 ZOOM_options_set_int(scan->options, "stepSize", *res->stepSize);
2561 if (res->positionOfTerm)
2562 ZOOM_options_set_int(scan->options, "position", *res->positionOfTerm);
2563 if (res->scanStatus)
2564 ZOOM_options_set_int(scan->options, "scanStatus", *res->scanStatus);
2565 if (res->numberOfEntriesReturned)
2566 ZOOM_options_set_int(scan->options, "number",
2567 *res->numberOfEntriesReturned);
2572 static zoom_ret send_sort(ZOOM_connection c,
2573 ZOOM_resultset resultset)
2576 resultset->r_sort_spec = 0;
2577 if (resultset->r_sort_spec)
2579 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
2580 Z_SortRequest *req = apdu->u.sortRequest;
2582 req->num_inputResultSetNames = 1;
2583 req->inputResultSetNames = (Z_InternationalString **)
2584 odr_malloc(c->odr_out, sizeof(*req->inputResultSetNames));
2585 req->inputResultSetNames[0] =
2586 odr_strdup(c->odr_out, resultset->setname);
2587 req->sortedResultSetName = odr_strdup(c->odr_out, resultset->setname);
2588 req->sortSequence = resultset->r_sort_spec;
2589 resultset->r_sort_spec = 0;
2590 return send_APDU(c, apdu);
2592 return zoom_complete;
2595 static zoom_ret send_present(ZOOM_connection c)
2598 Z_PresentRequest *req = 0;
2600 const char *syntax = 0;
2601 const char *elementSetName = 0;
2602 ZOOM_resultset resultset;
2607 yaz_log(log_details, "%p send_present no tasks", c);
2608 return zoom_complete;
2611 switch (c->tasks->which)
2613 case ZOOM_TASK_SEARCH:
2614 resultset = c->tasks->u.search.resultset;
2615 start = &c->tasks->u.search.start;
2616 count = &c->tasks->u.search.count;
2617 syntax = c->tasks->u.search.syntax;
2618 elementSetName = c->tasks->u.search.elementSetName;
2620 case ZOOM_TASK_RETRIEVE:
2621 resultset = c->tasks->u.retrieve.resultset;
2622 start = &c->tasks->u.retrieve.start;
2623 count = &c->tasks->u.retrieve.count;
2624 syntax = c->tasks->u.retrieve.syntax;
2625 elementSetName = c->tasks->u.retrieve.elementSetName;
2628 return zoom_complete;
2630 yaz_log(log_details, "%p send_present start=%d count=%d",
2633 if (*start < 0 || *count < 0 || *start + *count > resultset->size)
2635 set_dset_error(c, YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, "Bib-1",
2638 if (c->error) /* don't continue on error */
2639 return zoom_complete;
2640 yaz_log(log_details, "send_present resultset=%p start=%d count=%d",
2641 resultset, *start, *count);
2643 for (i = 0; i < *count; i++)
2646 record_cache_lookup(resultset, i + *start, syntax, elementSetName);
2651 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
2652 ZOOM_connection_put_event(c, event);
2660 yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i);
2661 return zoom_complete;
2664 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
2665 req = apdu->u.presentRequest;
2668 yaz_log(log_details, "%p send_present skip=%d", c, i);
2670 *req->resultSetStartPoint = *start + 1;
2672 if (resultset->step > 0 && resultset->step < *count)
2673 *req->numberOfRecordsRequested = resultset->step;
2675 *req->numberOfRecordsRequested = *count;
2677 if (*req->numberOfRecordsRequested + *start > resultset->size)
2678 *req->numberOfRecordsRequested = resultset->size - *start;
2679 assert(*req->numberOfRecordsRequested > 0);
2681 if (syntax && *syntax)
2682 req->preferredRecordSyntax =
2683 zoom_yaz_str_to_z3950oid(c, CLASS_RECSYN, syntax);
2685 if (resultset->schema && *resultset->schema)
2687 Z_RecordComposition *compo = (Z_RecordComposition *)
2688 odr_malloc(c->odr_out, sizeof(*compo));
2690 req->recordComposition = compo;
2691 compo->which = Z_RecordComp_complex;
2692 compo->u.complex = (Z_CompSpec *)
2693 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
2694 compo->u.complex->selectAlternativeSyntax = (bool_t *)
2695 odr_malloc(c->odr_out, sizeof(bool_t));
2696 *compo->u.complex->selectAlternativeSyntax = 0;
2698 compo->u.complex->generic = (Z_Specification *)
2699 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
2701 compo->u.complex->generic->which = Z_Schema_oid;
2702 compo->u.complex->generic->schema.oid = (Odr_oid *)
2703 zoom_yaz_str_to_z3950oid (c, CLASS_SCHEMA, resultset->schema);
2705 if (!compo->u.complex->generic->schema.oid)
2707 /* OID wasn't a schema! Try record syntax instead. */
2709 compo->u.complex->generic->schema.oid = (Odr_oid *)
2710 zoom_yaz_str_to_z3950oid (c, CLASS_RECSYN, resultset->schema);
2712 if (elementSetName && *elementSetName)
2714 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
2715 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
2716 compo->u.complex->generic->elementSpec->which =
2717 Z_ElementSpec_elementSetName;
2718 compo->u.complex->generic->elementSpec->u.elementSetName =
2719 odr_strdup(c->odr_out, elementSetName);
2722 compo->u.complex->generic->elementSpec = 0;
2723 compo->u.complex->num_dbSpecific = 0;
2724 compo->u.complex->dbSpecific = 0;
2725 compo->u.complex->num_recordSyntax = 0;
2726 compo->u.complex->recordSyntax = 0;
2728 else if (elementSetName && *elementSetName)
2730 Z_ElementSetNames *esn = (Z_ElementSetNames *)
2731 odr_malloc(c->odr_out, sizeof(*esn));
2732 Z_RecordComposition *compo = (Z_RecordComposition *)
2733 odr_malloc(c->odr_out, sizeof(*compo));
2735 esn->which = Z_ElementSetNames_generic;
2736 esn->u.generic = odr_strdup(c->odr_out, elementSetName);
2737 compo->which = Z_RecordComp_simple;
2738 compo->u.simple = esn;
2739 req->recordComposition = compo;
2741 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
2742 return send_APDU(c, apdu);
2745 ZOOM_API(ZOOM_scanset)
2746 ZOOM_connection_scan(ZOOM_connection c, const char *start)
2749 ZOOM_query q = ZOOM_query_create();
2751 ZOOM_query_prefix(q, start);
2753 s = ZOOM_connection_scan1(c, q);
2754 ZOOM_query_destroy(q);
2759 ZOOM_API(ZOOM_scanset)
2760 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
2762 ZOOM_scanset scan = 0;
2766 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
2767 scan->connection = c;
2768 scan->odr = odr_createmem(ODR_DECODE);
2769 scan->options = ZOOM_options_create_with_parent(c->options);
2771 scan->scan_response = 0;
2772 scan->srw_scan_response = 0;
2776 scan->databaseNames = set_DatabaseNames(c, c->options,
2777 &scan->num_databaseNames,
2782 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
2783 task->u.scan.scan = scan;
2788 while (ZOOM_event(1, &c))
2796 ZOOM_scanset_destroy(ZOOM_scanset scan)
2801 if (scan->refcount == 0)
2803 ZOOM_query_destroy(scan->query);
2805 odr_destroy(scan->odr);
2807 ZOOM_options_destroy(scan->options);
2812 static zoom_ret send_package(ZOOM_connection c)
2816 yaz_log(log_details, "%p send_package", c);
2818 return zoom_complete;
2819 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
2821 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
2822 ZOOM_connection_put_event(c, event);
2824 c->buf_out = c->tasks->u.package->buf_out;
2825 c->len_out = c->tasks->u.package->len_out;
2830 static zoom_ret ZOOM_connection_send_scan(ZOOM_connection c)
2833 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
2834 Z_ScanRequest *req = apdu->u.scanRequest;
2836 yaz_log(log_details, "%p send_scan", c);
2838 return zoom_complete;
2839 assert (c->tasks->which == ZOOM_TASK_SCAN);
2840 scan = c->tasks->u.scan.scan;
2842 /* Z39.50 scan can only carry RPN */
2843 if (scan->query->z_query->which == Z_Query_type_1 ||
2844 scan->query->z_query->which == Z_Query_type_101)
2846 Z_RPNQuery *rpn = scan->query->z_query->u.type_1;
2847 const char *cp = ZOOM_options_get(scan->options, "rpnCharset");
2850 yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8");
2853 rpn = yaz_copy_z_RPNQuery(rpn, c->odr_out);
2855 yaz_query_charset_convert_rpnquery(
2856 rpn, c->odr_out, cd);
2857 yaz_iconv_close(cd);
2860 req->attributeSet = rpn->attributeSetId;
2861 if (!req->attributeSet)
2862 req->attributeSet = odr_oiddup(c->odr_out, yaz_oid_attset_bib_1);
2863 if (rpn->RPNStructure->which == Z_RPNStructure_simple &&
2864 rpn->RPNStructure->u.simple->which == Z_Operand_APT)
2866 req->termListAndStartPoint =
2867 rpn->RPNStructure->u.simple->u.attributesPlusTerm;
2871 set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
2872 return zoom_complete;
2877 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2878 return zoom_complete;
2881 *req->numberOfTermsRequested =
2882 ZOOM_options_get_int(scan->options, "number", 10);
2884 req->preferredPositionInResponse =
2885 odr_intdup(c->odr_out,
2886 ZOOM_options_get_int(scan->options, "position", 1));
2889 odr_intdup(c->odr_out,
2890 ZOOM_options_get_int(scan->options, "stepSize", 0));
2892 req->databaseNames = scan->databaseNames;
2893 req->num_databaseNames = scan->num_databaseNames;
2895 return send_APDU(c, apdu);
2899 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2903 const char *option_val = 0;
2906 return zoom_complete;
2907 assert (c->tasks->which == ZOOM_TASK_SCAN);
2908 scan = c->tasks->u.scan.scan;
2910 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
2912 /* SRU scan can only carry CQL and PQF */
2913 if (scan->query->z_query->which == Z_Query_type_104)
2915 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
2916 sr->u.scan_request->scanClause.cql = scan->query->query_string;
2918 else if (scan->query->z_query->which == Z_Query_type_1
2919 || scan->query->z_query->which == Z_Query_type_101)
2921 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
2922 sr->u.scan_request->scanClause.pqf = scan->query->query_string;
2926 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
2927 return zoom_complete;
2930 sr->u.scan_request->maximumTerms = odr_intdup(
2931 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
2933 sr->u.scan_request->responsePosition = odr_intdup(
2934 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
2936 option_val = ZOOM_options_get(scan->options, "extraArgs");
2937 yaz_encode_sru_extra(sr, c->odr_out, option_val);
2938 return send_srw(c, sr);
2941 static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
2943 return zoom_complete;
2949 ZOOM_scanset_size(ZOOM_scanset scan)
2954 if (scan->scan_response && scan->scan_response->entries)
2955 return scan->scan_response->entries->num_entries;
2956 else if (scan->srw_scan_response)
2957 return scan->srw_scan_response->num_terms;
2961 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
2963 const char **value_term, size_t *value_len,
2964 const char **disp_term, size_t *disp_len)
2966 size_t noent = ZOOM_scanset_size(scan);
2977 if (scan->scan_response)
2979 Z_ScanResponse *res = scan->scan_response;
2980 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
2982 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
2984 *value_term = (const char *) t->term->u.general->buf;
2985 *value_len = t->term->u.general->len;
2988 *disp_term = t->displayTerm;
2989 *disp_len = strlen(*disp_term);
2991 else if (t->term->which == Z_Term_general)
2993 *disp_term = (const char *) t->term->u.general->buf;
2994 *disp_len = t->term->u.general->len;
2996 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
2999 if (scan->srw_scan_response)
3001 Z_SRW_scanResponse *res = scan->srw_scan_response;
3002 Z_SRW_scanTerm *t = res->terms + pos;
3005 *value_term = t->value;
3006 *value_len = strlen(*value_term);
3009 *disp_term = t->displayTerm;
3011 *disp_term = t->value;
3012 *disp_len = strlen(*disp_term);
3013 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
3018 ZOOM_API(const char *)
3019 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
3020 size_t *occ, size_t *len)
3022 const char *value_term = 0;
3023 size_t value_len = 0;
3024 const char *disp_term = 0;
3025 size_t disp_len = 0;
3027 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3028 &disp_term, &disp_len);
3034 ZOOM_API(const char *)
3035 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
3036 size_t *occ, size_t *len)
3038 const char *value_term = 0;
3039 size_t value_len = 0;
3040 const char *disp_term = 0;
3041 size_t disp_len = 0;
3043 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
3044 &disp_term, &disp_len);
3050 ZOOM_API(const char *)
3051 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
3053 return ZOOM_options_get(scan->options, key);
3057 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
3060 ZOOM_options_set(scan->options, key, val);
3063 static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
3066 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
3067 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3069 str = ZOOM_options_get(p->options, "package-name");
3071 req->packageName = odr_strdup(p->odr_out, str);
3073 str = ZOOM_options_get(p->options, "user-id");
3075 req->userId = odr_strdup_null(p->odr_out, str);
3077 req->packageType = odr_oiddup(p->odr_out, oid);
3079 str = ZOOM_options_get(p->options, "function");
3082 if (!strcmp (str, "create"))
3083 *req->function = Z_ExtendedServicesRequest_create;
3084 if (!strcmp (str, "delete"))
3085 *req->function = Z_ExtendedServicesRequest_delete;
3086 if (!strcmp (str, "modify"))
3087 *req->function = Z_ExtendedServicesRequest_modify;
3090 str = ZOOM_options_get(p->options, "waitAction");
3093 if (!strcmp (str, "wait"))
3094 *req->waitAction = Z_ExtendedServicesRequest_wait;
3095 if (!strcmp (str, "waitIfPossible"))
3096 *req->waitAction = Z_ExtendedServicesRequest_waitIfPossible;
3097 if (!strcmp (str, "dontWait"))
3098 *req->waitAction = Z_ExtendedServicesRequest_dontWait;
3099 if (!strcmp (str, "dontReturnPackage"))
3100 *req->waitAction = Z_ExtendedServicesRequest_dontReturnPackage;
3105 static const char *ill_array_lookup(void *clientData, const char *idx)
3107 ZOOM_package p = (ZOOM_package) clientData;
3108 return ZOOM_options_get(p->options, idx+4);
3111 static Z_External *encode_ill_request(ZOOM_package p)
3113 ODR out = p->odr_out;
3116 struct ill_get_ctl ctl;
3118 ctl.odr = p->odr_out;
3120 ctl.f = ill_array_lookup;
3122 req = ill_get_ILLRequest(&ctl, "ill", 0);
3124 if (!ill_Request(out, &req, 0, 0))
3126 int ill_request_size;
3127 char *ill_request_buf = odr_getbuf(out, &ill_request_size, 0);
3128 if (ill_request_buf)
3129 odr_setbuf(out, ill_request_buf, ill_request_size, 1);
3134 int illRequest_size = 0;
3135 char *illRequest_buf = odr_getbuf(out, &illRequest_size, 0);
3137 r = (Z_External *) odr_malloc(out, sizeof(*r));
3138 r->direct_reference = odr_oiddup(out, yaz_oid_general_isoill_1);
3139 r->indirect_reference = 0;
3141 r->which = Z_External_single;
3143 r->u.single_ASN1_type =
3144 odr_create_Odr_oct(out,
3145 (unsigned char *)illRequest_buf,
3151 static Z_ItemOrder *encode_item_order(ZOOM_package p)
3153 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc(p->odr_out, sizeof(*req));
3157 req->which = Z_IOItemOrder_esRequest;
3158 req->u.esRequest = (Z_IORequest *)
3159 odr_malloc(p->odr_out,sizeof(Z_IORequest));
3161 /* to keep part ... */
3162 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
3163 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
3164 req->u.esRequest->toKeep->supplDescription = 0;
3165 req->u.esRequest->toKeep->contact = (Z_IOContact *)
3166 odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
3168 str = ZOOM_options_get(p->options, "contact-name");
3169 req->u.esRequest->toKeep->contact->name =
3170 odr_strdup_null(p->odr_out, str);
3172 str = ZOOM_options_get(p->options, "contact-phone");
3173 req->u.esRequest->toKeep->contact->phone =
3174 odr_strdup_null(p->odr_out, str);
3176 str = ZOOM_options_get(p->options, "contact-email");
3177 req->u.esRequest->toKeep->contact->email =
3178 odr_strdup_null(p->odr_out, str);
3180 req->u.esRequest->toKeep->addlBilling = 0;
3182 /* not to keep part ... */
3183 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
3184 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
3186 str = ZOOM_options_get(p->options, "itemorder-setname");
3191 req->u.esRequest->notToKeep->resultSetItem = 0;
3194 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
3195 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
3197 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
3198 odr_strdup(p->odr_out, str);
3199 req->u.esRequest->notToKeep->resultSetItem->item =
3200 odr_intdup(p->odr_out, 0);
3202 str = ZOOM_options_get(p->options, "itemorder-item");
3203 *req->u.esRequest->notToKeep->resultSetItem->item =
3204 (str ? atoi(str) : 1);
3207 str = ZOOM_options_getl(p->options, "doc", &len);
3210 req->u.esRequest->notToKeep->itemRequest =
3211 z_ext_record_xml(p->odr_out, str, len);
3214 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
3219 Z_APDU *create_admin_package(ZOOM_package p, int type,
3220 Z_ESAdminOriginPartToKeep **toKeepP,
3221 Z_ESAdminOriginPartNotToKeep **notToKeepP)
3223 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_admin);
3226 Z_ESAdminOriginPartToKeep *toKeep;
3227 Z_ESAdminOriginPartNotToKeep *notToKeep;
3228 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3229 const char *first_db = "Default";
3231 char **db = set_DatabaseNames(p->connection, p->options, &num_db,
3236 r->direct_reference = odr_oiddup(p->odr_out, yaz_oid_extserv_admin);
3238 r->indirect_reference = 0;
3239 r->which = Z_External_ESAdmin;
3241 r->u.adminService = (Z_Admin *)
3242 odr_malloc(p->odr_out, sizeof(*r->u.adminService));
3243 r->u.adminService->which = Z_Admin_esRequest;
3244 r->u.adminService->u.esRequest = (Z_AdminEsRequest *)
3245 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest));
3247 toKeep = r->u.adminService->u.esRequest->toKeep =
3248 (Z_ESAdminOriginPartToKeep *)
3249 odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep));
3250 toKeep->which = type;
3251 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3252 toKeep->u.create = odr_nullval();
3253 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3255 r->u.adminService->u.esRequest->notToKeep = notToKeep =
3256 (Z_ESAdminOriginPartNotToKeep *)
3257 odr_malloc(p->odr_out,
3258 sizeof(*r->u.adminService->u.esRequest->notToKeep));
3259 notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow;
3260 notToKeep->u.recordsWillFollow = odr_nullval();
3264 *notToKeepP = notToKeep;
3269 static Z_APDU *create_xmlupdate_package(ZOOM_package p)
3271 Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
3272 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
3273 Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext));
3275 const char *doc = ZOOM_options_getl(p->options, "doc", &len);
3283 req->taskSpecificParameters = ext;
3284 ext->direct_reference = req->packageType;
3285 ext->descriptor = 0;
3286 ext->indirect_reference = 0;
3288 ext->which = Z_External_octet;
3289 ext->u.single_ASN1_type =
3290 odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
3294 static Z_APDU *create_update_package(ZOOM_package p)
3297 const char *first_db = "Default";
3299 char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
3300 const char *action = ZOOM_options_get(p->options, "action");
3301 int recordIdOpaque_len;
3302 const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
3303 &recordIdOpaque_len);
3304 const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
3306 const char *record_buf = ZOOM_options_getl(p->options, "record",
3308 int recordOpaque_len;
3309 const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
3311 const char *syntax_str = ZOOM_options_get(p->options, "syntax");
3312 const char *version = ZOOM_options_get(p->options, "updateVersion");
3314 const char *correlationInfo_note =
3315 ZOOM_options_get(p->options, "correlationInfo.note");
3316 const char *correlationInfo_id =
3317 ZOOM_options_get(p->options, "correlationInfo.id");
3319 Odr_oid *syntax_oid = 0;
3320 const Odr_oid *package_oid = yaz_oid_extserv_database_update;
3326 if (!record_buf && !recordOpaque_buf)
3328 record_buf = "void";
3330 syntax_str = "SUTRS";
3335 syntax_oid = yaz_string_to_oid_odr(yaz_oid_std(),
3336 CLASS_RECSYN, syntax_str,
3348 package_oid = yaz_oid_extserv_database_update_first_version;
3349 /* old update does not support specialUpdate */
3351 action = "recordInsert";
3355 action = "specialUpdate";
3356 package_oid = yaz_oid_extserv_database_update_second_version;
3360 action = "specialUpdate";
3361 package_oid = yaz_oid_extserv_database_update;
3367 if (!strcmp(action, "recordInsert"))
3368 action_no = Z_IUOriginPartToKeep_recordInsert;
3369 else if (!strcmp(action, "recordReplace"))
3370 action_no = Z_IUOriginPartToKeep_recordReplace;
3371 else if (!strcmp(action, "recordDelete"))
3372 action_no = Z_IUOriginPartToKeep_recordDelete;
3373 else if (!strcmp(action, "elementUpdate"))
3374 action_no = Z_IUOriginPartToKeep_elementUpdate;
3375 else if (!strcmp(action, "specialUpdate"))
3376 action_no = Z_IUOriginPartToKeep_specialUpdate;
3380 apdu = create_es_package(p, package_oid);
3383 Z_IUOriginPartToKeep *toKeep;
3384 Z_IUSuppliedRecords *notToKeep;
3385 Z_External *r = (Z_External *)
3386 odr_malloc(p->odr_out, sizeof(*r));
3387 const char *elementSetName =
3388 ZOOM_options_get(p->options, "elementSetName");
3390 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3392 r->direct_reference = odr_oiddup(p->odr_out, package_oid);
3394 r->which = Z_External_update;
3395 r->indirect_reference = 0;
3396 r->u.update = (Z_IUUpdate *)
3397 odr_malloc(p->odr_out, sizeof(*r->u.update));
3399 r->u.update->which = Z_IUUpdate_esRequest;
3400 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
3401 odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest));
3402 toKeep = r->u.update->u.esRequest->toKeep =
3403 (Z_IUOriginPartToKeep *)
3404 odr_malloc(p->odr_out, sizeof(*toKeep));
3406 toKeep->databaseName = odr_strdup(p->odr_out, first_db);
3409 toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
3411 toKeep->actionQualifier = 0;
3412 toKeep->action = odr_intdup(p->odr_out, action_no);
3414 notToKeep = r->u.update->u.esRequest->notToKeep =
3415 (Z_IUSuppliedRecords *)
3416 odr_malloc(p->odr_out, sizeof(*notToKeep));
3418 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
3419 odr_malloc(p->odr_out, sizeof(*notToKeep->elements));
3420 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
3421 odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
3422 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
3425 notToKeep->elements[0]->u.opaque =
3426 odr_create_Odr_oct(p->odr_out,
3427 (const unsigned char *) recordIdOpaque,
3428 recordIdOpaque_len);
3430 else if (recordIdNumber)
3432 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
3434 notToKeep->elements[0]->u.number =
3435 odr_intdup(p->odr_out, atoi(recordIdNumber));
3438 notToKeep->elements[0]->u.opaque = 0;
3439 notToKeep->elements[0]->supplementalId = 0;
3440 if (correlationInfo_note || correlationInfo_id)
3442 Z_IUCorrelationInfo *ci;
3443 ci = notToKeep->elements[0]->correlationInfo =
3444 (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
3445 ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
3446 ci->id = correlationInfo_id ?
3447 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
3450 notToKeep->elements[0]->correlationInfo = 0;
3451 if (recordOpaque_buf)
3453 notToKeep->elements[0]->record =
3454 z_ext_record_oid_any(p->odr_out, syntax_oid,
3455 recordOpaque_buf, recordOpaque_len);
3459 notToKeep->elements[0]->record =
3460 z_ext_record_oid(p->odr_out, syntax_oid,
3461 record_buf, record_len);
3466 ODR print = odr_createmem(ODR_PRINT);
3468 z_APDU(print, &apdu, 0, 0);
3475 ZOOM_package_send(ZOOM_package p, const char *type)
3482 odr_reset(p->odr_out);
3485 if (!strcmp(type, "itemorder"))
3487 apdu = create_es_package(p, yaz_oid_extserv_item_order);
3490 Z_External *r = (Z_External *) odr_malloc(p->odr_out, sizeof(*r));
3492 r->direct_reference =
3493 odr_oiddup(p->odr_out, yaz_oid_extserv_item_order);
3495 r->which = Z_External_itemOrder;
3496 r->indirect_reference = 0;
3497 r->u.itemOrder = encode_item_order(p);
3499 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
3502 else if (!strcmp(type, "create")) /* create database */
3504 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create,
3507 else if (!strcmp(type, "drop")) /* drop database */
3509 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop,
3512 else if (!strcmp(type, "commit")) /* commit changes */
3514 apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit,
3517 else if (!strcmp(type, "update")) /* update record(s) */
3519 apdu = create_update_package(p);
3521 else if (!strcmp(type, "xmlupdate"))
3523 apdu = create_xmlupdate_package(p);
3527 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
3531 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_PACKAGE);
3532 task->u.package = p;
3533 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
3534 p->buf_out = (char *) xmalloc(p->len_out);
3535 memcpy(p->buf_out, buf, p->len_out);
3540 while (ZOOM_event(1, &c))
3547 ZOOM_API(ZOOM_package)
3548 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
3550 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
3553 p->odr_out = odr_createmem(ODR_ENCODE);
3554 p->options = ZOOM_options_create_with_parent2(options, c->options);
3562 ZOOM_package_destroy(ZOOM_package p)
3567 if (p->refcount == 0)
3569 odr_destroy(p->odr_out);
3572 ZOOM_options_destroy(p->options);
3577 ZOOM_API(const char *)
3578 ZOOM_package_option_get(ZOOM_package p, const char *key)
3580 return ZOOM_options_get(p->options, key);
3583 ZOOM_API(const char *)
3584 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
3586 return ZOOM_options_getl(p->options, key, lenp);
3590 ZOOM_package_option_set(ZOOM_package p, const char *key,
3593 ZOOM_options_set(p->options, key, val);
3597 ZOOM_package_option_setl(ZOOM_package p, const char *key,
3598 const char *val, int len)
3600 ZOOM_options_setl(p->options, key, val, len);
3603 static int ZOOM_connection_exec_task(ZOOM_connection c)
3605 ZOOM_task task = c->tasks;
3606 zoom_ret ret = zoom_complete;
3610 yaz_log(log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
3611 c, task->which, task->running);
3612 if (c->error != ZOOM_ERROR_NONE)
3614 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3615 "removing tasks because of error = %d", c, c->error);
3616 ZOOM_connection_remove_tasks(c);
3621 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3622 "task already running", c);
3626 ret = zoom_complete;
3627 if (c->cs || task->which == ZOOM_TASK_CONNECT)
3629 switch (task->which)
3631 case ZOOM_TASK_SEARCH:
3632 if (c->proto == PROTO_HTTP)
3633 ret = ZOOM_connection_srw_send_search(c);
3635 ret = ZOOM_connection_send_search(c);
3637 case ZOOM_TASK_RETRIEVE:
3638 if (c->proto == PROTO_HTTP)
3639 ret = ZOOM_connection_srw_send_search(c);
3641 ret = send_present(c);
3643 case ZOOM_TASK_CONNECT:
3644 ret = do_connect(c);
3646 case ZOOM_TASK_SCAN:
3647 if (c->proto == PROTO_HTTP)
3648 ret = ZOOM_connection_srw_send_scan(c);
3650 ret = ZOOM_connection_send_scan(c);
3652 case ZOOM_TASK_PACKAGE:
3653 ret = send_package(c);
3655 case ZOOM_TASK_SORT:
3656 c->tasks->u.sort.resultset->r_sort_spec =
3657 c->tasks->u.sort.q->sort_spec;
3658 ret = send_sort(c, c->tasks->u.sort.resultset);
3664 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3665 "remove tasks because no connection exist", c);
3666 ZOOM_connection_remove_tasks(c);
3668 if (ret == zoom_complete)
3670 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3671 "task removed (complete)", c);
3672 ZOOM_connection_remove_task(c);
3675 yaz_log(log_details, "%p ZOOM_connection_exec_task "
3680 static zoom_ret send_sort_present(ZOOM_connection c)
3682 zoom_ret r = zoom_complete;
3684 if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
3685 r = send_sort(c, c->tasks->u.search.resultset);
3686 if (r == zoom_complete)
3687 r = send_present(c);
3691 static int es_response_taskpackage_update(ZOOM_connection c,
3692 Z_IUUpdateTaskPackage *utp)
3694 if (utp && utp->targetPart)
3696 Z_IUTargetPart *targetPart = utp->targetPart;
3697 switch ( *targetPart->updateStatus ) {
3698 case Z_IUTargetPart_success:
3699 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
3701 case Z_IUTargetPart_partial:
3702 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
3704 case Z_IUTargetPart_failure:
3705 ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
3706 if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
3707 response_diag(c, targetPart->globalDiagnostics[0]);
3710 // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
3715 static int es_response_taskpackage(ZOOM_connection c,
3716 Z_TaskPackage *taskPackage)
3719 Odr_oct *id = taskPackage->targetReference;
3721 ZOOM_options_setl(c->tasks->u.package->options,
3722 "targetReference", (char*) id->buf, id->len);
3725 switch ( *taskPackage->taskStatus ) {
3726 case Z_TaskPackage_pending:
3727 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
3729 case Z_TaskPackage_active:
3730 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
3732 case Z_TaskPackage_complete:
3733 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
3735 case Z_TaskPackage_aborted:
3736 ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
3737 if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
3738 response_diag(c, taskPackage->packageDiagnostics[0]);
3742 // taskSpecificParameters
3743 // NOTE: Only Update implemented, no others.
3744 if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
3745 Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
3746 es_response_taskpackage_update(c, utp);
3752 static int es_response(ZOOM_connection c,
3753 Z_ExtendedServicesResponse *res)
3755 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
3757 switch (*res->operationStatus) {
3758 case Z_ExtendedServicesResponse_done:
3759 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
3761 case Z_ExtendedServicesResponse_accepted:
3762 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
3764 case Z_ExtendedServicesResponse_failure:
3765 ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
3766 if (res->diagnostics && res->num_diagnostics > 0)
3767 response_diag(c, res->diagnostics[0]);
3770 if (res->taskPackage &&
3771 res->taskPackage->which == Z_External_extendedService)
3773 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
3774 es_response_taskpackage(c, taskPackage);
3776 if (res->taskPackage &&
3777 res->taskPackage->which == Z_External_octet)
3779 Odr_oct *doc = res->taskPackage->u.octet_aligned;
3780 ZOOM_options_setl(c->tasks->u.package->options,
3781 "xmlUpdateDoc", (char*) doc->buf, doc->len);
3786 static void interpret_init_diag(ZOOM_connection c,
3787 Z_DiagnosticFormat *diag)
3791 Z_DiagnosticFormat_s *ds = diag->elements[0];
3792 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
3793 response_default_diag(c, ds->u.defaultDiagRec);
3798 static void interpret_otherinformation_field(ZOOM_connection c,
3799 Z_OtherInformation *ui)
3802 for (i = 0; i < ui->num_elements; i++)
3804 Z_OtherInformationUnit *unit = ui->list[i];
3805 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
3806 unit->information.externallyDefinedInfo &&
3807 unit->information.externallyDefinedInfo->which ==
3810 interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1);
3816 static void set_init_option(const char *name, void *clientData) {
3817 ZOOM_connection c = (ZOOM_connection) clientData;
3820 sprintf(buf, "init_opt_%.70s", name);
3821 ZOOM_connection_option_set(c, buf, "1");
3825 static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
3827 Z_InitResponse *initrs;
3829 ZOOM_connection_set_mask(c, 0);
3830 yaz_log(log_details, "%p recv_apdu apdu->which=%d", c, apdu->which);
3833 case Z_APDU_initResponse:
3834 yaz_log(log_api, "%p recv_apdu: Received Init response", c);
3835 initrs = apdu->u.initResponse;
3836 ZOOM_connection_option_set(c, "serverImplementationId",
3837 initrs->implementationId ?
3838 initrs->implementationId : "");
3839 ZOOM_connection_option_set(c, "serverImplementationName",
3840 initrs->implementationName ?
3841 initrs->implementationName : "");
3842 ZOOM_connection_option_set(c, "serverImplementationVersion",
3843 initrs->implementationVersion ?
3844 initrs->implementationVersion : "");
3845 /* Set the three old options too, for old applications */
3846 ZOOM_connection_option_set(c, "targetImplementationId",
3847 initrs->implementationId ?
3848 initrs->implementationId : "");
3849 ZOOM_connection_option_set(c, "targetImplementationName",
3850 initrs->implementationName ?
3851 initrs->implementationName : "");
3852 ZOOM_connection_option_set(c, "targetImplementationVersion",
3853 initrs->implementationVersion ?
3854 initrs->implementationVersion : "");
3856 /* Make initrs->options available as ZOOM-level options */
3857 yaz_init_opt_decode(initrs->options, set_init_option, (void*) c);
3859 if (!*initrs->result)
3861 Z_External *uif = initrs->userInformationField;
3863 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */
3865 if (uif && uif->which == Z_External_userInfo1)
3866 interpret_otherinformation_field(c, uif->u.userInfo1);
3871 yaz_oi_get_string_oid(&apdu->u.initResponse->otherInfo,
3872 yaz_oid_userinfo_cookie, 1, 0);
3873 xfree(c->cookie_in);
3876 c->cookie_in = xstrdup(cookie);
3877 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
3878 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
3879 c->support_named_resultsets = 1;
3882 assert(c->tasks->which == ZOOM_TASK_CONNECT);
3883 ZOOM_connection_remove_task(c);
3885 ZOOM_connection_exec_task(c);
3887 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
3889 NMEM tmpmem = nmem_create();
3890 Z_CharSetandLanguageNegotiation *p =
3891 yaz_get_charneg_record(initrs->otherInfo);
3895 char *charset = NULL, *lang = NULL;
3898 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
3899 yaz_log(log_details, "%p recv_apdu target accepted: "
3900 "charset %s, language %s, select %d",
3902 charset ? charset : "none", lang ? lang : "none", sel);
3904 ZOOM_connection_option_set(c, "negotiation-charset",
3907 ZOOM_connection_option_set(c, "negotiation-lang",
3910 ZOOM_connection_option_set(
3911 c, "negotiation-charset-in-effect-for-records",
3912 (sel != 0) ? "1" : "0");
3913 nmem_destroy(tmpmem);
3917 case Z_APDU_searchResponse:
3918 yaz_log(log_api, "%p recv_apdu Search response", c);
3919 handle_search_response(c, apdu->u.searchResponse);
3920 if (send_sort_present(c) == zoom_complete)
3921 ZOOM_connection_remove_task(c);
3923 case Z_APDU_presentResponse:
3924 yaz_log(log_api, "%p recv_apdu Present response", c);
3925 handle_present_response(c, apdu->u.presentResponse);
3926 if (send_present(c) == zoom_complete)
3927 ZOOM_connection_remove_task(c);
3929 case Z_APDU_sortResponse:
3930 yaz_log(log_api, "%p recv_apdu Sort response", c);
3931 sort_response(c, apdu->u.sortResponse);
3932 if (send_present(c) == zoom_complete)
3933 ZOOM_connection_remove_task(c);
3935 case Z_APDU_scanResponse:
3936 yaz_log(log_api, "%p recv_apdu Scan response", c);
3937 scan_response(c, apdu->u.scanResponse);
3938 ZOOM_connection_remove_task(c);
3940 case Z_APDU_extendedServicesResponse:
3941 yaz_log(log_api, "%p recv_apdu Extended Services response", c);
3942 es_response(c, apdu->u.extendedServicesResponse);
3943 ZOOM_connection_remove_task(c);
3946 yaz_log(log_api, "%p recv_apdu Close PDU", c);
3947 if (!ZOOM_test_reconnect(c))
3949 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
3954 yaz_log(log_api, "%p Received unknown PDU", c);
3955 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
3961 static zoom_ret handle_srw_response(ZOOM_connection c,
3962 Z_SRW_searchRetrieveResponse *res)
3964 ZOOM_resultset resultset = 0;
3969 const char *syntax, *elementSetName;
3972 return zoom_complete;
3974 switch(c->tasks->which)
3976 case ZOOM_TASK_SEARCH:
3977 resultset = c->tasks->u.search.resultset;
3978 start = &c->tasks->u.search.start;
3979 count = &c->tasks->u.search.count;
3980 syntax = c->tasks->u.search.syntax;
3981 elementSetName = c->tasks->u.search.elementSetName;
3983 if (!c->tasks->u.search.recv_search_fired)
3985 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
3986 ZOOM_connection_put_event(c, event);
3987 c->tasks->u.search.recv_search_fired = 1;
3990 case ZOOM_TASK_RETRIEVE:
3991 resultset = c->tasks->u.retrieve.resultset;
3992 start = &c->tasks->u.retrieve.start;
3993 count = &c->tasks->u.retrieve.count;
3994 syntax = c->tasks->u.retrieve.syntax;
3995 elementSetName = c->tasks->u.retrieve.elementSetName;
3998 return zoom_complete;
4001 resultset->size = 0;
4003 if (res->resultSetId)
4004 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
4006 yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
4008 if (res->num_diagnostics > 0)
4010 set_SRU_error(c, &res->diagnostics[0]);
4014 if (res->numberOfRecords)
4015 resultset->size = *res->numberOfRecords;
4016 for (i = 0; i<res->num_records; i++)
4019 Z_SRW_record *sru_rec;
4020 Z_SRW_diagnostic *diag = 0;
4023 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
4024 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
4026 if (res->records[i].recordPosition &&
4027 *res->records[i].recordPosition > 0)
4028 pos = *res->records[i].recordPosition - 1;
4032 sru_rec = &res->records[i];
4034 npr->databaseName = 0;
4035 npr->which = Z_NamePlusRecord_databaseRecord;
4036 npr->u.databaseRecord = (Z_External *)
4037 odr_malloc(c->odr_in, sizeof(Z_External));
4038 npr->u.databaseRecord->descriptor = 0;
4039 npr->u.databaseRecord->direct_reference =
4040 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
4041 npr->u.databaseRecord->which = Z_External_octet;
4043 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
4044 odr_malloc(c->odr_in, sizeof(Odr_oct));
4045 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
4046 sru_rec->recordData_buf;
4047 npr->u.databaseRecord->u.octet_aligned->len =
4048 npr->u.databaseRecord->u.octet_aligned->size =
4049 sru_rec->recordData_len;
4051 if (sru_rec->recordSchema
4052 && !strcmp(sru_rec->recordSchema,
4053 "info:srw/schema/1/diagnostics-v1.1"))
4055 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
4056 sru_rec->recordData_len,
4060 record_cache_add(resultset, npr, pos, syntax, elementSetName,
4061 sru_rec->recordSchema, diag);
4065 if (*count + *start > resultset->size)
4066 *count = resultset->size - *start;
4070 nmem = odr_extract_mem(c->odr_in);
4071 nmem_transfer(odr_getmem(resultset->odr), nmem);
4075 return ZOOM_connection_srw_send_search(c);
4077 return zoom_complete;
4082 static void handle_srw_scan_response(ZOOM_connection c,
4083 Z_SRW_scanResponse *res)
4085 NMEM nmem = odr_extract_mem(c->odr_in);
4088 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
4090 scan = c->tasks->u.scan.scan;
4092 if (res->num_diagnostics > 0)
4093 set_SRU_error(c, &res->diagnostics[0]);
4095 scan->scan_response = 0;
4096 scan->srw_scan_response = res;
4097 nmem_transfer(odr_getmem(scan->odr), nmem);
4099 ZOOM_options_set_int(scan->options, "number", res->num_terms);
4105 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
4107 zoom_ret cret = zoom_complete;
4109 const char *addinfo = 0;
4110 const char *connection_head = z_HTTP_header_lookup(hres->headers,
4112 ZOOM_connection_set_mask(c, 0);
4113 yaz_log(log_details, "%p handle_http", c);
4115 if (!yaz_srw_check_content_type(hres))
4116 addinfo = "content-type";
4119 Z_SOAP *soap_package = 0;
4121 Z_SOAP_Handler soap_handlers[2] = {
4122 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
4125 ret = z_soap_codec(o, &soap_package,
4126 &hres->content_buf, &hres->content_len,
4128 if (!ret && soap_package->which == Z_SOAP_generic &&
4129 soap_package->u.generic->no == 0)
4131 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
4133 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
4134 ZOOM_options_setl(c->options, "sru_extra_response_data",
4135 sr->extraResponseData_buf, sr->extraResponseData_len);
4136 if (sr->which == Z_SRW_searchRetrieve_response)
4137 cret = handle_srw_response(c, sr->u.response);
4138 else if (sr->which == Z_SRW_scan_response)
4139 handle_srw_scan_response(c, sr->u.scan_response);
4143 else if (!ret && (soap_package->which == Z_SOAP_fault
4144 || soap_package->which == Z_SOAP_error))
4146 set_HTTP_error(c, hres->code,
4147 soap_package->u.fault->fault_code,
4148 soap_package->u.fault->fault_string);
4155 if (hres->code != 200)
4156 set_HTTP_error(c, hres->code, 0, 0);
4158 set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
4161 if (cret == zoom_complete)
4162 ZOOM_connection_remove_task(c);
4163 if (!strcmp(hres->version, "1.0"))
4165 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
4166 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
4171 /* HTTP 1.1: only if no close we stay alive .. */
4172 if (connection_head && !strcmp(connection_head, "close"))
4178 static int do_read(ZOOM_connection c)
4183 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
4184 ZOOM_connection_put_event(c, event);
4186 r = cs_get(c->cs, &c->buf_in, &c->len_in);
4187 more = cs_more(c->cs);
4188 yaz_log(log_details, "%p do_read len=%d more=%d", c, r, more);
4193 if (!ZOOM_test_reconnect(c))
4195 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4204 odr_reset(c->odr_in);
4205 odr_setbuf(c->odr_in, c->buf_in, r, 0);
4206 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
4207 ZOOM_connection_put_event(c, event);
4209 if (!z_GDU(c->odr_in, &gdu, 0, 0))
4212 int err = odr_geterrorx(c->odr_in, &x);
4214 const char *element = odr_getelement(c->odr_in);
4215 yaz_snprintf(msg, sizeof(msg),
4216 "ODR code %d:%d element=%s offset=%d",
4217 err, x, element ? element : "<unknown>",
4218 odr_offset(c->odr_in));
4219 set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg);
4222 FILE *ber_file = yaz_log_file();
4224 odr_dumpBER(ber_file, c->buf_in, r);
4231 z_GDU(c->odr_print, &gdu, 0, 0);
4232 if (gdu->which == Z_GDU_Z3950)
4233 recv_apdu(c, gdu->u.z3950);
4234 else if (gdu->which == Z_GDU_HTTP_Response)
4237 handle_http(c, gdu->u.HTTP_Response);
4239 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
4244 c->reconnect_ok = 0;
4249 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
4254 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
4255 ZOOM_connection_put_event(c, event);
4257 yaz_log(log_details, "%p do_write_ex len=%d", c, len_out);
4258 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
4260 yaz_log(log_details, "%p do_write_ex write failed", c);
4261 if (ZOOM_test_reconnect(c))
4263 return zoom_pending;
4265 if (c->state == STATE_CONNECTING)
4266 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4268 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4270 return zoom_complete;
4274 int mask = ZOOM_SELECT_EXCEPT;
4275 if (c->cs->io_pending & CS_WANT_WRITE)
4276 mask += ZOOM_SELECT_WRITE;
4277 if (c->cs->io_pending & CS_WANT_READ)
4278 mask += ZOOM_SELECT_READ;
4279 ZOOM_connection_set_mask(c, mask);
4280 yaz_log(log_details, "%p do_write_ex write incomplete mask=%d",
4285 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
4286 yaz_log(log_details, "%p do_write_ex write complete mask=%d",
4289 return zoom_pending;
4292 static zoom_ret do_write(ZOOM_connection c)
4294 return do_write_ex(c, c->buf_out, c->len_out);
4298 ZOOM_API(const char *)
4299 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
4301 return ZOOM_options_get(c->options, key);
4304 ZOOM_API(const char *)
4305 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
4307 return ZOOM_options_getl(c->options, key, lenp);
4311 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
4314 ZOOM_options_set(c->options, key, val);
4318 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
4319 const char *val, int len)
4321 ZOOM_options_setl(c->options, key, val, len);
4324 ZOOM_API(const char *)
4325 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
4327 return ZOOM_options_get(r->options, key);
4331 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
4334 ZOOM_options_set(r->options, key, val);
4339 ZOOM_connection_errcode(ZOOM_connection c)
4341 return ZOOM_connection_error(c, 0, 0);
4344 ZOOM_API(const char *)
4345 ZOOM_connection_errmsg(ZOOM_connection c)
4348 ZOOM_connection_error(c, &msg, 0);
4352 ZOOM_API(const char *)
4353 ZOOM_connection_addinfo(ZOOM_connection c)
4355 const char *addinfo;
4356 ZOOM_connection_error(c, 0, &addinfo);
4360 ZOOM_API(const char *)
4361 ZOOM_connection_diagset(ZOOM_connection c)
4363 const char *diagset;
4364 ZOOM_connection_error_x(c, 0, 0, &diagset);
4368 ZOOM_API(const char *)
4369 ZOOM_diag_str(int error)
4373 case ZOOM_ERROR_NONE:
4375 case ZOOM_ERROR_CONNECT:
4376 return "Connect failed";
4377 case ZOOM_ERROR_MEMORY:
4378 return "Out of memory";
4379 case ZOOM_ERROR_ENCODE:
4380 return "Encoding failed";
4381 case ZOOM_ERROR_DECODE:
4382 return "Decoding failed";
4383 case ZOOM_ERROR_CONNECTION_LOST:
4384 return "Connection lost";
4385 case ZOOM_ERROR_INIT:
4386 return "Init rejected";
4387 case ZOOM_ERROR_INTERNAL:
4388 return "Internal failure";
4389 case ZOOM_ERROR_TIMEOUT:
4391 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
4392 return "Unsupported protocol";
4393 case ZOOM_ERROR_UNSUPPORTED_QUERY:
4394 return "Unsupported query type";
4395 case ZOOM_ERROR_INVALID_QUERY:
4396 return "Invalid query";
4397 case ZOOM_ERROR_CQL_PARSE:
4398 return "CQL parsing error";
4399 case ZOOM_ERROR_CQL_TRANSFORM:
4400 return "CQL transformation error";
4401 case ZOOM_ERROR_CCL_CONFIG:
4402 return "CCL configuration error";
4403 case ZOOM_ERROR_CCL_PARSE:
4404 return "CCL parsing error";
4406 return diagbib1_str(error);
4411 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
4412 const char **addinfo, const char **diagset)
4414 int error = c->error;
4417 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
4418 *cp = ZOOM_diag_str(error);
4419 else if (!strcmp(c->diagset, "HTTP"))
4420 *cp = z_HTTP_errmsg(c->error);
4421 else if (!strcmp(c->diagset, "Bib-1"))
4422 *cp = ZOOM_diag_str(error);
4423 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
4424 *cp = yaz_diag_srw_str(c->error);
4426 *cp = "Unknown error and diagnostic set";
4429 *addinfo = c->addinfo ? c->addinfo : "";
4431 *diagset = c->diagset ? c->diagset : "";
4436 ZOOM_connection_error(ZOOM_connection c, const char **cp,
4437 const char **addinfo)
4439 return ZOOM_connection_error_x(c, cp, addinfo, 0);
4442 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
4444 ZOOM_Event event = 0;
4445 int r = cs_look(c->cs);
4446 yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
4451 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4452 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4454 ZOOM_connection_put_event(c, event);
4456 else if (r == CS_CONNECT)
4458 int ret = ret = cs_rcvconnect(c->cs);
4459 yaz_log(log_details, "%p ZOOM_connection_do_io "
4460 "cs_rcvconnect returned %d", c, ret);
4463 int mask = ZOOM_SELECT_EXCEPT;
4464 if (c->cs->io_pending & CS_WANT_WRITE)
4465 mask += ZOOM_SELECT_WRITE;
4466 if (c->cs->io_pending & CS_WANT_READ)
4467 mask += ZOOM_SELECT_READ;
4468 ZOOM_connection_set_mask(c, mask);
4469 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
4470 ZOOM_connection_put_event(c, event);
4474 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
4475 ZOOM_connection_put_event(c, event);
4477 if (c->proto == PROTO_Z3950)
4478 ZOOM_connection_send_init(c);
4481 /* no init request for SRW .. */
4482 assert(c->tasks->which == ZOOM_TASK_CONNECT);
4483 ZOOM_connection_remove_task(c);
4484 ZOOM_connection_set_mask(c, 0);
4485 ZOOM_connection_exec_task(c);
4487 c->state = STATE_ESTABLISHED;
4491 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
4497 if (mask & ZOOM_SELECT_EXCEPT)
4499 if (!ZOOM_test_reconnect(c))
4501 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
4506 if (mask & ZOOM_SELECT_READ)
4508 if (c->cs && (mask & ZOOM_SELECT_WRITE))
4514 ZOOM_connection_last_event(ZOOM_connection cs)
4517 return ZOOM_EVENT_NONE;
4518 return cs->last_event;
4522 static void cql2pqf_wrbuf_puts(const char *buf, void *client_data)
4524 WRBUF wrbuf = (WRBUF) client_data;
4525 wrbuf_puts(wrbuf, buf);
4529 * Returns an xmalloc()d string containing RPN that corresponds to the
4530 * CQL passed in. On error, sets the Connection object's error state
4531 * and returns a null pointer.
4532 * ### We could cache CQL parser and/or transformer in Connection.
4534 static char *cql2pqf(ZOOM_connection c, const char *cql)
4538 const char *cqlfile;
4539 cql_transform_t trans;
4542 parser = cql_parser_create();
4543 if ((error = cql_parser_string(parser, cql)) != 0) {
4544 cql_parser_destroy(parser);
4545 set_ZOOM_error(c, ZOOM_ERROR_CQL_PARSE, cql);
4549 cqlfile = ZOOM_connection_option_get(c, "cqlfile");
4552 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, "no CQL transform file");
4554 else if ((trans = cql_transform_open_fname(cqlfile)) == 0)
4557 sprintf(buf, "can't open CQL transform file '%.200s': %.200s",
4558 cqlfile, strerror(errno));
4559 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4563 WRBUF wrbuf_result = wrbuf_alloc();
4564 error = cql_transform(trans, cql_parser_result(parser),
4565 cql2pqf_wrbuf_puts, wrbuf_result);
4568 const char *addinfo;
4569 error = cql_transform_error(trans, &addinfo);
4570 sprintf(buf, "%.200s (addinfo=%.200s)",
4571 cql_strerror(error), addinfo);
4572 set_ZOOM_error(c, ZOOM_ERROR_CQL_TRANSFORM, buf);
4576 result = xstrdup(wrbuf_cstr(wrbuf_result));
4578 cql_transform_close(trans);
4579 wrbuf_destroy(wrbuf_result);
4581 cql_parser_destroy(parser);
4585 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
4589 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
4590 /* timeout and this connection was waiting */
4591 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
4593 ZOOM_connection_put_event(c, event);
4599 ZOOM_connection_process(ZOOM_connection c)
4605 event = ZOOM_connection_get_event(c);
4608 ZOOM_Event_destroy(event);
4611 ZOOM_connection_exec_task(c);
4612 event = ZOOM_connection_get_event(c);
4615 ZOOM_Event_destroy(event);
4622 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
4626 yaz_log(log_details, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
4628 for (i = 0; i<no; i++)
4630 ZOOM_connection c = cs[i];
4632 if (c && ZOOM_connection_process(c))
4638 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
4640 if (c->mask && mask)
4641 ZOOM_connection_do_io(c, mask);
4645 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
4648 return cs_fileno(c->cs);
4652 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
4660 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
4667 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
4669 return ZOOM_options_get_int(c->options, "timeout", 30);
4675 * c-file-style: "Stroustrup"
4676 * indent-tabs-mode: nil
4678 * vim: shiftwidth=4 tabstop=8 expandtab