Happy new year
[yaz-moved-to-github.git] / src / soap.c
index 5bbc9cc..5e58a49 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
- *
- * $Id: soap.c,v 1.12 2005-08-22 20:34:21 adam Exp $
  */
 /**
  * \file soap.c
  * This implements encoding and decoding of SOAP packages using
  * Libxml2.
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <yaz/soap.h>
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
@@ -45,8 +46,45 @@ int z_soap_codec_enc_xsl(ODR o, Z_SOAP **pp,
             return z_soap_error(o, p, "SOAP-ENV:Client",
                                 "Bad XML Document", 0);
 
-        /* check that root node is Envelope */
         ptr = xmlDocGetRootElement(doc);
+        if (!ptr || !ptr->ns)
+        {
+            xmlFreeDoc(doc);
+            return z_soap_error(o, p, "SOAP-ENV:Client",
+                                "No Envelope element", 0);
+        }
+        /* check for SRU root node match */
+        
+        for (i = 0; handlers[i].ns; i++)
+            if (!xmlStrcmp(ptr->ns->href, BAD_CAST handlers[i].ns))
+                break;
+        if (handlers[i].ns)
+        {
+            void *handler_data = 0;
+            xmlNode p_top_tmp; /* pseudo parent node needed */
+
+            p_top_tmp.children = ptr;
+            ret = (*handlers[i].f)(o, &p_top_tmp, &handler_data,
+                                   handlers[i].client_data,
+                                   handlers[i].ns);
+            
+            if (ret || !handler_data)
+                z_soap_error(o, p, "SOAP-ENV:Client",
+                             "SOAP Handler returned error", 0);
+            else
+            {
+                p->which = Z_SOAP_generic;
+                p->u.generic = (Z_SOAP_Generic *)
+                    odr_malloc(o, sizeof(*p->u.generic));
+                p->u.generic->no = i;
+                p->u.generic->ns = handlers[i].ns;
+                p->u.generic->p = handler_data;
+            }
+            xmlFreeDoc(doc);
+            return ret;
+        }
+        /* OK: assume SOAP */
+
         if (!ptr || ptr->type != XML_ELEMENT_NODE ||
             xmlStrcmp(ptr->name, BAD_CAST "Envelope") || !ptr->ns)
         {
@@ -227,7 +265,7 @@ int z_soap_codec_enc_xsl(ODR o, Z_SOAP **pp,
         }
         if (stylesheet)
         {
-            char *content = odr_malloc(o, strlen(stylesheet) + 40);
+            char *content = (char *) odr_malloc(o, strlen(stylesheet) + 40);
             
             xmlNodePtr pi, ptr = xmlDocGetRootElement(doc);
             sprintf(content, "type=\"text/xsl\" href=\"%s\"", stylesheet);
@@ -314,6 +352,7 @@ int z_soap_error(ODR o, Z_SOAP *p,
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab