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);
53 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri);
55 void ZOOM_set_dset_error(ZOOM_connection c, int error,
57 const char *addinfo, const char *addinfo2)
64 if (!c->diagset || strcmp(dset, c->diagset))
67 c->diagset = xstrdup(dset);
68 /* remove integer part from SRW diagset .. */
69 if ((cp = strrchr(c->diagset, '/')))
72 if (addinfo && addinfo2)
74 c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 3);
75 strcpy(c->addinfo, addinfo);
76 strcat(c->addinfo, ": ");
77 strcat(c->addinfo, addinfo2);
80 c->addinfo = xstrdup(addinfo);
81 if (error != ZOOM_ERROR_NONE)
83 yaz_log(c->log_api, "%p set_dset_error %s %s:%d %s %s",
84 c, c->host_port ? c->host_port : "<>", dset, error,
85 addinfo ? addinfo : "",
86 addinfo2 ? addinfo2 : "");
87 ZOOM_connection_remove_tasks(c);
91 int ZOOM_uri_to_code(const char *uri)
95 if ((cp = strrchr(uri, '/')))
100 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
102 ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
105 static void clear_error(ZOOM_connection c)
108 * If an error is tied to an operation then it's ok to clear: for
109 * example, a diagnostic returned from a search is cleared by a
110 * subsequent search. However, problems such as Connection Lost
111 * or Init Refused are not cleared, because they are not
112 * recoverable: doing another search doesn't help.
115 ZOOM_connection_remove_events(c);
118 case ZOOM_ERROR_CONNECT:
119 case ZOOM_ERROR_MEMORY:
120 case ZOOM_ERROR_DECODE:
121 case ZOOM_ERROR_CONNECTION_LOST:
122 case ZOOM_ERROR_INIT:
123 case ZOOM_ERROR_INTERNAL:
124 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
127 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
131 void ZOOM_connection_show_task(ZOOM_task task)
135 case ZOOM_TASK_SEARCH:
136 yaz_log(YLOG_LOG, "search p=%p", task);
138 case ZOOM_TASK_CONNECT:
139 yaz_log(YLOG_LOG, "connect p=%p", task);
142 yaz_log(YLOG_LOG, "scan p=%p", task);
147 void ZOOM_connection_show_tasks(ZOOM_connection c)
150 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
151 for (task = c->tasks; task; task = task->next)
152 ZOOM_connection_show_task(task);
155 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
157 ZOOM_task *taskp = &c->tasks;
159 taskp = &(*taskp)->next;
160 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
161 (*taskp)->running = 0;
162 (*taskp)->which = which;
168 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
170 return c->tasks ? 0 : 1;
173 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
175 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
177 task->next = c->tasks;
185 void ZOOM_connection_remove_task(ZOOM_connection c)
187 ZOOM_task task = c->tasks;
191 c->tasks = task->next;
194 case ZOOM_TASK_SEARCH:
195 resultset_destroy(task->u.search.resultset);
196 xfree(task->u.search.syntax);
197 xfree(task->u.search.elementSetName);
198 xfree(task->u.search.schema);
200 case ZOOM_TASK_CONNECT:
203 ZOOM_scanset_destroy(task->u.scan.scan);
205 case ZOOM_TASK_PACKAGE:
206 ZOOM_package_destroy(task->u.package);
209 resultset_destroy(task->u.sort.resultset);
210 ZOOM_query_destroy(task->u.sort.q);
219 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
220 ZOOM_connection_put_event(c, event);
225 void ZOOM_connection_remove_tasks(ZOOM_connection c)
228 ZOOM_connection_remove_task(c);
231 static void odr_wrbuf_write(ODR o, void *handle, int type,
232 const char *buf, int len)
234 WRBUF w = (WRBUF) handle;
235 wrbuf_write(w, buf, len);
238 ZOOM_API(ZOOM_connection)
239 ZOOM_connection_create(ZOOM_options options)
241 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
245 c->log_api = log_api0;
246 c->log_details = log_details0;
248 yaz_log(c->log_api, "%p ZOOM_connection_create", c);
250 c->proto = PROTO_Z3950;
252 ZOOM_connection_set_mask(c, 0);
254 c->state = STATE_IDLE;
257 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
264 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;
302 c->saveAPDU_wrbuf = 0;
304 ZOOM_memcached_init(c);
308 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
312 odr_destroy(c->odr_save);
317 c->odr_save = odr_createmem(ODR_PRINT);
318 odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
322 /* set database names. Take local databases (if set); otherwise
323 take databases given in ZURL (if set); otherwise use Default */
324 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
327 char **databaseNames;
328 const char *cp = ZOOM_options_get(options, "databaseName");
330 if ((!cp || !*cp) && con->host_port)
331 cs_get_host_args(con->host_port, &cp);
334 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
335 return databaseNames;
338 ZOOM_API(ZOOM_connection)
339 ZOOM_connection_new(const char *host, int portnum)
341 ZOOM_connection c = ZOOM_connection_create(0);
343 ZOOM_connection_connect(c, host, portnum);
347 static zoom_sru_mode get_sru_mode_from_string(const char *s)
350 return zoom_sru_soap;
351 if (!yaz_matchstr(s, "soap"))
352 return zoom_sru_soap;
353 else if (!yaz_matchstr(s, "get"))
355 else if (!yaz_matchstr(s, "post"))
356 return zoom_sru_post;
357 else if (!yaz_matchstr(s, "solr"))
358 return zoom_sru_solr;
359 return zoom_sru_error;
363 ZOOM_connection_connect(ZOOM_connection c,
364 const char *host, int portnum)
370 yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
371 c, host ? host : "null", portnum);
373 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
374 ZOOM_connection_remove_tasks(c);
378 yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
382 yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
385 val = ZOOM_options_get(c->options, "proxy");
388 yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
389 c->proxy = xstrdup(val);
394 val = ZOOM_options_get(c->options, "tproxy");
397 yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
398 c->tproxy = xstrdup(val);
403 val = ZOOM_options_get(c->options, "charset");
406 yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
407 c->charset = xstrdup(val);
411 val = ZOOM_options_get(c->options, "lang");
414 yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
415 c->lang = xstrdup(val);
423 const char *http_lead;
425 val = ZOOM_options_get(c->options, "sru");
426 if (val && *val && !strstr(host, "://"))
427 http_lead = "http://";
430 c->sru_mode = get_sru_mode_from_string(val);
435 sprintf(hostn, "%.80s:%d", host, portnum);
438 c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
439 strcpy(c->host_port, http_lead);
440 strcat(c->host_port, host);
445 * If the "<scheme>:" part of the host string is preceded by one
446 * or more comma-separated <name>=<value> pairs, these are taken
447 * to be options to be set on the connection object. Among other
448 * applications, this facility can be used to embed authentication
450 * user=admin,password=secret,tcp:localhost:9999
452 char *remainder = c->host_port;
453 char *pcolon = strchr(remainder, ':');
456 while ((pcomma = strchr(remainder, ',')) != 0 &&
457 (pcolon == 0 || pcomma < pcolon))
460 if ((pequals = strchr(remainder, '=')) != 0)
463 ZOOM_connection_option_set(c, remainder, pequals+1);
465 remainder = pcomma+1;
468 if (remainder != c->host_port)
470 remainder = xstrdup(remainder);
472 c->host_port = remainder;
476 xfree(c->sru_version);
477 val = ZOOM_options_get(c->options, "sru_version");
478 c->sru_version = xstrdup(val ? val : "1.2");
480 ZOOM_options_set(c->options, "host", c->host_port);
482 xfree(c->cookie_out);
484 val = ZOOM_options_get(c->options, "cookie");
487 yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
488 c->cookie_out = xstrdup(val);
493 val = ZOOM_options_get(c->options, "clientIP");
496 yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
498 c->client_IP = xstrdup(val);
503 val = ZOOM_options_get(c->options, "group");
505 c->group = xstrdup(val);
509 val = ZOOM_options_get(c->options, "user");
511 c->user = xstrdup(val);
515 val = ZOOM_options_get(c->options, "password");
517 val = ZOOM_options_get(c->options, "pass");
519 c->password = xstrdup(val);
521 val = ZOOM_options_get(c->options, "authenticationMode");
522 if (val && !strcmp(val, "url"))
523 c->url_authentication = 1;
525 c->url_authentication = 0;
527 c->maximum_record_size =
528 ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
529 c->preferred_message_size =
530 ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
532 c->async = ZOOM_options_get_bool(c->options, "async", 0);
534 yaz_cookies_destroy(c->cookies);
535 c->cookies = yaz_cookies_create();
537 if (ZOOM_memcached_configure(c))
539 ZOOM_connection_remove_tasks(c);
542 if (c->sru_mode == zoom_sru_error)
544 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
545 ZOOM_connection_remove_tasks(c);
550 odr_destroy(c->odr_print);
552 val = ZOOM_options_get(c->options, "apdufile");
555 c->odr_print = odr_createmem(ODR_PRINT);
556 if (strcmp(val, "-"))
558 FILE *f = fopen(val, "a");
561 WRBUF w = wrbuf_alloc();
562 wrbuf_printf(w, "fopen: %s", val);
563 ZOOM_set_error(c, ZOOM_ERROR_INTERNAL, wrbuf_cstr(w));
567 odr_setprint(c->odr_print, f);
570 else if (ZOOM_options_get_bool(c->options, "apdulog", 0))
572 c->odr_print = odr_createmem(ODR_PRINT);
573 odr_setprint_noclose(c->odr_print, yaz_log_file());
576 yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
577 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
581 while (ZOOM_event(1, &c))
586 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
590 /* remove ourselves from the resultsets in connection */
591 ZOOM_resultset *rp = &r->connection->resultsets;
594 assert(*rp); /* we must be in this list!! */
596 { /* OK, we're here - take us out of it */
607 ZOOM_connection_destroy(ZOOM_connection c)
612 yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
614 ZOOM_memcached_destroy(c);
618 for (r = c->resultsets; r; r = r->next)
624 odr_destroy(c->odr_in);
625 odr_destroy(c->odr_out);
627 odr_destroy(c->odr_save);
629 odr_destroy(c->odr_print);
630 ZOOM_options_destroy(c->options);
631 ZOOM_connection_remove_tasks(c);
632 ZOOM_connection_remove_events(c);
638 xfree(c->cookie_out);
644 xfree(c->sru_version);
646 yaz_cookies_destroy(c->cookies);
647 wrbuf_destroy(c->saveAPDU_wrbuf);
651 void ZOOM_resultset_addref(ZOOM_resultset r)
655 yaz_mutex_enter(r->mutex);
657 yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
659 yaz_mutex_leave(r->mutex);
663 static int g_resultsets = 0;
664 static YAZ_MUTEX g_resultset_mutex = 0;
666 /* TODO We need to initialize this before running threaded:
667 * call resultset_use(0) */
669 static int resultset_use(int delta) {
671 if (g_resultset_mutex == 0)
672 yaz_mutex_create(&g_resultset_mutex);
673 yaz_mutex_enter(g_resultset_mutex);
674 g_resultsets += delta;
675 resultset_count = g_resultsets;
676 yaz_mutex_leave(g_resultset_mutex);
677 return resultset_count;
680 int resultsets_count(void) {
681 return resultset_use(0);
684 ZOOM_resultset ZOOM_resultset_create(void)
687 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
691 yaz_log(log_details0, "%p ZOOM_resultset_create", r);
694 r->odr = odr_createmem(ODR_DECODE);
698 for (i = 0; i<RECORD_HASH_SIZE; i++)
699 r->record_hash[i] = 0;
703 r->databaseNames = 0;
704 r->num_databaseNames = 0;
707 r->num_res_facets = 0;
710 yaz_mutex_create(&r->mutex);
713 WRBUF w = wrbuf_alloc();
714 YAZ_SHPTR_INIT(r->record_wrbuf, w);
723 ZOOM_API(ZOOM_resultset)
724 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
727 ZOOM_query s = ZOOM_query_create();
729 ZOOM_query_prefix(s, q);
731 r = ZOOM_connection_search(c, s);
732 ZOOM_query_destroy(s);
736 ZOOM_API(ZOOM_resultset)
737 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
739 ZOOM_resultset r = ZOOM_resultset_create();
742 const char *syntax, *elementSetName, *schema;
743 yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
744 r->r_sort_spec = ZOOM_query_get_sortspec(q);
746 ZOOM_query_addref(q);
748 r->options = ZOOM_options_create_with_parent(c->options);
750 r->req_facets = odr_strdup_null(r->odr,
751 ZOOM_options_get(r->options, "facets"));
752 start = ZOOM_options_get_int(r->options, "start", 0);
753 count = ZOOM_options_get_int(r->options, "count", 0);
755 /* If "presentChunk" is defined use that; otherwise "step" */
756 const char *cp = ZOOM_options_get(r->options, "presentChunk");
757 r->step = ZOOM_options_get_int(r->options,
758 (cp != 0 ? "presentChunk": "step"), 0);
760 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
761 r->setname = odr_strdup_null(r->odr,
762 ZOOM_options_get(r->options, "setname"));
763 r->databaseNames = ZOOM_connection_get_databases(c, c->options,
764 &r->num_databaseNames,
767 r->next = c->resultsets;
770 ZOOM_memcached_resultset(r, q);
772 if (c->host_port && c->proto == PROTO_HTTP)
776 yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
777 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
781 yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
786 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
787 task->u.search.resultset = r;
788 task->u.search.start = start;
789 task->u.search.count = count;
791 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
792 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
793 elementSetName = ZOOM_options_get(r->options, "elementSetName");
794 task->u.search.elementSetName = elementSetName ?
795 xstrdup(elementSetName) : 0;
796 schema = ZOOM_options_get(r->options, "schema");
797 task->u.search.schema = schema ? xstrdup(schema) : 0;
799 ZOOM_resultset_addref(r);
803 while (ZOOM_event(1, &c))
810 ZOOM_resultset_sort(ZOOM_resultset r,
811 const char *sort_type, const char *sort_spec)
813 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
817 ZOOM_resultset_sort1(ZOOM_resultset r,
818 const char *sort_type, const char *sort_spec)
820 ZOOM_connection c = r->connection;
824 newq = ZOOM_query_create();
825 if (ZOOM_query_sortby(newq, sort_spec) < 0)
828 yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
829 r, r, sort_type, sort_spec);
833 if (c->host_port && c->proto == PROTO_HTTP)
837 yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
838 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
842 yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
848 ZOOM_resultset_cache_reset(r);
849 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
850 task->u.sort.resultset = r;
851 task->u.sort.q = newq;
853 ZOOM_resultset_addref(r);
857 while (ZOOM_event(1, &c))
865 ZOOM_resultset_destroy(ZOOM_resultset r)
867 resultset_destroy(r);
870 static void resultset_destroy(ZOOM_resultset r)
874 yaz_mutex_enter(r->mutex);
876 yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
878 if (r->refcount == 0)
880 yaz_mutex_leave(r->mutex);
882 yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
883 ZOOM_resultset_cache_reset(r);
884 ZOOM_resultset_release(r);
885 ZOOM_query_destroy(r->query);
886 ZOOM_options_destroy(r->options);
888 yaz_mutex_destroy(&r->mutex);
890 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
892 wrbuf_destroy(r->mc_key);
897 yaz_mutex_leave(r->mutex);
901 ZOOM_resultset_size(ZOOM_resultset r)
906 int ZOOM_test_reconnect(ZOOM_connection c)
910 if (!c->reconnect_ok)
912 ZOOM_connection_close(c);
914 c->tasks->running = 0;
915 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
917 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
918 ZOOM_connection_put_event(c, event);
923 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
924 int force_sync, int start, int count)
929 const char *syntax, *elementSetName;
933 yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
934 " count=%d", r, force_sync, start, count);
939 if (c->host_port && c->proto == PROTO_HTTP)
943 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
944 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
948 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
953 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
954 task->u.search.resultset = r;
955 task->u.search.start = start;
956 task->u.search.count = count;
958 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
959 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
960 elementSetName = ZOOM_options_get(r->options, "elementSetName");
961 task->u.search.elementSetName = elementSetName
962 ? xstrdup(elementSetName) : 0;
964 cp = ZOOM_options_get(r->options, "schema");
965 task->u.search.schema = cp ? xstrdup(cp) : 0;
967 ZOOM_resultset_addref(r);
969 if (!r->connection->async || force_sync)
970 while (r->connection && ZOOM_event(1, &r->connection))
975 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
976 size_t start, size_t count)
978 int force_present = 0;
982 yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
983 r, r, (long) start, (long) count);
986 ZOOM_resultset_retrieve(r, force_present, start, count);
990 for (i = 0; i< count; i++)
991 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
996 ZOOM_resultset_facets_size(ZOOM_resultset r)
998 return r->num_res_facets;
1001 ZOOM_API(ZOOM_facet_field)
1002 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name)
1004 int num = r->num_res_facets;
1005 ZOOM_facet_field *facets = r->res_facets;
1007 for (i = 0; i < num; i++)
1008 if (!strcmp(facets[i]->facet_name, name))
1013 ZOOM_API(ZOOM_facet_field)
1014 ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int idx)
1016 int num = r->num_res_facets;
1017 ZOOM_facet_field *facets = r->res_facets;
1018 if (idx >= 0 && idx < num)
1023 ZOOM_API(ZOOM_facet_field *)
1024 ZOOM_resultset_facets(ZOOM_resultset r)
1026 return r->res_facets;
1029 ZOOM_API(const char**)
1030 ZOOM_resultset_facets_names(ZOOM_resultset r)
1032 return (const char **) r->facets_names;
1035 ZOOM_API(const char*)
1036 ZOOM_facet_field_name(ZOOM_facet_field field)
1038 return field->facet_name;
1042 ZOOM_facet_field_term_count(ZOOM_facet_field field)
1044 return field->num_terms;
1047 ZOOM_API(const char*)
1048 ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq)
1050 *freq = field->facet_terms[idx].frequency;
1051 return field->facet_terms[idx].term;
1055 static void get_cert(ZOOM_connection c)
1060 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1062 ZOOM_connection_option_setl(c, "sslPeerCert",
1063 cert_buf, cert_len);
1068 static zoom_ret do_connect_host(ZOOM_connection c,
1069 const char *logical_url);
1071 static zoom_ret do_connect(ZOOM_connection c)
1073 return do_connect_host(c, c->host_port);
1076 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1082 c->cs = cs_create_host2(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add,
1083 c->tproxy ? c->tproxy : c->proxy,
1088 if (c->cs && c->cs->protocol == PROTO_HTTP)
1091 c->proto = PROTO_HTTP;
1093 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1094 ZOOM_connection_close(c);
1095 return zoom_complete;
1100 int ret = cs_connect(c->cs, add);
1103 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1104 ZOOM_connection_put_event(c, event);
1106 if (c->proto == PROTO_Z3950)
1107 ZOOM_connection_Z3950_send_init(c);
1110 /* no init request for SRW .. */
1111 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1112 ZOOM_connection_remove_task(c);
1113 ZOOM_connection_set_mask(c, 0);
1115 if (c->cs && c->location)
1116 send_HTTP_redirect(c, c->location);
1118 ZOOM_connection_exec_task(c);
1120 c->state = STATE_ESTABLISHED;
1121 return zoom_pending;
1125 int mask = ZOOM_SELECT_EXCEPT;
1126 if (c->cs->io_pending & CS_WANT_WRITE)
1127 mask += ZOOM_SELECT_WRITE;
1128 if (c->cs->io_pending & CS_WANT_READ)
1129 mask += ZOOM_SELECT_READ;
1130 ZOOM_connection_set_mask(c, mask);
1131 c->state = STATE_CONNECTING;
1132 return zoom_pending;
1135 c->state = STATE_IDLE;
1136 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1137 return zoom_complete;
1140 /* returns 1 if PDU was sent OK (still pending )
1141 0 if PDU was not sent OK (nothing to wait for)
1144 ZOOM_API(ZOOM_record)
1145 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1147 const char *syntax =
1148 ZOOM_options_get(s->options, "preferredRecordSyntax");
1149 const char *elementSetName =
1150 ZOOM_options_get(s->options, "elementSetName");
1151 const char *schema =
1152 ZOOM_options_get(s->options, "schema");
1154 return ZOOM_record_cache_lookup_i(s, pos, syntax, elementSetName, schema);
1157 ZOOM_API(ZOOM_record)
1158 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1160 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1165 * MIKE: I think force_sync should always be zero, but I don't
1166 * want to make this change until I get the go-ahead from
1167 * Adam, in case something depends on the old synchronous
1171 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1172 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1173 rec = ZOOM_resultset_record_immediate(r, pos);
1178 ZOOM_API(ZOOM_scanset)
1179 ZOOM_connection_scan(ZOOM_connection c, const char *start)
1182 ZOOM_query q = ZOOM_query_create();
1184 ZOOM_query_prefix(q, start);
1186 s = ZOOM_connection_scan1(c, q);
1187 ZOOM_query_destroy(q);
1192 ZOOM_API(ZOOM_scanset)
1193 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1195 ZOOM_scanset scan = 0;
1196 Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1200 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1201 scan->connection = c;
1202 scan->odr = odr_createmem(ODR_DECODE);
1203 scan->options = ZOOM_options_create_with_parent(c->options);
1205 scan->scan_response = 0;
1206 scan->srw_scan_response = 0;
1209 ZOOM_query_addref(q);
1210 scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1211 &scan->num_databaseNames,
1216 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1217 task->u.scan.scan = scan;
1222 while (ZOOM_event(1, &c))
1230 ZOOM_scanset_destroy(ZOOM_scanset scan)
1235 if (scan->refcount == 0)
1237 ZOOM_query_destroy(scan->query);
1239 odr_destroy(scan->odr);
1241 ZOOM_options_destroy(scan->options);
1246 static zoom_ret send_package(ZOOM_connection c)
1250 yaz_log(c->log_details, "%p send_package", c);
1252 return zoom_complete;
1253 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1255 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1256 ZOOM_connection_put_event(c, event);
1258 c->buf_out = c->tasks->u.package->buf_out;
1259 c->len_out = c->tasks->u.package->len_out;
1261 return ZOOM_send_buf(c);
1265 ZOOM_scanset_size(ZOOM_scanset scan)
1270 if (scan->scan_response && scan->scan_response->entries)
1271 return scan->scan_response->entries->num_entries;
1272 else if (scan->srw_scan_response)
1273 return scan->srw_scan_response->num_terms;
1277 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1279 const char **value_term, size_t *value_len,
1280 const char **disp_term, size_t *disp_len)
1282 size_t noent = ZOOM_scanset_size(scan);
1293 if (scan->scan_response)
1295 Z_ScanResponse *res = scan->scan_response;
1296 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1298 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1300 *value_term = (const char *) t->term->u.general->buf;
1301 *value_len = t->term->u.general->len;
1304 *disp_term = t->displayTerm;
1305 *disp_len = strlen(*disp_term);
1307 else if (t->term->which == Z_Term_general)
1309 *disp_term = (const char *) t->term->u.general->buf;
1310 *disp_len = t->term->u.general->len;
1312 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1315 if (scan->srw_scan_response)
1317 Z_SRW_scanResponse *res = scan->srw_scan_response;
1318 Z_SRW_scanTerm *t = res->terms + pos;
1321 *value_term = t->value;
1322 *value_len = strlen(*value_term);
1325 *disp_term = t->displayTerm;
1327 *disp_term = t->value;
1328 *disp_len = strlen(*disp_term);
1329 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1334 ZOOM_API(const char *)
1335 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1336 size_t *occ, size_t *len)
1338 const char *value_term = 0;
1339 size_t value_len = 0;
1340 const char *disp_term = 0;
1341 size_t disp_len = 0;
1343 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1344 &disp_term, &disp_len);
1350 ZOOM_API(const char *)
1351 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1352 size_t *occ, size_t *len)
1354 const char *value_term = 0;
1355 size_t value_len = 0;
1356 const char *disp_term = 0;
1357 size_t disp_len = 0;
1359 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1360 &disp_term, &disp_len);
1366 ZOOM_API(const char *)
1367 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1369 return ZOOM_options_get(scan->options, key);
1373 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1376 ZOOM_options_set(scan->options, key, val);
1380 ZOOM_API(ZOOM_package)
1381 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1383 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1386 p->odr_out = odr_createmem(ODR_ENCODE);
1387 p->options = ZOOM_options_create_with_parent2(options, c->options);
1395 ZOOM_package_destroy(ZOOM_package p)
1400 if (p->refcount == 0)
1402 odr_destroy(p->odr_out);
1405 ZOOM_options_destroy(p->options);
1410 ZOOM_API(const char *)
1411 ZOOM_package_option_get(ZOOM_package p, const char *key)
1413 return ZOOM_options_get(p->options, key);
1416 ZOOM_API(const char *)
1417 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1419 return ZOOM_options_getl(p->options, key, lenp);
1423 ZOOM_package_option_set(ZOOM_package p, const char *key,
1426 ZOOM_options_set(p->options, key, val);
1430 ZOOM_package_option_setl(ZOOM_package p, const char *key,
1431 const char *val, int len)
1433 ZOOM_options_setl(p->options, key, val, len);
1437 ZOOM_connection_exec_task(ZOOM_connection c)
1439 ZOOM_task task = c->tasks;
1440 zoom_ret ret = zoom_complete;
1444 yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1445 c, task->which, task->running);
1446 if (c->error != ZOOM_ERROR_NONE)
1448 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1449 "removing tasks because of error = %d", c, c->error);
1450 ZOOM_connection_remove_tasks(c);
1455 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1456 "task already running", c);
1460 ret = zoom_complete;
1461 if (c->cs || task->which == ZOOM_TASK_CONNECT)
1463 switch (task->which)
1465 case ZOOM_TASK_SEARCH:
1466 if (c->proto == PROTO_HTTP)
1467 ret = ZOOM_connection_srw_send_search(c);
1469 ret = ZOOM_connection_Z3950_search(c);
1471 case ZOOM_TASK_CONNECT:
1472 ret = do_connect(c);
1474 case ZOOM_TASK_SCAN:
1475 if (c->proto == PROTO_HTTP)
1476 ret = ZOOM_connection_srw_send_scan(c);
1478 ret = ZOOM_connection_Z3950_send_scan(c);
1480 case ZOOM_TASK_PACKAGE:
1481 ret = send_package(c);
1483 case ZOOM_TASK_SORT:
1484 c->tasks->u.sort.resultset->r_sort_spec =
1485 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1486 ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1492 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1493 "remove tasks because no connection exist", c);
1494 ZOOM_connection_remove_tasks(c);
1496 if (ret == zoom_complete)
1498 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1499 "task removed (complete)", c);
1500 ZOOM_connection_remove_task(c);
1503 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1509 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1511 Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1513 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1514 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1516 yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1517 if (c->user && c->password)
1519 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1520 c->user, c->password);
1524 return ZOOM_send_GDU(c, gdu);
1528 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1532 int r = z_GDU(c->odr_out, &gdu, 0, 0);
1534 return zoom_complete;
1536 z_GDU(c->odr_print, &gdu, 0, 0);
1538 z_GDU(c->odr_save, &gdu, 0, 0);
1539 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1540 odr_reset(c->odr_out);
1542 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1543 ZOOM_connection_put_event(c, event);
1545 return ZOOM_send_buf(c);
1548 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1549 const char *addinfo, const char *addinfo2)
1551 ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1555 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1557 zoom_ret cret = zoom_complete;
1560 const char *connection_head = z_HTTP_header_lookup(hres->headers,
1562 const char *location;
1564 ZOOM_connection_set_mask(c, 0);
1565 yaz_log(c->log_details, "%p handle_http", c);
1567 yaz_cookies_response(c->cookies, hres);
1568 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1569 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1572 if (c->no_redirects > 10)
1574 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1575 c->no_redirects = 0;
1576 ZOOM_connection_close(c);
1580 /* since redirect may change host we just reconnect. A smarter
1581 implementation might check whether it's the same server */
1583 int host_change = 0;
1584 location = yaz_check_location(c->odr_in, c->host_port,
1585 location, &host_change);
1587 c->location = xstrdup(location);
1588 do_connect_host(c, location);
1594 ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1597 if (c->no_redirects) /* end of redirect. change hosts again */
1598 ZOOM_connection_close(c);
1600 c->no_redirects = 0;
1604 if (hres->code != 200)
1605 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1608 yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1610 ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1612 ZOOM_connection_close(c);
1614 if (cret == zoom_complete)
1616 yaz_log(c->log_details, "removing tasks in handle_http");
1617 ZOOM_connection_remove_task(c);
1621 if (!strcmp(hres->version, "1.0"))
1623 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1624 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1629 /* HTTP 1.1: only if no close we stay alive.. */
1630 if (connection_head && !strcmp(connection_head, "close"))
1635 ZOOM_connection_close(c);
1638 c->tasks->running = 0;
1639 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1640 c->reconnect_ok = 0;
1644 c->reconnect_ok = 1; /* if the server closes anyway */
1649 static int do_read(ZOOM_connection c)
1654 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1655 ZOOM_connection_put_event(c, event);
1657 r = cs_get(c->cs, &c->buf_in, &c->len_in);
1658 more = cs_more(c->cs);
1659 yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1664 if (!ZOOM_test_reconnect(c))
1666 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1667 ZOOM_connection_close(c);
1675 odr_reset(c->odr_in);
1676 odr_setbuf(c->odr_in, c->buf_in, r, 0);
1677 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1678 ZOOM_connection_put_event(c, event);
1680 if (!z_GDU(c->odr_in, &gdu, 0, 0))
1682 /* even on failures we try to re-connect (HTTP) */
1683 if (!ZOOM_test_reconnect(c))
1686 int err = odr_geterrorx(c->odr_in, &x);
1688 const char *element = odr_getelement(c->odr_in);
1689 yaz_snprintf(msg, sizeof(msg),
1690 "ODR code %d:%d element=%s offset=%d",
1691 err, x, element ? element : "<unknown>",
1692 odr_offset(c->odr_in));
1693 ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1696 FILE *ber_file = yaz_log_file();
1698 odr_dumpBER(ber_file, c->buf_in, r);
1700 ZOOM_connection_close(c);
1706 z_GDU(c->odr_print, &gdu, 0, 0);
1708 z_GDU(c->odr_save, &gdu, 0, 0);
1709 if (gdu->which == Z_GDU_Z3950)
1710 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1711 else if (gdu->which == Z_GDU_HTTP_Response)
1714 handle_http(c, gdu->u.HTTP_Response);
1716 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1717 ZOOM_connection_close(c);
1725 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1730 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1731 ZOOM_connection_put_event(c, event);
1733 yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1734 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1736 yaz_log(c->log_details, "%p do_write_ex write failed", c);
1737 if (ZOOM_test_reconnect(c))
1739 return zoom_pending;
1741 if (c->state == STATE_CONNECTING)
1742 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1744 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1745 ZOOM_connection_close(c);
1746 return zoom_complete;
1750 int mask = ZOOM_SELECT_EXCEPT;
1751 if (c->cs->io_pending & CS_WANT_WRITE)
1752 mask += ZOOM_SELECT_WRITE;
1753 if (c->cs->io_pending & CS_WANT_READ)
1754 mask += ZOOM_SELECT_READ;
1755 ZOOM_connection_set_mask(c, mask);
1756 yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1761 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1762 yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1765 return zoom_pending;
1768 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1770 return do_write_ex(c, c->buf_out, c->len_out);
1774 ZOOM_API(const char *)
1775 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1777 if (!strcmp(key, "APDU"))
1779 return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1782 return ZOOM_options_get(c->options, key);
1785 ZOOM_API(const char *)
1786 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1788 if (!strcmp(key, "APDU"))
1790 if (c->saveAPDU_wrbuf)
1792 *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1793 return wrbuf_cstr(c->saveAPDU_wrbuf);
1802 return ZOOM_options_getl(c->options, key, lenp);
1806 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1809 if (!strcmp(key, "saveAPDU"))
1811 if (val && strcmp(val, "0"))
1813 if (!c->saveAPDU_wrbuf)
1814 c->saveAPDU_wrbuf = wrbuf_alloc();
1816 wrbuf_rewind(c->saveAPDU_wrbuf);
1820 wrbuf_destroy(c->saveAPDU_wrbuf);
1821 c->saveAPDU_wrbuf = 0;
1823 ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1826 ZOOM_options_set(c->options, key, val);
1830 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1831 const char *val, int len)
1833 ZOOM_options_setl(c->options, key, val, len);
1836 ZOOM_API(const char *)
1837 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1839 return ZOOM_options_get(r->options, key);
1843 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1846 ZOOM_options_set(r->options, key, val);
1851 ZOOM_connection_errcode(ZOOM_connection c)
1853 return ZOOM_connection_error(c, 0, 0);
1856 ZOOM_API(const char *)
1857 ZOOM_connection_errmsg(ZOOM_connection c)
1860 ZOOM_connection_error(c, &msg, 0);
1864 ZOOM_API(const char *)
1865 ZOOM_connection_addinfo(ZOOM_connection c)
1867 const char *addinfo;
1868 ZOOM_connection_error(c, 0, &addinfo);
1872 ZOOM_API(const char *)
1873 ZOOM_connection_diagset(ZOOM_connection c)
1875 const char *diagset;
1876 ZOOM_connection_error_x(c, 0, 0, &diagset);
1880 ZOOM_API(const char *)
1881 ZOOM_diag_str(int error)
1885 case ZOOM_ERROR_NONE:
1887 case ZOOM_ERROR_CONNECT:
1888 return "Connect failed";
1889 case ZOOM_ERROR_MEMORY:
1890 return "Out of memory";
1891 case ZOOM_ERROR_ENCODE:
1892 return "Encoding failed";
1893 case ZOOM_ERROR_DECODE:
1894 return "Decoding failed";
1895 case ZOOM_ERROR_CONNECTION_LOST:
1896 return "Connection lost";
1897 case ZOOM_ERROR_INIT:
1898 return "Init rejected";
1899 case ZOOM_ERROR_INTERNAL:
1900 return "Internal failure";
1901 case ZOOM_ERROR_TIMEOUT:
1903 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1904 return "Unsupported protocol";
1905 case ZOOM_ERROR_UNSUPPORTED_QUERY:
1906 return "Unsupported query type";
1907 case ZOOM_ERROR_INVALID_QUERY:
1908 return "Invalid query";
1909 case ZOOM_ERROR_CQL_PARSE:
1910 return "CQL parsing error";
1911 case ZOOM_ERROR_CQL_TRANSFORM:
1912 return "CQL transformation error";
1913 case ZOOM_ERROR_CCL_CONFIG:
1914 return "CCL configuration error";
1915 case ZOOM_ERROR_CCL_PARSE:
1916 return "CCL parsing error";
1917 case ZOOM_ERROR_ES_INVALID_ACTION:
1918 return "Extended Service. invalid action";
1919 case ZOOM_ERROR_ES_INVALID_VERSION:
1920 return "Extended Service. invalid version";
1921 case ZOOM_ERROR_ES_INVALID_SYNTAX:
1922 return "Extended Service. invalid syntax";
1923 case ZOOM_ERROR_MEMCACHED:
1926 return diagbib1_str(error);
1931 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1932 const char **addinfo, const char **diagset)
1934 int error = c->error;
1937 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1938 *cp = ZOOM_diag_str(error);
1939 else if (!strcmp(c->diagset, "HTTP"))
1940 *cp = z_HTTP_errmsg(c->error);
1941 else if (!strcmp(c->diagset, "Bib-1"))
1942 *cp = ZOOM_diag_str(error);
1943 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1944 *cp = yaz_diag_srw_str(c->error);
1946 *cp = "Unknown error and diagnostic set";
1949 *addinfo = c->addinfo ? c->addinfo : "";
1951 *diagset = c->diagset ? c->diagset : "";
1956 ZOOM_connection_error(ZOOM_connection c, const char **cp,
1957 const char **addinfo)
1959 return ZOOM_connection_error_x(c, cp, addinfo, 0);
1962 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1964 ZOOM_Event event = 0;
1965 int r = cs_look(c->cs);
1966 yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1971 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1972 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1973 ZOOM_connection_close(c);
1974 ZOOM_connection_put_event(c, event);
1976 else if (r == CS_CONNECT)
1978 int ret = ret = cs_rcvconnect(c->cs);
1979 yaz_log(c->log_details, "%p ZOOM_connection_do_io "
1980 "cs_rcvconnect returned %d", c, ret);
1983 int mask = ZOOM_SELECT_EXCEPT;
1984 if (c->cs->io_pending & CS_WANT_WRITE)
1985 mask += ZOOM_SELECT_WRITE;
1986 if (c->cs->io_pending & CS_WANT_READ)
1987 mask += ZOOM_SELECT_READ;
1988 ZOOM_connection_set_mask(c, mask);
1989 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1990 ZOOM_connection_put_event(c, event);
1994 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1995 ZOOM_connection_put_event(c, event);
1997 if (c->proto == PROTO_Z3950)
1998 ZOOM_connection_Z3950_send_init(c);
2001 /* no init request for SRW .. */
2002 if (c->tasks->which == ZOOM_TASK_CONNECT)
2004 ZOOM_connection_remove_task(c);
2005 ZOOM_connection_set_mask(c, 0);
2007 if (c->cs && c->location)
2008 send_HTTP_redirect(c, c->location);
2010 ZOOM_connection_exec_task(c);
2012 c->state = STATE_ESTABLISHED;
2016 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2017 ZOOM_connection_close(c);
2022 if (mask & ZOOM_SELECT_EXCEPT)
2024 if (!ZOOM_test_reconnect(c))
2026 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2027 ZOOM_connection_close(c);
2031 if (mask & ZOOM_SELECT_READ)
2033 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2039 ZOOM_connection_last_event(ZOOM_connection cs)
2042 return ZOOM_EVENT_NONE;
2043 return cs->last_event;
2047 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2051 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2052 /* timeout and this connection was waiting */
2053 ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2054 ZOOM_connection_close(c);
2055 ZOOM_connection_put_event(c, event);
2061 ZOOM_connection_process(ZOOM_connection c)
2067 event = ZOOM_connection_get_event(c);
2070 ZOOM_Event_destroy(event);
2073 ZOOM_connection_exec_task(c);
2074 event = ZOOM_connection_get_event(c);
2077 ZOOM_Event_destroy(event);
2084 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2088 yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2090 for (i = 0; i<no; i++)
2092 ZOOM_connection c = cs[i];
2094 if (c && ZOOM_connection_process(c))
2100 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2102 if (c->mask && mask)
2103 ZOOM_connection_do_io(c, mask);
2107 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2110 return cs_fileno(c->cs);
2114 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2122 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2129 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2131 return ZOOM_options_get_int(c->options, "timeout", 30);
2134 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2139 ZOOM_connection_set_mask(c, 0);
2140 c->state = STATE_IDLE;
2146 * c-file-style: "Stroustrup"
2147 * indent-tabs-mode: nil
2149 * vim: shiftwidth=4 tabstop=8 expandtab