/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2011 Index Data
+ * Copyright (C) 1995-2013 Index Data
* See the file LICENSE for details.
*/
/**
}
if (addinfo && addinfo2)
{
- c->addinfo = (char*) 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)
int ZOOM_uri_to_code(const char *uri)
{
- int code = 0;
+ int code = 0;
const char *cp;
if ((cp = strrchr(uri, '/')))
code = atoi(cp+1);
ZOOM_connection_remove_task(c);
}
+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)
c->host_port = 0;
c->proxy = 0;
-
+ c->tproxy = 0;
+
c->charset = c->lang = 0;
c->cookie_out = 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->sru_version = 0;
c->no_redirects = 0;
+ c->saveAPDU_wrbuf = 0;
return c;
}
+ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
+{
+ if (c->odr_save)
+ {
+ odr_destroy(c->odr_save);
+ c->odr_save = 0;
+ }
+ if (w)
+ {
+ c->odr_save = odr_createmem(ODR_PRINT);
+ odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
+ }
+}
/* set database names. Take local databases (if set); otherwise
take databases given in ZURL (if set); otherwise use Default */
{
char **databaseNames;
const char *cp = ZOOM_options_get(options, "databaseName");
-
+
if ((!cp || !*cp) && con->host_port)
- {
- if (strncmp(con->host_port, "unix:", 5) == 0)
- cp = strchr(con->host_port+5, ':');
- else
- cp = strchr(con->host_port, '/');
- if (cp)
- cp++;
- }
- if (!cp)
+ cs_get_host_args(con->host_port, &cp);
+ if (!cp || !*cp)
cp = "Default";
nmem_strsplit(odr_getmem(odr), "+", cp, &databaseNames, num);
return databaseNames;
c->proxy = xstrdup(val);
}
+ xfree(c->tproxy);
+ c->tproxy = 0;
+ val = ZOOM_options_get(c->options, "tproxy");
+ if (val && *val)
+ {
+ yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
+ c->tproxy = xstrdup(val);
+ }
+
xfree(c->charset);
c->charset = 0;
val = ZOOM_options_get(c->options, "charset");
}
else
c->host_port = xstrdup(host);
- }
+ }
{
/*
char *pcomma;
char *pequals;
while ((pcomma = strchr(remainder, ',')) != 0 &&
- (pcolon == 0 || pcomma < pcolon)) {
+ (pcolon == 0 || pcomma < pcolon))
+ {
*pcomma = '\0';
- if ((pequals = strchr(remainder, '=')) != 0) {
+ if ((pequals = strchr(remainder, '=')) != 0)
+ {
*pequals = '\0';
- /*printf("# setting '%s'='%s'\n", remainder, pequals+1);*/
ZOOM_connection_option_set(c, remainder, pequals+1);
}
remainder = pcomma+1;
}
- if (remainder != c->host_port) {
+ if (remainder != c->host_port)
+ {
+ remainder = xstrdup(remainder);
xfree(c->host_port);
- c->host_port = xstrdup(remainder);
- /*printf("# reset hp='%s'\n", remainder);*/
+ c->host_port = remainder;
}
}
c->cookie_out = 0;
val = ZOOM_options_get(c->options, "cookie");
if (val && *val)
- {
+ {
yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
c->cookie_out = xstrdup(val);
}
if (val && *val)
c->password = xstrdup(val);
-
+
c->maximum_record_size =
- ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
+ ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
c->preferred_message_size =
- ZOOM_options_get_int(c->options, "preferredMessageSize", 1024*1024);
+ ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
c->async = ZOOM_options_get_bool(c->options, "async", 0);
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 */
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->group);
xfree(c->password);
xfree(c->sru_version);
+ wrbuf_destroy(c->saveAPDU_wrbuf);
xfree(c);
}
r->query = q;
r->options = ZOOM_options_create_with_parent(c->options);
-
+
start = ZOOM_options_get_int(r->options, "start", 0);
count = ZOOM_options_get_int(r->options, "count", 0);
{
r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
r->odr);
-
+
r->connection = c;
#if ZOOM_RESULT_LISTS
task->u.search.count = count;
task->u.search.recv_search_fired = 0;
- syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
+ 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
+ task->u.search.elementSetName = elementSetName
? xstrdup(elementSetName) : 0;
-
+
ZOOM_resultset_addref(r);
ZOOM_query_addref(q);
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);
+ ZOOM_resultset_addref(r);
if (!c->async)
{
task->u.retrieve.start = start;
task->u.retrieve.count = count;
- syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
+ 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
+ task->u.retrieve.elementSetName = elementSetName
? xstrdup(elementSetName) : 0;
cp = ZOOM_options_get(r->options, "schema");
return 0;
}
+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];
+ }
+ return 0;
+}
ZOOM_API(ZOOM_facet_field *)
ZOOM_resultset_facets(ZOOM_resultset r)
}
ZOOM_API(const char**)
- ZOOM_resultset_facet_names(ZOOM_resultset r)
+ ZOOM_resultset_facets_names(ZOOM_resultset r)
{
return (const char **) r->facets_names;
}
{
char *cert_buf;
int cert_len;
-
+
if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
{
ZOOM_connection_option_setl(c, "sslPeerCert",
if (c->cs)
cs_close(c->cs);
- c->cs = cs_create_host_proxy(logical_url, 0, &add, c->proxy);
-
+ c->cs = cs_create_host_proxy(logical_url, 0, &add,
+ c->tproxy ? c->tproxy : c->proxy);
+
if (c->cs && c->cs->protocol == PROTO_HTTP)
{
#if YAZ_HAVE_XML2
if (c->cs->io_pending & CS_WANT_READ)
mask += ZOOM_SELECT_READ;
ZOOM_connection_set_mask(c, mask);
- c->state = STATE_CONNECTING;
+ c->state = STATE_CONNECTING;
return zoom_pending;
}
}
}
/* returns 1 if PDU was sent OK (still pending )
- 0 if PDU was not sent OK (nothing to wait for)
+ 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)
{
const char *syntax =
- ZOOM_options_get(s->options, "preferredRecordSyntax");
+ ZOOM_options_get(s->options, "preferredRecordSyntax");
const char *elementSetName =
ZOOM_options_get(s->options, "elementSetName");
{
ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
task->u.scan.scan = scan;
-
+
(scan->refcount)++;
if (!c->async)
{
ZOOM_query_destroy(scan->query);
odr_destroy(scan->odr);
-
+
ZOOM_options_destroy(scan->options);
xfree(scan);
}
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);
-
+
c->buf_out = c->tasks->u.package->buf_out;
c->len_out = c->tasks->u.package->len_out;
const char **disp_term, size_t *disp_len)
{
size_t noent = ZOOM_scanset_size(scan);
-
+
*value_term = 0;
*value_len = 0;
if (res->entries->entries[pos]->which == Z_Entry_termInfo)
{
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)
ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
&disp_term, &disp_len);
-
+
*len = value_len;
return value_term;
}
ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
&disp_term, &disp_len);
-
+
*len = disp_len;
return disp_term;
}
{
odr_destroy(p->odr_out);
xfree(p->buf_out);
-
+
ZOOM_options_destroy(p->options);
xfree(p);
}
ret = send_package(c);
break;
case ZOOM_TASK_SORT:
- c->tasks->u.sort.resultset->r_sort_spec =
+ 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;
z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
c->user, c->password);
}
- if (!z_GDU(c->odr_out, &gdu, 0, 0))
+ return ZOOM_send_GDU(c, gdu);
+}
+
+zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
+{
+ ZOOM_Event event;
+
+ 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_SEND_APDU);
+ ZOOM_connection_put_event(c, event);
+
return ZOOM_send_buf(c);
}
{
zoom_ret cret = zoom_complete;
int ret = -1;
- const char *addinfo = 0;
+ char *addinfo = 0;
const char *connection_head = z_HTTP_header_lookup(hres->headers,
"Connection");
const char *location;
return;
}
}
- else
- {
- ret = ZOOM_handle_sru(c, hres, &cret);
+ else
+ {
+ ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
if (ret == 0)
{
if (c->no_redirects) /* end of redirect. change hosts again */
if (hres->code != 200)
ZOOM_set_HTTP_error(c, hres->code, 0, 0);
else
+ {
+ yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
+ addinfo : "NULL");
ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
+ }
ZOOM_connection_close(c);
}
if (cret == zoom_complete)
c->reconnect_ok = 0;
}
}
+ else
+ c->reconnect_ok = 1; /* if the server closes anyway */
}
}
#endif
{
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);
more = cs_more(c->cs);
yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
{
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)
#endif
}
}
- c->reconnect_ok = 0;
}
return 1;
}
{
int r;
ZOOM_Event event;
-
+
event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
ZOOM_connection_put_event(c, event);
return zoom_complete;
}
else if (r == 1)
- {
+ {
int mask = ZOOM_SELECT_EXCEPT;
if (c->cs->io_pending & CS_WANT_WRITE)
mask += ZOOM_SELECT_WRITE;
ZOOM_API(const char *)
ZOOM_connection_option_get(ZOOM_connection c, const char *key)
{
- return ZOOM_options_get(c->options, 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_getl(c->options, key, lenp);
+ 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)
{
- ZOOM_options_set(c->options, key, 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)
int r = cs_look(c->cs);
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);
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];
{
c->mask = mask;
if (!c->cs)
- return -1;
+ return -1;
return 0;
}