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.

241 lines
5.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: soap-client.c,v 1.27 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_STDIO_H
  31. #include <stdio.h>
  32. #endif
  33. #include <nanohttp/nanohttp-logging.h>
  34. #include <nanohttp/nanohttp-client.h>
  35. #include "soap-client.h"
  36. static herror_t
  37. _soap_client_build_result(hresponse_t * res, SoapEnv ** env)
  38. {
  39. log_verbose2("Building result (%p)", res);
  40. if (res == NULL)
  41. return herror_new("_soap_client_build_result",
  42. GENERAL_INVALID_PARAM, "hresponse_t is NULL");
  43. if (res->in == NULL)
  44. return herror_new("_soap_client_build_result",
  45. GENERAL_INVALID_PARAM, "Empty response from server");
  46. if (res->errcode != 200)
  47. return herror_new("_soap_client_build_result",
  48. GENERAL_INVALID_PARAM, "HTTP code is not OK (%i)", res->errcode);
  49. return soap_env_new_from_stream(res->in, env);
  50. }
  51. herror_t
  52. soap_client_init_args(int argc, char *argv[])
  53. {
  54. return httpc_init(argc, argv);
  55. }
  56. void
  57. soap_client_destroy(void)
  58. {
  59. httpc_destroy();
  60. return;
  61. }
  62. herror_t
  63. soap_client_invoke(SoapCtx * call, SoapCtx ** response, const char *url,
  64. const char *soap_action)
  65. {
  66. /* Status */
  67. herror_t status;
  68. /* Result document */
  69. SoapEnv *res_env;
  70. /* Buffer variables */
  71. xmlBufferPtr buffer;
  72. char *content;
  73. char tmp[15];
  74. /* Transport variables */
  75. httpc_conn_t *conn;
  76. hresponse_t *res;
  77. /* multipart/related start id */
  78. char start_id[150];
  79. static int counter = 1;
  80. part_t *part;
  81. /* for copy attachments */
  82. char href[MAX_HREF_SIZE];
  83. /* Create buffer */
  84. buffer = xmlBufferCreate();
  85. xmlNodeDump(buffer, call->env->root->doc, call->env->root, 1, 0);
  86. content = (char *) xmlBufferContent(buffer);
  87. /* Transport via HTTP */
  88. if (!(conn = httpc_new()))
  89. {
  90. return herror_new("soap_client_invoke", SOAP_ERROR_CLIENT_INIT,
  91. "Unable to create SOAP client!");
  92. }
  93. /* Set soap action */
  94. if (soap_action != NULL)
  95. httpc_set_header(conn, "SoapAction", soap_action);
  96. httpc_set_header(conn, HEADER_CONNECTION, "Close");
  97. /* check for attachments */
  98. if (!call->attachments)
  99. {
  100. /* content-type is always 'text/xml' */
  101. httpc_set_header(conn, HEADER_CONTENT_TYPE, "text/xml");
  102. sprintf(tmp, "%d", (int) strlen(content));
  103. httpc_set_header(conn, HEADER_CONTENT_LENGTH, tmp);
  104. if ((status = httpc_post_begin(conn, url)) != H_OK)
  105. {
  106. httpc_close_free(conn);
  107. xmlBufferFree(buffer);
  108. return status;
  109. }
  110. if ((status = http_output_stream_write_string(conn->out, content)) != H_OK)
  111. {
  112. httpc_close_free(conn);
  113. xmlBufferFree(buffer);
  114. return status;
  115. }
  116. if ((status = httpc_post_end(conn, &res)) != H_OK)
  117. {
  118. httpc_close_free(conn);
  119. xmlBufferFree(buffer);
  120. return status;
  121. }
  122. }
  123. else
  124. {
  125. /* Use chunked transport */
  126. httpc_set_header(conn, HEADER_TRANSFER_ENCODING,
  127. TRANSFER_ENCODING_CHUNKED);
  128. sprintf(start_id, "289247829121218%d", counter++);
  129. if ((status = httpc_mime_begin(conn, url, start_id, "", "text/xml")) != H_OK)
  130. {
  131. httpc_close_free(conn);
  132. xmlBufferFree(buffer);
  133. return status;
  134. }
  135. if ((status = httpc_mime_next(conn, start_id, "text/xml", "binary")) != H_OK)
  136. {
  137. httpc_close_free(conn);
  138. xmlBufferFree(buffer);
  139. return status;
  140. }
  141. if ((status = http_output_stream_write(conn->out, content, strlen(content))) != H_OK)
  142. {
  143. httpc_close_free(conn);
  144. xmlBufferFree(buffer);
  145. return status;
  146. }
  147. for (part = call->attachments->parts; part; part = part->next)
  148. {
  149. status = httpc_mime_send_file(conn, part->id,
  150. part->content_type,
  151. part->transfer_encoding, part->filename);
  152. if (status != H_OK)
  153. {
  154. log_error2("Send file failed. Status:%d", status);
  155. httpc_close_free(conn);
  156. xmlBufferFree(buffer);
  157. return status;
  158. }
  159. }
  160. if ((status = httpc_mime_end(conn, &res)) != H_OK)
  161. {
  162. httpc_close_free(conn);
  163. xmlBufferFree(buffer);
  164. return status;
  165. }
  166. }
  167. /* Free buffer */
  168. xmlBufferFree(buffer);
  169. /* Build result */
  170. if ((status = _soap_client_build_result(res, &res_env)) != H_OK)
  171. {
  172. hresponse_free(res);
  173. httpc_close_free(conn);
  174. return status;
  175. }
  176. /* Create Context */
  177. *response = soap_ctx_new(res_env);
  178. /* soap_ctx_add_files(*response, res->attachments);*/
  179. if (res->attachments != NULL)
  180. {
  181. part = res->attachments->parts;
  182. while (part)
  183. {
  184. soap_ctx_add_file(*response, part->filename, part->content_type, href);
  185. part->deleteOnExit = 0;
  186. part = part->next;
  187. }
  188. part = (*response)->attachments->parts;
  189. while (part)
  190. {
  191. part->deleteOnExit = 1;
  192. part = part->next;
  193. }
  194. }
  195. hresponse_free(res);
  196. httpc_close_free(conn);
  197. return H_OK;
  198. }