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 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri);
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 : "");
89 int ZOOM_uri_to_code(const char *uri)
93 if ((cp = strrchr(uri, '/')))
98 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
100 ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
103 static void clear_error(ZOOM_connection c)
106 * If an error is tied to an operation then it's ok to clear: for
107 * example, a diagnostic returned from a search is cleared by a
108 * subsequent search. However, problems such as Connection Lost
109 * or Init Refused are not cleared, because they are not
110 * recoverable: doing another search doesn't help.
113 ZOOM_connection_remove_events(c);
116 case ZOOM_ERROR_CONNECT:
117 case ZOOM_ERROR_MEMORY:
118 case ZOOM_ERROR_DECODE:
119 case ZOOM_ERROR_CONNECTION_LOST:
120 case ZOOM_ERROR_INIT:
121 case ZOOM_ERROR_INTERNAL:
122 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
125 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
129 void ZOOM_connection_show_task(ZOOM_task task)
133 case ZOOM_TASK_SEARCH:
134 yaz_log(YLOG_LOG, "search p=%p", task);
136 case ZOOM_TASK_CONNECT:
137 yaz_log(YLOG_LOG, "connect p=%p", task);
140 yaz_log(YLOG_LOG, "scan p=%p", task);
145 void ZOOM_connection_show_tasks(ZOOM_connection c)
148 yaz_log(YLOG_LOG, "connection p=%p tasks", c);
149 for (task = c->tasks; task; task = task->next)
150 ZOOM_connection_show_task(task);
153 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
155 ZOOM_task *taskp = &c->tasks;
157 taskp = &(*taskp)->next;
158 *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
159 (*taskp)->running = 0;
160 (*taskp)->which = which;
166 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
168 return c->tasks ? 0 : 1;
171 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
173 ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
175 task->next = c->tasks;
183 void ZOOM_connection_remove_task(ZOOM_connection c)
185 ZOOM_task task = c->tasks;
189 c->tasks = task->next;
192 case ZOOM_TASK_SEARCH:
193 resultset_destroy(task->u.search.resultset);
194 xfree(task->u.search.syntax);
195 xfree(task->u.search.elementSetName);
196 xfree(task->u.search.schema);
198 case ZOOM_TASK_CONNECT:
201 ZOOM_scanset_destroy(task->u.scan.scan);
203 case ZOOM_TASK_PACKAGE:
204 ZOOM_package_destroy(task->u.package);
207 resultset_destroy(task->u.sort.resultset);
208 ZOOM_query_destroy(task->u.sort.q);
217 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
218 ZOOM_connection_put_event(c, event);
223 void ZOOM_connection_remove_tasks(ZOOM_connection c)
226 ZOOM_connection_remove_task(c);
229 static void odr_wrbuf_write(ODR o, void *handle, int type,
230 const char *buf, int len)
232 WRBUF w = (WRBUF) handle;
233 wrbuf_write(w, buf, len);
236 ZOOM_API(ZOOM_connection)
237 ZOOM_connection_create(ZOOM_options options)
239 ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
243 c->log_api = log_api0;
244 c->log_details = log_details0;
246 yaz_log(c->log_api, "%p ZOOM_connection_create", c);
248 c->proto = PROTO_Z3950;
250 ZOOM_connection_set_mask(c, 0);
252 c->state = STATE_IDLE;
255 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
262 c->options = ZOOM_options_create_with_parent(options);
269 c->charset = c->lang = 0;
279 c->url_authentication = 0;
281 c->maximum_record_size = 0;
282 c->preferred_message_size = 0;
284 c->odr_in = odr_createmem(ODR_DECODE);
285 c->odr_out = odr_createmem(ODR_ENCODE);
290 c->support_named_resultsets = 0;
291 c->last_event = ZOOM_EVENT_NONE;
293 c->m_queue_front = 0;
300 c->saveAPDU_wrbuf = 0;
302 ZOOM_memcached_init(c);
306 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
310 odr_destroy(c->odr_save);
315 c->odr_save = odr_createmem(ODR_PRINT);
316 odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
320 /* set database names. Take local databases (if set); otherwise
321 take databases given in ZURL (if set); otherwise use Default */
322 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
325 char **databaseNames;
326 const char *cp = ZOOM_options_get(options, "databaseName");
328 if ((!cp || !*cp) && con->host_port)
329 cs_get_host_args(con->host_port, &cp);
332 nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
333 return databaseNames;
336 ZOOM_API(ZOOM_connection)
337 ZOOM_connection_new(const char *host, int portnum)
339 ZOOM_connection c = ZOOM_connection_create(0);
341 ZOOM_connection_connect(c, host, portnum);
345 static zoom_sru_mode get_sru_mode_from_string(const char *s)
348 return zoom_sru_soap;
349 if (!yaz_matchstr(s, "soap"))
350 return zoom_sru_soap;
351 else if (!yaz_matchstr(s, "get"))
353 else if (!yaz_matchstr(s, "post"))
354 return zoom_sru_post;
355 else if (!yaz_matchstr(s, "solr"))
356 return zoom_sru_solr;
357 return zoom_sru_error;
361 ZOOM_connection_connect(ZOOM_connection c,
362 const char *host, int portnum)
368 yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
369 c, host ? host : "null", portnum);
371 ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
372 ZOOM_connection_remove_tasks(c);
376 yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
380 yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
383 val = ZOOM_options_get(c->options, "proxy");
386 yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
387 c->proxy = xstrdup(val);
392 val = ZOOM_options_get(c->options, "tproxy");
395 yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
396 c->tproxy = xstrdup(val);
401 val = ZOOM_options_get(c->options, "charset");
404 yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
405 c->charset = xstrdup(val);
409 val = ZOOM_options_get(c->options, "lang");
412 yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
413 c->lang = xstrdup(val);
421 const char *http_lead;
423 val = ZOOM_options_get(c->options, "sru");
424 if (val && *val && !strstr(host, "://"))
425 http_lead = "http://";
428 c->sru_mode = get_sru_mode_from_string(val);
433 sprintf(hostn, "%.80s:%d", host, portnum);
436 c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
437 strcpy(c->host_port, http_lead);
438 strcat(c->host_port, host);
443 * If the "<scheme>:" part of the host string is preceded by one
444 * or more comma-separated <name>=<value> pairs, these are taken
445 * to be options to be set on the connection object. Among other
446 * applications, this facility can be used to embed authentication
448 * user=admin,password=secret,tcp:localhost:9999
450 char *remainder = c->host_port;
451 char *pcolon = strchr(remainder, ':');
454 while ((pcomma = strchr(remainder, ',')) != 0 &&
455 (pcolon == 0 || pcomma < pcolon))
458 if ((pequals = strchr(remainder, '=')) != 0)
461 ZOOM_connection_option_set(c, remainder, pequals+1);
463 remainder = pcomma+1;
466 if (remainder != c->host_port)
468 remainder = xstrdup(remainder);
470 c->host_port = remainder;
474 xfree(c->sru_version);
475 val = ZOOM_options_get(c->options, "sru_version");
476 c->sru_version = xstrdup(val ? val : "1.2");
478 ZOOM_options_set(c->options, "host", c->host_port);
480 xfree(c->cookie_out);
482 val = ZOOM_options_get(c->options, "cookie");
485 yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
486 c->cookie_out = xstrdup(val);
491 val = ZOOM_options_get(c->options, "clientIP");
494 yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
496 c->client_IP = xstrdup(val);
501 val = ZOOM_options_get(c->options, "group");
503 c->group = xstrdup(val);
507 val = ZOOM_options_get(c->options, "user");
509 c->user = xstrdup(val);
513 val = ZOOM_options_get(c->options, "password");
515 val = ZOOM_options_get(c->options, "pass");
517 c->password = xstrdup(val);
519 val = ZOOM_options_get(c->options, "authenticationMode");
520 if (val && !strcmp(val, "url"))
521 c->url_authentication = 1;
523 c->url_authentication = 0;
525 c->maximum_record_size =
526 ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
527 c->preferred_message_size =
528 ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
530 c->async = ZOOM_options_get_bool(c->options, "async", 0);
532 yaz_cookies_destroy(c->cookies);
533 c->cookies = yaz_cookies_create();
535 if (ZOOM_memcached_configure(c))
537 ZOOM_connection_remove_tasks(c);
540 if (c->sru_mode == zoom_sru_error)
542 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
543 ZOOM_connection_remove_tasks(c);
548 odr_destroy(c->odr_print);
550 val = ZOOM_options_get(c->options, "apdufile");
553 c->odr_print = odr_createmem(ODR_PRINT);
554 if (strcmp(val, "-"))
556 FILE *f = fopen(val, "a");
559 WRBUF w = wrbuf_alloc();
560 wrbuf_printf(w, "fopen: %s", val);
561 ZOOM_set_error(c, ZOOM_ERROR_INTERNAL, wrbuf_cstr(w));
565 odr_setprint(c->odr_print, f);
568 else if (ZOOM_options_get_bool(c->options, "apdulog", 0))
570 c->odr_print = odr_createmem(ODR_PRINT);
571 odr_setprint_noclose(c->odr_print, yaz_log_file());
574 yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
575 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
579 while (ZOOM_event(1, &c))
584 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
588 /* remove ourselves from the resultsets in connection */
589 ZOOM_resultset *rp = &r->connection->resultsets;
592 assert(*rp); /* we must be in this list!! */
594 { /* OK, we're here - take us out of it */
605 ZOOM_connection_destroy(ZOOM_connection c)
610 yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
612 ZOOM_memcached_destroy(c);
616 for (r = c->resultsets; r; r = r->next)
622 odr_destroy(c->odr_in);
623 odr_destroy(c->odr_out);
625 odr_destroy(c->odr_save);
627 odr_destroy(c->odr_print);
628 ZOOM_options_destroy(c->options);
629 ZOOM_connection_remove_tasks(c);
630 ZOOM_connection_remove_events(c);
636 xfree(c->cookie_out);
642 xfree(c->sru_version);
644 yaz_cookies_destroy(c->cookies);
645 wrbuf_destroy(c->saveAPDU_wrbuf);
649 void ZOOM_resultset_addref(ZOOM_resultset r)
653 yaz_mutex_enter(r->mutex);
655 yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
657 yaz_mutex_leave(r->mutex);
661 static int g_resultsets = 0;
662 static YAZ_MUTEX g_resultset_mutex = 0;
664 /* TODO We need to initialize this before running threaded:
665 * call resultset_use(0) */
667 static int resultset_use(int delta) {
669 if (g_resultset_mutex == 0)
670 yaz_mutex_create(&g_resultset_mutex);
671 yaz_mutex_enter(g_resultset_mutex);
672 g_resultsets += delta;
673 resultset_count = g_resultsets;
674 yaz_mutex_leave(g_resultset_mutex);
675 return resultset_count;
678 int resultsets_count(void) {
679 return resultset_use(0);
682 ZOOM_resultset ZOOM_resultset_create(void)
685 ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
689 yaz_log(log_details0, "%p ZOOM_resultset_create", r);
692 r->odr = odr_createmem(ODR_DECODE);
696 for (i = 0; i<RECORD_HASH_SIZE; i++)
697 r->record_hash[i] = 0;
701 r->databaseNames = 0;
702 r->num_databaseNames = 0;
705 r->num_res_facets = 0;
708 yaz_mutex_create(&r->mutex);
711 WRBUF w = wrbuf_alloc();
712 YAZ_SHPTR_INIT(r->record_wrbuf, w);
721 ZOOM_API(ZOOM_resultset)
722 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
725 ZOOM_query s = ZOOM_query_create();
727 ZOOM_query_prefix(s, q);
729 r = ZOOM_connection_search(c, s);
730 ZOOM_query_destroy(s);
734 ZOOM_API(ZOOM_resultset)
735 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
737 ZOOM_resultset r = ZOOM_resultset_create();
740 const char *syntax, *elementSetName, *schema;
741 yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
742 r->r_sort_spec = ZOOM_query_get_sortspec(q);
744 ZOOM_query_addref(q);
746 r->options = ZOOM_options_create_with_parent(c->options);
748 r->req_facets = odr_strdup_null(r->odr,
749 ZOOM_options_get(r->options, "facets"));
750 start = ZOOM_options_get_int(r->options, "start", 0);
751 count = ZOOM_options_get_int(r->options, "count", 0);
753 /* If "presentChunk" is defined use that; otherwise "step" */
754 const char *cp = ZOOM_options_get(r->options, "presentChunk");
755 r->step = ZOOM_options_get_int(r->options,
756 (cp != 0 ? "presentChunk": "step"), 0);
758 r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
759 r->setname = odr_strdup_null(r->odr,
760 ZOOM_options_get(r->options, "setname"));
761 r->databaseNames = ZOOM_connection_get_databases(c, c->options,
762 &r->num_databaseNames,
765 r->next = c->resultsets;
768 ZOOM_memcached_resultset(r, q);
770 if (c->host_port && c->proto == PROTO_HTTP)
774 yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
775 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
779 yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
784 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
785 task->u.search.resultset = r;
786 task->u.search.start = start;
787 task->u.search.count = count;
789 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
790 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
791 elementSetName = ZOOM_options_get(r->options, "elementSetName");
792 task->u.search.elementSetName = elementSetName ?
793 xstrdup(elementSetName) : 0;
794 schema = ZOOM_options_get(r->options, "schema");
795 task->u.search.schema = schema ? xstrdup(schema) : 0;
797 ZOOM_resultset_addref(r);
801 while (ZOOM_event(1, &c))
808 ZOOM_resultset_sort(ZOOM_resultset r,
809 const char *sort_type, const char *sort_spec)
811 (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
815 ZOOM_resultset_sort1(ZOOM_resultset r,
816 const char *sort_type, const char *sort_spec)
818 ZOOM_connection c = r->connection;
822 newq = ZOOM_query_create();
823 if (ZOOM_query_sortby(newq, sort_spec) < 0)
826 yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
827 r, r, sort_type, sort_spec);
831 if (c->host_port && c->proto == PROTO_HTTP)
835 yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
836 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
840 yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
846 ZOOM_resultset_cache_reset(r);
847 task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
848 task->u.sort.resultset = r;
849 task->u.sort.q = newq;
851 ZOOM_resultset_addref(r);
855 while (ZOOM_event(1, &c))
863 ZOOM_resultset_destroy(ZOOM_resultset r)
865 resultset_destroy(r);
868 static void resultset_destroy(ZOOM_resultset r)
872 yaz_mutex_enter(r->mutex);
874 yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
876 if (r->refcount == 0)
878 yaz_mutex_leave(r->mutex);
880 yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
881 ZOOM_resultset_cache_reset(r);
882 ZOOM_resultset_release(r);
883 ZOOM_query_destroy(r->query);
884 ZOOM_options_destroy(r->options);
886 yaz_mutex_destroy(&r->mutex);
888 YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
890 wrbuf_destroy(r->mc_key);
895 yaz_mutex_leave(r->mutex);
899 ZOOM_resultset_size(ZOOM_resultset r)
904 int ZOOM_test_reconnect(ZOOM_connection c)
908 if (!c->reconnect_ok)
910 ZOOM_connection_close(c);
912 c->tasks->running = 0;
913 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
915 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
916 ZOOM_connection_put_event(c, event);
921 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
922 int force_sync, int start, int count)
927 const char *syntax, *elementSetName;
931 yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
932 " count=%d", r, force_sync, start, count);
937 if (c->host_port && c->proto == PROTO_HTTP)
941 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
942 ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
946 yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
951 task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
952 task->u.search.resultset = r;
953 task->u.search.start = start;
954 task->u.search.count = count;
956 syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
957 task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
958 elementSetName = ZOOM_options_get(r->options, "elementSetName");
959 task->u.search.elementSetName = elementSetName
960 ? xstrdup(elementSetName) : 0;
962 cp = ZOOM_options_get(r->options, "schema");
963 task->u.search.schema = cp ? xstrdup(cp) : 0;
965 ZOOM_resultset_addref(r);
967 if (!r->connection->async || force_sync)
968 while (r->connection && ZOOM_event(1, &r->connection))
973 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
974 size_t start, size_t count)
976 int force_present = 0;
980 yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
981 r, r, (long) start, (long) count);
984 ZOOM_resultset_retrieve(r, force_present, start, count);
988 for (i = 0; i< count; i++)
989 recs[i] = ZOOM_resultset_record_immediate(r, i+start);
994 ZOOM_resultset_facets_size(ZOOM_resultset r)
996 return r->num_res_facets;
999 ZOOM_API(ZOOM_facet_field)
1000 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name)
1002 int num = r->num_res_facets;
1003 ZOOM_facet_field *facets = r->res_facets;
1005 for (i = 0; i < num; i++)
1006 if (!strcmp(facets[i]->facet_name, name))
1011 ZOOM_API(ZOOM_facet_field)
1012 ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int idx)
1014 int num = r->num_res_facets;
1015 ZOOM_facet_field *facets = r->res_facets;
1016 if (idx >= 0 && idx < num)
1021 ZOOM_API(ZOOM_facet_field *)
1022 ZOOM_resultset_facets(ZOOM_resultset r)
1024 return r->res_facets;
1027 ZOOM_API(const char**)
1028 ZOOM_resultset_facets_names(ZOOM_resultset r)
1030 return (const char **) r->facets_names;
1033 ZOOM_API(const char*)
1034 ZOOM_facet_field_name(ZOOM_facet_field field)
1036 return field->facet_name;
1040 ZOOM_facet_field_term_count(ZOOM_facet_field field)
1042 return field->num_terms;
1045 ZOOM_API(const char*)
1046 ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq)
1048 *freq = field->facet_terms[idx].frequency;
1049 return field->facet_terms[idx].term;
1053 static void get_cert(ZOOM_connection c)
1058 if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1060 ZOOM_connection_option_setl(c, "sslPeerCert",
1061 cert_buf, cert_len);
1066 static zoom_ret do_connect_host(ZOOM_connection c,
1067 const char *logical_url);
1069 static zoom_ret do_connect(ZOOM_connection c)
1071 return do_connect_host(c, c->host_port);
1074 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1080 c->cs = cs_create_host2(logical_url, CS_FLAGS_DNS_NO_BLOCK, &add,
1081 c->tproxy ? c->tproxy : c->proxy,
1086 if (c->cs && c->cs->protocol == PROTO_HTTP)
1089 c->proto = PROTO_HTTP;
1091 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1092 ZOOM_connection_close(c);
1093 return zoom_complete;
1098 int ret = cs_connect(c->cs, add);
1101 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1102 ZOOM_connection_put_event(c, event);
1104 if (c->proto == PROTO_Z3950)
1105 ZOOM_connection_Z3950_send_init(c);
1108 /* no init request for SRW .. */
1109 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1110 ZOOM_connection_remove_task(c);
1111 ZOOM_connection_set_mask(c, 0);
1113 if (c->cs && c->location)
1114 send_HTTP_redirect(c, c->location);
1116 ZOOM_connection_exec_task(c);
1118 c->state = STATE_ESTABLISHED;
1119 return zoom_pending;
1123 int mask = ZOOM_SELECT_EXCEPT;
1124 if (c->cs->io_pending & CS_WANT_WRITE)
1125 mask += ZOOM_SELECT_WRITE;
1126 if (c->cs->io_pending & CS_WANT_READ)
1127 mask += ZOOM_SELECT_READ;
1128 ZOOM_connection_set_mask(c, mask);
1129 c->state = STATE_CONNECTING;
1130 return zoom_pending;
1133 c->state = STATE_IDLE;
1134 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1135 return zoom_complete;
1138 /* returns 1 if PDU was sent OK (still pending )
1139 0 if PDU was not sent OK (nothing to wait for)
1142 ZOOM_API(ZOOM_record)
1143 ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1145 const char *syntax =
1146 ZOOM_options_get(s->options, "preferredRecordSyntax");
1147 const char *elementSetName =
1148 ZOOM_options_get(s->options, "elementSetName");
1149 const char *schema =
1150 ZOOM_options_get(s->options, "schema");
1152 return ZOOM_record_cache_lookup_i(s, pos, syntax, elementSetName, schema);
1155 ZOOM_API(ZOOM_record)
1156 ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1158 ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1163 * MIKE: I think force_sync should always be zero, but I don't
1164 * want to make this change until I get the go-ahead from
1165 * Adam, in case something depends on the old synchronous
1169 if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1170 ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1171 rec = ZOOM_resultset_record_immediate(r, pos);
1176 ZOOM_API(ZOOM_scanset)
1177 ZOOM_connection_scan(ZOOM_connection c, const char *start)
1180 ZOOM_query q = ZOOM_query_create();
1182 ZOOM_query_prefix(q, start);
1184 s = ZOOM_connection_scan1(c, q);
1185 ZOOM_query_destroy(q);
1190 ZOOM_API(ZOOM_scanset)
1191 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1193 ZOOM_scanset scan = 0;
1194 Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1198 scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1199 scan->connection = c;
1200 scan->odr = odr_createmem(ODR_DECODE);
1201 scan->options = ZOOM_options_create_with_parent(c->options);
1203 scan->scan_response = 0;
1204 scan->srw_scan_response = 0;
1207 ZOOM_query_addref(q);
1208 scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1209 &scan->num_databaseNames,
1214 ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1215 task->u.scan.scan = scan;
1220 while (ZOOM_event(1, &c))
1228 ZOOM_scanset_destroy(ZOOM_scanset scan)
1233 if (scan->refcount == 0)
1235 ZOOM_query_destroy(scan->query);
1237 odr_destroy(scan->odr);
1239 ZOOM_options_destroy(scan->options);
1244 static zoom_ret send_package(ZOOM_connection c)
1248 yaz_log(c->log_details, "%p send_package", c);
1250 return zoom_complete;
1251 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1253 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1254 ZOOM_connection_put_event(c, event);
1256 c->buf_out = c->tasks->u.package->buf_out;
1257 c->len_out = c->tasks->u.package->len_out;
1259 return ZOOM_send_buf(c);
1263 ZOOM_scanset_size(ZOOM_scanset scan)
1268 if (scan->scan_response && scan->scan_response->entries)
1269 return scan->scan_response->entries->num_entries;
1270 else if (scan->srw_scan_response)
1271 return scan->srw_scan_response->num_terms;
1275 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1277 const char **value_term, size_t *value_len,
1278 const char **disp_term, size_t *disp_len)
1280 size_t noent = ZOOM_scanset_size(scan);
1291 if (scan->scan_response)
1293 Z_ScanResponse *res = scan->scan_response;
1294 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1296 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1298 *value_term = (const char *) t->term->u.general->buf;
1299 *value_len = t->term->u.general->len;
1302 *disp_term = t->displayTerm;
1303 *disp_len = strlen(*disp_term);
1305 else if (t->term->which == Z_Term_general)
1307 *disp_term = (const char *) t->term->u.general->buf;
1308 *disp_len = t->term->u.general->len;
1310 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1313 if (scan->srw_scan_response)
1315 Z_SRW_scanResponse *res = scan->srw_scan_response;
1316 Z_SRW_scanTerm *t = res->terms + pos;
1319 *value_term = t->value;
1320 *value_len = strlen(*value_term);
1323 *disp_term = t->displayTerm;
1325 *disp_term = t->value;
1326 *disp_len = strlen(*disp_term);
1327 *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1332 ZOOM_API(const char *)
1333 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1334 size_t *occ, size_t *len)
1336 const char *value_term = 0;
1337 size_t value_len = 0;
1338 const char *disp_term = 0;
1339 size_t disp_len = 0;
1341 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1342 &disp_term, &disp_len);
1348 ZOOM_API(const char *)
1349 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1350 size_t *occ, size_t *len)
1352 const char *value_term = 0;
1353 size_t value_len = 0;
1354 const char *disp_term = 0;
1355 size_t disp_len = 0;
1357 ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1358 &disp_term, &disp_len);
1364 ZOOM_API(const char *)
1365 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1367 return ZOOM_options_get(scan->options, key);
1371 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1374 ZOOM_options_set(scan->options, key, val);
1378 ZOOM_API(ZOOM_package)
1379 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1381 ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1384 p->odr_out = odr_createmem(ODR_ENCODE);
1385 p->options = ZOOM_options_create_with_parent2(options, c->options);
1393 ZOOM_package_destroy(ZOOM_package p)
1398 if (p->refcount == 0)
1400 odr_destroy(p->odr_out);
1403 ZOOM_options_destroy(p->options);
1408 ZOOM_API(const char *)
1409 ZOOM_package_option_get(ZOOM_package p, const char *key)
1411 return ZOOM_options_get(p->options, key);
1414 ZOOM_API(const char *)
1415 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1417 return ZOOM_options_getl(p->options, key, lenp);
1421 ZOOM_package_option_set(ZOOM_package p, const char *key,
1424 ZOOM_options_set(p->options, key, val);
1428 ZOOM_package_option_setl(ZOOM_package p, const char *key,
1429 const char *val, int len)
1431 ZOOM_options_setl(p->options, key, val, len);
1435 ZOOM_connection_exec_task(ZOOM_connection c)
1437 ZOOM_task task = c->tasks;
1438 zoom_ret ret = zoom_complete;
1442 yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1443 c, task->which, task->running);
1444 if (c->error != ZOOM_ERROR_NONE)
1446 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1447 "removing tasks because of error = %d", c, c->error);
1448 ZOOM_connection_remove_tasks(c);
1453 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1454 "task already running", c);
1458 ret = zoom_complete;
1459 if (c->cs || task->which == ZOOM_TASK_CONNECT)
1461 switch (task->which)
1463 case ZOOM_TASK_SEARCH:
1464 if (c->proto == PROTO_HTTP)
1465 ret = ZOOM_connection_srw_send_search(c);
1467 ret = ZOOM_connection_Z3950_search(c);
1469 case ZOOM_TASK_CONNECT:
1470 ret = do_connect(c);
1472 case ZOOM_TASK_SCAN:
1473 if (c->proto == PROTO_HTTP)
1474 ret = ZOOM_connection_srw_send_scan(c);
1476 ret = ZOOM_connection_Z3950_send_scan(c);
1478 case ZOOM_TASK_PACKAGE:
1479 ret = send_package(c);
1481 case ZOOM_TASK_SORT:
1482 c->tasks->u.sort.resultset->r_sort_spec =
1483 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1484 ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1490 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1491 "remove tasks because no connection exist", c);
1492 ZOOM_connection_remove_tasks(c);
1494 if (ret == zoom_complete)
1496 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1497 "task removed (complete)", c);
1498 ZOOM_connection_remove_task(c);
1501 yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1507 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1509 Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1511 gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1512 z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1514 yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1515 if (c->user && c->password)
1517 z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1518 c->user, c->password);
1522 return ZOOM_send_GDU(c, gdu);
1526 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1530 int r = z_GDU(c->odr_out, &gdu, 0, 0);
1532 return zoom_complete;
1534 z_GDU(c->odr_print, &gdu, 0, 0);
1536 z_GDU(c->odr_save, &gdu, 0, 0);
1537 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1538 odr_reset(c->odr_out);
1540 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1541 ZOOM_connection_put_event(c, event);
1543 return ZOOM_send_buf(c);
1546 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1547 const char *addinfo, const char *addinfo2)
1549 ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1553 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1555 zoom_ret cret = zoom_complete;
1558 const char *connection_head = z_HTTP_header_lookup(hres->headers,
1560 const char *location;
1562 ZOOM_connection_set_mask(c, 0);
1563 yaz_log(c->log_details, "%p handle_http", c);
1565 yaz_cookies_response(c->cookies, hres);
1566 if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1567 && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1570 if (c->no_redirects > 10)
1572 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1573 c->no_redirects = 0;
1574 ZOOM_connection_close(c);
1578 /* since redirect may change host we just reconnect. A smarter
1579 implementation might check whether it's the same server */
1581 int host_change = 0;
1582 location = yaz_check_location(c->odr_in, c->host_port,
1583 location, &host_change);
1585 c->location = xstrdup(location);
1586 do_connect_host(c, location);
1592 ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1595 if (c->no_redirects) /* end of redirect. change hosts again */
1596 ZOOM_connection_close(c);
1598 c->no_redirects = 0;
1602 if (hres->code != 200)
1603 ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1606 yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1608 ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1610 ZOOM_connection_close(c);
1612 if (cret == zoom_complete)
1614 yaz_log(c->log_details, "removing tasks in handle_http");
1615 ZOOM_connection_remove_task(c);
1619 if (!strcmp(hres->version, "1.0"))
1621 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1622 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1627 /* HTTP 1.1: only if no close we stay alive.. */
1628 if (connection_head && !strcmp(connection_head, "close"))
1633 ZOOM_connection_close(c);
1636 c->tasks->running = 0;
1637 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1638 c->reconnect_ok = 0;
1642 c->reconnect_ok = 1; /* if the server closes anyway */
1647 static int do_read(ZOOM_connection c)
1652 event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1653 ZOOM_connection_put_event(c, event);
1655 r = cs_get(c->cs, &c->buf_in, &c->len_in);
1656 more = cs_more(c->cs);
1657 yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1662 if (!ZOOM_test_reconnect(c))
1664 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1665 ZOOM_connection_close(c);
1673 odr_reset(c->odr_in);
1674 odr_setbuf(c->odr_in, c->buf_in, r, 0);
1675 event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1676 ZOOM_connection_put_event(c, event);
1678 if (!z_GDU(c->odr_in, &gdu, 0, 0))
1680 /* even on failures we try to re-connect (HTTP) */
1681 if (!ZOOM_test_reconnect(c))
1684 int err = odr_geterrorx(c->odr_in, &x);
1686 const char *element = odr_getelement(c->odr_in);
1687 yaz_snprintf(msg, sizeof(msg),
1688 "ODR code %d:%d element=%s offset=%d",
1689 err, x, element ? element : "<unknown>",
1690 odr_offset(c->odr_in));
1691 ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1694 FILE *ber_file = yaz_log_file();
1696 odr_dumpBER(ber_file, c->buf_in, r);
1698 ZOOM_connection_close(c);
1704 z_GDU(c->odr_print, &gdu, 0, 0);
1706 z_GDU(c->odr_save, &gdu, 0, 0);
1707 if (gdu->which == Z_GDU_Z3950)
1708 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1709 else if (gdu->which == Z_GDU_HTTP_Response)
1712 handle_http(c, gdu->u.HTTP_Response);
1714 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1715 ZOOM_connection_close(c);
1723 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1728 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1729 ZOOM_connection_put_event(c, event);
1731 yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1732 if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1734 yaz_log(c->log_details, "%p do_write_ex write failed", c);
1735 if (ZOOM_test_reconnect(c))
1737 return zoom_pending;
1739 if (c->state == STATE_CONNECTING)
1740 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1742 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1743 ZOOM_connection_close(c);
1744 return zoom_complete;
1748 int mask = ZOOM_SELECT_EXCEPT;
1749 if (c->cs->io_pending & CS_WANT_WRITE)
1750 mask += ZOOM_SELECT_WRITE;
1751 if (c->cs->io_pending & CS_WANT_READ)
1752 mask += ZOOM_SELECT_READ;
1753 ZOOM_connection_set_mask(c, mask);
1754 yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1759 ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1760 yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1763 return zoom_pending;
1766 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1768 return do_write_ex(c, c->buf_out, c->len_out);
1772 ZOOM_API(const char *)
1773 ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1775 if (!strcmp(key, "APDU"))
1777 return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1780 return ZOOM_options_get(c->options, key);
1783 ZOOM_API(const char *)
1784 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1786 if (!strcmp(key, "APDU"))
1788 if (c->saveAPDU_wrbuf)
1790 *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1791 return wrbuf_cstr(c->saveAPDU_wrbuf);
1800 return ZOOM_options_getl(c->options, key, lenp);
1804 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1807 if (!strcmp(key, "saveAPDU"))
1809 if (val && strcmp(val, "0"))
1811 if (!c->saveAPDU_wrbuf)
1812 c->saveAPDU_wrbuf = wrbuf_alloc();
1814 wrbuf_rewind(c->saveAPDU_wrbuf);
1818 wrbuf_destroy(c->saveAPDU_wrbuf);
1819 c->saveAPDU_wrbuf = 0;
1821 ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1824 ZOOM_options_set(c->options, key, val);
1828 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1829 const char *val, int len)
1831 ZOOM_options_setl(c->options, key, val, len);
1834 ZOOM_API(const char *)
1835 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1837 return ZOOM_options_get(r->options, key);
1841 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1844 ZOOM_options_set(r->options, key, val);
1849 ZOOM_connection_errcode(ZOOM_connection c)
1851 return ZOOM_connection_error(c, 0, 0);
1854 ZOOM_API(const char *)
1855 ZOOM_connection_errmsg(ZOOM_connection c)
1858 ZOOM_connection_error(c, &msg, 0);
1862 ZOOM_API(const char *)
1863 ZOOM_connection_addinfo(ZOOM_connection c)
1865 const char *addinfo;
1866 ZOOM_connection_error(c, 0, &addinfo);
1870 ZOOM_API(const char *)
1871 ZOOM_connection_diagset(ZOOM_connection c)
1873 const char *diagset;
1874 ZOOM_connection_error_x(c, 0, 0, &diagset);
1878 ZOOM_API(const char *)
1879 ZOOM_diag_str(int error)
1883 case ZOOM_ERROR_NONE:
1885 case ZOOM_ERROR_CONNECT:
1886 return "Connect failed";
1887 case ZOOM_ERROR_MEMORY:
1888 return "Out of memory";
1889 case ZOOM_ERROR_ENCODE:
1890 return "Encoding failed";
1891 case ZOOM_ERROR_DECODE:
1892 return "Decoding failed";
1893 case ZOOM_ERROR_CONNECTION_LOST:
1894 return "Connection lost";
1895 case ZOOM_ERROR_INIT:
1896 return "Init rejected";
1897 case ZOOM_ERROR_INTERNAL:
1898 return "Internal failure";
1899 case ZOOM_ERROR_TIMEOUT:
1901 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1902 return "Unsupported protocol";
1903 case ZOOM_ERROR_UNSUPPORTED_QUERY:
1904 return "Unsupported query type";
1905 case ZOOM_ERROR_INVALID_QUERY:
1906 return "Invalid query";
1907 case ZOOM_ERROR_CQL_PARSE:
1908 return "CQL parsing error";
1909 case ZOOM_ERROR_CQL_TRANSFORM:
1910 return "CQL transformation error";
1911 case ZOOM_ERROR_CCL_CONFIG:
1912 return "CCL configuration error";
1913 case ZOOM_ERROR_CCL_PARSE:
1914 return "CCL parsing error";
1915 case ZOOM_ERROR_ES_INVALID_ACTION:
1916 return "Extended Service. invalid action";
1917 case ZOOM_ERROR_ES_INVALID_VERSION:
1918 return "Extended Service. invalid version";
1919 case ZOOM_ERROR_ES_INVALID_SYNTAX:
1920 return "Extended Service. invalid syntax";
1921 case ZOOM_ERROR_MEMCACHED:
1924 return diagbib1_str(error);
1929 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1930 const char **addinfo, const char **diagset)
1932 int error = c->error;
1935 if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1936 *cp = ZOOM_diag_str(error);
1937 else if (!strcmp(c->diagset, "HTTP"))
1938 *cp = z_HTTP_errmsg(c->error);
1939 else if (!strcmp(c->diagset, "Bib-1"))
1940 *cp = ZOOM_diag_str(error);
1941 else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1942 *cp = yaz_diag_srw_str(c->error);
1944 *cp = "Unknown error and diagnostic set";
1947 *addinfo = c->addinfo ? c->addinfo : "";
1949 *diagset = c->diagset ? c->diagset : "";
1954 ZOOM_connection_error(ZOOM_connection c, const char **cp,
1955 const char **addinfo)
1957 return ZOOM_connection_error_x(c, cp, addinfo, 0);
1960 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1962 ZOOM_Event event = 0;
1963 int r = cs_look(c->cs);
1964 yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1969 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1970 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1971 ZOOM_connection_close(c);
1972 ZOOM_connection_put_event(c, event);
1974 else if (r == CS_CONNECT)
1976 int ret = ret = cs_rcvconnect(c->cs);
1977 yaz_log(c->log_details, "%p ZOOM_connection_do_io "
1978 "cs_rcvconnect returned %d", c, ret);
1981 int mask = ZOOM_SELECT_EXCEPT;
1982 if (c->cs->io_pending & CS_WANT_WRITE)
1983 mask += ZOOM_SELECT_WRITE;
1984 if (c->cs->io_pending & CS_WANT_READ)
1985 mask += ZOOM_SELECT_READ;
1986 ZOOM_connection_set_mask(c, mask);
1987 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1988 ZOOM_connection_put_event(c, event);
1992 event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1993 ZOOM_connection_put_event(c, event);
1995 if (c->proto == PROTO_Z3950)
1996 ZOOM_connection_Z3950_send_init(c);
1999 /* no init request for SRW .. */
2000 if (c->tasks->which == ZOOM_TASK_CONNECT)
2002 ZOOM_connection_remove_task(c);
2003 ZOOM_connection_set_mask(c, 0);
2005 if (c->cs && c->location)
2006 send_HTTP_redirect(c, c->location);
2008 ZOOM_connection_exec_task(c);
2010 c->state = STATE_ESTABLISHED;
2014 ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2015 ZOOM_connection_close(c);
2020 if (mask & ZOOM_SELECT_EXCEPT)
2022 if (!ZOOM_test_reconnect(c))
2024 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2025 ZOOM_connection_close(c);
2029 if (mask & ZOOM_SELECT_READ)
2031 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2037 ZOOM_connection_last_event(ZOOM_connection cs)
2040 return ZOOM_EVENT_NONE;
2041 return cs->last_event;
2045 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2049 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2050 /* timeout and this connection was waiting */
2051 ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2052 ZOOM_connection_close(c);
2053 ZOOM_connection_put_event(c, event);
2059 ZOOM_connection_process(ZOOM_connection c)
2065 event = ZOOM_connection_get_event(c);
2068 ZOOM_Event_destroy(event);
2071 ZOOM_connection_exec_task(c);
2072 event = ZOOM_connection_get_event(c);
2075 ZOOM_Event_destroy(event);
2082 ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2086 yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2088 for (i = 0; i<no; i++)
2090 ZOOM_connection c = cs[i];
2092 if (c && ZOOM_connection_process(c))
2098 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2100 if (c->mask && mask)
2101 ZOOM_connection_do_io(c, mask);
2105 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2108 return cs_fileno(c->cs);
2112 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2120 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2127 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2129 return ZOOM_options_get_int(c->options, "timeout", 30);
2132 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2137 ZOOM_connection_set_mask(c, 0);
2138 c->state = STATE_IDLE;
2144 * c-file-style: "Stroustrup"
2145 * indent-tabs-mode: nil
2147 * vim: shiftwidth=4 tabstop=8 expandtab