Remove unused local member wrbuf_opac
[yaz-moved-to-github.git] / src / zoom-c.c
index 796d904..6f6856e 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2009 Index Data
  * See the file LICENSE for details.
- *
- * $Id: zoom-c.c,v 1.143 2007-08-23 14:23:23 adam Exp $
  */
 /**
  * \file zoom-c.c
@@ -43,13 +41,30 @@ 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 char *cql2pqf(ZOOM_connection c, const char *cql);
 
+ZOOM_API(const char *) ZOOM_get_event_str(int event)
+{
+    static const char *ar[] = {
+        "NONE",
+        "CONNECT",
+        "SEND_DATA",
+        "RECV_DATA",
+        "TIMEOUT",
+        "UNKNOWN",
+        "SEND_APDU",
+        "RECV_APDU",
+        "RECV_RECORD",
+        "RECV_SEARCH",
+        "END"
+    };
+    return ar[event];
+}
 
 /*
  * This wrapper is just for logging failed lookups.  It would be nicer
  * if it could cause failure when a lookup fails, but that's hard.
  */
 static Odr_oid *zoom_yaz_str_to_z3950oid(ZOOM_connection c,
-                                     int oid_class, const char *str) {
+                                     oid_class oid_class, const char *str) {
     Odr_oid *res = yaz_string_to_oid_odr(yaz_oid_std(), oid_class, str,
                                      c->odr_out);
     if (res == 0)
@@ -174,6 +189,15 @@ static void set_dset_error(ZOOM_connection c, int error,
     }
 }
 
+static int uri_to_code(const char *uri)
+{
+    int code = 0;       
+    const char *cp;
+    if ((cp = strrchr(uri, '/')))
+        code = atoi(cp+1);
+    return code;
+}
+
 #if YAZ_HAVE_XML2
 static void set_HTTP_error(ZOOM_connection c, int error,
                            const char *addinfo, const char *addinfo2)
@@ -185,13 +209,7 @@ static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
 {
     const char *uri = d->uri;
     if (uri)
-    {
-        int code = 0;       
-        const char *cp;
-        if ((cp = strrchr(uri, '/')))
-            code = atoi(cp+1);
-        set_dset_error(c, code, uri, d->details, 0);
-    }
+        set_dset_error(c, uri_to_code(uri), uri, d->details, 0);
 }
 
 #endif
@@ -243,7 +261,7 @@ void ZOOM_connection_show_task(ZOOM_task task)
         yaz_log(YLOG_LOG, "connect p=%p", task);
         break;
     case ZOOM_TASK_SCAN:
-        yaz_log(YLOG_LOG, "scant p=%p", task);
+        yaz_log(YLOG_LOG, "scan p=%p", task);
         break;
     }
 }
@@ -396,7 +414,7 @@ ZOOM_API(ZOOM_connection)
     c->m_queue_front = 0;
     c->m_queue_back = 0;
 
-    c->sru_version = xstrdup("1.1");
+    c->sru_version = 0;
     return c;
 }
 
@@ -461,6 +479,14 @@ ZOOM_API(void)
     set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
     ZOOM_connection_remove_tasks(c);
 
+    if (ZOOM_options_get_bool(c->options, "apdulog", 0))
+    {
+        c->odr_print = odr_createmem(ODR_PRINT);
+        odr_setprint(c->odr_print, yaz_log_file());
+    }
+    else
+        c->odr_print = 0;
+
     if (c->cs)
     {
         yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
@@ -545,7 +571,7 @@ ZOOM_API(void)
 
     xfree(c->sru_version);
     val = ZOOM_options_get(c->options, "sru_version");
-    c->sru_version = xstrdup(val ? val : "1.1");
+    c->sru_version = xstrdup(val ? val : "1.2");
 
     ZOOM_options_set(c->options, "host", c->host_port);
 
@@ -773,6 +799,11 @@ ZOOM_API(void)
     xfree(c->diagset);
     odr_destroy(c->odr_in);
     odr_destroy(c->odr_out);
+    if (c->odr_print)
+    {
+        odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
+        odr_destroy(c->odr_print);
+    }
     ZOOM_options_destroy(c->options);
     ZOOM_connection_remove_tasks(c);
     ZOOM_connection_remove_events(c);
@@ -899,6 +930,7 @@ ZOOM_API(ZOOM_resultset)
     task->u.search.resultset = r;
     task->u.search.start = start;
     task->u.search.count = count;
+    task->u.search.recv_search_fired = 0;
 
     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax"); 
     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
@@ -986,8 +1018,6 @@ ZOOM_API(void)
                 wrbuf_destroy(rc->rec.wrbuf_marc);
             if (rc->rec.wrbuf_iconv)
                 wrbuf_destroy(rc->rec.wrbuf_iconv);
-            if (rc->rec.wrbuf_opac)
-                wrbuf_destroy(rc->rec.wrbuf_opac);
         }
         r->record_hash[i] = 0;
     }
@@ -1300,6 +1330,8 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
         odr_reset(out);
         return -1;
     }
+    if (c->odr_print)
+        z_APDU(c->odr_print, &a, 0, 0);
     yaz_log(log_details, "%p encoding_APDU encoding OK", c);
     return 0;
 }
@@ -1328,7 +1360,6 @@ static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
     Z_InitRequest *ireq = apdu->u.initRequest;
     Z_IdAuthentication *auth = (Z_IdAuthentication *)
         odr_malloc(c->odr_out, sizeof(*auth));
