X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=zutil%2Fzoom-c.c;h=648fba2beb2767935be940c60b062efa97f5e6be;hb=94c77ca831e55b3dee31184347f25dd0583085d2;hp=48a247299284d09593efe1231060ee8e4f568689;hpb=4ca818ec201c9dcf5461e419002c95fd2eac620e;p=yaz-moved-to-github.git diff --git a/zutil/zoom-c.c b/zutil/zoom-c.c index 48a2472..648fba2 100644 --- a/zutil/zoom-c.c +++ b/zutil/zoom-c.c @@ -2,7 +2,7 @@ * Copyright (c) 2000-2003, Index Data * See the file LICENSE for details. * - * $Id: zoom-c.c,v 1.27 2003-02-21 12:08:59 adam Exp $ + * $Id: zoom-c.c,v 1.39 2003-07-14 12:59:23 adam Exp $ * * ZOOM layer for C, connections, result sets, queries. */ @@ -68,7 +68,10 @@ static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c) { ZOOM_Event event = c->m_queue_front; if (!event) + { + c->last_event = ZOOM_EVENT_NONE; return 0; + } assert (c->m_queue_back); c->m_queue_front = event->prev; if (c->m_queue_front) @@ -106,11 +109,13 @@ static void set_dset_error (ZOOM_connection c, int error, addinfo2 ? addinfo2 : ""); } +#if HAVE_XML2 static void set_HTTP_error (ZOOM_connection c, int error, const char *addinfo, const char *addinfo2) { set_dset_error(c, error, "HTTP", addinfo, addinfo2); } +#endif static void set_ZOOM_error (ZOOM_connection c, int error, const char *addinfo) @@ -172,6 +177,7 @@ 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: @@ -502,6 +508,7 @@ ZOOM_resultset ZOOM_resultset_create () r->setname = 0; r->schema = 0; r->count = 0; + r->step = 0; r->record_cache = 0; r->r_sort_spec = 0; r->query = 0; @@ -537,6 +544,7 @@ 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); r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1); cp = ZOOM_options_get (r->options, "setname"); if (cp) @@ -591,8 +599,14 @@ ZOOM_resultset_destroy(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); + } if (r->connection) { /* remove ourselves from the resultsets in connection */ @@ -712,29 +726,25 @@ static zoom_ret do_connect (ZOOM_connection c) yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host); assert (!c->cs); + c->cs = cs_create_host (effective_host, 0, &add); - if (memcmp(c->host_port, "http:", 5) == 0) + if (c->cs && c->cs->protocol == PROTO_HTTP) { #if HAVE_XML2 - const char *path; + const char *path = 0; + c->proto = PROTO_HTTP; - effective_host = c->host_port + 5; - if (*effective_host == '/') - effective_host++; - if (*effective_host == '/') - effective_host++; - if (!(path = strchr(effective_host, '/'))) - path = "/"; + cs_get_host_args(c->host_port, &path); xfree(c->path); - c->path = xstrdup(path); + c->path = xmalloc(strlen(path)+2); + c->path[0] = '/'; + strcpy (c->path+1, path); #else - c->state = STATE_IDLE; set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW"); + do_close(c); return zoom_complete; #endif } - c->cs = cs_create_host (effective_host, 0, &add); - if (c->cs) { int ret = cs_connect (c->cs, add); @@ -979,8 +989,9 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c) } #if HAVE_XML2 -static zoom_ret send_srw (ZOOM_connection c, Z_SRW_searchRetrieve *sr) +static zoom_ret send_srw (ZOOM_connection c, Z_SRW_PDU *sr) { + char ctype[50]; Z_SOAP_Handler h[2] = { {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec}, {0, 0, 0} @@ -993,8 +1004,38 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_searchRetrieve *sr) gdu = z_get_HTTP_Request(c->odr_out); gdu->u.HTTP_Request->path = c->path; + + if (c->host_port) + { + const char *cp0 = strstr(c->host_port, "://"); + const char *cp1 = 0; + if (cp0) + cp0 = cp0+3; + else + cp0 = c->host_port; + + cp1 = strchr(cp0, '/'); + if (!cp1) + cp1 = cp0+strlen(cp0); + + if (cp0 && cp1) + { + char *h = odr_malloc(c->odr_out, cp1 - cp0 + 1); + memcpy (h, cp0, cp1 - cp0); + h[cp1-cp0] = '\0'; + z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, + "host", h); + } + } + + strcpy(ctype, "text/xml"); + if (c->charset && strlen(c->charset) < 20) + { + strcat(ctype, "; charset="); + strcat(ctype, c->charset); + } z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, - "Content-Type", "text/xml"); + "Content-Type", ctype); z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "SOAPAction", "\"\""); p->which = Z_SOAP_generic; @@ -1004,9 +1045,10 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_searchRetrieve *sr) p->u.generic->p = sr; p->ns = "http://schemas.xmlsoap.org/soap/envelope/"; - ret = z_soap_codec(o, &p, - &gdu->u.HTTP_Request->content_buf, - &gdu->u.HTTP_Request->content_len, h); + ret = z_soap_codec_enc(o, &p, + &gdu->u.HTTP_Request->content_buf, + &gdu->u.HTTP_Request->content_len, h, + c->charset); if (!z_GDU(c->odr_out, &gdu, 0, 0)) return zoom_complete; @@ -1026,7 +1068,8 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c) { int i; ZOOM_resultset resultset = 0; - Z_SRW_searchRetrieve *sr = 0; + Z_SRW_PDU *sr = 0; + const char *recordPacking = 0; if (c->error) /* don't continue on error */ return zoom_complete; @@ -1066,12 +1109,15 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c) if (resultset->query->z_query->which == Z_Query_type_104 && resultset->query->z_query->u.type_104->which == Z_External_CQL) { - sr->u.request->query = resultset->query->z_query->u.type_104->u.cql; + + sr->u.request->query_type = Z_SRW_query_type_cql; + sr->u.request->query.cql =resultset->query->z_query->u.type_104->u.cql; } else if (resultset->query->z_query->which == Z_Query_type_1 && resultset->query->z_query->u.type_1) { - sr->u.request->pQuery = resultset->query->query_string; + sr->u.request->query_type = Z_SRW_query_type_pqf; + sr->u.request->query.pqf = resultset->query->query_string; } else { @@ -1079,10 +1125,15 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c) return zoom_complete; } sr->u.request->startRecord = odr_intdup (c->odr_out, resultset->start + 1); - sr->u.request->maximumRecords = odr_intdup (c->odr_out, resultset->count); + sr->u.request->maximumRecords = odr_intdup ( + c->odr_out, resultset->step>0 ? resultset->step : resultset->count); sr->u.request->recordSchema = resultset->schema; - + recordPacking = ZOOM_resultset_option_get (resultset, "recordPacking"); + + if (recordPacking) + sr->u.request->recordPacking = odr_strdup(c->odr_out, recordPacking); + return send_srw(c, sr); } #else @@ -1149,8 +1200,7 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c) /* Regular piggyback - do it unless we're going to do sort */ *search_req->largeSetLowerBound = 2000000000; *search_req->smallSetUpperBound = 0; - *search_req->mediumSetPresentNumber = r->count; - smallSetElementSetName = 0; + *search_req->mediumSetPresentNumber = r->step>0 ? r->step : r->count; } else { @@ -1255,6 +1305,8 @@ ZOOM_record_clone (ZOOM_record srec) nrec = (ZOOM_record) xmalloc (sizeof(*nrec)); nrec->odr = odr_createmem(ODR_DECODE); nrec->wrbuf_marc = 0; + nrec->wrbuf_iconv = 0; + nrec->wrbuf_opac = 0; odr_setbuf (nrec->odr, buf, size, 0); z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0); @@ -1271,8 +1323,14 @@ ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos) ZOOM_API(ZOOM_record) ZOOM_resultset_record (ZOOM_resultset r, size_t pos) { - ZOOM_resultset_retrieve (r, 1, pos, 1); - return ZOOM_resultset_record_immediate (r, pos); + ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos); + + if (!rec) + { + ZOOM_resultset_retrieve (r, 1, pos, 1); + rec = ZOOM_resultset_record_immediate (r, pos); + } + return rec; } ZOOM_API(void) @@ -1282,13 +1340,86 @@ ZOOM_record_destroy (ZOOM_record rec) return; if (rec->wrbuf_marc) wrbuf_free (rec->wrbuf_marc, 1); + if (rec->wrbuf_iconv) + wrbuf_free (rec->wrbuf_iconv, 1); + if (rec->wrbuf_opac) + wrbuf_free (rec->wrbuf_opac, 1); odr_destroy (rec->odr); xfree (rec); } +static const char *record_iconv_return(ZOOM_record rec, int *len, + const char *buf, int sz, + const char *record_charset) +{ + char to[40]; + char from[40]; + yaz_iconv_t cd = 0; + + *from = '\0'; + strcpy(to, "UTF-8"); + if (record_charset && *record_charset) + { + /* Use "from,to" or just "from" */ + const char *cp =strchr(record_charset, ','); + int clen = strlen(record_charset); + if (cp && cp[1]) + { + strncpy( to, cp+1, sizeof(to)-1); + to[sizeof(to)-1] = '\0'; + clen = cp - record_charset; + } + if (clen > sizeof(from)-1) + clen = sizeof(from)-1; + + if (clen) + strncpy(from, record_charset, clen); + from[clen] = '\0'; + } + + if (*from && *to && (cd = yaz_iconv_open(to, from))) + { + char outbuf[12]; + size_t inbytesleft = sz; + const char *inp = buf; + + if (!rec->wrbuf_iconv) + rec->wrbuf_iconv = wrbuf_alloc(); + + wrbuf_rewind(rec->wrbuf_iconv); + + while (inbytesleft) + { + size_t outbytesleft = sizeof(outbuf); + char *outp = outbuf; + size_t r = yaz_iconv (cd, (char**) &inp, + &inbytesleft, + &outp, &outbytesleft); + if (r == (size_t) (-1)) + { + int e = yaz_iconv_error(cd); + if (e != YAZ_ICONV_E2BIG) + break; + } + wrbuf_write(rec->wrbuf_iconv, outbuf, outp - outbuf); + } + wrbuf_puts(rec->wrbuf_iconv, ""); + buf = wrbuf_buf(rec->wrbuf_iconv); + sz = wrbuf_len(rec->wrbuf_iconv); + yaz_iconv_close(cd); + } + if (len) + *len = sz; + return buf; +} + ZOOM_API(const char *) -ZOOM_record_get (ZOOM_record rec, const char *type, int *len) +ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len) { + char type[40]; + char charset[40]; + const char *cp; + int i; Z_NamePlusRecord *npr; if (len) @@ -1299,6 +1430,34 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) npr = rec->npr; if (!npr) return 0; + + cp = type_spec; + for (i = 0; cp[i] && i < sizeof(type)-1; i++) + { + if (cp[i] == ';' || cp[i] == ' ') + break; + type[i] = cp[i]; + } + type[i] = '\0'; + charset[0] = '\0'; + if (type_spec[i] == ';') + { + i++; + while (type_spec[i] == ' ') + i++; + if (!strncmp(type_spec+i, "charset=", 8)) + { + cp = type_spec+i+8; + for (i = 0; cp[i] && i < sizeof(charset)-1; i++) + { + if (cp[i] == ';' || cp[i] == ' ') + break; + charset[i] = cp[i]; + } + charset[i] = '\0'; + } + } + if (!strcmp (type, "database")) { if (len) @@ -1326,12 +1485,19 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) { Z_External *r = (Z_External *) npr->u.databaseRecord; oident *ent = oid_getentbyoid(r->direct_reference); - + + /* render bibliographic record .. */ + if (r->which == Z_External_OPAC) + { + r = r->u.opac->bibliographicRecord; + if (!r) + return 0; + ent = oid_getentbyoid(r->direct_reference); + } if (r->which == Z_External_sutrs) - { - if (len) *len = r->u.sutrs->len; - return (const char *) r->u.sutrs->buf; - } + return record_iconv_return(rec, len, + r->u.sutrs->buf, r->u.sutrs->len, + charset); else if (r->which == Z_External_octet) { yaz_marc_t mt; @@ -1355,16 +1521,18 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) r->u.octet_aligned->len, rec->wrbuf_marc) > 0) { - if (len) - *len = wrbuf_len(rec->wrbuf_marc); yaz_marc_destroy(mt); - return wrbuf_buf(rec->wrbuf_marc); + return record_iconv_return(rec, len, + wrbuf_buf(rec->wrbuf_marc), + wrbuf_len(rec->wrbuf_marc), + charset); } yaz_marc_destroy(mt); } - if (len) - *len = r->u.octet_aligned->len; - return (const char *) r->u.octet_aligned->buf; + return record_iconv_return(rec, len, + (const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len, + charset); } else if (r->which == Z_External_grs1) { @@ -1372,9 +1540,10 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) rec->wrbuf_marc = wrbuf_alloc(); wrbuf_rewind (rec->wrbuf_marc); yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0); - if (len) - *len = wrbuf_len(rec->wrbuf_marc); - return wrbuf_buf(rec->wrbuf_marc); + return record_iconv_return(rec, len, + wrbuf_buf(rec->wrbuf_marc), + wrbuf_len(rec->wrbuf_marc), + charset); } return 0; } @@ -1383,12 +1552,21 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) { Z_External *r = (Z_External *) npr->u.databaseRecord; oident *ent = oid_getentbyoid(r->direct_reference); + + /* render bibliographic record .. */ + if (r->which == Z_External_OPAC) + { + r = r->u.opac->bibliographicRecord; + if (!r) + return 0; + ent = oid_getentbyoid(r->direct_reference); + } if (r->which == Z_External_sutrs) - { - if (len) *len = r->u.sutrs->len; - return (const char *) r->u.sutrs->buf; - } + return record_iconv_return(rec, len, + (const char *) r->u.sutrs->buf, + r->u.sutrs->len, + charset); else if (r->which == Z_External_octet) { yaz_marc_t mt; @@ -1417,15 +1595,18 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) r->u.octet_aligned->len, rec->wrbuf_marc) > 0) { - if (len) - *len = wrbuf_len(rec->wrbuf_marc); yaz_marc_destroy(mt); - return wrbuf_buf(rec->wrbuf_marc); + return record_iconv_return(rec, len, + wrbuf_buf(rec->wrbuf_marc), + wrbuf_len(rec->wrbuf_marc), + charset); } yaz_marc_destroy(mt); } - if (len) *len = r->u.octet_aligned->len; - return (const char *) r->u.octet_aligned->buf; + return record_iconv_return(rec, len, + (const char *) r->u.octet_aligned->buf, + r->u.octet_aligned->len, + charset); } else if (r->which == Z_External_grs1) { @@ -1450,7 +1631,7 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) if (len) *len = r->u.octet_aligned->len; return (const char *) r->u.octet_aligned->buf; } - else /* grs-1, explain, ... */ + else /* grs-1, explain, OPAC, ... */ { if (len) *len = -1; return (const char *) npr->u.databaseRecord; @@ -1464,6 +1645,23 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len) return (const char *) npr->u.databaseRecord; return 0; } + else if (npr->which == Z_NamePlusRecord_databaseRecord && + !strcmp (type, "opac")) + + { + Z_External *r = (Z_External *) npr->u.databaseRecord; + if (r->which == Z_External_OPAC) + { + if (!rec->wrbuf_opac) + rec->wrbuf_opac = wrbuf_alloc(); + wrbuf_rewind (rec->wrbuf_opac); + yaz_display_OPAC(rec->wrbuf_opac, r->u.opac, 0); + return record_iconv_return(rec, len, + wrbuf_buf(rec->wrbuf_opac), + wrbuf_len(rec->wrbuf_opac), + charset); + } + } return 0; } @@ -1485,6 +1683,9 @@ static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, const char *syntax = ZOOM_resultset_option_get (r, "preferredRecordSyntax"); + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD); + ZOOM_connection_put_event(r->connection, event); + for (rc = r->record_cache; rc; rc = rc->next) { if (pos == rc->pos) @@ -1505,6 +1706,8 @@ static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, rc->rec.npr = npr; rc->rec.odr = 0; rc->rec.wrbuf_marc = 0; + rc->rec.wrbuf_iconv = 0; + rc->rec.wrbuf_opac = 0; if (elementSetName) rc->elementSetName = odr_strdup (r->odr, elementSetName); else @@ -1624,11 +1827,15 @@ static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr) static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr) { ZOOM_resultset resultset; - + ZOOM_Event event; + yaz_log (LOG_DEBUG, "got search response"); - + if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH) return ; + + event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH); + ZOOM_connection_put_event(c, event); resultset = c->tasks->u.search.resultset; @@ -1754,7 +1961,8 @@ static zoom_ret send_present (ZOOM_connection c) resultset->start += i; resultset->count -= i; *req->resultSetStartPoint = resultset->start + 1; - *req->numberOfRecordsRequested = resultset->count; + *req->numberOfRecordsRequested = resultset->step>0 ? + resultset->step : resultset->count; assert (*req->numberOfRecordsRequested > 0); if (syntax && *syntax) @@ -2075,21 +2283,26 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p) req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *) odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep)); - req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *) - odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem)); - str = ZOOM_options_get(p->options, "itemorder-setname"); if (!str) str = "default"; - req->u.esRequest->notToKeep->resultSetItem->resultSetId = - nmem_strdup (p->odr_out->mem, str); - req->u.esRequest->notToKeep->resultSetItem->item = - (int *) odr_malloc(p->odr_out, sizeof(int)); + + if (!*str) + req->u.esRequest->notToKeep->resultSetItem = 0; + else + { + req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *) + odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem)); + + req->u.esRequest->notToKeep->resultSetItem->resultSetId = + nmem_strdup (p->odr_out->mem, str); + req->u.esRequest->notToKeep->resultSetItem->item = + (int *) odr_malloc(p->odr_out, sizeof(int)); - str = ZOOM_options_get(p->options, "itemorder-item"); - *req->u.esRequest->notToKeep->resultSetItem->item = - (str ? atoi(str) : 1); - + str = ZOOM_options_get(p->options, "itemorder-item"); + *req->u.esRequest->notToKeep->resultSetItem->item = + (str ? atoi(str) : 1); + } req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p); return req; @@ -2403,6 +2616,7 @@ static void handle_srw_response(ZOOM_connection c, ZOOM_resultset resultset = 0; int i; NMEM nmem; + ZOOM_Event event; if (!c->tasks) return; @@ -2414,6 +2628,9 @@ static void handle_srw_response(ZOOM_connection c, else return ; + event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH); + ZOOM_connection_put_event(c, event); + resultset->size = 0; yaz_log(LOG_DEBUG, "got SRW response OK"); @@ -2488,7 +2705,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_searchRetrieve *sr = soap_package->u.generic->p; + Z_SRW_PDU *sr = soap_package->u.generic->p; if (sr->which == Z_SRW_searchRetrieve_response) handle_srw_response(c, sr->u.response); else @@ -2562,6 +2779,7 @@ static int do_read (ZOOM_connection c) { Z_GDU *gdu; ZOOM_Event event; + odr_reset (c->odr_in); odr_setbuf (c->odr_in, c->buf_in, r, 0); event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);