X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fzoom-c.c;h=e8740648c7955755c5d3d9eed719ea529eab516b;hb=9cc8f4a4d0fd7f1e4bd6c9d5c85ef2dccaab5743;hp=492c19052c59d75090bf8088330b708bb1a1244a;hpb=8d92137c228ec24df2d62a8039635bb8ad19558d;p=yaz-moved-to-github.git diff --git a/src/zoom-c.c b/src/zoom-c.c index 492c190..e874064 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -1,11 +1,14 @@ /* - * Copyright (c) 2000-2003, Index Data + * Copyright (C) 1995-2005, Index Data ApS * 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. + * $Id: zoom-c.c,v 1.40 2005-05-02 19:33:55 adam Exp $ + */ +/** + * \file zoom-c.c + * \brief Implements ZOOM C interface. */ + #include #include #include "zoom-p.h" @@ -21,9 +24,24 @@ #include #include +#if HAVE_SYS_TYPES_H +#include +#endif +#if HAVE_SYS_TIME_H +#include +#endif #if HAVE_SYS_POLL_H #include #endif +#if HAVE_SYS_SELECT_H +#include +#endif +#ifdef WIN32 +#include +#endif + +static int log_level = 0; +static int log_level_initialized = 0; typedef enum { zoom_pending, @@ -90,20 +108,34 @@ static void set_dset_error (ZOOM_connection c, int error, const char *dset, const char *addinfo, const char *addinfo2) { + char *cp; + if (!log_level_initialized) + { + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; + } + 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) + 2); strcpy(c->addinfo, addinfo); strcat(c->addinfo, addinfo2); } else if (addinfo) c->addinfo = xstrdup(addinfo); if (error) - yaz_log(LOG_DEBUG, "Error %s %s:%d %s %s", + yaz_log(log_level, "Error %s %s:%d %s %s", c->host_port ? c->host_port : "<>", dset, error, addinfo ? addinfo : "", addinfo2 ? addinfo2 : ""); @@ -177,7 +209,6 @@ void ZOOM_connection_remove_task (ZOOM_connection c) switch (task->which) { case ZOOM_TASK_SEARCH: - ZOOM_resultset_destroy (task->u.search.resultset); break; case ZOOM_TASK_RETRIEVE: @@ -191,6 +222,10 @@ void ZOOM_connection_remove_task (ZOOM_connection c) case ZOOM_TASK_PACKAGE: ZOOM_package_destroy (task->u.package); break; + case ZOOM_TASK_SORT: + ZOOM_resultset_destroy (task->u.sort.resultset); + ZOOM_query_destroy(task->u.sort.q); + break; default: assert (0); } @@ -213,12 +248,19 @@ ZOOM_connection_create (ZOOM_options options) { ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); + if (!log_level_initialized) + { + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; + } + c->proto = PROTO_Z3950; c->cs = 0; c->mask = 0; c->reconnect_ok = 0; c->state = STATE_IDLE; c->addinfo = 0; + c->diagset = 0; set_ZOOM_error(c, ZOOM_ERROR_NONE, 0); c->buf_in = 0; c->len_in = 0; @@ -257,54 +299,20 @@ static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options, int *num) { char **databaseNames; - const char *c; - int no = 2; const char *cp = ZOOM_options_get (options, "databaseName"); if (!cp || !*cp) { if (strncmp (con->host_port, "unix:", 5) == 0) - cp = strchr (con->host_port+5, ':'); + cp = strchr(con->host_port+5, ':'); else - cp = strchr (con->host_port, '/'); + cp = strchr(con->host_port, '/'); if (cp) cp++; } - if (cp) - { - c = cp; - while ((c = strchr(c, '+'))) - { - c++; - no++; - } - } - else + if (!cp) 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; + nmem_strsplit(con->odr_out->mem, "+", cp, &databaseNames, num); return databaseNames; } @@ -324,13 +332,19 @@ ZOOM_connection_connect(ZOOM_connection c, const char *val; ZOOM_task task; + if (!log_level_initialized) + { + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; + } + if (c->cs) { - yaz_log (LOG_DEBUG, "reconnect"); + yaz_log (log_level, "reconnect"); c->reconnect_ok = 1; return; } - yaz_log(LOG_DEBUG, "connect"); + yaz_log(log_level, "connect"); xfree (c->proxy); val = ZOOM_options_get (c->options, "proxy"); if (val && *val) @@ -406,7 +420,7 @@ ZOOM_query_destroy(ZOOM_query s) return; (s->refcount)--; - yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount); + yaz_log (log_level, "ZOOM_query_destroy count=%d", s->refcount); if (s->refcount == 0) { odr_destroy (s->odr); @@ -422,7 +436,10 @@ ZOOM_query_prefix(ZOOM_query s, const char *str) 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) + { + s->z_query = 0; return -1; + } return 0; } @@ -471,6 +488,7 @@ ZOOM_connection_destroy(ZOOM_connection c) xfree (c->buf_in); xfree (c->addinfo); + xfree (c->diagset); odr_destroy (c->odr_in); odr_destroy (c->odr_out); ZOOM_options_destroy (c->options); @@ -491,15 +509,22 @@ void ZOOM_resultset_addref (ZOOM_resultset r) if (r) { (r->refcount)++; - yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d", + yaz_log (log_level, "ZOOM_resultset_addref r=%p count=%d", r, r->refcount); } } + ZOOM_resultset ZOOM_resultset_create () { ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r)); - yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r); + if (!log_level_initialized) + { + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; + } + + yaz_log (log_level, "ZOOM_resultset_create r = %p", r); r->refcount = 1; r->size = 0; r->odr = odr_createmem (ODR_ENCODE); @@ -544,7 +569,12 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) 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); + { + /* 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) @@ -562,12 +592,12 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) { if (!c->cs) { - yaz_log(LOG_DEBUG, "NO COMSTACK"); + yaz_log(log_level, "NO COMSTACK"); ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT); } else { - yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT"); + yaz_log(log_level, "PREPARE FOR RECONNECT"); c->reconnect_ok = 1; } } @@ -587,26 +617,73 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) } ZOOM_API(void) + ZOOM_resultset_sort(ZOOM_resultset r, + const char *sort_type, const char *sort_spec) +{ + ZOOM_connection c = r->connection; + ZOOM_task task; + + if (!c) + return; + + if (c->host_port && c->proto == PROTO_HTTP) + { + if (!c->cs) + { + yaz_log(log_level, "NO COMSTACK"); + ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT); + } + else + { + yaz_log(log_level, "PREPARE FOR RECONNECT"); + 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 = ZOOM_query_create(); + ZOOM_query_sortby(task->u.sort.q, sort_spec); + + ZOOM_resultset_addref (r); + + if (!c->async) + { + while (ZOOM_event (1, &c)) + ; + } +} + +ZOOM_API(void) + ZOOM_resultset_cache_reset(ZOOM_resultset r) +{ + 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); + } + r->record_cache = 0; +} + +ZOOM_API(void) ZOOM_resultset_destroy(ZOOM_resultset r) { if (!r) return; (r->refcount)--; - yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d", + yaz_log (log_level, "ZOOM_resultset_destroy r = %p count=%d", r, r->refcount); if (r->refcount == 0) { - ZOOM_record_cache rc; + ZOOM_resultset_cache_reset(r); - 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 */ @@ -663,12 +740,12 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r, { if (!c->cs) { - yaz_log(LOG_DEBUG, "NO COMSTACK"); + yaz_log(log_level, "NO COMSTACK"); ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT); } else { - yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT"); + yaz_log(log_level, "PREPARE FOR RECONNECT"); c->reconnect_ok = 1; } } @@ -713,19 +790,39 @@ ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs, } } +static void get_cert(ZOOM_connection c) +{ + char *cert_buf; + int cert_len; + + if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len)) + { + ZOOM_connection_option_setl(c, "sslPeerCert", + cert_buf, cert_len); + xfree(cert_buf); + } +} + static zoom_ret do_connect (ZOOM_connection c) { void *add; const char *effective_host; + if (!log_level_initialized) + { + log_level = yaz_log_module_level("zoom"); + log_level_initialized = 1; + } + if (c->proxy) effective_host = c->proxy; else effective_host = c->host_port; - yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); + yaz_log (log_level, "do_connect host=%s", effective_host); - assert (!c->cs); + if (c->cs) + cs_close(c->cs); c->cs = cs_create_host (effective_host, 0, &add); if (c->cs && c->cs->protocol == PROTO_HTTP) @@ -736,7 +833,7 @@ static zoom_ret do_connect (ZOOM_connection c) c->proto = PROTO_HTTP; cs_get_host_args(c->host_port, &path); xfree(c->path); - c->path = xmalloc(strlen(path)+2); + c->path = (char*) xmalloc(strlen(path)+2); c->path[0] = '/'; strcpy (c->path+1, path); #else @@ -752,6 +849,7 @@ static zoom_ret do_connect (ZOOM_connection c) { 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_send_init(c); else @@ -811,7 +909,7 @@ static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out) val = ZOOM_options_get (c->options, buf); if (!val) break; - cp = strchr (val, ':'); + cp = strchr(val, ':'); if (!cp) continue; len = cp - val; @@ -855,7 +953,7 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out) z_APDU(odr_pr, &a, 0, 0); odr_destroy(odr_pr); } - yaz_log (LOG_DEBUG, "encoding failed"); + yaz_log (log_level, "encoding failed"); set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0); odr_reset(out); return -1; @@ -870,7 +968,7 @@ static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a) assert (a); if (encode_APDU(c, a, c->odr_out)) return zoom_complete; - yaz_log(LOG_DEBUG, "send APDU type=%d", a->which); + yaz_log(log_level, "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); @@ -884,15 +982,19 @@ static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a) 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"); - + const char *auth_password = ZOOM_options_get (c->options, "password"); + char *version; + + /* support the pass for backwards compatibility */ + if (!auth_password) + 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); @@ -903,40 +1005,22 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) 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 $"); + + /* Index Data's Z39.50 Implementor Id is 81 */ + ireq->implementationId = odr_prepend(c->odr_out, + ZOOM_options_get(c->options, "implementationId"), + odr_prepend(c->odr_out, "81", ireq->implementationId)); + + ireq->implementationName = odr_prepend(c->odr_out, + ZOOM_options_get(c->options, "implementationName"), + odr_prepend(c->odr_out, "ZOOM-C", ireq->implementationName)); + + version = odr_strdup(c->odr_out, "$Revision: 1.40 $"); + if (strlen(version) > 10) /* check for unexpanded CVS strings */ + version[strlen(version)-2] = '\0'; + ireq->implementationVersion = odr_prepend(c->odr_out, + ZOOM_options_get(c->options, "implementationVersion"), + odr_prepend(c->odr_out, &version[11], ireq->implementationVersion)); *ireq->maximumRecordSize = ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024); @@ -989,7 +1073,7 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) if (c->proxy) yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out, VAL_PROXY, 1, c->host_port); - if (c->charset||c->lang) + if (c->charset || c->lang) { Z_OtherInformation **oi; Z_OtherInformationUnit *oi_unit; @@ -998,14 +1082,26 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0))) { + char **charsets_addresses = 0; + char **langs_addresses = 0; + int charsets_count = 0; + int langs_count = 0; + + if (c->charset) + nmem_strsplit_blank(c->odr_out->mem, c->charset, + &charsets_addresses, &charsets_count); + if (c->lang) + nmem_strsplit_blank(c->odr_out->mem, c->lang, + &langs_addresses, &langs_count); 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); + yaz_set_proposal_charneg(c->odr_out, + (const char **) charsets_addresses, + charsets_count, + (const char **) langs_addresses, + langs_count, + 1); } } assert (apdu); @@ -1017,12 +1113,12 @@ 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}, + {"http://www.loc.gov/zing/srw/", 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_SOAP *p = (Z_SOAP*) odr_malloc(o, sizeof(*p)); Z_GDU *gdu; ZOOM_Event event; @@ -1044,7 +1140,7 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr) if (cp0 && cp1) { - char *h = odr_malloc(c->odr_out, cp1 - cp0 + 1); + char *h = (char*) 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, @@ -1063,7 +1159,7 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr) 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 = (Z_SOAP_Generic *) odr_malloc(o, sizeof(*p->u.generic)); p->u.generic->no = 0; p->u.generic->ns = 0; p->u.generic->p = sr; @@ -1201,6 +1297,11 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c) /* prepare query for the search request */ search_req->query = r->query->z_query; + if (!search_req->query) + { + set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0); + return zoom_complete; + } search_req->databaseNames = set_DatabaseNames (c, r->options, &search_req->num_databaseNames); @@ -1274,7 +1375,7 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c) break; } r->setname = xstrdup (setname); - yaz_log (LOG_DEBUG, "allocating set %s", r->setname); + yaz_log (log_level, "allocating set %s", r->setname); } else r->setname = xstrdup ("default"); @@ -1372,6 +1473,62 @@ ZOOM_record_destroy (ZOOM_record rec) xfree (rec); } +static const char *marc_iconv_return(ZOOM_record rec, int marc_type, + int *len, + const char *buf, int sz, + const char *record_charset) +{ + char to[40]; + char from[40]; + yaz_iconv_t cd = 0; + yaz_marc_t mt = yaz_marc_create(); + + *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); + yaz_marc_iconv(mt, cd); + } + + yaz_marc_xml(mt, marc_type); + if (!rec->wrbuf_marc) + rec->wrbuf_marc = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_marc); + if (yaz_marc_decode_wrbuf (mt, buf, sz, rec->wrbuf_marc) > 0) + { + yaz_marc_destroy(mt); + if (cd) + yaz_iconv_close(cd); + if (len) + *len = wrbuf_len(rec->wrbuf_marc); + return wrbuf_buf(rec->wrbuf_marc); + } + yaz_marc_destroy(mt); + if (cd) + yaz_iconv_close(cd); + return 0; +} + static const char *record_iconv_return(ZOOM_record rec, int *len, const char *buf, int sz, const char *record_charset) @@ -1385,7 +1542,7 @@ static const char *record_iconv_return(ZOOM_record rec, int *len, if (record_charset && *record_charset) { /* Use "from,to" or just "from" */ - const char *cp =strchr(record_charset, ','); + const char *cp = strchr(record_charset, ','); int clen = strlen(record_charset); if (cp && cp[1]) { @@ -1442,6 +1599,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) { char type[40]; char charset[40]; + char xpath[512]; const char *cp; int i; Z_NamePlusRecord *npr; @@ -1464,24 +1622,34 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) } type[i] = '\0'; charset[0] = '\0'; - if (type_spec[i] == ';') + while (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++) + int j = 0; + i = i + 8; /* skip charset= */ + for (j = 0; type_spec[i] && j < sizeof(charset)-1; i++, j++) { - if (cp[i] == ';' || cp[i] == ' ') + if (type_spec[i] == ';' || type_spec[i] == ' ') break; - charset[i] = cp[i]; + charset[j] = cp[i]; } - charset[i] = '\0'; + charset[j] = '\0'; } - } - + else if (!strncmp(type_spec+i, "xpath=", 6)) + { + int j = 0; + i = i + 6; + for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++) + xpath[j] = cp[i]; + xpath[j] = '\0'; + } + while (type_spec[i] == ' ') + i++; + } if (!strcmp (type, "database")) { if (len) @@ -1504,8 +1672,11 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) *len = strlen(desc); return desc; } - else if (!strcmp (type, "render") && - npr->which == Z_NamePlusRecord_databaseRecord) + if (npr->which != Z_NamePlusRecord_databaseRecord) + return 0; + + /* from now on - we have a database record .. */ + if (!strcmp (type, "render")) { Z_External *r = (Z_External *) npr->u.databaseRecord; oident *ent = oid_getentbyoid(r->direct_reference); @@ -1520,11 +1691,12 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) } if (r->which == Z_External_sutrs) return record_iconv_return(rec, len, - r->u.sutrs->buf, r->u.sutrs->len, + (char*) r->u.sutrs->buf, + r->u.sutrs->len, charset); else if (r->which == Z_External_octet) { - yaz_marc_t mt; + const char *ret_buf; switch (ent->value) { case VAL_SOIF: @@ -1535,23 +1707,13 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) 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); + ret_buf = marc_iconv_return( + rec, YAZ_MARC_LINE, len, + (const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len, + charset); + if (ret_buf) + return ret_buf; } return record_iconv_return(rec, len, (const char *) r->u.octet_aligned->buf, @@ -1571,8 +1733,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) } return 0; } - else if (npr->which == Z_NamePlusRecord_databaseRecord && - (!strcmp (type, "xml") || !strcmp(type, "oai"))) + else if (!strcmp (type, "xml") || !strcmp(type, "oai")) { Z_External *r = (Z_External *) npr->u.databaseRecord; oident *ent = oid_getentbyoid(r->direct_reference); @@ -1593,7 +1754,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) charset); else if (r->which == Z_External_octet) { - yaz_marc_t mt; + const char *ret_buf; int marc_decode_type = YAZ_MARC_MARCXML; if (!strcmp(type, "oai")) @@ -1608,24 +1769,13 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) 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); + ret_buf = marc_iconv_return( + rec, marc_decode_type, len, + (const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len, + charset); + if (ret_buf) + return ret_buf; } return record_iconv_return(rec, len, (const char *) r->u.octet_aligned->buf, @@ -1641,36 +1791,31 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) } 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) { - 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; - } + 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; + if (len) *len = -1; + return (const char *) npr->u.databaseRecord; } - else if (npr->which == Z_NamePlusRecord_databaseRecord && - !strcmp (type, "opac")) + else if (!strcmp (type, "opac")) { Z_External *r = (Z_External *) npr->u.databaseRecord; @@ -1832,13 +1977,17 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, if (present_phase && p->num_records == 0) { /* present response and we didn't get any records! */ - set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); + Z_NamePlusRecord *myrec = + zget_surrogateDiagRec(resultset->odr, 0, 14, 0); + record_cache_add(resultset, myrec, resultset->start); } } else if (present_phase) { /* present response and we didn't get any records! */ - set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); + Z_NamePlusRecord *myrec = + zget_surrogateDiagRec(resultset->odr, 0, 14, 0); + record_cache_add(resultset, myrec, resultset->start); } } } @@ -1853,7 +2002,7 @@ static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr) ZOOM_resultset resultset; ZOOM_Event event; - yaz_log (LOG_DEBUG, "got search response"); + yaz_log (log_level, "got search response"); if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) return ; @@ -1899,20 +2048,11 @@ static int scan_response (ZOOM_connection c, Z_ScanResponse *res) return 1; } -static zoom_ret send_sort (ZOOM_connection c) +static zoom_ret send_sort (ZOOM_connection c, + ZOOM_resultset resultset) { - 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); @@ -2192,7 +2332,7 @@ ZOOM_scanset_display_term (ZOOM_scanset scan, size_t pos, if (t->displayTerm) { - term = (const char *) t->term->u.general->buf; + term = t->displayTerm; *len = strlen(term); } else if (t->term->which == Z_Term_general) @@ -2309,7 +2449,7 @@ 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->which = Z_IOItemOrder_esRequest; req->u.esRequest = (Z_IORequest *) odr_malloc(p->odr_out,sizeof(Z_IORequest)); @@ -2363,6 +2503,175 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p) return req; } +Z_APDU *create_admin_package(ZOOM_package p, int type, + Z_ESAdminOriginPartToKeep **toKeepP, + Z_ESAdminOriginPartNotToKeep **notToKeepP) +{ + Z_APDU *apdu = create_es_package (p, VAL_ADMINSERVICE); + if (apdu) + { + Z_ESAdminOriginPartToKeep *toKeep; + Z_ESAdminOriginPartNotToKeep *notToKeep; + Z_External *r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r)); + const char *first_db = "Default"; + int num_db; + char **db = set_DatabaseNames(p->connection, p->options, &num_db); + if (num_db > 0) + first_db = db[0]; + + r->direct_reference = + yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, + VAL_ADMINSERVICE); + r->descriptor = 0; + r->indirect_reference = 0; + r->which = Z_External_ESAdmin; + + r->u.adminService = (Z_Admin *) + odr_malloc(p->odr_out, sizeof(*r->u.adminService)); + r->u.adminService->which = Z_Admin_esRequest; + r->u.adminService->u.esRequest = (Z_AdminEsRequest *) + odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest)); + + toKeep = r->u.adminService->u.esRequest->toKeep = + (Z_ESAdminOriginPartToKeep *) + odr_malloc(p->odr_out, sizeof(*r->u.adminService->u.esRequest->toKeep)); + toKeep->which = type; + toKeep->databaseName = odr_strdup(p->odr_out, first_db); + toKeep->u.create = odr_nullval(); + apdu->u.extendedServicesRequest->taskSpecificParameters = r; + + r->u.adminService->u.esRequest->notToKeep = notToKeep = + (Z_ESAdminOriginPartNotToKeep *) + odr_malloc(p->odr_out, + sizeof(*r->u.adminService->u.esRequest->notToKeep)); + notToKeep->which = Z_ESAdminOriginPartNotToKeep_recordsWillFollow; + notToKeep->u.recordsWillFollow = odr_nullval(); + if (toKeepP) + *toKeepP = toKeep; + if (notToKeepP) + *notToKeepP = notToKeep; + } + return apdu; +} + +static Z_APDU *create_update_package(ZOOM_package p) +{ + Z_APDU *apdu = 0; + const char *first_db = "Default"; + int num_db; + char **db = set_DatabaseNames(p->connection, p->options, &num_db); + const char *action = ZOOM_options_get(p->options, "action"); + const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque"); + const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber"); + const char *record_buf = ZOOM_options_get(p->options, "record"); + const char *syntax_str = ZOOM_options_get(p->options, "syntax"); + int syntax_oid = VAL_NONE; + int action_no = -1; + + if (syntax_str) + syntax_oid = oid_getvalbyname(syntax_str); + if (!record_buf) + { + record_buf = "void"; + syntax_oid = VAL_SUTRS; + } + if (syntax_oid != VAL_NONE) + syntax_oid = VAL_TEXT_XML; + + if (num_db > 0) + first_db = db[0]; + + if (!action) + action = "specialUpdate"; + + if (!strcmp(action, "recordInsert")) + action_no = Z_IUOriginPartToKeep_recordInsert; + else if (!strcmp(action, "recordReplace")) + action_no = Z_IUOriginPartToKeep_recordReplace; + else if (!strcmp(action, "recordDelete")) + action_no = Z_IUOriginPartToKeep_recordDelete; + else if (!strcmp(action, "elementUpdate")) + action_no = Z_IUOriginPartToKeep_elementUpdate; + else if (!strcmp(action, "specialUpdate")) + action_no = Z_IUOriginPartToKeep_specialUpdate; + else + return 0; + + apdu = create_es_package (p, VAL_DBUPDATE); + if (apdu) + { + Z_IUOriginPartToKeep *toKeep; + Z_IUSuppliedRecords *notToKeep; + Z_External *r = (Z_External *) + odr_malloc (p->odr_out, sizeof(*r)); + + apdu->u.extendedServicesRequest->taskSpecificParameters = r; + + r->direct_reference = + yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, + VAL_DBUPDATE); + r->descriptor = 0; + r->which = Z_External_update; + r->indirect_reference = 0; + r->u.update = (Z_IUUpdate *) + odr_malloc(p->odr_out, sizeof(*r->u.update)); + + r->u.update->which = Z_IUUpdate_esRequest; + r->u.update->u.esRequest = (Z_IUUpdateEsRequest *) + odr_malloc(p->odr_out, sizeof(*r->u.update->u.esRequest)); + toKeep = r->u.update->u.esRequest->toKeep = + (Z_IUOriginPartToKeep *) + odr_malloc(p->odr_out, sizeof(*toKeep)); + + toKeep->databaseName = odr_strdup(p->odr_out, first_db); + toKeep->schema = 0; + toKeep->elementSetName = 0; + toKeep->actionQualifier = 0; + toKeep->action = odr_intdup(p->odr_out, action_no); + + notToKeep = r->u.update->u.esRequest->notToKeep = + (Z_IUSuppliedRecords *) + odr_malloc(p->odr_out, sizeof(*notToKeep)); + notToKeep->num = 1; + notToKeep->elements = (Z_IUSuppliedRecords_elem **) + odr_malloc(p->odr_out, sizeof(*notToKeep->elements)); + notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *) + odr_malloc(p->odr_out, sizeof(**notToKeep->elements)); + notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque; + if (recordIdOpaque) + { + notToKeep->elements[0]->u.opaque = (Odr_oct *) + odr_malloc (p->odr_out, sizeof(Odr_oct)); + notToKeep->elements[0]->u.opaque->size = + notToKeep->elements[0]->u.opaque->len = strlen(recordIdOpaque); + notToKeep->elements[0]->u.opaque->buf = (unsigned char*) + odr_strdup(p->odr_out, recordIdOpaque); + } + else if (recordIdNumber) + { + notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number; + + notToKeep->elements[0]->u.number = + odr_intdup(p->odr_out, atoi(recordIdNumber)); + } + else + notToKeep->elements[0]->u.opaque = 0; + notToKeep->elements[0]->supplementalId = 0; + notToKeep->elements[0]->correlationInfo = 0; + notToKeep->elements[0]->record = + z_ext_record(p->odr_out, syntax_oid, + record_buf, strlen(record_buf)); + } + if (0 && apdu) + { + ODR print = odr_createmem(ODR_PRINT); + + z_APDU(print, &apdu, 0, 0); + odr_destroy(print); + } + return apdu; +} + ZOOM_API(void) ZOOM_package_send (ZOOM_package p, const char *type) { @@ -2376,11 +2685,10 @@ ZOOM_API(void) 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)); + Z_External *r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r)); r->direct_reference = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV, @@ -2393,6 +2701,25 @@ ZOOM_API(void) apdu->u.extendedServicesRequest->taskSpecificParameters = r; } } + else if (!strcmp(type, "create")) /* create database */ + { + apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_create, + 0, 0); + } + else if (!strcmp(type, "drop")) /* drop database */ + { + apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_drop, + 0, 0); + } + else if (!strcmp(type, "commit")) /* commit changes */ + { + apdu = create_admin_package(p, Z_ESAdminOriginPartToKeep_commit, + 0, 0); + } + else if (!strcmp(type, "update")) /* update record(s) */ + { + apdu = create_update_package(p); + } if (apdu) { if (encode_APDU(p->connection, apdu, p->odr_out) == 0) @@ -2466,20 +2793,20 @@ static int ZOOM_connection_exec_task (ZOOM_connection c) if (!task) { - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task="); + yaz_log (log_level, "ZOOM_connection_exec_task task="); return 0; } - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d", + yaz_log (log_level, "ZOOM_connection_exec_task type=%d run=%d", task->which, task->running); if (c->error != ZOOM_ERROR_NONE) { - yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error); + yaz_log (log_level, "remove tasks because of error = %d", c->error); ZOOM_connection_remove_tasks (c); return 0; } if (task->running) { - yaz_log (LOG_DEBUG, "task already running"); + yaz_log (log_level, "task already running"); return 0; } task->running = 1; @@ -2508,27 +2835,35 @@ static int ZOOM_connection_exec_task (ZOOM_connection c) break; case ZOOM_TASK_PACKAGE: ret = send_package(c); + break; + case ZOOM_TASK_SORT: + c->tasks->u.sort.resultset->r_sort_spec = + c->tasks->u.sort.q->sort_spec; + ret = send_sort(c, c->tasks->u.sort.resultset); break; } } else { - yaz_log (LOG_DEBUG, "remove tasks because no connection exist"); + yaz_log (log_level, "remove tasks because no connection exist"); ZOOM_connection_remove_tasks (c); } if (ret == zoom_complete) { - yaz_log (LOG_DEBUG, "task removed (complete)"); + yaz_log (log_level, "task removed (complete)"); ZOOM_connection_remove_task (c); return 0; } - yaz_log (LOG_DEBUG, "task pending"); + yaz_log (log_level, "task pending"); return 1; } static zoom_ret send_sort_present (ZOOM_connection c) { - zoom_ret r = send_sort (c); + zoom_ret r = zoom_complete; + + if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH) + r = send_sort (c, c->tasks->u.search.resultset); if (r == zoom_complete) r = send_present (c); return r; @@ -2560,11 +2895,21 @@ 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); + yaz_log (log_level, "recv APDU type=%d", apdu->which); switch(apdu->which) { case Z_APDU_initResponse: initrs = apdu->u.initResponse; + ZOOM_connection_option_set(c, "serverImplementationId", + initrs->implementationId ? + initrs->implementationId : ""); + ZOOM_connection_option_set(c, "serverImplementationName", + initrs->implementationName ? + initrs->implementationName : ""); + ZOOM_connection_option_set(c, "serverImplementationVersion", + initrs->implementationVersion ? + initrs->implementationVersion : ""); + /* Set the three old options too, for old applications */ ZOOM_connection_option_set(c, "targetImplementationId", initrs->implementationId ? initrs->implementationId : ""); @@ -2605,11 +2950,11 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) if (p) { - char *charset=NULL, *lang=NULL; + char *charset = NULL, *lang = NULL; int sel; yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel); - yaz_log(LOG_DEBUG, "Target accepted: charset %s, " + yaz_log(log_level, "Target accepted: charset %s, " "language %s, select %d", charset ? charset : "none", lang ? lang : "none", sel); if (charset) @@ -2618,6 +2963,10 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) if (lang) ZOOM_connection_option_set (c, "negotiation-lang", lang); + + ZOOM_connection_option_set ( + c, "negotiation-charset-in-effect-for-records", + (sel != 0) ? "1" : "0"); nmem_destroy(tmpmem); } } @@ -2688,7 +3037,7 @@ static void handle_srw_response(ZOOM_connection c, resultset->size = 0; - yaz_log(LOG_DEBUG, "got SRW response OK"); + yaz_log(log_level, "got SRW response OK"); if (res->numberOfRecords) resultset->size = *res->numberOfRecords; @@ -2716,7 +3065,7 @@ static void handle_srw_response(ZOOM_connection c, 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 = + npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*) res->records[i].recordData_buf; npr->u.databaseRecord->u.octet_aligned->len = npr->u.databaseRecord->u.octet_aligned->size = @@ -2725,8 +3074,16 @@ static void handle_srw_response(ZOOM_connection c, } if (res->num_diagnostics > 0) { - set_dset_error(c, *res->diagnostics[0].code, "SRW", - res->diagnostics[0].details, 0); + const char *uri = res->diagnostics[0].uri; + if (uri) + { + int code = 0; + const char *cp; + if ((cp = strrchr(uri, '/'))) + code = atoi(cp+1); + set_dset_error(c, code, uri, + res->diagnostics[0].details, 0); + } } nmem = odr_extract_mem(c->odr_in); nmem_transfer(resultset->odr->mem, nmem); @@ -2743,14 +3100,14 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) const char *connection_head = z_HTTP_header_lookup(hres->headers, "Connection"); c->mask = 0; - yaz_log (LOG_DEBUG, "handle_http"); + yaz_log (log_level, "handle_http"); if (content_type && !yaz_strcmp_del("text/xml", content_type, "; ")) { Z_SOAP *soap_package = 0; - ODR o = odr_createmem(ODR_DECODE); + ODR o = c->odr_in; Z_SOAP_Handler soap_handlers[2] = { - {"http://www.loc.gov/zing/srw/v1.0/", 0, + {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec}, {0, 0, 0} }; @@ -2760,7 +3117,7 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) if (!ret && soap_package->which == Z_SOAP_generic && soap_package->u.generic->no == 0) { - Z_SRW_PDU *sr = soap_package->u.generic->p; + Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p; if (sr->which == Z_SRW_searchRetrieve_response) handle_srw_response(c, sr->u.response); else @@ -2775,7 +3132,6 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) } else ret = -1; - odr_destroy(o); } if (ret) { @@ -2809,10 +3165,9 @@ static int do_read (ZOOM_connection c) 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 (log_level, "do_read len=%d more=%d", r, more); if (r == 1) return 0; if (r <= 0) @@ -2821,7 +3176,7 @@ static int do_read (ZOOM_connection c) { do_close (c); c->reconnect_ok = 0; - yaz_log (LOG_DEBUG, "reconnect read"); + yaz_log (log_level, "reconnect read"); c->tasks->running = 0; ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); } @@ -2843,7 +3198,13 @@ static int do_read (ZOOM_connection c) if (!z_GDU (c->odr_in, &gdu, 0, 0)) { - set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); + int x; + int err = odr_geterrorx(c->odr_in, &x); + char msg[60]; + const char *element = odr_getelement(c->odr_in); + sprintf (msg, "ODR code %d:%d element=%-20s", + err, x, element ? element : ""); + set_ZOOM_error(c, ZOOM_ERROR_DECODE, msg); do_close (c); } else if (gdu->which == Z_GDU_Z3950) @@ -2870,14 +3231,14 @@ static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out) event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA); 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 (log_level, "do_write_ex len=%d", len_out); + if ((r = cs_put(c->cs, buf_out, len_out)) < 0) { if (c->reconnect_ok) { do_close (c); c->reconnect_ok = 0; - yaz_log (LOG_DEBUG, "reconnect write"); + yaz_log (log_level, "reconnect write"); c->tasks->running = 0; ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); return zoom_pending; @@ -2896,12 +3257,12 @@ static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out) c->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); + yaz_log (log_level, "do_write_ex 1 mask=%d", c->mask); } else { c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT; - yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask); + yaz_log (log_level, "do_write_ex 2 mask=%d", c->mask); } return zoom_pending; } @@ -2920,11 +3281,18 @@ ZOOM_connection_option_get (ZOOM_connection c, const char *key) ZOOM_API(void) ZOOM_connection_option_set (ZOOM_connection c, const char *key, - const char *val) + const char *val) { ZOOM_options_set (c->options, key, val); } +ZOOM_API(void) +ZOOM_connection_option_setl (ZOOM_connection c, const char *key, + const char *val, int len) +{ + ZOOM_options_setl (c->options, key, val, len); +} + ZOOM_API(const char *) ZOOM_resultset_option_get (ZOOM_resultset r, const char *key) { @@ -2988,6 +3356,8 @@ ZOOM_diag_str (int error) return "Unsupported protocol"; case ZOOM_ERROR_UNSUPPORTED_QUERY: return "Unsupported query type"; + case ZOOM_ERROR_INVALID_QUERY: + return "Invalid query"; default: return diagbib1_str (error); } @@ -3006,7 +3376,7 @@ ZOOM_connection_error_x (ZOOM_connection c, const char **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"; @@ -3029,7 +3399,7 @@ static int 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", + yaz_log (log_level, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d", c, mask, r); if (r == CS_NONE) @@ -3045,7 +3415,7 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask) event = ZOOM_Event_create (ZOOM_EVENT_CONNECT); ret = cs_rcvconnect (c->cs); - yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret); + yaz_log (log_level, "cs_rcvconnect returned %d", ret); if (ret == 1) { c->mask = ZOOM_SELECT_EXCEPT; @@ -3058,6 +3428,7 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask) else if (ret == 0) { ZOOM_connection_put_event (c, event); + get_cert(c); if (c->proto == PROTO_Z3950) ZOOM_connection_send_init(c); else @@ -3098,7 +3469,8 @@ ZOOM_connection_last_event(ZOOM_connection cs) ZOOM_API(int) ZOOM_event (int no, ZOOM_connection *cs) { - int timeout = 5000; + int timeout = 30; /* default timeout in seconds */ + int timeout_set = 0; /* whether it was overriden at all */ #if HAVE_SYS_POLL_H struct pollfd pollfds[1024]; ZOOM_connection poll_cs[1024]; @@ -3155,10 +3527,18 @@ ZOOM_event (int no, ZOOM_connection *cs) 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; + + /* -1 is used for indefinite timeout (no timeout), so -2 here. */ + this_timeout = ZOOM_options_get_int (c->options, "timeout", -2); + if (this_timeout != -2) + { + /* ensure the minimum timeout is used */ + if (!timeout_set) + timeout = this_timeout; + else if (this_timeout != -1 && this_timeout < timeout) + timeout = this_timeout; + timeout_set = 1; + } #if HAVE_SYS_POLL_H if (mask) { @@ -3194,14 +3574,11 @@ ZOOM_event (int no, ZOOM_connection *cs) } #endif } - if (timeout >= 5000) - timeout = 30; - if (!nfds) return 0; #if HAVE_SYS_POLL_H - r = poll (pollfds, nfds, timeout * 1000); + r = poll (pollfds, nfds, (timeout == -1 ? -1 : timeout * 1000)); for (i = 0; i