-    char *version;
 
     ODR_MASK_SET(ireq->options, Z_Options_search);
     ODR_MASK_SET(ireq->options, Z_Options_present);
@@ -1341,11 +1372,10 @@ static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
     
-    /* 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->implementationId);
     
     ireq->implementationName = 
         odr_prepend(c->odr_out,
@@ -1353,14 +1383,10 @@ static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
                     odr_prepend(c->odr_out, "ZOOM-C",
                                 ireq->implementationName));
     
-    version = odr_strdup(c->odr_out, "$Revision: 1.143 $");
-    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->implementationVersion);
     
     *ireq->maximumRecordSize = c->maximum_record_size;
     *ireq->preferredMessageSize = c->preferred_message_size;
@@ -1368,15 +1394,9 @@ static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
     if (c->group || c->password)
     {
         Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
-        pass->groupId = 0;
-        if (c->group)
-            pass->groupId = odr_strdup(c->odr_out, c->group);
-        pass->userId = 0;
-        if (c->user)
-            pass->userId = odr_strdup(c->odr_out, c->user);
-        pass->password = 0;
-        if (c->password)
-            pass->password = odr_strdup(c->odr_out, c->password);
+        pass->groupId = odr_strdup_null(c->odr_out, c->group);
+        pass->userId = odr_strdup_null(c->odr_out, c->user);
+        pass->password = odr_strdup_null(c->odr_out, c->password);
         auth->which = Z_IdAuthentication_idPass;
         auth->u.idPass = pass;
         ireq->idAuthentication = auth;
@@ -1417,8 +1437,17 @@ static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
 {
     Z_GDU *gdu;
     ZOOM_Event event;
-
-    gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port, c->path);
+    const char *database =  ZOOM_options_get(c->options, "databaseName");
+    char *fdatabase = 0;
+    
+    if (database)
+    {
+        fdatabase = (char *) odr_malloc(c->odr_out, strlen(database)+2);
+        strcpy(fdatabase, "/");
+        strcat(fdatabase, database);
+    }
+    gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
+                                       fdatabase ? fdatabase : c->path);
 
     if (c->sru_mode == zoom_sru_get)
     {
@@ -1434,6 +1463,8 @@ static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
     }
     if (!z_GDU(c->odr_out, &gdu, 0, 0))
         return zoom_complete;
+    if (c->odr_print)
+        z_GDU(c->odr_print, &gdu, 0, 0);
     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
         
     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
@@ -1444,6 +1475,16 @@ static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
 #endif
 
 #if YAZ_HAVE_XML2
+static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
+{
+    Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
+    sr->username = c->user;
+    sr->password = c->password;
+    return sr;
+}
+#endif
+
+#if YAZ_HAVE_XML2
 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
 {
     int i;
@@ -1459,7 +1500,8 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
     {
     case ZOOM_TASK_SEARCH:
         resultset = c->tasks->u.search.resultset;
-        resultset->setname = xstrdup("default");
+        if (!resultset->setname)
+            resultset->setname = xstrdup("default");
         ZOOM_options_set(resultset->options, "setname", resultset->setname);
         start = &c->tasks->u.search.start;
         count = &c->tasks->u.search.count;
@@ -1469,7 +1511,7 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
 
         start = &c->tasks->u.retrieve.start;
         count = &c->tasks->u.retrieve.count;
-        
+
         if (*start >= resultset->size)
             return zoom_complete;
         if (*start + *count > resultset->size)
@@ -1500,9 +1542,7 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
     }
     assert(resultset->query);
         
-    sr = yaz_srw_get_pdu(c->odr_out, Z_SRW_searchRetrieve_request,
-                         c->sru_version);
-
+    sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
     if (resultset->query->z_query->which == Z_Query_type_104
         && resultset->query->z_query->u.type_104->which == Z_External_CQL)
     {
@@ -1522,7 +1562,8 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
     }
     sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
     sr->u.request->maximumRecords = odr_intdup(
-        c->odr_out, resultset->step>0 ? resultset->step : *count);
+        c->odr_out, (resultset->step > 0 && resultset->step < *count) ? 
+        resultset->step : *count);
     sr->u.request->recordSchema = resultset->schema;
     
     option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
@@ -1530,8 +1571,7 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
         sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
 
     option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
-    if (option_val)
-        sr->extra_args = odr_strdup(c->odr_out, option_val);
+    yaz_encode_sru_extra(sr, c->odr_out, option_val);
     return send_srw(c, sr);
 }
 #else
@@ -1734,10 +1774,14 @@ ZOOM_API(ZOOM_record)
     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);
     
+    nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
+    nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
+    nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
+    nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
+    nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
     odr_destroy(odr_enc);
     return nrec;
 }
@@ -1783,21 +1827,16 @@ ZOOM_API(void)
         wrbuf_destroy(rec->wrbuf_marc);
     if (rec->wrbuf_iconv)
         wrbuf_destroy(rec->wrbuf_iconv);
-    if (rec->wrbuf_opac)
-        wrbuf_destroy(rec->wrbuf_opac);
     odr_destroy(rec->odr);
     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)
+
+static yaz_iconv_t iconv_create_charset(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");
@@ -1819,63 +1858,71 @@ static const char *marc_iconv_return(ZOOM_record rec, int marc_type,
             strncpy(from, record_charset, clen);
         from[clen] = '\0';
     }
-
     if (*from && *to)
-    {
         cd = yaz_iconv_open(to, from);
-        yaz_marc_iconv(mt, cd);
-    }
+    return cd;
+}
 
+static const char *return_marc_record(ZOOM_record rec, int marc_type,
+                                      int *len,
+                                      const char *buf, int sz,
+                                      const char *record_charset)
+{
+    yaz_iconv_t cd = iconv_create_charset(record_charset);
+    yaz_marc_t mt = yaz_marc_create();
+    const char *ret_string = 0;
+
+    if (cd)
+        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_cstr(rec->wrbuf_marc);
+        ret_string = wrbuf_cstr(rec->wrbuf_marc);
     }
     yaz_marc_destroy(mt);
     if (cd)
         yaz_iconv_close(cd);
-    return 0;
+    return ret_string;
 }
 
-static const char *record_iconv_return(ZOOM_record rec, int *len,
-                                       const char *buf, int sz,
-                                       const char *record_charset)
+static const char *return_opac_record(ZOOM_record rec, int marc_type,
+                                      int *len,
+                                      Z_OPACRecord *opac_rec,
+                                      const char *record_charset)
 {
-    char to[40];
-    char from[40];
-    yaz_iconv_t cd = 0;
+    yaz_iconv_t cd = iconv_create_charset(record_charset);
+    yaz_marc_t mt = yaz_marc_create();
 
-    *from = '\0';
-    strcpy(to, "UTF-8");
+    if (cd)
+        yaz_marc_iconv(mt, cd);
+    yaz_marc_xml(mt, marc_type);
 
-    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 (!rec->wrbuf_marc)
+        rec->wrbuf_marc = wrbuf_alloc();
+    wrbuf_rewind(rec->wrbuf_marc);
+
+    yaz_opac_decode_wrbuf(mt, opac_rec, rec->wrbuf_marc);
+    yaz_marc_destroy(mt);
+
+    if (cd)
+        yaz_iconv_close(cd);
+    if (len)
+        *len = wrbuf_len(rec->wrbuf_marc);
+    return wrbuf_cstr(rec->wrbuf_marc);
+}
 
-    if (*from && *to && (cd = yaz_iconv_open(to, from)))
+static const char *return_string_record(ZOOM_record rec, int *len,
+                                        const char *buf, int sz,
+                                        const char *record_charset)
+{
+    yaz_iconv_t cd = iconv_create_charset(record_charset);
+
+    if (cd)
     {
         if (!rec->wrbuf_iconv)
             rec->wrbuf_iconv = wrbuf_alloc();
@@ -1894,6 +1941,56 @@ static const char *record_iconv_return(ZOOM_record rec, int *len,
     return buf;
 }
 
+static const char *return_record(ZOOM_record rec, int *len,
+                                 Z_NamePlusRecord *npr,
+                                 int marctype, const char *charset)
+{
+    Z_External *r = (Z_External *) npr->u.databaseRecord;
+    const Odr_oid *oid = r->direct_reference;
+    
+    /* render bibliographic record .. */
+    if (r->which == Z_External_OPAC)
+    {
+        return return_opac_record(rec, marctype, len,
+                                  r->u.opac, charset);
+    }
+    if (r->which == Z_External_sutrs)
+        return return_string_record(rec, len,
+                                    (char*) r->u.sutrs->buf,
+                                    r->u.sutrs->len,
+                                    charset);
+    else if (r->which == Z_External_octet)
+    {
+        if (yaz_oid_is_iso2709(oid))
+        {
+            const char *ret_buf = return_marc_record(
+                rec, marctype, len,
+                (const char *) r->u.octet_aligned->buf,
+                r->u.octet_aligned->len,
+                charset);
+            if (ret_buf)
+                return ret_buf;
+        }
+        return return_string_record(rec, len,
+                                    (const char *) r->u.octet_aligned->buf,
+                                    r->u.octet_aligned->len,
+                                    charset);
+    }
+    else if (r->which == Z_External_grs1)
+    {
+        if (!rec->wrbuf_marc)
+            rec->wrbuf_marc = wrbuf_alloc();
+        wrbuf_rewind(rec->wrbuf_marc);
+        yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
+        return return_string_record(rec, len,
+                                    wrbuf_buf(rec->wrbuf_marc),
+                                    wrbuf_len(rec->wrbuf_marc),
+                                    charset);
+    }
+    return 0;
+}
+    
+
 ZOOM_API(int)
     ZOOM_record_error(ZOOM_record rec, const char **cp,
                       const char **addinfo, const char **diagset)
