Changed the way PQF queries are encoded in SRU GET/POST. PQF search
[yaz-moved-to-github.git] / src / srwutil.c
index 841b571..71d6f94 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 1995-2005, Index Data ApS
+ * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: srwutil.c,v 1.44 2006-06-14 05:47:10 adam Exp $
+ * $Id: srwutil.c,v 1.48 2006-09-06 13:15:49 adam Exp $
  */
 /**
  * \file srwutil.c
@@ -41,9 +41,10 @@ void encode_uri_char(char *dst, char ch)
     }
 }
 
-void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
+static void yaz_array_to_uri_ex(char **path, ODR o, char **name, char **value,
+                                const char *extra_args)
 {
-    size_t i, szp = 0, sz = 0;
+    size_t i, szp = 0, sz = extra_args ? 1+strlen(extra_args) : 1;
     for(i = 0; name[i]; i++)
         sz += strlen(name[i]) + 3 + strlen(value[i]) * 3;
     *path = odr_malloc(o, sz);
@@ -67,9 +68,21 @@ void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
             szp += vlen;
         }
     }
+    if (extra_args)
+    {
+        if (i)
+            (*path)[szp++] = '&';
+        memcpy(*path + szp, extra_args, strlen(extra_args));
+        szp += strlen(extra_args);
+    }
     (*path)[szp] = '\0';
 }
 
+void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
+{
+    return yaz_array_to_uri_ex(path, o, name, value, 0);
+}
+
 int yaz_uri_array(const char *path, ODR o, char ***name, char ***val)
 {
     int no = 2;
@@ -226,8 +239,8 @@ static int yaz_base64decode(const char *in, char *out)
  * Look for authentication tokens in HTTP Basic parameters or in x-username/x-password
  * parameters. Added by SH.
  */
-static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq, char *username,
-        char *password, ODR decode)
+static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq,
+                               char *username, char *password, ODR decode)
 {
     const char *basic = z_HTTP_header_lookup(hreq->headers, "Authorization");
 
@@ -333,7 +346,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,
@@ -422,7 +435,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},
@@ -447,17 +460,15 @@ 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;
-        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 */
@@ -469,6 +480,7 @@ 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;
@@ -486,7 +498,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;
@@ -496,8 +508,24 @@ 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"))
-                    pQuery = 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-username"))
                     username = v;
                 else if (!strcmp(n, "x-password"))
@@ -519,7 +547,10 @@ 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"))
-                    pScanClause = v;
+                {
+                    querytype = Z_SRW_query_type_pqf;
+                    scanClause = v;
+                }
                 else if (!strcmp(n, "maximumRecords"))
                     maximumRecords = v;
                 else if (!strcmp(n, "startRecord"))
@@ -563,14 +594,9 @@ 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 = Z_SRW_query_type_cql;
+                sr->u.request->query_type = querytype;
                 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, 
@@ -650,14 +676,9 @@ int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
 
             if (scanClause)
             {
-                sr->u.scan_request->query_type = Z_SRW_query_type_cql;
+                sr->u.scan_request->query_type = querytype;
                 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, 
@@ -740,13 +761,19 @@ Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o)
     return res;
 }
 
-Z_SRW_PDU *yaz_srw_get(ODR o, int which)
+Z_SRW_PDU *yaz_srw_get_core_v_1_1(ODR o)
 {
-    Z_SRW_PDU *sr = (Z_SRW_PDU *) odr_malloc(o, sizeof(*o));
+    Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
+    p->srw_version = odr_strdup(o, "1.1");
+    p->username = 0;
+    p->password = 0;
+    p->extra_args = 0;
+    return p;
+}
 
-    sr->username = 0;
-    sr->password = 0;
-    sr->srw_version = odr_strdup(o, "1.1");
+Z_SRW_PDU *yaz_srw_get(ODR o, int which)
+{
+    Z_SRW_PDU *sr = yaz_srw_get_core_v_1_1(o);
     sr->which = which;
     switch(which)
     {
@@ -1087,12 +1114,14 @@ 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, "x-pquery",
+            add_val_str(encode, name, value, &i, "query",
                         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, "x-cql",
+            add_val_str(encode, name, value, &i, "query",
                         srw_pdu->u.request->query.xcql);
+            add_val_str(encode, name, value, &i, "x-querytype", "xcql");
             break;
         }
         switch(srw_pdu->u.request->sort_type)
@@ -1134,12 +1163,14 @@ 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, "x-pScanClause",
+            add_val_str(encode, name, value, &i, "scanClause",
                         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, "x-cqlScanClause",
+            add_val_str(encode, name, value, &i, "scanClause",
                         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", 
@@ -1168,11 +1199,13 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
 
     if (yaz_get_sru_parms(srw_pdu, encode, name, value))
         return -1;
-    yaz_array_to_uri(&uri_args, encode, name, value);
+    yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args);
 
     hreq->method = "GET";
     
-    path = odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 3);
+    path = odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4
+                      +(srw_pdu->extra_args ? strlen(srw_pdu->extra_args) : 0)
+        );
     sprintf(path, "%s?%s", hreq->path, uri_args);
     hreq->path = path;
 
@@ -1190,7 +1223,7 @@ int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
     if (yaz_get_sru_parms(srw_pdu, encode, name, value))
         return -1;
 
-    yaz_array_to_uri(&uri_args, encode, name, value);
+    yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args);
 
     hreq->method = "POST";
     
@@ -1207,7 +1240,7 @@ 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 HAVE_XML2
+#if YAZ_HAVE_XML2
         {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
 #endif
         {0, 0, 0}