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