@@ -1902,7 +1999,18 @@ ZOOM_API(int)
     
     if (!rec)
         return 0;
+
     npr = rec->npr;
+    if (rec->diag_uri)
+    {
+        if (cp)
+            *cp = rec->diag_message;
+        if (addinfo)
+            *addinfo = rec->diag_details;
+        if (diagset)
+            *diagset = rec->diag_set;
+        return uri_to_code(rec->diag_uri);
+    }
     if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
     {
         Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
@@ -2005,6 +2113,12 @@ ZOOM_API(const char *)
             *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
         return npr->databaseName;
     }
+    else if (!strcmp(type, "schema"))
+    {
+        if (len)
+            *len = rec->schema ? strlen(rec->schema) : 0;
+        return rec->schema;
+    }
     else if (!strcmp(type, "syntax"))
     {
         const char *desc = 0;   
@@ -2025,116 +2139,15 @@ ZOOM_API(const char *)
     /* from now on - we have a database record .. */
     if (!strcmp(type, "render"))
     {
-        Z_External *r = (Z_External *) npr->u.databaseRecord;
-        const Odr_oid *oid = r->direct_reference;
-
-        /* render bibliographic record .. */
-        if (r->which == Z_External_OPAC)
-        {
-            r = r->u.opac->bibliographicRecord;
-            if (!r)
-                return 0;
-            oid = r->direct_reference;
-        }
-        if (r->which == Z_External_sutrs)
-            return record_iconv_return(rec, len,
-                                       (char*) r->u.sutrs->buf,
-                                       r->u.sutrs->len,
-                                       charset);
-        else if (r->which == Z_External_octet)
-        {
-            if (yaz_oid_is_iso2709(oid))
-            {
-                const char *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,
-                                       r->u.octet_aligned->len,
-                                       charset);
-        }
-        else if (r->which == Z_External_grs1)
-        {
-            if (!rec->wrbuf_marc)
-                rec->wrbuf_marc = wrbuf_alloc();
-            wrbuf_rewind(rec->wrbuf_marc);
-            yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
-            return record_iconv_return(rec, len,
-                                       wrbuf_buf(rec->wrbuf_marc),
-                                       wrbuf_len(rec->wrbuf_marc),
-                                       charset);
-        }
-        return 0;
+        return return_record(rec, len, npr, YAZ_MARC_LINE, charset);
     }
     else if (!strcmp(type, "xml"))
     {
-        Z_External *r = (Z_External *) npr->u.databaseRecord;
-        const Odr_oid *oid = r->direct_reference;
-
-        /* render bibliographic record .. */
-        if (r->which == Z_External_OPAC)
-        {
-            r = r->u.opac->bibliographicRecord;
-            if (!r)
-                return 0;
-            oid = r->direct_reference;
-        }
-        
-        if (r->which == Z_External_sutrs)
-            return record_iconv_return(rec, len,
-                                       (const char *) r->u.sutrs->buf,
-                                       r->u.sutrs->len,
-                                       charset);
-        else if (r->which == Z_External_octet)
-        {
-            int marc_decode_type = YAZ_MARC_MARCXML;
-            if (yaz_oid_is_iso2709(oid))
-            {
-                const char *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,
-                                       r->u.octet_aligned->len,
-                                       charset);
-        }
-        else if (r->which == Z_External_grs1)
-        {
-            if (len) *len = 5;
-            return "GRS-1";
-        }
-        return 0;
+        return return_record(rec, len, npr, YAZ_MARC_MARCXML, charset);
     }
     else if (!strcmp(type, "raw"))
     {
-        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;
-        }
-        return 0;
+        return return_record(rec, len, npr, YAZ_MARC_ISO2709, charset);
     }
     else if (!strcmp (type, "ext"))
     {
@@ -2142,20 +2155,8 @@ ZOOM_API(const char *)
         return (const char *) npr->u.databaseRecord;
     }
     else if (!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 return_record(rec, len, npr, YAZ_MARC_MARCXML, charset);
     }
     return 0;
 }
