Added cs_get_SSL. yaz-client-ssl prints peer info
[yaz-moved-to-github.git] / src / zoom-c.c
index cb3dbdb..b5241f3 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 2000-2004, Index Data
  * See the file LICENSE for details.
  *
- * $Id: zoom-c.c,v 1.19 2004-01-16 10:04:54 adam Exp $
+ * $Id: zoom-c.c,v 1.26 2004-04-06 17:47:24 adam Exp $
  *
  * ZOOM layer for C, connections, result sets, queries.
  */
@@ -90,10 +90,18 @@ static void set_dset_error (ZOOM_connection c, int error,
                             const char *dset,
                             const char *addinfo, const char *addinfo2)
 {
+    char *cp;
     xfree (c->addinfo);
     c->addinfo = 0;
     c->error = error;
-    c->diagset = dset;
+    if (!c->diagset || strcmp(dset, c->diagset))
+    {
+        xfree(c->diagset);
+        c->diagset = xstrdup(dset);
+       /* remove integer part from SRW diagset .. */
+        if ((cp = strrchr(c->diagset, '/')))
+          *cp = '\0';
+    }
     if (addinfo && addinfo2)
     {
         c->addinfo = xmalloc(strlen(addinfo) + strlen(addinfo2) + 2);
@@ -191,6 +199,10 @@ void ZOOM_connection_remove_task (ZOOM_connection c)
         case ZOOM_TASK_PACKAGE:
             ZOOM_package_destroy (task->u.package);
             break;
+       case ZOOM_TASK_SORT:
+           ZOOM_resultset_destroy (task->u.sort.resultset);
+           ZOOM_query_destroy(task->u.sort.q);
+           break;
        default:
            assert (0);
        }
@@ -219,6 +231,7 @@ ZOOM_connection_create (ZOOM_options options)
     c->reconnect_ok = 0;
     c->state = STATE_IDLE;
     c->addinfo = 0;
+    c->diagset = 0;
     set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
     c->buf_in = 0;
     c->len_in = 0;
@@ -422,7 +435,10 @@ ZOOM_query_prefix(ZOOM_query s, const char *str)
     s->z_query->which = Z_Query_type_1;
     s->z_query->u.type_1 =  p_query_rpn(s->odr, PROTO_Z3950, str);
     if (!s->z_query->u.type_1)
+    {
+       s->z_query = 0;
        return -1;
+    }
     return 0;
 }
 
@@ -471,6 +487,7 @@ ZOOM_connection_destroy(ZOOM_connection c)
 
     xfree (c->buf_in);
     xfree (c->addinfo);
+    xfree (c->diagset);
     odr_destroy (c->odr_in);
     odr_destroy (c->odr_out);
     ZOOM_options_destroy (c->options);
@@ -495,6 +512,7 @@ void ZOOM_resultset_addref (ZOOM_resultset r)
                  r, r->refcount);
     }
 }
+
 ZOOM_resultset ZOOM_resultset_create ()
 {
     ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
@@ -592,6 +610,62 @@ ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
 }
 
 ZOOM_API(void)
