For Libxml2 and friends, YAZ defines YAZ_HAVE_{XML2,XSLT,EXSLT) in
[yaz-moved-to-github.git] / src / srwutil.c
index 9a54e23..9e698c6 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: srwutil.c,v 1.40 2006-05-07 18:35:47 adam Exp $
+ * $Id: srwutil.c,v 1.45 2006-07-06 10:17:54 adam Exp $
  */
 /**
  * \file srwutil.c
@@ -333,7 +333,7 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
             const char *charset_p = 0;
             
             static Z_SOAP_Handler soap_handlers[4] = {
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
                 {"http://www.loc.gov/zing/srw/", 0,
                  (Z_SOAP_fun) yaz_srw_codec},
                 {"http://www.loc.gov/zing/srw/v1.0/", 0,
@@ -398,6 +398,23 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
     return 2;
 }
 
+static int yaz_sru_integer_decode(ODR odr, const char *pname, 
+                                  const char *valstr, int **valp,
+                                  Z_SRW_diagnostic **diag, int *num_diag)
+
+{
+    int ival;
+    if (!valstr)
+        return 0;
+    if (sscanf(valstr, "%d", &ival) != 1)
+    {
+        yaz_add_srw_diagnostic(odr, diag, num_diag,
+                               YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
+        return 0;
+    }
+    *valp = odr_intdup(odr, ival);
+    return 1;
+}
 /**
   http://www.loc.gov/z3950/agency/zing/srw/service.html
 */ 
@@ -405,7 +422,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                    Z_SOAP **soap_package, ODR decode, char **charset,
                    Z_SRW_diagnostic **diag, int *num_diag)
 {
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
     static Z_SOAP_Handler soap_handlers[2] = {
         {"http://www.loc.gov/zing/srw/", 0,
          (Z_SOAP_fun) yaz_srw_codec},
@@ -430,7 +447,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
     {
         char *db = "Default";
         const char *p0 = hreq->path, *p1;
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
         const char *operation = 0;
         char *version = 0;
         char *query = 0;
@@ -469,7 +486,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
         if (!strcmp(hreq->method, "POST"))
             p1 = hreq->content_buf;
         yaz_uri_array(p1, decode, &uri_name, &uri_val);
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
         if (uri_name)
         {
             int i;
@@ -514,21 +531,27 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 else if (!strcmp(n, "extraRequestData"))
                     extraRequestData = v;
                 else
-                    yaz_add_srw_diagnostic(decode, diag, num_diag, 8, n);
+                    yaz_add_srw_diagnostic(decode, diag, num_diag,
+                                           YAZ_SRW_UNSUPP_PARAMETER, n);
             }
         }
         if (!version)
         {
             if (uri_name)
-                yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "version");
+                yaz_add_srw_diagnostic(
+                    decode, diag, num_diag,
+                    YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "version");
             version = "1.1";
         }
         if (strcmp(version, "1.1"))
-            yaz_add_srw_diagnostic(decode, diag, num_diag, 5, "1.1");
+            yaz_add_srw_diagnostic(decode, diag, num_diag,
+                                   YAZ_SRW_UNSUPP_VERSION, "1.1");
         if (!operation)
         {
             if (uri_name)
-                yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "operation");
+                yaz_add_srw_diagnostic(
+                    decode, diag, num_diag, 
+                    YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "operation");
             operation = "explain";
         }
         if (!strcmp(operation, "searchRetrieve"))
@@ -549,7 +572,9 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 sr->u.request->query.pqf = pQuery;
             }
             else
-                yaz_add_srw_diagnostic(decode, diag, num_diag, 7, "query");
+                yaz_add_srw_diagnostic(
+                    decode, diag, num_diag, 
+                    YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "query");
 
             if (sortKeys)
             {
@@ -561,12 +586,13 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
             sr->u.request->recordPacking = recordPacking;
             sr->u.request->stylesheet = stylesheet;
 
-            if (maximumRecords)
-                sr->u.request->maximumRecords =
-                    odr_intdup(decode, atoi(maximumRecords));
-            if (startRecord)
-                sr->u.request->startRecord =
-                    odr_intdup(decode, atoi(startRecord));
+            yaz_sru_integer_decode(decode, "maximumRecords", maximumRecords, 
+                                   &sr->u.request->maximumRecords, 
+                                   diag, num_diag);
+
+            yaz_sru_integer_decode(decode, "startRecord", startRecord, 
+                                   &sr->u.request->startRecord,
+                                   diag, num_diag);
 
             sr->u.request->database = db;
 
@@ -633,16 +659,20 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 sr->u.scan_request->scanClause.pqf = pScanClause;
             }
             else
-                yaz_add_srw_diagnostic(decode, diag, num_diag, 7,
-                                       "scanClause");
+                yaz_add_srw_diagnostic(
+                    decode, diag, num_diag, 
+                    YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "scanClause");
             sr->u.scan_request->database = db;
+            
+            yaz_sru_integer_decode(decode, "maximumTerms",
+                                   maximumTerms, 
+                                   &sr->u.scan_request->maximumTerms,
+                                   diag, num_diag);
 
-            if (maximumTerms)
-                sr->u.scan_request->maximumTerms =
-                    odr_intdup(decode, atoi(maximumTerms));
-            if (responsePosition)
-                sr->u.scan_request->responsePosition =
-                    odr_intdup(decode, atoi(responsePosition));
+            yaz_sru_integer_decode(decode, "responsePosition",
+                                   responsePosition, 
+                                   &sr->u.scan_request->responsePosition,
+                                   diag, num_diag);
 
             sr->u.scan_request->stylesheet = stylesheet;
 
@@ -686,7 +716,8 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
             
             (*soap_package)->ns = "SRU";
 
-            yaz_add_srw_diagnostic(decode, diag, num_diag, 4, operation);
+            yaz_add_srw_diagnostic(decode, diag, num_diag, 
+                                   YAZ_SRW_UNSUPP_OPERATION, operation);
             return 0;
         }
 #endif
@@ -1129,7 +1160,7 @@ static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
 }
 
 int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
-                       ODR encode, char *charset)
+                       ODR encode, const char *charset)
 {
     char *name[30], *value[30]; /* definite upper limit for SRU params */
     char *uri_args;
@@ -1151,7 +1182,7 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
 }
 
 int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
-                        ODR encode, char *charset)
+                        ODR encode, const char *charset)
 {
     char *name[30], *value[30]; /* definite upper limit for SRU params */
     char *uri_args;
@@ -1172,6 +1203,35 @@ int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
     return 0;
 }
 
+int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
+                        ODR odr, const char *charset)
+{
+    Z_SOAP_Handler handlers[2] = {
+#if YAZ_HAVE_XML2
+        {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
+#endif
+        {0, 0, 0}
+    };
+    Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p));
+    z_HTTP_header_add_content_type(odr,
+                                   &hreq->headers,
+                                   "text/xml", charset);
+    
+    z_HTTP_header_add(odr, &hreq->headers,
+                      "SOAPAction", "\"\"");
+    p->which = Z_SOAP_generic;
+    p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
+    p->u.generic->no = 0;
+    p->u.generic->ns = 0;
+    p->u.generic->p = srw_pdu;
+    p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
+    
+    return z_soap_codec_enc(odr, &p,
+                            &hreq->content_buf,
+                            &hreq->content_len, handlers,
+                            charset);
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4