@@ -2178,53 +2179,58 @@ static size_t record_hash(int pos)
 
 static void record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, 
                              int pos,
-                             const char *syntax, const char *elementSetName)
+                             const char *syntax, const char *elementSetName,
+                             const char *schema,
+                             Z_SRW_diagnostic *diag)
 {
-    ZOOM_record_cache rc;
+    ZOOM_record_cache rc = 0;
     
     ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
     ZOOM_connection_put_event(r->connection, event);
 
     for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
     {
-        if (pos == rc->pos)
+        if (pos == rc->pos 
+            && strcmp_null(r->schema, rc->schema) == 0
+            && strcmp_null(elementSetName,rc->elementSetName) == 0
+            && strcmp_null(syntax, rc->syntax) == 0)
+            break;
+    }
+    if (!rc)
+    {
+        rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
+        rc->rec.odr = 0;
+        rc->rec.wrbuf_marc = 0;
+        rc->rec.wrbuf_iconv = 0;
+        rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
+        
+        rc->syntax = odr_strdup_null(r->odr, syntax);
+        
+        rc->schema = odr_strdup_null(r->odr, r->schema);
+
+        rc->pos = pos;
+        rc->next = r->record_hash[record_hash(pos)];
+        r->record_hash[record_hash(pos)] = rc;
+    }
+    rc->rec.npr = npr;
+    rc->rec.schema = odr_strdup_null(r->odr, schema);
+    rc->rec.diag_set = 0;
+    rc->rec.diag_uri = 0;
+    rc->rec.diag_message = 0;
+    rc->rec.diag_details = 0;
+    if (diag)
+    {
+        if (diag->uri)
         {
-            if (strcmp_null(r->schema, rc->schema))
-                continue;
-            if (strcmp_null(elementSetName,rc->elementSetName))
-                continue;
-            if (strcmp_null(syntax, rc->syntax))
-                continue;
-            /* not destroying rc->npr (it's handled by nmem )*/
-            rc->rec.npr = npr;
-            /* keeping wrbuf_marc too */
-            return;
+            char *cp;
+            rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
+            if ((cp = strrchr(rc->rec.diag_set, '/')))
+                *cp = '\0';
+            rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
         }
+        rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);            
+        rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
     }
