-/*
- * Copyright (c) 2000-2003, Index Data
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) Index Data
* See the file LICENSE for details.
- *
- * $Id: zoom-c.c,v 1.4 2003-11-19 19:07:26 adam Exp $
- *
- * ZOOM layer for C, connections, result sets, queries.
*/
+/**
+ * \file zoom-c.c
+ * \brief Implements ZOOM C interface.
+ */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <assert.h>
#include <string.h>
+#include <errno.h>
#include "zoom-p.h"
#include <yaz/yaz-util.h>
#include <yaz/xmalloc.h>
#include <yaz/otherinfo.h>
#include <yaz/log.h>
-#include <yaz/pquery.h>
-#include <yaz/marcdisp.h>
#include <yaz/diagbib1.h>
#include <yaz/charneg.h>
-#include <yaz/ill.h>
-#include <yaz/srw.h>
+#include <yaz/query-charset.h>
+#include <yaz/snprintf.h>
+#include <yaz/facet.h>
-#if HAVE_SYS_POLL_H
-#include <sys/poll.h>
-#endif
-
-typedef enum {
- zoom_pending,
- zoom_complete
-} zoom_ret;
+#include <yaz/shptr.h>
-static zoom_ret ZOOM_connection_send_init (ZOOM_connection c);
-static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
+#if SHPTR
+YAZ_SHPTR_TYPE(WRBUF)
+#endif
-static ZOOM_Event ZOOM_Event_create (int kind)
-{
- ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
- event->kind = kind;
- event->next = 0;
- event->prev = 0;
- return event;
-}
+static int log_api0 = 0;
+static int log_details0 = 0;
-static void ZOOM_Event_destroy (ZOOM_Event event)
-{
- xfree (event);
-}
+static void resultset_destroy(ZOOM_resultset r);
+static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
-static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
+static void initlog(void)
{
- if (c->m_queue_back)
- {
- c->m_queue_back->prev = event;
- assert (c->m_queue_front);
- }
- else
- {
- assert (!c->m_queue_front);
- c->m_queue_front = event;
- }
- event->next = c->m_queue_back;
- event->prev = 0;
- c->m_queue_back = event;
-}
+ static int log_level_initialized = 0;
-static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
-{
- ZOOM_Event event = c->m_queue_front;
- if (!event)
- {
- c->last_event = ZOOM_EVENT_NONE;
- return 0;
- }
- assert (c->m_queue_back);
- c->m_queue_front = event->prev;
- if (c->m_queue_front)
+ if (!log_level_initialized)
{
- assert (c->m_queue_back);
- c->m_queue_front->next = 0;
+ log_api0 = yaz_log_module_level("zoom");
+ log_details0 = yaz_log_module_level("zoomdetails");
+ log_level_initialized = 1;
}
- else
- c->m_queue_back = 0;
- c->last_event = event->kind;
- return event;
}
+void ZOOM_connection_remove_tasks(ZOOM_connection c);
-static void set_dset_error (ZOOM_connection c, int error,
- const char *dset,
- const char *addinfo, const char *addinfo2)
+void ZOOM_set_dset_error(ZOOM_connection c, int error,
+ const char *dset,
+ const char *addinfo, const char *addinfo2)
{
- xfree (c->addinfo);
+ char *cp;
+
+ xfree(c->addinfo);
c->addinfo = 0;
c->error = error;
- c->diagset = dset;
+ if (!c->diagset || strcmp(dset, c->diagset))
+ {
+ xfree(c->diagset);
+ c->diagset = xstrdup(dset);
+ /* remove integer part from SRW diagset .. */
+ if ((cp = strrchr(c->diagset, '/')))
+ *cp = '\0';
+ }
if (addinfo && addinfo2)
{
- c->addinfo = xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
+ c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 3);
strcpy(c->addinfo, addinfo);
+ strcat(c->addinfo, ": ");
strcat(c->addinfo, addinfo2);
}
else if (addinfo)
c->addinfo = xstrdup(addinfo);
- if (error)
- yaz_log(LOG_DEBUG, "Error %s %s:%d %s %s",
- c->host_port ? c->host_port : "<>", dset, error,
+ if (error != ZOOM_ERROR_NONE)
+ {
+ yaz_log(c->log_api, "%p set_dset_error %s %s:%d %s %s",
+ c, c->host_port ? c->host_port : "<>", dset, error,
addinfo ? addinfo : "",
addinfo2 ? addinfo2 : "");
+ ZOOM_connection_remove_tasks(c);
+ }
}
-#if HAVE_XML2
-static void set_HTTP_error (ZOOM_connection c, int error,
- const char *addinfo, const char *addinfo2)
+int ZOOM_uri_to_code(const char *uri)
{
- set_dset_error(c, error, "HTTP", addinfo, addinfo2);
+ int code = 0;
+ const char *cp;
+ if ((cp = strrchr(uri, '/')))
+ code = atoi(cp+1);
+ return code;
}
-#endif
-static void set_ZOOM_error (ZOOM_connection c, int error,
- const char *addinfo)
+
+
+void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
{
- set_dset_error(c, error, "ZOOM", addinfo, 0);
+ ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
}
-static void clear_error (ZOOM_connection c)
+static void clear_error(ZOOM_connection c)
{
+ /*
+ * If an error is tied to an operation then it's ok to clear: for
+ * example, a diagnostic returned from a search is cleared by a
+ * subsequent search. However, problems such as Connection Lost
+ * or Init Refused are not cleared, because they are not
+ * recoverable: doing another search doesn't help.
+ */
+ ZOOM_connection_remove_events(c);
switch (c->error)
{
case ZOOM_ERROR_CONNECT:
case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
break;
default:
- set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
+ ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
+ }
+}
+
+void ZOOM_connection_show_task(ZOOM_task task)
+{
+ switch(task->which)
+ {
+ case ZOOM_TASK_SEARCH:
+ yaz_log(YLOG_LOG, "search p=%p", task);
+ break;
+ case ZOOM_TASK_RETRIEVE:
+ yaz_log(YLOG_LOG, "retrieve p=%p", task);
+ break;
+ case ZOOM_TASK_CONNECT:
+ yaz_log(YLOG_LOG, "connect p=%p", task);
+ break;
+ case ZOOM_TASK_SCAN:
+ yaz_log(YLOG_LOG, "scan p=%p", task);
+ break;
}
}
-ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
+void ZOOM_connection_show_tasks(ZOOM_connection c)
+{
+ ZOOM_task task;
+ yaz_log(YLOG_LOG, "connection p=%p tasks", c);
+ for (task = c->tasks; task; task = task->next)
+ ZOOM_connection_show_task(task);
+}
+
+ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
{
ZOOM_task *taskp = &c->tasks;
while (*taskp)
- taskp = &(*taskp)->next;
- *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
+ taskp = &(*taskp)->next;
+ *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
(*taskp)->running = 0;
(*taskp)->which = which;
(*taskp)->next = 0;
- clear_error (c);
+ clear_error(c);
return *taskp;
}
-ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
+ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
+{
+ return c->tasks ? 0 : 1;
+}
+
+ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
{
- ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
+ ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
task->next = c->tasks;
c->tasks = task;
task->running = 0;
task->which = which;
- clear_error (c);
return task;
}
-void ZOOM_connection_remove_task (ZOOM_connection c)
+void ZOOM_connection_remove_task(ZOOM_connection c)
{
ZOOM_task task = c->tasks;
if (task)
{
- c->tasks = task->next;
- switch (task->which)
- {
- case ZOOM_TASK_SEARCH:
-
- ZOOM_resultset_destroy (task->u.search.resultset);
- break;
- case ZOOM_TASK_RETRIEVE:
- ZOOM_resultset_destroy (task->u.retrieve.resultset);
- break;
+ c->tasks = task->next;
+ switch (task->which)
+ {
+ case ZOOM_TASK_SEARCH:
+ resultset_destroy(task->u.search.resultset);
+ xfree(task->u.search.syntax);
+ xfree(task->u.search.elementSetName);
+ xfree(task->u.search.schema);
+ break;
+ case ZOOM_TASK_RETRIEVE:
+ resultset_destroy(task->u.retrieve.resultset);
+ xfree(task->u.retrieve.syntax);
+ xfree(task->u.retrieve.elementSetName);
+ xfree(task->u.retrieve.schema);
+ break;
case ZOOM_TASK_CONNECT:
break;
case ZOOM_TASK_SCAN:
- ZOOM_scanset_destroy (task->u.scan.scan);
+ ZOOM_scanset_destroy(task->u.scan.scan);
break;
case ZOOM_TASK_PACKAGE:
- ZOOM_package_destroy (task->u.package);
+ ZOOM_package_destroy(task->u.package);
+ break;
+ case ZOOM_TASK_SORT:
+ resultset_destroy(task->u.sort.resultset);
+ ZOOM_query_destroy(task->u.sort.q);
break;
- default:
- assert (0);
- }
- xfree (task);
+ default:
+ assert(0);
+ }
+ xfree(task);
+
+ if (!c->tasks)
+ {
+ ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
+ ZOOM_connection_put_event(c, event);
+ }
}
}
-static int ZOOM_connection_exec_task (ZOOM_connection c);
-
-void ZOOM_connection_remove_tasks (ZOOM_connection c)
+void ZOOM_connection_remove_tasks(ZOOM_connection c)
{
while (c->tasks)
- ZOOM_connection_remove_task(c);
+ ZOOM_connection_remove_task(c);
}
-static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
+static void odr_wrbuf_write(ODR o, void *handle, int type,
+ const char *buf, int len)
+{
+ WRBUF w = (WRBUF) handle;
+ wrbuf_write(w, buf, len);
+}
ZOOM_API(ZOOM_connection)
-ZOOM_connection_create (ZOOM_options options)
+ ZOOM_connection_create(ZOOM_options options)
{
- ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
+ ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
+
+ initlog();
+
+ c->log_api = log_api0;
+ c->log_details = log_details0;
+
+ yaz_log(c->log_api, "%p ZOOM_connection_create", c);
c->proto = PROTO_Z3950;
c->cs = 0;
- c->mask = 0;
+ ZOOM_connection_set_mask(c, 0);
c->reconnect_ok = 0;
c->state = STATE_IDLE;
c->addinfo = 0;
- set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
+ c->diagset = 0;
+ ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
c->buf_in = 0;
c->len_in = 0;
c->buf_out = 0;
c->options = ZOOM_options_create_with_parent(options);
c->host_port = 0;
- c->path = 0;
c->proxy = 0;
-
+ c->tproxy = 0;
+
c->charset = c->lang = 0;
c->cookie_out = 0;
c->client_IP = 0;
c->tasks = 0;
- c->odr_in = odr_createmem (ODR_DECODE);
- c->odr_out = odr_createmem (ODR_ENCODE);
+ c->user = 0;
+ c->group = 0;
+ c->password = 0;
+ c->url_authentication = 0;
+
+ c->maximum_record_size = 0;
+ c->preferred_message_size = 0;
+
+ c->odr_in = odr_createmem(ODR_DECODE);
+ c->odr_out = odr_createmem(ODR_ENCODE);
+ c->odr_print = 0;
+ c->odr_save = 0;
c->async = 0;
c->support_named_resultsets = 0;
c->m_queue_front = 0;
c->m_queue_back = 0;
+
+ c->sru_version = 0;
+ c->no_redirects = 0;
+ c->cookies = 0;
+ c->saveAPDU_wrbuf = 0;
return c;
}
-/* set database names. Take local databases (if set); otherwise
- take databases given in ZURL (if set); otherwise use Default */
-static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
- int *num)
+ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
{
- char **databaseNames;
- const char *c;
- int no = 2;
- const char *cp = ZOOM_options_get (options, "databaseName");
-
- if (!cp || !*cp)
+ if (c->odr_save)
{
- if (strncmp (con->host_port, "unix:", 5) == 0)
- cp = strchr (con->host_port+5, ':');
- else
- cp = strchr (con->host_port, '/');
- if (cp)
- cp++;
+ odr_destroy(c->odr_save);
+ c->odr_save = 0;
}
- if (cp)
+ if (w)
{
- c = cp;
- while ((c = strchr(c, '+')))
- {
- c++;
- no++;
- }
+ c->odr_save = odr_createmem(ODR_PRINT);
+ odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
}
- else
- cp = "Default";
- databaseNames = (char**)
- odr_malloc (con->odr_out, no * sizeof(*databaseNames));
- no = 0;
- while (*cp)
- {
- c = strchr (cp, '+');
- if (!c)
- c = cp + strlen(cp);
- else if (c == cp)
- {
- cp++;
- continue;
- }
- /* cp ptr to first char of db name, c is char
- following db name */
- databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
- memcpy (databaseNames[no], cp, c-cp);
- databaseNames[no++][c-cp] = '\0';
- cp = c;
- if (*cp)
- cp++;
- }
- databaseNames[no] = NULL;
- *num = no;
+}
+
+/* set database names. Take local databases (if set); otherwise
+ take databases given in ZURL (if set); otherwise use Default */
+char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
+ int *num, ODR odr)
+{
+ char **databaseNames;
+ const char *cp = ZOOM_options_get(options, "databaseName");
+
+ if ((!cp || !*cp) && con->host_port)
+ cs_get_host_args(con->host_port, &cp);
+ if (!cp || !*cp)
+ cp = "Default";
+ nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
return databaseNames;
}
ZOOM_API(ZOOM_connection)
-ZOOM_connection_new (const char *host, int portnum)
+ ZOOM_connection_new(const char *host, int portnum)
{
- ZOOM_connection c = ZOOM_connection_create (0);
+ ZOOM_connection c = ZOOM_connection_create(0);
- ZOOM_connection_connect (c, host, portnum);
+ ZOOM_connection_connect(c, host, portnum);
return c;
}
+static zoom_sru_mode get_sru_mode_from_string(const char *s)
+{
+ if (!s || !*s)
+ return zoom_sru_soap;
+ if (!yaz_matchstr(s, "soap"))
+ return zoom_sru_soap;
+ else if (!yaz_matchstr(s, "get"))
+ return zoom_sru_get;
+ else if (!yaz_matchstr(s, "post"))
+ return zoom_sru_post;
+ else if (!yaz_matchstr(s, "solr"))
+ return zoom_sru_solr;
+ return zoom_sru_error;
+}
+
ZOOM_API(void)
-ZOOM_connection_connect(ZOOM_connection c,
- const char *host, int portnum)
+ ZOOM_connection_connect(ZOOM_connection c,
+ const char *host, int portnum)
{
const char *val;
- ZOOM_task task;
+ const char *http_lead;
+
+ initlog();
+
+ yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
+ c, host ? host : "null", portnum);
+
+ ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
+ ZOOM_connection_remove_tasks(c);
+
+ if (c->odr_print)
+ {
+ odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
+ odr_destroy(c->odr_print);
+ }
+ if (ZOOM_options_get_bool(c->options, "apdulog", 0))
+ {
+ c->odr_print = odr_createmem(ODR_PRINT);
+ odr_setprint(c->odr_print, yaz_log_file());
+ }
+ else
+ c->odr_print = 0;
if (c->cs)
{
- yaz_log (LOG_DEBUG, "reconnect");
+ yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
c->reconnect_ok = 1;
return;
}
- yaz_log(LOG_DEBUG, "connect");
- xfree (c->proxy);
- val = ZOOM_options_get (c->options, "proxy");
+ yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
+ xfree(c->proxy);
+ c->proxy = 0;
+ val = ZOOM_options_get(c->options, "proxy");
if (val && *val)
- c->proxy = xstrdup (val);
- else
- c->proxy = 0;
+ {
+ yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
+ c->proxy = xstrdup(val);
+ }
- xfree (c->charset);
- val = ZOOM_options_get (c->options, "charset");
+ xfree(c->tproxy);
+ c->tproxy = 0;
+ val = ZOOM_options_get(c->options, "tproxy");
if (val && *val)
- c->charset = xstrdup (val);
- else
- c->charset = 0;
+ {
+ yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
+ c->tproxy = xstrdup(val);
+ }
- xfree (c->lang);
- val = ZOOM_options_get (c->options, "lang");
+ xfree(c->charset);
+ c->charset = 0;
+ val = ZOOM_options_get(c->options, "charset");
if (val && *val)
- c->lang = xstrdup (val);
- else
- c->lang = 0;
+ {
+ yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
+ c->charset = xstrdup(val);
+ }
- xfree (c->host_port);
- if (portnum)
+ xfree(c->lang);
+ val = ZOOM_options_get(c->options, "lang");
+ if (val && *val)
{
- char hostn[128];
- sprintf (hostn, "%.80s:%d", host, portnum);
- c->host_port = xstrdup(hostn);
+ yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
+ c->lang = xstrdup(val);
}
else
- c->host_port = xstrdup(host);
+ c->lang = 0;
- ZOOM_options_set(c->options, "host", c->host_port);
+ val = ZOOM_options_get(c->options, "sru");
+ if (val && *val && !strstr(host, "://"))
+ http_lead = "http://";
+ else
+ http_lead = "";
+ c->sru_mode = get_sru_mode_from_string(val);
- val = ZOOM_options_get (c->options, "cookie");
- if (val && *val)
- c->cookie_out = xstrdup (val);
+ if (host)
+ {
+ char hostn[128];
+ xfree(c->host_port);
+ if (portnum)
+ {
+ sprintf(hostn, "%.80s:%d", host, portnum);
+ host = hostn;
+ }
+ c->host_port = xmalloc(strlen(host) + strlen(http_lead) + 1);
+ strcpy(c->host_port, http_lead);
+ strcat(c->host_port, host);
+ }
+
+ {
+ /*
+ * If the "<scheme>:" part of the host string is preceded by one
+ * or more comma-separated <name>=<value> pairs, these are taken
+ * to be options to be set on the connection object. Among other
+ * applications, this facility can be used to embed authentication
+ * in a host string:
+ * user=admin,password=secret,tcp:localhost:9999
+ */
+ char *remainder = c->host_port;
+ char *pcolon = strchr(remainder, ':');
+ char *pcomma;
+ char *pequals;
+ while ((pcomma = strchr(remainder, ',')) != 0 &&
+ (pcolon == 0 || pcomma < pcolon))
+ {
+ *pcomma = '\0';
+ if ((pequals = strchr(remainder, '=')) != 0)
+ {
+ *pequals = '\0';
+ ZOOM_connection_option_set(c, remainder, pequals+1);
+ }
+ remainder = pcomma+1;
+ }
- val = ZOOM_options_get (c->options, "clientIP");
- if (val && *val)
- c->client_IP = xstrdup (val);
+ if (remainder != c->host_port)
+ {
+ remainder = xstrdup(remainder);
+ xfree(c->host_port);
+ c->host_port = remainder;
+ }
+ }
- c->async = ZOOM_options_get_bool (c->options, "async", 0);
-
- set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
+ xfree(c->sru_version);
+ val = ZOOM_options_get(c->options, "sru_version");
+ c->sru_version = xstrdup(val ? val : "1.2");
- task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
+ ZOOM_options_set(c->options, "host", c->host_port);
- if (!c->async)
+ xfree(c->cookie_out);
+ c->cookie_out = 0;
+ val = ZOOM_options_get(c->options, "cookie");
+ if (val && *val)
{
- while (ZOOM_event (1, &c))
- ;
+ yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
+ c->cookie_out = xstrdup(val);
}
-}
-ZOOM_API(ZOOM_query)
-ZOOM_query_create(void)
-{
- ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
+ xfree(c->client_IP);
+ c->client_IP = 0;
+ val = ZOOM_options_get(c->options, "clientIP");
+ if (val && *val)
+ {
+ yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
+ c, val);
+ c->client_IP = xstrdup(val);
+ }
- s->refcount = 1;
- s->z_query = 0;
- s->sort_spec = 0;
- s->odr = odr_createmem (ODR_ENCODE);
- s->query_string = 0;
+ xfree(c->group);
+ c->group = 0;
+ val = ZOOM_options_get(c->options, "group");
+ if (val && *val)
+ c->group = xstrdup(val);
- return s;
-}
+ xfree(c->user);
+ c->user = 0;
+ val = ZOOM_options_get(c->options, "user");
+ if (val && *val)
+ c->user = xstrdup(val);
-ZOOM_API(void)
-ZOOM_query_destroy(ZOOM_query s)
-{
- if (!s)
- return;
+ xfree(c->password);
+ c->password = 0;
+ val = ZOOM_options_get(c->options, "password");
+ if (!val)
+ val = ZOOM_options_get(c->options, "pass");
+ if (val && *val)
+ c->password = xstrdup(val);
- (s->refcount)--;
- yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
- if (s->refcount == 0)
- {
- odr_destroy (s->odr);
- xfree (s);
- }
-}
+ val = ZOOM_options_get(c->options, "authenticationMode");
+ if (val && !strcmp(val, "url"))
+ c->url_authentication = 1;
+ else
+ c->url_authentication = 0;
-ZOOM_API(int)
-ZOOM_query_prefix(ZOOM_query s, const char *str)
-{
- s->query_string = odr_strdup(s->odr, str);
- s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query));
- s->z_query->which = Z_Query_type_1;
- s->z_query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
- if (!s->z_query->u.type_1)
- return -1;
- return 0;
-}
+ c->maximum_record_size =
+ ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
+ c->preferred_message_size =
+ ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
-ZOOM_API(int)
-ZOOM_query_cql(ZOOM_query s, const char *str)
-{
- Z_External *ext;
+ c->async = ZOOM_options_get_bool(c->options, "async", 0);
+
+ yaz_cookies_destroy(c->cookies);
+ c->cookies = yaz_cookies_create();
- s->query_string = odr_strdup(s->odr, str);
+ if (c->sru_mode == zoom_sru_error)
+ {
+ ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
+ ZOOM_connection_remove_tasks(c);
+ return;
+ }
- ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
- ext->direct_reference = odr_getoidbystr(s->odr, "1.2.840.10003.16.2");
- ext->indirect_reference = 0;
- ext->descriptor = 0;
- ext->which = Z_External_CQL;
- ext->u.cql = s->query_string;
-
- s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query));
- s->z_query->which = Z_Query_type_104;
- s->z_query->u.type_104 = ext;
+ yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
+ ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
- return 0;
+ if (!c->async)
+ {
+ while (ZOOM_event(1, &c))
+ ;
+ }
}
-ZOOM_API(int)
-ZOOM_query_sortby(ZOOM_query s, const char *criteria)
+ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
{
- s->sort_spec = yaz_sort_spec (s->odr, criteria);
- if (!s->sort_spec)
- return -1;
- return 0;
+ if (r->connection)
+ {
+ /* remove ourselves from the resultsets in connection */
+ ZOOM_resultset *rp = &r->connection->resultsets;
+ while (1)
+ {
+ assert(*rp); /* we must be in this list!! */
+ if (*rp == r)
+ { /* OK, we're here - take us out of it */
+ *rp = (*rp)->next;
+ break;
+ }
+ rp = &(*rp)->next;
+ }
+ r->connection = 0;
+ }
}
-static zoom_ret do_write(ZOOM_connection c);
-
ZOOM_API(void)
-ZOOM_connection_destroy(ZOOM_connection c)
+ ZOOM_connection_destroy(ZOOM_connection c)
{
ZOOM_resultset r;
if (!c)
- return;
+ return;
+ yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
if (c->cs)
- cs_close (c->cs);
+ cs_close(c->cs);
+
for (r = c->resultsets; r; r = r->next)
- r->connection = 0;
-
- xfree (c->buf_in);
- xfree (c->addinfo);
- odr_destroy (c->odr_in);
- odr_destroy (c->odr_out);
- ZOOM_options_destroy (c->options);
- ZOOM_connection_remove_tasks (c);
- xfree (c->host_port);
- xfree (c->path);
- xfree (c->proxy);
- xfree (c->charset);
- xfree (c->lang);
- xfree (c->cookie_out);
- xfree (c->cookie_in);
- xfree (c->client_IP);
- xfree (c);
-}
-
-void ZOOM_resultset_addref (ZOOM_resultset r)
+ r->connection = 0;
+
+ xfree(c->buf_in);
+ xfree(c->addinfo);
+ xfree(c->diagset);
+ odr_destroy(c->odr_in);
+ odr_destroy(c->odr_out);
+ if (c->odr_save)
+ odr_destroy(c->odr_save);
+ if (c->odr_print)
+ {
+ odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
+ odr_destroy(c->odr_print);
+ }
+ ZOOM_options_destroy(c->options);
+ ZOOM_connection_remove_tasks(c);
+ ZOOM_connection_remove_events(c);
+ xfree(c->host_port);
+ xfree(c->proxy);
+ xfree(c->tproxy);
+ xfree(c->charset);
+ xfree(c->lang);
+ xfree(c->cookie_out);
+ xfree(c->cookie_in);
+ xfree(c->client_IP);
+ xfree(c->user);
+ xfree(c->group);
+ xfree(c->password);
+ xfree(c->sru_version);
+ yaz_cookies_destroy(c->cookies);
+ wrbuf_destroy(c->saveAPDU_wrbuf);
+ xfree(c);
+}
+
+void ZOOM_resultset_addref(ZOOM_resultset r)
{
if (r)
{
- (r->refcount)++;
- yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
- r, r->refcount);
+ yaz_mutex_enter(r->mutex);
+ (r->refcount)++;
+ yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
+ r, r->refcount);
+ yaz_mutex_leave(r->mutex);
}
}
-ZOOM_resultset ZOOM_resultset_create ()
+
+static int g_resultsets = 0;
+static YAZ_MUTEX g_resultset_mutex = 0;
+
+/* TODO We need to initialize this before running threaded:
+ * call resultset_use(0) */
+
+static int resultset_use(int delta) {
+ int resultset_count;
+ if (g_resultset_mutex == 0)
+ yaz_mutex_create(&g_resultset_mutex);
+ yaz_mutex_enter(g_resultset_mutex);
+ g_resultsets += delta;
+ resultset_count = g_resultsets;
+ yaz_mutex_leave(g_resultset_mutex);
+ return resultset_count;
+}
+
+int resultsets_count(void) {
+ return resultset_use(0);
+}
+
+ZOOM_resultset ZOOM_resultset_create(void)
{
- ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
+ int i;
+ ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
- yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
+ initlog();
+
+ yaz_log(log_details0, "%p ZOOM_resultset_create", r);
r->refcount = 1;
r->size = 0;
- r->odr = odr_createmem (ODR_ENCODE);
- r->start = 0;
+ r->odr = odr_createmem(ODR_ENCODE);
r->piggyback = 1;
r->setname = 0;
- r->schema = 0;
- r->count = 0;
r->step = 0;
- r->record_cache = 0;
+ for (i = 0; i<RECORD_HASH_SIZE; i++)
+ r->record_hash[i] = 0;
r->r_sort_spec = 0;
r->query = 0;
r->connection = 0;
- r->next = 0;
+ r->databaseNames = 0;
+ r->num_databaseNames = 0;
+ r->facets = 0;
+ r->num_facets = 0;
+ r->facets_names = 0;
+ r->mutex = 0;
+ yaz_mutex_create(&r->mutex);
+#if SHPTR
+ {
+ WRBUF w = wrbuf_alloc();
+ YAZ_SHPTR_INIT(r->record_wrbuf, w);
+ }
+#endif
+ resultset_use(1);
return r;
}
ZOOM_API(ZOOM_resultset)
-ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
+ ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
{
ZOOM_resultset r;
ZOOM_query s = ZOOM_query_create();
- ZOOM_query_prefix (s, q);
+ ZOOM_query_prefix(s, q);
- r = ZOOM_connection_search (c, s);
- ZOOM_query_destroy (s);
+ r = ZOOM_connection_search(c, s);
+ ZOOM_query_destroy(s);
return r;
}
ZOOM_API(ZOOM_resultset)
-ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
+ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
{
- ZOOM_resultset r = ZOOM_resultset_create ();
+ ZOOM_resultset r = ZOOM_resultset_create();
ZOOM_task task;
const char *cp;
+ int start, count;
+ const char *syntax, *elementSetName, *schema;
- r->r_sort_spec = q->sort_spec;
+ yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
+ r->r_sort_spec = ZOOM_query_get_sortspec(q);
r->query = q;
r->options = ZOOM_options_create_with_parent(c->options);
- r->start = ZOOM_options_get_int(r->options, "start", 0);
- r->count = ZOOM_options_get_int(r->options, "count", 0);
- r->step = ZOOM_options_get_int(r->options, "step", 0);
- r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
- cp = ZOOM_options_get (r->options, "setname");
+ start = ZOOM_options_get_int(r->options, "start", 0);
+ count = ZOOM_options_get_int(r->options, "count", 0);
+ {
+ /* If "presentChunk" is defined use that; otherwise "step" */
+ const char *cp = ZOOM_options_get(r->options, "presentChunk");
+ r->step = ZOOM_options_get_int(r->options,
+ (cp != 0 ? "presentChunk": "step"), 0);
+ }
+ r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
+ cp = ZOOM_options_get(r->options, "setname");
if (cp)
r->setname = xstrdup(cp);
- cp = ZOOM_options_get (r->options, "schema");
- if (cp)
- r->schema = xstrdup(cp);
-
- r->connection = c;
+ r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
+ r->odr);
+
+ r->connection = c;
r->next = c->resultsets;
c->resultsets = r;
-
if (c->host_port && c->proto == PROTO_HTTP)
{
if (!c->cs)
{
- yaz_log(LOG_DEBUG, "NO COMSTACK");
+ yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
}
else
{
- yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
+ yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
c->reconnect_ok = 1;
}
}
- task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
+ task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
task->u.search.resultset = r;
- ZOOM_resultset_addref (r);
+ task->u.search.start = start;
+ task->u.search.count = count;
+ task->u.search.recv_search_fired = 0;
+
+ syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
+ task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
+ elementSetName = ZOOM_options_get(r->options, "elementSetName");
+ task->u.search.elementSetName = elementSetName ?
+ xstrdup(elementSetName) : 0;
+ schema = ZOOM_options_get(r->options, "schema");
+ task->u.search.schema = schema ? xstrdup(schema) : 0;
+
+ ZOOM_resultset_addref(r);
- (q->refcount)++;
+ ZOOM_query_addref(q);
if (!c->async)
{
- while (ZOOM_event (1, &c))
- ;
+ while (ZOOM_event(1, &c))
+ ;
}
return r;
}
ZOOM_API(void)
-ZOOM_resultset_destroy(ZOOM_resultset r)
+ ZOOM_resultset_sort(ZOOM_resultset r,
+ const char *sort_type, const char *sort_spec)
+{
+ (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
+}
+
+ZOOM_API(int)
+ ZOOM_resultset_sort1(ZOOM_resultset r,
+ const char *sort_type, const char *sort_spec)
+{
+ ZOOM_connection c = r->connection;
+ ZOOM_task task;
+ ZOOM_query newq;
+
+ newq = ZOOM_query_create();
+ if (ZOOM_query_sortby(newq, sort_spec) < 0)
+ return -1;
+
+ yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
+ r, r, sort_type, sort_spec);
+ if (!c)
+ return 0;
+
+ if (c->host_port && c->proto == PROTO_HTTP)
+ {
+ if (!c->cs)
+ {
+ yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
+ ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
+ }
+ else
+ {
+ yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
+ r);
+ c->reconnect_ok = 1;
+ }
+ }
+
+ ZOOM_resultset_cache_reset(r);
+ task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
+ task->u.sort.resultset = r;
+ task->u.sort.q = newq;
+
+ ZOOM_resultset_addref(r);
+
+ if (!c->async)
+ {
+ while (ZOOM_event(1, &c))
+ ;
+ }
+
+ return 0;
+}
+
+ZOOM_API(void)
+ ZOOM_resultset_destroy(ZOOM_resultset r)
+{
+ resultset_destroy(r);
+}
+
+static void resultset_destroy(ZOOM_resultset r)
{
if (!r)
return;
+ yaz_mutex_enter(r->mutex);
(r->refcount)--;
- yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
- r, r->refcount);
+ yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
+ r, r, r->refcount);
if (r->refcount == 0)
{
- ZOOM_record_cache rc;
-
- for (rc = r->record_cache; rc; rc = rc->next)
- {
- if (rc->rec.wrbuf_marc)
- wrbuf_free (rc->rec.wrbuf_marc, 1);
- if (rc->rec.wrbuf_iconv)
- wrbuf_free (rc->rec.wrbuf_iconv, 1);
- if (rc->rec.wrbuf_opac)
- wrbuf_free (rc->rec.wrbuf_opac, 1);
- }
- if (r->connection)
- {
- /* remove ourselves from the resultsets in connection */
- ZOOM_resultset *rp = &r->connection->resultsets;
- while (1)
- {
- assert (*rp); /* we must be in this list!! */
- if (*rp == r)
- { /* OK, we're here - take us out of it */
- *rp = (*rp)->next;
- break;
- }
- rp = &(*rp)->next;
- }
- }
- ZOOM_query_destroy (r->query);
- ZOOM_options_destroy (r->options);
- odr_destroy (r->odr);
- xfree (r->setname);
- xfree (r->schema);
- xfree (r);
+ yaz_mutex_leave(r->mutex);
+
+ yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
+ ZOOM_resultset_cache_reset(r);
+ ZOOM_resultset_release(r);
+ ZOOM_query_destroy(r->query);
+ ZOOM_options_destroy(r->options);
+ odr_destroy(r->odr);
+ xfree(r->setname);
+ yaz_mutex_destroy(&r->mutex);
+#if SHPTR
+ YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
+#endif
+ resultset_use(-1);
+ xfree(r);
}
+ else
+ yaz_mutex_leave(r->mutex);
}
ZOOM_API(size_t)
-ZOOM_resultset_size (ZOOM_resultset r)
+ ZOOM_resultset_size(ZOOM_resultset r)
{
return r->size;
}
-static void do_close (ZOOM_connection c)
+int ZOOM_test_reconnect(ZOOM_connection c)
{
- if (c->cs)
- cs_close(c->cs);
- c->cs = 0;
- c->mask = 0;
- c->state = STATE_IDLE;
+ ZOOM_Event event;
+
+ if (!c->reconnect_ok)
+ return 0;
+ ZOOM_connection_close(c);
+ c->reconnect_ok = 0;
+ c->tasks->running = 0;
+ ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
+
+ event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
+ ZOOM_connection_put_event(c, event);
+
+ return 1;
}
-static void ZOOM_resultset_retrieve (ZOOM_resultset r,
- int force_sync, int start, int count)
+static void ZOOM_resultset_retrieve(ZOOM_resultset r,
+ int force_sync, int start, int count)
{
ZOOM_task task;
ZOOM_connection c;
const char *cp;
+ const char *syntax, *elementSetName;
if (!r)
- return;
+ return;
+ yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
+ " count=%d", r, force_sync, start, count);
c = r->connection;
if (!c)
- return;
+ return;
if (c->host_port && c->proto == PROTO_HTTP)
{
if (!c->cs)
{
- yaz_log(LOG_DEBUG, "NO COMSTACK");
+ yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
}
else
{
- yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
+ yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
+ "reconnect", r);
c->reconnect_ok = 1;
}
}
- task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
+ task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
task->u.retrieve.resultset = r;
task->u.retrieve.start = start;
task->u.retrieve.count = count;
- cp = ZOOM_options_get (r->options, "schema");
- if (cp)
- {
- if (!r->schema || strcmp(r->schema, cp))
- {
- xfree(r->schema);
- r->schema = xstrdup(cp);
- }
- }
+ syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
+ task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
+ elementSetName = ZOOM_options_get(r->options, "elementSetName");
+ task->u.retrieve.elementSetName = elementSetName
+ ? xstrdup(elementSetName) : 0;
+
+ cp = ZOOM_options_get(r->options, "schema");
+ task->u.retrieve.schema = cp ? xstrdup(cp) : 0;
- ZOOM_resultset_addref (r);
+ ZOOM_resultset_addref(r);
if (!r->connection->async || force_sync)
- while (r->connection && ZOOM_event (1, &r->connection))
- ;
+ while (r->connection && ZOOM_event(1, &r->connection))
+ ;
}
ZOOM_API(void)
-ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
- size_t start, size_t count)
+ ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
+ size_t start, size_t count)
{
int force_present = 0;
if (!r)
- return ;
+ return ;
+ yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
+ r, r, (long) start, (long) count);
if (count && recs)
force_present = 1;
- ZOOM_resultset_retrieve (r, force_present, start, count);
+ ZOOM_resultset_retrieve(r, force_present, start, count);
if (force_present)
{
size_t i;
for (i = 0; i< count; i++)
- recs[i] = ZOOM_resultset_record_immediate (r, i+start);
+ recs[i] = ZOOM_resultset_record_immediate(r, i+start);
}
}
-static zoom_ret do_connect (ZOOM_connection c)
-{
- void *add;
- const char *effective_host;
-
- if (c->proxy)
- effective_host = c->proxy;
- else
- effective_host = c->host_port;
-
- yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
+ZOOM_API(size_t)
+ ZOOM_resultset_facets_size(ZOOM_resultset r) {
+ return r->num_facets;
+}
+
+ZOOM_API(ZOOM_facet_field)
+ ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
+ int num = r->num_facets;
+ ZOOM_facet_field *facets = r->facets;
+ int index;
+ for (index = 0; index < num; index++) {
+ if (!strcmp(facets[index]->facet_name, name)) {
+ return facets[index];
+ }
+ }
+ return 0;
+}
- assert (!c->cs);
- c->cs = cs_create_host (effective_host, 0, &add);
-
- if (c->cs && c->cs->protocol == PROTO_HTTP)
- {
-#if HAVE_XML2
- const char *path = 0;
-
- c->proto = PROTO_HTTP;
- cs_get_host_args(c->host_port, &path);
- xfree(c->path);
- c->path = xmalloc(strlen(path)+2);
- c->path[0] = '/';
- strcpy (c->path+1, path);
-#else
- set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
- do_close(c);
- return zoom_complete;
-#endif
+ZOOM_API(ZOOM_facet_field)
+ ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
+ int num = r->num_facets;
+ ZOOM_facet_field *facets = r->facets;
+ if (index >= 0 && index < num) {
+ return facets[index];
}
- if (c->cs)
- {
- int ret = cs_connect (c->cs, add);
- if (ret == 0)
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
- ZOOM_connection_put_event(c, event);
- if (c->proto == PROTO_Z3950)
- ZOOM_connection_send_init(c);
- else
- {
- /* no init request for SRW .. */
- assert (c->tasks->which == ZOOM_TASK_CONNECT);
- ZOOM_connection_remove_task (c);
- c->mask = 0;
- ZOOM_connection_exec_task (c);
- }
- c->state = STATE_ESTABLISHED;
- return zoom_pending;
- }
- else if (ret > 0)
- {
- c->state = STATE_CONNECTING;
- c->mask = ZOOM_SELECT_EXCEPT;
- if (c->cs->io_pending & CS_WANT_WRITE)
- c->mask += ZOOM_SELECT_WRITE;
- if (c->cs->io_pending & CS_WANT_READ)
- c->mask += ZOOM_SELECT_READ;
- return zoom_pending;
- }
- }
- c->state = STATE_IDLE;
- set_ZOOM_error(c, ZOOM_ERROR_CONNECT, effective_host);
- return zoom_complete;
+ return 0;
}
-int z3950_connection_socket(ZOOM_connection c)
+ZOOM_API(ZOOM_facet_field *)
+ ZOOM_resultset_facets(ZOOM_resultset r)
{
- if (c->cs)
- return cs_fileno(c->cs);
- return -1;
+ return r->facets;
}
-int z3950_connection_mask(ZOOM_connection c)
+ZOOM_API(const char**)
+ ZOOM_resultset_facets_names(ZOOM_resultset r)
{
- if (c->cs)
- return c->mask;
- return 0;
+ return (const char **) r->facets_names;
}
-static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
+ZOOM_API(const char*)
+ ZOOM_facet_field_name(ZOOM_facet_field field)
{
- int i;
- for (i = 0; i<200; i++)
- {
- size_t len;
- Z_OtherInformation **oi;
- char buf[80];
- const char *val;
- const char *cp;
- int oidval;
-
- sprintf (buf, "otherInfo%d", i);
- val = ZOOM_options_get (c->options, buf);
- if (!val)
- break;
- cp = strchr (val, ':');
- if (!cp)
- continue;
- len = cp - val;
- if (len >= sizeof(buf))
- len = sizeof(buf)-1;
- memcpy (buf, val, len);
- buf[len] = '\0';
- oidval = oid_getvalbyname (buf);
- if (oidval == VAL_NONE)
- continue;
-
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
- }
-}
-
-static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
-{
- assert (a);
- if (c->cookie_out)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
- }
- if (c->client_IP)
- {
- Z_OtherInformation **oi;
- yaz_oi_APDU(a, &oi);
- yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
- }
- otherInfo_attach (c, a, out);
- if (!z_APDU(out, &a, 0, 0))
- {
- FILE *outf = fopen("/tmp/apdu.txt", "a");
- if (a && outf)
- {
- ODR odr_pr = odr_createmem(ODR_PRINT);
- fprintf (outf, "a=%p\n", a);
- odr_setprint(odr_pr, outf);
- z_APDU(odr_pr, &a, 0, 0);
- odr_destroy(odr_pr);
- }
- yaz_log (LOG_DEBUG, "encoding failed");
- set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
- odr_reset(out);
- return -1;
- }
-
- return 0;
+ return field->facet_name;
}
-static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
+ZOOM_API(size_t)
+ ZOOM_facet_field_term_count(ZOOM_facet_field field)
{
- ZOOM_Event event;
- assert (a);
- if (encode_APDU(c, a, c->odr_out))
- return zoom_complete;
- yaz_log(LOG_DEBUG, "send APDU type=%d", a->which);
- c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
- event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event (c, event);
- odr_reset(c->odr_out);
- return do_write (c);
+ return field->num_terms;
}
-/* returns 1 if PDU was sent OK (still pending )
- 0 if PDU was not sent OK (nothing to wait for)
-*/
-
-static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
-{
- const char *impid, *impname, *impver;
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
- Z_InitRequest *ireq = apdu->u.initRequest;
- Z_IdAuthentication *auth = (Z_IdAuthentication *)
- odr_malloc(c->odr_out, sizeof(*auth));
- const char *auth_groupId = ZOOM_options_get (c->options, "group");
- const char *auth_userId = ZOOM_options_get (c->options, "user");
- const char *auth_password = ZOOM_options_get (c->options, "pass");
-
- ODR_MASK_SET(ireq->options, Z_Options_search);
- ODR_MASK_SET(ireq->options, Z_Options_present);
- ODR_MASK_SET(ireq->options, Z_Options_scan);
- ODR_MASK_SET(ireq->options, Z_Options_sort);
- ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
- ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
-
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
- ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
-
- impid = ZOOM_options_get (c->options, "implementationId");
- ireq->implementationId =
- (char *) odr_malloc (c->odr_out, 15 + (impid ? strlen(impid) : 0));
- strcpy (ireq->implementationId, "");
- if (impid)
- {
- strcat (ireq->implementationId, impid);
- strcat (ireq->implementationId, "/");
- }
- strcat (ireq->implementationId, "81"); /* Index's implementor ID */
-
- impname = ZOOM_options_get (c->options, "implementationName");
- ireq->implementationName =
- (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
- strcpy (ireq->implementationName, "");
- if (impname)
- {
- strcat (ireq->implementationName, impname);
- strcat (ireq->implementationName, "/");
- }
- strcat (ireq->implementationName, "ZOOM-C/YAZ");
-
- impver = ZOOM_options_get (c->options, "implementationVersion");
- ireq->implementationVersion =
- (char *) odr_malloc (c->odr_out, strlen("$Revision: 1.4 $") + 2 +
- (impver ? strlen(impver) : 0));
- strcpy (ireq->implementationVersion, "");
- if (impver)
- {
- strcat (ireq->implementationVersion, impver);
- strcat (ireq->implementationVersion, "/");
- }
- strcat (ireq->implementationVersion, "$Revision: 1.4 $");
-
- *ireq->maximumRecordSize =
- ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
- *ireq->preferredMessageSize =
- ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
-
- if (auth_groupId || auth_password)
- {
- Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
- int i = 0;
- pass->groupId = 0;
- if (auth_groupId && *auth_groupId)
- {
- pass->groupId = (char *)
- odr_malloc(c->odr_out, strlen(auth_groupId)+1);
- strcpy(pass->groupId, auth_groupId);
- i++;
- }
- pass->userId = 0;
- if (auth_userId && *auth_userId)
- {
- pass->userId = (char *)
- odr_malloc(c->odr_out, strlen(auth_userId)+1);
- strcpy(pass->userId, auth_userId);
- i++;
- }
- pass->password = 0;
- if (auth_password && *auth_password)
- {
- pass->password = (char *)
- odr_malloc(c->odr_out, strlen(auth_password)+1);
- strcpy(pass->password, auth_password);
- i++;
- }
- if (i)
- {
- auth->which = Z_IdAuthentication_idPass;
- auth->u.idPass = pass;
- ireq->idAuthentication = auth;
- }
- }
- else if (auth_userId)
- {
- auth->which = Z_IdAuthentication_open;
- auth->u.open = (char *)
- odr_malloc(c->odr_out, strlen(auth_userId)+1);
- strcpy(auth->u.open, auth_userId);
- ireq->idAuthentication = auth;
- }
- if (c->proxy)
- yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
- VAL_PROXY, 1, c->host_port);
- if (c->charset||c->lang)
- {
- Z_OtherInformation **oi;
- Z_OtherInformationUnit *oi_unit;
-
- yaz_oi_APDU(apdu, &oi);
-
- if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
- {
- ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
-
- oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
- oi_unit->information.externallyDefinedInfo =
- yaz_set_proposal_charneg
- (c->odr_out,
- (const char **)&c->charset, (c->charset) ? 1:0,
- (const char **)&c->lang, (c->lang) ? 1:0, 1);
- }
- }
- assert (apdu);
- return send_APDU (c, apdu);
-}
-
-#if HAVE_XML2
-static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr)
-{
- char ctype[50];
- Z_SOAP_Handler h[2] = {
- {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
- {0, 0, 0}
- };
- ODR o = odr_createmem(ODR_ENCODE);
- int ret;
- Z_SOAP *p = odr_malloc(o, sizeof(*p));
- Z_GDU *gdu;
- ZOOM_Event event;
-
- gdu = z_get_HTTP_Request(c->odr_out);
- gdu->u.HTTP_Request->path = c->path;
-
- if (c->host_port)
- {
- const char *cp0 = strstr(c->host_port, "://");
- const char *cp1 = 0;
- if (cp0)
- cp0 = cp0+3;
- else
- cp0 = c->host_port;
-
- cp1 = strchr(cp0, '/');
- if (!cp1)
- cp1 = cp0+strlen(cp0);
-
- if (cp0 && cp1)
- {
- char *h = odr_malloc(c->odr_out, cp1 - cp0 + 1);
- memcpy (h, cp0, cp1 - cp0);
- h[cp1-cp0] = '\0';
- z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
- "Host", h);
- }
- }
-
- strcpy(ctype, "text/xml");
- if (c->charset && strlen(c->charset) < 20)
- {
- strcat(ctype, "; charset=");
- strcat(ctype, c->charset);
- }
- z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
- "Content-Type", ctype);
- z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers,
- "SOAPAction", "\"\"");
- p->which = Z_SOAP_generic;
- p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
- p->u.generic->no = 0;
- p->u.generic->ns = 0;
- p->u.generic->p = sr;
- p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
-
- ret = z_soap_codec_enc(o, &p,
- &gdu->u.HTTP_Request->content_buf,
- &gdu->u.HTTP_Request->content_len, h,
- c->charset);
-
- if (!z_GDU(c->odr_out, &gdu, 0, 0))
- return zoom_complete;
- c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
-
- odr_destroy(o);
-
- event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event (c, event);
- odr_reset(c->odr_out);
- return do_write (c);
+ZOOM_API(const char*)
+ ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
+ *freq = field->facet_terms[idx].frequency;
+ return field->facet_terms[idx].term;
}
-#endif
-
-#if HAVE_XML2
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
-{
- int i;
- ZOOM_resultset resultset = 0;
- Z_SRW_PDU *sr = 0;
- const char *recordPacking = 0;
-
- if (c->error) /* don't continue on error */
- return zoom_complete;
- assert (c->tasks);
- if (c->tasks->which == ZOOM_TASK_SEARCH)
- {
- resultset = c->tasks->u.search.resultset;
- resultset->setname = xstrdup ("default");
- ZOOM_options_set (resultset->options, "setname", resultset->setname);
- }
- else if(c->tasks->which == ZOOM_TASK_RETRIEVE)
- {
- resultset = c->tasks->u.retrieve.resultset;
-
- resultset->start = c->tasks->u.retrieve.start;
- resultset->count = c->tasks->u.retrieve.count;
-
- if (resultset->start >= resultset->size)
- return zoom_complete;
- if (resultset->start + resultset->count > resultset->size)
- resultset->count = resultset->size - resultset->start;
- for (i = 0; i<resultset->count; i++)
- {
- ZOOM_record rec =
- record_cache_lookup (resultset, i + resultset->start);
- if (!rec)
- break;
- }
- if (i == resultset->count)
- return zoom_complete;
- }
- assert(resultset->query);
-
- sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
- if (resultset->query->z_query->which == Z_Query_type_104
- && resultset->query->z_query->u.type_104->which == Z_External_CQL)
- {
+static void get_cert(ZOOM_connection c)
+{
+ char *cert_buf;
+ int cert_len;
- sr->u.request->query_type = Z_SRW_query_type_cql;
- sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql;
- }
- else if (resultset->query->z_query->which == Z_Query_type_1 &&
- resultset->query->z_query->u.type_1)
+ if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
{
- sr->u.request->query_type = Z_SRW_query_type_pqf;
- sr->u.request->query.pqf = resultset->query->query_string;
+ ZOOM_connection_option_setl(c, "sslPeerCert",
+ cert_buf, cert_len);
+ xfree(cert_buf);
}
- else
- {
- set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
- return zoom_complete;
- }
- sr->u.request->startRecord = odr_intdup (c->odr_out, resultset->start + 1);
- sr->u.request->maximumRecords = odr_intdup (
- c->odr_out, resultset->step>0 ? resultset->step : resultset->count);
- sr->u.request->recordSchema = resultset->schema;
+}
- recordPacking = ZOOM_resultset_option_get (resultset, "recordPacking");
+static zoom_ret do_connect_host(ZOOM_connection c,
+ const char *logical_url);
- if (recordPacking)
- sr->u.request->recordPacking = odr_strdup(c->odr_out, recordPacking);
-
- return send_srw(c, sr);
-}
-#else
-static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
+static zoom_ret do_connect(ZOOM_connection c)
{
- return zoom_complete;
+ return do_connect_host(c, c->host_port);
}
-#endif
-static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
+static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
{
- ZOOM_resultset r;
- int lslb, ssub, mspn;
- const char *syntax;
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
- Z_SearchRequest *search_req = apdu->u.searchRequest;
- const char *elementSetName;
- const char *smallSetElementSetName;
- const char *mediumSetElementSetName;
-
- assert (c->tasks);
- assert (c->tasks->which == ZOOM_TASK_SEARCH);
-
- r = c->tasks->u.search.resultset;
-
- elementSetName =
- ZOOM_options_get (r->options, "elementSetName");
- smallSetElementSetName =
- ZOOM_options_get (r->options, "smallSetElementSetName");
- mediumSetElementSetName =
- ZOOM_options_get (r->options, "mediumSetElementSetName");
-
- if (!smallSetElementSetName)
- smallSetElementSetName = elementSetName;
-
- if (!mediumSetElementSetName)
- mediumSetElementSetName = elementSetName;
-
- assert (r);
- assert (r->query);
-
- /* prepare query for the search request */
- search_req->query = r->query->z_query;
-
- search_req->databaseNames =
- set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
+ void *add;
- /* get syntax (no need to provide unless piggyback is in effect) */
- syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
+ if (c->cs)
+ cs_close(c->cs);
+ c->cs = cs_create_host_proxy(logical_url, 0, &add,
+ c->tproxy ? c->tproxy : c->proxy);
- lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
- ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
- mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
- if (lslb != -1 && ssub != -1 && mspn != -1)
- {
- /* So're a Z39.50 expert? Let's hope you don't do sort */
- *search_req->largeSetLowerBound = lslb;
- *search_req->smallSetUpperBound = ssub;
- *search_req->mediumSetPresentNumber = mspn;
- }
- else if (r->start == 0 && r->count > 0
- && r->piggyback && !r->r_sort_spec && !r->schema)
- {
- /* Regular piggyback - do it unless we're going to do sort */
- *search_req->largeSetLowerBound = 2000000000;
- *search_req->smallSetUpperBound = 1;
- *search_req->mediumSetPresentNumber = r->step>0 ? r->step : r->count;
- }
- else
- {
- /* non-piggyback. Need not provide elementsets or syntaxes .. */
- smallSetElementSetName = 0;
- mediumSetElementSetName = 0;
- syntax = 0;
- }
- if (smallSetElementSetName && *smallSetElementSetName)
- {
- Z_ElementSetNames *esn = (Z_ElementSetNames *)
- odr_malloc (c->odr_out, sizeof(*esn));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
- search_req->smallSetElementSetNames = esn;
- }
- if (mediumSetElementSetName && *mediumSetElementSetName)
+ if (c->cs && c->cs->protocol == PROTO_HTTP)
{
- Z_ElementSetNames *esn = (Z_ElementSetNames *)
- odr_malloc (c->odr_out, sizeof(*esn));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
- search_req->mediumSetElementSetNames = esn;
+#if YAZ_HAVE_XML2
+ c->proto = PROTO_HTTP;
+#else
+ ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
+ ZOOM_connection_close(c);
+ return zoom_complete;
+#endif
}
- if (syntax)
- search_req->preferredRecordSyntax =
- yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
-
- if (!r->setname)
+ if (c->cs)
{
- if (c->support_named_resultsets)
+ int ret = cs_connect(c->cs, add);
+ if (ret == 0)
{
- char setname[14];
- int ord;
- /* find the lowest unused ordinal so that we re-use
- result sets on the server. */
- for (ord = 1; ; ord++)
+ ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
+ ZOOM_connection_put_event(c, event);
+ get_cert(c);
+ if (c->proto == PROTO_Z3950)
+ ZOOM_connection_Z3950_send_init(c);
+ else
{
- ZOOM_resultset rp;
- sprintf (setname, "%d", ord);
- for (rp = c->resultsets; rp; rp = rp->next)
- if (rp->setname && !strcmp (rp->setname, setname))
- break;
- if (!rp)
- break;
+ /* no init request for SRW .. */
+ assert(c->tasks->which == ZOOM_TASK_CONNECT);
+ ZOOM_connection_remove_task(c);
+ ZOOM_connection_set_mask(c, 0);
+ ZOOM_connection_exec_task(c);
}
- r->setname = xstrdup (setname);
- yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
+ c->state = STATE_ESTABLISHED;
+ return zoom_pending;
+ }
+ else if (ret > 0)
+ {
+ int mask = ZOOM_SELECT_EXCEPT;
+ if (c->cs->io_pending & CS_WANT_WRITE)
+ mask += ZOOM_SELECT_WRITE;
+ if (c->cs->io_pending & CS_WANT_READ)
+ mask += ZOOM_SELECT_READ;
+ ZOOM_connection_set_mask(c, mask);
+ c->state = STATE_CONNECTING;
+ return zoom_pending;
}
- else
- r->setname = xstrdup ("default");
- ZOOM_options_set (r->options, "setname", r->setname);
- }
- search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
- /* send search request */
- return send_APDU (c, apdu);
-}
-
-static void response_diag (ZOOM_connection c, Z_DiagRec *p)
-{
- int oclass;
- Z_DefaultDiagFormat *r;
- char *addinfo = 0;
-
- xfree (c->addinfo);
- c->addinfo = 0;
- if (p->which != Z_DiagRec_defaultFormat)
- {
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- return;
- }
- r = p->u.defaultFormat;
- switch (r->which)
- {
- case Z_DefaultDiagFormat_v2Addinfo:
- addinfo = r->u.v2Addinfo;
- break;
- case Z_DefaultDiagFormat_v3Addinfo:
- addinfo = r->u.v3Addinfo;
- break;
}
- set_dset_error(c, *r->condition,
- yaz_z3950oid_to_str(r->diagnosticSetId, &oclass),
- addinfo, 0);
+ c->state = STATE_IDLE;
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
+ return zoom_complete;
}
-ZOOM_API(ZOOM_record)
-ZOOM_record_clone (ZOOM_record srec)
-{
- char *buf;
- int size;
- ODR odr_enc;
- ZOOM_record nrec;
-
- odr_enc = odr_createmem(ODR_ENCODE);
- if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
- return 0;
- buf = odr_getbuf (odr_enc, &size, 0);
-
- nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
- nrec->odr = odr_createmem(ODR_DECODE);
- nrec->wrbuf_marc = 0;
- nrec->wrbuf_iconv = 0;
- nrec->wrbuf_opac = 0;
- odr_setbuf (nrec->odr, buf, size, 0);
- z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
-
- odr_destroy (odr_enc);
- return nrec;
-}
+/* returns 1 if PDU was sent OK (still pending )
+ 0 if PDU was not sent OK (nothing to wait for)
+*/
ZOOM_API(ZOOM_record)
-ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
+ ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
{
- return record_cache_lookup (s, pos);
+ const char *syntax =
+ ZOOM_options_get(s->options, "preferredRecordSyntax");
+ const char *elementSetName =
+ ZOOM_options_get(s->options, "elementSetName");
+ const char *schema =
+ ZOOM_options_get(s->options, "schema");
+
+ return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
}
ZOOM_API(ZOOM_record)
-ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
+ ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
{
ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
if (!rec)
{
- ZOOM_resultset_retrieve (r, 1, pos, 1);
- rec = ZOOM_resultset_record_immediate (r, pos);
+ /*
+ * MIKE: I think force_sync should always be zero, but I don't
+ * want to make this change until I get the go-ahead from
+ * Adam, in case something depends on the old synchronous
+ * behaviour.
+ */
+ int force_sync = 1;
+ if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
+ ZOOM_resultset_retrieve(r, force_sync, pos, 1);
+ rec = ZOOM_resultset_record_immediate(r, pos);
}
return rec;
}
-ZOOM_API(void)
-ZOOM_record_destroy (ZOOM_record rec)
-{
- if (!rec)
- return;
- if (rec->wrbuf_marc)
- wrbuf_free (rec->wrbuf_marc, 1);
- if (rec->wrbuf_iconv)
- wrbuf_free (rec->wrbuf_iconv, 1);
- if (rec->wrbuf_opac)
- wrbuf_free (rec->wrbuf_opac, 1);
- odr_destroy (rec->odr);
- xfree (rec);
-}
-
-static const char *record_iconv_return(ZOOM_record rec, int *len,
- const char *buf, int sz,
- const char *record_charset)
-{
- char to[40];
- char from[40];
- yaz_iconv_t cd = 0;
-
- *from = '\0';
- strcpy(to, "UTF-8");
- if (record_charset && *record_charset)
- {
- /* Use "from,to" or just "from" */
- const char *cp =strchr(record_charset, ',');
- int clen = strlen(record_charset);
- if (cp && cp[1])
- {
- strncpy( to, cp+1, sizeof(to)-1);
- to[sizeof(to)-1] = '\0';
- clen = cp - record_charset;
- }
- if (clen > sizeof(from)-1)
- clen = sizeof(from)-1;
-
- if (clen)
- strncpy(from, record_charset, clen);
- from[clen] = '\0';
- }
-
- if (*from && *to && (cd = yaz_iconv_open(to, from)))
- {
- char outbuf[12];
- size_t inbytesleft = sz;
- const char *inp = buf;
-
- if (!rec->wrbuf_iconv)
- rec->wrbuf_iconv = wrbuf_alloc();
-
- wrbuf_rewind(rec->wrbuf_iconv);
-
- while (inbytesleft)
- {
- size_t outbytesleft = sizeof(outbuf);
- char *outp = outbuf;
- size_t r = yaz_iconv (cd, (char**) &inp,
- &inbytesleft,
- &outp, &outbytesleft);
- if (r == (size_t) (-1))
- {
- int e = yaz_iconv_error(cd);
- if (e != YAZ_ICONV_E2BIG)
- break;
- }
- wrbuf_write(rec->wrbuf_iconv, outbuf, outp - outbuf);
- }
- wrbuf_puts(rec->wrbuf_iconv, "");
- buf = wrbuf_buf(rec->wrbuf_iconv);
- sz = wrbuf_len(rec->wrbuf_iconv);
- yaz_iconv_close(cd);
- }
- if (len)
- *len = sz;
- return buf;
-}
-
-ZOOM_API(const char *)
-ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
-{
- char type[40];
- char charset[40];
- const char *cp;
- int i;
- Z_NamePlusRecord *npr;
-
- if (len)
- *len = 0; /* default return */
-
- if (!rec)
- return 0;
- npr = rec->npr;
- if (!npr)
- return 0;
-
- cp = type_spec;
- for (i = 0; cp[i] && i < sizeof(type)-1; i++)
- {
- if (cp[i] == ';' || cp[i] == ' ')
- break;
- type[i] = cp[i];
- }
- type[i] = '\0';
- charset[0] = '\0';
- if (type_spec[i] == ';')
- {
- i++;
- while (type_spec[i] == ' ')
- i++;
- if (!strncmp(type_spec+i, "charset=", 8))
- {
- cp = type_spec+i+8;
- for (i = 0; cp[i] && i < sizeof(charset)-1; i++)
- {
- if (cp[i] == ';' || cp[i] == ' ')
- break;
- charset[i] = cp[i];
- }
- charset[i] = '\0';
- }
- }
-
- if (!strcmp (type, "database"))
- {
- if (len)
- *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
- return npr->databaseName;
- }
- else if (!strcmp (type, "syntax"))
- {
- const char *desc = 0;
- if (npr->which == Z_NamePlusRecord_databaseRecord)
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
- if (ent)
- desc = ent->desc;
- }
- if (!desc)
- desc = "none";
- if (len)
- *len = strlen(desc);
- return desc;
- }
- else if (!strcmp (type, "render") &&
- npr->which == Z_NamePlusRecord_databaseRecord)
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
-
- /* render bibliographic record .. */
- if (r->which == Z_External_OPAC)
- {
- r = r->u.opac->bibliographicRecord;
- if (!r)
- return 0;
- ent = oid_getentbyoid(r->direct_reference);
- }
- if (r->which == Z_External_sutrs)
- return record_iconv_return(rec, len,
- r->u.sutrs->buf, r->u.sutrs->len,
- charset);
- else if (r->which == Z_External_octet)
- {
- yaz_marc_t mt;
- switch (ent->value)
- {
- case VAL_SOIF:
- case VAL_HTML:
- case VAL_SUTRS:
- break;
- case VAL_TEXT_XML:
- case VAL_APPLICATION_XML:
- break;
- default:
- if (!rec->wrbuf_marc)
- rec->wrbuf_marc = wrbuf_alloc();
-
- mt = yaz_marc_create();
- wrbuf_rewind (rec->wrbuf_marc);
- if (yaz_marc_decode_wrbuf (
- mt, (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- rec->wrbuf_marc) > 0)
- {
- yaz_marc_destroy(mt);
- return record_iconv_return(rec, len,
- wrbuf_buf(rec->wrbuf_marc),
- wrbuf_len(rec->wrbuf_marc),
- charset);
- }
- yaz_marc_destroy(mt);
- }
- return record_iconv_return(rec, len,
- (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- charset);
- }
- else if (r->which == Z_External_grs1)
- {
- if (!rec->wrbuf_marc)
- rec->wrbuf_marc = wrbuf_alloc();
- wrbuf_rewind (rec->wrbuf_marc);
- yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
- return record_iconv_return(rec, len,
- wrbuf_buf(rec->wrbuf_marc),
- wrbuf_len(rec->wrbuf_marc),
- charset);
- }
- return 0;
- }
- else if (npr->which == Z_NamePlusRecord_databaseRecord &&
- (!strcmp (type, "xml") || !strcmp(type, "oai")))
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- oident *ent = oid_getentbyoid(r->direct_reference);
-
- /* render bibliographic record .. */
- if (r->which == Z_External_OPAC)
- {
- r = r->u.opac->bibliographicRecord;
- if (!r)
- return 0;
- ent = oid_getentbyoid(r->direct_reference);
- }
-
- if (r->which == Z_External_sutrs)
- return record_iconv_return(rec, len,
- (const char *) r->u.sutrs->buf,
- r->u.sutrs->len,
- charset);
- else if (r->which == Z_External_octet)
- {
- yaz_marc_t mt;
- int marc_decode_type = YAZ_MARC_MARCXML;
-
- if (!strcmp(type, "oai"))
- marc_decode_type = YAZ_MARC_OAIMARC;
- switch (ent->value)
- {
- case VAL_SOIF:
- case VAL_HTML:
- case VAL_SUTRS:
- break;
- case VAL_TEXT_XML:
- case VAL_APPLICATION_XML:
- break;
- default:
- if (!rec->wrbuf_marc)
- rec->wrbuf_marc = wrbuf_alloc();
- wrbuf_rewind (rec->wrbuf_marc);
- mt = yaz_marc_create();
-
- yaz_marc_xml(mt, YAZ_MARC_MARCXML);
- if (yaz_marc_decode_wrbuf (
- mt, (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- rec->wrbuf_marc) > 0)
- {
- yaz_marc_destroy(mt);
- return record_iconv_return(rec, len,
- wrbuf_buf(rec->wrbuf_marc),
- wrbuf_len(rec->wrbuf_marc),
- charset);
- }
- yaz_marc_destroy(mt);
- }
- return record_iconv_return(rec, len,
- (const char *) r->u.octet_aligned->buf,
- r->u.octet_aligned->len,
- charset);
- }
- else if (r->which == Z_External_grs1)
- {
- if (len) *len = 5;
- return "GRS-1";
- }
- return 0;
- }
- else if (!strcmp (type, "raw"))
- {
- if (npr->which == Z_NamePlusRecord_databaseRecord)
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
-
- if (r->which == Z_External_sutrs)
- {
- if (len) *len = r->u.sutrs->len;
- return (const char *) r->u.sutrs->buf;
- }
- else if (r->which == Z_External_octet)
- {
- if (len) *len = r->u.octet_aligned->len;
- return (const char *) r->u.octet_aligned->buf;
- }
- else /* grs-1, explain, OPAC, ... */
- {
- if (len) *len = -1;
- return (const char *) npr->u.databaseRecord;
- }
- }
- return 0;
- }
- else if (!strcmp (type, "ext"))
- {
- if (npr->which == Z_NamePlusRecord_databaseRecord)
- return (const char *) npr->u.databaseRecord;
- return 0;
- }
- else if (npr->which == Z_NamePlusRecord_databaseRecord &&
- !strcmp (type, "opac"))
-
- {
- Z_External *r = (Z_External *) npr->u.databaseRecord;
- if (r->which == Z_External_OPAC)
- {
- if (!rec->wrbuf_opac)
- rec->wrbuf_opac = wrbuf_alloc();
- wrbuf_rewind (rec->wrbuf_opac);
- yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0);
- return record_iconv_return(rec, len,
- wrbuf_buf(rec->wrbuf_opac),
- wrbuf_len(rec->wrbuf_opac),
- charset);
- }
- }
- return 0;
-}
-
-static int strcmp_null(const char *v1, const char *v2)
-{
- if (!v1 && !v2)
- return 0;
- if (!v1 || !v2)
- return -1;
- return strcmp(v1, v2);
-}
-
-static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr,
- int pos)
-{
- ZOOM_record_cache rc;
- const char *elementSetName =
- ZOOM_resultset_option_get (r, "elementSetName");
- const char *syntax =
- ZOOM_resultset_option_get (r, "preferredRecordSyntax");
-
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
- ZOOM_connection_put_event(r->connection, event);
-
- for (rc = r->record_cache; rc; rc = rc->next)
- {
- if (pos == rc->pos)
- {
- if (strcmp_null(r->schema, rc->schema))
- continue;
- if (strcmp_null(elementSetName,rc->elementSetName))
- continue;
- if (strcmp_null(syntax, rc->syntax))
- continue;
- /* not destroying rc->npr (it's handled by nmem )*/
- rc->rec.npr = npr;
- /* keeping wrbuf_marc too */
- return;
- }
- }
- rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
- rc->rec.npr = npr;
- rc->rec.odr = 0;
- rc->rec.wrbuf_marc = 0;
- rc->rec.wrbuf_iconv = 0;
- rc->rec.wrbuf_opac = 0;
- if (elementSetName)
- rc->elementSetName = odr_strdup (r->odr, elementSetName);
- else
- rc->elementSetName = 0;
-
- if (syntax)
- rc->syntax = odr_strdup (r->odr, syntax);
- else
- rc->syntax = 0;
-
- if (r->schema)
- rc->schema = odr_strdup (r->odr, r->schema);
- else
- rc->schema = 0;
-
- rc->pos = pos;
- rc->next = r->record_cache;
- r->record_cache = rc;
-}
-
-static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
-{
- ZOOM_record_cache rc;
- const char *elementSetName =
- ZOOM_resultset_option_get (r, "elementSetName");
- const char *syntax =
- ZOOM_resultset_option_get (r, "preferredRecordSyntax");
-
- for (rc = r->record_cache; rc; rc = rc->next)
- {
- if (pos == rc->pos)
- {
- if (strcmp_null(r->schema, rc->schema))
- continue;
- if (strcmp_null(elementSetName,rc->elementSetName))
- continue;
- if (strcmp_null(syntax, rc->syntax))
- continue;
- return &rc->rec;
- }
- }
- return 0;
-}
-
-static void handle_records (ZOOM_connection c, Z_Records *sr,
- int present_phase)
-{
- ZOOM_resultset resultset;
-
- if (!c->tasks)
- return ;
- switch (c->tasks->which)
- {
- case ZOOM_TASK_SEARCH:
- resultset = c->tasks->u.search.resultset;
- break;
- case ZOOM_TASK_RETRIEVE:
- resultset = c->tasks->u.retrieve.resultset;
- break;
- default:
- return;
- }
- if (sr && sr->which == Z_Records_NSD)
- {
- Z_DiagRec dr, *dr_p = &dr;
- dr.which = Z_DiagRec_defaultFormat;
- dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
-
- response_diag (c, dr_p);
- }
- else if (sr && sr->which == Z_Records_multipleNSD)
- {
- if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
- response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
- else
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- }
- else
- {
- if (resultset->count + resultset->start > resultset->size)
- resultset->count = resultset->size - resultset->start;
- if (resultset->count < 0)
- resultset->count = 0;
- if (sr && sr->which == Z_Records_DBOSD)
- {
- int i;
- NMEM nmem = odr_extract_mem (c->odr_in);
- Z_NamePlusRecordList *p =
- sr->u.databaseOrSurDiagnostics;
- for (i = 0; i<p->num_records; i++)
- {
- record_cache_add (resultset, p->records[i],
- i+ resultset->start);
- }
- /* transfer our response to search_nmem .. we need it later */
- nmem_transfer (resultset->odr->mem, nmem);
- nmem_destroy (nmem);
- if (present_phase && p->num_records == 0)
- {
- /* present response and we didn't get any records! */
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- }
- }
- else if (present_phase)
- {
- /* present response and we didn't get any records! */
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- }
- }
-}
-
-static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
-{
- handle_records (c, pr->records, 1);
-}
-
-static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
-{
- ZOOM_resultset resultset;
- ZOOM_Event event;
-
- yaz_log (LOG_DEBUG, "got search response");
-
- if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
- return ;
-
- event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
- ZOOM_connection_put_event(c, event);
-
- resultset = c->tasks->u.search.resultset;
-
- resultset->size = *sr->resultCount;
- handle_records (c, sr->records, 0);
-}
-
-static void sort_response (ZOOM_connection c, Z_SortResponse *res)
-{
- if (res->diagnostics && res->num_diagnostics > 0)
- response_diag (c, res->diagnostics[0]);
-}
-
-static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
-{
- NMEM nmem = odr_extract_mem (c->odr_in);
- ZOOM_scanset scan;
-
- if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
- return 0;
- scan = c->tasks->u.scan.scan;
-
- if (res->entries && res->entries->nonsurrogateDiagnostics)
- response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
- scan->scan_response = res;
- nmem_transfer (scan->odr->mem, nmem);
- if (res->stepSize)
- ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
- if (res->positionOfTerm)
- ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
- if (res->scanStatus)
- ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
- if (res->numberOfEntriesReturned)
- ZOOM_options_set_int (scan->options, "number",
- *res->numberOfEntriesReturned);
- nmem_destroy (nmem);
- return 1;
-}
-
-static zoom_ret send_sort (ZOOM_connection c)
-{
- ZOOM_resultset resultset;
-
- if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
- return zoom_complete;
-
- resultset = c->tasks->u.search.resultset;
-
- if (c->error)
- {
- resultset->r_sort_spec = 0;
- return zoom_complete;
- }
- if (resultset->r_sort_spec)
- {
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
- Z_SortRequest *req = apdu->u.sortRequest;
-
- req->num_inputResultSetNames = 1;
- req->inputResultSetNames = (Z_InternationalString **)
- odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
- req->inputResultSetNames[0] =
- odr_strdup (c->odr_out, resultset->setname);
- req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
- req->sortSequence = resultset->r_sort_spec;
- resultset->r_sort_spec = 0;
- return send_APDU (c, apdu);
- }
- return zoom_complete;
-}
-
-static zoom_ret send_present (ZOOM_connection c)
+ZOOM_API(ZOOM_scanset)
+ ZOOM_connection_scan(ZOOM_connection c, const char *start)
{
- Z_APDU *apdu = 0;
- Z_PresentRequest *req = 0;
- int i = 0;
- const char *syntax = 0;
- const char *elementSetName = 0;
- ZOOM_resultset resultset;
-
- if (!c->tasks)
- return zoom_complete;
-
- switch (c->tasks->which)
- {
- case ZOOM_TASK_SEARCH:
- resultset = c->tasks->u.search.resultset;
- break;
- case ZOOM_TASK_RETRIEVE:
- resultset = c->tasks->u.retrieve.resultset;
- resultset->start = c->tasks->u.retrieve.start;
- resultset->count = c->tasks->u.retrieve.count;
-
- if (resultset->start >= resultset->size)
- return zoom_complete;
- if (resultset->start + resultset->count > resultset->size)
- resultset->count = resultset->size - resultset->start;
- break;
- default:
- return zoom_complete;
- }
-
- syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
- elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
-
- if (c->error) /* don't continue on error */
- return zoom_complete;
- if (resultset->start < 0)
- return zoom_complete;
- for (i = 0; i<resultset->count; i++)
- {
- ZOOM_record rec =
- record_cache_lookup (resultset, i + resultset->start);
- if (!rec)
- break;
- }
- if (i == resultset->count)
- return zoom_complete;
-
- apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
- req = apdu->u.presentRequest;
+ ZOOM_scanset s;
+ ZOOM_query q = ZOOM_query_create();
- resultset->start += i;
- resultset->count -= i;
- *req->resultSetStartPoint = resultset->start + 1;
- *req->numberOfRecordsRequested = resultset->step>0 ?
- resultset->step : resultset->count;
- assert (*req->numberOfRecordsRequested > 0);
+ ZOOM_query_prefix(q, start);
- if (syntax && *syntax)
- req->preferredRecordSyntax =
- yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
-
- if (resultset->schema && *resultset->schema)
- {
- Z_RecordComposition *compo = (Z_RecordComposition *)
- odr_malloc (c->odr_out, sizeof(*compo));
-
- req->recordComposition = compo;
- compo->which = Z_RecordComp_complex;
- compo->u.complex = (Z_CompSpec *)
- odr_malloc(c->odr_out, sizeof(*compo->u.complex));
- compo->u.complex->selectAlternativeSyntax = (bool_t *)
- odr_malloc(c->odr_out, sizeof(bool_t));
- *compo->u.complex->selectAlternativeSyntax = 0;
-
- compo->u.complex->generic = (Z_Specification *)
- odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
-
- compo->u.complex->generic->which = Z_Schema_oid;
- compo->u.complex->generic->schema.oid = (Odr_oid *)
- yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, resultset->schema);
-
- if (!compo->u.complex->generic->schema.oid)
- {
- /* OID wasn't a schema! Try record syntax instead. */
+ s = ZOOM_connection_scan1(c, q);
+ ZOOM_query_destroy(q);
+ return s;
- compo->u.complex->generic->schema.oid = (Odr_oid *)
- yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, resultset->schema);
- }
- if (elementSetName && *elementSetName)
- {
- compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
- odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
- compo->u.complex->generic->elementSpec->which =
- Z_ElementSpec_elementSetName;
- compo->u.complex->generic->elementSpec->u.elementSetName =
- odr_strdup (c->odr_out, elementSetName);
- }
- else
- compo->u.complex->generic->elementSpec = 0;
- compo->u.complex->num_dbSpecific = 0;
- compo->u.complex->dbSpecific = 0;
- compo->u.complex->num_recordSyntax = 0;
- compo->u.complex->recordSyntax = 0;
- }
- else if (elementSetName && *elementSetName)
- {
- Z_ElementSetNames *esn = (Z_ElementSetNames *)
- odr_malloc (c->odr_out, sizeof(*esn));
- Z_RecordComposition *compo = (Z_RecordComposition *)
- odr_malloc (c->odr_out, sizeof(*compo));
-
- esn->which = Z_ElementSetNames_generic;
- esn->u.generic = odr_strdup (c->odr_out, elementSetName);
- compo->which = Z_RecordComp_simple;
- compo->u.simple = esn;
- req->recordComposition = compo;
- }
- req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
- return send_APDU (c, apdu);
}
ZOOM_API(ZOOM_scanset)
-ZOOM_connection_scan (ZOOM_connection c, const char *start)
+ ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
{
- ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
+ ZOOM_scanset scan = 0;
+ Z_Query *z_query = ZOOM_query_get_Z_Query(q);
+ if (!z_query)
+ return 0;
+ scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
scan->connection = c;
- scan->odr = odr_createmem (ODR_DECODE);
- scan->options = ZOOM_options_create_with_parent (c->options);
+ scan->odr = odr_createmem(ODR_DECODE);
+ scan->options = ZOOM_options_create_with_parent(c->options);
scan->refcount = 1;
scan->scan_response = 0;
+ scan->srw_scan_response = 0;
+
+ scan->query = q;
+ ZOOM_query_addref(q);
+ scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
+ &scan->num_databaseNames,
+ scan->odr);
- if ((scan->termListAndStartPoint =
- p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
- start)))
+ if (1)
{
- ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
+ ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
task->u.scan.scan = scan;
-
+
(scan->refcount)++;
if (!c->async)
{
- while (ZOOM_event (1, &c))
+ while (ZOOM_event(1, &c))
;
}
}
}
ZOOM_API(void)
-ZOOM_scanset_destroy (ZOOM_scanset scan)
+ ZOOM_scanset_destroy(ZOOM_scanset scan)
{
if (!scan)
return;
(scan->refcount)--;
if (scan->refcount == 0)
{
- odr_destroy (scan->odr);
-
- ZOOM_options_destroy (scan->options);
- xfree (scan);
+ ZOOM_query_destroy(scan->query);
+
+ odr_destroy(scan->odr);
+
+ ZOOM_options_destroy(scan->options);
+ xfree(scan);
}
}
-static zoom_ret send_package (ZOOM_connection c)
+static zoom_ret send_package(ZOOM_connection c)
{
ZOOM_Event event;
- if (!c->tasks)
- return zoom_complete;
- assert (c->tasks->which == ZOOM_TASK_PACKAGE);
-
- event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
- ZOOM_connection_put_event (c, event);
-
- return do_write_ex (c, c->tasks->u.package->buf_out,
- c->tasks->u.package->len_out);
-}
-static zoom_ret send_scan (ZOOM_connection c)
-{
- ZOOM_scanset scan;
- Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
- Z_ScanRequest *req = apdu->u.scanRequest;
+ yaz_log(c->log_details, "%p send_package", c);
if (!c->tasks)
return zoom_complete;
- assert (c->tasks->which == ZOOM_TASK_SCAN);
- scan = c->tasks->u.scan.scan;
-
- req->termListAndStartPoint = scan->termListAndStartPoint;
- req->attributeSet = scan->attributeSet;
-
- *req->numberOfTermsRequested =
- ZOOM_options_get_int(scan->options, "number", 10);
+ assert (c->tasks->which == ZOOM_TASK_PACKAGE);
- req->preferredPositionInResponse =
- odr_intdup (c->odr_out,
- ZOOM_options_get_int(scan->options, "position", 1));
+ event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
+ ZOOM_connection_put_event(c, event);
- req->stepSize =
- odr_intdup (c->odr_out,
- ZOOM_options_get_int(scan->options, "stepSize", 0));
-
- req->databaseNames = set_DatabaseNames (c, scan->options,
- &req->num_databaseNames);
+ c->buf_out = c->tasks->u.package->buf_out;
+ c->len_out = c->tasks->u.package->len_out;
- return send_APDU (c, apdu);
+ return ZOOM_send_buf(c);
}
ZOOM_API(size_t)
-ZOOM_scanset_size (ZOOM_scanset scan)
+ ZOOM_scanset_size(ZOOM_scanset scan)
{
- if (!scan || !scan->scan_response || !scan->scan_response->entries)
+ if (!scan)
return 0;
- return scan->scan_response->entries->num_entries;
+
+ if (scan->scan_response && scan->scan_response->entries)
+ return scan->scan_response->entries->num_entries;
+ else if (scan->srw_scan_response)
+ return scan->srw_scan_response->num_terms;
+ return 0;
}
-ZOOM_API(const char *)
-ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
- int *occ, int *len)
-{
- const char *term = 0;
- size_t noent = ZOOM_scanset_size (scan);
- Z_ScanResponse *res = scan->scan_response;
-
- *len = 0;
+static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
+ size_t *occ,
+ const char **value_term, size_t *value_len,
+ const char **disp_term, size_t *disp_len)
+{
+ size_t noent = ZOOM_scanset_size(scan);
+
+ *value_term = 0;
+ *value_len = 0;
+
+ *disp_term = 0;
+ *disp_len = 0;
+
*occ = 0;
if (pos >= noent)
- return 0;
- if (res->entries->entries[pos]->which == Z_Entry_termInfo)
+ return;
+ if (scan->scan_response)
{
- Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
-
- if (t->term->which == Z_Term_general)
+ Z_ScanResponse *res = scan->scan_response;
+ if (res->entries->entries[pos]->which == Z_Entry_termInfo)
{
- term = (const char *) t->term->u.general->buf;
- *len = t->term->u.general->len;
+ Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
+
+ *value_term = (const char *) t->term->u.general->buf;
+ *value_len = t->term->u.general->len;
+ if (t->displayTerm)
+ {
+ *disp_term = t->displayTerm;
+ *disp_len = strlen(*disp_term);
+ }
+ else if (t->term->which == Z_Term_general)
+ {
+ *disp_term = (const char *) t->term->u.general->buf;
+ *disp_len = t->term->u.general->len;
+ }
+ *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
}
- *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
}
- return term;
-}
-
-ZOOM_API(const char *)
-ZOOM_scanset_display_term (ZOOM_scanset scan, size_t pos,
- int *occ, int *len)
-{
- const char *term = 0;
- size_t noent = ZOOM_scanset_size (scan);
- Z_ScanResponse *res = scan->scan_response;
-
- *len = 0;
- *occ = 0;
- if (pos >= noent)
- return 0;
- if (res->entries->entries[pos]->which == Z_Entry_termInfo)
+ if (scan->srw_scan_response)
{
- Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
-
- if (t->displayTerm)
- {
- term = (const char *) t->term->u.general->buf;
- *len = strlen(term);
- }
- else if (t->term->which == Z_Term_general)
+ Z_SRW_scanResponse *res = scan->srw_scan_response;
+ Z_SRW_scanTerm *t = res->terms + pos;
+ if (t)
{
- term = (const char *) t->term->u.general->buf;
- *len = t->term->u.general->len;
+ *value_term = t->value;
+ *value_len = strlen(*value_term);
+
+ if (t->displayTerm)
+ *disp_term = t->displayTerm;
+ else
+ *disp_term = t->value;
+ *disp_len = strlen(*disp_term);
+ *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
}
- *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
}
- return term;
}
ZOOM_API(const char *)
-ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
+ ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
+ size_t *occ, size_t *len)
{
- return ZOOM_options_get (scan->options, key);
+ const char *value_term = 0;
+ size_t value_len = 0;
+ const char *disp_term = 0;
+ size_t disp_len = 0;
+
+ ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
+ &disp_term, &disp_len);
+
+ *len = value_len;
+ return value_term;
}
-ZOOM_API(void)
-ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
- const char *val)
+ZOOM_API(const char *)
+ ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
+ size_t *occ, size_t *len)
{
- ZOOM_options_set (scan->options, key, val);
-}
-
-static Z_APDU *create_es_package (ZOOM_package p, int type)
-{
- const char *str;
- Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
- Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
-
- *req->function = Z_ExtendedServicesRequest_create;
-
- str = ZOOM_options_get(p->options, "package-name");
- if (str && *str)
- req->packageName = nmem_strdup (p->odr_out->mem, str);
-
- str = ZOOM_options_get(p->options, "user-id");
- if (str)
- req->userId = nmem_strdup (p->odr_out->mem, str);
-
- req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
- type);
-
- str = ZOOM_options_get(p->options, "function");
- if (str)
- {
- if (!strcmp (str, "create"))
- *req->function = 1;
- if (!strcmp (str, "delete"))
- *req->function = 2;
- if (!strcmp (str, "modify"))
- *req->function = 3;
- }
- return apdu;
-}
-
-static const char *ill_array_lookup (void *clientData, const char *idx)
-{
- ZOOM_package p = (ZOOM_package) clientData;
- return ZOOM_options_get (p->options, idx+4);
-}
-
-static Z_External *encode_ill_request (ZOOM_package p)
-{
- ODR out = p->odr_out;
- ILL_Request *req;
- Z_External *r = 0;
- struct ill_get_ctl ctl;
-
- ctl.odr = p->odr_out;
- ctl.clientData = p;
- ctl.f = ill_array_lookup;
-
- req = ill_get_ILLRequest(&ctl, "ill", 0);
-
- if (!ill_Request (out, &req, 0, 0))
- {
- int ill_request_size;
- char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
- if (ill_request_buf)
- odr_setbuf (out, ill_request_buf, ill_request_size, 1);
- return 0;
- }
- else
- {
- oident oid;
- int illRequest_size = 0;
- char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
-
- oid.proto = PROTO_GENERAL;
- oid.oclass = CLASS_GENERAL;
- oid.value = VAL_ISO_ILL_1;
-
- r = (Z_External *) odr_malloc (out, sizeof(*r));
- r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
- r->indirect_reference = 0;
- r->descriptor = 0;
- r->which = Z_External_single;
-
- r->u.single_ASN1_type = (Odr_oct *)
- odr_malloc (out, sizeof(*r->u.single_ASN1_type));
- r->u.single_ASN1_type->buf = (unsigned char*)
- odr_malloc (out, illRequest_size);
- r->u.single_ASN1_type->len = illRequest_size;
- r->u.single_ASN1_type->size = illRequest_size;
- memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
- }
- return r;
-}
+ const char *value_term = 0;
+ size_t value_len = 0;
+ const char *disp_term = 0;
+ size_t disp_len = 0;
-static Z_ItemOrder *encode_item_order(ZOOM_package p)
-{
- Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
- const char *str;
-
- req->which=Z_IOItemOrder_esRequest;
- req->u.esRequest = (Z_IORequest *)
- odr_malloc(p->odr_out,sizeof(Z_IORequest));
-
- /* to keep part ... */
- req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
- odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
- req->u.esRequest->toKeep->supplDescription = 0;
- req->u.esRequest->toKeep->contact = (Z_IOContact *)
- odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
-
- str = ZOOM_options_get(p->options, "contact-name");
- req->u.esRequest->toKeep->contact->name = str ?
- nmem_strdup (p->odr_out->mem, str) : 0;
-
- str = ZOOM_options_get(p->options, "contact-phone");
- req->u.esRequest->toKeep->contact->phone = str ?
- nmem_strdup (p->odr_out->mem, str) : 0;
-
- str = ZOOM_options_get(p->options, "contact-email");
- req->u.esRequest->toKeep->contact->email = str ?
- nmem_strdup (p->odr_out->mem, str) : 0;
-
- req->u.esRequest->toKeep->addlBilling = 0;
-
- /* not to keep part ... */
- req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
- odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
-
- str = ZOOM_options_get(p->options, "itemorder-setname");
- if (!str)
- str = "default";
-
- if (!*str)
- req->u.esRequest->notToKeep->resultSetItem = 0;
- else
- {
- req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
- odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
+ ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
+ &disp_term, &disp_len);
- req->u.esRequest->notToKeep->resultSetItem->resultSetId =
- nmem_strdup (p->odr_out->mem, str);
- req->u.esRequest->notToKeep->resultSetItem->item =
- (int *) odr_malloc(p->odr_out, sizeof(int));
-
- str = ZOOM_options_get(p->options, "itemorder-item");
- *req->u.esRequest->notToKeep->resultSetItem->item =
- (str ? atoi(str) : 1);
- }
- req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
-
- return req;
+ *len = disp_len;
+ return disp_term;
+}
+
+ZOOM_API(const char *)
+ ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
+{
+ return ZOOM_options_get(scan->options, key);
}
ZOOM_API(void)
- ZOOM_package_send (ZOOM_package p, const char *type)
+ ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
+ const char *val)
{
- Z_APDU *apdu = 0;
- ZOOM_connection c;
- if (!p)
- return;
- c = p->connection;
- odr_reset (p->odr_out);
- xfree (p->buf_out);
- p->buf_out = 0;
- if (!strcmp(type, "itemorder"))
- {
- Z_External *r;
- apdu = create_es_package (p, VAL_ITEMORDER);
- if (apdu)
- {
- r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
-
- r->direct_reference =
- yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
- VAL_ITEMORDER);
- r->descriptor = 0;
- r->which = Z_External_itemOrder;
- r->indirect_reference = 0;
- r->u.itemOrder = encode_item_order (p);
-
- apdu->u.extendedServicesRequest->taskSpecificParameters = r;
- }
- }
- if (apdu)
- {
- if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
- {
- char *buf;
-
- ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
- task->u.package = p;
- buf = odr_getbuf(p->odr_out, &p->len_out, 0);
- p->buf_out = (char *) xmalloc (p->len_out);
- memcpy (p->buf_out, buf, p->len_out);
-
- (p->refcount)++;
- if (!c->async)
- {
- while (ZOOM_event (1, &c))
- ;
- }
- }
- }
+ ZOOM_options_set(scan->options, key, val);
}
+
ZOOM_API(ZOOM_package)
- ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
+ ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
{
- ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
+ ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
p->connection = c;
- p->odr_out = odr_createmem (ODR_ENCODE);
- p->options = ZOOM_options_create_with_parent2 (options, c->options);
+ p->odr_out = odr_createmem(ODR_ENCODE);
+ p->options = ZOOM_options_create_with_parent2(options, c->options);
p->refcount = 1;
p->buf_out = 0;
p->len_out = 0;
(p->refcount)--;
if (p->refcount == 0)
{
- odr_destroy (p->odr_out);
- xfree (p->buf_out);
-
- ZOOM_options_destroy (p->options);
- xfree (p);
+ odr_destroy(p->odr_out);
+ xfree(p->buf_out);
+
+ ZOOM_options_destroy(p->options);
+ xfree(p);
}
}
ZOOM_API(const char *)
-ZOOM_package_option_get (ZOOM_package p, const char *key)
+ ZOOM_package_option_get(ZOOM_package p, const char *key)
{
- return ZOOM_options_get (p->options, key);
+ return ZOOM_options_get(p->options, key);
}
+ZOOM_API(const char *)
+ ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
+{
+ return ZOOM_options_getl(p->options, key, lenp);
+}
ZOOM_API(void)
-ZOOM_package_option_set (ZOOM_package p, const char *key,
- const char *val)
+ ZOOM_package_option_set(ZOOM_package p, const char *key,
+ const char *val)
+{
+ ZOOM_options_set(p->options, key, val);
+}
+
+ZOOM_API(void)
+ ZOOM_package_option_setl(ZOOM_package p, const char *key,
+ const char *val, int len)
{
- ZOOM_options_set (p->options, key, val);
+ ZOOM_options_setl(p->options, key, val, len);
}
-static int ZOOM_connection_exec_task (ZOOM_connection c)
+ZOOM_API(int)
+ ZOOM_connection_exec_task(ZOOM_connection c)
{
ZOOM_task task = c->tasks;
zoom_ret ret = zoom_complete;
if (!task)
- {
- yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
- return 0;
- }
- yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
- task->which, task->running);
+ return 0;
+ yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
+ c, task->which, task->running);
if (c->error != ZOOM_ERROR_NONE)
{
- yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
- ZOOM_connection_remove_tasks (c);
+ yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
+ "removing tasks because of error = %d", c, c->error);
+ ZOOM_connection_remove_tasks(c);
return 0;
}
if (task->running)
{
- yaz_log (LOG_DEBUG, "task already running");
- return 0;
+ yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
+ "task already running", c);
+ return 0;
}
task->running = 1;
ret = zoom_complete;
if (c->proto == PROTO_HTTP)
ret = ZOOM_connection_srw_send_search(c);
else
- ret = ZOOM_connection_send_search(c);
+ ret = ZOOM_connection_Z3950_send_search(c);
break;
case ZOOM_TASK_RETRIEVE:
if (c->proto == PROTO_HTTP)
ret = ZOOM_connection_srw_send_search(c);
else
- ret = send_present (c);
+ ret = send_Z3950_present(c);
break;
case ZOOM_TASK_CONNECT:
ret = do_connect(c);
break;
case ZOOM_TASK_SCAN:
- ret = send_scan(c);
+ if (c->proto == PROTO_HTTP)
+ ret = ZOOM_connection_srw_send_scan(c);
+ else
+ ret = ZOOM_connection_Z3950_send_scan(c);
break;
case ZOOM_TASK_PACKAGE:
ret = send_package(c);
break;
+ case ZOOM_TASK_SORT:
+ c->tasks->u.sort.resultset->r_sort_spec =
+ ZOOM_query_get_sortspec(c->tasks->u.sort.q);
+ ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
+ break;
}
}
else
{
- yaz_log (LOG_DEBUG, "remove tasks because no connection exist");
- ZOOM_connection_remove_tasks (c);
+ yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
+ "remove tasks because no connection exist", c);
+ ZOOM_connection_remove_tasks(c);
}
if (ret == zoom_complete)
{
- yaz_log (LOG_DEBUG, "task removed (complete)");
- ZOOM_connection_remove_task (c);
+ yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
+ "task removed (complete)", c);
+ ZOOM_connection_remove_task(c);
return 0;
}
- yaz_log (LOG_DEBUG, "task pending");
+ yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
+ "task pending", c);
return 1;
}
-static zoom_ret send_sort_present (ZOOM_connection c)
-{
- zoom_ret r = send_sort (c);
- if (r == zoom_complete)
- r = send_present (c);
- return r;
-}
+#if YAZ_HAVE_XML2
-static int es_response (ZOOM_connection c,
- Z_ExtendedServicesResponse *res)
+static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
{
- if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
- return 0;
- if (res->diagnostics && res->num_diagnostics > 0)
- response_diag(c, res->diagnostics[0]);
- if (res->taskPackage &&
- res->taskPackage->which == Z_External_extendedService)
- {
- Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
- Odr_oct *id = taskPackage->targetReference;
-
- if (id)
- ZOOM_options_setl (c->tasks->u.package->options,
- "targetReference", (char*) id->buf, id->len);
- }
- return 1;
-}
-
+ Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
-static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
-{
- Z_InitResponse *initrs;
-
- c->mask = 0;
- yaz_log (LOG_DEBUG, "recv APDU type=%d", apdu->which);
- switch(apdu->which)
- {
- case Z_APDU_initResponse:
- initrs = apdu->u.initResponse;
- ZOOM_connection_option_set(c, "targetImplementationId",
- initrs->implementationId ?
- initrs->implementationId : "");
- ZOOM_connection_option_set(c, "targetImplementationName",
- initrs->implementationName ?
- initrs->implementationName : "");
- ZOOM_connection_option_set(c, "targetImplementationVersion",
- initrs->implementationVersion ?
- initrs->implementationVersion : "");
- if (!*initrs->result)
- {
- set_ZOOM_error(c, ZOOM_ERROR_INIT, 0);
- }
- else
- {
- char *cookie =
- yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
- VAL_COOKIE, 1, 0);
- xfree (c->cookie_in);
- c->cookie_in = 0;
- if (cookie)
- c->cookie_in = xstrdup(cookie);
- if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
- ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
- c->support_named_resultsets = 1;
- if (c->tasks)
- {
- assert (c->tasks->which == ZOOM_TASK_CONNECT);
- ZOOM_connection_remove_task (c);
- }
- ZOOM_connection_exec_task (c);
- }
- if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
- {
- NMEM tmpmem = nmem_create();
- Z_CharSetandLanguageNegotiation *p =
- yaz_get_charneg_record(initrs->otherInfo);
-
- if (p)
- {
- char *charset=NULL, *lang=NULL;
- int sel;
-
- yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
- yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
- "language %s, select %d",
- charset ? charset : "none", lang ? lang : "none", sel);
- if (charset)
- ZOOM_connection_option_set (c, "negotiation-charset",
- charset);
- if (lang)
- ZOOM_connection_option_set (c, "negotiation-lang",
- lang);
- nmem_destroy(tmpmem);
- }
- }
- break;
- case Z_APDU_searchResponse:
- handle_search_response (c, apdu->u.searchResponse);
- if (send_sort_present (c) == zoom_complete)
- ZOOM_connection_remove_task (c);
- break;
- case Z_APDU_presentResponse:
- handle_present_response (c, apdu->u.presentResponse);
- if (send_present (c) == zoom_complete)
- ZOOM_connection_remove_task (c);
- break;
- case Z_APDU_sortResponse:
- sort_response (c, apdu->u.sortResponse);
- if (send_present (c) == zoom_complete)
- ZOOM_connection_remove_task (c);
- break;
- case Z_APDU_scanResponse:
- scan_response (c, apdu->u.scanResponse);
- ZOOM_connection_remove_task (c);
- break;
- case Z_APDU_extendedServicesResponse:
- es_response (c, apdu->u.extendedServicesResponse);
- ZOOM_connection_remove_task (c);
- break;
- case Z_APDU_close:
- if (c->reconnect_ok)
- {
- do_close(c);
- c->tasks->running = 0;
- ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
- }
- else
- {
- set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
- do_close(c);
- }
- break;
- default:
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- do_close(c);
+ gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
+ z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
+ "text/xml");
+ yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
+ if (c->user && c->password)
+ {
+ z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
+ c->user, c->password);
}
+ return ZOOM_send_GDU(c, gdu);
}
-#if HAVE_XML2
-static void handle_srw_response(ZOOM_connection c,
- Z_SRW_searchRetrieveResponse *res)
+zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
{
- ZOOM_resultset resultset = 0;
- int i;
- NMEM nmem;
ZOOM_Event event;
- if (!c->tasks)
- return;
-
- if (c->tasks->which == ZOOM_TASK_SEARCH)
- resultset = c->tasks->u.search.resultset;
- else if (c->tasks->which == ZOOM_TASK_RETRIEVE)
- resultset = c->tasks->u.retrieve.resultset;
- else
- return ;
+ int r = z_GDU(c->odr_out, &gdu, 0, 0);
+ if (!r)
+ return zoom_complete;
+ if (c->odr_print)
+ z_GDU(c->odr_print, &gdu, 0, 0);
+ if (c->odr_save)
+ z_GDU(c->odr_save, &gdu, 0, 0);
+ c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
+ odr_reset(c->odr_out);
- event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
+ event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
ZOOM_connection_put_event(c, event);
- resultset->size = 0;
-
- yaz_log(LOG_DEBUG, "got SRW response OK");
-
- if (res->numberOfRecords)
- resultset->size = *res->numberOfRecords;
-
- for (i = 0; i<res->num_records; i++)
- {
- int pos;
-
- Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
- odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
+ return ZOOM_send_buf(c);
+}
- if (res->records[i].recordPosition &&
- *res->records[i].recordPosition > 0)
- pos = *res->records[i].recordPosition - 1;
- else
- pos = resultset->start + i;
-
- npr->databaseName = 0;
- npr->which = Z_NamePlusRecord_databaseRecord;
- npr->u.databaseRecord = (Z_External *)
- odr_malloc(c->odr_in, sizeof(Z_External));
- npr->u.databaseRecord->descriptor = 0;
- npr->u.databaseRecord->direct_reference =
- yaz_oidval_to_z3950oid(c->odr_in, CLASS_RECSYN, VAL_TEXT_XML);
- npr->u.databaseRecord->which = Z_External_octet;
- npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
- odr_malloc(c->odr_in, sizeof(Odr_oct));
- npr->u.databaseRecord->u.octet_aligned->buf =
- res->records[i].recordData_buf;
- npr->u.databaseRecord->u.octet_aligned->len =
- npr->u.databaseRecord->u.octet_aligned->size =
- res->records[i].recordData_len;
- record_cache_add (resultset, npr, pos);
- }
- if (res->num_diagnostics > 0)
- {
- set_dset_error(c, *res->diagnostics[0].code, "SRW",
- res->diagnostics[0].details, 0);
- }
- nmem = odr_extract_mem(c->odr_in);
- nmem_transfer(resultset->odr->mem, nmem);
- nmem_destroy(nmem);
+#if YAZ_HAVE_XML2
+void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
+ const char *addinfo, const char *addinfo2)
+{
+ ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
}
#endif
-#if HAVE_XML2
+
static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
{
+ zoom_ret cret = zoom_complete;
int ret = -1;
- const char *content_type = z_HTTP_header_lookup(hres->headers,
- "Content-Type");
+ char *addinfo = 0;
const char *connection_head = z_HTTP_header_lookup(hres->headers,
"Connection");
- c->mask = 0;
- yaz_log (LOG_DEBUG, "handle_http");
-
- if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
- {
- Z_SOAP *soap_package = 0;
- ODR o = odr_createmem(ODR_DECODE);
- Z_SOAP_Handler soap_handlers[2] = {
- {"http://www.loc.gov/zing/srw/v1.0/", 0,
- (Z_SOAP_fun) yaz_srw_codec},
- {0, 0, 0}
- };
- ret = z_soap_codec(o, &soap_package,
- &hres->content_buf, &hres->content_len,
- soap_handlers);
- if (!ret && soap_package->which == Z_SOAP_generic &&
- soap_package->u.generic->no == 0)
+ const char *location;
+
+ ZOOM_connection_set_mask(c, 0);
+ yaz_log(c->log_details, "%p handle_http", c);
+
+ yaz_cookies_response(c->cookies, hres);
+ if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
+ && (location = z_HTTP_header_lookup(hres->headers, "Location")))
+ {
+ c->no_redirects++;
+ if (c->no_redirects > 10)
{
- Z_SRW_PDU *sr = soap_package->u.generic->p;
- if (sr->which == Z_SRW_searchRetrieve_response)
- handle_srw_response(c, sr->u.response);
- else
- ret = -1;
+ ZOOM_set_HTTP_error(c, hres->code, 0, 0);
+ c->no_redirects = 0;
+ ZOOM_connection_close(c);
}
- else if (!ret && (soap_package->which == Z_SOAP_fault
- || soap_package->which == Z_SOAP_error))
+ else
{
- set_HTTP_error(c, hres->code,
- soap_package->u.fault->fault_code,
- soap_package->u.fault->fault_string);
+ /* since redirect may change host we just reconnect. A smarter
+ implementation might check whether it's the same server */
+
+ int host_change = 0;
+ location = yaz_check_location(c->odr_in, c->host_port,
+ location, &host_change);
+ if (do_connect_host(c, location) == zoom_complete)
+ return; /* connect failed.. */
+ send_HTTP_redirect(c, location);
+ return;
}
- else
- ret = -1;
- odr_destroy(o);
+ }
+ else
+ {
+ ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
+ if (ret == 0)
+ {
+ if (c->no_redirects) /* end of redirect. change hosts again */
+ ZOOM_connection_close(c);
+ }
+ c->no_redirects = 0;
}
if (ret)
{
if (hres->code != 200)
- set_HTTP_error(c, hres->code, 0, 0);
+ ZOOM_set_HTTP_error(c, hres->code, 0, 0);
else
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- do_close (c);
+ {
+ yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
+ addinfo : "NULL");
+ ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
+ }
+ ZOOM_connection_close(c);
}
- ZOOM_connection_remove_task(c);
- if (!strcmp(hres->version, "1.0"))
+ if (cret == zoom_complete)
{
- /* HTTP 1.0: only if Keep-Alive we stay alive.. */
- if (!connection_head || strcmp(connection_head, "Keep-Alive"))
- do_close(c);
+ yaz_log(c->log_details, "removing tasks in handle_http");
+ ZOOM_connection_remove_task(c);
}
- else
{
- /* HTTP 1.1: only if no close we stay alive .. */
- if (connection_head && !strcmp(connection_head, "close"))
- do_close(c);
+ int must_close = 0;
+ if (!strcmp(hres->version, "1.0"))
+ {
+ /* HTTP 1.0: only if Keep-Alive we stay alive.. */
+ if (!connection_head || strcmp(connection_head, "Keep-Alive"))
+ must_close = 1;
+ }
+ else
+ {
+ /* HTTP 1.1: only if no close we stay alive.. */
+ if (connection_head && !strcmp(connection_head, "close"))
+ must_close = 1;
+ }
+ if (must_close)
+ {
+ ZOOM_connection_close(c);
+ if (c->tasks)
+ {
+ c->tasks->running = 0;
+ ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
+ c->reconnect_ok = 0;
+ }
+ }
+ else
+ c->reconnect_ok = 1; /* if the server closes anyway */
}
}
#endif
-static int do_read (ZOOM_connection c)
+static int do_read(ZOOM_connection c)
{
int r, more;
ZOOM_Event event;
-
- event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
- ZOOM_connection_put_event (c, event);
-
- r = cs_get (c->cs, &c->buf_in, &c->len_in);
+ event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
+ ZOOM_connection_put_event(c, event);
+
+ r = cs_get(c->cs, &c->buf_in, &c->len_in);
more = cs_more(c->cs);
- yaz_log (LOG_DEBUG, "do_read len=%d more=%d", r, more);
+ yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
if (r == 1)
- return 0;
+ return 0;
if (r <= 0)
{
- if (c->reconnect_ok)
- {
- do_close (c);
- c->reconnect_ok = 0;
- yaz_log (LOG_DEBUG, "reconnect read");
- c->tasks->running = 0;
- ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
- }
- else
+ if (!ZOOM_test_reconnect(c))
{
- set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
- do_close (c);
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
+ ZOOM_connection_close(c);
}
}
else
Z_GDU *gdu;
ZOOM_Event event;
- odr_reset (c->odr_in);
- odr_setbuf (c->odr_in, c->buf_in, r, 0);
- event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
- ZOOM_connection_put_event (c, event);
-
- if (!z_GDU (c->odr_in, &gdu, 0, 0))
- {
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- do_close (c);
- }
- else if (gdu->which == Z_GDU_Z3950)
- handle_apdu (c, gdu->u.z3950);
- else if (gdu->which == Z_GDU_HTTP_Response)
+ odr_reset(c->odr_in);
+ odr_setbuf(c->odr_in, c->buf_in, r, 0);
+ event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
+ ZOOM_connection_put_event(c, event);
+
+ if (!z_GDU(c->odr_in, &gdu, 0, 0))
+ {
+ int x;
+ int err = odr_geterrorx(c->odr_in, &x);
+ char msg[100];
+ const char *element = odr_getelement(c->odr_in);
+ yaz_snprintf(msg, sizeof(msg),
+ "ODR code %d:%d element=%s offset=%d",
+ err, x, element ? element : "<unknown>",
+ odr_offset(c->odr_in));
+ ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
+ if (c->log_api)
+ {
+ FILE *ber_file = yaz_log_file();
+ if (ber_file)
+ odr_dumpBER(ber_file, c->buf_in, r);
+ }
+ ZOOM_connection_close(c);
+ }
+ else
{
-#if HAVE_XML2
- handle_http (c, gdu->u.HTTP_Response);
+ if (c->odr_print)
+ z_GDU(c->odr_print, &gdu, 0, 0);
+ if (c->odr_save)
+ z_GDU(c->odr_save, &gdu, 0, 0);
+ if (gdu->which == Z_GDU_Z3950)
+ ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
+ else if (gdu->which == Z_GDU_HTTP_Response)
+ {
+#if YAZ_HAVE_XML2
+ handle_http(c, gdu->u.HTTP_Response);
#else
- set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
- do_close (c);
+ ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
+ ZOOM_connection_close(c);
#endif
+ }
}
- c->reconnect_ok = 0;
}
return 1;
}
-static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
+static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
{
int r;
ZOOM_Event event;
-
+
event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
- ZOOM_connection_put_event (c, event);
+ ZOOM_connection_put_event(c, event);
- yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
- if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
+ yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
+ if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
{
- if (c->reconnect_ok)
+ yaz_log(c->log_details, "%p do_write_ex write failed", c);
+ if (ZOOM_test_reconnect(c))
{
- do_close (c);
- c->reconnect_ok = 0;
- yaz_log (LOG_DEBUG, "reconnect write");
- c->tasks->running = 0;
- ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
return zoom_pending;
}
- if (c->state == STATE_CONNECTING)
- set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
- else
- set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
- do_close (c);
- return zoom_complete;
+ if (c->state == STATE_CONNECTING)
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
+ else
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
+ ZOOM_connection_close(c);
+ return zoom_complete;
}
else if (r == 1)
- {
- c->mask = ZOOM_SELECT_EXCEPT;
+ {
+ int mask = ZOOM_SELECT_EXCEPT;
if (c->cs->io_pending & CS_WANT_WRITE)
- c->mask += ZOOM_SELECT_WRITE;
+ mask += ZOOM_SELECT_WRITE;
if (c->cs->io_pending & CS_WANT_READ)
- c->mask += ZOOM_SELECT_READ;
- yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
+ mask += ZOOM_SELECT_READ;
+ ZOOM_connection_set_mask(c, mask);
+ yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
+ c, c->mask);
}
else
{
- c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
- yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
+ ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
+ yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
+ c, c->mask);
}
return zoom_pending;
}
-static zoom_ret do_write(ZOOM_connection c)
+zoom_ret ZOOM_send_buf(ZOOM_connection c)
{
- return do_write_ex (c, c->buf_out, c->len_out);
+ return do_write_ex(c, c->buf_out, c->len_out);
}
ZOOM_API(const char *)
-ZOOM_connection_option_get (ZOOM_connection c, const char *key)
+ ZOOM_connection_option_get(ZOOM_connection c, const char *key)
+{
+ if (!strcmp(key, "APDU"))
+ {
+ return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
+ }
+ else
+ return ZOOM_options_get(c->options, key);
+}
+
+ZOOM_API(const char *)
+ ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
{
- return ZOOM_options_get (c->options, key);
+ if (!strcmp(key, "APDU"))
+ {
+ if (c->saveAPDU_wrbuf)
+ {
+ *lenp = wrbuf_len(c->saveAPDU_wrbuf);
+ return wrbuf_cstr(c->saveAPDU_wrbuf);
+ }
+ else
+ {
+ *lenp = 0;
+ return "";
+ }
+ }
+ else
+ return ZOOM_options_getl(c->options, key, lenp);
+}
+
+ZOOM_API(void)
+ ZOOM_connection_option_set(ZOOM_connection c, const char *key,
+ const char *val)
+{
+ if (!strcmp(key, "saveAPDU"))
+ {
+ if (val && strcmp(val, "0"))
+ {
+ if (!c->saveAPDU_wrbuf)
+ c->saveAPDU_wrbuf = wrbuf_alloc();
+ else
+ wrbuf_rewind(c->saveAPDU_wrbuf);
+ }
+ else
+ {
+ wrbuf_destroy(c->saveAPDU_wrbuf);
+ c->saveAPDU_wrbuf = 0;
+ }
+ ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
+ }
+ else
+ ZOOM_options_set(c->options, key, val);
}
ZOOM_API(void)
-ZOOM_connection_option_set (ZOOM_connection c, const char *key,
- const char *val)
+ ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
+ const char *val, int len)
{
- ZOOM_options_set (c->options, key, val);
+ ZOOM_options_setl(c->options, key, val, len);
}
ZOOM_API(const char *)
-ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
+ ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
{
- return ZOOM_options_get (r->options, key);
+ return ZOOM_options_get(r->options, key);
}
ZOOM_API(void)
-ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
- const char *val)
+ ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
+ const char *val)
{
- ZOOM_options_set (r->options, key, val);
+ ZOOM_options_set(r->options, key, val);
}
ZOOM_API(int)
-ZOOM_connection_errcode (ZOOM_connection c)
+ ZOOM_connection_errcode(ZOOM_connection c)
{
- return ZOOM_connection_error (c, 0, 0);
+ return ZOOM_connection_error(c, 0, 0);
}
ZOOM_API(const char *)
-ZOOM_connection_errmsg (ZOOM_connection c)
+ ZOOM_connection_errmsg(ZOOM_connection c)
{
const char *msg;
- ZOOM_connection_error (c, &msg, 0);
+ ZOOM_connection_error(c, &msg, 0);
return msg;
}
ZOOM_API(const char *)
-ZOOM_connection_addinfo (ZOOM_connection c)
+ ZOOM_connection_addinfo(ZOOM_connection c)
{
const char *addinfo;
- ZOOM_connection_error (c, 0, &addinfo);
+ ZOOM_connection_error(c, 0, &addinfo);
return addinfo;
}
ZOOM_API(const char *)
-ZOOM_diag_str (int error)
+ ZOOM_connection_diagset(ZOOM_connection c)
+{
+ const char *diagset;
+ ZOOM_connection_error_x(c, 0, 0, &diagset);
+ return diagset;
+}
+
+ZOOM_API(const char *)
+ ZOOM_diag_str(int error)
{
switch (error)
{
case ZOOM_ERROR_NONE:
- return "No error";
+ return "No error";
case ZOOM_ERROR_CONNECT:
- return "Connect failed";
+ return "Connect failed";
case ZOOM_ERROR_MEMORY:
- return "Out of memory";
+ return "Out of memory";
case ZOOM_ERROR_ENCODE:
- return "Encoding failed";
+ return "Encoding failed";
case ZOOM_ERROR_DECODE:
- return "Decoding failed";
+ return "Decoding failed";
case ZOOM_ERROR_CONNECTION_LOST:
- return "Connection lost";
+ return "Connection lost";
case ZOOM_ERROR_INIT:
- return "Init rejected";
+ return "Init rejected";
case ZOOM_ERROR_INTERNAL:
- return "Internal failure";
+ return "Internal failure";
case ZOOM_ERROR_TIMEOUT:
- return "Timeout";
+ return "Timeout";
case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
- return "Unsupported protocol";
+ return "Unsupported protocol";
case ZOOM_ERROR_UNSUPPORTED_QUERY:
- return "Unsupported query type";
+ return "Unsupported query type";
+ case ZOOM_ERROR_INVALID_QUERY:
+ return "Invalid query";
+ case ZOOM_ERROR_CQL_PARSE:
+ return "CQL parsing error";
+ case ZOOM_ERROR_CQL_TRANSFORM:
+ return "CQL transformation error";
+ case ZOOM_ERROR_CCL_CONFIG:
+ return "CCL configuration error";
+ case ZOOM_ERROR_CCL_PARSE:
+ return "CCL parsing error";
+ case ZOOM_ERROR_ES_INVALID_ACTION:
+ return "Extended Service. invalid action";
+ case ZOOM_ERROR_ES_INVALID_VERSION:
+ return "Extended Service. invalid version";
+ case ZOOM_ERROR_ES_INVALID_SYNTAX:
+ return "Extended Service. invalid syntax";
default:
- return diagbib1_str (error);
+ return diagbib1_str(error);
}
}
ZOOM_API(int)
-ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
- const char **addinfo, const char **diagset)
+ ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
+ const char **addinfo, const char **diagset)
{
int error = c->error;
if (cp)
*cp = z_HTTP_errmsg(c->error);
else if (!strcmp(c->diagset, "Bib-1"))
*cp = ZOOM_diag_str(error);
- else if (!strcmp(c->diagset, "SRW"))
+ else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
*cp = yaz_diag_srw_str(c->error);
else
*cp = "Unknown error and diagnostic set";
}
ZOOM_API(int)
-ZOOM_connection_error (ZOOM_connection c, const char **cp,
- const char **addinfo)
+ ZOOM_connection_error(ZOOM_connection c, const char **cp,
+ const char **addinfo)
{
return ZOOM_connection_error_x(c, cp, addinfo, 0);
}
-static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
+static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
{
ZOOM_Event event = 0;
int r = cs_look(c->cs);
- yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
- c, mask, r);
-
+ yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
+ c, mask, r);
+
if (r == CS_NONE)
{
- event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
- set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
- do_close (c);
- ZOOM_connection_put_event (c, event);
+ event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
+ ZOOM_connection_close(c);
+ ZOOM_connection_put_event(c, event);
}
else if (r == CS_CONNECT)
{
- int ret;
- event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
-
- ret = cs_rcvconnect (c->cs);
- yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
+ int ret = ret = cs_rcvconnect(c->cs);
+ yaz_log(c->log_details, "%p ZOOM_connection_do_io "
+ "cs_rcvconnect returned %d", c, ret);
if (ret == 1)
{
- c->mask = ZOOM_SELECT_EXCEPT;
+ int mask = ZOOM_SELECT_EXCEPT;
if (c->cs->io_pending & CS_WANT_WRITE)
- c->mask += ZOOM_SELECT_WRITE;
+ mask += ZOOM_SELECT_WRITE;
if (c->cs->io_pending & CS_WANT_READ)
- c->mask += ZOOM_SELECT_READ;
- ZOOM_connection_put_event (c, event);
+ mask += ZOOM_SELECT_READ;
+ ZOOM_connection_set_mask(c, mask);
+ event = ZOOM_Event_create(ZOOM_EVENT_NONE);
+ ZOOM_connection_put_event(c, event);
}
else if (ret == 0)
{
- ZOOM_connection_put_event (c, event);
+ event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
+ ZOOM_connection_put_event(c, event);
+ get_cert(c);
if (c->proto == PROTO_Z3950)
- ZOOM_connection_send_init(c);
+ ZOOM_connection_Z3950_send_init(c);
else
{
/* no init request for SRW .. */
- assert (c->tasks->which == ZOOM_TASK_CONNECT);
- ZOOM_connection_remove_task (c);
- c->mask = 0;
- ZOOM_connection_exec_task (c);
+ assert(c->tasks->which == ZOOM_TASK_CONNECT);
+ ZOOM_connection_remove_task(c);
+ ZOOM_connection_set_mask(c, 0);
+ ZOOM_connection_exec_task(c);
}
c->state = STATE_ESTABLISHED;
}
else
{
- set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
- do_close (c);
- ZOOM_connection_put_event (c, event);
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
+ ZOOM_connection_close(c);
}
}
else
{
+ if (mask & ZOOM_SELECT_EXCEPT)
+ {
+ if (!ZOOM_test_reconnect(c))
+ {
+ ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
+ ZOOM_connection_close(c);
+ }
+ return;
+ }
if (mask & ZOOM_SELECT_READ)
- do_read (c);
+ do_read(c);
if (c->cs && (mask & ZOOM_SELECT_WRITE))
- do_write (c);
+ ZOOM_send_buf(c);
}
- return 1;
}
ZOOM_API(int)
-ZOOM_connection_last_event(ZOOM_connection cs)
+ ZOOM_connection_last_event(ZOOM_connection cs)
{
if (!cs)
return ZOOM_EVENT_NONE;
return cs->last_event;
}
-ZOOM_API(int)
-ZOOM_event (int no, ZOOM_connection *cs)
-{
- int timeout = 5000;
-#if HAVE_SYS_POLL_H
- struct pollfd pollfds[1024];
- ZOOM_connection poll_cs[1024];
-#else
- struct timeval tv;
- fd_set input, output, except;
-#endif
- int i, r, nfds;
- int max_fd = 0;
-
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- ZOOM_Event event;
- if (c && (event = ZOOM_connection_get_event(c)))
- {
- ZOOM_Event_destroy (event);
- return i+1;
- }
- }
- for (i = 0; i<no; i++)
- {
- ZOOM_connection c = cs[i];
- ZOOM_Event event;
- if (c && ZOOM_connection_exec_task (c))
- {
- if ((event = ZOOM_connection_get_event(c)))
- {
- ZOOM_Event_destroy (event);
- return i+1;
- }
- }
- }
-#if HAVE_SYS_POLL_H
-#else
- FD_ZERO (&input);
- FD_ZERO (&output);
- FD_ZERO (&except);
-#endif
- nfds = 0;
- for (i = 0; i<no; i++)
+ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
+{
+ if (c->mask)
{
- ZOOM_connection c = cs[i];
- int fd, mask;
- int this_timeout;
-
- if (!c)
- continue;
- fd = z3950_connection_socket(c);
- mask = z3950_connection_mask(c);
-
- if (fd == -1)
- continue;
- if (max_fd < fd)
- max_fd = fd;
-
- this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
- if (this_timeout != -1 && this_timeout < timeout)
- timeout = this_timeout;
-#if HAVE_SYS_POLL_H
- if (mask)
- {
- short poll_events = 0;
-
- if (mask & ZOOM_SELECT_READ)
- poll_events += POLLIN;
- if (mask & ZOOM_SELECT_WRITE)
- poll_events += POLLOUT;
- if (mask & ZOOM_SELECT_EXCEPT)
- poll_events += POLLERR;
- pollfds[nfds].fd = fd;
- pollfds[nfds].events = poll_events;
- pollfds[nfds].revents = 0;
- poll_cs[nfds] = c;
- nfds++;
- }
-#else
- if (mask & ZOOM_SELECT_READ)
- {
- FD_SET (fd, &input);
- nfds++;
- }
- if (mask & ZOOM_SELECT_WRITE)
- {
- FD_SET (fd, &output);
- nfds++;
- }
- if (mask & ZOOM_SELECT_EXCEPT)
- {
- FD_SET (fd, &except);
- nfds++;
- }
-#endif
+ ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
+ /* timeout and this connection was waiting */
+ ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
+ ZOOM_connection_close(c);
+ ZOOM_connection_put_event(c, event);
}
- if (timeout >= 5000)
- timeout = 30;
+ return 0;
+}
- if (!nfds)
+ZOOM_API(int)
+ ZOOM_connection_process(ZOOM_connection c)
+{
+ ZOOM_Event event;
+ if (!c)
return 0;
-#if HAVE_SYS_POLL_H
- r = poll (pollfds, nfds, timeout * 1000);
- for (i = 0; i<nfds; i++)
+ event = ZOOM_connection_get_event(c);
+ if (event)
{
- ZOOM_connection c = poll_cs[i];
- if (r && c->mask)
- {
- int mask = 0;
- if (pollfds[i].revents & POLLIN)
- mask += ZOOM_SELECT_READ;
- if (pollfds[i].revents & POLLOUT)
- mask += ZOOM_SELECT_WRITE;
- if (pollfds[i].revents & POLLERR)
- mask += ZOOM_SELECT_EXCEPT;
- if (mask)
- ZOOM_connection_do_io(c, mask);
- }
- else if (r == 0 && c->mask)
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
- /* timeout and this connection was waiting */
- set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
- do_close (c);
- ZOOM_connection_put_event(c, event);
- }
+ ZOOM_Event_destroy(event);
+ return 1;
}
-#else
- tv.tv_sec = timeout;
- tv.tv_usec = 0;
- yaz_log (LOG_DEBUG, "select start");
- r = select (max_fd+1, &input, &output, &except, &tv);
- yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
- for (i = 0; i<no; i++)
+ ZOOM_connection_exec_task(c);
+ event = ZOOM_connection_get_event(c);
+ if (event)
{
- ZOOM_connection c = cs[i];
- int fd, mask;
-
- if (!c)
- continue;
- fd = z3950_connection_socket(c);
- mask = 0;
- if (r && c->mask)
- {
- /* no timeout and real socket */
- if (FD_ISSET(fd, &input))
- mask += ZOOM_SELECT_READ;
- if (FD_ISSET(fd, &output))
- mask += ZOOM_SELECT_WRITE;
- if (FD_ISSET(fd, &except))
- mask += ZOOM_SELECT_EXCEPT;
- if (mask)
- ZOOM_connection_do_io(c, mask);
- }
- if (r == 0 && c->mask)
- {
- ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
- /* timeout and this connection was waiting */
- set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
- do_close (c);
- yaz_log (LOG_DEBUG, "timeout");
- ZOOM_connection_put_event(c, event);
- }
+ ZOOM_Event_destroy(event);
+ return 1;
}
-#endif
+ return 0;
+}
+
+ZOOM_API(int)
+ ZOOM_event_nonblock(int no, ZOOM_connection *cs)
+{
+ int i;
+
+ yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
+
for (i = 0; i<no; i++)
{
- ZOOM_connection c = cs[i];
- ZOOM_Event event;
- if (c && (event = ZOOM_connection_get_event(c)))
- {
- ZOOM_Event_destroy (event);
- return i+1;
- }
+ ZOOM_connection c = cs[i];
+
+ if (c && ZOOM_connection_process(c))
+ return i+1;
}
return 0;
}
+
+ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
+{
+ if (c->mask && mask)
+ ZOOM_connection_do_io(c, mask);
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
+{
+ if (c->cs)
+ return cs_fileno(c->cs);
+ return -1;
+}
+
+ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
+{
+ c->mask = mask;
+ if (!c->cs)
+ return -1;
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
+{
+ if (c->cs)
+ return c->mask;
+ return 0;
+}
+
+ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
+{
+ return ZOOM_options_get_int(c->options, "timeout", 30);
+}
+
+ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
+{
+ if (c->cs)
+ cs_close(c->cs);
+ c->cs = 0;
+ ZOOM_connection_set_mask(c, 0);
+ c->state = STATE_IDLE;
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+