SRW record retrieval works for ZOOM
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 17 Feb 2003 14:35:42 +0000 (14:35 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 17 Feb 2003 14:35:42 +0000 (14:35 +0000)
include/yaz/srw.h
include/yaz/zoom.h
server/seshigh.c
zoom/zoomsh.c
zutil/soap.c
zutil/srw.c
zutil/zgdu.c
zutil/zoom-c.c
zutil/zoom-p.h

index f2cc9ae..4fad9fd 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2002-2003, Index Data.
  * See the file LICENSE for details.
  *
- * $Id: srw.h,v 1.2 2003-02-14 18:49:23 adam Exp $
+ * $Id: srw.h,v 1.3 2003-02-17 14:35:42 adam Exp $
  */
 
 #ifndef YAZ_SRW_H
@@ -32,6 +32,7 @@ typedef struct {
     int  *maximumRecords;
     char *recordSchema;
     char *recordPacking;
+    char *database;
 } Z_SRW_searchRetrieveRequest;
 
 typedef struct {
@@ -58,14 +59,12 @@ typedef struct {
     } u;
 } Z_SRW_searchRetrieve;
 
-#if 1
 YAZ_EXPORT int yaz_srw_codec(ODR o, void * pptr,
                              Z_SRW_searchRetrieve **handler_data,
                              void *client_data, const char *ns);
-#else
-YAZ_EXPORT int yaz_srw_codec(ODR o, xmlNodePtr pptr,
-                             Z_SRW_searchRetrieve **handler_data,
-                             void *client_data, const char *ns);
-#endif
 YAZ_EXPORT Z_SRW_searchRetrieve *yaz_srw_get(ODR o, int which);
+
+YAZ_EXPORT const char *yaz_srw_error_str (int code);
+
+
 #endif
index 956bd51..c5bca0e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Public header for ZOOM C.
- * $Id: zoom.h,v 1.16 2003-01-06 08:20:27 adam Exp $
+ * $Id: zoom.h,v 1.17 2003-02-17 14:35:42 adam Exp $
  */
 
 #include <yaz/yconfig.h>
@@ -93,6 +93,7 @@ ZOOM_diag_str (int error);
 #define ZOOM_ERROR_INTERNAL 10006
 #define ZOOM_ERROR_TIMEOUT 10007
 #define ZOOM_ERROR_UNSUPPORTED_PROTOCOL 10008
+#define ZOOM_ERROR_UNSUPPORTED_QUERY 10009
 
 ZOOM_API(int)
 ZOOM_connection_last_event(ZOOM_connection cs);
index a6a9a13..b19cc62 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: seshigh.c,v 1.135 2003-02-14 18:49:24 adam Exp $
+ * $Id: seshigh.c,v 1.136 2003-02-17 14:35:42 adam Exp $
  */
 
 /*
@@ -502,7 +502,9 @@ static void srw_bend_fetch(association *assoc, int pos,
         record->recordData_buf = rr.record;
         record->recordData_len = rr.len;
         record->recordPosition = odr_intdup(o, pos);
-        record->recordSchema = odr_strdup(o, srw_req->recordSchema);
+        record->recordSchema = 0;
+        if (srw_req->recordSchema)
+            record->recordSchema = odr_strdup(o, srw_req->recordSchema);
     }
 }
 
@@ -520,7 +522,7 @@ static void srw_bend_search(association *assoc, request *req,
     rr.setname = "default";
     rr.replace_set = 1;
     rr.num_bases = 1;
-    rr.basenames = &base;
+    rr.basenames = &srw_req->database;
     rr.referenceId = 0;
 
     ext = (Z_External *) odr_malloc(assoc->decode, sizeof(*ext));
@@ -548,6 +550,7 @@ static void srw_bend_search(association *assoc, request *req,
     rr.errcode = 0;
     rr.errstring = 0;
     rr.search_info = 0;
+    yaz_log_zquery(rr.query);
     (assoc->init->bend_search)(assoc->backend, &rr);
     srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
     if (rr.errcode)
@@ -557,6 +560,7 @@ static void srw_bend_search(association *assoc, request *req,
            odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
         srw_res->diagnostics[0].code = 
             odr_intdup(assoc->encode, rr.errcode);
+        srw_res->diagnostics[0].details = rr.errstring;
     }
     else
     {
@@ -721,8 +725,27 @@ static void process_http_request(association *assoc, request *req)
                     Z_SRW_searchRetrieve *res =
                         yaz_srw_get(assoc->encode,
                                     Z_SRW_searchRetrieve_response);
-                    
-                    srw_bend_search(assoc, req, sr->u.request, res->u.response);
+
+                    if (!sr->u.request->database)
+                    {
+                        const char *p0 = hreq->path, *p1;
+                        if (*p0 == '/')
+                            p0++;
+                        p1 = strchr(p0, '?');
+                        if (!p1)
+                            p1 = p0 + strlen(p0);
+                        if (p1 != p0)
+                        {
+                            sr->u.request->database =
+                                odr_malloc(assoc->decode, p1 - p0 + 1);
+                            memcpy (sr->u.request->database, p0, p1 - p0);
+                            sr->u.request->database[p1 - p0] = '\0';
+                        }
+                        else
+                            sr->u.request->database = "Default";
+                    }
+                    srw_bend_search(assoc, req, sr->u.request,
+                                    res->u.response);
                     
                     soap_package->u.generic->p = res;
                     http_code = 200;
@@ -735,6 +758,7 @@ static void process_http_request(association *assoc, request *req)
                                &hres->content_buf, &hres->content_len,
                                soap_handlers);
             hres->code = http_code;
+            z_HTTP_header_add(o, &hres->headers, "Content-Type", "text/xml");
         }
 #endif
         if (!p) /* still no response ? */
index cd0ab0e..8097066 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: zoomsh.c,v 1.13 2003-02-14 18:49:24 adam Exp $
+ * $Id: zoomsh.c,v 1.14 2003-02-17 14:35:42 adam Exp $
  *
  * ZOOM-C Shell
  */
@@ -173,7 +173,7 @@ static void cmd_show (ZOOM_connection *c, ZOOM_resultset *r,
        if (!c[i])
            continue;
        if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
-           fprintf (stderr, "%s error: %s (%s:%d) %s\n",
+           printf ("%s error: %s (%s:%d) %s\n",
                     ZOOM_connection_option_get(c[i], "host"), errmsg,
                     dset, error, addinfo);
        else if (r[i])
@@ -216,7 +216,7 @@ static void cmd_ext (ZOOM_connection *c, ZOOM_resultset *r,
        if (!p[i])
            continue;
        if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
-           fprintf (stderr, "%s error: %s (%s:%d) %s\n",
+           printf ("%s error: %s (%s:%d) %s\n",
                     ZOOM_connection_option_get(c[i], "host"), errmsg,
                     dset, error, addinfo);
        else if (p[i])
@@ -244,7 +244,7 @@ static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
     }
     else if (ZOOM_query_prefix (s, query_str))
     {
-       fprintf (stderr, "Bad PQF: %s\n", query_str);
+       printf ("Bad PQF: %s\n", query_str);
        return;
     }
     for (i = 0; i<MAX_CON; i++)
@@ -269,9 +269,9 @@ static void cmd_search (ZOOM_connection *c, ZOOM_resultset *r,
        if (!c[i])
            continue;
        if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
-           fprintf (stderr, "%s error: %s (%s:%d) %s\n",
-                    ZOOM_connection_option_get(c[i], "host"), errmsg,
-                    dset, error, addinfo);
+           printf ("%s error: %s (%s:%d) %s\n",
+                   ZOOM_connection_option_get(c[i], "host"), errmsg,
+                   dset, error, addinfo);
        else if (r[i])
        {
            /* OK, no major errors. Look at the result count */
@@ -323,7 +323,7 @@ static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
                         const char **args)
 {
     int error;
-    const char *errmsg, *addinfo;
+    const char *errmsg, *addinfo, *dset;
     char host[60];
     int j, i;
     if (!next_token_copy (args, host, sizeof(host)))
@@ -354,12 +354,11 @@ static void cmd_connect (ZOOM_connection *c, ZOOM_resultset *r,
     }
     c[i] = ZOOM_connection_create (options);
     ZOOM_connection_connect (c[i], host, 0);
-
-    if ((error = ZOOM_connection_error(c[i], &errmsg, &addinfo)))
-       printf ("%s error: %s (%d) %s\n",
-                ZOOM_connection_option_get(c[i], "host"),
-               errmsg, error, addinfo);
-    
+       
+    if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
+       printf ("%s error: %s (%s:%d) %s\n",
+           ZOOM_connection_option_get(c[i], "host"), errmsg,
+           dset, error, addinfo);
 }
 
 static int cmd_parse (ZOOM_connection *c, ZOOM_resultset *r,
@@ -417,7 +416,7 @@ void shell(ZOOM_connection *c, ZOOM_resultset *r,
            add_history(line_in);
 #endif
        if(strlen(line_in) > 999) {
-           fprintf(stderr,"Input line too long\n");
+           printf("Input line too long\n");
            break;
        };
        strcpy(buf,line_in);
@@ -441,6 +440,9 @@ int main (int argc, char **argv)
     ZOOM_connection z39_con[MAX_CON];
     ZOOM_resultset  z39_res[MAX_CON];
 
+#if 0
+    yaz_log_init_level(65535);
+#endif
     for (i = 0; i<MAX_CON; i++)
     {
        z39_con[i] = 0;
index a19fc57..157533c 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2002-2003, Index Data.
  * See the file LICENSE for details.
  *
- * $Id: soap.c,v 1.2 2003-02-14 18:49:24 adam Exp $
+ * $Id: soap.c,v 1.3 2003-02-17 14:35:42 adam Exp $
  */
 
 #include <yaz/soap.h>
@@ -54,7 +54,7 @@ int z_soap_codec(ODR o, Z_SOAP **pp,
         /* check that root node is Envelope */
         ptr = xmlDocGetRootElement(doc);
         if (!ptr || ptr->type != XML_ELEMENT_NODE ||
-            strcmp(ptr->name, "Envelope"))
+            strcmp(ptr->name, "Envelope") || !ptr->ns)
         {
             xmlFreeDoc(doc);
             return z_soap_error(o, p, "SOAP-ENV:Client",
@@ -183,6 +183,8 @@ int z_soap_codec(ODR o, Z_SOAP **pp,
 
         envelope_ptr = xmlNewNode(0, "Envelope");
         ns_env = xmlNewNs(envelope_ptr, p->ns, "SOAP-ENV");
+        xmlSetNs(envelope_ptr, ns_env);
+
         body_ptr = xmlNewChild(envelope_ptr, ns_env, "Body", 0);
         xmlDocSetRootElement(doc, envelope_ptr);
 
index 9da8df6..3976e8c 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2002-2003, Index Data.
  * See the file LICENSE for details.
  *
- * $Id: srw.c,v 1.2 2003-02-14 18:49:24 adam Exp $
+ * $Id: srw.c,v 1.3 2003-02-17 14:35:42 adam Exp $
  */
 
 #include <yaz/srw.h>
@@ -51,6 +51,7 @@ static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
     struct _xmlAttr *attr;
     if (!match_element(ptr, elem))
         return 0;
+#if 0
     for (attr = ptr->properties; attr; attr = attr->next)
         if (!strcmp(attr->name, "type") &&
             attr->children && attr->children->type == XML_TEXT_NODE)
@@ -65,6 +66,7 @@ static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
         }
     if (!attr)
         return 0;
+#endif
     ptr = ptr->children;
     if (!ptr || ptr->type != XML_TEXT_NODE)
         return 0;
@@ -86,6 +88,7 @@ static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
     struct _xmlAttr *attr;
     if (!match_element(ptr, elem))
         return 0;
+#if 0
     for (attr = ptr->properties; attr; attr = attr->next)
         if (!strcmp(attr->name, "type") &&
             attr->children && attr->children->type == XML_TEXT_NODE)
@@ -100,6 +103,7 @@ static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, int **val)
         }
     if (!attr)
         return 0;
+#endif
     ptr = ptr->children;
     if (!ptr || ptr->type != XML_TEXT_NODE)
         return 0;
@@ -247,6 +251,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_searchRetrieve **handler_data,
             req->maximumRecords = 0;
             req->recordSchema = 0;
             req->recordPacking = 0;
+            req->database = 0;
 
             for (; ptr; ptr = ptr->next)
             {
@@ -271,6 +276,9 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_searchRetrieve **handler_data,
                 else if (match_xsd_integer(ptr, "maximumRecords", o,
                                            &req->maximumRecords))
                     ;
+                else if (match_xsd_string(ptr, "database", o,
+                                           &req->database))
+                    ;
                 /* missing is xQuery, xSortKeys .. */
             }
         }
@@ -339,6 +347,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_searchRetrieve **handler_data,
             add_xsd_integer(ptr, "maximumRecords", req->maximumRecords);
             add_xsd_string(ptr, "recordSchema", req->recordSchema);
             add_xsd_string(ptr, "recordPacking", req->recordPacking);
+            add_xsd_string(ptr, "database", req->database);
         }
         else if ((*p)->which == Z_SRW_searchRetrieve_response)
         {
@@ -388,6 +397,7 @@ Z_SRW_searchRetrieve *yaz_srw_get(ODR o, int which)
         sr->u.request->maximumRecords = 0;
         sr->u.request->recordSchema = 0;
         sr->u.request->recordPacking = 0;
+        sr->u.request->database = 0;
         break;
     case Z_SRW_searchRetrieve_response:
         sr->u.response = odr_malloc(o, sizeof(*sr->u.response));
@@ -402,4 +412,102 @@ Z_SRW_searchRetrieve *yaz_srw_get(ODR o, int which)
     }
     return sr;
 }
+
+#endif
+
+
+const char *yaz_srw_error_str (int code)
+{
+    return "SRW error";
+}
+
+#if 0
+
+1 Permanent system error
+2 System temporarily unavailable
+3 Authentication error
+    /* Diagnostics Relating to CQL */
+Number Description
+10 Illegal query
+11 Unsupported query type (XCQL vs CQL)
+12 Too many characters in query
+13 Unbalanced or illegal use of parentheses
+14 Unbalanced or illegal use of quotes
+15 Illegal or unsupported index set
+16 Illegal or unsupported index
+17 Illegal or unsupported combination of index and index set
+18 Illegal or unsupported combination of indexes
+19 Illegal or unsupported relation
+20 Illegal or unsupported relation modifier
+21 Illegal or unsupported combination of relation modifers
+22 Illegal or unsupported combination of relation and index
+23 Too many characters in term
+24 Illegal combination of relation and term
+25 Special characters not quoted in term
+26 Non special character escaped in term
+27 Empty term unsupported
+28 Masking character not supported
+29 Masked words too short
+30 Too many masking characters in term
+31 Anchoring character not supported
+32 Anchoring character in illegal or unsupported position
+33 Combination of proximity/adjacency and masking characters not supported
+34 Combination of proximity/adjacency and anchoring characters not supported
+35 Terms only exclusion (stop) words
+36 Term in invalid format for index or relation
+37 Illegal or unsupported boolean operator
+38 Too many boolean operators in query
+39 Proximity not supported
+40 Illegal or unsupported proximity relation
+41 Illegal or unsupported proximity distance
+42 Illegal or unsupported proximity unit
+43 Illegal or unsupported proximity ordering
+44 Illegal or unsupported combination of proximity modifiers
+45 Index set name (prefix) assigned to multiple identifiers
+/* Diagnostics Relating to Result Sets */
+Number Description
+50 Result sets not supported
+51 Result set does not exist
+52 Result set temporarily unavailable
+53 Result sets only supported for retrieval
+54 Retrieval may only occur from an existing result set
+55 Combination of result sets with search terms not supported
+56 Only combination of single result set with search terms supported
+57 Result set created but no records available
+58 Result set created with unpredictable partial results available
+59 Result set created with valid partial results available
+/* Diagnostics Relating to Records */
+Number Description
+60 Too many records retrieved
+61 First record position out of range
+62 Negative number of records requested
+63 System error in retrieving records
+64 Record temporarily unavailable
+65 Record does not exist
+66 Unknown schema for retrieval
+67 Record not available in this schema
+68 Not authorised to send record
+69 Not authorised to send record in this schema
+70 Record too large to send
+/* Diagnostics Relating to Sorting */
+Number Description
+80 Sort not supported
+81 Unsupported sort type (sortKeys vs xSortKeys)
+82 Illegal or unsupported sort sequence
+83 Too many records
+84 Too many sort keys
+85 Duplicate sort keys
+86 Incompatible record formats
+87 Unsupported schema for sort
+88 Unsupported tag path for sort
+89 Tag path illegal or unsupported for schema
+90 Illegal or unsupported direction value
+91 Illegal or unsupported case value
+92 Illegal or unsupported missing value action
+/* Diagnostics Relating to Explain */
+Number Description
+100 Explain not supported
+101 Explain request type not supported (SOAP vs GET)
+102 Explain record temporarily unavailable
+
 #endif
index 5524118..6213b12 100644 (file)
@@ -2,12 +2,12 @@
  * Copyright (c) 2002-2003, Index Data.
  * See the file LICENSE for details.
  *
- * $Id: zgdu.c,v 1.2 2003-02-14 18:49:24 adam Exp $
+ * $Id: zgdu.c,v 1.3 2003-02-17 14:35:42 adam Exp $
  */
 
 #include <yaz/proto.h>
 
-#define HTTP_DEBUG 0
+#define HTTP_DEBUG 1
 
 static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers,
                                   char **content_buf, int *content_len)
index d8fab11..9a9e9da 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2000-2003, Index Data
  * See the file LICENSE for details.
  *
- * $Id: zoom-c.c,v 1.19 2003-02-14 19:10:00 adam Exp $
+ * $Id: zoom-c.c,v 1.20 2003-02-17 14:35:42 adam Exp $
  *
  * ZOOM layer for C, connections, result sets, queries.
  */
@@ -10,6 +10,7 @@
 #include <string.h>
 #include "zoom-p.h"
 
+#include <yaz/yaz-util.h>
 #include <yaz/xmalloc.h>
 #include <yaz/otherinfo.h>
 #include <yaz/log.h>
@@ -81,23 +82,40 @@ static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
     return event;
 }
 
-static void set_bib1_error (ZOOM_connection c, int error)
+
+static void set_dset_error (ZOOM_connection c, int error,
+                            const char *dset,
+                            const char *addinfo, const char *addinfo2)
 {
     xfree (c->addinfo);
     c->addinfo = 0;
     c->error = error;
-    c->diagset = "Bib-1";
+    c->diagset = dset;
+    if (addinfo && addinfo2)
+    {
+        c->addinfo = 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_LOG, "Error %s %s:%d %s %s",
+                c->host_port ? c->host_port : "<>", dset, error,
+                addinfo ? addinfo : "",
+                addinfo2 ? addinfo2 : "");
+}
+
+static void set_HTTP_error (ZOOM_connection c, int error,
+                            const char *addinfo, const char *addinfo2)
+{
+    return set_dset_error(c, error, "HTTP", addinfo, addinfo2);
 }
 
 static void set_ZOOM_error (ZOOM_connection c, int error,
                            const char *addinfo)
 {
-    xfree (c->addinfo);
-    c->addinfo = 0;
-    c->error = error;
-    c->diagset = "ZOOM";
-    if (addinfo)
-        c->addinfo = xstrdup(addinfo);
+    return set_dset_error(c, error, "ZOOM", addinfo, 0);
 }
 
 static void clear_error (ZOOM_connection c)
@@ -477,6 +495,7 @@ ZOOM_resultset ZOOM_resultset_create ()
     r->start = 0;
     r->piggyback = 1;
     r->setname = 0;
+    r->schema = 0;
     r->count = 0;
     r->record_cache = 0;
     r->r_sort_spec = 0;
@@ -518,13 +537,30 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
     r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
     cp = ZOOM_options_get (r->options, "setname");
     if (cp)
-        r->setname = xstrdup (cp);
+        r->setname = xstrdup(cp);
+    cp = ZOOM_options_get (r->options, "schema");
+    if (cp)
+        r->schema = xstrdup(cp);
     
     r->connection = c;
 
     r->next = c->resultsets;
     c->resultsets = r;
 
+    if (c->host_port && c->proto == PROTO_SRW)
+    {
+        if (!c->cs)
+        {
+            yaz_log(LOG_DEBUG, "NO COMSTACK");
+            ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
+        }
+        else
+        {
+            yaz_log(LOG_DEBUG, "PREPARE FOR RECONNECT");
+            c->reconnect_ok = 1;
+        }
+    }
+
     task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
     task->u.search.resultset = r;
     ZOOM_resultset_addref (r);  
@@ -573,6 +609,7 @@ ZOOM_resultset_destroy(ZOOM_resultset r)
        ZOOM_options_destroy (r->options);
        odr_destroy (r->odr);
         xfree (r->setname);
+        xfree (r->schema);
        xfree (r);
     }
 }
@@ -597,6 +634,7 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r,
 {
     ZOOM_task task;
     ZOOM_connection c;
+    const char *cp;
 
     if (!r)
        return;
@@ -608,6 +646,16 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r,
     task->u.retrieve.start = start;
     task->u.retrieve.count = count;
 
+    cp = ZOOM_options_get (r->options, "schema");
+    if (cp)
+    {
+        if (!r->schema || strcmp(r->schema, cp))
+        {
+            xfree(r->schema);
+            r->schema = xstrdup(cp);
+        }
+    }
+
     ZOOM_resultset_addref (r);
 
     if (!r->connection->async || force_sync)
@@ -792,6 +840,7 @@ static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
 {
     ZOOM_Event event;
+    yaz_log(LOG_LOG, "sending Z39.50 APDU");
     assert (a);
     if (encode_APDU(c, a, c->odr_out))
        return zoom_complete;
@@ -959,25 +1008,60 @@ static zoom_ret send_srw (ZOOM_connection c, Z_SRW_searchRetrieve *sr)
 #if HAVE_XSLT
 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
 {
-    ZOOM_resultset resultset;
-    Z_SRW_searchRetrieve *sr = yaz_srw_get(c->odr_out,
-                                           Z_SRW_searchRetrieve_request);
+    int i;
+    ZOOM_resultset resultset = 0;
+    Z_SRW_searchRetrieve *sr = 0;
 
+    if (c->error)                  /* don't continue on error */
+       return zoom_complete;
     assert (c->tasks);
-    assert (c->tasks->which == ZOOM_TASK_SEARCH);
-    
-    resultset = c->tasks->u.search.resultset;
-    assert(resultset);
-    assert (resultset->z_query);
+    if (c->tasks->which == ZOOM_TASK_SEARCH)
+        resultset = c->tasks->u.search.resultset;
+    else if(c->tasks->which == ZOOM_TASK_RETRIEVE)
+    {
+        resultset = c->tasks->u.retrieve.resultset;
+
+        resultset->start = c->tasks->u.retrieve.start;
+        resultset->count = c->tasks->u.retrieve.count;
+        
+        if (resultset->start >= resultset->size)
+            return zoom_complete;
+        if (resultset->start + resultset->count > resultset->size)
+            resultset->count = resultset->size - resultset->start;
+
+        for (i = 0; i<resultset->count; i++)
+        {
+            ZOOM_record rec =
+                record_cache_lookup (resultset, i + resultset->start);
+            if (!rec)
+                break;
+        }
+        if (i == resultset->count)
+            return zoom_complete;
+    }
+    assert(resultset->z_query);
+        
+    sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
 
     if (resultset->z_query->which == Z_Query_type_104
         && resultset->z_query->u.type_104->which == Z_External_CQL)
         sr->u.request->query = resultset->z_query->u.type_104->u.cql;
+    else if (resultset->z_query->which == Z_Query_type_1)
+    { 
+        set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, "Type-1");
+        return zoom_complete;
+    }
     else
-        sr->u.request->query = "dc.title = computer";        
-
+    {
+        set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
+        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->recordSchema = resultset->schema;
+
+    resultset->setname = xstrdup ("default");
+    ZOOM_options_set (resultset->options, "setname", resultset->setname);
  
     return send_srw(c, sr);
 }
@@ -998,7 +1082,6 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
     const char *elementSetName;
     const char *smallSetElementSetName;
     const char *mediumSetElementSetName;
-    const char *schema;
 
     assert (c->tasks);
     assert (c->tasks->which == ZOOM_TASK_SEARCH);
@@ -1011,8 +1094,6 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
        ZOOM_options_get (r->options, "smallSetElementSetName");
     mediumSetElementSetName =
        ZOOM_options_get (r->options, "mediumSetElementSetName");
-    schema =
-       ZOOM_options_get (r->options, "schema");
 
     if (!smallSetElementSetName)
        smallSetElementSetName = elementSetName;
@@ -1043,7 +1124,7 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
        *search_req->mediumSetPresentNumber = mspn;
     }
     else if (r->start == 0 && r->count > 0
-            && r->piggyback && !r->r_sort_spec && !schema)
+            && r->piggyback && !r->r_sort_spec && !r->schema)
     {
        /* Regular piggyback - do it unless we're going to do sort */
        *search_req->largeSetLowerBound = 2000000000;
@@ -1124,8 +1205,6 @@ static void response_diag (ZOOM_connection c, Z_DiagRec *p)
        return;
     }
     r = p->u.defaultFormat;
-    c->diagset = yaz_z3950oid_to_str(r->diagnosticSetId, &oclass);
-
     switch (r->which)
     {
     case Z_DefaultDiagFormat_v2Addinfo:
@@ -1135,9 +1214,9 @@ static void response_diag (ZOOM_connection c, Z_DiagRec *p)
        addinfo = r->u.v3Addinfo;
        break;
     }
-    if (addinfo)
-       c->addinfo = xstrdup (addinfo);
-    c->error = *r->condition;
+    set_dset_error(c, *r->condition,
+                   yaz_z3950oid_to_str(r->diagnosticSetId, &oclass),
+                   addinfo, 0);
 }
 
 ZOOM_API(ZOOM_record)
