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.

469 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: soap-server.c,v 1.26 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_STRING_H
  28. #include <string.h>
  29. #endif
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #ifdef WIN32
  34. #define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2)
  35. #endif
  36. #include <nanohttp/nanohttp-logging.h>
  37. #include <nanohttp/nanohttp-server.h>
  38. #include "soap-admin.h"
  39. #include "soap-server.h"
  40. static SoapRouterNode *head = NULL;
  41. static SoapRouterNode *tail = NULL;
  42. // static SoapRouter *router_find(const char *context);
  43. static void
  44. _soap_server_send_env(http_output_stream_t * out, SoapEnv * env)
  45. {
  46. xmlBufferPtr buffer;
  47. if (env == NULL || env->root == NULL)
  48. return;
  49. buffer = xmlBufferCreate();
  50. xmlNodeDump(buffer, env->root->doc, env->root, 1, 1);
  51. http_output_stream_write_string(out,
  52. (const char *) xmlBufferContent(buffer));
  53. xmlBufferFree(buffer);
  54. return;
  55. }
  56. static void
  57. _soap_server_send_fault(httpd_conn_t * conn, const char *errmsg)
  58. {
  59. SoapEnv *envres;
  60. hpair_t *header;
  61. herror_t err;
  62. char buffer[45];
  63. header = hpairnode_new(HEADER_CONTENT_TYPE, "text/xml", NULL);
  64. httpd_set_headers(conn, header);
  65. if ((err = httpd_send_header(conn, 500, "FAILED")) != H_OK)
  66. {
  67. /* WARNING: unhandled exception ! */
  68. log_error4("%s():%s [%d]", herror_func(err), herror_message(err),
  69. herror_code(err));
  70. herror_release(err);
  71. return;
  72. }
  73. err = soap_env_new_with_fault(Fault_Server,
  74. errmsg ? errmsg : "General error",
  75. "cSOAP_Server", NULL, &envres);
  76. if (err != H_OK)
  77. {
  78. log_error1(herror_message(err));
  79. http_output_stream_write_string(conn->out, "<html><head></head><body>");
  80. http_output_stream_write_string(conn->out, "<h1>Error</h1><hr/>");
  81. http_output_stream_write_string(conn->out,
  82. "Error while sending fault object:<br>Message: ");
  83. http_output_stream_write_string(conn->out, herror_message(err));
  84. http_output_stream_write_string(conn->out, "<br />Function: ");
  85. http_output_stream_write_string(conn->out, herror_func(err));
  86. http_output_stream_write_string(conn->out, "<br />Error code: ");
  87. sprintf(buffer, "%d", herror_code(err));
  88. http_output_stream_write_string(conn->out, buffer);
  89. http_output_stream_write_string(conn->out, "</body></html>");
  90. herror_release(err);
  91. }
  92. else
  93. {
  94. _soap_server_send_env(conn->out, envres);
  95. }
  96. hpairnode_free(header);
  97. return;
  98. }
  99. static void
  100. _soap_server_send_ctx(httpd_conn_t * conn, SoapCtx * ctx)
  101. {
  102. static int counter = 1;
  103. xmlBufferPtr buffer;
  104. char strbuffer[32];
  105. part_t *part;
  106. if (ctx->env == NULL || ctx->env->root == NULL || ctx->env->root->doc == NULL)
  107. return;
  108. xmlThrDefIndentTreeOutput(1);
  109. /* xmlKeepBlanksDefault(0);*/
  110. buffer = xmlBufferCreate();
  111. xmlNodeDump(buffer, ctx->env->root->doc, ctx->env->root, 1, 1);
  112. if (ctx->attachments)
  113. {
  114. sprintf(strbuffer, "000128590350940924234%d", counter++);
  115. httpd_mime_send_header(conn, strbuffer, "", "text/xml", 200, "OK");
  116. httpd_mime_next(conn, strbuffer, "text/xml", "binary");
  117. http_output_stream_write_string(conn->out,
  118. (const char *) xmlBufferContent(buffer));
  119. part = ctx->attachments->parts;
  120. while (part)
  121. {
  122. httpd_mime_send_file(conn, part->id, part->content_type,
  123. part->transfer_encoding, part->filename);
  124. part = part->next;
  125. }
  126. httpd_mime_end(conn);
  127. }
  128. else
  129. {
  130. char buflen[100];
  131. xmlXPathContextPtr xpathCtx;
  132. xmlXPathObjectPtr xpathObj;
  133. xpathCtx = xmlXPathNewContext(ctx->env->root->doc);
  134. xpathObj = xmlXPathEvalExpression("//Fault", xpathCtx);
  135. snprintf(buflen, 100, "%d", xmlBufferLength(buffer));
  136. httpd_set_header(conn, HEADER_CONTENT_LENGTH, buflen);
  137. httpd_set_header(conn, HEADER_CONTENT_TYPE, "text/xml");
  138. if ((xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0)
  139. {
  140. httpd_send_header(conn, 500, "FAILED");
  141. }
  142. else
  143. {
  144. httpd_send_header(conn, 200, "OK");
  145. }
  146. http_output_stream_write_string(conn->out,
  147. (const char *) xmlBufferContent(buffer));
  148. xmlXPathFreeObject(xpathObj);
  149. xmlXPathFreeContext(xpathCtx);
  150. }
  151. xmlBufferFree(buffer);
  152. return;
  153. }
  154. static void
  155. _soap_server_send_description(httpd_conn_t *conn, xmlDocPtr wsdl)
  156. {
  157. char length[16];
  158. xmlBufferPtr buf;
  159. buf = xmlBufferCreate();
  160. xmlNodeDump(buf, wsdl, xmlDocGetRootElement(wsdl), 0, 0);
  161. sprintf(length, "%d", xmlBufferLength(buf));
  162. httpd_set_header(conn, HEADER_CONTENT_TYPE, "text/xml");
  163. httpd_set_header(conn, HEADER_CONTENT_LENGTH, length);
  164. httpd_send_header(conn, 200, "OK");
  165. http_output_stream_write_string(conn->out, xmlBufferContent(buf));
  166. xmlBufferFree(buf);
  167. return;
  168. }
  169. static SoapRouterNode *
  170. router_node_new(SoapRouter * router, const char *context, SoapRouterNode * next)
  171. {
  172. const char *noname = "/lost_found";
  173. SoapRouterNode *node;
  174. if (!(node = (SoapRouterNode *) malloc(sizeof(SoapRouterNode)))) {
  175. log_error2("malloc failed (%s)", strerror(errno));
  176. return NULL;
  177. }
  178. if (context)
  179. {
  180. node->context = strdup(context);
  181. }
  182. else
  183. {
  184. log_warn2("context is null. Using '%s'", noname);
  185. node->context = strdup(noname);
  186. }
  187. node->router = router;
  188. node->next = next;
  189. return node;
  190. }
  191. SoapRouter *
  192. soap_server_find_router(const char *context)
  193. {
  194. SoapRouterNode *node;
  195. for (node = head; node; node = node->next)
  196. {
  197. if (!strcmp(node->context, context))
  198. return node->router;
  199. }
  200. return NULL;
  201. }
  202. static void
  203. soap_server_entry(httpd_conn_t * conn, hrequest_t * req)
  204. {
  205. char buffer[1054];
  206. char *urn;
  207. char *method;
  208. SoapCtx *ctx, *ctxres;
  209. SoapRouter *router;
  210. SoapService *service;
  211. SoapEnv *env;
  212. herror_t err;
  213. if (!(router = soap_server_find_router(req->path)))
  214. {
  215. _soap_server_send_fault(conn, "Cannot find router");
  216. return;
  217. }
  218. else if (req->method == HTTP_REQUEST_GET && router->wsdl)
  219. {
  220. _soap_server_send_description(conn, router->wsdl);
  221. return;
  222. }
  223. if (req->method != HTTP_REQUEST_POST)
  224. {
  225. httpd_send_header(conn, 200, "OK");
  226. http_output_stream_write_string(conn->out,
  227. "<html>"
  228. "<head>"
  229. "</head>"
  230. "<body>"
  231. "<h1>Sorry!</h1>"
  232. "<hr />"
  233. "<div>I only speak with 'POST' method </div>"
  234. "</body>"
  235. "</html>");
  236. return;
  237. }
  238. if ((err = soap_env_new_from_stream(req->in, &env)) != H_OK)
  239. {
  240. _soap_server_send_fault(conn, herror_message(err));
  241. herror_release(err);
  242. return;
  243. }
  244. if (env == NULL)
  245. {
  246. _soap_server_send_fault(conn, "Can not receive POST data!");
  247. }
  248. else
  249. {
  250. ctx = soap_ctx_new(env);
  251. ctx->action = hpairnode_get_ignore_case(req->header, "SoapAction");
  252. if (ctx->action)
  253. ctx->action = strdup(ctx->action);
  254. ctx->http = req;
  255. soap_ctx_add_files(ctx, req->attachments);
  256. if (ctx->env == NULL)
  257. {
  258. _soap_server_send_fault(conn, "Can not parse POST data!");
  259. }
  260. else
  261. {
  262. /* soap_xml_doc_print(env->root->doc); */
  263. if (!(urn=soap_env_find_urn(ctx->env)))
  264. {
  265. _soap_server_send_fault(conn, "No URN found!");
  266. soap_ctx_free(ctx);
  267. return;
  268. }
  269. else
  270. {
  271. log_verbose2("urn: '%s'", urn);
  272. }
  273. if (!(method=soap_env_find_methodname(ctx->env)))
  274. {
  275. _soap_server_send_fault(conn, "No method found!");
  276. soap_ctx_free(ctx);
  277. return;
  278. }
  279. else
  280. {
  281. log_verbose2("method: '%s'", method);
  282. }
  283. service = soap_router_find_service(router, urn, method);
  284. if (service == NULL)
  285. {
  286. sprintf(buffer, "URN '%s' not found", urn);
  287. _soap_server_send_fault(conn, buffer);
  288. soap_ctx_free(ctx);
  289. return;
  290. }
  291. else
  292. {
  293. log_verbose2("func: %p", service->func);
  294. ctxres = soap_ctx_new(NULL);
  295. /* ===================================== */
  296. /* CALL SERVICE FUNCTION */
  297. /* ===================================== */
  298. if ((err = service->func(ctx, ctxres)) != H_OK)
  299. {
  300. sprintf(buffer, "Service returned following error message: '%s'",
  301. herror_message(err));
  302. herror_release(err);
  303. _soap_server_send_fault(conn, buffer);
  304. soap_ctx_free(ctx);
  305. return;
  306. }
  307. if (ctxres->env == NULL)
  308. {
  309. sprintf(buffer, "Service '%s' returned no envelope", urn);
  310. _soap_server_send_fault(conn, buffer);
  311. soap_ctx_free(ctx);
  312. return;
  313. }
  314. else
  315. {
  316. /* httpd_send_header(conn, 200, "OK");
  317. _soap_server_send_env(conn->out, ctxres->env);
  318. */
  319. _soap_server_send_ctx(conn, ctxres);
  320. /* free envctx */
  321. soap_ctx_free(ctxres);
  322. }
  323. }
  324. }
  325. soap_ctx_free(ctx);
  326. }
  327. }
  328. herror_t
  329. soap_server_init_args(int argc, char *argv[])
  330. {
  331. herror_t err;
  332. if ((err = httpd_init(argc, argv)) != H_OK)
  333. return err;
  334. return soap_admin_init_args(argc, argv);
  335. }
  336. int
  337. soap_server_register_router(SoapRouter * router, const char *context)
  338. {
  339. if (!httpd_register_secure(context, soap_server_entry, router->auth))
  340. {
  341. return 0;
  342. }
  343. if (tail == NULL)
  344. {
  345. head = tail = router_node_new(router, context, NULL);
  346. }
  347. else
  348. {
  349. tail->next = router_node_new(router, context, NULL);
  350. tail = tail->next;
  351. }
  352. return 1;
  353. }
  354. SoapRouterNode *
  355. soap_server_get_routers(void)
  356. {
  357. return head;
  358. }
  359. herror_t
  360. soap_server_run(void)
  361. {
  362. return httpd_run();
  363. }
  364. int
  365. soap_server_get_port(void)
  366. {
  367. return httpd_get_port();
  368. }
  369. const char *
  370. soap_server_get_protocol(void)
  371. {
  372. return httpd_get_protocol();
  373. }
  374. void
  375. soap_server_destroy()
  376. {
  377. SoapRouterNode *node = head;
  378. SoapRouterNode *tmp;
  379. while (node != NULL)
  380. {
  381. tmp = node->next;
  382. log_verbose2("soap_router_free(%p)", node->router);
  383. soap_router_free(node->router);
  384. free(node->context);
  385. free(node);
  386. node = tmp;
  387. }
  388. httpd_destroy();
  389. return;
  390. }