2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: soap.c,v 1.1 2003-10-27 12:21:35 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);
114 /* check for fault package */
115 if (!strcmp(ptr->ns->href, p->ns)
116 && !strcmp(ptr->name, "Fault") && ptr->children)
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;
127 if (ptr->children && ptr->children->type == XML_TEXT_NODE)
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);
145 for (i = 0; handlers[i].ns; i++)
146 if (!strcmp(ptr->ns->href, handlers[i].ns))
150 void *handler_data = 0;
151 ret = (*handlers[i].f)(o, pptr, &handler_data,
152 handlers[i].client_data,
154 if (ret || !handler_data)
155 z_soap_error(o, p, "SOAP-ENV:Client",
156 "SOAP Handler returned error", 0);
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;
168 ret = z_soap_error(o, p, "SOAP-ENV:Client",
169 "No handler for NS", ptr->ns->href);
175 else if (o->direction == ODR_ENCODE)
179 xmlNodePtr envelope_ptr, body_ptr;
181 xmlDocPtr doc = xmlNewDoc("1.0");
183 envelope_ptr = xmlNewNode(0, "Envelope");
184 ns_env = xmlNewNs(envelope_ptr, p->ns, "SOAP-ENV");
185 xmlSetNs(envelope_ptr, ns_env);
187 body_ptr = xmlNewChild(envelope_ptr, ns_env, "Body", 0);
188 xmlDocSetRootElement(doc, envelope_ptr);
190 if (p->which == Z_SOAP_fault || p->which == Z_SOAP_error)
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);
197 xmlNewChild(fault_ptr, ns_env, "details", f->details);
199 else if (p->which == Z_SOAP_generic)
201 int ret, no = p->u.generic->no;
203 ret = (*handlers[no].f)(o, body_ptr, &p->u.generic->p,
204 handlers[no].client_data,
209 if (p->which == Z_SOAP_generic && !strcmp(p->ns, "SRU"))
211 xmlDocSetRootElement(doc, body_ptr->children);
218 xmlDocDumpMemoryEnc(doc, &buf_out, &len_out, encoding);
220 xmlDocDumpMemory(doc, &buf_out, &len_out);
221 *content_buf = (char *) odr_malloc(o, len_out);
222 *content_len = len_out;
223 memcpy(*content_buf, buf_out, len_out);
232 int z_soap_codec_enc(ODR o, Z_SOAP **pp,
233 char **content_buf, int *content_len,
234 Z_SOAP_Handler *handlers, const char *encoding)
236 static char *err_xml =
237 "<?xml version=\"1.0\"?>\n"
239 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
240 "\t<SOAP-ENV:Body>\n"
241 "\t\t<SOAP-ENV:Fault>\n"
242 "\t\t\t<faultcode>SOAP-ENV:Server</faultcode>\n"
243 "\t\t\t<faultstring>HTTP error</faultstring>\n"
244 "\t\t\t<detail>SOAP not supported in this YAZ configuration</detail>\n"
245 "\t\t</SOAP-ENV:Fault>\n"
246 "\t</SOAP-ENV:Body>\n"
247 "</SOAP-ENV:Envelope>\n";
248 if (o->direction == ODR_ENCODE)
250 *content_buf = err_xml;
251 *content_len = strlen(err_xml);
256 int z_soap_codec(ODR o, Z_SOAP **pp,
257 char **content_buf, int *content_len,
258 Z_SOAP_Handler *handlers)
260 return z_soap_codec_enc(o, pp, content_buf, content_len, handlers, 0);