@@ -1368,7 +1447,17 @@ ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
     return 0;
 }
 
-static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
+static int strcmp_null(const char *v1, const char *v2)
+{
+    if (!v1 && !v2)
+        return 0;
+    if (!v1 || !v2)
+        return -1;
+    return strcmp(v1, v2);
+}
+
+static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, 
+                              int pos)
 {
     ZOOM_record_cache rc;
     const char *elementSetName =
@@ -1380,20 +1469,16 @@ static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
     {
        if (pos == rc->pos)
        {
-           if ((!elementSetName && !rc->elementSetName)
-               || (elementSetName && rc->elementSetName &&
-                   !strcmp (elementSetName, rc->elementSetName)))
-           {
-                if ((!syntax && !rc->syntax)
-                    || (syntax && rc->syntax &&
-                        !strcmp (syntax, rc->syntax)))
-                {
-                    /* not destroying rc->npr (it's handled by nmem )*/
-                    rc->rec.npr = npr;
-                    /* keeping wrbuf_marc too */
-                    return;
-                }
-           }
+           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;
        }
     }
     rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
@@ -1410,6 +1495,11 @@ static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
     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_cache;
     r->record_cache = rc;
@@ -1427,15 +1517,13 @@ static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
     {
        if (pos == rc->pos)
        {
-           if ((!elementSetName && !rc->elementSetName)
-               || (elementSetName && rc->elementSetName &&
-                   !strcmp (elementSetName, rc->elementSetName)))
-            {
-                if ((!syntax && !rc->syntax)
-                    || (syntax && rc->syntax &&
-                        !strcmp (syntax, rc->syntax)))
-                    return &rc->rec;
-            }
+           if (strcmp_null(r->schema, rc->schema))
+                continue;
+           if (strcmp_null(elementSetName,rc->elementSetName))
+                continue;
+            if (strcmp_null(syntax, rc->syntax))
+                continue;
+            return &rc->rec;
        }
     }
     return 0;
@@ -1599,7 +1687,6 @@ static zoom_ret send_present (ZOOM_connection c)
     int i = 0;
     const char *syntax = 0;
     const char *elementSetName = 0;
-    const char *schema = 0;
     ZOOM_resultset  resultset;
 
     if (!c->tasks)
@@ -1626,7 +1713,6 @@ static zoom_ret send_present (ZOOM_connection c)
 
     syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
     elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
-    schema = ZOOM_resultset_option_get (resultset, "schema");
 
     if (c->error)                  /* don't continue on error */
        return zoom_complete;
@@ -1655,7 +1741,7 @@ static zoom_ret send_present (ZOOM_connection c)
        req->preferredRecordSyntax =
            yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
 
-    if (schema && *schema)
+    if (resultset->schema && *resultset->schema)
     {
        Z_RecordComposition *compo = (Z_RecordComposition *)
             odr_malloc (c->odr_out, sizeof(*compo));
@@ -1673,14 +1759,14 @@ static zoom_ret send_present (ZOOM_connection c)
 
         compo->u.complex->generic->which = Z_Specification_oid;
         compo->u.complex->generic->u.oid = (Odr_oid *)
-            yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
+            yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, resultset->schema);
 
         if (!compo->u.complex->generic->u.oid)
         {
             /* OID wasn't a schema! Try record syntax instead. */
 
             compo->u.complex->generic->u.oid = (Odr_oid *)
-                yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
+                yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, resultset->schema);
         }
         if (elementSetName && *elementSetName)
         {
@@ -2077,83 +2163,6 @@ ZOOM_package_option_get (ZOOM_package p, const char *key)
     return ZOOM_options_get (p->options, key);
 }
 
