1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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);
51 if (!ptr || ptr->type != XML_ELEMENT_NODE || !ptr->ns)
54 return z_soap_error(o, p, "SOAP-ENV:Client",
55 "No Envelope element", 0);
57 /* check for SRU root node match */
58 for (i = 0; handlers[i].ns; i++)
60 const char *hns = handlers[i].ns;
63 if (yaz_match_glob(hns, (const char *) ptr->ns->href))
68 if (yaz_match_glob(hns, (const char *) ptr->name))
74 void *handler_data = 0;
75 xmlNode p_top_tmp; /* pseudo parent node needed */
77 p_top_tmp.children = ptr;
78 ret = (*handlers[i].f)(o, &p_top_tmp, &handler_data,
79 handlers[i].client_data,
80 (const char *)ptr->ns->href);
82 if (ret || !handler_data)
83 z_soap_error(o, p, "SOAP-ENV:Client",
84 "SOAP Handler returned error", 0);
87 p->which = Z_SOAP_generic;
88 p->u.generic = (Z_SOAP_Generic *)
89 odr_malloc(o, sizeof(*p->u.generic));
91 p->u.generic->ns = handlers[i].ns;
92 p->u.generic->p = handler_data;
98 if (xmlStrcmp(ptr->name, BAD_CAST "Envelope"))
101 return z_soap_error(o, p, "SOAP-ENV:Client",
102 "No Envelope element", 0);
106 /* determine SOAP version */
107 const char * ns_envelope = (const char *) ptr->ns->href;
108 if (!strcmp(ns_envelope, soap_v1_1))
110 else if (!strcmp(ns_envelope, soap_v1_2))
115 return z_soap_error(o, p, "SOAP-ENV:Client",
116 "Bad SOAP version", 0);
120 while(ptr && ptr->type == XML_TEXT_NODE)
122 if (ptr && ptr->type == XML_ELEMENT_NODE &&
123 !xmlStrcmp(ptr->ns->href, BAD_CAST p->ns) &&
124 !xmlStrcmp(ptr->name, BAD_CAST "Header"))
127 while(ptr && ptr->type == XML_TEXT_NODE)
130 /* check that Body is present */
131 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
132 xmlStrcmp(ptr->name, BAD_CAST "Body"))
135 return z_soap_error(o, p, "SOAP-ENV:Client",
136 "SOAP Body element not found", 0);
138 if (xmlStrcmp(ptr->ns->href, BAD_CAST p->ns))
141 return z_soap_error(o, p, "SOAP-ENV:Client",
142 "SOAP bad NS for Body element", 0);
146 while (ptr && ptr->type == XML_TEXT_NODE)
148 if (!ptr || ptr->type != XML_ELEMENT_NODE)
151 return z_soap_error(o, p, "SOAP-ENV:Client",
152 "SOAP No content for Body", 0);
157 return z_soap_error(o, p, "SOAP-ENV:Client",
158 "SOAP No namespace for content", 0);
160 /* check for fault package */
161 if (!xmlStrcmp(ptr->ns->href, BAD_CAST p->ns)
162 && !xmlStrcmp(ptr->name, BAD_CAST "Fault") && ptr->children)
166 p->which = Z_SOAP_fault;
167 p->u.fault = (Z_SOAP_Fault *) odr_malloc(o, sizeof(*p->u.fault));
168 p->u.fault->fault_code = 0;
169 p->u.fault->fault_string = 0;
170 p->u.fault->details = 0;
173 if (ptr->children && ptr->children->type == XML_TEXT_NODE)
175 if (!xmlStrcmp(ptr->name, BAD_CAST "faultcode"))
176 p->u.fault->fault_code =
177 odr_strdup(o, (const char *)
178 ptr->children->content);
179 if (!xmlStrcmp(ptr->name, BAD_CAST "faultstring"))
180 p->u.fault->fault_string =
181 odr_strdup(o, (const char *)
182 ptr->children->content);
183 if (!xmlStrcmp(ptr->name, BAD_CAST "details"))
184 p->u.fault->details =
185 odr_strdup(o, (const char *)
186 ptr->children->content);
194 const char *ns = (const char *) ptr->ns->href;
195 for (i = 0; handlers[i].ns; i++)
197 if (strchr(handlers[i].ns, ':') &&
198 yaz_match_glob(handlers[i].ns, ns))
203 void *handler_data = 0;
204 ret = (*handlers[i].f)(o, pptr, &handler_data,
205 handlers[i].client_data, ns);
206 if (ret || !handler_data)
207 z_soap_error(o, p, "SOAP-ENV:Client",
208 "SOAP Handler returned error", 0);
211 p->which = Z_SOAP_generic;
212 p->u.generic = (Z_SOAP_Generic *)
213 odr_malloc(o, sizeof(*p->u.generic));
214 p->u.generic->no = i;
215 p->u.generic->ns = handlers[i].ns;
216 p->u.generic->p = handler_data;
221 ret = z_soap_error(o, p, "SOAP-ENV:Client",
222 "No handler for NS", ns);
228 else if (o->direction == ODR_ENCODE)
232 xmlNodePtr envelope_ptr, body_ptr;
234 xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
236 envelope_ptr = xmlNewNode(0, BAD_CAST "Envelope");
237 ns_env = xmlNewNs(envelope_ptr, BAD_CAST p->ns,
238 BAD_CAST "SOAP-ENV");
239 xmlSetNs(envelope_ptr, ns_env);
241 body_ptr = xmlNewChild(envelope_ptr, ns_env, BAD_CAST "Body",
243 xmlDocSetRootElement(doc, envelope_ptr);
245 if (p->which == Z_SOAP_fault || p->which == Z_SOAP_error)
247 Z_SOAP_Fault *f = p->u.fault;
248 xmlNodePtr fault_ptr = xmlNewChild(body_ptr, ns_env,
249 BAD_CAST "Fault", 0);
250 xmlNewChild(fault_ptr, ns_env, BAD_CAST "faultcode",
251 BAD_CAST f->fault_code);
252 xmlNewChild(fault_ptr, ns_env, BAD_CAST "faultstring",
253 BAD_CAST f->fault_string);
255 xmlNewChild(fault_ptr, ns_env, BAD_CAST "details",
256 BAD_CAST f->details);
258 else if (p->which == Z_SOAP_generic)
260 int ret, no = p->u.generic->no;
262 ret = (*handlers[no].f)(o, body_ptr, &p->u.generic->p,
263 handlers[no].client_data,
271 if (p->which == Z_SOAP_generic && !strcmp(p->ns, "SRU"))
273 xmlDocSetRootElement(doc, body_ptr->children);
274 body_ptr->children = 0;
275 xmlFreeNode(envelope_ptr);
279 char *content = (char *) odr_malloc(o, strlen(stylesheet) + 40);
281 xmlNodePtr pi, ptr = xmlDocGetRootElement(doc);
282 sprintf(content, "type=\"text/xsl\" href=\"%s\"", stylesheet);
283 pi = xmlNewPI(BAD_CAST "xml-stylesheet",
285 xmlAddPrevSibling(ptr, pi);
292 xmlDocDumpMemoryEnc(doc, &buf_out, &len_out, encoding);
294 xmlDocDumpMemory(doc, &buf_out, &len_out);
295 *content_buf = (char *) odr_malloc(o, len_out);
296 *content_len = len_out;
297 memcpy(*content_buf, buf_out, len_out);
306 int z_soap_codec_enc_xsl(ODR o, Z_SOAP **pp,
307 char **content_buf, int *content_len,
308 Z_SOAP_Handler *handlers, const char *encoding,
309 const char *stylesheet)
311 static char *err_xml =
312 "<?xml version=\"1.0\"?>\n"
314 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
315 "\t<SOAP-ENV:Body>\n"
316 "\t\t<SOAP-ENV:Fault>\n"
317 "\t\t\t<faultcode>SOAP-ENV:Server</faultcode>\n"
318 "\t\t\t<faultstring>HTTP error</faultstring>\n"
319 "\t\t\t<detail>SOAP not supported in this YAZ configuration</detail>\n"
320 "\t\t</SOAP-ENV:Fault>\n"
321 "\t</SOAP-ENV:Body>\n"
322 "</SOAP-ENV:Envelope>\n";
323 if (o->direction == ODR_ENCODE)
325 *content_buf = err_xml;
326 *content_len = strlen(err_xml);
331 int z_soap_codec_enc(ODR o, Z_SOAP **pp,
332 char **content_buf, int *content_len,
333 Z_SOAP_Handler *handlers,
334 const char *encoding)
336 return z_soap_codec_enc_xsl(o, pp, content_buf, content_len, handlers,
340 int z_soap_codec(ODR o, Z_SOAP **pp,
341 char **content_buf, int *content_len,
342 Z_SOAP_Handler *handlers)
344 return z_soap_codec_enc(o, pp, content_buf, content_len, handlers, 0);
347 int z_soap_error(ODR o, Z_SOAP *p,
348 const char *fault_code, const char *fault_string,
351 p->which = Z_SOAP_error;
352 p->u.soap_error = (Z_SOAP_Fault *)
353 odr_malloc(o, sizeof(*p->u.soap_error));
354 p->u.soap_error->fault_code = odr_strdup(o, fault_code);
355 p->u.soap_error->fault_string = odr_strdup(o, fault_string);
357 p->u.soap_error->details = odr_strdup(o, details);
359 p->u.soap_error->details = 0;
366 * c-file-style: "Stroustrup"
367 * indent-tabs-mode: nil
369 * vim: shiftwidth=4 tabstop=8 expandtab