-    rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
-    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
-        rc->elementSetName = 0;
-
-    if (syntax)
-        rc->syntax = odr_strdup(r->odr, syntax);
-    else
-        rc->syntax = 0;
-
-    if (r->schema)
-        rc->schema = odr_strdup(r->odr, r->schema);
-    else
-        rc->schema = 0;
-
-    rc->pos = pos;
-    rc->next = r->record_hash[record_hash(pos)];
-    r->record_hash[record_hash(pos)] = rc;
 }
 
 static ZOOM_record record_cache_lookup(ZOOM_resultset r, int pos,
@@ -2301,7 +2307,8 @@ static void handle_records(ZOOM_connection c, Z_Records *sr,
             for (i = 0; i<p->num_records; i++)
             {
                 record_cache_add(resultset, p->records[i], i + *start,
-                                 syntax, elementSetName);
+                                 syntax, elementSetName,
+                                 elementSetName, 0);
             }
             *count -= i;
             if (*count < 0)
@@ -2320,7 +2327,7 @@ static void handle_records(ZOOM_connection c, Z_Records *sr,
                 Z_NamePlusRecord *myrec = 
                     zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
                 record_cache_add(resultset, myrec, *start,
-                                 syntax, elementSetName);
+                                 syntax, elementSetName, 0, 0);
             }
         }
         else if (present_phase)
@@ -2328,7 +2335,8 @@ static void handle_records(ZOOM_connection c, Z_Records *sr,
             /* present response and we didn't get any records! */
             Z_NamePlusRecord *myrec = 
                 zget_surrogateDiagRec(resultset->odr, 0, 14, 0);
-            record_cache_add(resultset, myrec, *start, syntax, elementSetName);
+            record_cache_add(resultset, myrec, *start, syntax, elementSetName,
+                             0, 0);
         }
     }
 }
@@ -2605,8 +2613,12 @@ static zoom_ret send_present(ZOOM_connection c)
         yaz_log(log_details, "%p send_present skip=%d", c, i);
 
     *req->resultSetStartPoint = *start + 1;
-    *req->numberOfRecordsRequested = resultset->step>0 ?
-        resultset->step : *count;
+
+    if (resultset->step > 0 && resultset->step < *count)
+        *req->numberOfRecordsRequested = resultset->step;
+    else
+        *req->numberOfRecordsRequested = *count;
+    
     if (*req->numberOfRecordsRequested + *start > resultset->size)
         *req->numberOfRecordsRequested = resultset->size - *start;
     assert(*req->numberOfRecordsRequested > 0);
@@ -2840,7 +2852,7 @@ static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
     assert (c->tasks->which == ZOOM_TASK_SCAN);
     scan = c->tasks->u.scan.scan;
         
-    sr = yaz_srw_get_pdu(c->odr_out, Z_SRW_scan_request, c->sru_version);
+    sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
 
     /* SRU scan can only carry CQL and PQF */
     if (scan->query->z_query->which == Z_Query_type_104)
@@ -2867,8 +2879,7 @@ static zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
         c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
     
     option_val = ZOOM_options_get(scan->options, "extraArgs");
-    if (option_val)
-        sr->extra_args = odr_strdup(c->odr_out, option_val);
+    yaz_encode_sru_extra(sr, c->odr_out, option_val);
     return send_srw(c, sr);
 }
 #else
@@ -3006,7 +3017,7 @@ static Z_APDU *create_es_package(ZOOM_package p, const Odr_oid *oid)
     
     str = ZOOM_options_get(p->options, "user-id");
     if (str)
-        req->userId = odr_strdup(p->odr_out, str);
+        req->userId = odr_strdup_null(p->odr_out, str);
     
     req->packageType = odr_oiddup(p->odr_out, oid);
 
@@ -3086,6 +3097,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;
+    int len;
     
     req->which = Z_IOItemOrder_esRequest;
     req->u.esRequest = (Z_IORequest *) 
@@ -3099,16 +3111,16 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p)
         odr_malloc(p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
         
     str = ZOOM_options_get(p->options, "contact-name");
-    req->u.esRequest->toKeep->contact->name = str ?
-        odr_strdup(p->odr_out, str) : 0;
+    req->u.esRequest->toKeep->contact->name =
+        odr_strdup_null(p->odr_out, str);
         
     str = ZOOM_options_get(p->options, "contact-phone");
-    req->u.esRequest->toKeep->contact->phone = str ?
-        odr_strdup(p->odr_out, str) : 0;
+    req->u.esRequest->toKeep->contact->phone =
+        odr_strdup_null(p->odr_out, str);
         
     str = ZOOM_options_get(p->options, "contact-email");
-    req->u.esRequest->toKeep->contact->email = str ?
-        odr_strdup(p->odr_out, str) : 0;
+    req->u.esRequest->toKeep->contact->email =
+        odr_strdup_null(p->odr_out, str);
         
     req->u.esRequest->toKeep->addlBilling = 0;
         
@@ -3137,11 +3149,11 @@ static Z_ItemOrder *encode_item_order(ZOOM_package p)
             (str ? atoi(str) : 1);
     }
 
-    str = ZOOM_options_get(p->options, "doc");
+    str = ZOOM_options_getl(p->options, "doc", &len);
     if (str)
     {
         req->u.esRequest->notToKeep->itemRequest =
-            z_ext_record_xml(p->odr_out, str, strlen(str));
+            z_ext_record_xml(p->odr_out, str, len);
     }
     else
         req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
@@ -3204,10 +3216,14 @@ static Z_APDU *create_xmlupdate_package(ZOOM_package p)
     Z_APDU *apdu = create_es_package(p, yaz_oid_extserv_xml_es);
     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");
+    int len;
+    const char *doc = ZOOM_options_getl(p->options, "doc", &len);
 
     if (!doc)