-#if HAVE_GSOAP
-static zoom_ret ZOOM_srw_search(ZOOM_connection c, ZOOM_resultset r,
-                                const char *cql)
-{
-    int ret;
-    struct xcql__operandType *xQuery = 0;
-    char *action = 0;
-    xsd__integer startRecord = r->start + 1;
-    xsd__integer maximumRecord = r->count;
-    const char *schema = ZOOM_resultset_option_get (r, "schema");
-    struct zs__searchRetrieveResponse res;
-    xsd__string recordPacking = 0;
-    
-
-    if (!schema)
-        schema = "http://www.loc.gov/marcxml/";
-
-    ret = soap_call_zs__searchRetrieveRequest(c->soap, c->host_port,
-                                              action,
-                                              &r->z_query->u.type_104->u.cql,
-                                              xQuery, 
-                                              0, 0,
-                                              &startRecord, &maximumRecord,
-                                              (char **) &schema,
-                                              &recordPacking,
-                                              &res);
-    if (ret != SOAP_OK)
-    {
-        const char **s = soap_faultdetail(c->soap);
-        xfree (c->addinfo);
-        c->addinfo = 0;
-        if (s && *s)
-            c->addinfo = xstrdup(*s);
-        c->diagset = "SOAP";
-        c->error = ret;
-    }
-    else
-    {
-        if (res.diagnostics.__sizeDiagnostics > 0)
-        {
-            int i = 0;
-            xfree (c->addinfo);
-            c->addinfo = 0;
-            c->diagset = "SRW";
-            c->error = res.diagnostics.diagnostic[i]->code;
-            if (res.diagnostics.diagnostic[i]->details)
-                c->addinfo =
-                    xstrdup(res.diagnostics.diagnostic[i]->details);
-            
-        }
-        else
-        {
-            int i;
-            r->size = res.numberOfRecords;
-            if (res.resultSetId)
-                r->setname = xstrdup(res.resultSetId);
-            for (i = 0; i < res.records.__sizeRecords; i++)
-            {
-                char *rdata = res.records.record[i]->recordData;
-                if (rdata)
-                {
-                    Z_NamePlusRecord *npr =
-                        odr_malloc(r->odr, sizeof(*npr));
-                    Z_External *ext =
-                        z_ext_record(r->odr, VAL_TEXT_XML,
-                                     rdata, strlen(rdata));
-                    npr->databaseName = 0;
-                    npr->which = Z_NamePlusRecord_databaseRecord;
-                    npr->u.databaseRecord = ext;
-                    record_cache_add (r, npr, r->start + i);
-                }
-            }
-        }
-    }
-    return zoom_complete;
-}
-#endif
 
 ZOOM_API(void)
 ZOOM_package_option_set (ZOOM_package p, const char *key,
@@ -2187,38 +2196,6 @@ static int ZOOM_connection_exec_task (ZOOM_connection c)
     }
     task->running = 1;
     ret = zoom_complete;
