64310bc26485d8215b589b90d16502cf94c6cea9
[yaz-moved-to-github.git] / src / soap.c
1 /*
2  * Copyright (c) 2002-2003, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: soap.c,v 1.2 2003-12-18 23:04:23 adam Exp $
6  */
7
8 #include <yaz/soap.h>
9
10 #if HAVE_XML2
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
13
14 static const char *soap_v1_1 = "http://schemas.xmlsoap.org/soap/envelope/";
15 static const char *soap_v1_2 = "http://www.w3.org/2001/06/soap-envelope";
16
17 int z_soap_error(ODR o, Z_SOAP *p,
18                  const char *fault_code, const char *fault_string,
19                  const char *details)
20 {
21     p->which = Z_SOAP_error;
22     p->u.soap_error = (Z_SOAP_Fault *) 
23         odr_malloc(o, sizeof(*p->u.soap_error));
24     p->u.soap_error->fault_code = odr_strdup(o, fault_code);
25     p->u.soap_error->fault_string = odr_strdup(o, fault_string);
26     if (details)
27         p->u.soap_error->details = odr_strdup(o, details);
28     else
29         p->u.soap_error->details = 0;
30     return -1;
31 }
32
33 int z_soap_codec_enc(ODR o, Z_SOAP **pp, 
34                  char **content_buf, int *content_len,
35                  Z_SOAP_Handler *handlers,
36                  const char *encoding)
37 {
38     if (o->direction == ODR_DECODE)
39     {
40         Z_SOAP *p;
41         xmlNodePtr ptr, pptr;
42         xmlDocPtr doc;
43         int i, ret;
44
45         if (!content_buf || !*content_buf || !content_len)
46             return -1;
47
48         *pp = p = (Z_SOAP *) odr_malloc(o, sizeof(*p));
49         p->ns = soap_v1_1;
50
51         doc = xmlParseMemory(*content_buf, *content_len);
52         if (!doc)
53             return z_soap_error(o, p, "SOAP-ENV:Client",
54                                 "Bad XML Document", 0);
55         /* check that root node is Envelope */
56         ptr = xmlDocGetRootElement(doc);
57         if (!ptr || ptr->type != XML_ELEMENT_NODE ||
58             strcmp(ptr->name, "Envelope") || !ptr->ns)
59         {
60             xmlFreeDoc(doc);
61             return z_soap_error(o, p, "SOAP-ENV:Client",
62                                 "No Envelope element", 0);
63         }
64         else
65         {
66             /* determine SOAP version */
67             const char * ns_envelope = ptr->ns->href;
68             if (!strcmp(ns_envelope, soap_v1_1))
69                 p->ns = soap_v1_1;
70             else if (!strcmp(ns_envelope, soap_v1_2))
71                 p->ns = soap_v1_2;
72             else
73             {
74                 xmlFreeDoc(doc);
75                 return z_soap_error(o, p, "SOAP-ENV:Client",
76                                     "Bad SOAP version", 0);
77             }
78         }
79         ptr = ptr->children;
80         while(ptr && ptr->type == XML_TEXT_NODE)
81             ptr = ptr->next;
82         if (ptr && ptr->type == XML_ELEMENT_NODE &&
83             !strcmp(ptr->ns->href, p->ns) &&
84             !strcmp(ptr->name, "Header"))
85         {
86             ptr = ptr->next;
87             while(ptr && ptr->type == XML_TEXT_NODE)
88                 ptr = ptr->next;
89         }
90         /* check that Body is present */
91         if (!ptr || ptr->type != XML_ELEMENT_NODE || 
92             strcmp(ptr->name, "Body"))
93         {
94             xmlFreeDoc(doc);
95             return z_soap_error(o, p, "SOAP-ENV:Client",
96                                 "SOAP Body element not found", 0);
97         }
98         if (strcmp(ptr->ns->href, p->ns))
99         {
100             xmlFreeDoc(doc);
101             return z_soap_error(o, p, "SOAP-ENV:Client",
102                                 "SOAP bad NS for Body element", 0);
103         }
104         pptr = ptr;
105         ptr = ptr->children;
106         while (ptr && ptr->type == XML_TEXT_NODE)
107             ptr = ptr->next;
108         if (!ptr || ptr->type != XML_ELEMENT_NODE)
109         {
110             xmlFreeDoc(doc);
111             return z_soap_error(o, p, "SOAP-ENV:Client",
112                                 "SOAP No content for Body", 0);
113         }
114         /* check for fault package */
115         if (!strcmp(ptr->ns->href, p->ns)
116             && !strcmp(ptr->name, "Fault") && ptr->children)
117         {
118             ptr = ptr->children;
119
120             p->which = Z_SOAP_fault;
121             p->u.fault = odr_malloc(o, sizeof(*p->u.fault));
122             p->u.fault->fault_code = 0;
123             p->u.fault->fault_string = 0;
124             p->u.fault->details = 0;
125             while (ptr)
126             {
127                 if (ptr->children && ptr->children->type == XML_TEXT_NODE)
128                 {
129                     if (!strcmp(ptr->name, "faultcode"))
130                         p->u.fault->fault_code =
131                             odr_strdup(o, ptr->children->content);
132                     if (!strcmp(ptr->name, "faultstring"))
133                         p->u.fault->fault_string =
134                             odr_strdup(o, ptr->children->content);
135                     if (!strcmp(ptr->name, "details"))
136                         p->u.fault->details =
137                             odr_strdup(o, ptr->children->content);
138                 }
139                 ptr = ptr->next;
140             }
141             ret = 0;
142         }
143         else
144         {
145             for (i = 0; handlers[i].ns; i++)
146                 if (!strcmp(ptr->ns->href, handlers[i].ns))
147                     break;
148             if (handlers[i].ns)
149             {
150                 void *handler_data = 0;
151                 ret = (*handlers[i].f)(o, pptr, &handler_data,
152                                        handlers[i].client_data,
153                                        handlers[i].ns);
154                 if (ret || !handler_data)
155                     z_soap_error(o, p, "SOAP-ENV:Client",
156                                  "SOAP Handler returned error", 0);
157                 else
158                 {
159                     p->which = Z_SOAP_generic;
160                     p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
161                     p->u.generic->no = i;
162                     p->u.generic->ns = handlers[i].ns;
163                     p->u.generic->p = handler_data;
164                 }
165             }
166             else
167             {
168                 ret = z_soap_error(o, p, "SOAP-ENV:Client", 
169                                    "No handler for NS", ptr->ns->href);
170             }
171         }
172         xmlFreeDoc(doc);
173         return ret;
174     }
175     else if (o->direction == ODR_ENCODE)
176     {
177         Z_SOAP *p = *pp;
178         xmlNsPtr ns_env;
179         xmlNodePtr envelope_ptr, body_ptr;
180
181         xmlDocPtr doc = xmlNewDoc("1.0");
182
183         envelope_ptr = xmlNewNode(0, "Envelope");
184         ns_env = xmlNewNs(envelope_ptr, p->ns, "SOAP-ENV");
185         xmlSetNs(envelope_ptr, ns_env);
186
187         body_ptr = xmlNewChild(envelope_ptr, ns_env, "Body", 0);
188         xmlDocSetRootElement(doc, envelope_ptr);
189
190         if (p->which == Z_SOAP_fault || p->which == Z_SOAP_error)
191         {
192             Z_SOAP_Fault *f = p->u.fault;
193             xmlNodePtr fault_ptr = xmlNewChild(body_ptr, ns_env, "Fault", 0);
194             xmlNewChild(fault_ptr, ns_env, "faultcode",  f->fault_code);
195             xmlNewChild(fault_ptr, ns_env, "faultstring", f->fault_string);
196             if (f->details)
197                 xmlNewChild(fault_ptr, ns_env, "details", f->details);
198         }
199         else if (p->which == Z_SOAP_generic)
200         {
201             int ret, no = p->u.generic->no;
202             
203             ret = (*handlers[no].f)(o, body_ptr, &p->u.generic->p,
204                                     handlers[no].client_data,
205                                     handlers[no].ns);
206             if (ret)
207             {
208                 xmlFreeNode(envelope_ptr);
209                 xmlFreeDoc(doc);
210                 return ret;
211             }
212         }
213         if (p->which == Z_SOAP_generic && !strcmp(p->ns, "SRU"))
214         {
215             xmlDocSetRootElement(doc, body_ptr->children);
216         }
217         if (1)
218         {
219             xmlChar *buf_out;
220             int len_out;
221             if (encoding)
222                 xmlDocDumpMemoryEnc(doc, &buf_out, &len_out, encoding);
223             else
224                 xmlDocDumpMemory(doc, &buf_out, &len_out);
225             *content_buf = (char *) odr_malloc(o, len_out);
226             *content_len = len_out;
227             memcpy(*content_buf, buf_out, len_out);
228             xmlFree(buf_out);
229         }
230         xmlFreeNode(envelope_ptr);
231         xmlFreeDoc(doc);
232         return 0;
233     }
234     return 0;
235 }
236 #else
237 int z_soap_codec_enc(ODR o, Z_SOAP **pp, 
238                      char **content_buf, int *content_len,
239                      Z_SOAP_Handler *handlers, const char *encoding)
240 {
241     static char *err_xml =
242         "<?xml version=\"1.0\"?>\n"
243         "<SOAP-ENV:Envelope"
244         " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
245         "\t<SOAP-ENV:Body>\n"
246         "\t\t<SOAP-ENV:Fault>\n"
247         "\t\t\t<faultcode>SOAP-ENV:Server</faultcode>\n"
248         "\t\t\t<faultstring>HTTP error</faultstring>\n"
249         "\t\t\t<detail>SOAP not supported in this YAZ configuration</detail>\n"
250         "\t\t</SOAP-ENV:Fault>\n"
251         "\t</SOAP-ENV:Body>\n"
252         "</SOAP-ENV:Envelope>\n";
253     if (o->direction == ODR_ENCODE)
254     {
255         *content_buf = err_xml;
256         *content_len = strlen(err_xml);
257     }
258     return -1;
259 }
260 #endif
261 int z_soap_codec(ODR o, Z_SOAP **pp, 
262                  char **content_buf, int *content_len,
263                  Z_SOAP_Handler *handlers)
264 {
265     return z_soap_codec_enc(o, pp, content_buf, content_len, handlers, 0);
266 }
267