+    {
         doc = "";
+        len = 0;
+    }
 
     req->taskSpecificParameters = ext;
     ext->direct_reference = req->packageType;
@@ -3216,8 +3232,7 @@ static Z_APDU *create_xmlupdate_package(ZOOM_package p)
     
     ext->which = Z_External_octet;
     ext->u.single_ASN1_type =
-        odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc,
-                           strlen(doc));
+        odr_create_Odr_oct(p->odr_out, (const unsigned char *) doc, len);
     return apdu;
 }
 
@@ -3228,9 +3243,16 @@ static Z_APDU *create_update_package(ZOOM_package p)
     int num_db;
     char **db = set_DatabaseNames(p->connection, p->options, &num_db, p->odr_out);
     const char *action = ZOOM_options_get(p->options, "action");
-    const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque");
+    int recordIdOpaque_len;
+    const char *recordIdOpaque = ZOOM_options_getl(p->options, "recordIdOpaque",
+        &recordIdOpaque_len);
     const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
-    const char *record_buf = ZOOM_options_get(p->options, "record");
+    int record_len;
+    const char *record_buf = ZOOM_options_getl(p->options, "record",
+        &record_len);
+    int recordOpaque_len;
+    const char *recordOpaque_buf = ZOOM_options_getl(p->options, "recordOpaque",
+        &recordOpaque_len);
     const char *syntax_str = ZOOM_options_get(p->options, "syntax");
     const char *version = ZOOM_options_get(p->options, "updateVersion");
 
@@ -3246,9 +3268,10 @@ static Z_APDU *create_update_package(ZOOM_package p)
         version = "3";
     if (!syntax_str)
         syntax_str = "xml";
-    if (!record_buf)
+    if (!record_buf && !recordOpaque_buf)
     {
         record_buf = "void";
+        record_len = 4;
         syntax_str = "SUTRS";
     }
 
@@ -3328,9 +3351,7 @@ static Z_APDU *create_update_package(ZOOM_package p)
         toKeep->databaseName = odr_strdup(p->odr_out, first_db);
         toKeep->schema = 0;
         
-        toKeep->elementSetName = 0;
-        if (elementSetName)
-            toKeep->elementSetName = odr_strdup(p->odr_out, elementSetName);
+        toKeep->elementSetName = odr_strdup_null(p->odr_out, elementSetName);
             
         toKeep->actionQualifier = 0;
         toKeep->action = odr_intdup(p->odr_out, action_no);
@@ -3349,7 +3370,7 @@ static Z_APDU *create_update_package(ZOOM_package p)
             notToKeep->elements[0]->u.opaque = 
                 odr_create_Odr_oct(p->odr_out,
                                    (const unsigned char *) recordIdOpaque,
-                                   strlen(recordIdOpaque));
+                                   recordIdOpaque_len);
         }
         else if (recordIdNumber)
         {
@@ -3365,17 +3386,25 @@ static Z_APDU *create_update_package(ZOOM_package p)
         {
             Z_IUCorrelationInfo *ci;
             ci = notToKeep->elements[0]->correlationInfo =
-                odr_malloc(p->odr_out, sizeof(*ci));
-            ci->note = correlationInfo_note ?
-                odr_strdup(p->odr_out, correlationInfo_note) : 0;
+                (Z_IUCorrelationInfo *) odr_malloc(p->odr_out, sizeof(*ci));
+            ci->note = odr_strdup_null(p->odr_out, correlationInfo_note);
             ci->id = correlationInfo_id ?
                 odr_intdup(p->odr_out, atoi(correlationInfo_id)) : 0;
         }
         else
             notToKeep->elements[0]->correlationInfo = 0;
-        notToKeep->elements[0]->record =
-            z_ext_record_oid(p->odr_out, syntax_oid,
-                             record_buf, strlen(record_buf));
+        if (recordOpaque_buf)
+        {
+            notToKeep->elements[0]->record =
+                z_ext_record_oid_any(p->odr_out, syntax_oid,
+                                 recordOpaque_buf, recordOpaque_len);
+        }
+        else
+        {
+            notToKeep->elements[0]->record =
+                z_ext_record_oid(p->odr_out, syntax_oid,
+                                 record_buf, record_len);
+        }
     }
     if (0 && apdu)
     {
@@ -3496,6 +3525,11 @@ ZOOM_API(const char *)
     return ZOOM_options_get(p->options, key);
 }
 
+ZOOM_API(const char *)
+    ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
+{
+    return ZOOM_options_getl(p->options, key, lenp);
+}
 
 ZOOM_API(void)
     ZOOM_package_option_set(ZOOM_package p, const char *key,
@@ -3504,6 +3538,13 @@ ZOOM_API(void)
     ZOOM_options_set(p->options, key, val);
 }
 
+ZOOM_API(void)
+    ZOOM_package_option_setl(ZOOM_package p, const char *key,
+                             const char *val, int len)
+{
+    ZOOM_options_setl(p->options, key, val, len);
+}
+
 static int ZOOM_connection_exec_task(ZOOM_connection c)
 {
     ZOOM_task task = c->tasks;
@@ -3592,22 +3633,90 @@ static zoom_ret send_sort_present(ZOOM_connection c)
     return r;
 }
 
