Link SSL with libyaz.la and yaz-client only.
[yaz-moved-to-github.git] / src / seshigh.c
index 46e6210..d1205ad 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2008 Index Data
  * See the file LICENSE for details.
- *
- * $Id: seshigh.c,v 1.125 2007-08-21 13:20:51 adam Exp $
  */
 /**
  * \file seshigh.c
@@ -737,29 +735,7 @@ static int srw_bend_fetch(association *assoc, int pos,
     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 = (char *) 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";
+        yaz_mk_sru_surrogate(o, record, pos, code, rr.errstring);
         return 0;
     }
     else if (rr.len >= 0)
@@ -767,10 +743,8 @@ static int srw_bend_fetch(association *assoc, int pos,
         record->recordData_buf = rr.record;
         record->recordData_len = rr.len;
         record->recordPosition = odr_intdup(o, pos);
-        if (rr.schema)
-            record->recordSchema = odr_strdup(o, rr.schema);
-        else
-            record->recordSchema = 0;
+        record->recordSchema = odr_strdup_null(
+            o, rr.schema ? rr.schema : srw_req->recordSchema);
     }
     if (rr.errcode)
     {
@@ -793,8 +767,7 @@ static int cql2pqf(ODR odr, const char *cql, cql_transform_t ct,
     r = cql_parser_string(cp, cql);
     if (r)
     {
-        /* CQL syntax error */
-        srw_errcode = 10; 
+        srw_errcode = YAZ_SRW_QUERY_SYNTAX_ERROR;
     }
     if (!r)
     {
@@ -803,7 +776,7 @@ static int cql2pqf(ODR odr, const char *cql, cql_transform_t ct,
                               cql_parser_result(cp),
                               rpn_buf, sizeof(rpn_buf)-1);
         if (r)
-            srw_errcode  = cql_transform_error(ct, &add);
+            srw_errcode = cql_transform_error(ct, &add);
     }
     if (!r)
     {
@@ -818,7 +791,7 @@ static int cql2pqf(ODR odr, const char *cql, cql_transform_t ct,
             int code = yaz_pqf_error(pp, &pqf_msg, &off);
             yaz_log(YLOG_WARN, "PQF Parser Error %s (code %d)",
                     pqf_msg, code);
-            srw_errcode = 10;
+            srw_errcode = YAZ_SRW_QUERY_SYNTAX_ERROR;
         }
         else
         {
@@ -840,14 +813,14 @@ static int cql2pqf_scan(ODR odr, const char *cql, cql_transform_t ct,
     if (srw_error)
         return srw_error;
     if (query.which != Z_Query_type_1 && query.which != Z_Query_type_101)
-        return 10; /* bad query type */
+        return YAZ_SRW_QUERY_SYNTAX_ERROR; /* bad query type */
     rpn = query.u.type_1;
     if (!rpn->RPNStructure) 
-        return 10; /* must be structure */
+        return YAZ_SRW_QUERY_SYNTAX_ERROR; /* must be structure */
     if (rpn->RPNStructure->which != Z_RPNStructure_simple)
-        return 10; /* must be simple */
+        return YAZ_SRW_QUERY_SYNTAX_ERROR; /* must be simple */
     if (rpn->RPNStructure->u.simple->which != Z_Operand_APT)
-        return 10; /* must be attributes plus term node .. */
+        return YAZ_SRW_QUERY_SYNTAX_ERROR; /* must be be attributes + term */
     memcpy(result, rpn->RPNStructure->u.simple->u.attributesPlusTerm,
            sizeof(*result));
     return 0;
@@ -863,7 +836,7 @@ static int ccl2pqf(ODR odr, const Odr_oct *ccl, CCL_bibset bibset,
     ccl0 = odr_strdupn(odr, (char*) ccl->buf, ccl->len);
     if ((node = ccl_find_str(bibset, ccl0, &errcode, &pos)) == 0) {
         bsrr->errstring = (char*) ccl_err_msg(errcode);
-        return 10;              /* Query syntax error */
+        return YAZ_SRW_QUERY_SYNTAX_ERROR;    /* Query syntax error */
     }
 
     bsrr->query->which = Z_Query_type_1;
@@ -1801,8 +1774,8 @@ static void process_http_request(association *assoc, request *req)
         if (sr->which == Z_SRW_searchRetrieve_request)
         {
             Z_SRW_PDU *res =
-                yaz_srw_get(assoc->encode, Z_SRW_searchRetrieve_response);
-
+                yaz_srw_get_pdu(assoc->encode, Z_SRW_searchRetrieve_response,
+                                sr->srw_version);
             stylesheet = sr->u.request->stylesheet;
             if (num_diagnostic)
             {
@@ -1819,7 +1792,8 @@ static void process_http_request(association *assoc, request *req)
         }
         else if (sr->which == Z_SRW_explain_request)
         {
-            Z_SRW_PDU *res = yaz_srw_get(o, Z_SRW_explain_response);
+            Z_SRW_PDU *res = yaz_srw_get_pdu(o, Z_SRW_explain_response,
+                                             sr->srw_version);
             stylesheet = sr->u.explain_request->stylesheet;
             if (num_diagnostic)
             {   
@@ -1833,7 +1807,8 @@ static void process_http_request(association *assoc, request *req)
         }
         else if (sr->which == Z_SRW_scan_request)
         {
-            Z_SRW_PDU *res = yaz_srw_get(o, Z_SRW_scan_response);
+            Z_SRW_PDU *res = yaz_srw_get_pdu(o, Z_SRW_scan_response,
+                                             sr->srw_version);
             stylesheet = sr->u.scan_request->stylesheet;
             if (num_diagnostic)
             {   
@@ -1847,7 +1822,8 @@ static void process_http_request(association *assoc, request *req)
         }
         else if (sr->which == Z_SRW_update_request)
         {
-            Z_SRW_PDU *res = yaz_srw_get(o, Z_SRW_update_response);
+            Z_SRW_PDU *res = yaz_srw_get_pdu(o, Z_SRW_update_response,
+                                             sr->srw_version);
             yaz_log(YLOG_DEBUG, "handling SRW UpdateRequest");
             if (num_diagnostic)
             {   
@@ -2377,7 +2353,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.125 $");
+    version = odr_strdup(assoc->encode, "$Revision: 1.128 $");
     if (strlen(version) > 10)   /* check for unexpanded CVS strings */
         version[strlen(version)-2] = '\0';
     resp->implementationVersion = odr_prepend(assoc->encode,
@@ -2544,11 +2520,9 @@ static Z_Records *pack_records(association *a, char *setname, int start,
 
         *next = freq.last_in_set ? 0 : recno + 1;
 
-        /* backend should be able to signal whether error is system-wide
-           or only pertaining to current record */
         if (freq.errcode)
         {
-            if (!freq.surrogate_flag)
+            if (!freq.surrogate_flag) /* non-surrogate diagnostic i.e. global */
             {
                 char s[20];
                 *pres = Z_PresentStatus_failure;
@@ -2620,10 +2594,7 @@ static Z_Records *pack_records(association *a, char *setname, int start,
         if (!(thisrec = (Z_NamePlusRecord *)
               odr_malloc(a->encode, sizeof(*thisrec))))
             return 0;
-        if (freq.basename)
-            thisrec->databaseName = odr_strdup(a->encode, freq.basename);
-        else
-            thisrec->databaseName = 0;
+        thisrec->databaseName = odr_strdup_null(a->encode, freq.basename);
         thisrec->which = Z_NamePlusRecord_databaseRecord;
 
         if (!freq.output_format)
@@ -3051,7 +3022,9 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
     /* For YAZ 2.0 and earlier it was the backend handler that
        initialized entries (member display_term did not exist)
        YAZ 2.0 and later sets 'entries'  and initialize all members
-       including 'display_term'.
+       including 'display_term'. If YAZ 2.0 or later sees that
+       entries was modified - we assume that it is an old handler and
+       that 'display_term' is _not_ set.
     */
     if (bsrr->num_entries > 0) 
     {
@@ -3110,8 +3083,13 @@ static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
                     odr_malloc(assoc->encode, sizeof(*t));
                 t->suggestedAttributes = 0;
                 t->displayTerm = 0;
-                if (bsrr->entries[i].display_term)
+                if (save_entries == bsrr->entries && 
+                    bsrr->entries[i].display_term)
                 {
+                    /* the entries was _not_ set by the handler. So it's
+                       safe to test for new member display_term. It is
+                       NULL'ed by us.
+                    */
                     t->displayTerm = odr_strdup(assoc->encode,
                                                 bsrr->entries[i].display_term);
                 }