-#if 0
-    if (c->proto == PROTO_SRW)
-    {
-        ZOOM_resultset resultset;
-        switch (task->which)
-        {
-        case ZOOM_TASK_SEARCH:
-            resultset = c->tasks->u.search.resultset;
-            if (resultset->z_query && 
-                resultset->z_query->which == Z_Query_type_104
-                && resultset->z_query->u.type_104->which == Z_External_CQL)
-                ret = ZOOM_srw_search(c, resultset, 
-                                      resultset->z_query->u.type_104->u.cql);
-            break;
-        case ZOOM_TASK_RETRIEVE:
-            resultset = c->tasks->u.retrieve.resultset;
-            resultset->start = c->tasks->u.retrieve.start;
-            resultset->count = c->tasks->u.retrieve.count;
-
-            if (resultset->start >= resultset->size)
-                return zoom_complete;
-            if (resultset->start + resultset->count > resultset->size)
-                resultset->count = resultset->size - resultset->start;
-
-            if (resultset->z_query && 
-                resultset->z_query->which == Z_Query_type_104
-                && resultset->z_query->u.type_104->which == Z_External_CQL)
-                ret = ZOOM_srw_search(c, resultset, 
-                                      resultset->z_query->u.type_104->u.cql);
-            break;
-        }
-#endif
     if (c->cs || task->which == ZOOM_TASK_CONNECT)
     {
         switch (task->which)
@@ -2230,7 +2207,10 @@ static int ZOOM_connection_exec_task (ZOOM_connection c)
                 ret = ZOOM_connection_send_search(c);
             break;
         case ZOOM_TASK_RETRIEVE:
-            ret = send_present (c);
+            if (c->proto == PROTO_SRW)
+                ret = ZOOM_connection_srw_send_search(c);
+            else
+                ret = send_present (c);
             break;
         case ZOOM_TASK_CONNECT:
             ret = do_connect(c);
@@ -2396,12 +2376,139 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
     }
 }
 
