2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: soap.c,v 1.3 2003-12-20 00:51:19 adam Exp $
11 #include <libxml/parser.h>
12 #include <libxml/tree.h>
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";
17 int z_soap_error(ODR o, Z_SOAP *p,
18 const char *fault_code, const char *fault_string,
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);
27 p->u.soap_error->details = odr_strdup(o, details);
29 p->u.soap_error->details = 0;
33 int z_soap_codec_enc(ODR o, Z_SOAP **pp,
34 char **content_buf, int *content_len,
35 Z_SOAP_Handler *handlers,
38 if (o->direction == ODR_DECODE)
45 if (!content_buf || !*content_buf || !content_len)
48 *pp = p = (Z_SOAP *) odr_malloc(o, sizeof(*p));
51 doc = xmlParseMemory(*content_buf, *content_len);
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)
61 return z_soap_error(o, p, "SOAP-ENV:Client",
62 "No Envelope element", 0);
66 /* determine SOAP version */
67 const char * ns_envelope = ptr->ns->href;
68 if (!strcmp(ns_envelope, soap_v1_1))
70 else if (!strcmp(ns_envelope, soap_v1_2))
75 return z_soap_error(o, p, "SOAP-ENV:Client",
76 "Bad SOAP version", 0);
80 while(ptr && ptr->type == XML_TEXT_NODE)
82 if (ptr && ptr->type == XML_ELEMENT_NODE &&
83 !strcmp(ptr->ns->href, p->ns) &&
84 !strcmp(ptr->name, "Header"))
87 while(ptr && ptr->type == XML_TEXT_NODE)
90 /* check that Body is present */
91 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
92 strcmp(ptr->name, "Body"))
95 return z_soap_error(o, p, "SOAP-ENV:Client",
96 "SOAP Body element not found", 0);
98 if (strcmp(ptr->ns->href, p->ns))
101 return z_soap_error(o, p, "SOAP-ENV:Client",
102 "SOAP bad NS for Body element", 0);
106 while (ptr && ptr->type == XML_TEXT_NODE)
108 if (!ptr || ptr->type != XML_ELEMENT_NODE)
111 return z_soap_error(o, p, "SOAP-ENV:Client",
112 "SOAP No content for Body", 0);
117 return z_soap_error(o, p, "SOAP-ENV:Client",
118 "SOAP No namespace for content", 0);
120 /* check for fault package */
121 if (!strcmp(ptr->ns->href, p->ns)
122 && !strcmp(ptr->name, "Fault") && ptr->children)
126 p->which = Z_SOAP_fault;
127 p->u.fault = odr_malloc(o, sizeof(*p->u.fault));
128 p->u.fault->fault_code = 0;
129 p->u.fault->fault_string = 0;
130 p->u.fault->details = 0;
133 if (ptr->children && ptr->children->type == XML_TEXT_NODE)
135 if (!strcmp(ptr->name, "faultcode"))
136 p->u.fault->fault_code =
137 odr_strdup(o, ptr->children->content);
138 if (!strcmp(ptr->name, "faultstring"))
139 p->u.fault->fault_string =
140 odr_strdup(o, ptr->children->content);
141 if (!strcmp(ptr->name, "details"))
142 p->u.fault->details =
143 odr_strdup(o, ptr->children->content);
151 for (i = 0; handlers[i].ns; i++)
152 if (!strcmp(ptr->ns->href, handlers[i].ns))
156 void *handler_data = 0;
157 ret = (*handlers[i].f)(o, pptr, &handler_data,
158 handlers[i].client_data,
160 if (ret || !handler_data)
161 z_soap_error(o, p, "SOAP-ENV:Client",
162 "SOAP Handler returned error", 0);
165 p->which = Z_SOAP_generic;
166 p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
167 p->u.generic->no = i;
168 p->u.generic->ns = handlers[i].ns;
169 p->u.generic->p = handler_data;
174 ret = z_soap_error(o, p, "SOAP-ENV:Client",
175 "No handler for NS", ptr->ns->href);
181 else if (o->direction == ODR_ENCODE)
185 xmlNodePtr envelope_ptr, body_ptr;
187 xmlDocPtr doc = xmlNewDoc("1.0");
189 envelope_ptr = xmlNewNode(0, "Envelope");
190 ns_env = xmlNewNs(envelope_ptr, p->ns, "SOAP-ENV");
191 xmlSetNs(envelope_ptr, ns_env);
193 body_ptr = xmlNewChild(envelope_ptr, ns_env, "Body", 0);
194 xmlDocSetRootElement(doc, envelope_ptr);
196 if (p->which == Z_SOAP_fault || p->which == Z_SOAP_error)
198 Z_SOAP_Fault *f = p->u.fault;
199 xmlNodePtr fault_ptr = xmlNewChild(body_ptr, ns_env, "Fault", 0);
200 xmlNewChild(fault_ptr, ns_env, "faultcode", f->fault_code);
201 xmlNewChild(fault_ptr, ns_env, "faultstring", f->fault_string);
203 xmlNewChild(fault_ptr, ns_env, "details", f->details);
205 else if (p->which == Z_SOAP_generic)
207 int ret, no = p->u.generic->no;
209 ret = (*handlers[no].f)(o, body_ptr, &p->u.generic->p,
210 handlers[no].client_data,
218 if (p->which == Z_SOAP_generic && !strcmp(p->ns, "SRU"))
220 xmlDocSetRootElement(doc, body_ptr->children);
227 xmlDocDumpMemoryEnc(doc, &buf_out, &len_out, encoding);
229 xmlDocDumpMemory(doc, &buf_out, &len_out);
230 *content_buf = (char *) odr_malloc(o, len_out);
231 *content_len = len_out;
232 memcpy(*content_buf, buf_out, len_out);
241 int z_soap_codec_enc(ODR o, Z_SOAP **pp,
242 char **content_buf, int *content_len,
243 Z_SOAP_Handler *handlers, const char *encoding)
245 static char *err_xml =
246 "<?xml version=\"1.0\"?>\n"
248 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
249 "\t<SOAP-ENV:Body>\n"
250 "\t\t<SOAP-ENV:Fault>\n"
251 "\t\t\t<faultcode>SOAP-ENV:Server</faultcode>\n"
252 "\t\t\t<faultstring>HTTP error</faultstring>\n"
253 "\t\t\t<detail>SOAP not supported in this YAZ configuration</detail>\n"
254 "\t\t</SOAP-ENV:Fault>\n"
255 "\t</SOAP-ENV:Body>\n"
256 "</SOAP-ENV:Envelope>\n";
257 if (o->direction == ODR_ENCODE)
259 *content_buf = err_xml;
260 *content_len = strlen(err_xml);
265 int z_soap_codec(ODR o, Z_SOAP **pp,
266 char **content_buf, int *content_len,
267 Z_SOAP_Handler *handlers)
269 return z_soap_codec_enc(o, pp, content_buf, content_len, handlers, 0);