Remove member soap_handler from statserv_options_block
[yaz-moved-to-github.git] / src / zoom-c.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 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_RETRIEVE:
140         yaz_log(YLOG_LOG, "retrieve p=%p", task);
141         break;
142     case ZOOM_TASK_CONNECT:
143         yaz_log(YLOG_LOG, "connect p=%p", task);
144         break;
145     case ZOOM_TASK_SCAN:
146         yaz_log(YLOG_LOG, "scan p=%p", task);
147         break;
148     }
149 }
150
151 void ZOOM_connection_show_tasks(ZOOM_connection c)
152 {
153     ZOOM_task task;
154     yaz_log(YLOG_LOG, "connection p=%p tasks", c);
155     for (task = c->tasks; task; task = task->next)
156         ZOOM_connection_show_task(task);
157 }
158
159 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
160 {
161     ZOOM_task *taskp = &c->tasks;
162     while (*taskp)
163         taskp = &(*taskp)->next;
164     *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
165     (*taskp)->running = 0;
166     (*taskp)->which = which;
167     (*taskp)->next = 0;
168     clear_error(c);
169     return *taskp;
170 }
171
172 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
173 {
174     return c->tasks ? 0 : 1;
175 }
176
177 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
178 {
179     ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
180
181     task->next = c->tasks;
182     c->tasks = task;
183
184     task->running = 0;
185     task->which = which;
186     return task;
187 }
188
189 void ZOOM_connection_remove_task(ZOOM_connection c)
190 {
191     ZOOM_task task = c->tasks;
192
193     if (task)
194     {
195         c->tasks = task->next;
196         switch (task->which)
197         {
198         case ZOOM_TASK_SEARCH:
199             resultset_destroy(task->u.search.resultset);
200             xfree(task->u.search.syntax);
201             xfree(task->u.search.elementSetName);
202             xfree(task->u.search.schema);
203             break;
204         case ZOOM_TASK_RETRIEVE:
205             resultset_destroy(task->u.retrieve.resultset);
206             xfree(task->u.retrieve.syntax);
207             xfree(task->u.retrieve.elementSetName);
208             xfree(task->u.retrieve.schema);
209             break;
210         case ZOOM_TASK_CONNECT:
211             break;
212         case ZOOM_TASK_SCAN:
213             ZOOM_scanset_destroy(task->u.scan.scan);
214             break;
215         case ZOOM_TASK_PACKAGE:
216             ZOOM_package_destroy(task->u.package);
217             break;
218         case ZOOM_TASK_SORT:
219             resultset_destroy(task->u.sort.resultset);
220             ZOOM_query_destroy(task->u.sort.q);
221             break;
222         default:
223             assert(0);
224         }
225         xfree(task);
226
227         if (!c->tasks)
228         {
229             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
230             ZOOM_connection_put_event(c, event);
231         }
232     }
233 }
234
235 void ZOOM_connection_remove_tasks(ZOOM_connection c)
236 {
237     while (c->tasks)
238         ZOOM_connection_remove_task(c);
239 }
240
241 static void odr_wrbuf_write(ODR o, void *handle, int type,
242                             const char *buf, int len)
243 {
244     WRBUF w = (WRBUF) handle;
245     wrbuf_write(w, buf, len);
246 }
247
248 ZOOM_API(ZOOM_connection)
249     ZOOM_connection_create(ZOOM_options options)
250 {
251     ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
252
253     initlog();
254
255     c->log_api = log_api0;
256     c->log_details = log_details0;
257
258     yaz_log(c->log_api, "%p ZOOM_connection_create", c);
259
260     c->proto = PROTO_Z3950;
261     c->cs = 0;
262     ZOOM_connection_set_mask(c, 0);
263     c->reconnect_ok = 0;
264     c->state = STATE_IDLE;
265     c->addinfo = 0;
266     c->diagset = 0;
267     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
268     c->buf_in = 0;
269     c->len_in = 0;
270     c->buf_out = 0;
271     c->len_out = 0;
272     c->resultsets = 0;
273
274     c->options = ZOOM_options_create_with_parent(options);
275
276     c->host_port = 0;
277     c->proxy = 0;
278     c->tproxy = 0;
279
280     c->charset = c->lang = 0;
281
282     c->cookie_out = 0;
283     c->cookie_in = 0;
284     c->client_IP = 0;
285     c->tasks = 0;
286
287     c->user = 0;
288     c->group = 0;
289     c->password = 0;
290     c->url_authentication = 0;
291
292     c->maximum_record_size = 0;
293     c->preferred_message_size = 0;
294
295     c->odr_in = odr_createmem(ODR_DECODE);
296     c->odr_out = odr_createmem(ODR_ENCODE);
297     c->odr_print = 0;
298     c->odr_save = 0;
299
300     c->async = 0;
301     c->support_named_resultsets = 0;
302     c->last_event = ZOOM_EVENT_NONE;
303
304     c->m_queue_front = 0;
305     c->m_queue_back = 0;
306
307     c->sru_version = 0;
308     c->no_redirects = 0;
309     c->saveAPDU_wrbuf = 0;
310     return c;
311 }
312
313 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
314 {
315     if (c->odr_save)
316     {
317         odr_destroy(c->odr_save);
318         c->odr_save = 0;
319     }
320     if (w)
321     {
322         c->odr_save = odr_createmem(ODR_PRINT);
323         odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
324     }
325 }
326
327 /* set database names. Take local databases (if set); otherwise
328    take databases given in ZURL (if set); otherwise use Default */
329 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
330                                      int *num, ODR odr)
331 {
332     char **databaseNames;
333     const char *cp = ZOOM_options_get(options, "databaseName");
334
335     if ((!cp || !*cp) && con->host_port)
336         cs_get_host_args(con->host_port, &cp);
337     if (!cp || !*cp)
338         cp = "Default";
339     nmem_strsplit(odr_getmem(odr), "+", cp,  &databaseNames, num);
340     return databaseNames;
341 }
342
343 ZOOM_API(ZOOM_connection)
344     ZOOM_connection_new(const char *host, int portnum)
345 {
346     ZOOM_connection c = ZOOM_connection_create(0);
347
348     ZOOM_connection_connect(c, host, portnum);
349     return c;
350 }
351
352 static zoom_sru_mode get_sru_mode_from_string(const char *s)
353 {
354     if (!s || !*s)
355         return zoom_sru_soap;
356     if (!yaz_matchstr(s, "soap"))
357         return zoom_sru_soap;
358     else if (!yaz_matchstr(s, "get"))
359         return zoom_sru_get;
360     else if (!yaz_matchstr(s, "post"))
361         return zoom_sru_post;
362     else if (!yaz_matchstr(s, "solr"))
363         return zoom_sru_solr;
364     return zoom_sru_error;
365 }
366
367 ZOOM_API(void)
368     ZOOM_connection_connect(ZOOM_connection c,
369                             const char *host, int portnum)
370 {
371     const char *val;
372
373     initlog();
374
375     yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
376             c, host ? host : "null", portnum);
377
378     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
379     ZOOM_connection_remove_tasks(c);
380
381     if (c->odr_print)
382     {
383         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
384         odr_destroy(c->odr_print);
385     }
386     if (ZOOM_options_get_bool(c->options, "apdulog", 0))
387     {
388         c->odr_print = odr_createmem(ODR_PRINT);
389         odr_setprint(c->odr_print, yaz_log_file());
390     }
391     else
392         c->odr_print = 0;
393
394     if (c->cs)
395     {
396         yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
397         c->reconnect_ok = 1;
398         return;
399     }
400     yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
401     xfree(c->proxy);
402     c->proxy = 0;
403     val = ZOOM_options_get(c->options, "proxy");
404     if (val && *val)
405     {
406         yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
407         c->proxy = xstrdup(val);
408     }
409
410     xfree(c->tproxy);
411     c->tproxy = 0;
412     val = ZOOM_options_get(c->options, "tproxy");
413     if (val && *val)
414     {
415         yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
416         c->tproxy = xstrdup(val);
417     }
418
419     xfree(c->charset);
420     c->charset = 0;
421     val = ZOOM_options_get(c->options, "charset");
422     if (val && *val)
423     {
424         yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
425         c->charset = xstrdup(val);
426     }
427
428     xfree(c->lang);
429     val = ZOOM_options_get(c->options, "lang");
430     if (val && *val)
431     {
432         yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
433         c->lang = xstrdup(val);
434     }
435     else
436         c->lang = 0;
437
438     if (host)
439     {
440         xfree(c->host_port);
441         if (portnum)
442         {
443             char hostn[128];
444             sprintf(hostn, "%.80s:%d", host, portnum);
445             c->host_port = xstrdup(hostn);
446         }
447         else
448             c->host_port = xstrdup(host);
449     }
450
451     {
452         /*
453          * If the "<scheme>:" part of the host string is preceded by one
454          * or more comma-separated <name>=<value> pairs, these are taken
455          * to be options to be set on the connection object.  Among other
456          * applications, this facility can be used to embed authentication
457          * in a host string:
458          *          user=admin,password=secret,tcp:localhost:9999
459          */
460         char *remainder = c->host_port;
461         char *pcolon = strchr(remainder, ':');
462         char *pcomma;
463         char *pequals;
464         while ((pcomma = strchr(remainder, ',')) != 0 &&
465                (pcolon == 0 || pcomma < pcolon))
466         {
467             *pcomma = '\0';
468             if ((pequals = strchr(remainder, '=')) != 0)
469             {
470                 *pequals = '\0';
471                 ZOOM_connection_option_set(c, remainder, pequals+1);
472             }
473             remainder = pcomma+1;
474         }
475
476         if (remainder != c->host_port)
477         {
478             remainder = xstrdup(remainder);
479             xfree(c->host_port);
480             c->host_port = remainder;
481         }
482     }
483
484     val = ZOOM_options_get(c->options, "sru");
485     c->sru_mode = get_sru_mode_from_string(val);
486
487     xfree(c->sru_version);
488     val = ZOOM_options_get(c->options, "sru_version");
489     c->sru_version = xstrdup(val ? val : "1.2");
490
491     ZOOM_options_set(c->options, "host", c->host_port);
492
493     xfree(c->cookie_out);
494     c->cookie_out = 0;
495     val = ZOOM_options_get(c->options, "cookie");
496     if (val && *val)
497     {
498         yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
499         c->cookie_out = xstrdup(val);
500     }
501
502     xfree(c->client_IP);
503     c->client_IP = 0;
504     val = ZOOM_options_get(c->options, "clientIP");
505     if (val && *val)
506     {
507         yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
508                 c, val);
509         c->client_IP = xstrdup(val);
510     }
511
512     xfree(c->group);
513     c->group = 0;
514     val = ZOOM_options_get(c->options, "group");
515     if (val && *val)
516         c->group = xstrdup(val);
517
518     xfree(c->user);
519     c->user = 0;
520     val = ZOOM_options_get(c->options, "user");
521     if (val && *val)
522         c->user = xstrdup(val);
523
524     xfree(c->password);
525     c->password = 0;
526     val = ZOOM_options_get(c->options, "password");
527     if (!val)
528         val = ZOOM_options_get(c->options, "pass");
529     if (val && *val)
530         c->password = xstrdup(val);
531
532     val = ZOOM_options_get(c->options, "authenticationMode");
533     if (val && !strcmp(val, "url"))
534         c->url_authentication = 1;
535     else
536         c->url_authentication = 0;
537
538     c->maximum_record_size =
539         ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
540     c->preferred_message_size =
541         ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
542
543     c->async = ZOOM_options_get_bool(c->options, "async", 0);
544
545     if (c->sru_mode == zoom_sru_error)
546     {
547         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
548         ZOOM_connection_remove_tasks(c);
549         return;
550     }
551
552     yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
553     ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
554
555     if (!c->async)
556     {
557         while (ZOOM_event(1, &c))
558             ;
559     }
560 }
561
562 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
563 {
564 #if ZOOM_RESULT_LISTS
565 #else
566     if (r->connection)
567     {
568         /* remove ourselves from the resultsets in connection */
569         ZOOM_resultset *rp = &r->connection->resultsets;
570         while (1)
571         {
572             assert(*rp);   /* we must be in this list!! */
573             if (*rp == r)
574             {   /* OK, we're here - take us out of it */
575                 *rp = (*rp)->next;
576                 break;
577             }
578             rp = &(*rp)->next;
579         }
580         r->connection = 0;
581     }
582 #endif
583 }
584
585 ZOOM_API(void)
586     ZOOM_connection_destroy(ZOOM_connection c)
587 {
588 #if ZOOM_RESULT_LISTS
589     ZOOM_resultsets list;
590 #else
591     ZOOM_resultset r;
592 #endif
593     if (!c)
594         return;
595     yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
596     if (c->cs)
597         cs_close(c->cs);
598
599 #if ZOOM_RESULT_LISTS
600     /* Remove the connection's usage of resultsets */
601     list = c->resultsets;
602     while (list) {
603         ZOOM_resultsets removed = list;
604         ZOOM_resultset_destroy(list->resultset);
605         list = list->next;
606         xfree(removed);
607     }
608 #else
609     for (r = c->resultsets; r; r = r->next)
610         r->connection = 0;
611 #endif
612
613     xfree(c->buf_in);
614     xfree(c->addinfo);
615     xfree(c->diagset);
616     odr_destroy(c->odr_in);
617     odr_destroy(c->odr_out);
618     if (c->odr_save)
619         odr_destroy(c->odr_save);
620     if (c->odr_print)
621     {
622         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
623         odr_destroy(c->odr_print);
624     }
625     ZOOM_options_destroy(c->options);
626     ZOOM_connection_remove_tasks(c);
627     ZOOM_connection_remove_events(c);
628     xfree(c->host_port);
629     xfree(c->proxy);
630     xfree(c->tproxy);
631     xfree(c->charset);
632     xfree(c->lang);
633     xfree(c->cookie_out);
634     xfree(c->cookie_in);
635     xfree(c->client_IP);
636     xfree(c->user);
637     xfree(c->group);
638     xfree(c->password);
639     xfree(c->sru_version);
640     wrbuf_destroy(c->saveAPDU_wrbuf);
641     xfree(c);
642 }
643
644 void ZOOM_resultset_addref(ZOOM_resultset r)
645 {
646     if (r)
647     {
648         yaz_mutex_enter(r->mutex);
649         (r->refcount)++;
650         yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
651                 r, r->refcount);
652         yaz_mutex_leave(r->mutex);
653     }
654 }
655
656 static int g_resultsets = 0;
657 static YAZ_MUTEX g_resultset_mutex = 0;
658
659 /* TODO We need to initialize this before running threaded:
660  * call resultset_use(0)  */
661
662 static int resultset_use(int delta) {
663     int resultset_count;
664     if (g_resultset_mutex == 0)
665         yaz_mutex_create(&g_resultset_mutex);
666     yaz_mutex_enter(g_resultset_mutex);
667     g_resultsets += delta;
668     resultset_count = g_resultsets;
669     yaz_mutex_leave(g_resultset_mutex);
670     return resultset_count;
671 }
672
673 int resultsets_count(void) {
674     return resultset_use(0);
675 }
676
677 ZOOM_resultset ZOOM_resultset_create(void)
678 {
679     int i;
680     ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
681
682     initlog();
683
684     yaz_log(log_details0, "%p ZOOM_resultset_create", r);
685     r->refcount = 1;
686     r->size = 0;
687     r->odr = odr_createmem(ODR_ENCODE);
688     r->piggyback = 1;
689     r->setname = 0;
690     r->step = 0;
691     for (i = 0; i<RECORD_HASH_SIZE; i++)
692         r->record_hash[i] = 0;
693     r->r_sort_spec = 0;
694     r->query = 0;
695     r->connection = 0;
696     r->databaseNames = 0;
697     r->num_databaseNames = 0;
698     r->facets = 0;
699     r->num_facets = 0;
700     r->facets_names = 0;
701     r->mutex = 0;
702     yaz_mutex_create(&r->mutex);
703 #if SHPTR
704     {
705         WRBUF w = wrbuf_alloc();
706         YAZ_SHPTR_INIT(r->record_wrbuf, w);
707     }
708 #endif
709     resultset_use(1);
710     return r;
711 }
712
713 ZOOM_API(ZOOM_resultset)
714     ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
715 {
716     ZOOM_resultset r;
717     ZOOM_query s = ZOOM_query_create();
718
719     ZOOM_query_prefix(s, q);
720
721     r = ZOOM_connection_search(c, s);
722     ZOOM_query_destroy(s);
723     return r;
724 }
725
726 ZOOM_API(ZOOM_resultset)
727     ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
728 {
729     ZOOM_resultset r = ZOOM_resultset_create();
730     ZOOM_task task;
731     const char *cp;
732     int start, count;
733     const char *syntax, *elementSetName, *schema;
734 #if ZOOM_RESULT_LISTS
735     ZOOM_resultsets set;
736 #endif
737
738     yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
739     r->r_sort_spec = ZOOM_query_get_sortspec(q);
740     r->query = q;
741
742     r->options = ZOOM_options_create_with_parent(c->options);
743
744     start = ZOOM_options_get_int(r->options, "start", 0);
745     count = ZOOM_options_get_int(r->options, "count", 0);
746     {
747         /* If "presentChunk" is defined use that; otherwise "step" */
748         const char *cp = ZOOM_options_get(r->options, "presentChunk");
749         r->step = ZOOM_options_get_int(r->options,
750                                        (cp != 0 ? "presentChunk": "step"), 0);
751     }
752     r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
753     cp = ZOOM_options_get(r->options, "setname");
754     if (cp)
755         r->setname = xstrdup(cp);
756
757     r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
758                                          r->odr);
759
760     r->connection = c;
761
762 #if ZOOM_RESULT_LISTS
763     yaz_log(log_details, "%p ZOOM_connection_search: Adding new resultset (%p) to resultsets (%p) ", c, r, c->resultsets);
764     set = xmalloc(sizeof(*set));
765     ZOOM_resultset_addref(r);
766     set->resultset = r;
767     set->next = c->resultsets;
768     c->resultsets = set;
769 #else
770     r->next = c->resultsets;
771     c->resultsets = r;
772 #endif
773     if (c->host_port && c->proto == PROTO_HTTP)
774     {
775         if (!c->cs)
776         {
777             yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
778             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
779         }
780         else
781         {
782             yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
783             c->reconnect_ok = 1;
784         }
785     }
786
787     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
788     task->u.search.resultset = r;
789     task->u.search.start = start;
790     task->u.search.count = count;
791     task->u.search.recv_search_fired = 0;
792
793     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
794     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
795     elementSetName = ZOOM_options_get(r->options, "elementSetName");
796     task->u.search.elementSetName = elementSetName ?
797         xstrdup(elementSetName) : 0;
798     schema = ZOOM_options_get(r->options, "schema");
799     task->u.search.schema = schema ? xstrdup(schema) : 0;
800
801     ZOOM_resultset_addref(r);
802
803     ZOOM_query_addref(q);
804
805     if (!c->async)
806     {
807         while (ZOOM_event(1, &c))
808             ;
809     }
810     return r;
811 }
812
813 ZOOM_API(void)
814     ZOOM_resultset_sort(ZOOM_resultset r,
815                          const char *sort_type, const char *sort_spec)
816 {
817     (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
818 }
819
820 ZOOM_API(int)
821     ZOOM_resultset_sort1(ZOOM_resultset r,
822                          const char *sort_type, const char *sort_spec)
823 {
824     ZOOM_connection c = r->connection;
825     ZOOM_task task;
826     ZOOM_query newq;
827
828     newq = ZOOM_query_create();
829     if (ZOOM_query_sortby(newq, sort_spec) < 0)
830         return -1;
831
832     yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
833             r, r, sort_type, sort_spec);
834     if (!c)
835         return 0;
836
837     if (c->host_port && c->proto == PROTO_HTTP)
838     {
839         if (!c->cs)
840         {
841             yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
842             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
843         }
844         else
845         {
846             yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
847                     r);
848             c->reconnect_ok = 1;
849         }
850     }
851
852     ZOOM_resultset_cache_reset(r);
853     task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
854     task->u.sort.resultset = r;
855     task->u.sort.q = newq;
856
857     ZOOM_resultset_addref(r);
858
859     if (!c->async)
860     {
861         while (ZOOM_event(1, &c))
862             ;
863     }
864
865     return 0;
866 }
867
868 ZOOM_API(void)
869     ZOOM_resultset_destroy(ZOOM_resultset r)
870 {
871     resultset_destroy(r);
872 }
873
874 static void resultset_destroy(ZOOM_resultset r)
875 {
876     if (!r)
877         return;
878     yaz_mutex_enter(r->mutex);
879     (r->refcount)--;
880     yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
881             r, r, r->refcount);
882     if (r->refcount == 0)
883     {
884         yaz_mutex_leave(r->mutex);
885
886         yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
887         ZOOM_resultset_cache_reset(r);
888         ZOOM_resultset_release(r);
889         ZOOM_query_destroy(r->query);
890         ZOOM_options_destroy(r->options);
891         odr_destroy(r->odr);
892         xfree(r->setname);
893         yaz_mutex_destroy(&r->mutex);
894 #if SHPTR
895         YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
896 #endif
897         resultset_use(-1);
898         xfree(r);
899     }
900     else
901         yaz_mutex_leave(r->mutex);
902 }
903
904 ZOOM_API(size_t)
905     ZOOM_resultset_size(ZOOM_resultset r)
906 {
907     return r->size;
908 }
909
910 int ZOOM_test_reconnect(ZOOM_connection c)
911 {
912     ZOOM_Event event;
913
914     if (!c->reconnect_ok)
915         return 0;
916     ZOOM_connection_close(c);
917     c->reconnect_ok = 0;
918     c->tasks->running = 0;
919     ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
920
921     event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
922     ZOOM_connection_put_event(c, event);
923
924     return 1;
925 }
926
927 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
928                                     int force_sync, int start, int count)
929 {
930     ZOOM_task task;
931     ZOOM_connection c;
932     const char *cp;
933     const char *syntax, *elementSetName;
934
935     if (!r)
936         return;
937     yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
938             " count=%d", r, force_sync, start, count);
939     c = r->connection;
940     if (!c)
941         return;
942
943     if (c->host_port && c->proto == PROTO_HTTP)
944     {
945         if (!c->cs)
946         {
947             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
948             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
949         }
950         else
951         {
952             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
953                     "reconnect", r);
954             c->reconnect_ok = 1;
955         }
956     }
957     task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
958     task->u.retrieve.resultset = r;
959     task->u.retrieve.start = start;
960     task->u.retrieve.count = count;
961
962     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
963     task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
964     elementSetName = ZOOM_options_get(r->options, "elementSetName");
965     task->u.retrieve.elementSetName = elementSetName
966         ? xstrdup(elementSetName) : 0;
967
968     cp = ZOOM_options_get(r->options, "schema");
969     task->u.retrieve.schema = cp ? xstrdup(cp) : 0;
970
971     ZOOM_resultset_addref(r);
972
973     if (!r->connection->async || force_sync)
974         while (r->connection && ZOOM_event(1, &r->connection))
975             ;
976 }
977
978 ZOOM_API(void)
979     ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
980                            size_t start, size_t count)
981 {
982     int force_present = 0;
983
984     if (!r)
985         return ;
986     yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
987             r, r, (long) start, (long) count);
988     if (count && recs)
989         force_present = 1;
990     ZOOM_resultset_retrieve(r, force_present, start, count);
991     if (force_present)
992     {
993         size_t i;
994         for (i = 0; i< count; i++)
995             recs[i] = ZOOM_resultset_record_immediate(r, i+start);
996     }
997 }
998
999 ZOOM_API(size_t)
1000     ZOOM_resultset_facets_size(ZOOM_resultset r) {
1001     return r->num_facets;
1002 }
1003
1004 ZOOM_API(ZOOM_facet_field)
1005     ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
1006     int num = r->num_facets;
1007     ZOOM_facet_field *facets = r->facets;
1008     int index;
1009     for (index = 0; index < num; index++) {
1010         if (!strcmp(facets[index]->facet_name, name)) {
1011             return facets[index];
1012         }
1013     }
1014     return 0;
1015 }
1016
1017 ZOOM_API(ZOOM_facet_field)
1018     ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
1019     int num = r->num_facets;
1020     ZOOM_facet_field *facets = r->facets;
1021     if (index >= 0 && index < num) {
1022         return facets[index];
1023     }
1024     return 0;
1025 }
1026
1027 ZOOM_API(ZOOM_facet_field *)
1028     ZOOM_resultset_facets(ZOOM_resultset r)
1029 {
1030     return r->facets;
1031 }
1032
1033 ZOOM_API(const char**)
1034     ZOOM_resultset_facets_names(ZOOM_resultset r)
1035 {
1036     return (const char **) r->facets_names;
1037 }
1038
1039 ZOOM_API(const char*)
1040     ZOOM_facet_field_name(ZOOM_facet_field field)
1041 {
1042     return field->facet_name;
1043 }
1044
1045 ZOOM_API(size_t)
1046     ZOOM_facet_field_term_count(ZOOM_facet_field field)
1047 {
1048     return field->num_terms;
1049 }
1050
1051 ZOOM_API(const char*)
1052     ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1053     *freq = field->facet_terms[idx].frequency;
1054     return field->facet_terms[idx].term;
1055 }
1056
1057
1058 static void get_cert(ZOOM_connection c)
1059 {
1060     char *cert_buf;
1061     int cert_len;
1062
1063     if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1064     {
1065         ZOOM_connection_option_setl(c, "sslPeerCert",
1066                                     cert_buf, cert_len);
1067         xfree(cert_buf);
1068     }
1069 }
1070
1071 static zoom_ret do_connect_host(ZOOM_connection c,
1072                                 const char *logical_url);
1073
1074 static zoom_ret do_connect(ZOOM_connection c)
1075 {
1076     return do_connect_host(c, c->host_port);
1077 }
1078
1079 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1080 {
1081     void *add;
1082
1083     if (c->cs)
1084         cs_close(c->cs);
1085     c->cs = cs_create_host_proxy(logical_url, 0, &add,
1086                                  c->tproxy ? c->tproxy : c->proxy);
1087
1088     if (c->cs && c->cs->protocol == PROTO_HTTP)
1089     {
1090 #if YAZ_HAVE_XML2
1091         c->proto = PROTO_HTTP;
1092 #else
1093         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1094         ZOOM_connection_close(c);
1095         return zoom_complete;
1096 #endif
1097     }
1098     if (c->cs)
1099     {
1100         int ret = cs_connect(c->cs, add);
1101         if (ret == 0)
1102         {
1103             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1104             ZOOM_connection_put_event(c, event);
1105             get_cert(c);
1106             if (c->proto == PROTO_Z3950)
1107                 ZOOM_connection_Z3950_send_init(c);
1108             else
1109             {
1110                 /* no init request for SRW .. */
1111                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1112                 ZOOM_connection_remove_task(c);
1113                 ZOOM_connection_set_mask(c, 0);
1114                 ZOOM_connection_exec_task(c);
1115             }
1116             c->state = STATE_ESTABLISHED;
1117             return zoom_pending;
1118         }
1119         else if (ret > 0)
1120         {
1121             int mask = ZOOM_SELECT_EXCEPT;
1122             if (c->cs->io_pending & CS_WANT_WRITE)
1123                 mask += ZOOM_SELECT_WRITE;
1124             if (c->cs->io_pending & CS_WANT_READ)
1125                 mask += ZOOM_SELECT_READ;
1126             ZOOM_connection_set_mask(c, mask);
1127             c->state = STATE_CONNECTING;
1128             return zoom_pending;
1129         }
1130     }
1131     c->state = STATE_IDLE;
1132     ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1133     return zoom_complete;
1134 }
1135
1136 /* returns 1 if PDU was sent OK (still pending )
1137    0 if PDU was not sent OK (nothing to wait for)
1138 */
1139
1140 ZOOM_API(ZOOM_record)
1141     ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1142 {
1143     const char *syntax =
1144         ZOOM_options_get(s->options, "preferredRecordSyntax");
1145     const char *elementSetName =
1146         ZOOM_options_get(s->options, "elementSetName");
1147     const char *schema =
1148         ZOOM_options_get(s->options, "schema");
1149
1150     return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
1151 }
1152
1153 ZOOM_API(ZOOM_record)
1154     ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1155 {
1156     ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1157
1158     if (!rec)
1159     {
1160         /*
1161          * MIKE: I think force_sync should always be zero, but I don't
1162          * want to make this change until I get the go-ahead from
1163          * Adam, in case something depends on the old synchronous
1164          * behaviour.
1165          */
1166         int force_sync = 1;
1167         if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1168         ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1169         rec = ZOOM_resultset_record_immediate(r, pos);
1170     }
1171     return rec;
1172 }
1173
1174 ZOOM_API(ZOOM_scanset)
1175     ZOOM_connection_scan(ZOOM_connection c, const char *start)
1176 {
1177     ZOOM_scanset s;
1178     ZOOM_query q = ZOOM_query_create();
1179
1180     ZOOM_query_prefix(q, start);
1181
1182     s = ZOOM_connection_scan1(c, q);
1183     ZOOM_query_destroy(q);
1184     return s;
1185
1186 }
1187
1188 ZOOM_API(ZOOM_scanset)
1189     ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1190 {
1191     ZOOM_scanset scan = 0;
1192     Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1193
1194     if (!z_query)
1195         return 0;
1196     scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1197     scan->connection = c;
1198     scan->odr = odr_createmem(ODR_DECODE);
1199     scan->options = ZOOM_options_create_with_parent(c->options);
1200     scan->refcount = 1;
1201     scan->scan_response = 0;
1202     scan->srw_scan_response = 0;
1203
1204     scan->query = q;
1205     ZOOM_query_addref(q);
1206     scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1207                                             &scan->num_databaseNames,
1208                                             scan->odr);
1209
1210     if (1)
1211     {
1212         ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1213         task->u.scan.scan = scan;
1214
1215         (scan->refcount)++;
1216         if (!c->async)
1217         {
1218             while (ZOOM_event(1, &c))
1219                 ;
1220         }
1221     }
1222     return scan;
1223 }
1224
1225 ZOOM_API(void)
1226     ZOOM_scanset_destroy(ZOOM_scanset scan)
1227 {
1228     if (!scan)
1229         return;
1230     (scan->refcount)--;
1231     if (scan->refcount == 0)
1232     {
1233         ZOOM_query_destroy(scan->query);
1234
1235         odr_destroy(scan->odr);
1236
1237         ZOOM_options_destroy(scan->options);
1238         xfree(scan);
1239     }
1240 }
1241
1242 static zoom_ret send_package(ZOOM_connection c)
1243 {
1244     ZOOM_Event event;
1245
1246     yaz_log(c->log_details, "%p send_package", c);
1247     if (!c->tasks)
1248         return zoom_complete;
1249     assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1250
1251     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1252     ZOOM_connection_put_event(c, event);
1253
1254     c->buf_out = c->tasks->u.package->buf_out;
1255     c->len_out = c->tasks->u.package->len_out;
1256
1257     return ZOOM_send_buf(c);
1258 }
1259
1260 ZOOM_API(size_t)
1261     ZOOM_scanset_size(ZOOM_scanset scan)
1262 {
1263     if (!scan)
1264         return 0;
1265
1266     if (scan->scan_response && scan->scan_response->entries)
1267         return scan->scan_response->entries->num_entries;
1268     else if (scan->srw_scan_response)
1269         return scan->srw_scan_response->num_terms;
1270     return 0;
1271 }
1272
1273 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1274                                 size_t *occ,
1275                                 const char **value_term, size_t *value_len,
1276                                 const char **disp_term, size_t *disp_len)
1277 {
1278     size_t noent = ZOOM_scanset_size(scan);
1279
1280     *value_term = 0;
1281     *value_len = 0;
1282
1283     *disp_term = 0;
1284     *disp_len = 0;
1285
1286     *occ = 0;
1287     if (pos >= noent)
1288         return;
1289     if (scan->scan_response)
1290     {
1291         Z_ScanResponse *res = scan->scan_response;
1292         if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1293         {
1294             Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1295
1296             *value_term = (const char *) t->term->u.general->buf;
1297             *value_len = t->term->u.general->len;
1298             if (t->displayTerm)
1299             {
1300                 *disp_term = t->displayTerm;
1301                 *disp_len = strlen(*disp_term);
1302             }
1303             else if (t->term->which == Z_Term_general)
1304             {
1305                 *disp_term = (const char *) t->term->u.general->buf;
1306                 *disp_len = t->term->u.general->len;
1307             }
1308             *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1309         }
1310     }
1311     if (scan->srw_scan_response)
1312     {
1313         Z_SRW_scanResponse *res = scan->srw_scan_response;
1314         Z_SRW_scanTerm *t = res->terms + pos;
1315         if (t)
1316         {
1317             *value_term = t->value;
1318             *value_len = strlen(*value_term);
1319
1320             if (t->displayTerm)
1321                 *disp_term = t->displayTerm;
1322             else
1323                 *disp_term = t->value;
1324             *disp_len = strlen(*disp_term);
1325             *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1326         }
1327     }
1328 }
1329
1330 ZOOM_API(const char *)
1331     ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1332                       size_t *occ, size_t *len)
1333 {
1334     const char *value_term = 0;
1335     size_t value_len = 0;
1336     const char *disp_term = 0;
1337     size_t disp_len = 0;
1338
1339     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1340                         &disp_term, &disp_len);
1341
1342     *len = value_len;
1343     return value_term;
1344 }
1345
1346 ZOOM_API(const char *)
1347     ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1348                               size_t *occ, size_t *len)
1349 {
1350     const char *value_term = 0;
1351     size_t value_len = 0;
1352     const char *disp_term = 0;
1353     size_t disp_len = 0;
1354
1355     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1356                         &disp_term, &disp_len);
1357
1358     *len = disp_len;
1359     return disp_term;
1360 }
1361
1362 ZOOM_API(const char *)
1363     ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1364 {
1365     return ZOOM_options_get(scan->options, key);
1366 }
1367
1368 ZOOM_API(void)
1369     ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1370                             const char *val)
1371 {
1372     ZOOM_options_set(scan->options, key, val);
1373 }
1374
1375
1376 ZOOM_API(ZOOM_package)
1377     ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1378 {
1379     ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1380
1381     p->connection = c;
1382     p->odr_out = odr_createmem(ODR_ENCODE);
1383     p->options = ZOOM_options_create_with_parent2(options, c->options);
1384     p->refcount = 1;
1385     p->buf_out = 0;
1386     p->len_out = 0;
1387     return p;
1388 }
1389
1390 ZOOM_API(void)
1391     ZOOM_package_destroy(ZOOM_package p)
1392 {
1393     if (!p)
1394         return;
1395     (p->refcount)--;
1396     if (p->refcount == 0)
1397     {
1398         odr_destroy(p->odr_out);
1399         xfree(p->buf_out);
1400
1401         ZOOM_options_destroy(p->options);
1402         xfree(p);
1403     }
1404 }
1405
1406 ZOOM_API(const char *)
1407     ZOOM_package_option_get(ZOOM_package p, const char *key)
1408 {
1409     return ZOOM_options_get(p->options, key);
1410 }
1411
1412 ZOOM_API(const char *)
1413     ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1414 {
1415     return ZOOM_options_getl(p->options, key, lenp);
1416 }
1417
1418 ZOOM_API(void)
1419     ZOOM_package_option_set(ZOOM_package p, const char *key,
1420                             const char *val)
1421 {
1422     ZOOM_options_set(p->options, key, val);
1423 }
1424
1425 ZOOM_API(void)
1426     ZOOM_package_option_setl(ZOOM_package p, const char *key,
1427                              const char *val, int len)
1428 {
1429     ZOOM_options_setl(p->options, key, val, len);
1430 }
1431
1432 ZOOM_API(int)
1433     ZOOM_connection_exec_task(ZOOM_connection c)
1434 {
1435     ZOOM_task task = c->tasks;
1436     zoom_ret ret = zoom_complete;
1437
1438     if (!task)
1439         return 0;
1440     yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1441             c, task->which, task->running);
1442     if (c->error != ZOOM_ERROR_NONE)
1443     {
1444         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1445                 "removing tasks because of error = %d", c, c->error);
1446         ZOOM_connection_remove_tasks(c);
1447         return 0;
1448     }
1449     if (task->running)
1450     {
1451         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1452                 "task already running", c);
1453         return 0;
1454     }
1455     task->running = 1;
1456     ret = zoom_complete;
1457     if (c->cs || task->which == ZOOM_TASK_CONNECT)
1458     {
1459         switch (task->which)
1460         {
1461         case ZOOM_TASK_SEARCH:
1462             if (c->proto == PROTO_HTTP)
1463                 ret = ZOOM_connection_srw_send_search(c);
1464             else
1465                 ret = ZOOM_connection_Z3950_send_search(c);
1466             break;
1467         case ZOOM_TASK_RETRIEVE:
1468             if (c->proto == PROTO_HTTP)
1469                 ret = ZOOM_connection_srw_send_search(c);
1470             else
1471                 ret = send_Z3950_present(c);
1472             break;
1473         case ZOOM_TASK_CONNECT:
1474             ret = do_connect(c);
1475             break;
1476         case ZOOM_TASK_SCAN:
1477             if (c->proto == PROTO_HTTP)
1478                 ret = ZOOM_connection_srw_send_scan(c);
1479             else
1480                 ret = ZOOM_connection_Z3950_send_scan(c);
1481             break;
1482         case ZOOM_TASK_PACKAGE:
1483             ret = send_package(c);
1484             break;
1485         case ZOOM_TASK_SORT:
1486             c->tasks->u.sort.resultset->r_sort_spec =
1487                 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1488             ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1489             break;
1490         }
1491     }
1492     else
1493     {
1494         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1495                 "remove tasks because no connection exist", c);
1496         ZOOM_connection_remove_tasks(c);
1497     }
1498     if (ret == zoom_complete)
1499     {
1500         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1501                 "task removed (complete)", c);
1502         ZOOM_connection_remove_task(c);
1503         return 0;
1504     }
1505     yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1506             "task pending", c);
1507     return 1;
1508 }
1509
1510 #if YAZ_HAVE_XML2
1511
1512 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri,
1513                                   Z_HTTP_Response *cookie_hres)
1514 {
1515     struct Z_HTTP_Header *h;
1516     char *combined_cookies = 0;
1517     int combined_cookies_len = 0;
1518     Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1519
1520     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1521     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1522                       "text/xml");
1523
1524     for (h = cookie_hres->headers; h; h = h->next)
1525     {
1526         if (!strcmp(h->name, "Set-Cookie"))
1527         {
1528             char *cp;
1529
1530             if (!(cp = strchr(h->value, ';')))
1531                 cp = h->value + strlen(h->value);
1532             if (cp - h->value >= 1) {
1533                 combined_cookies = xrealloc(combined_cookies, combined_cookies_len + cp - h->value + 3);
1534                 memcpy(combined_cookies+combined_cookies_len, h->value, cp - h->value);
1535                 combined_cookies[combined_cookies_len + cp - h->value] = '\0';
1536                 strcat(combined_cookies,"; ");
1537                 combined_cookies_len = strlen(combined_cookies);
1538             }
1539         }
1540     }
1541
1542     if (combined_cookies_len)
1543     {
1544         z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
1545                           "Cookie", combined_cookies);
1546         xfree(combined_cookies);
1547     }
1548
1549     if (c->user && c->password)
1550     {
1551         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1552                                      c->user, c->password);
1553     }
1554     return ZOOM_send_GDU(c, gdu);
1555 }
1556
1557 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1558 {
1559     ZOOM_Event event;
1560
1561     int r = z_GDU(c->odr_out, &gdu, 0, 0);
1562     if (!r)
1563         return zoom_complete;
1564     if (c->odr_print)
1565         z_GDU(c->odr_print, &gdu, 0, 0);
1566     if (c->odr_save)
1567         z_GDU(c->odr_save, &gdu, 0, 0);
1568     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1569     odr_reset(c->odr_out);
1570
1571     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1572     ZOOM_connection_put_event(c, event);
1573
1574     return ZOOM_send_buf(c);
1575 }
1576
1577 #if YAZ_HAVE_XML2
1578 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1579                          const char *addinfo, const char *addinfo2)
1580 {
1581     ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1582 }
1583 #endif
1584
1585
1586 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1587 {
1588     zoom_ret cret = zoom_complete;
1589     int ret = -1;
1590     char *addinfo = 0;
1591     const char *connection_head = z_HTTP_header_lookup(hres->headers,
1592                                                        "Connection");
1593     const char *location;
1594
1595     ZOOM_connection_set_mask(c, 0);
1596     yaz_log(c->log_details, "%p handle_http", c);
1597
1598     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1599         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1600     {
1601         c->no_redirects++;
1602         if (c->no_redirects > 10)
1603         {
1604             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1605             c->no_redirects = 0;
1606             ZOOM_connection_close(c);
1607         }
1608         else
1609         {
1610             /* since redirect may change host we just reconnect. A smarter
1611                implementation might check whether it's the same server */
1612             do_connect_host(c, location);
1613             send_HTTP_redirect(c, location, hres);
1614             /* we're OK for now. Operation is not really complete */
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     default:
1946         return diagbib1_str(error);
1947     }
1948 }
1949
1950 ZOOM_API(int)
1951     ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1952                             const char **addinfo, const char **diagset)
1953 {
1954     int error = c->error;
1955     if (cp)
1956     {
1957         if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1958             *cp = ZOOM_diag_str(error);
1959         else if (!strcmp(c->diagset, "HTTP"))
1960             *cp = z_HTTP_errmsg(c->error);
1961         else if (!strcmp(c->diagset, "Bib-1"))
1962             *cp = ZOOM_diag_str(error);
1963         else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1964             *cp = yaz_diag_srw_str(c->error);
1965         else
1966             *cp = "Unknown error and diagnostic set";
1967     }
1968     if (addinfo)
1969         *addinfo = c->addinfo ? c->addinfo : "";
1970     if (diagset)
1971         *diagset = c->diagset ? c->diagset : "";
1972     return c->error;
1973 }
1974
1975 ZOOM_API(int)
1976     ZOOM_connection_error(ZOOM_connection c, const char **cp,
1977                           const char **addinfo)
1978 {
1979     return ZOOM_connection_error_x(c, cp, addinfo, 0);
1980 }
1981
1982 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1983 {
1984     ZOOM_Event event = 0;
1985     int r = cs_look(c->cs);
1986     yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1987             c, mask, r);
1988
1989     if (r == CS_NONE)
1990     {
1991         event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1992         ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1993         ZOOM_connection_close(c);
1994         ZOOM_connection_put_event(c, event);
1995     }
1996     else if (r == CS_CONNECT)
1997     {
1998         int ret = ret = cs_rcvconnect(c->cs);
1999         yaz_log(c->log_details, "%p ZOOM_connection_do_io "
2000                 "cs_rcvconnect returned %d", c, ret);
2001         if (ret == 1)
2002         {
2003             int mask = ZOOM_SELECT_EXCEPT;
2004             if (c->cs->io_pending & CS_WANT_WRITE)
2005                 mask += ZOOM_SELECT_WRITE;
2006             if (c->cs->io_pending & CS_WANT_READ)
2007                 mask += ZOOM_SELECT_READ;
2008             ZOOM_connection_set_mask(c, mask);
2009             event = ZOOM_Event_create(ZOOM_EVENT_NONE);
2010             ZOOM_connection_put_event(c, event);
2011         }
2012         else if (ret == 0)
2013         {
2014             event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2015             ZOOM_connection_put_event(c, event);
2016             get_cert(c);
2017             if (c->proto == PROTO_Z3950)
2018                 ZOOM_connection_Z3950_send_init(c);
2019             else
2020             {
2021                 /* no init request for SRW .. */
2022                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
2023                 ZOOM_connection_remove_task(c);
2024                 ZOOM_connection_set_mask(c, 0);
2025                 ZOOM_connection_exec_task(c);
2026             }
2027             c->state = STATE_ESTABLISHED;
2028         }
2029         else
2030         {
2031             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2032             ZOOM_connection_close(c);
2033         }
2034     }
2035     else
2036     {
2037         if (mask & ZOOM_SELECT_EXCEPT)
2038         {
2039             if (!ZOOM_test_reconnect(c))
2040             {
2041                 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2042                 ZOOM_connection_close(c);
2043             }
2044             return;
2045         }
2046         if (mask & ZOOM_SELECT_READ)
2047             do_read(c);
2048         if (c->cs && (mask & ZOOM_SELECT_WRITE))
2049             ZOOM_send_buf(c);
2050     }
2051 }
2052
2053 ZOOM_API(int)
2054     ZOOM_connection_last_event(ZOOM_connection cs)
2055 {
2056     if (!cs)
2057         return ZOOM_EVENT_NONE;
2058     return cs->last_event;
2059 }
2060
2061
2062 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2063 {
2064     if (c->mask)
2065     {
2066         ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2067         /* timeout and this connection was waiting */
2068         ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2069         ZOOM_connection_close(c);
2070         ZOOM_connection_put_event(c, event);
2071     }
2072     return 0;
2073 }
2074
2075 ZOOM_API(int)
2076     ZOOM_connection_process(ZOOM_connection c)
2077 {
2078     ZOOM_Event event;
2079     if (!c)
2080         return 0;
2081
2082     event = ZOOM_connection_get_event(c);
2083     if (event)
2084     {
2085         ZOOM_Event_destroy(event);
2086         return 1;
2087     }
2088     ZOOM_connection_exec_task(c);
2089     event = ZOOM_connection_get_event(c);
2090     if (event)
2091     {
2092         ZOOM_Event_destroy(event);
2093         return 1;
2094     }
2095     return 0;
2096 }
2097
2098 ZOOM_API(int)
2099     ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2100 {
2101     int i;
2102
2103     yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2104
2105     for (i = 0; i<no; i++)
2106     {
2107         ZOOM_connection c = cs[i];
2108
2109         if (c && ZOOM_connection_process(c))
2110             return i+1;
2111     }
2112     return 0;
2113 }
2114
2115 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2116 {
2117     if (c->mask && mask)
2118         ZOOM_connection_do_io(c, mask);
2119     return 0;
2120 }
2121
2122 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2123 {
2124     if (c->cs)
2125         return cs_fileno(c->cs);
2126     return -1;
2127 }
2128
2129 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2130 {
2131     c->mask = mask;
2132     if (!c->cs)
2133         return -1;
2134     return 0;
2135 }
2136
2137 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2138 {
2139     if (c->cs)
2140         return c->mask;
2141     return 0;
2142 }
2143
2144 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2145 {
2146     return ZOOM_options_get_int(c->options, "timeout", 30);
2147 }
2148
2149 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2150 {
2151     if (c->cs)
2152         cs_close(c->cs);
2153     c->cs = 0;
2154     ZOOM_connection_set_mask(c, 0);
2155     c->state = STATE_IDLE;
2156 }
2157
2158 /*
2159  * Local variables:
2160  * c-basic-offset: 4
2161  * c-file-style: "Stroustrup"
2162  * indent-tabs-mode: nil
2163  * End:
2164  * vim: shiftwidth=4 tabstop=8 expandtab
2165  */
2166