X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fzoom-c.c;h=e5987f52205a0c9f234fcb7df75253599943b1f5;hb=862b4ea8c948d499192abd1fd420000bec94d249;hp=492c19052c59d75090bf8088330b708bb1a1244a;hpb=8d92137c228ec24df2d62a8039635bb8ad19558d;p=yaz-moved-to-github.git diff --git a/src/zoom-c.c b/src/zoom-c.c index 492c190..e5987f5 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.43 2005-06-06 12:23:47 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_api = 0; +static int log_details = 0; typedef enum { zoom_pending, @@ -33,6 +51,17 @@ typedef enum { 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); +static void initlog() +{ + static int log_level_initialized = 0; + if (!log_level_initialized) + { + log_api = yaz_log_module_level("zoom"); + log_details = yaz_log_module_level("zoomdetails"); + log_level_initialized = 1; + } +} + static ZOOM_Event ZOOM_Event_create (int kind) { ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event)); @@ -90,21 +119,30 @@ static void set_dset_error (ZOOM_connection c, int error, const char *dset, const char *addinfo, const char *addinfo2) { + 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) + 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", - c->host_port ? c->host_port : "<>", dset, error, + yaz_log(log_api, "%p set_dset_error %s %s:%d %s %s", + c, c->host_port ? c->host_port : "<>", dset, error, addinfo ? addinfo : "", addinfo2 ? addinfo2 : ""); } @@ -177,7 +215,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 +228,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 +254,17 @@ ZOOM_connection_create (ZOOM_options options) { ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c)); + initlog(); + + yaz_log(log_api, "%p ZOOM_connection_create", c); + 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 +303,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,31 +336,45 @@ ZOOM_connection_connect(ZOOM_connection c, const char *val; ZOOM_task task; + initlog(); + + yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d", + c, host, portnum); + if (c->cs) { - yaz_log (LOG_DEBUG, "reconnect"); + yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c); c->reconnect_ok = 1; return; } - yaz_log(LOG_DEBUG, "connect"); + yaz_log(log_details, "%p ZOOM_connection_connect connect", c); xfree (c->proxy); val = ZOOM_options_get (c->options, "proxy"); if (val && *val) + { + yaz_log(log_details, "%p ZOOM_connection_connect proxy=%s", c, val); c->proxy = xstrdup (val); + } else c->proxy = 0; xfree (c->charset); val = ZOOM_options_get (c->options, "charset"); if (val && *val) + { + yaz_log(log_details, "%p ZOOM_connection_connect charset=%s", c, val); c->charset = xstrdup (val); + } else c->charset = 0; xfree (c->lang); val = ZOOM_options_get (c->options, "lang"); if (val && *val) + { + yaz_log(log_details, "%p ZOOM_connection_connect lang=%s", c, val); c->lang = xstrdup (val); + } else c->lang = 0; @@ -366,13 +392,21 @@ ZOOM_connection_connect(ZOOM_connection c, val = ZOOM_options_get (c->options, "cookie"); if (val && *val) - c->cookie_out = xstrdup (val); + { + yaz_log(log_details, "%p ZOOM_connection_connect cookie=%s", c, val); + c->cookie_out = xstrdup (val); + } val = ZOOM_options_get (c->options, "clientIP"); if (val && *val) + { + yaz_log(log_details, "%p ZOOM_connection_connect clientIP=%s", + c, val); c->client_IP = xstrdup (val); + } c->async = ZOOM_options_get_bool (c->options, "async", 0); + yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async); set_ZOOM_error(c, ZOOM_ERROR_NONE, 0); @@ -390,6 +424,7 @@ ZOOM_query_create(void) { ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s)); + yaz_log(log_details, "%p ZOOM_query_create", s); s->refcount = 1; s->z_query = 0; s->sort_spec = 0; @@ -406,7 +441,7 @@ ZOOM_query_destroy(ZOOM_query s) return; (s->refcount)--; - yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount); + yaz_log(log_details, "%p ZOOM_query_destroy count=%d", s, s->refcount); if (s->refcount == 0) { odr_destroy (s->odr); @@ -422,7 +457,12 @@ 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) + { + yaz_log(log_details, "%p ZOOM_query_prefix str=%s failed", s, str); + s->z_query = 0; return -1; + } + yaz_log(log_details, "%p ZOOM_query_prefix str=%s", s, str); return 0; } @@ -444,6 +484,8 @@ ZOOM_query_cql(ZOOM_query s, const char *str) s->z_query->which = Z_Query_type_104; s->z_query->u.type_104 = ext; + yaz_log(log_details, "%p ZOOM_query_cql str=%s", s, str); + return 0; } @@ -452,7 +494,12 @@ ZOOM_query_sortby(ZOOM_query s, const char *criteria) { s->sort_spec = yaz_sort_spec (s->odr, criteria); if (!s->sort_spec) + { + yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s failed", + s, criteria); return -1; + } + yaz_log(log_details, "%p ZOOM_query_sortby criteria=%s", s, criteria); return 0; } @@ -464,6 +511,7 @@ ZOOM_connection_destroy(ZOOM_connection c) ZOOM_resultset r; if (!c) return; + yaz_log(log_api, "%p ZOOM_connection_destroy", c); if (c->cs) cs_close (c->cs); for (r = c->resultsets; r; r = r->next) @@ -471,6 +519,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 +540,18 @@ 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_log(log_details, "%p ZOOM_resultset_addref 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); + initlog(); + + yaz_log(log_details, "%p ZOOM_resultset_create", r); r->refcount = 1; r->size = 0; r->odr = odr_createmem (ODR_ENCODE); @@ -537,6 +589,7 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) ZOOM_task task; const char *cp; + yaz_log(log_api, "%p ZOOM_connection_search set %p query %p", c, r, q); r->r_sort_spec = q->sort_spec; r->query = q; @@ -544,7 +597,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 +620,12 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q) { if (!c->cs) { - yaz_log(LOG_DEBUG, "NO COMSTACK"); + yaz_log(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(log_details, "ZOOM_connection_search: reconnect"); c->reconnect_ok = 1; } } @@ -587,26 +645,76 @@ 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; + + yaz_log(log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s", + r, r, sort_type, sort_spec); + if (!c) + return; + + if (c->host_port && c->proto == PROTO_HTTP) + { + if (!c->cs) + { + yaz_log(log_details, "%p ZOOM_resultset_sort: no comstack", r); + ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT); + } + else + { + yaz_log(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 = 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", - r, r->refcount); + yaz_log(log_details, "%p ZOOM_resultset_destroy r=%p count=%d", + r, 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 */ @@ -655,6 +763,8 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r, if (!r) return; + yaz_log(log_details, "%p ZOOM_resultset_retrieve force_sync=%d start=%d" + " count=%d", r, force_sync, start, count); c = r->connection; if (!c) return; @@ -663,12 +773,13 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r, { if (!c->cs) { - yaz_log(LOG_DEBUG, "NO COMSTACK"); + yaz_log(log_details, "%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_details, "%p ZOOM_resultset_retrieve: prepare " + "reconnect", r); c->reconnect_ok = 1; } } @@ -702,6 +813,8 @@ ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs, if (!r) return ; + yaz_log(log_api, "%p ZOOM_resultset_records r=%p start=%d count=%d", + r, r, start, count); if (count && recs) force_present = 1; ZOOM_resultset_retrieve (r, force_present, start, count); @@ -713,6 +826,19 @@ 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; @@ -723,9 +849,10 @@ static zoom_ret do_connect (ZOOM_connection c) else effective_host = c->host_port; - yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); + yaz_log(log_details, "%p do_connect effective_host=%s", c, 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 +863,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 +879,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 +939,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,12 +983,12 @@ 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_api, "%p encoding_APDU: encoding failed", c); set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0); odr_reset(out); return -1; } - + yaz_log(log_details, "%p encoding_APDU encoding OK", c); return 0; } @@ -870,7 +998,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_details, "%p send APDU type=%d", c, 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 +1012,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 +1035,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.43 $"); + 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 +1103,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 +1112,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 +1143,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 +1170,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 +1189,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; @@ -1183,6 +1309,8 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c) r = c->tasks->u.search.resultset; + yaz_log(log_details, "%p ZOOM_connection_send_search set=%p", c, r); + elementSetName = ZOOM_options_get (r->options, "elementSetName"); smallSetElementSetName = @@ -1201,6 +1329,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,31 +1407,26 @@ 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_details, "%p ZOOM_connection_send_search: allocating " + "set %s", c, r->setname); } else + { + yaz_log(log_details, "%p ZOOM_connection_send_search: using " + "default set", c); 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) +static void response_default_diag(ZOOM_connection c, Z_DefaultDiagFormat *r) { 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: @@ -1308,11 +1436,21 @@ static void response_diag (ZOOM_connection c, Z_DiagRec *p) addinfo = r->u.v3Addinfo; break; } + xfree (c->addinfo); + c->addinfo = 0; set_dset_error(c, *r->condition, yaz_z3950oid_to_str(r->diagnosticSetId, &oclass), addinfo, 0); } +static void response_diag(ZOOM_connection c, Z_DiagRec *p) +{ + if (p->which != Z_DiagRec_defaultFormat) + set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); + else + response_default_diag(c, p->u.defaultFormat); +} + ZOOM_API(ZOOM_record) ZOOM_record_clone (ZOOM_record srec) { @@ -1372,6 +1510,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 +1579,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 +1636,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 +1659,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 +1709,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 +1728,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 +1744,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 +1770,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 +1791,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 +1806,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 +1828,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; @@ -1795,13 +1977,7 @@ static void handle_records (ZOOM_connection c, Z_Records *sr, 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); - } + response_default_diag(c, sr->u.nonSurrogateDiagnostic); else if (sr && sr->which == Z_Records_multipleNSD) { if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1) @@ -1832,13 +2008,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); } } } @@ -1852,9 +2032,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"); - + if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) return ; @@ -1899,20 +2077,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); @@ -1931,7 +2100,7 @@ static zoom_ret send_sort (ZOOM_connection c) return zoom_complete; } -static zoom_ret send_present (ZOOM_connection c) +static zoom_ret send_present(ZOOM_connection c) { Z_APDU *apdu = 0; Z_PresentRequest *req = 0; @@ -1941,8 +2110,10 @@ static zoom_ret send_present (ZOOM_connection c) ZOOM_resultset resultset; if (!c->tasks) + { + yaz_log(log_details, "%p send_present no tasks", c); return zoom_complete; - + } switch (c->tasks->which) { case ZOOM_TASK_SEARCH: @@ -1954,13 +2125,19 @@ static zoom_ret send_present (ZOOM_connection c) resultset->count = c->tasks->u.retrieve.count; if (resultset->start >= resultset->size) + { + yaz_log(log_details, "%p send_present start=%d >= size=%d", + c, 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; } + yaz_log(log_details, "%p send_present start=%d count=%d", + c, resultset->start, resultset->count); syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax"); elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName"); @@ -1977,18 +2154,26 @@ static zoom_ret send_present (ZOOM_connection c) break; } if (i == resultset->count) + { + yaz_log(log_details, "%p send_present skip=%d no more to fetch", c, i); return zoom_complete; + } apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest); req = apdu->u.presentRequest; resultset->start += i; resultset->count -= i; + + if (i) + yaz_log(log_details, "%p send_present skip=%d", c, i); + *req->resultSetStartPoint = resultset->start + 1; *req->numberOfRecordsRequested = resultset->step>0 ? resultset->step : resultset->count; assert (*req->numberOfRecordsRequested > 0); + if (syntax && *syntax) req->preferredRecordSyntax = yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax); @@ -2099,6 +2284,8 @@ ZOOM_scanset_destroy (ZOOM_scanset scan) static zoom_ret send_package (ZOOM_connection c) { ZOOM_Event event; + + yaz_log(log_details, "%p send_package", c); if (!c->tasks) return zoom_complete; assert (c->tasks->which == ZOOM_TASK_PACKAGE); @@ -2115,6 +2302,8 @@ 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(log_details, "%p send_scan", c); if (!c->tasks) return zoom_complete; assert (c->tasks->which == ZOOM_TASK_SCAN); @@ -2192,7 +2381,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 +2498,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 +2552,199 @@ 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_xmlupdate_package(ZOOM_package p) +{ + Z_APDU *apdu = create_es_package(p, VAL_XMLUPDATE); + Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest; + Z_External *ext = (Z_External *) odr_malloc(p->odr_out, sizeof(*ext)); + const char *doc = ZOOM_options_get(p->options, "doc"); + + req->taskSpecificParameters = ext; + ext->direct_reference = req->packageType; + ext->descriptor = 0; + ext->indirect_reference = 0; + + ext->which = Z_External_octet; + ext->u.single_ASN1_type = (Odr_oct *) + odr_malloc (p->odr_out, sizeof(Odr_oct)); + + if (!doc) + doc = ""; + ext->u.single_ASN1_type->buf = (unsigned char*) + odr_strdup(p->odr_out, doc); + ext->u.single_ASN1_type->size = ext->u.single_ASN1_type->len = strlen(doc); + 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 +2758,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 +2774,29 @@ 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); + } + else if (!strcmp(type, "xmlupdate")) + { + apdu = create_xmlupdate_package(p); + } if (apdu) { if (encode_APDU(p->connection, apdu, p->odr_out) == 0) @@ -2465,21 +2869,20 @@ static int ZOOM_connection_exec_task (ZOOM_connection c) zoom_ret ret = zoom_complete; if (!task) - { - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task="); return 0; - } - yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d", - task->which, task->running); + yaz_log(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); + yaz_log(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"); + yaz_log(log_details, "%p ZOOM_connection_exec_task " + "task already running", c); return 0; } task->running = 1; @@ -2508,27 +2911,38 @@ 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_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)"); + yaz_log(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(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); + 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; @@ -2551,20 +2965,66 @@ static int es_response (ZOOM_connection c, ZOOM_options_setl (c->tasks->u.package->options, "targetReference", (char*) id->buf, id->len); } + if (res->taskPackage && + res->taskPackage->which == Z_External_octet) + { + Odr_oct *doc = res->taskPackage->u.octet_aligned; + ZOOM_options_setl (c->tasks->u.package->options, + "xmlUpdateDoc", (char*) doc->buf, doc->len); + } return 1; } +static void interpret_init_diag(ZOOM_connection c, + Z_DiagnosticFormat *diag) +{ + if (diag->num > 0) + { + Z_DiagnosticFormat_s *ds = diag->elements[0]; + if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec) + response_default_diag(c, ds->u.defaultDiagRec); + } +} + + +static void interpret_otherinformation_field(ZOOM_connection c, + Z_OtherInformation *ui) +{ + int i; + for (i = 0; i < ui->num_elements; i++) + { + Z_OtherInformationUnit *unit = ui->list[i]; + if (unit->which == Z_OtherInfo_externallyDefinedInfo && + unit->information.externallyDefinedInfo && + unit->information.externallyDefinedInfo->which == + Z_External_diag1) + { + interpret_init_diag(c, unit->information.externallyDefinedInfo->u.diag1); + } + } +} -static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) +static void recv_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_details, "%p recv_apdu apdu->which=%d", c, apdu->which); switch(apdu->which) { case Z_APDU_initResponse: + yaz_log(log_api, "%p recv_apd: Received Init response", c); 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 : ""); @@ -2576,7 +3036,12 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) initrs->implementationVersion : ""); if (!*initrs->result) { - set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); + Z_External *uif = initrs->userInformationField; + + set_ZOOM_error(c, ZOOM_ERROR_INIT, 0); /* default error */ + + if (uif && uif->which == Z_External_userInfo1) + interpret_otherinformation_field(c, uif->u.userInfo1); } else { @@ -2605,12 +3070,13 @@ 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, " - "language %s, select %d", + yaz_log(log_details, "%p recv_apdu target accepted: " + "charset %s, language %s, select %d", + c, charset ? charset : "none", lang ? lang : "none", sel); if (charset) ZOOM_connection_option_set (c, "negotiation-charset", @@ -2618,34 +3084,44 @@ 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); } } break; case Z_APDU_searchResponse: + yaz_log(log_api, "%p recv_apdu Search response", c); handle_search_response (c, apdu->u.searchResponse); if (send_sort_present (c) == zoom_complete) ZOOM_connection_remove_task (c); break; case Z_APDU_presentResponse: + yaz_log(log_api, "%p recv_apdu Present response", c); handle_present_response (c, apdu->u.presentResponse); if (send_present (c) == zoom_complete) ZOOM_connection_remove_task (c); break; case Z_APDU_sortResponse: + yaz_log(log_api, "%p recv_apdu Sort response", c); sort_response (c, apdu->u.sortResponse); if (send_present (c) == zoom_complete) ZOOM_connection_remove_task (c); break; case Z_APDU_scanResponse: + yaz_log(log_api, "%p recv_apdu Scan response", c); scan_response (c, apdu->u.scanResponse); ZOOM_connection_remove_task (c); break; case Z_APDU_extendedServicesResponse: + yaz_log(log_api, "%p recv_apdu Extended Services response", c); es_response (c, apdu->u.extendedServicesResponse); ZOOM_connection_remove_task (c); break; case Z_APDU_close: + yaz_log(log_api, "%p recv_apdu Close PDU", c); if (c->reconnect_ok) { do_close(c); @@ -2659,6 +3135,7 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu) } break; default: + yaz_log(log_api, "%p Received unknown PDU", c); set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0); do_close(c); } @@ -2688,7 +3165,7 @@ static void handle_srw_response(ZOOM_connection c, resultset->size = 0; - yaz_log(LOG_DEBUG, "got SRW response OK"); + yaz_log(log_details, "%p handle_srw_response got SRW response OK", c); if (res->numberOfRecords) resultset->size = *res->numberOfRecords; @@ -2716,7 +3193,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 +3202,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 +3228,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_details, "%p handle_http", c); 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 +3245,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 +3260,6 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres) } else ret = -1; - odr_destroy(o); } if (ret) { @@ -2809,10 +3293,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_details, "%p do_read len=%d more=%d", c, r, more); if (r == 1) return 0; if (r <= 0) @@ -2821,7 +3304,7 @@ static int do_read (ZOOM_connection c) { do_close (c); c->reconnect_ok = 0; - yaz_log (LOG_DEBUG, "reconnect read"); + yaz_log(log_details, "%p do_read reconnect read", c); c->tasks->running = 0; ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); } @@ -2843,11 +3326,17 @@ 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) - handle_apdu (c, gdu->u.z3950); + recv_apdu (c, gdu->u.z3950); else if (gdu->which == Z_GDU_HTTP_Response) { #if HAVE_XML2 @@ -2870,14 +3359,15 @@ 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_details, "%p do_write_ex len=%d", c, len_out); + if ((r = cs_put(c->cs, buf_out, len_out)) < 0) { + yaz_log(log_details, "%p do_write_ex write failed", c); if (c->reconnect_ok) { do_close (c); c->reconnect_ok = 0; - yaz_log (LOG_DEBUG, "reconnect write"); + yaz_log(log_details, "%p do_write_ex reconnect write", c); c->tasks->running = 0; ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT); return zoom_pending; @@ -2896,12 +3386,14 @@ 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_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); + yaz_log(log_details, "%p do_write_ex write complete mask=%d", + c, c->mask); } return zoom_pending; } @@ -2920,11 +3412,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 +3487,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 +3507,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 +3530,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_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d", c, mask, r); if (r == CS_NONE) @@ -3045,7 +3546,8 @@ 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_details, "%p ZOOM_connection_do_io " + "cs_rcvconnect returned %d", c, ret); if (ret == 1) { c->mask = ZOOM_SELECT_EXCEPT; @@ -3058,6 +3560,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 +3601,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 +3659,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 +3706,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