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.

198 lines
4.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: echoattachments-client.c,v 1.11 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: CSOAP examples project
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
  20. *
  21. * Email: ferhatayaz@yahoo.com
  22. ******************************************************************/
  23. #include <nanohttp/nanohttp-logging.h>
  24. #include <libcsoap/soap-client.h>
  25. static const char *urn = "urn:examples";
  26. static const char *url = "http://localhost:10000/echoattachments";
  27. static const char *method = "echo";
  28. void
  29. compareFiles(const char *received, const char *sent)
  30. {
  31. FILE *f1, *f2;
  32. char c1, c2;
  33. long s1, s2;
  34. printf("Opening received file to compare: '%s'\n", received);
  35. f1 = fopen(received, "r");
  36. if (!f1)
  37. {
  38. fprintf(stderr, "Can not open '%s'\n", received);
  39. return;
  40. }
  41. printf("Opening sent file to compare: '%s'\n", sent);
  42. f2 = fopen(sent, "r");
  43. if (!f2)
  44. {
  45. fprintf(stderr, "Can not open '%s'\n", sent);
  46. fclose(f1);
  47. return;
  48. }
  49. fseek(f1, 0, SEEK_END);
  50. fseek(f2, 0, SEEK_END);
  51. s1 = ftell(f1);
  52. s2 = ftell(f2);
  53. fseek(f1, 0, SEEK_SET);
  54. fseek(f2, 0, SEEK_SET);
  55. if (s1 > s2)
  56. {
  57. printf("ERROR: files are not equal! Received file is bigger\n");
  58. fclose(f1);
  59. fclose(f2);
  60. return;
  61. }
  62. else if (s2 > s1)
  63. {
  64. printf("ERROR: files are not equal! Sent file is bigger\n");
  65. fclose(f1);
  66. fclose(f2);
  67. return;
  68. }
  69. while (feof(f1))
  70. {
  71. c1 = fgetc(f1);
  72. c2 = fgetc(f2);
  73. if (c1 != c2)
  74. {
  75. printf("ERROR: files are not equal! Byte compare failed\n");
  76. fclose(f1);
  77. fclose(f2);
  78. break;
  79. }
  80. }
  81. printf("OK: files are equal!\n");
  82. fclose(f1);
  83. fclose(f2);
  84. }
  85. int
  86. main(int argc, char *argv[])
  87. {
  88. SoapCtx *ctx, *ctx2;
  89. char href[MAX_HREF_SIZE];
  90. xmlNodePtr fault;
  91. herror_t err;
  92. if (argc < 2)
  93. {
  94. fprintf(stderr, "usage: %s <filename> [url]\n", argv[0]);
  95. exit(1);
  96. }
  97. /*
  98. Initialize soap client */
  99. err = soap_client_init_args(argc, argv);
  100. if (err != H_OK)
  101. {
  102. log_error4("[%d] %s():%s ", herror_code(err), herror_func(err),
  103. herror_message(err));
  104. herror_release(err);
  105. return 1;
  106. }
  107. /*
  108. Create a context object */
  109. err = soap_ctx_new_with_method(urn, method, &ctx);
  110. if (err != H_OK)
  111. {
  112. log_error4("[%d] %s():%s ", herror_code(err), herror_func(err),
  113. herror_message(err));
  114. herror_release(err);
  115. return 1;
  116. }
  117. /*
  118. Add file to the context */
  119. err = soap_ctx_add_file(ctx, argv[1], "application/octet-stream", href);
  120. if (err != H_OK)
  121. {
  122. log_error4("%s():%s [%d]", herror_func(err), herror_message(err),
  123. herror_code(err));
  124. herror_release(err);
  125. return 1;
  126. }
  127. /*
  128. Add file reference to the envelope */
  129. soap_env_add_attachment(ctx->env, "source", href);
  130. /*
  131. Send soap request to the server */
  132. printf("sending request ...\n");
  133. if (argc > 2)
  134. err = soap_client_invoke(ctx, &ctx2, argv[2], "");
  135. else
  136. err = soap_client_invoke(ctx, &ctx2, url, "");
  137. if (err != H_OK)
  138. {
  139. log_error4("%s():%s [%d]", herror_func(err), herror_message(err),
  140. herror_code(err));
  141. herror_release(err);
  142. return 1;
  143. }
  144. /*
  145. Handle response (just print to the screen) */
  146. fault = soap_env_get_fault(ctx2->env);
  147. if (fault)
  148. {
  149. soap_xml_doc_print(ctx2->env->root->doc);
  150. }
  151. else if (ctx2->attachments)
  152. {
  153. compareFiles(ctx2->attachments->parts->filename, argv[1]);
  154. }
  155. else
  156. {
  157. printf("No attachments!");
  158. soap_xml_doc_print(ctx2->env->root->doc);
  159. }
  160. /*
  161. Clean up */
  162. soap_ctx_free(ctx2);
  163. soap_ctx_free(ctx);
  164. soap_client_destroy();
  165. return 0;
  166. }