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