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