You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

712 lines
14 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: soap-env.c,v 1.20 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A SOAP client/server library in C
  5. * Copyright (C) 2003 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_STDARG_H
  28. #include <stdarg.h>
  29. #endif
  30. #ifdef HAVE_STDIO_H
  31. #include <stdio.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #ifdef HAVE_ERRNO_H
  37. #include <errno.h>
  38. #endif
  39. #ifdef WIN32
  40. #define USE_XMLSTRING
  41. #endif
  42. #ifdef USE_XMLSTRING
  43. #include <libxml/xmlstring.h>
  44. #endif
  45. #include <nanohttp/nanohttp-logging.h>
  46. #include "soap-xml.h"
  47. #include "soap-env.h"
  48. /*
  49. Parameters:
  50. 1- soap_env_ns
  51. 2- soap_env_enc
  52. 3- xsi_ns
  53. 4- xsd_ns
  54. 3- method name
  55. 4- uri
  56. 5- method name(again)
  57. */
  58. #define _SOAP_MSG_TEMPLATE_ \
  59. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"%s\" SOAP-ENV:encodingStyle=\"%s\"" \
  60. " xmlns:xsi=\"%s\"" \
  61. " xmlns:xsd=\"%s\">" \
  62. " <SOAP-ENV:Header />" \
  63. " <SOAP-ENV:Body>"\
  64. " <m:%s xmlns:m=\"%s\">"\
  65. " </m:%s>" \
  66. " </SOAP-ENV:Body>"\
  67. "</SOAP-ENV:Envelope>"
  68. /*
  69. Parameters:
  70. 1- soap_env_ns
  71. 2- soap_env_enc
  72. 3- xsi_ns
  73. 4- xsd_ns
  74. 3- method name
  75. 4- uri
  76. 5- method name(again)
  77. */
  78. #define _SOAP_MSG_TEMPLATE_EMPTY_TARGET_ \
  79. "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"%s\" SOAP-ENV:encodingStyle=\"%s\"" \
  80. " xmlns:xsi=\"%s\"" \
  81. " xmlns:xsd=\"%s\">" \
  82. " <SOAP-ENV:Header />" \
  83. " <SOAP-ENV:Body>"\
  84. " <%s xmlns=\"%s\">"\
  85. " </%s>" \
  86. " </SOAP-ENV:Body>"\
  87. "</SOAP-ENV:Envelope>"
  88. /* ---------------------------------------------------------------------------- */
  89. /* XML Serializers (implemented at the and of this document) */
  90. /* ---------------------------------------------------------------------------- */
  91. struct XmlNodeHolder
  92. {
  93. xmlNodePtr node;
  94. };
  95. static void
  96. xmlbuilder_start_element(const xmlChar * element_name, int attr_count, xmlChar ** keys, xmlChar ** values, void *userData)
  97. {
  98. struct XmlNodeHolder *holder = (struct XmlNodeHolder *) userData;
  99. xmlNodePtr parent = NULL;
  100. if (holder == NULL)
  101. return;
  102. parent = holder->node;
  103. if (parent == NULL)
  104. return;
  105. holder->node = xmlNewChild(parent, NULL, element_name, NULL);
  106. return;
  107. }
  108. static void
  109. xmlbuilder_characters(const xmlChar * element_name, const xmlChar * chars, void *userData)
  110. {
  111. struct XmlNodeHolder *holder = (struct XmlNodeHolder *) userData;
  112. xmlNodePtr parent = NULL;
  113. if (holder == NULL)
  114. return;
  115. parent = holder->node;
  116. if (parent == NULL)
  117. return;
  118. xmlNewTextChild(parent, NULL, element_name, chars);
  119. return;
  120. }
  121. static void
  122. xmlbuilder_end_element(const xmlChar * element_name, void *userData)
  123. {
  124. struct XmlNodeHolder *holder = (struct XmlNodeHolder *) userData;
  125. xmlNodePtr parent = NULL;
  126. if (holder == NULL)
  127. return;
  128. parent = holder->node;
  129. if (parent == NULL)
  130. return;
  131. holder->node = parent->parent;
  132. return;
  133. }
  134. herror_t
  135. soap_env_new_from_doc(xmlDocPtr doc, SoapEnv ** out)
  136. {
  137. xmlNodePtr node;
  138. SoapEnv *env;
  139. if (doc == NULL)
  140. {
  141. log_error1("Can not create xml document!");
  142. return herror_new("soap_env_new_from_doc",
  143. GENERAL_INVALID_PARAM,
  144. "XML Document (xmlDocPtr) is NULL");
  145. }
  146. if (!(node = xmlDocGetRootElement(doc)))
  147. {
  148. log_error1("XML document is empty!");
  149. return herror_new("soap_env_new_from_doc",
  150. XML_ERROR_EMPTY_DOCUMENT, "XML Document is empty!");
  151. }
  152. if (!(env = (SoapEnv *) malloc(sizeof(SoapEnv))))
  153. {
  154. log_error2("malloc failed (%s)", strerror(errno));
  155. return herror_new("soap_env_from_doc", GENERAL_INVALID_PARAM, "malloc failed");
  156. }
  157. env->root = node;
  158. env->header = soap_env_get_header(env);
  159. env->body = soap_env_get_body(env);
  160. env->cur = soap_env_get_method(env);
  161. *out = env;
  162. return H_OK;
  163. }
  164. herror_t
  165. soap_env_new_from_buffer(const char *buffer, SoapEnv ** out)
  166. {
  167. xmlDocPtr doc;
  168. herror_t err;
  169. if (buffer == NULL)
  170. return herror_new("soap_env_new_from_buffer",
  171. GENERAL_INVALID_PARAM, "buffer (first param) is NULL");
  172. if (!(doc = xmlParseDoc(BAD_CAST buffer)))
  173. return herror_new("soap_env_new_from_buffer",
  174. XML_ERROR_PARSE, "Can not parse xml");
  175. if ((err = soap_env_new_from_doc(doc, out)) != H_OK)
  176. {
  177. xmlFreeDoc(doc);
  178. }
  179. return err;
  180. }
  181. herror_t
  182. soap_env_new_with_fault(fault_code_t faultcode,
  183. const char *faultstring,
  184. const char *faultactor, const char *detail,
  185. SoapEnv ** out)
  186. {
  187. xmlDocPtr doc;
  188. herror_t err;
  189. doc = soap_fault_build(faultcode, faultstring, faultactor, detail);
  190. if (doc == NULL)
  191. return herror_new("soap_env_new_with_fault",
  192. XML_ERROR_PARSE, "Can not parse fault xml");
  193. if ((err = soap_env_new_from_doc(doc, out)) != H_OK)
  194. {
  195. xmlFreeDoc(doc);
  196. }
  197. return err;
  198. }
  199. herror_t
  200. soap_env_new_with_response(SoapEnv * request, SoapEnv ** out)
  201. {
  202. char *method, *res_method;
  203. herror_t ret;
  204. char *urn;
  205. if (request == NULL)
  206. {
  207. return herror_new("soap_env_new_with_response",
  208. GENERAL_INVALID_PARAM, "request (first param) is NULL");
  209. }
  210. if (request->root == NULL)
  211. {
  212. return herror_new("soap_env_new_with_response",
  213. GENERAL_INVALID_PARAM,
  214. "request (first param) has no xml structure");
  215. }
  216. if (!(method = soap_env_find_methodname(request)))
  217. {
  218. return herror_new("soap_env_new_with_response",
  219. GENERAL_INVALID_PARAM,
  220. "Method name '%s' not found in request",
  221. SAVE_STR(method));
  222. }
  223. if (!(urn = soap_env_find_urn(request)))
  224. {
  225. /* here we have no chance to find out the namespace */
  226. /* try to continue without namespace (urn) */
  227. urn = "";
  228. }
  229. if (!(res_method = (char *)malloc(strlen(method)+9)))
  230. return herror_new("soap_env_new_with_response", GENERAL_INVALID_PARAM, "malloc failed");
  231. sprintf(res_method, "%sResponse", method);
  232. ret = soap_env_new_with_method(urn, res_method, out);
  233. free(res_method);
  234. return ret;
  235. }
  236. herror_t
  237. soap_env_new_with_method(const char *urn, const char *method, SoapEnv ** out)
  238. {
  239. xmlDocPtr env;
  240. xmlChar buffer[1054];
  241. log_verbose2("URN = '%s'", urn);
  242. log_verbose2("Method = '%s'", method);
  243. if (!strcmp(urn, ""))
  244. {
  245. #ifdef USE_XMLSTRING
  246. xmlStrPrintf(buffer, 1054, BAD_CAST _SOAP_MSG_TEMPLATE_EMPTY_TARGET_,
  247. soap_env_ns, soap_env_enc, soap_xsi_ns,
  248. soap_xsd_ns, BAD_CAST method, BAD_CAST urn, BAD_CAST method);
  249. #else
  250. sprintf(buffer, _SOAP_MSG_TEMPLATE_EMPTY_TARGET_,
  251. soap_env_ns, soap_env_enc, soap_xsi_ns,
  252. soap_xsd_ns, method, urn, method);
  253. #endif
  254. }
  255. else
  256. {
  257. #ifdef USE_XMLSTRING
  258. xmlStrPrintf(buffer, 1054, BAD_CAST _SOAP_MSG_TEMPLATE_,
  259. soap_env_ns, soap_env_enc, soap_xsi_ns,
  260. soap_xsd_ns, BAD_CAST method, BAD_CAST urn, BAD_CAST method);
  261. #else
  262. sprintf(buffer, _SOAP_MSG_TEMPLATE_,
  263. soap_env_ns, soap_env_enc, soap_xsi_ns,
  264. soap_xsd_ns, method, urn, method);
  265. #endif
  266. }
  267. if (!(env = xmlParseDoc(buffer)))
  268. return herror_new("soap_env_new_with_method",
  269. XML_ERROR_PARSE, "Can not parse xml");
  270. return soap_env_new_from_doc(env, out);
  271. }
  272. static int
  273. _soap_env_xml_io_read(void *ctx, char *buffer, int len)
  274. {
  275. int readed;
  276. http_input_stream_t *in = (http_input_stream_t *) ctx;
  277. if (!http_input_stream_is_ready(in))
  278. return 0;
  279. readed = http_input_stream_read(in, buffer, len);
  280. if (readed == -1)
  281. return 0;
  282. return readed;
  283. }
  284. static int
  285. _soap_env_xml_io_close(void *ctx)
  286. {
  287. /* do nothing */
  288. return 0;
  289. }
  290. herror_t
  291. soap_env_new_from_stream(http_input_stream_t * in, SoapEnv ** out)
  292. {
  293. xmlDocPtr doc;
  294. doc = xmlReadIO(_soap_env_xml_io_read,
  295. _soap_env_xml_io_close, in, "", NULL, 0);
  296. if (in->err != H_OK)
  297. return in->err;
  298. if (doc == NULL)
  299. return herror_new("soap_env_new_from_stream",
  300. XML_ERROR_PARSE, "Trying to parse not valid xml");
  301. return soap_env_new_from_doc(doc, out);
  302. }
  303. xmlNodePtr
  304. soap_env_add_item(SoapEnv * call, const char *type, const char *name, const char *value)
  305. {
  306. xmlNodePtr newnode;
  307. newnode = xmlNewTextChild(call->cur, NULL, BAD_CAST name, BAD_CAST value);
  308. if (newnode == NULL)
  309. {
  310. log_error1("Can not create new xml node");
  311. return NULL;
  312. }
  313. if (type)
  314. {
  315. if (!xmlNewProp(newnode, BAD_CAST "xsi:type", BAD_CAST type))
  316. {
  317. log_error1("Can not create new xml attribute");
  318. return NULL;
  319. }
  320. }
  321. return newnode;
  322. }
  323. xmlNodePtr
  324. soap_env_add_itemf(SoapEnv * call, const char *type,
  325. const char *name, const char *format, ...)
  326. {
  327. va_list ap;
  328. char buffer[1054];
  329. va_start(ap, format);
  330. vsprintf(buffer, format, ap);
  331. va_end(ap);
  332. return soap_env_add_item(call, type, name, buffer);
  333. }
  334. xmlNodePtr
  335. soap_env_add_attachment(SoapEnv * call, const char *name, const char *href)
  336. {
  337. xmlNodePtr newnode;
  338. newnode = xmlNewTextChild(call->cur, NULL, BAD_CAST name, BAD_CAST "");
  339. if (newnode == NULL)
  340. {
  341. log_error1("Can not create new xml node");
  342. return NULL;
  343. }
  344. if (href)
  345. {
  346. if (!xmlNewProp(newnode, BAD_CAST "href", BAD_CAST href))
  347. {
  348. log_error1("Can not create new xml attribute");
  349. return NULL;
  350. }
  351. }
  352. return newnode;
  353. }
  354. void
  355. soap_env_add_custom(SoapEnv * call, void *obj, XmlSerializerCallback cb, const char *type, const char *name)
  356. {
  357. struct XmlNodeHolder holder;
  358. holder.node = soap_env_get_method(call);
  359. cb(obj, BAD_CAST name,
  360. xmlbuilder_start_element,
  361. xmlbuilder_characters, xmlbuilder_end_element, &holder);
  362. return;
  363. }
  364. xmlNodePtr
  365. soap_env_push_item(SoapEnv * call, const char *type, const char *name)
  366. {
  367. xmlNodePtr node;
  368. if ((node = soap_env_add_item(call, type, name, "")))
  369. call->cur = node;
  370. return node;
  371. }
  372. void
  373. soap_env_pop_item(SoapEnv * call)
  374. {
  375. call->cur = call->cur->parent;
  376. return;
  377. }
  378. void
  379. soap_env_free(SoapEnv * env)
  380. {
  381. if (env)
  382. {
  383. if (env->root)
  384. {
  385. xmlFreeDoc(env->root->doc);
  386. }
  387. free(env);
  388. }
  389. return;
  390. }
  391. xmlNodePtr
  392. soap_env_get_body(SoapEnv * env)
  393. {
  394. xmlNodePtr node;
  395. if (env == NULL)
  396. {
  397. log_error1("env object is NULL");
  398. return NULL;
  399. }
  400. if (env->root == NULL)
  401. {
  402. log_error1("env has no xml");
  403. return NULL;
  404. }
  405. for (node = soap_xml_get_children(env->root); node; node = soap_xml_get_next(node))
  406. {
  407. if (!xmlStrcmp(node->name, BAD_CAST "Body")
  408. && !xmlStrcmp(node->ns->href, BAD_CAST soap_env_ns))
  409. return node;
  410. }
  411. log_error1("Body tag not found!");
  412. return NULL;
  413. }
  414. xmlNodePtr
  415. soap_env_get_header(SoapEnv *env)
  416. {
  417. xmlNodePtr node;
  418. if (!env)
  419. {
  420. log_error1("SoapEnv is NULL");
  421. return NULL;
  422. }
  423. if (!env->root)
  424. {
  425. log_error1("SoapEnv contains no document");
  426. return NULL;
  427. }
  428. for (node = soap_xml_get_children(env->root); node; node = soap_xml_get_next(node))
  429. {
  430. if (!xmlStrcmp(node->name, BAD_CAST "Header")
  431. && !xmlStrcmp(node->ns->href, BAD_CAST soap_env_ns))
  432. return node;
  433. }
  434. return NULL;
  435. }
  436. xmlNodePtr
  437. soap_env_get_fault(SoapEnv * env)
  438. {
  439. xmlNodePtr node;
  440. node = soap_env_get_body(env);
  441. if (!node)
  442. return NULL;
  443. while (node != NULL)
  444. {
  445. if (!xmlStrcmp(node->name, BAD_CAST "Fault"))
  446. return node;
  447. node = soap_xml_get_next(node);
  448. }
  449. /* log_warn1 ("Node Fault tag found!");*/
  450. return NULL;
  451. }
  452. xmlNodePtr
  453. soap_env_get_method(SoapEnv * env)
  454. {
  455. xmlNodePtr body;
  456. if (!(body = soap_env_get_body(env)))
  457. {
  458. log_verbose1("SoapEnv contains no Body");
  459. return NULL;
  460. }
  461. /* method is the first child */
  462. return soap_xml_get_children(body);
  463. }
  464. /* XXX: unused function? */
  465. xmlNodePtr
  466. _soap_env_get_body(SoapEnv * env)
  467. {
  468. xmlNodePtr body;
  469. xmlNodeSetPtr nodeset;
  470. xmlXPathObjectPtr xpathobj;
  471. if (env == NULL)
  472. {
  473. log_error1("SoapEnv is NULL");
  474. return NULL;
  475. }
  476. if (env->root == NULL)
  477. {
  478. log_error1("SoapEnv contains no XML document");
  479. return NULL;
  480. }
  481. /*
  482. find <Body> tag find out namespace xpath: //Envelope/Body/ */
  483. xpathobj = soap_xpath_eval(env->root->doc, "//Envelope/Body");
  484. if (!xpathobj)
  485. {
  486. log_error1("No Body (xpathobj)!");
  487. return NULL;
  488. }
  489. if (!(nodeset = xpathobj->nodesetval))
  490. {
  491. log_error1("No Body (nodeset)!");
  492. xmlXPathFreeObject(xpathobj);
  493. return NULL;
  494. }
  495. if (nodeset->nodeNr < 1)
  496. {
  497. log_error1("No Body (nodeNr)!");
  498. xmlXPathFreeObject(xpathobj);
  499. return NULL;
  500. }
  501. body = nodeset->nodeTab[0]; /* body is <Body> */
  502. xmlXPathFreeObject(xpathobj);
  503. return body;
  504. }
  505. char *
  506. soap_env_find_urn(SoapEnv * env)
  507. {
  508. xmlNsPtr ns;
  509. xmlNodePtr body, node;
  510. if (!(body = soap_env_get_body(env)))
  511. {
  512. log_verbose1("body is NULL");
  513. return 0;
  514. }
  515. /* node is the first child */
  516. if (!(node = soap_xml_get_children(body)))
  517. {
  518. log_error1("No namespace found");
  519. return 0;
  520. }
  521. /* if (node->ns && node->ns->prefix) MRC 1/25/2006 */
  522. if (node->ns)
  523. {
  524. ns = xmlSearchNs(body->doc, node, node->ns->prefix);
  525. if (ns != NULL)
  526. {
  527. return((char *) ns->href); /* namespace found! */
  528. }
  529. }
  530. else
  531. {
  532. static char *empty = "";
  533. log_warn1("No namespace found");
  534. return(empty);
  535. }
  536. return 0;
  537. }
  538. char *
  539. soap_env_find_methodname(SoapEnv * env)
  540. {
  541. xmlNodePtr body, node;
  542. body = soap_env_get_body(env);
  543. if (body == NULL)
  544. return 0;
  545. node = soap_xml_get_children(body); /* node is the first child */
  546. if (node == NULL)
  547. {
  548. log_error1("No method found");
  549. return 0;
  550. }
  551. if (node->name == NULL)
  552. {
  553. log_error1("No methodname found");
  554. return 0;
  555. }
  556. return((char *) node->name);
  557. }