+    ZOOM_resultset_sort(ZOOM_resultset r,
+                       const char *sort_type, const char *sort_spec)
+{
+    ZOOM_connection c = r->connection;
+    ZOOM_task task;
+
+    if (!c)
+       return;
+
+    if (c->host_port && c->proto == PROTO_HTTP)
+    {
+        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;
+        }
+    }
+    
+    ZOOM_resultset_cache_reset(r);
+    task = ZOOM_connection_add_task (c, ZOOM_TASK_SORT);
+    task->u.sort.resultset = r;
+    task->u.sort.q = ZOOM_query_create();
+    ZOOM_query_sortby(task->u.sort.q, sort_spec);
+
+    ZOOM_resultset_addref (r);  
+
+    if (!c->async)
+    {
+       while (ZOOM_event (1, &c))
+           ;
+    }
+}
+
+ZOOM_API(void)
+    ZOOM_resultset_cache_reset(ZOOM_resultset r)
+{
+    ZOOM_record_cache rc;
+    
+    for (rc = r->record_cache; rc; rc = rc->next)
+    {
+       if (rc->rec.wrbuf_marc)
+           wrbuf_free (rc->rec.wrbuf_marc, 1);
+       if (rc->rec.wrbuf_iconv)
+           wrbuf_free (rc->rec.wrbuf_iconv, 1);
+       if (rc->rec.wrbuf_opac)
+           wrbuf_free (rc->rec.wrbuf_opac, 1);
+    }
+    r->record_cache = 0;
+}
+
+ZOOM_API(void)
 ZOOM_resultset_destroy(ZOOM_resultset r)
 {
     if (!r)
@@ -601,17 +675,8 @@ ZOOM_resultset_destroy(ZOOM_resultset r)
              r, r->refcount);
     if (r->refcount == 0)
     {
-        ZOOM_record_cache rc;
+       ZOOM_resultset_cache_reset(r);
 
-        for (rc = r->record_cache; rc; rc = rc->next)
-       {
-            if (rc->rec.wrbuf_marc)
-                wrbuf_free (rc->rec.wrbuf_marc, 1);
-            if (rc->rec.wrbuf_iconv)
-                wrbuf_free (rc->rec.wrbuf_iconv, 1);
-            if (rc->rec.wrbuf_opac)
-                wrbuf_free (rc->rec.wrbuf_opac, 1);
-       }
        if (r->connection)
        {
            /* remove ourselves from the resultsets in connection */
@@ -922,7 +987,7 @@ static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
        ZOOM_options_get(c->options, "implementationName"),
        odr_prepend(c->odr_out, "ZOOM-C", ireq->implementationName));
 
-    version = odr_strdup(c->odr_out, "$Revision: 1.19 $");
+    version = odr_strdup(c->odr_out, "$Revision: 1.26 $");
     if (strlen(version) > 10)  /* check for unexpanded CVS strings */
        version[strlen(version)-2] = '\0';
     ireq->implementationVersion = odr_prepend(c->odr_out,
@@ -1192,6 +1257,11 @@ static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
 
     /* prepare query for the search request */
     search_req->query = r->query->z_query;
+    if (!search_req->query)
+    {
+        set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0);
+       return zoom_complete;
+    }
 
     search_req->databaseNames =
        set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
@@ -1519,21 +1589,23 @@ ZOOM_record_get (ZOOM_record rec, const char *type_spec, int *len)
            i++;
        if (!strncmp(type_spec+i, "charset=", 8))
        {
-           cp = type_spec+i+8;
-           for (i = 0; cp[i] && i < sizeof(charset)-1; i++)
+           int j = 0;
+           i = i + 8; /* skip charset= */
+           for (j = 0; type_spec[i]  && j < sizeof(charset)-1; i++, j++)
            {
-               if (cp[i] == ';' || cp[i] == ' ')
+               if (type_spec[i] == ';' || type_spec[i] == ' ')
                    break;
-               charset[i] = cp[i];
+               charset[j] = cp[i];
            }
-           charset[i] = '\0';
+           charset[j] = '\0';
        }
        else if (!strncmp(type_spec+i, "xpath=", 6))
        {
-           cp = type_spec+i+6;
-           for (i = 0; cp[i] && i < sizeof(xpath)-1; i++)
-               xpath[i] = cp[i];
-           xpath[i] = '\0';
+           int j = 0; 
+           i = i + 6;
+           for (j = 0; type_spec[i] && j < sizeof(xpath)-1; i++, j++)
+               xpath[j] = cp[i];
+           xpath[j] = '\0';
        } 
        while (type_spec[i] == ' ')
            i++;
@@ -1931,20 +2003,11 @@ static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
     return 1;
 }
 
