* Copyright (c) 2000-2003, Index Data
* See the file LICENSE for details.
*
- * $Id: zoom-c.c,v 1.29 2003-03-11 11:09:17 adam Exp $
+ * $Id: zoom-c.c,v 1.41 2003-08-19 12:32:29 adam Exp $
*
* ZOOM layer for C, connections, result sets, queries.
*/
{
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)
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)
switch (task->which)
{
case ZOOM_TASK_SEARCH:
+
ZOOM_resultset_destroy (task->u.search.resultset);
break;
case ZOOM_TASK_RETRIEVE:
r->setname = 0;
r->schema = 0;
r->count = 0;
+ r->step = 0;
r->record_cache = 0;
r->r_sort_spec = 0;
r->query = 0;
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)
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 */
c->proto = PROTO_HTTP;
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
set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
do_close(c);
assert (a);
if (encode_APDU(c, a, c->odr_out))
return zoom_complete;
+ yaz_log(LOG_DEBUG, "send APDU type=%d", a->which);
c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
ZOOM_connection_put_event (c, event);
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)
{
int i;
ZOOM_resultset resultset = 0;
Z_SRW_PDU *sr = 0;
+ const char *recordPacking = 0;
if (c->error) /* don't continue on error */
return zoom_complete;
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
{
/* 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->smallSetUpperBound = 1;
+ *search_req->mediumSetPresentNumber = r->step>0 ? r->step : r->count;
}
else
{
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);
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)
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)
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)
{
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;
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)
{
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;
}
{
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;
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)
{
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;
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;
}
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)
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
static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
{
ZOOM_resultset resultset;
-
+ ZOOM_Event event;
+
yaz_log (LOG_DEBUG, "got search response");
-
+
if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
return ;
+
+ event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
+ ZOOM_connection_put_event(c, event);
resultset = c->tasks->u.search.resultset;
resultset->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)
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;
Z_InitResponse *initrs;
c->mask = 0;
- yaz_log (LOG_DEBUG, "handle_apdu type=%d", apdu->which);
+ yaz_log (LOG_DEBUG, "recv APDU type=%d", apdu->which);
switch(apdu->which)
{
case Z_APDU_initResponse:
ZOOM_resultset resultset = 0;
int i;
NMEM nmem;
+ ZOOM_Event event;
if (!c->tasks)
return;
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");
static int do_read (ZOOM_connection c)
{
- int r;
+ int r, more;
ZOOM_Event event;
event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
ZOOM_connection_put_event (c, event);
- yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
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);
if (r == 1)
return 0;
if (r <= 0)
{
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);