+static int es_response_taskpackage_update(ZOOM_connection c,
+               Z_IUUpdateTaskPackage *utp)
+{
+       if (utp && utp->targetPart)
+       {
+               Z_IUTargetPart *targetPart = utp->targetPart;
+               switch ( *targetPart->updateStatus ) {
+                       case Z_IUTargetPart_success:
+                               ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "success");
+                               break;
+                       case Z_IUTargetPart_partial:
+                               ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "partial");
+                               break;
+                       case Z_IUTargetPart_failure:
+                               ZOOM_options_set(c->tasks->u.package->options,"updateStatus", "failure");
+                               if (targetPart->globalDiagnostics && targetPart->num_globalDiagnostics > 0)
+                                       response_diag(c, targetPart->globalDiagnostics[0]);
+                               break;
+               }
+               // NOTE: Individual record status, surrogate diagnostics, and supplemental diagnostics ARE NOT REPORTED.
+       }
+    return 1;
+}
+
+static int es_response_taskpackage(ZOOM_connection c,
+                                   Z_TaskPackage *taskPackage)
+{
+       // targetReference
+       Odr_oct *id = taskPackage->targetReference;
+       if (id)
+               ZOOM_options_setl(c->tasks->u.package->options,
+                                                       "targetReference", (char*) id->buf, id->len);
+       
+       // taskStatus
+       switch ( *taskPackage->taskStatus ) {
+               case Z_TaskPackage_pending:
+                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "pending");
+                       break;
+               case Z_TaskPackage_active:
+                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "active");
+                       break;
+               case Z_TaskPackage_complete:
+                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "complete");
+                       break;
+               case Z_TaskPackage_aborted:
+                       ZOOM_options_set(c->tasks->u.package->options,"taskStatus", "aborted");
+                       if ( taskPackage->num_packageDiagnostics && taskPackage->packageDiagnostics )
+                               response_diag(c, taskPackage->packageDiagnostics[0]);
+                       break;
+       }
+       
+       // taskSpecificParameters
+       // NOTE: Only Update implemented, no others.
+       if ( taskPackage->taskSpecificParameters->which == Z_External_update ) {
+                       Z_IUUpdateTaskPackage *utp = taskPackage->taskSpecificParameters->u.update->u.taskPackage;
+                       es_response_taskpackage_update(c, utp);
+       }
+       return 1;
+}
+
+
 static int es_response(ZOOM_connection c,
                        Z_ExtendedServicesResponse *res)
 {
     if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
         return 0;
-    if (res->diagnostics && res->num_diagnostics > 0)
-        response_diag(c, res->diagnostics[0]);
+    switch (*res->operationStatus) {
+        case Z_ExtendedServicesResponse_done:
+            ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "done");
+            break;
+        case Z_ExtendedServicesResponse_accepted:
+            ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "accepted");
+            break;
+        case Z_ExtendedServicesResponse_failure:
+            ZOOM_options_set(c->tasks->u.package->options,"operationStatus", "failure");
+            if (res->diagnostics && res->num_diagnostics > 0)
+                response_diag(c, res->diagnostics[0]);
+            break;
+    }
     if (res->taskPackage &&
         res->taskPackage->which == Z_External_extendedService)
     {
         Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
-        Odr_oct *id = taskPackage->targetReference;
-        
-        if (id)
-            ZOOM_options_setl(c->tasks->u.package->options,
-                              "targetReference", (char*) id->buf, id->len);
+        es_response_taskpackage(c, taskPackage);
     }
     if (res->taskPackage && 
         res->taskPackage->which == Z_External_octet)
@@ -3794,38 +3903,45 @@ static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
 }
 
 #if YAZ_HAVE_XML2
