Fixed bug #172: surrogate diagnostics in SRW lacking NS.
[yaz-moved-to-github.git] / src / seshigh.c
index 3395c8b..f0b2ef9 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2004, Index Data
  * See the file LICENSE for details.
  *
- * $Id: seshigh.c,v 1.17 2004-01-15 10:16:27 adam Exp $
+ * $Id: seshigh.c,v 1.32 2004-10-09 08:21:38 adam Exp $
  */
 
 /*
@@ -471,6 +471,7 @@ static int srw_bend_init(association *assoc)
     ce = yaz_set_proposal_charneg(assoc->decode, &encoding, 1, 0, 0, 1);
     assoc->init->charneg_request = ce->u.charNeg3;
 #endif
+    assoc->backend = 0;
     if (!(binitres = (*cb->bend_init)(assoc->init)))
     {
        yaz_log(LOG_WARN, "Bad response from backend.");
@@ -509,9 +510,22 @@ static int srw_bend_fetch(association *assoc, int pos,
 
     rr.comp->u.complex->generic = (Z_Specification *) 
            odr_malloc(assoc->decode, sizeof(Z_Specification));
+
+    /* schema uri = recordSchema (or NULL if recordSchema is not given) */
     rr.comp->u.complex->generic->which = Z_Schema_uri;
     rr.comp->u.complex->generic->schema.uri = srw_req->recordSchema;
+
+    /* ESN = recordSchema if recordSchema is present */
     rr.comp->u.complex->generic->elementSpec = 0;
+    if (srw_req->recordSchema)
+    {
+       rr.comp->u.complex->generic->elementSpec = 
+           (Z_ElementSpec *) odr_malloc(assoc->encode, sizeof(Z_ElementSpec));
+       rr.comp->u.complex->generic->elementSpec->which = 
+           Z_ElementSpec_elementSetName;
+       rr.comp->u.complex->generic->elementSpec->u.elementSetName =
+           srw_req->recordSchema;
+    }
     
     rr.stream = assoc->encode;
     rr.print = assoc->print;
