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.

185 lines
4.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: client_mime.c,v 1.1 2004/10/15 13:42:07 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C (example)
  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: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #include <nanohttp/nanohttp-client.h>
  25. #include <stdio.h>
  26. #define MAX_BUFFER_SIZE 1024
  27. static
  28. void show_response(hresponse_t *res)
  29. {
  30. hpair_t *pair;
  31. byte_t buffer[MAX_BUFFER_SIZE+1];
  32. int read;
  33. if (res == NULL)
  34. {
  35. log_error1("Response is NULL!");
  36. return;
  37. }
  38. log_info2("Spec : '%s'", res->spec);
  39. log_info2("Status: %d", res->errcode);
  40. log_info2("Desc : '%s'", res->desc);
  41. pair = res->header;
  42. while (pair != NULL) {
  43. log_debug3("%s: %s", pair->key, pair->value);
  44. pair = pair->next;
  45. }
  46. if (res->in == NULL)
  47. {
  48. log_warn1("No input stream!");
  49. return;
  50. }
  51. while (http_input_stream_is_ready(res->in))
  52. {
  53. read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE);
  54. buffer[read] = '\0';
  55. puts(buffer);
  56. }
  57. }
  58. static
  59. int send_file(httpc_conn_t *conn, const char* filename,
  60. const char* id, const char* content_type)
  61. {
  62. int status, size;
  63. FILE *f = fopen(filename, "r");
  64. char buffer[MAX_BUFFER_SIZE];
  65. if (f == NULL)
  66. {
  67. log_error2("can not open file: '%s'", filename);
  68. return 0;
  69. }
  70. status = httpc_mime_next(conn, id, content_type, "binary");
  71. if (status != HSOCKET_OK)
  72. return status;
  73. while (!feof(f)) {
  74. size = fread(buffer, 1, MAX_BUFFER_SIZE, f);
  75. if (size == -1)
  76. {
  77. log_error1("Can not read file!");
  78. fclose(f);
  79. return 0;
  80. }
  81. status = http_output_stream_write(conn->out, buffer, size);
  82. if (status != HSOCKET_OK)
  83. {
  84. log_error1("Can not send data!");
  85. fclose(f);
  86. return 0;
  87. }
  88. }
  89. fclose(f);
  90. return 1;
  91. }
  92. int main(int argc, char *argv[])
  93. {
  94. httpc_conn_t *conn; /* Client connection object */
  95. hresponse_t *res; /* Response object **/
  96. int status;
  97. FILE *f;
  98. size_t size;
  99. char url[50], file[50], id[50], content_type[50];
  100. char buffer[MAX_BUFFER_SIZE+1];
  101. /* Check usage */
  102. if (argc < 5) {
  103. fprintf(stderr, "usage %s <url> <file> <id> <content-type>\n", argv[0]);
  104. exit(1);
  105. }
  106. /* Set log level to see more information written by the library */
  107. log_set_level(HLOG_VERBOSE);
  108. /* Initialize httpc module */
  109. if (httpc_init(argc, argv))
  110. {
  111. log_error1("Can not init httpc");
  112. return 1;
  113. }
  114. /* Create the client connection object */
  115. conn = httpc_new();
  116. httpc_set_header(conn, "SOAPAction", "\"\"");
  117. httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED);
  118. /*
  119. Open connection for mime
  120. */
  121. status = httpc_mime_begin(conn, argv[1], argv[3], "", argv[4]);
  122. if (status != HSOCKET_OK)
  123. {
  124. log_error2("Can not start MIME: %d", status);
  125. exit(1);
  126. }
  127. if (!send_file(conn, argv[2], argv[3], argv[4]))
  128. return 1;
  129. while (1)
  130. {
  131. printf("Enter filename ['.' for finish]: ");
  132. gets(file);
  133. if (!strcmp(file, "."))
  134. break;
  135. printf("Enter part id:");
  136. gets(id);
  137. printf("Enter content-type:");
  138. gets(content_type);
  139. if (!send_file(conn, file, id, content_type))
  140. return 1;
  141. }
  142. res = httpc_mime_end(conn);
  143. /* Show response */
  144. show_response(res);
  145. /* clean up*/
  146. hresponse_free(res);
  147. httpc_free(conn);
  148. return 0;
  149. }