Fix yaz_use_attribute_create (uninit memory)
[yaz-moved-to-github.git] / src / srwutil.c
index 2591fc9..493a7a3 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2013 Index Data
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -39,30 +39,37 @@ char *yaz_encode_sru_dbpath_odr(ODR out, const char *db)
     return dst;
 }
 
-Z_AttributeList *yaz_use_attribute_create(ODR o, const char *name)
+Z_AttributeElement *yaz_string_element_create(ODR o, int type,
+                                              const char *value)
 {
-    Z_AttributeList *attributes= (Z_AttributeList *)
-        odr_malloc(o, sizeof(*attributes));
-    Z_AttributeElement ** elements;
-    attributes->num_attributes = 1;
-    elements = (Z_AttributeElement**)
-        odr_malloc(o, attributes->num_attributes * sizeof(*elements));
-    elements[0] = (Z_AttributeElement*) odr_malloc(o,sizeof(**elements));
-    elements[0]->attributeType = odr_intdup(o, 1);
-    elements[0]->attributeSet = odr_nullval();
-    elements[0]->which = Z_AttributeValue_complex;
-    elements[0]->value.complex = (Z_ComplexAttribute *)
+    Z_AttributeElement *element = (Z_AttributeElement*)
+        odr_malloc(o, sizeof(*element));
+    element->attributeType = odr_intdup(o, type);
+    element->attributeSet = 0;
+    element->which = Z_AttributeValue_complex;
+    element->value.complex = (Z_ComplexAttribute *)
         odr_malloc(o, sizeof(Z_ComplexAttribute));
-    elements[0]->value.complex->num_list = 1;
-    elements[0]->value.complex->list = (Z_StringOrNumeric **)
+    element->value.complex->num_list = 1;
+    element->value.complex->list = (Z_StringOrNumeric **)
         odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *));
-    elements[0]->value.complex->list[0] = (Z_StringOrNumeric *)
+    element->value.complex->list[0] = (Z_StringOrNumeric *)
         odr_malloc(o, sizeof(Z_StringOrNumeric));
-    elements[0]->value.complex->list[0]->which = Z_StringOrNumeric_string;
-    elements[0]->value.complex->list[0]->u.string = odr_strdup(o, name);
-    elements[0]->value.complex->semanticAction = 0;
-    elements[0]->value.complex->num_semanticAction = 0;
-    attributes->attributes = elements;
+    element->value.complex->list[0]->which = Z_StringOrNumeric_string;
+    element->value.complex->list[0]->u.string = odr_strdup(o, value);
+    element->value.complex->semanticAction = 0;
+    element->value.complex->num_semanticAction = 0;
+    return element;
+}
+
+Z_AttributeList *yaz_use_attribute_create(ODR o, const char *name)
+{
+    Z_AttributeList *attributes = (Z_AttributeList *)
+        odr_malloc(o, sizeof(*attributes));
+
+    attributes->num_attributes = 1;
+    attributes->attributes = (Z_AttributeElement**)
+        odr_malloc(o, sizeof(*attributes->attributes));
+    attributes->attributes[0] = yaz_string_element_create(o, 1, name);
     return attributes;
 }
 
@@ -141,13 +148,6 @@ static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq,
     }
 }
 
-void yaz_uri_val_int(const char *path, const char *name, ODR o, Odr_int **intp)
-{
-    const char *v = yaz_uri_val(path, name, o);
-    if (v)
-        *intp = odr_intdup(o, atoi(v));
-}
-
 void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d,
                            const char *uri, const char *message,
                            const char *details)
@@ -241,14 +241,21 @@ static void grab_charset(ODR o, const char *content_type, char **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 j = 0, i = 0;
+            int sep = 0;
+            charset_p += 10; /* skip ; charset=  */
+            if (charset_p[i] == '"' || charset_p[i] == '\'')
+                sep = charset_p[i++];
+            *charset = odr_strdup(o, charset_p);
+            while (charset_p[i] && charset_p[i] != sep)
+            {
+                if (!sep && strchr("; \n\r", charset_p[i]))
+                    break;
+                if (charset_p[i] == '\\' && charset_p[i+1])
+                    i++;
+                (*charset)[j++] = charset_p[i++];
+            }
+            (*charset)[j] = '\0';
         }
     }
 }
@@ -286,7 +293,6 @@ int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
                 p1 = p0 + strlen(p0);
             if (p1 != p0)
                 db = yaz_decode_sru_dbpath_odr(decode, p0, p1 - p0);
-            grab_charset(decode, content_type, charset);
 
             ret = z_soap_codec(decode, soap_package,
                                &hreq->content_buf, &hreq->content_len,
@@ -1053,6 +1059,7 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
     char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
     char *uri_args;
     char *path;
+    char *cp;
 
     z_HTTP_header_add_basic_auth(encode, &hreq->headers,
                                  srw_pdu->username, srw_pdu->password);
@@ -1062,10 +1069,15 @@ int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
 
     hreq->method = "GET";
 
+    cp = strchr(hreq->path, '#');
+    if (cp)
+        *cp = '\0';
+
     path = (char *)
         odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4);
 
-    sprintf(path, "%s?%s", hreq->path, uri_args);
+    sprintf(path, "%s%c%s", hreq->path, strchr(hreq->path, '?') ? '&' : '?', 
+            uri_args);
     hreq->path = path;
 
     z_HTTP_header_add_content_type(encode, &hreq->headers,
@@ -1113,7 +1125,7 @@ int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
                                  srw_pdu->username, srw_pdu->password);
     z_HTTP_header_add_content_type(odr,
                                    &hreq->headers,
-                                   "text/xml", charset);
+                                   "text/xml", 0 /* no charset in MIME */);
 
     z_HTTP_header_add(odr, &hreq->headers,
                       "SOAPAction", "\"\"");
@@ -1182,6 +1194,9 @@ void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args)
         Z_SRW_extra_arg **ea = &sr->extra_args;
         yaz_uri_to_array(extra_args, odr, &name, &val);
 
+        /** append rather than override */
+        while (*ea)
+            ea = &(*ea)->next;
         while (*name)
         {
             *ea = (Z_SRW_extra_arg *) odr_malloc(odr, sizeof(**ea));