Link SSL with libyaz.la and yaz-client only.
[yaz-moved-to-github.git] / src / seshigh.c
index c10ae91..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.123 2007-07-27 08:05:52 adam Exp $
  */
 /**
  * \file seshigh.c
@@ -76,6 +74,7 @@
 
 #include <yaz/srw.h>
 #include <yaz/backend.h>
+#include <yaz/yaz-ccl.h>
 
 static void process_gdu_request(association *assoc, request *req);
 static int process_z_request(association *assoc, request *req, char **msg);
@@ -736,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)
@@ -766,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)
     {
@@ -792,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)
     {
@@ -802,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)
     {
@@ -817,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
         {
@@ -839,19 +813,38 @@ 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;
 }
                    
+
+static int ccl2pqf(ODR odr, const Odr_oct *ccl, CCL_bibset bibset,
+                   bend_search_rr *bsrr) {
+    char *ccl0;
+    struct ccl_rpn_node *node;
+    int errcode, pos;
+
+    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 YAZ_SRW_QUERY_SYNTAX_ERROR;    /* Query syntax error */
+    }
+
+    bsrr->query->which = Z_Query_type_1;
+    bsrr->query->u.type_1 = ccl_rpn_query(odr, node);
+    return 0;
+}
+
+
 static void srw_bend_search(association *assoc, request *req,
                             Z_SRW_PDU *sr,
                             Z_SRW_searchRetrieveResponse *srw_res,
@@ -1781,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)
             {
@@ -1799,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)
             {   
@@ -1813,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)
             {   
@@ -1827,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)
             {   
@@ -2357,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.123 $");
+    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,
@@ -2524,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;
@@ -2600,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)
@@ -2675,6 +2666,18 @@ static Z_APDU *process_searchRequest(association *assoc, request *reqb,
             if (srw_errcode)
                 bsrr->errcode = yaz_diag_srw_to_bib1(srw_errcode);
         }
+
+        if (assoc->server && assoc->server->ccl_transform 
+            && req->query->which == Z_Query_type_2) /*CCL*/
+        {
+            /* have a CCL query and a CCL to PQF transform .. */
+            int srw_errcode = 
+                ccl2pqf(bsrr->stream, req->query->u.type_2,
+                        assoc->server->ccl_transform, bsrr);
+            if (srw_errcode)
+                bsrr->errcode = yaz_diag_srw_to_bib1(srw_errcode);
+        }
+
         if (!bsrr->errcode)
             (assoc->init->bend_search)(assoc->backend, bsrr);
         if (!bsrr->request)  /* backend not ready with the search response */