Ignore generated diagsru_update.h
[yaz-moved-to-github.git] / src / srwutil.c
index 71d6f94..560976b 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: srwutil.c,v 1.48 2006-09-06 13:15:49 adam Exp $
+ * $Id: srwutil.c,v 1.53 2006-10-27 11:22:09 adam Exp $
  */
 /**
  * \file srwutil.c
@@ -80,7 +80,7 @@ static void yaz_array_to_uri_ex(char **path, ODR o, char **name, char **value,
 
 void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
 {
-    return yaz_array_to_uri_ex(path, o, name, value, 0);
+    yaz_array_to_uri_ex(path, o, name, value, 0);
 }
 
 int yaz_uri_array(const char *path, ODR o, char ***name, char ***val)
@@ -328,6 +328,36 @@ void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d,
     yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo);
 }
 
+
+void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d,
+                                   int *num, int code, const char *addinfo)
+{
+    char uri[40];
+    
+    sprintf(uri, "info:srw/diagnostic/12/%d", code);
+    yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo);
+}
+
+
+static void grab_charset(ODR o, const char *content_type, char **charset)
+{
+    if (charset)
+    { 
+        const char *charset_p = 0;
+        if (content_type && (charset_p = strstr(content_type, "; charset=")))
+        {
+            int i = 0;
+            charset_p += 10;
+            while (i < 20 && charset_p[i] &&
+                   !strchr("; \n\r", charset_p[i]))
+                i++;
+            *charset = (char*) odr_malloc(o, i+1);
+            memcpy(*charset, charset_p, i);
+            (*charset)[i] = '\0';
+        }
+    }
+}
+
 int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                    Z_SOAP **soap_package, ODR decode, char **charset)
 {
@@ -343,7 +373,6 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
             char *db = "Default";
             const char *p0 = hreq->path, *p1;
             int ret = -1;
-            const char *charset_p = 0;
             
             static Z_SOAP_Handler soap_handlers[4] = {
 #if YAZ_HAVE_XML2
@@ -369,17 +398,8 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 db[p1 - p0] = '\0';
             }
 
-            if (charset && (charset_p = strstr(content_type, "; charset=")))
-            {
-                int i = 0;
-                charset_p += 10;
-                while (i < 20 && charset_p[i] &&
-                       !strchr("; \n\r", charset_p[i]))
-                    i++;
-                *charset = (char*) odr_malloc(decode, i+1);
-                memcpy(*charset, charset_p, i);
-                (*charset)[i] = '\0';
-            }
+            grab_charset(decode, content_type, charset);
+
             ret = z_soap_codec(decode, soap_package, 
                                &hreq->content_buf, &hreq->content_len,
                                soap_handlers);
@@ -411,10 +431,10 @@ 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, 
+static int yaz_sru_decode_integer(ODR odr, const char *pname, 
                                   const char *valstr, int **valp,
-                                  Z_SRW_diagnostic **diag, int *num_diag)
-
+                                  Z_SRW_diagnostic **diag, int *num_diag,
+                                  int min_value)
 {
     int ival;
     if (!valstr)
@@ -425,9 +445,16 @@ static int yaz_sru_integer_decode(ODR odr, const char *pname,
                                YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
         return 0;
     }
+    if (min_value >= 0 && ival < min_value)
+    {
+        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
 */ 
@@ -443,20 +470,18 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
     };
 #endif
     const char *content_type = z_HTTP_header_lookup(hreq->headers,
-                                                    "Content-Type");
+                                            "Content-Type");
+
     /*
-      SRU GET: allow any content type.
+      SRU GET: ignore content type.
       SRU POST: we support "application/x-www-form-urlencoded";
       not  "multipart/form-data" .
     */