-static zoom_ret send_sort (ZOOM_connection c)
+static zoom_ret send_sort (ZOOM_connection c,
+                          ZOOM_resultset resultset)
 {
-    ZOOM_resultset  resultset;
-
-    if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
-       return zoom_complete;
-
-    resultset = c->tasks->u.search.resultset;
-
     if (c->error)
-    {
        resultset->r_sort_spec = 0;
-       return zoom_complete;
-    }
     if (resultset->r_sort_spec)
     {
        Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
@@ -2453,7 +2516,8 @@ static Z_APDU *create_update_package(ZOOM_package p)
     int num_db;
     char **db = set_DatabaseNames(p->connection, p->options, &num_db);
     const char *action = ZOOM_options_get(p->options, "action");
-    const char *recordId = ZOOM_options_get(p->options, "recordId");
+    const char *recordIdOpaque = ZOOM_options_get(p->options, "recordIdOpaque");
+    const char *recordIdNumber = ZOOM_options_get(p->options, "recordIdNumber");
     const char *record_buf = ZOOM_options_get(p->options, "record");
     const char *syntax_str = ZOOM_options_get(p->options, "syntax");
     int syntax_oid = VAL_NONE;
@@ -2529,14 +2593,21 @@ static Z_APDU *create_update_package(ZOOM_package p)
        notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
            odr_malloc(p->odr_out, sizeof(**notToKeep->elements));
        notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
-       if (recordId)
+       if (recordIdOpaque)
        {
            notToKeep->elements[0]->u.opaque = (Odr_oct *)
                odr_malloc (p->odr_out, sizeof(Odr_oct));
-           notToKeep->elements[0]->u.opaque->size = strlen(recordId);
-           notToKeep->elements[0]->u.opaque->len = strlen(recordId);
+           notToKeep->elements[0]->u.opaque->size =
+               notToKeep->elements[0]->u.opaque->len = strlen(recordIdOpaque);
            notToKeep->elements[0]->u.opaque->buf =
-               odr_strdup(p->odr_out, recordId);
+               odr_strdup(p->odr_out, recordIdOpaque);
+       }
+       else if (recordIdNumber)
+       {
+           notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_number;
+           
+           notToKeep->elements[0]->u.number =
+               odr_intdup(p->odr_out, atoi(recordIdNumber));
        }
        else
            notToKeep->elements[0]->u.opaque = 0;
@@ -2714,6 +2785,11 @@ static int ZOOM_connection_exec_task (ZOOM_connection c)
             break;
         case ZOOM_TASK_PACKAGE:
             ret = send_package(c);
+           break;
+       case ZOOM_TASK_SORT:
+           c->tasks->u.sort.resultset->r_sort_spec = 
+               c->tasks->u.sort.q->sort_spec;
+           ret = send_sort(c, c->tasks->u.sort.resultset);
             break;
         }
     }
@@ -2734,7 +2810,10 @@ static int ZOOM_connection_exec_task (ZOOM_connection c)
 
 static zoom_ret send_sort_present (ZOOM_connection c)
 {
-    zoom_ret r = send_sort (c);
+    zoom_ret r = zoom_complete;
+
+    if (c->tasks && c->tasks->which == ZOOM_TASK_SEARCH)
+       r = send_sort (c, c->tasks->u.search.resultset);
     if (r == zoom_complete)
        r = send_present (c);
     return r;
@@ -2941,8 +3020,16 @@ static void handle_srw_response(ZOOM_connection c,
     }
     if (res->num_diagnostics > 0)
     {
-        set_dset_error(c, *res->diagnostics[0].code, "SRW",
-                       res->diagnostics[0].details, 0);
+       const char *uri = res->diagnostics[0].uri;
+       if (uri)
+       {
+           int code = 0;       
+           const char *cp;
+           if ((cp = strrchr(uri, '/')))
+               code = atoi(cp+1);
+           set_dset_error(c, code, uri,
+                          res->diagnostics[0].details, 0);
+       }
     }
     nmem = odr_extract_mem(c->odr_in);
     nmem_transfer(resultset->odr->mem, nmem);
@@ -3208,6 +3295,8 @@ ZOOM_diag_str (int error)
        return "Unsupported protocol";
     case ZOOM_ERROR_UNSUPPORTED_QUERY:
        return "Unsupported query type";
+    case ZOOM_ERROR_INVALID_QUERY:
+       return "Invalid query";
     default:
        return diagbib1_str (error);
     }
@@ -3226,7 +3315,7 @@ ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
             *cp = z_HTTP_errmsg(c->error);
         else if (!strcmp(c->diagset, "Bib-1"))
             *cp = ZOOM_diag_str(error);
-        else if (!strcmp(c->diagset, "SRW"))
+        else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
             *cp = yaz_diag_srw_str(c->error);
         else
             *cp = "Unknown error and diagnostic set";