zoom: one common search task
[yaz-moved-to-github.git] / src / zoom-c.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-c.c
7  * \brief Implements ZOOM C interface.
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "zoom-p.h"
17
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/otherinfo.h>
21 #include <yaz/log.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>
27
28 #include <yaz/shptr.h>
29
30 #if SHPTR
31 YAZ_SHPTR_TYPE(WRBUF)
32 #endif
33
34 static int log_api0 = 0;
35 static int log_details0 = 0;
36
37 static void resultset_destroy(ZOOM_resultset r);
38 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
39
40 static void initlog(void)
41 {
42     static int log_level_initialized = 0;
43
44     if (!log_level_initialized)
45     {
46         log_api0 = yaz_log_module_level("zoom");
47         log_details0 = yaz_log_module_level("zoomdetails");
48         log_level_initialized = 1;
49     }
50 }
51
52 void ZOOM_connection_remove_tasks(ZOOM_connection c);
53
54 void ZOOM_set_dset_error(ZOOM_connection c, int error,
55                          const char *dset,
56                          const char *addinfo, const char *addinfo2)
57 {
58     char *cp;
59
60     xfree(c->addinfo);
61     c->addinfo = 0;
62     c->error = error;
63     if (!c->diagset || strcmp(dset, c->diagset))
64     {
65         xfree(c->diagset);
66         c->diagset = xstrdup(dset);
67         /* remove integer part from SRW diagset .. */
68         if ((cp = strrchr(c->diagset, '/')))
69             *cp = '\0';
70     }
71     if (addinfo && addinfo2)
72     {
73         c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 3);
74         strcpy(c->addinfo, addinfo);
75         strcat(c->addinfo, ": ");
76         strcat(c->addinfo, addinfo2);
77     }
78     else if (addinfo)
79         c->addinfo = xstrdup(addinfo);
80     if (error != ZOOM_ERROR_NONE)
81     {
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);
87     }
88 }
89
90 int ZOOM_uri_to_code(const char *uri)
91 {
92     int code = 0;
93     const char *cp;
94     if ((cp = strrchr(uri, '/')))
95         code = atoi(cp+1);
96     return code;
97 }
98
99
100
101 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
102 {
103     ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
104 }
105
106 static void clear_error(ZOOM_connection c)
107 {
108     /*
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.
114      */
115
116     ZOOM_connection_remove_events(c);
117     switch (c->error)
118     {
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:
126         break;
127     default:
128         ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
129     }
130 }
131
132 void ZOOM_connection_show_task(ZOOM_task task)
133 {
134     switch(task->which)
135     {
136     case ZOOM_TASK_SEARCH:
137         yaz_log(YLOG_LOG, "search p=%p", task);
138         break;
139     case ZOOM_TASK_CONNECT:
140         yaz_log(YLOG_LOG, "connect p=%p", task);
141         break;
142     case ZOOM_TASK_SCAN:
143         yaz_log(YLOG_LOG, "scan p=%p", task);
144         break;
145     }
146 }
147
148 void ZOOM_connection_show_tasks(ZOOM_connection c)
149 {
150     ZOOM_task task;
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);
154 }
155
156 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
157 {
158     ZOOM_task *taskp = &c->tasks;
159     while (*taskp)
160         taskp = &(*taskp)->next;
161     *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
162     (*taskp)->running = 0;
163     (*taskp)->which = which;
164     (*taskp)->next = 0;
165     clear_error(c);
166     return *taskp;
167 }
168
169 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
170 {
171     return c->tasks ? 0 : 1;
172 }
173
174 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
175 {
176     ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
177
178     task->next = c->tasks;
179     c->tasks = task;
180
181     task->running = 0;
182     task->which = which;
183     return task;
184 }
185
186 void ZOOM_connection_remove_task(ZOOM_connection c)
187 {
188     ZOOM_task task = c->tasks;
189
190     if (task)
191     {
192         c->tasks = task->next;
193         switch (task->which)
194         {
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);
200             break;
201         case ZOOM_TASK_CONNECT:
202             break;
203         case ZOOM_TASK_SCAN:
204             ZOOM_scanset_destroy(task->u.scan.scan);
205             break;
206         case ZOOM_TASK_PACKAGE:
207             ZOOM_package_destroy(task->u.package);
208             break;
209         case ZOOM_TASK_SORT:
210             resultset_destroy(task->u.sort.resultset);
211             ZOOM_query_destroy(task->u.sort.q);
212             break;
213         default:
214             assert(0);
215         }
216         xfree(task);
217
218         if (!c->tasks)
219         {
220             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
221             ZOOM_connection_put_event(c, event);
222         }
223     }
224 }
225
226 void ZOOM_connection_remove_tasks(ZOOM_connection c)
227 {
228     while (c->tasks)
229         ZOOM_connection_remove_task(c);
230 }
231
232 static void odr_wrbuf_write(ODR o, void *handle, int type,
233                             const char *buf, int len)
234 {
235     WRBUF w = (WRBUF) handle;
236     wrbuf_write(w, buf, len);
237 }
238
239 ZOOM_API(ZOOM_connection)
240     ZOOM_connection_create(ZOOM_options options)
241 {
242     ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
243
244     initlog();
245
246     c->log_api = log_api0;
247     c->log_details = log_details0;
248
249     yaz_log(c->log_api, "%p ZOOM_connection_create", c);
250
251     c->proto = PROTO_Z3950;
252     c->cs = 0;
253     ZOOM_connection_set_mask(c, 0);
254     c->reconnect_ok = 0;
255     c->state = STATE_IDLE;
256     c->addinfo = 0;
257     c->diagset = 0;
258     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
259     c->buf_in = 0;
260     c->len_in = 0;
261     c->buf_out = 0;
262     c->len_out = 0;
263     c->resultsets = 0;
264
265     c->options = ZOOM_options_create_with_parent(options);
266
267     c->host_port = 0;
268     c->proxy = 0;
269     c->tproxy = 0;
270
271     c->charset = c->lang = 0;
272
273     c->cookie_out = 0;
274     c->cookie_in = 0;
275     c->client_IP = 0;
276     c->tasks = 0;
277
278     c->user = 0;
279     c->group = 0;
280     c->password = 0;
281     c->url_authentication = 0;
282
283     c->maximum_record_size = 0;
284     c->preferred_message_size = 0;
285
286     c->odr_in = odr_createmem(ODR_DECODE);
287     c->odr_out = odr_createmem(ODR_ENCODE);
288     c->odr_print = 0;
289     c->odr_save = 0;
290
291     c->async = 0;
292     c->support_named_resultsets = 0;
293     c->last_event = ZOOM_EVENT_NONE;
294
295     c->m_queue_front = 0;
296     c->m_queue_back = 0;
297
298     c->sru_version = 0;
299     c->no_redirects = 0;
300     c->cookies = 0;
301     c->saveAPDU_wrbuf = 0;
302
303 #if HAVE_LIBMEMCACHED_MEMCACHED_H
304     c->mc_st = 0;
305 #endif
306     return c;
307 }
308
309 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
310 {
311     if (c->odr_save)
312     {
313         odr_destroy(c->odr_save);
314         c->odr_save = 0;
315     }
316     if (w)
317     {
318         c->odr_save = odr_createmem(ODR_PRINT);
319         odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
320     }
321 }
322
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,
326                                      int *num, ODR odr)
327 {
328     char **databaseNames;
329     const char *cp = ZOOM_options_get(options, "databaseName");
330
331     if ((!cp || !*cp) && con->host_port)
332         cs_get_host_args(con->host_port, &cp);
333     if (!cp || !*cp)
334         cp = "Default";
335     nmem_strsplit(odr_getmem(odr), "+", cp,  &databaseNames, num);
336     return databaseNames;
337 }
338
339 ZOOM_API(ZOOM_connection)
340     ZOOM_connection_new(const char *host, int portnum)
341 {
342     ZOOM_connection c = ZOOM_connection_create(0);
343
344     ZOOM_connection_connect(c, host, portnum);
345     return c;
346 }
347
348 static zoom_sru_mode get_sru_mode_from_string(const char *s)
349 {
350     if (!s || !*s)
351         return zoom_sru_soap;
352     if (!yaz_matchstr(s, "soap"))
353         return zoom_sru_soap;
354     else if (!yaz_matchstr(s, "get"))
355         return zoom_sru_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;
361 }
362
363 ZOOM_API(void)
364     ZOOM_connection_connect(ZOOM_connection c,
365                             const char *host, int portnum)
366 {
367     const char *val;
368     const char *http_lead;
369
370     initlog();
371
372     yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
373             c, host ? host : "null", portnum);
374
375     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
376     ZOOM_connection_remove_tasks(c);
377
378     if (c->odr_print)
379     {
380         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
381         odr_destroy(c->odr_print);
382     }
383     if (ZOOM_options_get_bool(c->options, "apdulog", 0))
384     {
385         c->odr_print = odr_createmem(ODR_PRINT);
386         odr_setprint(c->odr_print, yaz_log_file());
387     }
388     else
389         c->odr_print = 0;
390
391     if (c->cs)
392     {
393         yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
394         c->reconnect_ok = 1;
395         return;
396     }
397     yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
398     xfree(c->proxy);
399     c->proxy = 0;
400     val = ZOOM_options_get(c->options, "proxy");
401     if (val && *val)
402     {
403         yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
404         c->proxy = xstrdup(val);
405     }
406
407     xfree(c->tproxy);
408     c->tproxy = 0;
409     val = ZOOM_options_get(c->options, "tproxy");
410     if (val && *val)
411     {
412         yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
413         c->tproxy = xstrdup(val);
414     }
415
416     xfree(c->charset);
417     c->charset = 0;
418     val = ZOOM_options_get(c->options, "charset");
419     if (val && *val)
420     {
421         yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
422         c->charset = xstrdup(val);
423     }
424
425     xfree(c->lang);
426     val = ZOOM_options_get(c->options, "lang");
427     if (val && *val)
428     {
429         yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
430         c->lang = xstrdup(val);
431     }
432     else
433         c->lang = 0;
434
435     val = ZOOM_options_get(c->options, "sru");
436     if (val && *val && !strstr(host, "://"))
437         http_lead = "http://";
438     else
439         http_lead = "";
440     c->sru_mode = get_sru_mode_from_string(val);
441
442     if (host)
443     {
444         char hostn[128];
445         xfree(c->host_port);
446         if (portnum)
447         {
448             sprintf(hostn, "%.80s:%d", host, portnum);
449             host = hostn;
450         }
451         c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
452         strcpy(c->host_port, http_lead);
453         strcat(c->host_port, host);
454     }
455
456     {
457         /*
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
462          * in a host string:
463          *          user=admin,password=secret,tcp:localhost:9999
464          */
465         char *remainder = c->host_port;
466         char *pcolon = strchr(remainder, ':');
467         char *pcomma;
468         char *pequals;
469         while ((pcomma = strchr(remainder, ',')) != 0 &&
470                (pcolon == 0 || pcomma < pcolon))
471         {
472             *pcomma = '\0';
473             if ((pequals = strchr(remainder, '=')) != 0)
474             {
475                 *pequals = '\0';
476                 ZOOM_connection_option_set(c, remainder, pequals+1);
477             }
478             remainder = pcomma+1;
479         }
480
481         if (remainder != c->host_port)
482         {
483             remainder = xstrdup(remainder);
484             xfree(c->host_port);
485             c->host_port = remainder;
486         }
487     }
488
489     xfree(c->sru_version);
490     val = ZOOM_options_get(c->options, "sru_version");
491     c->sru_version = xstrdup(val ? val : "1.2");
492
493     ZOOM_options_set(c->options, "host", c->host_port);
494
495     xfree(c->cookie_out);
496     c->cookie_out = 0;
497     val = ZOOM_options_get(c->options, "cookie");
498     if (val && *val)
499     {
500         yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
501         c->cookie_out = xstrdup(val);
502     }
503
504     xfree(c->client_IP);
505     c->client_IP = 0;
506     val = ZOOM_options_get(c->options, "clientIP");
507     if (val && *val)
508     {
509         yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
510                 c, val);
511         c->client_IP = xstrdup(val);
512     }
513
514     xfree(c->group);
515     c->group = 0;
516     val = ZOOM_options_get(c->options, "group");
517     if (val && *val)
518         c->group = xstrdup(val);
519
520     xfree(c->user);
521     c->user = 0;
522     val = ZOOM_options_get(c->options, "user");
523     if (val && *val)
524         c->user = xstrdup(val);
525
526     xfree(c->password);
527     c->password = 0;
528     val = ZOOM_options_get(c->options, "password");
529     if (!val)
530         val = ZOOM_options_get(c->options, "pass");
531     if (val && *val)
532         c->password = xstrdup(val);
533
534     val = ZOOM_options_get(c->options, "authenticationMode");
535     if (val && !strcmp(val, "url"))
536         c->url_authentication = 1;
537     else
538         c->url_authentication = 0;
539
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);
544
545     c->async = ZOOM_options_get_bool(c->options, "async", 0);
546
547     yaz_cookies_destroy(c->cookies);
548     c->cookies = yaz_cookies_create();
549
550 #if HAVE_LIBMEMCACHED_MEMCACHED_H
551     if (c->mc_st)
552     {
553         memcached_free(c->mc_st);
554         c->mc_st = 0;
555     }
556 #endif
557     val = ZOOM_options_get(c->options, "memcached");
558     if (val && *val)
559     {
560 #if HAVE_LIBMEMCACHED_MEMCACHED_H
561         c->mc_st = memcached(val, strlen(val));
562         if (!c->mc_st)
563         {
564             ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
565             return;
566         }
567 #else
568         ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
569         return;
570 #endif
571     }
572
573     if (c->sru_mode == zoom_sru_error)
574     {
575         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
576         ZOOM_connection_remove_tasks(c);
577         return;
578     }
579
580     yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
581     ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
582
583     if (!c->async)
584     {
585         while (ZOOM_event(1, &c))
586             ;
587     }
588 }
589
590 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
591 {
592     if (r->connection)
593     {
594         /* remove ourselves from the resultsets in connection */
595         ZOOM_resultset *rp = &r->connection->resultsets;
596         while (1)
597         {
598             assert(*rp);   /* we must be in this list!! */
599             if (*rp == r)
600             {   /* OK, we're here - take us out of it */
601                 *rp = (*rp)->next;
602                 break;
603             }
604             rp = &(*rp)->next;
605         }
606         r->connection = 0;
607     }
608 }
609
610 ZOOM_API(void)
611     ZOOM_connection_destroy(ZOOM_connection c)
612 {
613     ZOOM_resultset r;
614     if (!c)
615         return;
616     yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
617
618 #if HAVE_LIBMEMCACHED_MEMCACHED_H
619     if (c->mc_st)
620         memcached_free(c->mc_st);
621 #endif
622     if (c->cs)
623         cs_close(c->cs);
624
625     for (r = c->resultsets; r; r = r->next)
626         r->connection = 0;
627
628     xfree(c->buf_in);
629     xfree(c->addinfo);
630     xfree(c->diagset);
631     odr_destroy(c->odr_in);
632     odr_destroy(c->odr_out);
633     if (c->odr_save)
634         odr_destroy(c->odr_save);
635     if (c->odr_print)
636     {
637         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
638         odr_destroy(c->odr_print);
639     }
640     ZOOM_options_destroy(c->options);
641     ZOOM_connection_remove_tasks(c);
642     ZOOM_connection_remove_events(c);
643     xfree(c->host_port);
644     xfree(c->proxy);
645     xfree(c->tproxy);
646     xfree(c->charset);
647     xfree(c->lang);
648     xfree(c->cookie_out);
649     xfree(c->cookie_in);
650     xfree(c->client_IP);
651     xfree(c->user);
652     xfree(c->group);
653     xfree(c->password);
654     xfree(c->sru_version);
655     yaz_cookies_destroy(c->cookies);
656     wrbuf_destroy(c->saveAPDU_wrbuf);
657     xfree(c);
658 }
659
660 void ZOOM_resultset_addref(ZOOM_resultset r)
661 {
662     if (r)
663     {
664         yaz_mutex_enter(r->mutex);
665         (r->refcount)++;
666         yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
667                 r, r->refcount);
668         yaz_mutex_leave(r->mutex);
669     }
670 }
671
672 static int g_resultsets = 0;
673 static YAZ_MUTEX g_resultset_mutex = 0;
674
675 /* TODO We need to initialize this before running threaded:
676  * call resultset_use(0)  */
677
678 static int resultset_use(int delta) {
679     int resultset_count;
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;
687 }
688
689 int resultsets_count(void) {
690     return resultset_use(0);
691 }
692
693 ZOOM_resultset ZOOM_resultset_create(void)
694 {
695     int i;
696     ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
697
698     initlog();
699
700     yaz_log(log_details0, "%p ZOOM_resultset_create", r);
701     r->refcount = 1;
702     r->size = 0;
703     r->odr = odr_createmem(ODR_ENCODE);
704     r->piggyback = 1;
705     r->setname = 0;
706     r->step = 0;
707     for (i = 0; i<RECORD_HASH_SIZE; i++)
708         r->record_hash[i] = 0;
709     r->r_sort_spec = 0;
710     r->query = 0;
711     r->connection = 0;
712     r->databaseNames = 0;
713     r->num_databaseNames = 0;
714     r->facets = 0;
715     r->num_facets = 0;
716     r->facets_names = 0;
717     r->mutex = 0;
718     yaz_mutex_create(&r->mutex);
719 #if SHPTR
720     {
721         WRBUF w = wrbuf_alloc();
722         YAZ_SHPTR_INIT(r->record_wrbuf, w);
723     }
724 #endif
725     resultset_use(1);
726     r->mc_key = 0;
727     r->live_set = 0;
728     return r;
729 }
730
731 ZOOM_API(ZOOM_resultset)
732     ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
733 {
734     ZOOM_resultset r;
735     ZOOM_query s = ZOOM_query_create();
736
737     ZOOM_query_prefix(s, q);
738
739     r = ZOOM_connection_search(c, s);
740     ZOOM_query_destroy(s);
741     return r;
742 }
743
744 ZOOM_API(ZOOM_resultset)
745     ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
746 {
747     ZOOM_resultset r = ZOOM_resultset_create();
748     const char *cp;
749     ZOOM_task task;
750     int start, count;
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);
754     r->query = q;
755
756     r->options = ZOOM_options_create_with_parent(c->options);
757
758     start = ZOOM_options_get_int(r->options, "start", 0);
759     count = ZOOM_options_get_int(r->options, "count", 0);
760     {
761         /* If "presentChunk" is defined use that; otherwise "step" */
762         const char *cp = ZOOM_options_get(r->options, "presentChunk");
763         r->step = ZOOM_options_get_int(r->options,
764                                        (cp != 0 ? "presentChunk": "step"), 0);
765     }
766     r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
767     cp = ZOOM_options_get(r->options, "setname");
768     if (cp)
769         r->setname = xstrdup(cp);
770
771     r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
772                                          r->odr);
773
774     r->connection = c;
775     r->next = c->resultsets;
776     c->resultsets = r;
777
778 #if HAVE_LIBMEMCACHED_MEMCACHED_H
779     r->mc_key = wrbuf_alloc();
780     wrbuf_puts(r->mc_key, c->host_port);
781     wrbuf_puts(r->mc_key, ";");
782     wrbuf_puts(r->mc_key, ZOOM_query_get_query_string(q));
783     if (c->mc_st)
784     {
785         size_t v_len;
786         uint32_t flags;
787         memcached_return_t rc;
788         char *v = memcached_get(c->mc_st, wrbuf_buf(r->mc_key),
789                                 wrbuf_len(r->mc_key), &v_len, &flags, &rc);
790         if (v)
791         {
792             yaz_log(YLOG_LOG, "For key %s got value %.*s",
793                     wrbuf_cstr(r->mc_key), (int) v_len, v);
794         }
795         else
796         {
797             yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key));
798         }
799     }
800 #endif
801
802     if (c->host_port && c->proto == PROTO_HTTP)
803     {
804         if (!c->cs)
805         {
806             yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
807             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
808         }
809         else
810         {
811             yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
812             c->reconnect_ok = 1;
813         }
814     }
815
816     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
817     task->u.search.resultset = r;
818     task->u.search.start = start;
819     task->u.search.count = count;
820     task->u.search.recv_search_fired = 0;
821
822     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
823     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
824     elementSetName = ZOOM_options_get(r->options, "elementSetName");
825     task->u.search.elementSetName = elementSetName ?
826         xstrdup(elementSetName) : 0;
827     schema = ZOOM_options_get(r->options, "schema");
828     task->u.search.schema = schema ? xstrdup(schema) : 0;
829
830     ZOOM_resultset_addref(r);
831
832     ZOOM_query_addref(q);
833
834     if (!c->async)
835     {
836         while (ZOOM_event(1, &c))
837             ;
838     }
839     return r;
840 }
841
842 ZOOM_API(void)
843     ZOOM_resultset_sort(ZOOM_resultset r,
844                          const char *sort_type, const char *sort_spec)
845 {
846     (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
847 }
848
849 ZOOM_API(int)
850     ZOOM_resultset_sort1(ZOOM_resultset r,
851                          const char *sort_type, const char *sort_spec)
852 {
853     ZOOM_connection c = r->connection;
854     ZOOM_task task;
855     ZOOM_query newq;
856
857     newq = ZOOM_query_create();
858     if (ZOOM_query_sortby(newq, sort_spec) < 0)
859         return -1;
860
861     yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
862             r, r, sort_type, sort_spec);
863     if (!c)
864         return 0;
865
866     if (c->host_port && c->proto == PROTO_HTTP)
867     {
868         if (!c->cs)
869         {
870             yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
871             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
872         }
873         else
874         {
875             yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
876                     r);
877             c->reconnect_ok = 1;
878         }
879     }
880
881     ZOOM_resultset_cache_reset(r);
882     task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
883     task->u.sort.resultset = r;
884     task->u.sort.q = newq;
885
886     ZOOM_resultset_addref(r);
887
888     if (!c->async)
889     {
890         while (ZOOM_event(1, &c))
891             ;
892     }
893
894     return 0;
895 }
896
897 ZOOM_API(void)
898     ZOOM_resultset_destroy(ZOOM_resultset r)
899 {
900     resultset_destroy(r);
901 }
902
903 static void resultset_destroy(ZOOM_resultset r)
904 {
905     if (!r)
906         return;
907     yaz_mutex_enter(r->mutex);
908     (r->refcount)--;
909     yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
910             r, r, r->refcount);
911     if (r->refcount == 0)
912     {
913         yaz_mutex_leave(r->mutex);
914
915         yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
916         ZOOM_resultset_cache_reset(r);
917         ZOOM_resultset_release(r);
918         ZOOM_query_destroy(r->query);
919         ZOOM_options_destroy(r->options);
920         odr_destroy(r->odr);
921         xfree(r->setname);
922         yaz_mutex_destroy(&r->mutex);
923 #if SHPTR
924         YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
925 #endif
926         wrbuf_destroy(r->mc_key);
927         resultset_use(-1);
928         xfree(r);
929     }
930     else
931         yaz_mutex_leave(r->mutex);
932 }
933
934 ZOOM_API(size_t)
935     ZOOM_resultset_size(ZOOM_resultset r)
936 {
937     return r->size;
938 }
939
940 int ZOOM_test_reconnect(ZOOM_connection c)
941 {
942     ZOOM_Event event;
943
944     if (!c->reconnect_ok)
945         return 0;
946     ZOOM_connection_close(c);
947     c->reconnect_ok = 0;
948     c->tasks->running = 0;
949     ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
950
951     event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
952     ZOOM_connection_put_event(c, event);
953
954     return 1;
955 }
956
957 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
958                                     int force_sync, int start, int count)
959 {
960     ZOOM_task task;
961     ZOOM_connection c;
962     const char *cp;
963     const char *syntax, *elementSetName;
964
965     if (!r)
966         return;
967     yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
968             " count=%d", r, force_sync, start, count);
969     c = r->connection;
970     if (!c)
971         return;
972
973     if (c->host_port && c->proto == PROTO_HTTP)
974     {
975         if (!c->cs)
976         {
977             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
978             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
979         }
980         else
981         {
982             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
983                     "reconnect", r);
984             c->reconnect_ok = 1;
985         }
986     }
987     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
988     task->u.search.resultset = r;
989     task->u.search.start = start;
990     task->u.search.count = count;
991
992     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
993     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
994     elementSetName = ZOOM_options_get(r->options, "elementSetName");
995     task->u.search.elementSetName = elementSetName
996         ? xstrdup(elementSetName) : 0;
997
998     cp = ZOOM_options_get(r->options, "schema");
999     task->u.search.schema = cp ? xstrdup(cp) : 0;
1000
1001     ZOOM_resultset_addref(r);
1002
1003     if (!r->connection->async || force_sync)
1004         while (r->connection && ZOOM_event(1, &r->connection))
1005             ;
1006 }
1007
1008 ZOOM_API(void)
1009     ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1010                            size_t start, size_t count)
1011 {
1012     int force_present = 0;
1013
1014     if (!r)
1015         return ;
1016     yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1017             r, r, (long) start, (long) count);
1018     if (count && recs)
1019         force_present = 1;
1020     ZOOM_resultset_retrieve(r, force_present, start, count);
1021     if (force_present)
1022     {
1023         size_t i;
1024         for (i = 0; i< count; i++)
1025             recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1026     }
1027 }
1028
1029 ZOOM_API(size_t)
1030     ZOOM_resultset_facets_size(ZOOM_resultset r) {
1031     return r->num_facets;
1032 }
1033
1034 ZOOM_API(ZOOM_facet_field)
1035     ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
1036     int num = r->num_facets;
1037     ZOOM_facet_field *facets = r->facets;
1038     int index;
1039     for (index = 0; index < num; index++) {
1040         if (!strcmp(facets[index]->facet_name, name)) {
1041             return facets[index];
1042         }
1043     }
1044     return 0;
1045 }
1046
1047 ZOOM_API(ZOOM_facet_field)
1048     ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
1049     int num = r->num_facets;
1050     ZOOM_facet_field *facets = r->facets;
1051     if (index >= 0 && index < num) {
1052         return facets[index];
1053     }
1054     return 0;
1055 }
1056
1057 ZOOM_API(ZOOM_facet_field *)
1058     ZOOM_resultset_facets(ZOOM_resultset r)
1059 {
1060     return r->facets;
1061 }
1062
1063 ZOOM_API(const char**)
1064     ZOOM_resultset_facets_names(ZOOM_resultset r)
1065 {
1066     return (const char **) r->facets_names;
1067 }
1068
1069 ZOOM_API(const char*)
1070     ZOOM_facet_field_name(ZOOM_facet_field field)
1071 {
1072     return field->facet_name;
1073 }
1074
1075 ZOOM_API(size_t)
1076     ZOOM_facet_field_term_count(ZOOM_facet_field field)
1077 {
1078     return field->num_terms;
1079 }
1080
1081 ZOOM_API(const char*)
1082     ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1083     *freq = field->facet_terms[idx].frequency;
1084     return field->facet_terms[idx].term;
1085 }
1086
1087
1088 static void get_cert(ZOOM_connection c)
1089 {
1090     char *cert_buf;
1091     int cert_len;
1092
1093     if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1094     {
1095         ZOOM_connection_option_setl(c, "sslPeerCert",
1096                                     cert_buf, cert_len);
1097         xfree(cert_buf);
1098     }
1099 }
1100
1101 static zoom_ret do_connect_host(ZOOM_connection c,
1102                                 const char *logical_url);
1103
1104 static zoom_ret do_connect(ZOOM_connection c)
1105 {
1106     return do_connect_host(c, c->host_port);
1107 }
1108
1109 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1110 {
1111     void *add;
1112
1113     if (c->cs)
1114         cs_close(c->cs);
1115     c->cs = cs_create_host_proxy(logical_url, 0, &add,
1116                                  c->tproxy ? c->tproxy : c->proxy);
1117
1118     if (c->cs && c->cs->protocol == PROTO_HTTP)
1119     {
1120 #if YAZ_HAVE_XML2
1121         c->proto = PROTO_HTTP;
1122 #else
1123         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1124         ZOOM_connection_close(c);
1125         return zoom_complete;
1126 #endif
1127     }
1128     if (c->cs)
1129     {
1130         int ret = cs_connect(c->cs, add);
1131         if (ret == 0)
1132         {
1133             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1134             ZOOM_connection_put_event(c, event);
1135             get_cert(c);
1136             if (c->proto == PROTO_Z3950)
1137                 ZOOM_connection_Z3950_send_init(c);
1138             else
1139             {
1140                 /* no init request for SRW .. */
1141                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1142                 ZOOM_connection_remove_task(c);
1143                 ZOOM_connection_set_mask(c, 0);
1144                 ZOOM_connection_exec_task(c);
1145             }
1146             c->state = STATE_ESTABLISHED;
1147             return zoom_pending;
1148         }
1149         else if (ret > 0)
1150         {
1151             int mask = ZOOM_SELECT_EXCEPT;
1152             if (c->cs->io_pending & CS_WANT_WRITE)
1153                 mask += ZOOM_SELECT_WRITE;
1154             if (c->cs->io_pending & CS_WANT_READ)
1155                 mask += ZOOM_SELECT_READ;
1156             ZOOM_connection_set_mask(c, mask);
1157             c->state = STATE_CONNECTING;
1158             return zoom_pending;
1159         }
1160     }
1161     c->state = STATE_IDLE;
1162     ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1163     return zoom_complete;
1164 }
1165
1166 /* returns 1 if PDU was sent OK (still pending )
1167    0 if PDU was not sent OK (nothing to wait for)
1168 */
1169
1170 ZOOM_API(ZOOM_record)
1171     ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1172 {
1173     const char *syntax =
1174         ZOOM_options_get(s->options, "preferredRecordSyntax");
1175     const char *elementSetName =
1176         ZOOM_options_get(s->options, "elementSetName");
1177     const char *schema =
1178         ZOOM_options_get(s->options, "schema");
1179
1180     return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
1181 }
1182
1183 ZOOM_API(ZOOM_record)
1184     ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1185 {
1186     ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1187
1188     if (!rec)
1189     {
1190         /*
1191          * MIKE: I think force_sync should always be zero, but I don't
1192          * want to make this change until I get the go-ahead from
1193          * Adam, in case something depends on the old synchronous
1194          * behaviour.
1195          */
1196         int force_sync = 1;
1197         if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1198         ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1199         rec = ZOOM_resultset_record_immediate(r, pos);
1200     }
1201     return rec;
1202 }
1203
1204 ZOOM_API(ZOOM_scanset)
1205     ZOOM_connection_scan(ZOOM_connection c, const char *start)
1206 {
1207     ZOOM_scanset s;
1208     ZOOM_query q = ZOOM_query_create();
1209
1210     ZOOM_query_prefix(q, start);
1211
1212     s = ZOOM_connection_scan1(c, q);
1213     ZOOM_query_destroy(q);
1214     return s;
1215
1216 }
1217
1218 ZOOM_API(ZOOM_scanset)
1219     ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1220 {
1221     ZOOM_scanset scan = 0;
1222     Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1223
1224     if (!z_query)
1225         return 0;
1226     scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1227     scan->connection = c;
1228     scan->odr = odr_createmem(ODR_DECODE);
1229     scan->options = ZOOM_options_create_with_parent(c->options);
1230     scan->refcount = 1;
1231     scan->scan_response = 0;
1232     scan->srw_scan_response = 0;
1233
1234     scan->query = q;
1235     ZOOM_query_addref(q);
1236     scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1237                                             &scan->num_databaseNames,
1238                                             scan->odr);
1239
1240     if (1)
1241     {
1242         ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1243         task->u.scan.scan = scan;
1244
1245         (scan->refcount)++;
1246         if (!c->async)
1247         {
1248             while (ZOOM_event(1, &c))
1249                 ;
1250         }
1251     }
1252     return scan;
1253 }
1254
1255 ZOOM_API(void)
1256     ZOOM_scanset_destroy(ZOOM_scanset scan)
1257 {
1258     if (!scan)
1259         return;
1260     (scan->refcount)--;
1261     if (scan->refcount == 0)
1262     {
1263         ZOOM_query_destroy(scan->query);
1264
1265         odr_destroy(scan->odr);
1266
1267         ZOOM_options_destroy(scan->options);
1268         xfree(scan);
1269     }
1270 }
1271
1272 static zoom_ret send_package(ZOOM_connection c)
1273 {
1274     ZOOM_Event event;
1275
1276     yaz_log(c->log_details, "%p send_package", c);
1277     if (!c->tasks)
1278         return zoom_complete;
1279     assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1280
1281     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1282     ZOOM_connection_put_event(c, event);
1283
1284     c->buf_out = c->tasks->u.package->buf_out;
1285     c->len_out = c->tasks->u.package->len_out;
1286
1287     return ZOOM_send_buf(c);
1288 }
1289
1290 ZOOM_API(size_t)
1291     ZOOM_scanset_size(ZOOM_scanset scan)
1292 {
1293     if (!scan)
1294         return 0;
1295
1296     if (scan->scan_response && scan->scan_response->entries)
1297         return scan->scan_response->entries->num_entries;
1298     else if (scan->srw_scan_response)
1299         return scan->srw_scan_response->num_terms;
1300     return 0;
1301 }
1302
1303 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1304                                 size_t *occ,
1305                                 const char **value_term, size_t *value_len,
1306                                 const char **disp_term, size_t *disp_len)
1307 {
1308     size_t noent = ZOOM_scanset_size(scan);
1309
1310     *value_term = 0;
1311     *value_len = 0;
1312
1313     *disp_term = 0;
1314     *disp_len = 0;
1315
1316     *occ = 0;
1317     if (pos >= noent)
1318         return;
1319     if (scan->scan_response)
1320     {
1321         Z_ScanResponse *res = scan->scan_response;
1322         if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1323         {
1324             Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1325
1326             *value_term = (const char *) t->term->u.general->buf;
1327             *value_len = t->term->u.general->len;
1328             if (t->displayTerm)
1329             {
1330                 *disp_term = t->displayTerm;
1331                 *disp_len = strlen(*disp_term);
1332             }
1333             else if (t->term->which == Z_Term_general)
1334             {
1335                 *disp_term = (const char *) t->term->u.general->buf;
1336                 *disp_len = t->term->u.general->len;
1337             }
1338             *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1339         }
1340     }
1341     if (scan->srw_scan_response)
1342     {
1343         Z_SRW_scanResponse *res = scan->srw_scan_response;
1344         Z_SRW_scanTerm *t = res->terms + pos;
1345         if (t)
1346         {
1347             *value_term = t->value;
1348             *value_len = strlen(*value_term);
1349
1350             if (t->displayTerm)
1351                 *disp_term = t->displayTerm;
1352             else
1353                 *disp_term = t->value;
1354             *disp_len = strlen(*disp_term);
1355             *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1356         }
1357     }
1358 }
1359
1360 ZOOM_API(const char *)
1361     ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1362                       size_t *occ, size_t *len)
1363 {
1364     const char *value_term = 0;
1365     size_t value_len = 0;
1366     const char *disp_term = 0;
1367     size_t disp_len = 0;
1368
1369     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1370                         &disp_term, &disp_len);
1371
1372     *len = value_len;
1373     return value_term;
1374 }
1375
1376 ZOOM_API(const char *)
1377     ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1378                               size_t *occ, size_t *len)
1379 {
1380     const char *value_term = 0;
1381     size_t value_len = 0;
1382     const char *disp_term = 0;
1383     size_t disp_len = 0;
1384
1385     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1386                         &disp_term, &disp_len);
1387
1388     *len = disp_len;
1389     return disp_term;
1390 }
1391
1392 ZOOM_API(const char *)
1393     ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1394 {
1395     return ZOOM_options_get(scan->options, key);
1396 }
1397
1398 ZOOM_API(void)
1399     ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1400                             const char *val)
1401 {
1402     ZOOM_options_set(scan->options, key, val);
1403 }
1404
1405
1406 ZOOM_API(ZOOM_package)
1407     ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1408 {
1409     ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1410
1411     p->connection = c;
1412     p->odr_out = odr_createmem(ODR_ENCODE);
1413     p->options = ZOOM_options_create_with_parent2(options, c->options);
1414     p->refcount = 1;
1415     p->buf_out = 0;
1416     p->len_out = 0;
1417     return p;
1418 }
1419
1420 ZOOM_API(void)
1421     ZOOM_package_destroy(ZOOM_package p)
1422 {
1423     if (!p)
1424         return;
1425     (p->refcount)--;
1426     if (p->refcount == 0)
1427     {
1428         odr_destroy(p->odr_out);
1429         xfree(p->buf_out);
1430
1431         ZOOM_options_destroy(p->options);
1432         xfree(p);
1433     }
1434 }
1435
1436 ZOOM_API(const char *)
1437     ZOOM_package_option_get(ZOOM_package p, const char *key)
1438 {
1439     return ZOOM_options_get(p->options, key);
1440 }
1441
1442 ZOOM_API(const char *)
1443     ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1444 {
1445     return ZOOM_options_getl(p->options, key, lenp);
1446 }
1447
1448 ZOOM_API(void)
1449     ZOOM_package_option_set(ZOOM_package p, const char *key,
1450                             const char *val)
1451 {
1452     ZOOM_options_set(p->options, key, val);
1453 }
1454
1455 ZOOM_API(void)
1456     ZOOM_package_option_setl(ZOOM_package p, const char *key,
1457                              const char *val, int len)
1458 {
1459     ZOOM_options_setl(p->options, key, val, len);
1460 }
1461
1462 ZOOM_API(int)
1463     ZOOM_connection_exec_task(ZOOM_connection c)
1464 {
1465     ZOOM_task task = c->tasks;
1466     zoom_ret ret = zoom_complete;
1467
1468     if (!task)
1469         return 0;
1470     yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1471             c, task->which, task->running);
1472     if (c->error != ZOOM_ERROR_NONE)
1473     {
1474         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1475                 "removing tasks because of error = %d", c, c->error);
1476         ZOOM_connection_remove_tasks(c);
1477         return 0;
1478     }
1479     if (task->running)
1480     {
1481         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1482                 "task already running", c);
1483         return 0;
1484     }
1485     task->running = 1;
1486     ret = zoom_complete;
1487     if (c->cs || task->which == ZOOM_TASK_CONNECT)
1488     {
1489         switch (task->which)
1490         {
1491         case ZOOM_TASK_SEARCH:
1492             if (c->proto == PROTO_HTTP)
1493                 ret = ZOOM_connection_srw_send_search(c);
1494             else
1495                 ret = ZOOM_connection_Z3950_send_search(c);
1496             break;
1497         case ZOOM_TASK_CONNECT:
1498             ret = do_connect(c);
1499             break;
1500         case ZOOM_TASK_SCAN:
1501             if (c->proto == PROTO_HTTP)
1502                 ret = ZOOM_connection_srw_send_scan(c);
1503             else
1504                 ret = ZOOM_connection_Z3950_send_scan(c);
1505             break;
1506         case ZOOM_TASK_PACKAGE:
1507             ret = send_package(c);
1508             break;
1509         case ZOOM_TASK_SORT:
1510             c->tasks->u.sort.resultset->r_sort_spec =
1511                 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1512             ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1513             break;
1514         }
1515     }
1516     else
1517     {
1518         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1519                 "remove tasks because no connection exist", c);
1520         ZOOM_connection_remove_tasks(c);
1521     }
1522     if (ret == zoom_complete)
1523     {
1524         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1525                 "task removed (complete)", c);
1526         ZOOM_connection_remove_task(c);
1527         return 0;
1528     }
1529     yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1530             "task pending", c);
1531     return 1;
1532 }
1533
1534 #if YAZ_HAVE_XML2
1535
1536 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1537 {
1538     Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1539
1540     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1541     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1542                       "text/xml");
1543     yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1544     if (c->user && c->password)
1545     {
1546         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1547                                      c->user, c->password);
1548     }
1549     return ZOOM_send_GDU(c, gdu);
1550 }
1551
1552 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1553 {
1554     ZOOM_Event event;
1555
1556     int r = z_GDU(c->odr_out, &gdu, 0, 0);
1557     if (!r)
1558         return zoom_complete;
1559     if (c->odr_print)
1560         z_GDU(c->odr_print, &gdu, 0, 0);
1561     if (c->odr_save)
1562         z_GDU(c->odr_save, &gdu, 0, 0);
1563     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1564     odr_reset(c->odr_out);
1565
1566     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1567     ZOOM_connection_put_event(c, event);
1568
1569     return ZOOM_send_buf(c);
1570 }
1571
1572 #if YAZ_HAVE_XML2
1573 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1574                          const char *addinfo, const char *addinfo2)
1575 {
1576     ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1577 }
1578 #endif
1579
1580
1581 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1582 {
1583     zoom_ret cret = zoom_complete;
1584     int ret = -1;
1585     char *addinfo = 0;
1586     const char *connection_head = z_HTTP_header_lookup(hres->headers,
1587                                                        "Connection");
1588     const char *location;
1589
1590     ZOOM_connection_set_mask(c, 0);
1591     yaz_log(c->log_details, "%p handle_http", c);
1592
1593     yaz_cookies_response(c->cookies, hres);
1594     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1595         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1596     {
1597         c->no_redirects++;
1598         if (c->no_redirects > 10)
1599         {
1600             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1601             c->no_redirects = 0;
1602             ZOOM_connection_close(c);
1603         }
1604         else
1605         {
1606             /* since redirect may change host we just reconnect. A smarter
1607                implementation might check whether it's the same server */
1608
1609             int host_change = 0;
1610             location = yaz_check_location(c->odr_in, c->host_port,
1611                                           location, &host_change);
1612             if (do_connect_host(c, location) == zoom_complete)
1613                 return;  /* connect failed.. */
1614             send_HTTP_redirect(c, location);
1615             return;
1616         }
1617     }
1618     else
1619     {
1620         ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1621         if (ret == 0)
1622         {
1623             if (c->no_redirects) /* end of redirect. change hosts again */
1624                 ZOOM_connection_close(c);
1625         }
1626         c->no_redirects = 0;
1627     }
1628     if (ret)
1629     {
1630         if (hres->code != 200)
1631             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1632         else
1633         {
1634             yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1635                     addinfo : "NULL");
1636             ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1637         }
1638         ZOOM_connection_close(c);
1639     }
1640     if (cret == zoom_complete)
1641     {
1642         yaz_log(c->log_details, "removing tasks in handle_http");
1643         ZOOM_connection_remove_task(c);
1644     }
1645     {
1646         int must_close = 0;
1647         if (!strcmp(hres->version, "1.0"))
1648         {
1649             /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1650             if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1651                 must_close = 1;
1652         }
1653         else
1654         {
1655             /* HTTP 1.1: only if no close we stay alive.. */
1656             if (connection_head && !strcmp(connection_head, "close"))
1657                 must_close = 1;
1658         }
1659         if (must_close)
1660         {
1661             ZOOM_connection_close(c);
1662             if (c->tasks)
1663             {
1664                 c->tasks->running = 0;
1665                 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1666                 c->reconnect_ok = 0;
1667             }
1668         }
1669         else
1670             c->reconnect_ok = 1; /* if the server closes anyway */
1671     }
1672 }
1673 #endif
1674
1675 static int do_read(ZOOM_connection c)
1676 {
1677     int r, more;
1678     ZOOM_Event event;
1679
1680     event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1681     ZOOM_connection_put_event(c, event);
1682
1683     r = cs_get(c->cs, &c->buf_in, &c->len_in);
1684     more = cs_more(c->cs);
1685     yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1686     if (r == 1)
1687         return 0;
1688     if (r <= 0)
1689     {
1690         if (!ZOOM_test_reconnect(c))
1691         {
1692             ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1693             ZOOM_connection_close(c);
1694         }
1695     }
1696     else
1697     {
1698         Z_GDU *gdu;
1699         ZOOM_Event event;
1700
1701         odr_reset(c->odr_in);
1702         odr_setbuf(c->odr_in, c->buf_in, r, 0);
1703         event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1704         ZOOM_connection_put_event(c, event);
1705
1706         if (!z_GDU(c->odr_in, &gdu, 0, 0))
1707         {
1708             int x;
1709             int err = odr_geterrorx(c->odr_in, &x);
1710             char msg[100];
1711             const char *element = odr_getelement(c->odr_in);
1712             yaz_snprintf(msg, sizeof(msg),
1713                     "ODR code %d:%d element=%s offset=%d",
1714                     err, x, element ? element : "<unknown>",
1715                     odr_offset(c->odr_in));
1716             ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1717             if (c->log_api)
1718             {
1719                 FILE *ber_file = yaz_log_file();
1720                 if (ber_file)
1721                     odr_dumpBER(ber_file, c->buf_in, r);
1722             }
1723             ZOOM_connection_close(c);
1724         }
1725         else
1726         {
1727             if (c->odr_print)
1728                 z_GDU(c->odr_print, &gdu, 0, 0);
1729             if (c->odr_save)
1730                 z_GDU(c->odr_save, &gdu, 0, 0);
1731             if (gdu->which == Z_GDU_Z3950)
1732                 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1733             else if (gdu->which == Z_GDU_HTTP_Response)
1734             {
1735 #if YAZ_HAVE_XML2
1736                 handle_http(c, gdu->u.HTTP_Response);
1737 #else
1738                 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1739                 ZOOM_connection_close(c);
1740 #endif
1741             }
1742         }
1743     }
1744     return 1;
1745 }
1746
1747 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1748 {
1749     int r;
1750     ZOOM_Event event;
1751
1752     event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1753     ZOOM_connection_put_event(c, event);
1754
1755     yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1756     if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1757     {
1758         yaz_log(c->log_details, "%p do_write_ex write failed", c);
1759         if (ZOOM_test_reconnect(c))
1760         {
1761             return zoom_pending;
1762         }
1763         if (c->state == STATE_CONNECTING)
1764             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1765         else
1766             ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1767         ZOOM_connection_close(c);
1768         return zoom_complete;
1769     }
1770     else if (r == 1)
1771     {
1772         int mask = ZOOM_SELECT_EXCEPT;
1773         if (c->cs->io_pending & CS_WANT_WRITE)
1774             mask += ZOOM_SELECT_WRITE;
1775         if (c->cs->io_pending & CS_WANT_READ)
1776             mask += ZOOM_SELECT_READ;
1777         ZOOM_connection_set_mask(c, mask);
1778         yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1779                 c, c->mask);
1780     }
1781     else
1782     {
1783         ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1784         yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1785                 c, c->mask);
1786     }
1787     return zoom_pending;
1788 }
1789
1790 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1791 {
1792     return do_write_ex(c, c->buf_out, c->len_out);
1793 }
1794
1795
1796 ZOOM_API(const char *)
1797     ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1798 {
1799     if (!strcmp(key, "APDU"))
1800     {
1801         return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1802     }
1803     else
1804         return ZOOM_options_get(c->options, key);
1805 }
1806
1807 ZOOM_API(const char *)
1808     ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1809 {
1810     if (!strcmp(key, "APDU"))
1811     {
1812         if (c->saveAPDU_wrbuf)
1813         {
1814             *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1815             return wrbuf_cstr(c->saveAPDU_wrbuf);
1816         }
1817         else
1818         {
1819             *lenp = 0;
1820             return "";
1821         }
1822     }
1823     else
1824         return ZOOM_options_getl(c->options, key, lenp);
1825 }
1826
1827 ZOOM_API(void)
1828     ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1829                                const char *val)
1830 {
1831     if (!strcmp(key, "saveAPDU"))
1832     {
1833         if (val && strcmp(val, "0"))
1834         {
1835             if (!c->saveAPDU_wrbuf)
1836                 c->saveAPDU_wrbuf = wrbuf_alloc();
1837             else
1838                 wrbuf_rewind(c->saveAPDU_wrbuf);
1839         }
1840         else
1841         {
1842             wrbuf_destroy(c->saveAPDU_wrbuf);
1843             c->saveAPDU_wrbuf = 0;
1844         }
1845         ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1846     }
1847     else
1848         ZOOM_options_set(c->options, key, val);
1849 }
1850
1851 ZOOM_API(void)
1852     ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1853                                 const char *val, int len)
1854 {
1855     ZOOM_options_setl(c->options, key, val, len);
1856 }
1857
1858 ZOOM_API(const char *)
1859     ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1860 {
1861     return ZOOM_options_get(r->options, key);
1862 }
1863
1864 ZOOM_API(void)
1865     ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1866                               const char *val)
1867 {
1868     ZOOM_options_set(r->options, key, val);
1869 }
1870
1871
1872 ZOOM_API(int)
1873     ZOOM_connection_errcode(ZOOM_connection c)
1874 {
1875     return ZOOM_connection_error(c, 0, 0);
1876 }
1877
1878 ZOOM_API(const char *)
1879     ZOOM_connection_errmsg(ZOOM_connection c)
1880 {
1881     const char *msg;
1882     ZOOM_connection_error(c, &msg, 0);
1883     return msg;
1884 }
1885
1886 ZOOM_API(const char *)
1887     ZOOM_connection_addinfo(ZOOM_connection c)
1888 {
1889     const char *addinfo;
1890     ZOOM_connection_error(c, 0, &addinfo);
1891     return addinfo;
1892 }
1893
1894 ZOOM_API(const char *)
1895     ZOOM_connection_diagset(ZOOM_connection c)
1896 {
1897     const char *diagset;
1898     ZOOM_connection_error_x(c, 0, 0, &diagset);
1899     return diagset;
1900 }
1901
1902 ZOOM_API(const char *)
1903     ZOOM_diag_str(int error)
1904 {
1905     switch (error)
1906     {
1907     case ZOOM_ERROR_NONE:
1908         return "No error";
1909     case ZOOM_ERROR_CONNECT:
1910         return "Connect failed";
1911     case ZOOM_ERROR_MEMORY:
1912         return "Out of memory";
1913     case ZOOM_ERROR_ENCODE:
1914         return "Encoding failed";
1915     case ZOOM_ERROR_DECODE:
1916         return "Decoding failed";
1917     case ZOOM_ERROR_CONNECTION_LOST:
1918         return "Connection lost";
1919     case ZOOM_ERROR_INIT:
1920         return "Init rejected";
1921     case ZOOM_ERROR_INTERNAL:
1922         return "Internal failure";
1923     case ZOOM_ERROR_TIMEOUT:
1924         return "Timeout";
1925     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1926         return "Unsupported protocol";
1927     case ZOOM_ERROR_UNSUPPORTED_QUERY:
1928         return "Unsupported query type";
1929     case ZOOM_ERROR_INVALID_QUERY:
1930         return "Invalid query";
1931     case ZOOM_ERROR_CQL_PARSE:
1932         return "CQL parsing error";
1933     case ZOOM_ERROR_CQL_TRANSFORM:
1934         return "CQL transformation error";
1935     case ZOOM_ERROR_CCL_CONFIG:
1936         return "CCL configuration error";
1937     case ZOOM_ERROR_CCL_PARSE:
1938         return "CCL parsing error";
1939     case ZOOM_ERROR_ES_INVALID_ACTION:
1940         return "Extended Service. invalid action";
1941     case ZOOM_ERROR_ES_INVALID_VERSION:
1942         return "Extended Service. invalid version";
1943     case ZOOM_ERROR_ES_INVALID_SYNTAX:
1944         return "Extended Service. invalid syntax";
1945     case ZOOM_ERROR_MEMCACHED:
1946         return "Memcached";
1947     default:
1948         return diagbib1_str(error);
1949     }
1950 }
1951
1952 ZOOM_API(int)
1953     ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1954                             const char **addinfo, const char **diagset)
1955 {
1956     int error = c->error;
1957     if (cp)
1958     {
1959         if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1960             *cp = ZOOM_diag_str(error);
1961         else if (!strcmp(c->diagset, "HTTP"))
1962             *cp = z_HTTP_errmsg(c->error);
1963         else if (!strcmp(c->diagset, "Bib-1"))
1964             *cp = ZOOM_diag_str(error);
1965         else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1966             *cp = yaz_diag_srw_str(c->error);
1967         else
1968             *cp = "Unknown error and diagnostic set";
1969     }
1970     if (addinfo)
1971         *addinfo = c->addinfo ? c->addinfo : "";
1972     if (diagset)
1973         *diagset = c->diagset ? c->diagset : "";
1974     return c->error;
1975 }
1976
1977 ZOOM_API(int)
1978     ZOOM_connection_error(ZOOM_connection c, const char **cp,
1979                           const char **addinfo)
1980 {
1981     return ZOOM_connection_error_x(c, cp, addinfo, 0);
1982 }
1983
1984 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1985 {
1986     ZOOM_Event event = 0;
1987     int r = cs_look(c->cs);
1988     yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1989             c, mask, r);
1990
1991     if (r == CS_NONE)
1992     {
1993         event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1994         ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1995         ZOOM_connection_close(c);
1996         ZOOM_connection_put_event(c, event);
1997     }
1998     else if (r == CS_CONNECT)
1999     {
2000         int ret = ret = cs_rcvconnect(c->cs);
2001         yaz_log(c->log_details, "%p ZOOM_connection_do_io "
2002                 "cs_rcvconnect returned %d", c, ret);
2003         if (ret == 1)
2004         {
2005             int mask = ZOOM_SELECT_EXCEPT;
2006             if (c->cs->io_pending & CS_WANT_WRITE)
2007                 mask += ZOOM_SELECT_WRITE;
2008             if (c->cs->io_pending & CS_WANT_READ)
2009                 mask += ZOOM_SELECT_READ;
2010             ZOOM_connection_set_mask(c, mask);
2011             event = ZOOM_Event_create(ZOOM_EVENT_NONE);
2012             ZOOM_connection_put_event(c, event);
2013         }
2014         else if (ret == 0)
2015         {
2016             event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2017             ZOOM_connection_put_event(c, event);
2018             get_cert(c);
2019             if (c->proto == PROTO_Z3950)
2020                 ZOOM_connection_Z3950_send_init(c);
2021             else
2022             {
2023                 /* no init request for SRW .. */
2024                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
2025                 ZOOM_connection_remove_task(c);
2026                 ZOOM_connection_set_mask(c, 0);
2027                 ZOOM_connection_exec_task(c);
2028             }
2029             c->state = STATE_ESTABLISHED;
2030         }
2031         else
2032         {
2033             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2034             ZOOM_connection_close(c);
2035         }
2036     }
2037     else
2038     {
2039         if (mask & ZOOM_SELECT_EXCEPT)
2040         {
2041             if (!ZOOM_test_reconnect(c))
2042             {
2043                 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2044                 ZOOM_connection_close(c);
2045             }
2046             return;
2047         }
2048         if (mask & ZOOM_SELECT_READ)
2049             do_read(c);
2050         if (c->cs && (mask & ZOOM_SELECT_WRITE))
2051             ZOOM_send_buf(c);
2052     }
2053 }
2054
2055 ZOOM_API(int)
2056     ZOOM_connection_last_event(ZOOM_connection cs)
2057 {
2058     if (!cs)
2059         return ZOOM_EVENT_NONE;
2060     return cs->last_event;
2061 }
2062
2063
2064 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2065 {
2066     if (c->mask)
2067     {
2068         ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2069         /* timeout and this connection was waiting */
2070         ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2071         ZOOM_connection_close(c);
2072         ZOOM_connection_put_event(c, event);
2073     }
2074     return 0;
2075 }
2076
2077 ZOOM_API(int)
2078     ZOOM_connection_process(ZOOM_connection c)
2079 {
2080     ZOOM_Event event;
2081     if (!c)
2082         return 0;
2083
2084     event = ZOOM_connection_get_event(c);
2085     if (event)
2086     {
2087         ZOOM_Event_destroy(event);
2088         return 1;
2089     }
2090     ZOOM_connection_exec_task(c);
2091     event = ZOOM_connection_get_event(c);
2092     if (event)
2093     {
2094         ZOOM_Event_destroy(event);
2095         return 1;
2096     }
2097     return 0;
2098 }
2099
2100 ZOOM_API(int)
2101     ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2102 {
2103     int i;
2104
2105     yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2106
2107     for (i = 0; i<no; i++)
2108     {
2109         ZOOM_connection c = cs[i];
2110
2111         if (c && ZOOM_connection_process(c))
2112             return i+1;
2113     }
2114     return 0;
2115 }
2116
2117 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2118 {
2119     if (c->mask && mask)
2120         ZOOM_connection_do_io(c, mask);
2121     return 0;
2122 }
2123
2124 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2125 {
2126     if (c->cs)
2127         return cs_fileno(c->cs);
2128     return -1;
2129 }
2130
2131 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2132 {
2133     c->mask = mask;
2134     if (!c->cs)
2135         return -1;
2136     return 0;
2137 }
2138
2139 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2140 {
2141     if (c->cs)
2142         return c->mask;
2143     return 0;
2144 }
2145
2146 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2147 {
2148     return ZOOM_options_get_int(c->options, "timeout", 30);
2149 }
2150
2151 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2152 {
2153     if (c->cs)
2154         cs_close(c->cs);
2155     c->cs = 0;
2156     ZOOM_connection_set_mask(c, 0);
2157     c->state = STATE_IDLE;
2158 }
2159
2160 /*
2161  * Local variables:
2162  * c-basic-offset: 4
2163  * c-file-style: "Stroustrup"
2164  * indent-tabs-mode: nil
2165  * End:
2166  * vim: shiftwidth=4 tabstop=8 expandtab
2167  */
2168