-    if (!strcmp(hreq->method, "GET") 
-        ||
-        (!strcmp(hreq->method, "POST") 
-         && content_type &&
-         !yaz_strcmp_del("application/x-www-form-urlencoded",
-                         content_type, "; ")
-            )
-        )
+    if (!strcmp(hreq->method, "GET")
+        || 
+             (!strcmp(hreq->method, "POST") && content_type &&
+              !yaz_strcmp_del("application/x-www-form-urlencoded",
+                              content_type, "; ")))
     {
         char *db = "Default";
         const char *p0 = hreq->path, *p1;
@@ -464,11 +489,13 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
         const char *operation = 0;
         char *version = 0;
         char *query = 0;
+        char *pQuery = 0;
         char *username = 0;
         char *password = 0;
         char *sortKeys = 0;
         char *stylesheet = 0;
         char *scanClause = 0;
+        char *pScanClause = 0;
         char *recordXPath = 0;
         char *recordSchema = 0;
         char *recordPacking = "xml";  /* xml packing is default for SRU */
@@ -480,10 +507,11 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
 #endif
         char **uri_name;
         char **uri_val;
-        int querytype = Z_SRW_query_type_cql;
 
-        if (charset)
-            *charset = 0;
+        grab_charset(decode, content_type, charset);
+        if (charset && *charset == 0 && !strcmp(hreq->method, "GET"))
+            *charset = "UTF-8";
+
         if (*p0 == '/')
             p0++;
         p1 = strchr(p0, '?');
@@ -508,24 +536,8 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 char *v = uri_val[i];
                 if (!strcmp(n, "query"))
                     query = v;
-                else if (!strcmp(n, "x-pquery")) /* deprecated */
-                {
-                    query = v;
-                    querytype = Z_SRW_query_type_pqf;
-                }
-                else if (!strcmp(n, "x-querytype"))
-                {
-                    if (!strcmp(v, "cql"))
-                        querytype = Z_SRW_query_type_cql;
-                    else if (!strcmp(v, "pqf"))
-                        querytype = Z_SRW_query_type_pqf;
-                    else
-                    {
-                        yaz_add_srw_diagnostic(
-                            decode, diag, num_diag,
-                            YAZ_SRW_UNSUPP_PARAMETER_VALUE, v);
-                    }
-                }
+                else if (!strcmp(n, "x-pquery"))
+                    pQuery = v;
                 else if (!strcmp(n, "x-username"))
                     username = v;
                 else if (!strcmp(n, "x-password"))
@@ -547,10 +559,7 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 else if (!strcmp(n, "scanClause"))
                     scanClause = v;
                 else if (!strcmp(n, "x-pScanClause"))
-                {
-                    querytype = Z_SRW_query_type_pqf;
-                    scanClause = v;
-                }
+                    pScanClause = v;
                 else if (!strcmp(n, "maximumRecords"))
                     maximumRecords = v;
                 else if (!strcmp(n, "startRecord"))
@@ -594,9 +603,14 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
             yaz_srw_decodeauth(sr, hreq, username, password, decode);
             if (query)
             {
-                sr->u.request->query_type = querytype;
+                sr->u.request->query_type = Z_SRW_query_type_cql;
                 sr->u.request->query.cql = query;
             }
+            else if (pQuery)
+            {
+                sr->u.request->query_type = Z_SRW_query_type_pqf;
+                sr->u.request->query.pqf = pQuery;
+            }
             else
                 yaz_add_srw_diagnostic(
                     decode, diag, num_diag, 
@@ -612,13 +626,13 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
             sr->u.request->recordPacking = recordPacking;
             sr->u.request->stylesheet = stylesheet;
 
-            yaz_sru_integer_decode(decode, "maximumRecords", maximumRecords, 
+            yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords, 
                                    &sr->u.request->maximumRecords, 
-                                   diag, num_diag);
-
-            yaz_sru_integer_decode(decode, "startRecord", startRecord, 
+                                   diag, num_diag, 0);
+            
+            yaz_sru_decode_integer(decode, "startRecord", startRecord, 
                                    &sr->u.request->startRecord,
-                                   diag, num_diag);
+                                   diag, num_diag, 1);
 
             sr->u.request->database = db;
 
@@ -676,24 +690,29 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
 
             if (scanClause)
             {
-                sr->u.scan_request->query_type = querytype;
+                sr->u.scan_request->query_type = Z_SRW_query_type_cql;
                 sr->u.scan_request->scanClause.cql = scanClause;
             }
+            else if (pScanClause)
+            {
+                sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
+                sr->u.scan_request->scanClause.pqf = pScanClause;
+            }
             else
                 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",
+            yaz_sru_decode_integer(decode, "maximumTerms",
                                    maximumTerms, 
                                    &sr->u.scan_request->maximumTerms,
-                                   diag, num_diag);
-
-            yaz_sru_integer_decode(decode, "responsePosition",
+                                   diag, num_diag, 0);
+            
+            yaz_sru_decode_integer(decode, "responsePosition",
                                    responsePosition, 
                                    &sr->u.scan_request->responsePosition,
-                                   diag, num_diag);
+                                   diag, num_diag, 0);
 
             sr->u.scan_request->stylesheet = stylesheet;
 
@@ -1114,14 +1133,12 @@ static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
                         srw_pdu->u.request->query.cql);
             break;
         case Z_SRW_query_type_pqf:
-            add_val_str(encode, name, value, &i, "query",
+            add_val_str(encode, name, value, &i, "x-pquery",
                         srw_pdu->u.request->query.pqf);
-            add_val_str(encode, name, value, &i, "x-querytype", "pqf");
             break;
         case Z_SRW_query_type_xcql:
-            add_val_str(encode, name, value, &i, "query",
+            add_val_str(encode, name, value, &i, "x-cql",
                         srw_pdu->u.request->query.xcql);
-            add_val_str(encode, name, value, &i, "x-querytype", "xcql");
             break;
         }
         switch(srw_pdu->u.request->sort_type)
@@ -1163,14 +1180,12 @@ static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
                         srw_pdu->u.scan_request->scanClause.cql);
             break;
         case Z_SRW_query_type_pqf:
-            add_val_str(encode, name, value, &i, "scanClause",
+            add_val_str(encode, name, value, &i, "x-pScanClause",
                         srw_pdu->u.scan_request->scanClause.pqf);
-            add_val_str(encode, name, value, &i, "x-querytype", "pqf");
             break;
         case Z_SRW_query_type_xcql:
-            add_val_str(encode, name, value, &i, "scanClause",
+            add_val_str(encode, name, value, &i, "x-cqlScanClause",
                         srw_pdu->u.scan_request->scanClause.xcql);
-            add_val_str(encode, name, value, &i, "x-querytype", "xcql");
             break;
         }
         add_val_int(encode, name, value, &i, "responsePosition",