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