1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM C interface.
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/otherinfo.h>
22 #include <yaz/diagbib1.h>
23 #include <yaz/charneg.h>
24 #include <yaz/query-charset.h>
25 #include <yaz/snprintf.h>
26 #include <yaz/facet.h>
28 #include <yaz/shptr.h>
34 static int log_api0 = 0;
35 static int log_details0 = 0;
37 static void resultset_destroy(ZOOM_resultset r);
38 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
40 static void initlog(void)
42 static int log_level_initialized = 0;
44 if (!log_level_initialized)
46 log_api0 = yaz_log_module_level("zoom");
47 log_details0 = yaz_log_module_level("zoomdetails");
48 log_level_initialized = 1;
52 void ZOOM_connection_remove_tasks(ZOOM_connection c);
54 void ZOOM_set_dset_error(ZOOM_connection c, int error,
56 const char *addinfo, const char *addinfo2)
63 if (!c->diagset || strcmp(dset, c->diagset))
66 c->diagset = xstrdup(dset);
67 /* remove integer part from SRW diagset .. */
68 if ((cp = strrchr(c->diagset, '/')))
71 if (addinfo && addinfo2)
73 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 3);
74 strcpy(c->addinfo, addinfo);
75 strcat(c->addinfo, ": ");
76 strcat(c->addinfo, addinfo2);
79 c->addinfo = xstrdup(addinfo);
80 if (error != ZOOM_ERROR_NONE)
82 yaz_log(c->log_api, "%p set_dset_error %s %s:%d %s %s",
83 c, c->host_port ? c->host_port : "<>", dset, error,
84 addinfo ? addinfo : "",
85 addinfo2 ? addinfo2 : "");
86 ZOOM_connection_remove_tasks(c);
90 int ZOOM_uri_to_code(const char *uri)
94 if ((cp = strrchr(uri, '/')))
101 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
103 ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
106 static void clear_error(ZOOM_connection c)
109 * If an error is tied to an operation then it's ok to clear: for
110 * example, a diagnostic returned from a search is cleared by a
111 * subsequent search. However, problems such as Connection Lost
112 * or Init Refused are not cleared, because they are not
113 * recoverable: doing another search doesn't help.
116 ZOOM_connection_remove_events(c);
119 case ZOOM_ERROR_CONNECT:
120 case ZOOM_ERROR_MEMORY:
121 case ZOOM_ERROR_DECODE:
122 case ZOOM_ERROR_CONNECTION_LOST:
123 case ZOOM_ERROR_INIT:
124 case ZOOM_ERROR_INTERNAL:
125 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
128 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
132 void ZOOM_connection_show_task(ZOOM_task task)
136 case ZOOM_TASK_SEARCH:
137 yaz_log(YLOG_LOG, "search p=%p", task);
139 case ZOOM_TASK_CONNECT:
140 yaz_log(YLOG_LOG, "connect p=%p", task);
143 yaz_log(YLOG_LOG, "scan p=%p", task);
148 void ZOOM_connection_show_tasks(ZOOM_connection c)
151 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
152 for (task = c->tasks; task; task = task->next)
153 ZOOM_connection_show_task(task);
156 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
158 ZOOM_task *taskp = &c->tasks;
160 taskp = &(*taskp)->next;
161 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
162 (*taskp)->running = 0;
163 (*taskp)->which = which;
169 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
171 return c->tasks ? 0 : 1;
174 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
176 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
178 task->next = c->tasks;
186 void ZOOM_connection_remove_task(ZOOM_connection c)
188 ZOOM_task task = c->tasks;
192 c->tasks = task->next;
195 case ZOOM_TASK_SEARCH:
196 resultset_destroy(task->u.search.resultset);
197 xfree(task->u.search.syntax);
198 xfree(task->u.search.elementSetName);
199 xfree(task->u.search.schema);
201 case ZOOM_TASK_CONNECT:
204 ZOOM_scanset_destroy(task->u.scan.scan);
206 case ZOOM_TASK_PACKAGE:
207 ZOOM_package_destroy(task->u.package);
210 resultset_destroy(task->u.sort.resultset);
211 ZOOM_query_destroy(task->u.sort.q);
220 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
221 ZOOM_connection_put_event(c, event);
226 void ZOOM_connection_remove_tasks(ZOOM_connection c)
229 ZOOM_connection_remove_task(c);
232 static void odr_wrbuf_write(ODR o, void *handle, int type,
233 const char *buf, int len)
235 WRBUF w = (WRBUF) handle;
236 wrbuf_write(w, buf, len);
239 ZOOM_API(ZOOM_connection)
240 ZOOM_connection_create(ZOOM_options options)
242 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
246 c->log_api = log_api0;
247 c->log_details = log_details0;
249 yaz_log(c->log_api, "%p ZOOM_connection_create", c);
251 c->proto = PROTO_Z3950;
253 ZOOM_connection_set_mask(c, 0);
255 c->state = STATE_IDLE;
258 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
265 c->options = ZOOM_options_create_with_parent(options);
271 c->charset = c->lang = 0;
281 c->url_authentication = 0;
283 c->maximum_record_size = 0;
284 c->preferred_message_size = 0;
286 c->odr_in = odr_createmem(ODR_DECODE);
287 c->odr_out = odr_createmem(ODR_ENCODE);
292 c->support_named_resultsets = 0;
293 c->last_event = ZOOM_EVENT_NONE;
295 c->m_queue_front = 0;
301 c->saveAPDU_wrbuf = 0;
303 #if HAVE_LIBMEMCACHED_MEMCACHED_H
309 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
313 odr_destroy(c->odr_save);
318 c->odr_save = odr_createmem(ODR_PRINT);
319 odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
323 /* set database names. Take local databases (if set); otherwise
324 take databases given in ZURL (if set); otherwise use Default */
325 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
328 char **databaseNames;
329 const char *cp = ZOOM_options_get(options, "databaseName");
331 if ((!cp || !*cp) && con->host_port)
332 cs_get_host_args(con->host_port, &cp);
335 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
336 return databaseNames;
339 ZOOM_API(ZOOM_connection)
340 ZOOM_connection_new(const char *host, int portnum)
342 ZOOM_connection c = ZOOM_connection_create(0);
344 ZOOM_connection_connect(c, host, portnum);
348 static zoom_sru_mode get_sru_mode_from_string(const char *s)
351 return zoom_sru_soap;
352 if (!yaz_matchstr(s, "soap"))
353 return zoom_sru_soap;
354 else if (!yaz_matchstr(s, "get"))
356 else if (!yaz_matchstr(s, "post"))
357 return zoom_sru_post;
358 else if (!yaz_matchstr(s, "solr"))
359 return zoom_sru_solr;
360 return zoom_sru_error;
364 ZOOM_connection_connect(ZOOM_connection c,
365 const char *host, int portnum)
368 const char *http_lead;
372 yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
373 c, host ? host : "null", portnum);
375 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
376 ZOOM_connection_remove_tasks(c);
380 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
381 odr_destroy(c->odr_print);
383 if (ZOOM_options_get_bool(c->options, "apdulog", 0))
385 c->odr_print = odr_createmem(ODR_PRINT);
386 odr_setprint(c->odr_print, yaz_log_file());
393 yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
397 yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
400 val = ZOOM_options_get(c->options, "proxy");
403 yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
404 c->proxy = xstrdup(val);
409 val = ZOOM_options_get(c->options, "tproxy");
412 yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
413 c->tproxy = xstrdup(val);
418 val = ZOOM_options_get(c->options, "charset");
421 yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
422 c->charset = xstrdup(val);
426 val = ZOOM_options_get(c->options, "lang");
429 yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
430 c->lang = xstrdup(val);
435 val = ZOOM_options_get(c->options, "sru");
436 if (val && *val && !strstr(host, "://"))
437 http_lead = "http://";
440 c->sru_mode = get_sru_mode_from_string(val);
448 sprintf(hostn, "%.80s:%d", host, portnum);
451 c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
452 strcpy(c->host_port, http_lead);
453 strcat(c->host_port, host);
458 * If the "<scheme>:" part of the host string is preceded by one
459 * or more comma-separated <name>=<value> pairs, these are taken
460 * to be options to be set on the connection object. Among other
461 * applications, this facility can be used to embed authentication
463 * user=admin,password=secret,tcp:localhost:9999
465 char *remainder = c->host_port;
466 char *pcolon = strchr(remainder, ':');
469 while ((pcomma = strchr(remainder, ',')) != 0 &&
470 (pcolon == 0 || pcomma < pcolon))
473 if ((pequals = strchr(remainder, '=')) != 0)
476 ZOOM_connection_option_set(c, remainder, pequals+1);
478 remainder = pcomma+1;
481 if (remainder != c->host_port)
483 remainder = xstrdup(remainder);
485 c->host_port = remainder;
489 xfree(c->sru_version);
490 val = ZOOM_options_get(c->options, "sru_version");
491 c->sru_version = xstrdup(val ? val : "1.2");
493 ZOOM_options_set(c->options, "host", c->host_port);
495 xfree(c->cookie_out);
497 val = ZOOM_options_get(c->options, "cookie");
500 yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
501 c->cookie_out = xstrdup(val);
506 val = ZOOM_options_get(c->options, "clientIP");
509 yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
511 c->client_IP = xstrdup(val);
516 val = ZOOM_options_get(c->options, "group");
518 c->group = xstrdup(val);
522 val = ZOOM_options_get(c->options, "user");
524 c->user = xstrdup(val);
528 val = ZOOM_options_get(c->options, "password");
530 val = ZOOM_options_get(c->options, "pass");
532 c->password = xstrdup(val);
534 val = ZOOM_options_get(c->options, "authenticationMode");
535 if (val && !strcmp(val, "url"))
536 c->url_authentication = 1;
538 c->url_authentication = 0;
540 c->maximum_record_size =
541 ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
542 c->preferred_message_size =
543 ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
545 c->async = ZOOM_options_get_bool(c->options, "async", 0);
547 yaz_cookies_destroy(c->cookies);
548 c->cookies = yaz_cookies_create();
550 #if HAVE_LIBMEMCACHED_MEMCACHED_H
553 memcached_free(c->mc_st);
557 val = ZOOM_options_get(c->options, "memcached");
560 #if HAVE_LIBMEMCACHED_MEMCACHED_H
561 c->mc_st = memcached(val, strlen(val));
564 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
568 ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
573 if (c->sru_mode == zoom_sru_error)
575 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
576 ZOOM_connection_remove_tasks(c);
580 yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
581 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
585 while (ZOOM_event(1, &c))
590 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
594 /* remove ourselves from the resultsets in connection */
595 ZOOM_resultset *rp = &r->connection->resultsets;
598 assert(*rp); /* we must be in this list!! */
600 { /* OK, we're here - take us out of it */
611 ZOOM_connection_destroy(ZOOM_connection c)
616 yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
618 #if HAVE_LIBMEMCACHED_MEMCACHED_H
620 memcached_free(c->mc_st);
625 for (r = c->resultsets; r; r = r->next)
631 odr_destroy(c->odr_in);
632 odr_destroy(c->odr_out);
634 odr_destroy(c->odr_save);
637 odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
638 odr_destroy(c->odr_print);
640 ZOOM_options_destroy(c->options);
641 ZOOM_connection_remove_tasks(c);
642 ZOOM_connection_remove_events(c);
648 xfree(c->cookie_out);
654 xfree(c->sru_version);
655 yaz_cookies_destroy(c->cookies);
656 wrbuf_destroy(c->saveAPDU_wrbuf);
660 void ZOOM_resultset_addref(ZOOM_resultset r)
664 yaz_mutex_enter(r->mutex);
666 yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
668 yaz_mutex_leave(r->mutex);
672 static int g_resultsets = 0;
673 static YAZ_MUTEX g_resultset_mutex = 0;
675 /* TODO We need to initialize this before running threaded:
676 * call resultset_use(0) */
678 static int resultset_use(int delta) {
680 if (g_resultset_mutex == 0)
681 yaz_mutex_create(&g_resultset_mutex);
682 yaz_mutex_enter(g_resultset_mutex);
683 g_resultsets += delta;
684 resultset_count = g_resultsets;
685 yaz_mutex_leave(g_resultset_mutex);
686 return resultset_count;
689 int resultsets_count(void) {
690 return resultset_use(0);
693 ZOOM_resultset ZOOM_resultset_create(void)
696 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
700 yaz_log(log_details0, "%p ZOOM_resultset_create", r);
703 r->odr = odr_createmem(ODR_ENCODE);
707 for (i = 0; i<RECORD_HASH_SIZE; i++)
708 r->record_hash[i] = 0;
712 r->databaseNames = 0;
713 r->num_databaseNames = 0;
718 yaz_mutex_create(&r->mutex);
721 WRBUF w = wrbuf_alloc();
722 YAZ_SHPTR_INIT(r->record_wrbuf, w);
731 ZOOM_API(ZOOM_resultset)
732 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
735 ZOOM_query s = ZOOM_query_create();
737 ZOOM_query_prefix(s, q);
739 r = ZOOM_connection_search(c, s);
740 ZOOM_query_destroy(s);
744 ZOOM_API(ZOOM_resultset)
745 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
747 ZOOM_resultset r = ZOOM_resultset_create();
751 const char *syntax, *elementSetName, *schema;
752 yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
753 r->r_sort_spec = ZOOM_query_get_sortspec(q);
755 ZOOM_query_addref(q);
757 r->options = ZOOM_options_create_with_parent(c->options);
759 start = ZOOM_options_get_int(r->options, "start", 0);
760 count = ZOOM_options_get_int(r->options, "count", 0);
762 /* If "presentChunk" is defined use that; otherwise "step" */
763 const char *cp = ZOOM_options_get(r->options, "presentChunk");
764 r->step = ZOOM_options_get_int(r->options,
765 (cp != 0 ? "presentChunk": "step"), 0);
767 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
768 cp = ZOOM_options_get(r->options, "setname");
770 r->setname = xstrdup(cp);
772 r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
776 r->next = c->resultsets;
779 #if HAVE_LIBMEMCACHED_MEMCACHED_H
780 r->mc_key = wrbuf_alloc();
781 wrbuf_puts(r->mc_key, "0;");
782 wrbuf_puts(r->mc_key, c->host_port);
783 wrbuf_puts(r->mc_key, ";");
785 wrbuf_puts(r->mc_key, c->user);
786 wrbuf_puts(r->mc_key, ";");
788 wrbuf_puts(r->mc_key, c->group);
789 wrbuf_puts(r->mc_key, ";");
791 wrbuf_sha1_puts(r->mc_key, c->password, 1);
792 wrbuf_puts(r->mc_key, ";");
793 wrbuf_sha1_puts(r->mc_key, ZOOM_query_get_query_string(q), 1);
794 wrbuf_puts(r->mc_key, ";");
795 /* TODO: add sorting */
800 memcached_return_t rc;
801 char *v = memcached_get(c->mc_st, wrbuf_buf(r->mc_key),
802 wrbuf_len(r->mc_key), &v_len, &flags, &rc);
806 WRBUF w = wrbuf_alloc();
808 wrbuf_write(w, v, v_len);
810 r->size = odr_atoi(wrbuf_cstr(w));
812 yaz_log(YLOG_LOG, "For key %s got value %s",
813 wrbuf_cstr(r->mc_key), wrbuf_cstr(w));
816 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
817 ZOOM_connection_put_event(c, event);
822 yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key));
827 if (c->host_port && c->proto == PROTO_HTTP)
831 yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
832 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
836 yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
841 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
842 task->u.search.resultset = r;
843 task->u.search.start = start;
844 task->u.search.count = count;
845 task->u.search.recv_search_fired = 0;
847 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
848 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
849 elementSetName = ZOOM_options_get(r->options, "elementSetName");
850 task->u.search.elementSetName = elementSetName ?
851 xstrdup(elementSetName) : 0;
852 schema = ZOOM_options_get(r->options, "schema");
853 task->u.search.schema = schema ? xstrdup(schema) : 0;
855 ZOOM_resultset_addref(r);
859 while (ZOOM_event(1, &c))
866 ZOOM_resultset_sort(ZOOM_resultset r,
867 const char *sort_type, const char *sort_spec)
869 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
873 ZOOM_resultset_sort1(ZOOM_resultset r,
874 const char *sort_type, const char *sort_spec)
876 ZOOM_connection c = r->connection;
880 newq = ZOOM_query_create();
881 if (ZOOM_query_sortby(newq, sort_spec) < 0)
884 yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
885 r, r, sort_type, sort_spec);
889 if (c->host_port && c->proto == PROTO_HTTP)
893 yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
894 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
898 yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
904 ZOOM_resultset_cache_reset(r);
905 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
906 task->u.sort.resultset = r;
907 task->u.sort.q = newq;
909 ZOOM_resultset_addref(r);
913 while (ZOOM_event(1, &c))
921 ZOOM_resultset_destroy(ZOOM_resultset r)
923 resultset_destroy(r);
926 static void resultset_destroy(ZOOM_resultset r)
930 yaz_mutex_enter(r->mutex);
932 yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
934 if (r->refcount == 0)
936 yaz_mutex_leave(r->mutex);
938 yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
939 ZOOM_resultset_cache_reset(r);
940 ZOOM_resultset_release(r);
941 ZOOM_query_destroy(r->query);
942 ZOOM_options_destroy(r->options);
945 yaz_mutex_destroy(&r->mutex);
947 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
949 wrbuf_destroy(r->mc_key);
954 yaz_mutex_leave(r->mutex);
958 ZOOM_resultset_size(ZOOM_resultset r)
963 int ZOOM_test_reconnect(ZOOM_connection c)
967 if (!c->reconnect_ok)
969 ZOOM_connection_close(c);
971 c->tasks->running = 0;
972 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
974 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
975 ZOOM_connection_put_event(c, event);
980 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
981 int force_sync, int start, int count)
986 const char *syntax, *elementSetName;
990 yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
991 " count=%d", r, force_sync, start, count);
996 if (c->host_port && c->proto == PROTO_HTTP)
1000 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
1001 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1005 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
1007 c->reconnect_ok = 1;
1010 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
1011 task->u.search.resultset = r;
1012 task->u.search.start = start;
1013 task->u.search.count = count;
1015 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1016 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
1017 elementSetName = ZOOM_options_get(r->options, "elementSetName");
1018 task->u.search.elementSetName = elementSetName
1019 ? xstrdup(elementSetName) : 0;
1021 cp = ZOOM_options_get(r->options, "schema");
1022 task->u.search.schema = cp ? xstrdup(cp) : 0;
1024 ZOOM_resultset_addref(r);
1026 if (!r->connection->async || force_sync)
1027 while (r->connection && ZOOM_event(1, &r->connection))
1032 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1033 size_t start, size_t count)
1035 int force_present = 0;
1039 yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1040 r, r, (long) start, (long) count);
1043 ZOOM_resultset_retrieve(r, force_present, start, count);
1047 for (i = 0; i< count; i++)
1048 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1053 ZOOM_resultset_facets_size(ZOOM_resultset r) {
1054 return r->num_facets;
1057 ZOOM_API(ZOOM_facet_field)
1058 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
1059 int num = r->num_facets;
1060 ZOOM_facet_field *facets = r->facets;
1062 for (index = 0; index < num; index++) {
1063 if (!strcmp(facets[index]->facet_name, name)) {
1064 return facets[index];
1070 ZOOM_API(ZOOM_facet_field)
1071 ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
1072 int num = r->num_facets;
1073 ZOOM_facet_field *facets = r->facets;
1074 if (index >= 0 && index < num) {
1075 return facets[index];
1080 ZOOM_API(ZOOM_facet_field *)
1081 ZOOM_resultset_facets(ZOOM_resultset r)
1086 ZOOM_API(const char**)
1087 ZOOM_resultset_facets_names(ZOOM_resultset r)
1089 return (const char **) r->facets_names;
1092 ZOOM_API(const char*)
1093 ZOOM_facet_field_name(ZOOM_facet_field field)
1095 return field->facet_name;
1099 ZOOM_facet_field_term_count(ZOOM_facet_field field)
1101 return field->num_terms;
1104 ZOOM_API(const char*)
1105 ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1106 *freq = field->facet_terms[idx].frequency;
1107 return field->facet_terms[idx].term;
1111 static void get_cert(ZOOM_connection c)
1116 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1118 ZOOM_connection_option_setl(c, "sslPeerCert",
1119 cert_buf, cert_len);
1124 static zoom_ret do_connect_host(ZOOM_connection c,
1125 const char *logical_url);
1127 static zoom_ret do_connect(ZOOM_connection c)
1129 return do_connect_host(c, c->host_port);
1132 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1138 c->cs = cs_create_host_proxy(logical_url, 0, &add,
1139 c->tproxy ? c->tproxy : c->proxy);
1141 if (c->cs && c->cs->protocol == PROTO_HTTP)
1144 c->proto = PROTO_HTTP;
1146 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1147 ZOOM_connection_close(c);
1148 return zoom_complete;
1153 int ret = cs_connect(c->cs, add);
1156 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1157 ZOOM_connection_put_event(c, event);
1159 if (c->proto == PROTO_Z3950)
1160 ZOOM_connection_Z3950_send_init(c);
1163 /* no init request for SRW .. */
1164 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1165 ZOOM_connection_remove_task(c);
1166 ZOOM_connection_set_mask(c, 0);
1167 ZOOM_connection_exec_task(c);
1169 c->state = STATE_ESTABLISHED;
1170 return zoom_pending;
1174 int mask = ZOOM_SELECT_EXCEPT;
1175 if (c->cs->io_pending & CS_WANT_WRITE)
1176 mask += ZOOM_SELECT_WRITE;
1177 if (c->cs->io_pending & CS_WANT_READ)
1178 mask += ZOOM_SELECT_READ;
1179 ZOOM_connection_set_mask(c, mask);
1180 c->state = STATE_CONNECTING;
1181 return zoom_pending;
1184 c->state = STATE_IDLE;
1185 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1186 return zoom_complete;
1189 /* returns 1 if PDU was sent OK (still pending )
1190 0 if PDU was not sent OK (nothing to wait for)
1193 ZOOM_API(ZOOM_record)
1194 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1196 const char *syntax =
1197 ZOOM_options_get(s->options, "preferredRecordSyntax");
1198 const char *elementSetName =
1199 ZOOM_options_get(s->options, "elementSetName");
1200 const char *schema =
1201 ZOOM_options_get(s->options, "schema");
1203 return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
1206 ZOOM_API(ZOOM_record)
1207 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1209 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1214 * MIKE: I think force_sync should always be zero, but I don't
1215 * want to make this change until I get the go-ahead from
1216 * Adam, in case something depends on the old synchronous
1220 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1221 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1222 rec = ZOOM_resultset_record_immediate(r, pos);
1227 ZOOM_API(ZOOM_scanset)
1228 ZOOM_connection_scan(ZOOM_connection c, const char *start)
1231 ZOOM_query q = ZOOM_query_create();
1233 ZOOM_query_prefix(q, start);
1235 s = ZOOM_connection_scan1(c, q);
1236 ZOOM_query_destroy(q);
1241 ZOOM_API(ZOOM_scanset)
1242 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1244 ZOOM_scanset scan = 0;
1245 Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1249 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1250 scan->connection = c;
1251 scan->odr = odr_createmem(ODR_DECODE);
1252 scan->options = ZOOM_options_create_with_parent(c->options);
1254 scan->scan_response = 0;
1255 scan->srw_scan_response = 0;
1258 ZOOM_query_addref(q);
1259 scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1260 &scan->num_databaseNames,
1265 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1266 task->u.scan.scan = scan;
1271 while (ZOOM_event(1, &c))
1279 ZOOM_scanset_destroy(ZOOM_scanset scan)
1284 if (scan->refcount == 0)
1286 ZOOM_query_destroy(scan->query);
1288 odr_destroy(scan->odr);
1290 ZOOM_options_destroy(scan->options);
1295 static zoom_ret send_package(ZOOM_connection c)
1299 yaz_log(c->log_details, "%p send_package", c);
1301 return zoom_complete;
1302 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1304 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1305 ZOOM_connection_put_event(c, event);
1307 c->buf_out = c->tasks->u.package->buf_out;
1308 c->len_out = c->tasks->u.package->len_out;
1310 return ZOOM_send_buf(c);
1314 ZOOM_scanset_size(ZOOM_scanset scan)
1319 if (scan->scan_response && scan->scan_response->entries)
1320 return scan->scan_response->entries->num_entries;
1321 else if (scan->srw_scan_response)
1322 return scan->srw_scan_response->num_terms;
1326 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1328 const char **value_term, size_t *value_len,
1329 const char **disp_term, size_t *disp_len)
1331 size_t noent = ZOOM_scanset_size(scan);
1342 if (scan->scan_response)
1344 Z_ScanResponse *res = scan->scan_response;
1345 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1347 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1349 *value_term = (const char *) t->term->u.general->buf;
1350 *value_len = t->term->u.general->len;
1353 *disp_term = t->displayTerm;
1354 *disp_len = strlen(*disp_term);
1356 else if (t->term->which == Z_Term_general)
1358 *disp_term = (const char *) t->term->u.general->buf;
1359 *disp_len = t->term->u.general->len;
1361 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1364 if (scan->srw_scan_response)
1366 Z_SRW_scanResponse *res = scan->srw_scan_response;
1367 Z_SRW_scanTerm *t = res->terms + pos;
1370 *value_term = t->value;
1371 *value_len = strlen(*value_term);
1374 *disp_term = t->displayTerm;
1376 *disp_term = t->value;
1377 *disp_len = strlen(*disp_term);
1378 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1383 ZOOM_API(const char *)
1384 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1385 size_t *occ, size_t *len)
1387 const char *value_term = 0;
1388 size_t value_len = 0;
1389 const char *disp_term = 0;
1390 size_t disp_len = 0;
1392 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1393 &disp_term, &disp_len);
1399 ZOOM_API(const char *)
1400 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1401 size_t *occ, size_t *len)
1403 const char *value_term = 0;
1404 size_t value_len = 0;
1405 const char *disp_term = 0;
1406 size_t disp_len = 0;
1408 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1409 &disp_term, &disp_len);
1415 ZOOM_API(const char *)
1416 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1418 return ZOOM_options_get(scan->options, key);
1422 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1425 ZOOM_options_set(scan->options, key, val);
1429 ZOOM_API(ZOOM_package)
1430 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1432 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1435 p->odr_out = odr_createmem(ODR_ENCODE);
1436 p->options = ZOOM_options_create_with_parent2(options, c->options);
1444 ZOOM_package_destroy(ZOOM_package p)
1449 if (p->refcount == 0)
1451 odr_destroy(p->odr_out);
1454 ZOOM_options_destroy(p->options);
1459 ZOOM_API(const char *)
1460 ZOOM_package_option_get(ZOOM_package p, const char *key)
1462 return ZOOM_options_get(p->options, key);
1465 ZOOM_API(const char *)
1466 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1468 return ZOOM_options_getl(p->options, key, lenp);
1472 ZOOM_package_option_set(ZOOM_package p, const char *key,
1475 ZOOM_options_set(p->options, key, val);
1479 ZOOM_package_option_setl(ZOOM_package p, const char *key,
1480 const char *val, int len)
1482 ZOOM_options_setl(p->options, key, val, len);
1486 ZOOM_connection_exec_task(ZOOM_connection c)
1488 ZOOM_task task = c->tasks;
1489 zoom_ret ret = zoom_complete;
1493 yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1494 c, task->which, task->running);
1495 if (c->error != ZOOM_ERROR_NONE)
1497 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1498 "removing tasks because of error = %d", c, c->error);
1499 ZOOM_connection_remove_tasks(c);
1504 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1505 "task already running", c);
1509 ret = zoom_complete;
1510 if (c->cs || task->which == ZOOM_TASK_CONNECT)
1512 switch (task->which)
1514 case ZOOM_TASK_SEARCH:
1515 if (c->proto == PROTO_HTTP)
1516 ret = ZOOM_connection_srw_send_search(c);
1518 ret = ZOOM_connection_Z3950_send_search(c);
1520 case ZOOM_TASK_CONNECT:
1521 ret = do_connect(c);
1523 case ZOOM_TASK_SCAN:
1524 if (c->proto == PROTO_HTTP)
1525 ret = ZOOM_connection_srw_send_scan(c);
1527 ret = ZOOM_connection_Z3950_send_scan(c);
1529 case ZOOM_TASK_PACKAGE:
1530 ret = send_package(c);
1532 case ZOOM_TASK_SORT:
1533 c->tasks->u.sort.resultset->r_sort_spec =
1534 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1535 ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1541 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1542 "remove tasks because no connection exist", c);
1543 ZOOM_connection_remove_tasks(c);
1545 if (ret == zoom_complete)
1547 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1548 "task removed (complete)", c);
1549 ZOOM_connection_remove_task(c);
1552 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1559 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1561 Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1563 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1564 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1566 yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1567 if (c->user && c->password)
1569 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1570 c->user, c->password);
1572 return ZOOM_send_GDU(c, gdu);
1575 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1579 int r = z_GDU(c->odr_out, &gdu, 0, 0);
1581 return zoom_complete;
1583 z_GDU(c->odr_print, &gdu, 0, 0);
1585 z_GDU(c->odr_save, &gdu, 0, 0);
1586 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1587 odr_reset(c->odr_out);
1589 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1590 ZOOM_connection_put_event(c, event);
1592 return ZOOM_send_buf(c);
1596 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1597 const char *addinfo, const char *addinfo2)
1599 ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1604 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1606 zoom_ret cret = zoom_complete;
1609 const char *connection_head = z_HTTP_header_lookup(hres->headers,
1611 const char *location;
1613 ZOOM_connection_set_mask(c, 0);
1614 yaz_log(c->log_details, "%p handle_http", c);
1616 yaz_cookies_response(c->cookies, hres);
1617 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1618 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1621 if (c->no_redirects > 10)
1623 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1624 c->no_redirects = 0;
1625 ZOOM_connection_close(c);
1629 /* since redirect may change host we just reconnect. A smarter
1630 implementation might check whether it's the same server */
1632 int host_change = 0;
1633 location = yaz_check_location(c->odr_in, c->host_port,
1634 location, &host_change);
1635 if (do_connect_host(c, location) == zoom_complete)
1636 return; /* connect failed.. */
1637 send_HTTP_redirect(c, location);
1643 ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1646 if (c->no_redirects) /* end of redirect. change hosts again */
1647 ZOOM_connection_close(c);
1649 c->no_redirects = 0;
1653 if (hres->code != 200)
1654 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1657 yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1659 ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1661 ZOOM_connection_close(c);
1663 if (cret == zoom_complete)
1665 yaz_log(c->log_details, "removing tasks in handle_http");
1666 ZOOM_connection_remove_task(c);
1670 if (!strcmp(hres->version, "1.0"))
1672 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1673 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1678 /* HTTP 1.1: only if no close we stay alive.. */
1679 if (connection_head && !strcmp(connection_head, "close"))
1684 ZOOM_connection_close(c);
1687 c->tasks->running = 0;
1688 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1689 c->reconnect_ok = 0;
1693 c->reconnect_ok = 1; /* if the server closes anyway */
1698 static int do_read(ZOOM_connection c)
1703 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1704 ZOOM_connection_put_event(c, event);
1706 r = cs_get(c->cs, &c->buf_in, &c->len_in);
1707 more = cs_more(c->cs);
1708 yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1713 if (!ZOOM_test_reconnect(c))
1715 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1716 ZOOM_connection_close(c);
1724 odr_reset(c->odr_in);
1725 odr_setbuf(c->odr_in, c->buf_in, r, 0);
1726 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1727 ZOOM_connection_put_event(c, event);
1729 if (!z_GDU(c->odr_in, &gdu, 0, 0))
1732 int err = odr_geterrorx(c->odr_in, &x);
1734 const char *element = odr_getelement(c->odr_in);
1735 yaz_snprintf(msg, sizeof(msg),
1736 "ODR code %d:%d element=%s offset=%d",
1737 err, x, element ? element : "<unknown>",
1738 odr_offset(c->odr_in));
1739 ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1742 FILE *ber_file = yaz_log_file();
1744 odr_dumpBER(ber_file, c->buf_in, r);
1746 ZOOM_connection_close(c);
1751 z_GDU(c->odr_print, &gdu, 0, 0);
1753 z_GDU(c->odr_save, &gdu, 0, 0);
1754 if (gdu->which == Z_GDU_Z3950)
1755 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1756 else if (gdu->which == Z_GDU_HTTP_Response)
1759 handle_http(c, gdu->u.HTTP_Response);
1761 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1762 ZOOM_connection_close(c);
1770 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1775 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1776 ZOOM_connection_put_event(c, event);
1778 yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1779 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1781 yaz_log(c->log_details, "%p do_write_ex write failed", c);
1782 if (ZOOM_test_reconnect(c))
1784 return zoom_pending;
1786 if (c->state == STATE_CONNECTING)
1787 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1789 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1790 ZOOM_connection_close(c);
1791 return zoom_complete;
1795 int mask = ZOOM_SELECT_EXCEPT;
1796 if (c->cs->io_pending & CS_WANT_WRITE)
1797 mask += ZOOM_SELECT_WRITE;
1798 if (c->cs->io_pending & CS_WANT_READ)
1799 mask += ZOOM_SELECT_READ;
1800 ZOOM_connection_set_mask(c, mask);
1801 yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1806 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1807 yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1810 return zoom_pending;
1813 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1815 return do_write_ex(c, c->buf_out, c->len_out);
1819 ZOOM_API(const char *)
1820 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1822 if (!strcmp(key, "APDU"))
1824 return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1827 return ZOOM_options_get(c->options, key);
1830 ZOOM_API(const char *)
1831 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1833 if (!strcmp(key, "APDU"))
1835 if (c->saveAPDU_wrbuf)
1837 *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1838 return wrbuf_cstr(c->saveAPDU_wrbuf);
1847 return ZOOM_options_getl(c->options, key, lenp);
1851 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1854 if (!strcmp(key, "saveAPDU"))
1856 if (val && strcmp(val, "0"))
1858 if (!c->saveAPDU_wrbuf)
1859 c->saveAPDU_wrbuf = wrbuf_alloc();
1861 wrbuf_rewind(c->saveAPDU_wrbuf);
1865 wrbuf_destroy(c->saveAPDU_wrbuf);
1866 c->saveAPDU_wrbuf = 0;
1868 ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1871 ZOOM_options_set(c->options, key, val);
1875 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1876 const char *val, int len)
1878 ZOOM_options_setl(c->options, key, val, len);
1881 ZOOM_API(const char *)
1882 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1884 return ZOOM_options_get(r->options, key);
1888 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1891 ZOOM_options_set(r->options, key, val);
1896 ZOOM_connection_errcode(ZOOM_connection c)
1898 return ZOOM_connection_error(c, 0, 0);
1901 ZOOM_API(const char *)
1902 ZOOM_connection_errmsg(ZOOM_connection c)
1905 ZOOM_connection_error(c, &msg, 0);
1909 ZOOM_API(const char *)
1910 ZOOM_connection_addinfo(ZOOM_connection c)
1912 const char *addinfo;
1913 ZOOM_connection_error(c, 0, &addinfo);
1917 ZOOM_API(const char *)
1918 ZOOM_connection_diagset(ZOOM_connection c)
1920 const char *diagset;
1921 ZOOM_connection_error_x(c, 0, 0, &diagset);
1925 ZOOM_API(const char *)
1926 ZOOM_diag_str(int error)
1930 case ZOOM_ERROR_NONE:
1932 case ZOOM_ERROR_CONNECT:
1933 return "Connect failed";
1934 case ZOOM_ERROR_MEMORY:
1935 return "Out of memory";
1936 case ZOOM_ERROR_ENCODE:
1937 return "Encoding failed";
1938 case ZOOM_ERROR_DECODE:
1939 return "Decoding failed";
1940 case ZOOM_ERROR_CONNECTION_LOST:
1941 return "Connection lost";
1942 case ZOOM_ERROR_INIT:
1943 return "Init rejected";
1944 case ZOOM_ERROR_INTERNAL:
1945 return "Internal failure";
1946 case ZOOM_ERROR_TIMEOUT:
1948 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1949 return "Unsupported protocol";
1950 case ZOOM_ERROR_UNSUPPORTED_QUERY:
1951 return "Unsupported query type";
1952 case ZOOM_ERROR_INVALID_QUERY:
1953 return "Invalid query";
1954 case ZOOM_ERROR_CQL_PARSE:
1955 return "CQL parsing error";
1956 case ZOOM_ERROR_CQL_TRANSFORM:
1957 return "CQL transformation error";
1958 case ZOOM_ERROR_CCL_CONFIG:
1959 return "CCL configuration error";
1960 case ZOOM_ERROR_CCL_PARSE:
1961 return "CCL parsing error";
1962 case ZOOM_ERROR_ES_INVALID_ACTION:
1963 return "Extended Service. invalid action";
1964 case ZOOM_ERROR_ES_INVALID_VERSION:
1965 return "Extended Service. invalid version";
1966 case ZOOM_ERROR_ES_INVALID_SYNTAX:
1967 return "Extended Service. invalid syntax";
1968 case ZOOM_ERROR_MEMCACHED:
1971 return diagbib1_str(error);
1976 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1977 const char **addinfo, const char **diagset)
1979 int error = c->error;
1982 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1983 *cp = ZOOM_diag_str(error);
1984 else if (!strcmp(c->diagset, "HTTP"))
1985 *cp = z_HTTP_errmsg(c->error);
1986 else if (!strcmp(c->diagset, "Bib-1"))
1987 *cp = ZOOM_diag_str(error);
1988 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1989 *cp = yaz_diag_srw_str(c->error);
1991 *cp = "Unknown error and diagnostic set";
1994 *addinfo = c->addinfo ? c->addinfo : "";
1996 *diagset = c->diagset ? c->diagset : "";
2001 ZOOM_connection_error(ZOOM_connection c, const char **cp,
2002 const char **addinfo)
2004 return ZOOM_connection_error_x(c, cp, addinfo, 0);
2007 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
2009 ZOOM_Event event = 0;
2010 int r = cs_look(c->cs);
2011 yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
2016 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2017 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2018 ZOOM_connection_close(c);
2019 ZOOM_connection_put_event(c, event);
2021 else if (r == CS_CONNECT)
2023 int ret = ret = cs_rcvconnect(c->cs);
2024 yaz_log(c->log_details, "%p ZOOM_connection_do_io "
2025 "cs_rcvconnect returned %d", c, ret);
2028 int mask = ZOOM_SELECT_EXCEPT;
2029 if (c->cs->io_pending & CS_WANT_WRITE)
2030 mask += ZOOM_SELECT_WRITE;
2031 if (c->cs->io_pending & CS_WANT_READ)
2032 mask += ZOOM_SELECT_READ;
2033 ZOOM_connection_set_mask(c, mask);
2034 event = ZOOM_Event_create(ZOOM_EVENT_NONE);
2035 ZOOM_connection_put_event(c, event);
2039 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2040 ZOOM_connection_put_event(c, event);
2042 if (c->proto == PROTO_Z3950)
2043 ZOOM_connection_Z3950_send_init(c);
2046 /* no init request for SRW .. */
2047 assert(c->tasks->which == ZOOM_TASK_CONNECT);
2048 ZOOM_connection_remove_task(c);
2049 ZOOM_connection_set_mask(c, 0);
2050 ZOOM_connection_exec_task(c);
2052 c->state = STATE_ESTABLISHED;
2056 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2057 ZOOM_connection_close(c);
2062 if (mask & ZOOM_SELECT_EXCEPT)
2064 if (!ZOOM_test_reconnect(c))
2066 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2067 ZOOM_connection_close(c);
2071 if (mask & ZOOM_SELECT_READ)
2073 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2079 ZOOM_connection_last_event(ZOOM_connection cs)
2082 return ZOOM_EVENT_NONE;
2083 return cs->last_event;
2087 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2091 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2092 /* timeout and this connection was waiting */
2093 ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2094 ZOOM_connection_close(c);
2095 ZOOM_connection_put_event(c, event);
2101 ZOOM_connection_process(ZOOM_connection c)
2107 event = ZOOM_connection_get_event(c);
2110 ZOOM_Event_destroy(event);
2113 ZOOM_connection_exec_task(c);
2114 event = ZOOM_connection_get_event(c);
2117 ZOOM_Event_destroy(event);
2124 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2128 yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2130 for (i = 0; i<no; i++)
2132 ZOOM_connection c = cs[i];
2134 if (c && ZOOM_connection_process(c))
2140 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2142 if (c->mask && mask)
2143 ZOOM_connection_do_io(c, mask);
2147 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2150 return cs_fileno(c->cs);
2154 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2162 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2169 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2171 return ZOOM_options_get_int(c->options, "timeout", 30);
2174 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2179 ZOOM_connection_set_mask(c, 0);
2180 c->state = STATE_IDLE;
2186 * c-file-style: "Stroustrup"
2187 * indent-tabs-mode: nil
2189 * vim: shiftwidth=4 tabstop=8 expandtab