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.

261 lines
5.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* soapclient.c
  2. // compile with
  3. // gcc soapclient.c -o soapclient -lcsoap
  4. */
  5. /* Author: Adrianus Warmenhoven */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <nanohttp/nanohttp-logging.h>
  10. #include <libcsoap/soap-client.h>
  11. #define MAX_LINE_LENGTH 65535
  12. /* stripcslashes - unescapes the string */
  13. void
  14. stripcslashes(char *str, int *len)
  15. {
  16. char *source, *target, *end;
  17. int nlen = *len, i;
  18. char numtmp[4];
  19. for (source = str, end = str + nlen, target = str; source < end; source++)
  20. {
  21. if (*source == '\\' && source + 1 < end)
  22. {
  23. source++;
  24. switch (*source)
  25. {
  26. case 'n':
  27. *target++ = '\n';
  28. nlen--;
  29. break;
  30. case 'r':
  31. *target++ = '\r';
  32. nlen--;
  33. break;
  34. case 'a':
  35. *target++ = '\a';
  36. nlen--;
  37. break;
  38. case 't':
  39. *target++ = '\t';
  40. nlen--;
  41. break;
  42. case 'v':
  43. *target++ = '\v';
  44. nlen--;
  45. break;
  46. case 'b':
  47. *target++ = '\b';
  48. nlen--;
  49. break;
  50. case 'f':
  51. *target++ = '\f';
  52. nlen--;
  53. break;
  54. case '\\':
  55. *target++ = '\\';
  56. nlen--;
  57. break;
  58. case 'x':
  59. if (source + 1 < end && isxdigit((int) (*(source + 1))))
  60. {
  61. numtmp[0] = *++source;
  62. if (source + 1 < end && isxdigit((int) (*(source + 1))))
  63. {
  64. numtmp[1] = *++source;
  65. numtmp[2] = '\0';
  66. nlen -= 3;
  67. }
  68. else
  69. {
  70. numtmp[1] = '\0';
  71. nlen -= 2;
  72. }
  73. *target++ = (char) strtol(numtmp, NULL, 16);
  74. break;
  75. }
  76. default:
  77. i = 0;
  78. while (source < end && *source >= '0' && *source <= '7' && i < 3)
  79. {
  80. numtmp[i++] = *source++;
  81. }
  82. if (i)
  83. {
  84. numtmp[i] = '\0';
  85. *target++ = (char) strtol(numtmp, NULL, 8);
  86. nlen -= i;
  87. source--;
  88. }
  89. else
  90. {
  91. *target++ = *source;
  92. nlen--;
  93. }
  94. }
  95. }
  96. else
  97. {
  98. *target++ = *source;
  99. }
  100. }
  101. if (nlen != 0)
  102. *target = '\0';
  103. *len = nlen;
  104. }
  105. /* ParseLine - gets a line, finds the commas, unescapes the value and adds it
  106. // to the SOAP request
  107. */
  108. void
  109. ParseLine(SoapCtx * ctx, char *Buffer, int LineLen)
  110. {
  111. char *Line, *FirstCommaPos, *SecondCommaPos;
  112. int len;
  113. /* if wrong line length, return */
  114. if (LineLen <= 0)
  115. return;
  116. /* alloc buffer for the line, copy it */
  117. Line = (char *) malloc(LineLen + 1);
  118. memcpy(Line, Buffer, LineLen);
  119. Line[LineLen] = '\0';
  120. /* find first comma */
  121. FirstCommaPos = strchr(Line, ',');
  122. if (!FirstCommaPos)
  123. return;
  124. /* find second comma */
  125. SecondCommaPos = strchr(FirstCommaPos + 1, ',');
  126. if (!SecondCommaPos)
  127. return;
  128. /* separate the three strings */
  129. FirstCommaPos[0] = '\0';
  130. SecondCommaPos[0] = '\0';
  131. /* unescape */
  132. len = strlen(SecondCommaPos + 1);
  133. stripcslashes(SecondCommaPos + 1, &len);
  134. /* add to the request */
  135. soap_env_add_item(ctx->env, Line, FirstCommaPos + 1, SecondCommaPos + 1);
  136. /* free the buffer */
  137. free(Line);
  138. }
  139. void
  140. printusage(char *FileName)
  141. {
  142. printf("Usage: %s [URL] [URN] [SOAPMETHOD]\n\n", FileName);
  143. printf("- [URL] is the url of the SOAP server.\n");
  144. printf("- [URN] is the namespace for the method/webservice.\n");
  145. printf("- [SOAPMETHOD] is the method to call.\n\n");
  146. printf("All commandline paramaters are mandatory.\n\n");
  147. printf
  148. ("%s reads the parameters for the SOAPMETHOD from STDIN as a comma-separated list\n\n",
  149. FileName);
  150. printf("Each line is one parameter and has the following form:\n\n");
  151. printf("xsd:[TYPE],[PARAMETERNAME],[PARAMETERVALUE]\n\n");
  152. printf
  153. ("- [TYPE] is a basic type for SOAP or a complex type defined by the WSDL.\n");
  154. printf
  155. ("- [PARAMETERNAME] is the name for the parameter of the SOAPMETHOD on this line.\n");
  156. printf
  157. ("- [PARAMETERVALUE] is the value for the parameter. N.B. \nand other escapes are recognized.\n");
  158. }
  159. int
  160. main(int argc, char *argv[])
  161. {
  162. SoapCtx *ctx, *ctx2;
  163. herror_t err;
  164. /* create buffer */
  165. char Buffer[MAX_LINE_LENGTH];
  166. long bytes_read, bytes_left;
  167. char *EndLinePos;
  168. /* check the parameter count */
  169. if (argc != 4)
  170. {
  171. printusage(argv[0]);
  172. return 1;
  173. }
  174. /* init cSOAP client */
  175. err = soap_client_init_args(argc, argv);
  176. if (err != H_OK)
  177. {
  178. log_error4("%s():%s [%d]", herror_func(err),
  179. herror_message(err), herror_code(err));
  180. herror_release(err);
  181. return 1;
  182. }
  183. /* create a SoapCtx object */
  184. err = soap_ctx_new_with_method(argv[2], argv[3], &ctx);
  185. if (err != H_OK)
  186. {
  187. log_error4("%s():%s [%d]", herror_func(err),
  188. herror_message(err), herror_code(err));
  189. herror_release(err);
  190. return 1;
  191. }
  192. /* read from stdin until EOF */
  193. while (!feof(stdin))
  194. {
  195. bytes_read = fread(Buffer, 1, MAX_LINE_LENGTH, stdin);
  196. /* pass each line into ParseLine */
  197. while ((EndLinePos = strchr(Buffer, '\n')))
  198. {
  199. ParseLine(ctx, Buffer, EndLinePos - Buffer);
  200. memmove(Buffer, EndLinePos + 1, bytes_read - (EndLinePos - Buffer + 1));
  201. Buffer[bytes_read - (EndLinePos - Buffer + 1)] = '\0';
  202. }
  203. /* no '\n' found in the whole Buffer, that means line's too */
  204. bytes_left = strlen(Buffer);
  205. if (bytes_left == MAX_LINE_LENGTH)
  206. {
  207. log_error1("The parameter line is too long.");
  208. herror_release(err);
  209. soap_ctx_free(ctx);
  210. return 1;
  211. }
  212. }
  213. /* invoke */
  214. err = soap_client_invoke(ctx, &ctx2, argv[1], "");
  215. if (err != H_OK)
  216. {
  217. log_error4("[%d] %s(): %s ", herror_code(err),
  218. herror_func(err), herror_message(err));
  219. herror_release(err);
  220. soap_ctx_free(ctx);
  221. return 1;
  222. }
  223. /* print the result */
  224. soap_xml_doc_print(ctx2->env->root->doc);
  225. /* free the objects */
  226. soap_ctx_free(ctx2);
  227. soap_ctx_free(ctx);
  228. /* destroy the cSOAP client */
  229. soap_client_destroy();
  230. return 0;
  231. }