1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SOAP
9 * This implements encoding and decoding of SOAP packages using
17 #include <yaz/match_glob.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
23 static const char *soap_v1_1 = "http://schemas.xmlsoap.org/soap/envelope/";
24 static const char *soap_v1_2 = "http://www.w3.org/2001/06/soap-envelope";
26 int z_soap_codec_enc_xsl(ODR o, Z_SOAP **pp,
27 char **content_buf, int *content_len,
28 Z_SOAP_Handler *handlers,
30 const char *stylesheet)
32 if (o->direction == ODR_DECODE)
39 if (!content_buf || !*content_buf || !content_len)
42 *pp = p = (Z_SOAP *) odr_malloc(o, sizeof(*p));
45 doc = xmlParseMemory(*content_buf, *content_len);
47 return z_soap_error(o, p, "SOAP-ENV:Client",
48 "Bad XML Document", 0);
50 ptr = xmlDocGetRootElement(doc);
54 return z_soap_error(o, p, "SOAP-ENV:Client",
55 "No Envelope element", 0);
57 /* check for SRU root node match */
59 for (i = 0; handlers[i].ns; i++)
60 if (yaz_match_glob(handlers[i].ns, (const char *)ptr->ns->href))
64 void *handler_data = 0;
65 xmlNode p_top_tmp; /* pseudo parent node needed */
67 p_top_tmp.children = ptr;
68 ret = (*handlers[i].f)(o, &p_top_tmp, &handler_data,
69 handlers[i].client_data,
70 (const char *)ptr->ns->href);
72 if (ret || !handler_data)
73 z_soap_error(o, p, "SOAP-ENV:Client",
74 "SOAP Handler returned error", 0);
77 p->which = Z_SOAP_generic;
78 p->u.generic = (Z_SOAP_Generic *)
79 odr_malloc(o, sizeof(*p->u.generic));
81 p->u.generic->ns = handlers[i].ns;
82 p->u.generic->p = handler_data;
89 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
90 xmlStrcmp(ptr->name, BAD_CAST "Envelope") || !ptr->ns)
93 return z_soap_error(o, p, "SOAP-ENV:Client",
94 "No Envelope element", 0);
98 /* determine SOAP version */
99 const char * ns_envelope = (const char *) ptr->ns->href;
100 if (!strcmp(ns_envelope, soap_v1_1))
102 else if (!strcmp(ns_envelope, soap_v1_2))
107 return z_soap_error(o, p, "SOAP-ENV:Client",
108 "Bad SOAP version", 0);
112 while(ptr && ptr->type == XML_TEXT_NODE)
114 if (ptr && ptr->type == XML_ELEMENT_NODE &&
115 !xmlStrcmp(ptr->ns->href, BAD_CAST p->ns) &&
116 !xmlStrcmp(ptr->name, BAD_CAST "Header"))
119 while(ptr && ptr->type == XML_TEXT_NODE)
122 /* check that Body is present */
123 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
124 xmlStrcmp(ptr->name, BAD_CAST "Body"))
127 return z_soap_error(o, p, "SOAP-ENV:Client",
128 "SOAP Body element not found", 0);
130 if (xmlStrcmp(ptr->ns->href, BAD_CAST p->ns))
133 return z_soap_error(o, p, "SOAP-ENV:Client",
134 "SOAP bad NS for Body element", 0);
138 while (ptr && ptr->type == XML_TEXT_NODE)
140 if (!ptr || ptr->type != XML_ELEMENT_NODE)
143 return z_soap_error(o, p, "SOAP-ENV:Client",
144 "SOAP No content for Body", 0);
149 return z_soap_error(o, p, "SOAP-ENV:Client",
150 "SOAP No namespace for content", 0);
152 /* check for fault package */
153 if (!xmlStrcmp(ptr->ns->href, BAD_CAST p->ns)
154 && !xmlStrcmp(ptr->name, BAD_CAST "Fault") && ptr->children)
158 p->which = Z_SOAP_fault;
159 p->u.fault = (Z_SOAP_Fault *) odr_malloc(o, sizeof(*p->u.fault));
160 p->u.fault->fault_code = 0;
161 p->u.fault->fault_string = 0;
162 p->u.fault->details = 0;
165 if (ptr->children && ptr->children->type == XML_TEXT_NODE)
167 if (!xmlStrcmp(ptr->name, BAD_CAST "faultcode"))
168 p->u.fault->fault_code =
169 odr_strdup(o, (const char *)
170 ptr->children->content);
171 if (!xmlStrcmp(ptr->name, BAD_CAST "faultstring"))
172 p->u.fault->fault_string =
173 odr_strdup(o, (const char *)
174 ptr->children->content);
175 if (!xmlStrcmp(ptr->name, BAD_CAST "details"))
176 p->u.fault->details =
177 odr_strdup(o, (const char *)
178 ptr->children->content);
186 const char *ns = (const char *) ptr->ns->href;
187 for (i = 0; handlers[i].ns; i++)
189 if (yaz_match_glob(handlers[i].ns, ns))
194 void *handler_data = 0;
195 ret = (*handlers[i].f)(o, pptr, &handler_data,
196 handlers[i].client_data, ns);
197 if (ret || !handler_data)
198 z_soap_error(o, p, "SOAP-ENV:Client",
199 "SOAP Handler returned error", 0);
202 p->which = Z_SOAP_generic;
203 p->u.generic = (Z_SOAP_Generic *)
204 odr_malloc(o, sizeof(*p->u.generic));
205 p->u.generic->no = i;
206 p->u.generic->ns = handlers[i].ns;
207 p->u.generic->p = handler_data;
212 ret = z_soap_error(o, p, "SOAP-ENV:Client",
213 "No handler for NS", ns);
219 else if (o->direction == ODR_ENCODE)
223 xmlNodePtr envelope_ptr, body_ptr;
225 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
227 envelope_ptr = xmlNewNode(0, BAD_CAST "Envelope");
228 ns_env = xmlNewNs(envelope_ptr, BAD_CAST p->ns,
229 BAD_CAST "SOAP-ENV");
230 xmlSetNs(envelope_ptr, ns_env);
232 body_ptr = xmlNewChild(envelope_ptr, ns_env, BAD_CAST "Body",
234 xmlDocSetRootElement(doc, envelope_ptr);
236 if (p->which == Z_SOAP_fault || p->which == Z_SOAP_error)
238 Z_SOAP_Fault *f = p->u.fault;
239 xmlNodePtr fault_ptr = xmlNewChild(body_ptr, ns_env,
240 BAD_CAST "Fault", 0);
241 xmlNewChild(fault_ptr, ns_env, BAD_CAST "faultcode",
242 BAD_CAST f->fault_code);
243 xmlNewChild(fault_ptr, ns_env, BAD_CAST "faultstring",
244 BAD_CAST f->fault_string);
246 xmlNewChild(fault_ptr, ns_env, BAD_CAST "details",
247 BAD_CAST f->details);
249 else if (p->which == Z_SOAP_generic)
251 int ret, no = p->u.generic->no;
253 ret = (*handlers[no].f)(o, body_ptr, &p->u.generic->p,
254 handlers[no].client_data,
262 if (p->which == Z_SOAP_generic && !strcmp(p->ns, "SRU"))
264 xmlDocSetRootElement(doc, body_ptr->children);
265 body_ptr->children = 0;
266 xmlFreeNode(envelope_ptr);
270 char *content = (char *) odr_malloc(o, strlen(stylesheet) + 40);
272 xmlNodePtr pi, ptr = xmlDocGetRootElement(doc);
273 sprintf(content, "type=\"text/xsl\" href=\"%s\"", stylesheet);
274 pi = xmlNewPI(BAD_CAST "xml-stylesheet",
276 xmlAddPrevSibling(ptr, pi);
283 xmlDocDumpMemoryEnc(doc, &buf_out, &len_out, encoding);
285 xmlDocDumpMemory(doc, &buf_out, &len_out);
286 *content_buf = (char *) odr_malloc(o, len_out);
287 *content_len = len_out;
288 memcpy(*content_buf, buf_out, len_out);
297 int z_soap_codec_enc_xsl(ODR o, Z_SOAP **pp,
298 char **content_buf, int *content_len,
299 Z_SOAP_Handler *handlers, const char *encoding,
300 const char *stylesheet)
302 static char *err_xml =
303 "<?xml version=\"1.0\"?>\n"
305 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
306 "\t<SOAP-ENV:Body>\n"
307 "\t\t<SOAP-ENV:Fault>\n"
308 "\t\t\t<faultcode>SOAP-ENV:Server</faultcode>\n"
309 "\t\t\t<faultstring>HTTP error</faultstring>\n"
310 "\t\t\t<detail>SOAP not supported in this YAZ configuration</detail>\n"
311 "\t\t</SOAP-ENV:Fault>\n"
312 "\t</SOAP-ENV:Body>\n"
313 "</SOAP-ENV:Envelope>\n";
314 if (o->direction == ODR_ENCODE)
316 *content_buf = err_xml;
317 *content_len = strlen(err_xml);
322 int z_soap_codec_enc(ODR o, Z_SOAP **pp,
323 char **content_buf, int *content_len,
324 Z_SOAP_Handler *handlers,
325 const char *encoding)
327 return z_soap_codec_enc_xsl(o, pp, content_buf, content_len, handlers,
331 int z_soap_codec(ODR o, Z_SOAP **pp,
332 char **content_buf, int *content_len,
333 Z_SOAP_Handler *handlers)
335 return z_soap_codec_enc(o, pp, content_buf, content_len, handlers, 0);
338 int z_soap_error(ODR o, Z_SOAP *p,
339 const char *fault_code, const char *fault_string,
342 p->which = Z_SOAP_error;
343 p->u.soap_error = (Z_SOAP_Fault *)
344 odr_malloc(o, sizeof(*p->u.soap_error));
345 p->u.soap_error->fault_code = odr_strdup(o, fault_code);
346 p->u.soap_error->fault_string = odr_strdup(o, fault_string);
348 p->u.soap_error->details = odr_strdup(o, details);
350 p->u.soap_error->details = 0;
357 * c-file-style: "Stroustrup"
358 * indent-tabs-mode: nil
360 * vim: shiftwidth=4 tabstop=8 expandtab