+#if HAVE_XSLT
+static void handle_srw_response(ZOOM_connection c,
+                                Z_SRW_searchRetrieveResponse *res)
+{
+    ZOOM_resultset resultset = 0;
+    int i;
+    NMEM nmem;
+
+    if (!c->tasks)
+        return;
+
+    if (c->tasks->which == ZOOM_TASK_SEARCH)
+        resultset = c->tasks->u.search.resultset;
+    else if (c->tasks->which == ZOOM_TASK_RETRIEVE)
+        resultset = c->tasks->u.retrieve.resultset;
+    else
+       return ;
+
+    resultset->size = 0;
+
+    yaz_log(LOG_LOG, "got SRW response OK");
+    
+    if (res->numberOfRecords)
+        resultset->size = *res->numberOfRecords;
+
+    for (i = 0; i<res->num_records; i++)
+    {
+        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 = resultset->start + 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 =
+            yaz_oidval_to_z3950oid(c->odr_in, CLASS_RECSYN, VAL_TEXT_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 = 
+            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);
+        yaz_log(LOG_LOG, "add SRW record to cache to pos %d", pos);
+    }
+    if (res->num_diagnostics > 0)
+    {
+        set_dset_error(c, *res->diagnostics[0].code, "SRW",
+                       res->diagnostics[0].details, 0);
+    }
+    nmem = odr_extract_mem(c->odr_in);
+    nmem_transfer(resultset->odr->mem, nmem);
+    nmem_destroy(nmem);
+}
+#endif
+
+#if HAVE_XSLT
 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
 {
+    int ret = -1;
+    const char *content_type = z_HTTP_header_lookup(hres->headers,
+                                                    "Content-Type");
+    const char *connection_head = z_HTTP_header_lookup(hres->headers,
+                                                       "Connection");
     c->mask = 0;
     yaz_log (LOG_DEBUG, "handle_http");
+
+    if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
+    {
+        Z_SOAP *soap_package = 0;
+        ODR o = odr_createmem(ODR_DECODE);
+        Z_SOAP_Handler soap_handlers[2] = {
+            {"http://www.loc.gov/zing/srw/v1.0/", 0,
+             (Z_SOAP_fun) yaz_srw_codec},
+            {0, 0, 0}
+        };
+        ret = z_soap_codec(o, &soap_package,
+                           &hres->content_buf, &hres->content_len,
+                           soap_handlers);
+        if (!ret && soap_package->which == Z_SOAP_generic &&
+            soap_package->u.generic->no == 0)
+        {
+            Z_SRW_searchRetrieve *sr = soap_package->u.generic->p;
+            if (sr->which == Z_SRW_searchRetrieve_response)
+                handle_srw_response(c, sr->u.response);
+            else
+                ret = -1;
+        }
+        else if (!ret && (soap_package->which == Z_SOAP_fault
+                          || soap_package->which == Z_SOAP_error))
+        {
+            set_HTTP_error(c, hres->code,
+                           soap_package->u.fault->fault_code,
+                           soap_package->u.fault->fault_string);
+        }
+        else
+            ret = -1;
+        odr_destroy(o);
+    }
+    if (ret)
+    {
+        if (hres->code != 200)
+            set_HTTP_error(c, hres->code, 0, 0);
+        else
+            set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
+        do_close (c);
+    }
     ZOOM_connection_remove_task(c);
+    if (!strcmp(hres->version, "1.0"))
+    {
+        /* HTTP 1.0: only if Keep-Alive we stay alive.. */
+        if (!connection_head || strcmp(connection_head, "Keep-Alive"))
+            do_close(c);
+    }
+    else 
+    {
+        /* HTTP 1.1: only if no close we stay alive .. */
+        if (connection_head && !strcmp(connection_head, "close"))
+            do_close(c);
+    }
 }
