Adjustments to make YAZ compile as C++ code.
[yaz-moved-to-github.git] / src / zoom-c.c
index 6ebcc33..8befe19 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: zoom-c.c,v 1.141 2007-08-16 10:09:36 adam Exp $
+ * $Id: zoom-c.c,v 1.152 2007-11-30 11:44:47 adam Exp $
  */
 /**
  * \file zoom-c.c
@@ -49,7 +49,7 @@ static char *cql2pqf(ZOOM_connection c, const char *cql);
  * 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 +174,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 +194,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
@@ -395,6 +398,8 @@ ZOOM_API(ZOOM_connection)
 
     c->m_queue_front = 0;
     c->m_queue_back = 0;
+
+    c->sru_version = 0;
     return c;
 }
 
@@ -541,6 +546,10 @@ ZOOM_API(void)
     val = ZOOM_options_get(c->options, "sru");
     c->sru_mode = get_sru_mode_from_string(val);
 
+    xfree(c->sru_version);
+    val = ZOOM_options_get(c->options, "sru_version");
+    c->sru_version = xstrdup(val ? val : "1.2");
+
     ZOOM_options_set(c->options, "host", c->host_port);
 
     xfree(c->cookie_out);
@@ -781,6 +790,7 @@ ZOOM_API(void)
     xfree(c->user);
     xfree(c->group);
     xfree(c->password);
+    xfree(c->sru_version);
     xfree(c);
 }
 
@@ -1346,7 +1356,7 @@ 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.141 $");
+    version = odr_strdup(c->odr_out, "$Revision: 1.152 $");
     if (strlen(version) > 10)   /* check for unexpanded CVS strings */
         version[strlen(version)-2] = '\0';
     ireq->implementationVersion = 
@@ -1361,15 +1371,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;
@@ -1436,6 +1440,15 @@ static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
 }
 #endif
 
+
+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;
+}
+
+
 #if YAZ_HAVE_XML2
 static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
 {
@@ -1493,8 +1506,7 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
     }
     assert(resultset->query);
         
-    sr = yaz_srw_get(c->odr_out, Z_SRW_searchRetrieve_request);
-
+    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)
     {
@@ -1730,6 +1742,11 @@ ZOOM_API(ZOOM_record)
     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;
 }
@@ -1886,6 +1903,7 @@ static const char *record_iconv_return(ZOOM_record rec, int *len,
     return buf;
 }
 
+
 ZOOM_API(int)
     ZOOM_record_error(ZOOM_record rec, const char **cp,
                       const char **addinfo, const char **diagset)
@@ -1894,7 +1912,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;
@@ -1997,6 +2026,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;   
@@ -2170,53 +2205,59 @@ 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->rec.wrbuf_opac = 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,
@@ -2293,7 +2334,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)
@@ -2312,7 +2354,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)
@@ -2320,7 +2362,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);
         }
     }
 }
@@ -2832,7 +2875,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(c->odr_out, Z_SRW_scan_request);
+    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)
@@ -2998,7 +3041,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);
 
@@ -3091,16 +3134,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;
         
@@ -3320,9 +3363,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);
@@ -3357,9 +3398,8 @@ 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;
         }
@@ -3821,6 +3861,9 @@ static void handle_srw_response(ZOOM_connection c,
 
     resultset->size = 0;
 
+    if (res->resultSetId)
+        ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
+
     yaz_log(log_details, "%p handle_srw_response got SRW response OK", c);
     
     if (res->numberOfRecords)
@@ -3829,7 +3872,10 @@ static void handle_srw_response(ZOOM_connection c,
     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));
 
@@ -3839,6 +3885,8 @@ static void handle_srw_response(ZOOM_connection c,
         else
             pos = *start + i;
         
+        sru_rec = &res->records[i];
+
         npr->databaseName = 0;
         npr->which = Z_NamePlusRecord_databaseRecord;
         npr->u.databaseRecord = (Z_External *)
@@ -3851,11 +3899,22 @@ static void handle_srw_response(ZOOM_connection c,
         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;
+            sru_rec->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);
+            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);
     }
     if (res->num_diagnostics > 0)
         set_SRU_error(c, &res->diagnostics[0]);
@@ -3915,6 +3974,8 @@ 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);
             else if (sr->which == Z_SRW_scan_response)