@@ -532,7 +546,35 @@ static int srw_bend_fetch(association *assoc, int pos,
 
     (*assoc->init->bend_fetch)(assoc->backend, &rr);
 
-    if (rr.len >= 0)
+    if (rr.errcode && rr.surrogate_flag)
+    {
+       int code = yaz_diag_bib1_to_srw(rr.errcode);
+       const char *message = yaz_diag_srw_str(code);
+       int len = 200;
+       if (message)
+           len += strlen(message);
+       if (rr.errstring)
+           len += strlen(rr.errstring);
+
+        record->recordData_buf = odr_malloc(o, len);
+       
+       sprintf(record->recordData_buf, "<diagnostic "
+               "xmlns=\"http://www.loc.gov/zing/srw/diagnostic/\">\n"
+               " <uri>info:srw/diagnostic/1/%d</uri>\n", code);
+       if (rr.errstring)
+           sprintf(record->recordData_buf + strlen(record->recordData_buf),
+                   " <details>%s</details>\n", rr.errstring);
+       if (message)
+           sprintf(record->recordData_buf + strlen(record->recordData_buf),
+                   " <message>%s</message>\n", message);
+       sprintf(record->recordData_buf + strlen(record->recordData_buf),
+               "</diagnostic>\n");
+       record->recordData_len = strlen(record->recordData_buf);
+        record->recordPosition = odr_intdup(o, pos);
+       record->recordSchema = "info:srw/schema/1/diagnostics-v1.1";
+       return 0;
+    }
+    else if (rr.len >= 0)
     {
         record->recordData_buf = rr.record;
         record->recordData_len = rr.len;
@@ -605,7 +647,8 @@ static void srw_bend_search(association *assoc, request *req,
             const char *pqf_msg;
             size_t off;
             int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
-            yaz_log(LOG_LOG, "%*s^\n", off+4, "");
+           if (off < 200)
+               yaz_log(LOG_LOG, "%*s^\n", (int)off+4, "");
             yaz_log(LOG_LOG, "Bad PQF: %s (code %d)\n", pqf_msg, code);
             
             srw_error = 10;
@@ -631,9 +674,8 @@ static void srw_bend_search(association *assoc, request *req,
         srw_res->num_diagnostics = 1;
         srw_res->diagnostics = (Z_SRW_diagnostic *)
            odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
-        srw_res->diagnostics[0].code = 
-            odr_intdup(assoc->encode, srw_error);
-        srw_res->diagnostics[0].details = 0;
+       yaz_mk_std_diagnostic(assoc->encode,
+                             srw_res->diagnostics, srw_error, 0);
         return;
     }
     
@@ -649,7 +691,6 @@ static void srw_bend_search(association *assoc, request *req,
     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)
     {
         yaz_log(LOG_DEBUG, "bend_search returned Bib-1 code %d", rr.errcode);
@@ -661,13 +702,11 @@ static void srw_bend_search(association *assoc, request *req,
         srw_res->num_diagnostics = 1;
         srw_res->diagnostics = (Z_SRW_diagnostic *)
            odr_malloc(assoc->encode, sizeof(*srw_res->diagnostics));
-        srw_res->diagnostics[0].code = 
-            odr_intdup(assoc->encode, 
-                       yaz_diag_bib1_to_srw (rr.errcode));
-        srw_res->diagnostics[0].details = rr.errstring;
-        yaz_log(LOG_DEBUG, "srw_bend_search returned SRW error %d",
-                *srw_res->diagnostics[0].code);
-                
+       yaz_mk_std_diagnostic(assoc->encode, srw_res->diagnostics,
+                             yaz_diag_bib1_to_srw (rr.errcode),
+                             rr.errstring);
+        yaz_log(LOG_DEBUG, "srw_bend_search returned SRW error %s",
+                srw_res->diagnostics[0].uri);
     }
     else
     {
@@ -684,7 +723,12 @@ static void srw_bend_search(association *assoc, request *req,
 
             if (start > rr.hits)
             {
-                yaz_log(LOG_LOG, "Request out or range");
+               srw_res->num_diagnostics = 1;
+               srw_res->diagnostics = (Z_SRW_diagnostic *)
+                   odr_malloc(assoc->encode, 
+                              sizeof(*srw_res->diagnostics));
+               yaz_mk_std_diagnostic(assoc->encode,  srw_res->diagnostics,
+                                     61, 0);
             }
             else
             {
@@ -713,10 +757,11 @@ static void srw_bend_search(association *assoc, request *req,
                         srw_res->diagnostics = (Z_SRW_diagnostic *)
                             odr_malloc(assoc->encode, 
                                        sizeof(*srw_res->diagnostics));
-                        srw_res->diagnostics[0].code = 
-                            odr_intdup(assoc->encode, 
-                                       yaz_diag_bib1_to_srw (errcode));
-                        srw_res->diagnostics[0].details = rr.errstring;
+
+                       yaz_mk_std_diagnostic(assoc->encode, 
+                                             srw_res->diagnostics,
+                                             yaz_diag_bib1_to_srw (errcode),
+                                             rr.errstring);
                         break;
                     }
                     if (srw_res->records[j].recordData_buf)
@@ -754,6 +799,7 @@ static void srw_bend_explain(association *assoc, request *req,
         rr.print = assoc->print;
         rr.explain_buf = 0;
        rr.database = srw_req->database;
+       rr.schema = "http://explain.z3950.org/dtd/2.0/";
         (*assoc->init->bend_explain)(assoc->backend, &rr);
         if (rr.explain_buf)
         {
@@ -761,7 +807,7 @@ static void srw_bend_explain(association *assoc, request *req,
            if (srw_req->recordPacking && 
                !strcmp(srw_req->recordPacking, "xml"))
                packing = Z_SRW_recordPacking_XML;
-           srw_res->record.recordSchema = 0;
+           srw_res->record.recordSchema = rr.schema;
            srw_res->record.recordPacking = packing;
             srw_res->record.recordData_buf = rr.explain_buf;
             srw_res->record.recordData_len = strlen(rr.explain_buf);
@@ -859,6 +905,7 @@ static void process_http_request(association *assoc, request *req)
        }
        else
        {
+                yaz_log(LOG_LOG, "generate soap error");
            http_code = 500;
            z_soap_error(assoc->encode, soap_package,
                         "SOAP-ENV:Client", "Bad method", 0); 
@@ -1032,6 +1079,8 @@ static int process_z_request(association *assoc, request *req, char **msg)
            return -1;
        }
        break;
+    case Z_APDU_triggerResourceControlRequest:
+       return 0;
     default:
        *msg = "Bad APDU received";
        return -1;
@@ -1197,6 +1246,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
             assoc->init->charneg_request = negotiation;
     }
     
+    assoc->backend = 0;
     if (!(binitres = (*cb->bend_init)(assoc->init)))
     {
        yaz_log(LOG_WARN, "Bad response from backend.");
@@ -1283,6 +1333,8 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
        ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
        strcat(options, " negotiation");
     }
+       
+    ODR_MASK_SET(resp->options, Z_Options_triggerResourceCtrl);
 
     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
     {
@@ -1319,7 +1371,7 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
                assoc->init->implementation_name,
                odr_prepend(assoc->encode, "GFS", resp->implementationName));
 
-    version = odr_strdup(assoc->encode, "$Revision: 1.17 $");
+    version = odr_strdup(assoc->encode, "$Revision: 1.32 $");
     if (strlen(version) > 10)  /* check for unexpanded CVS strings */
        version[strlen(version)-2] = '\0';
     resp->implementationVersion = odr_prepend(assoc->encode,
@@ -1414,6 +1466,7 @@ static Z_External *init_diagnostics(ODR odr, int error, char *addinfo)
 
     e->which = Z_DiagnosticFormat_s_defaultDiagRec;
     e->u.defaultDiagRec = justdiag(odr, error, addinfo);
+    e->message = 0;
     return x;
 }
 
@@ -1496,7 +1549,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
     records->u.databaseOrSurDiagnostics = reclist;
     reclist->num_records = 0;
     reclist->records = list;
-    *pres = Z_PRES_SUCCESS;
+    *pres = Z_PresentStatus_success;
     *num = 0;
     *next = 0;
 
@@ -1540,7 +1593,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
            if (!freq.surrogate_flag)
            {
                char s[20];
-               *pres = Z_PRES_FAILURE;
+               *pres = Z_PresentStatus_failure;
                /* for 'present request out of range',
                    set addinfo to record position if not set */
                if (freq.errcode == 13 && freq.errstring == 0)
@@ -1563,13 +1616,14 @@ static Z_Records *pack_records(association *a, char *setname, int start,
            this_length = odr_total(a->encode) - total_length - dumped_records;
        yaz_log(LOG_DEBUG, "  fetched record, len=%d, total=%d dumped=%d",
            this_length, total_length, dumped_records);
-       if (this_length + total_length > a->preferredMessageSize)
+       if (a->preferredMessageSize > 0 &&
+               this_length + total_length > a->preferredMessageSize)
        {
            /* record is small enough, really */
            if (this_length <= a->preferredMessageSize && recno > start)
            {
                yaz_log(LOG_DEBUG, "  Dropped last normal-sized record");
-               *pres = Z_PRES_PARTIAL_2;
+               *pres = Z_PresentStatus_partial_2;
                break;
            }
            /* record can only be fetched by itself */
@@ -1689,7 +1743,7 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb,
     int *nulint = odr_intdup (assoc->encode, 0);
     bool_t *sr = odr_intdup(assoc->encode, 1);
     int *next = odr_intdup(assoc->encode, 0);
-    int *none = odr_intdup(assoc->encode, Z_RES_NONE);
+    int *none = odr_intdup(assoc->encode, Z_SearchResponse_none);
 
     apdu->which = Z_APDU_searchResponse;
     apdu->u.searchResponse = resp;
@@ -1835,7 +1889,7 @@ static Z_APDU *process_presentRequest(association *assoc, request *reqb,
        if (bprr->errcode)
        {
            resp->records = diagrec(assoc, bprr->errcode, bprr->errstring);
-           *resp->presentStatus = Z_PRES_FAILURE;
+           *resp->presentStatus = Z_PresentStatus_failure;
        }
     }
     apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
@@ -2049,7 +2103,7 @@ static Z_APDU *process_sortRequest(association *assoc, request *reqb,
     bsrr->stream = assoc->encode;
     bsrr->print = assoc->print;
 
-    bsrr->sort_status = Z_SortStatus_failure;
+    bsrr->sort_status = Z_SortResponse_failure;
     bsrr->errcode = 0;
     bsrr->errstring = 0;