+#endif
 
 static int do_read (ZOOM_connection c)
 {
@@ -2449,7 +2556,14 @@ static int do_read (ZOOM_connection c)
        else if (gdu->which == Z_GDU_Z3950)
            handle_apdu (c, gdu->u.z3950);
         else if (gdu->which == Z_GDU_HTTP_Response)
+        {
+#if HAVE_XSLT
             handle_http (c, gdu->u.HTTP_Response);
+#else
+            set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
+           do_close (c);
+#endif
+        }
         c->reconnect_ok = 0;
     }
     return 1;
@@ -2579,6 +2693,8 @@ ZOOM_diag_str (int error)
        return "Timeout";
     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
        return "Unsupported protocol";
+    case ZOOM_ERROR_UNSUPPORTED_QUERY:
+       return "Unsupported query type";
     default:
        return diagbib1_str (error);
     }
@@ -2591,22 +2707,16 @@ ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
     int error = c->error;
     if (cp)
     {
-        if (!c->diagset)
+        if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
+            *cp = ZOOM_diag_str(error);
+        else if (!strcmp(c->diagset, "HTTP"))
+            *cp = z_HTTP_errmsg(c->error);
+        else if (!strcmp(c->diagset, "Bib-1"))
             *cp = ZOOM_diag_str(error);
-#if HAVE_GSOAP
         else if (!strcmp(c->diagset, "SRW"))
-            *cp = yaz_srw_diag_str(error);
-        else if (c->soap && !strcmp(c->diagset, "SOAP"))
-        {
-            const char **s = soap_faultstring(c->soap);
-            if (s && *s)
-                *cp = *s;
-            else
-                *cp = "unknown";
-        }
-#endif
+            *cp = yaz_srw_error_str(c->error);
         else
-            *cp = ZOOM_diag_str(error);
+            *cp = "Unknown error and diagnostic set";
     }
     if (addinfo)
         *addinfo = c->addinfo ? c->addinfo : "";
index f62d940..2b776d9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Private C header for ZOOM C.
- * $Id: zoom-p.h,v 1.4 2003-02-14 18:49:24 adam Exp $
+ * $Id: zoom-p.h,v 1.5 2003-02-17 14:35:42 adam Exp $
  */
 
 #if HAVE_XSLT
@@ -90,6 +90,7 @@ struct ZOOM_resultset_p {
     int count;
     int piggyback;
     char *setname;
+    char *schema;
     ODR odr;
     ZOOM_record_cache record_cache;
     ZOOM_options options;
@@ -107,6 +108,7 @@ struct ZOOM_record_cache_p {
     struct ZOOM_record_p rec;
     char *elementSetName;
     char *syntax;
+    char *schema;
     int pos;
     ZOOM_record_cache next;
 };