-static void handle_srw_response(ZOOM_connection c,
-                                Z_SRW_searchRetrieveResponse *res)
+static zoom_ret handle_srw_response(ZOOM_connection c,
+                                    Z_SRW_searchRetrieveResponse *res)
 {
     ZOOM_resultset resultset = 0;
     int i;
     NMEM nmem;
     ZOOM_Event event;
-    int *start;
+    int *start, *count;
     const char *syntax, *elementSetName;
 
     if (!c->tasks)
-        return;
+        return zoom_complete;
 
     switch(c->tasks->which)
     {
     case ZOOM_TASK_SEARCH:
         resultset = c->tasks->u.search.resultset;
         start = &c->tasks->u.search.start;
+        count = &c->tasks->u.search.count;
         syntax = c->tasks->u.search.syntax;
         elementSetName = c->tasks->u.search.elementSetName;        
+
+        if (!c->tasks->u.search.recv_search_fired)
+        {
+            event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
+            ZOOM_connection_put_event(c, event);
+            c->tasks->u.search.recv_search_fired = 1;
+        }
         break;
     case ZOOM_TASK_RETRIEVE:
         resultset = c->tasks->u.retrieve.resultset;
         start = &c->tasks->u.retrieve.start;
+        count = &c->tasks->u.retrieve.count;
         syntax = c->tasks->u.retrieve.syntax;
         elementSetName = c->tasks->u.retrieve.elementSetName;
         break;
     default:
-        return;
+        return zoom_complete;
     }
-    event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
-    ZOOM_connection_put_event(c, event);
 
     resultset->size = 0;
 
@@ -3833,46 +3949,77 @@ static void handle_srw_response(ZOOM_connection c,
         ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
 
     yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
-    
-    if (res->numberOfRecords)
-        resultset->size = *res->numberOfRecords;
 
-    for (i = 0; i<res->num_records; i++)
+    if (res->num_diagnostics > 0)
     {
-        int pos;
-
-        Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
-            odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
-
-        if (res->records[i].recordPosition && 
-            *res->records[i].recordPosition > 0)
-            pos = *res->records[i].recordPosition - 1;
-        else
-            pos = *start + i;
+        set_SRU_error(c, &res->diagnostics[0]);
+    }
+    else
+    {
+        if (res->numberOfRecords)
+            resultset->size = *res->numberOfRecords;
+        for (i = 0; i<res->num_records; i++)
+        {
+            int pos;
+            Z_SRW_record *sru_rec;
+            Z_SRW_diagnostic *diag = 0;
+            int num_diag;
+            
+            Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
+                odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
+            
+            if (res->records[i].recordPosition && 
+                *res->records[i].recordPosition > 0)
+                pos = *res->records[i].recordPosition - 1;
+            else
+                pos = *start + i;
+            
+            sru_rec = &res->records[i];
+            
+            npr->databaseName = 0;
+            npr->which = Z_NamePlusRecord_databaseRecord;
+            npr->u.databaseRecord = (Z_External *)
+                odr_malloc(c->odr_in, sizeof(Z_External));
+            npr->u.databaseRecord->descriptor = 0;
+            npr->u.databaseRecord->direct_reference =
+                odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
+            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 = (unsigned char*)
+                sru_rec->recordData_buf;
+            npr->u.databaseRecord->u.octet_aligned->len = 
+                npr->u.databaseRecord->u.octet_aligned->size = 
+                sru_rec->recordData_len;
+            
+            if (sru_rec->recordSchema 
+                && !strcmp(sru_rec->recordSchema,
+                           "info:srw/schema/1/diagnostics-v1.1"))
+            {
+                sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
+                                                 sru_rec->recordData_len,
+                                                 &diag, &num_diag,
+                                                 resultset->odr);
+            }
+            record_cache_add(resultset, npr, pos, syntax, elementSetName,
+                             sru_rec->recordSchema, diag);
+        }
+        *count -= i;
+        *start += i;
+        if (*count + *start > resultset->size)
+            *count = resultset->size - *start;
+        if (*count < 0)
+            *count = 0;
         
-        npr->databaseName = 0;
-        npr->which = Z_NamePlusRecord_databaseRecord;
-        npr->u.databaseRecord = (Z_External *)
-            odr_malloc(c->odr_in, sizeof(Z_External));
-        npr->u.databaseRecord->descriptor = 0;
-        npr->u.databaseRecord->direct_reference =
-            odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
-        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 = (unsigned char*)
-            res->records[i].recordData_buf;
-        npr->u.databaseRecord->u.octet_aligned->len = 
-            npr->u.databaseRecord->u.octet_aligned->size = 
-            res->records[i].recordData_len;
-        record_cache_add(resultset, npr, pos, syntax, elementSetName);
+        nmem = odr_extract_mem(c->odr_in);
+        nmem_transfer(odr_getmem(resultset->odr), nmem);
+        nmem_destroy(nmem);
+
+        if (*count > 0)
+            return ZOOM_connection_srw_send_search(c);
     }
-    if (res->num_diagnostics > 0)
-        set_SRU_error(c, &res->diagnostics[0]);
-    nmem = odr_extract_mem(c->odr_in);
-    nmem_transfer(odr_getmem(resultset->odr), nmem);
-    nmem_destroy(nmem);
+    return zoom_complete;
 }
 #endif
 
@@ -3902,6 +4049,7 @@ static void handle_srw_scan_response(ZOOM_connection c,
 #if YAZ_HAVE_XML2
 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
 {
+    zoom_ret cret = zoom_complete;
     int ret = -1;
     const char *addinfo = 0;
     const char *connection_head = z_HTTP_header_lookup(hres->headers,
@@ -3926,8 +4074,10 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
             soap_package->u.generic->no == 0)
         {
             Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
+
+            ZOOM_options_set(c->options, "sru_version", sr->srw_version);
             if (sr->which == Z_SRW_searchRetrieve_response)
-                handle_srw_response(c, sr->u.response);
+                cret = handle_srw_response(c, sr->u.response);
             else if (sr->which == Z_SRW_scan_response)
                 handle_srw_scan_response(c, sr->u.scan_response);
             else
@@ -3951,7 +4101,8 @@ static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
             set_ZOOM_error(c, ZOOM_ERROR_DECODE, addinfo);
         do_close(c);
     }
-    ZOOM_connection_remove_task(c);
+    if (cret == zoom_complete)
+        ZOOM_connection_remove_task(c);
     if (!strcmp(hres->version, "1.0"))
     {
         /* HTTP 1.0: only if Keep-Alive we stay alive.. */
@@ -4017,16 +4168,21 @@ static int do_read(ZOOM_connection c)
             }
             do_close(c);
         }
-        else if (gdu->which == Z_GDU_Z3950)
-            recv_apdu(c, gdu->u.z3950);
-        else if (gdu->which == Z_GDU_HTTP_Response)
+        else
         {
+            if (c->odr_print)
+                z_GDU(c->odr_print, &gdu, 0, 0);
+            if (gdu->which == Z_GDU_Z3950)
+                recv_apdu(c, gdu->u.z3950);
+            else if (gdu->which == Z_GDU_HTTP_Response)
+            {
 #if YAZ_HAVE_XML2
-            handle_http(c, gdu->u.HTTP_Response);
+                handle_http(c, gdu->u.HTTP_Response);
 #else
-            set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
-            do_close(c);
+                set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
+                do_close(c);
 #endif
+            }
         }
         c->reconnect_ok = 0;
     }
@@ -4459,6 +4615,7 @@ ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab