webclient_file.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <rtthread.h>
  4. #include "webclient.h"
  5. #include "webclient_internal.h"
  6. #ifdef RT_USING_FINSH
  7. #include <finsh.h>
  8. #endif
  9. #ifdef RT_USING_DFS
  10. #include <dfs_posix.h>
  11. int webclient_get_file(const char* URI, const char* filename)
  12. {
  13. int fd = -1;
  14. size_t offset;
  15. size_t length, total_length = 0;
  16. rt_uint8_t* ptr = NULL;
  17. struct webclient_session* session = NULL;
  18. session = webclient_open(URI);
  19. if (session == NULL)
  20. {
  21. rt_kprintf("open website failed.\n");
  22. goto __exit;
  23. }
  24. if (session->response != 200)
  25. {
  26. rt_kprintf("wrong response: %d\n", session->response);
  27. goto __exit;
  28. }
  29. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
  30. if (fd < 0)
  31. {
  32. rt_kprintf("open file failed\n");
  33. goto __exit;
  34. }
  35. ptr = web_malloc(WEBCLIENT_RESPONSE_BUFSZ);
  36. if (ptr == NULL)
  37. {
  38. rt_kprintf("out of memory\n");
  39. goto __exit;
  40. }
  41. if (session->content_length == 0)
  42. {
  43. while (1)
  44. {
  45. length = webclient_read(session, ptr, WEBCLIENT_RESPONSE_BUFSZ);
  46. if (length > 0)
  47. {
  48. write(fd, ptr, length);
  49. total_length += length;
  50. rt_kprintf(">");
  51. }
  52. else
  53. break;
  54. }
  55. }
  56. else
  57. {
  58. for (offset = 0; offset < session->content_length;)
  59. {
  60. length = webclient_read(session, ptr,
  61. session->content_length - offset > WEBCLIENT_RESPONSE_BUFSZ ?
  62. WEBCLIENT_RESPONSE_BUFSZ : session->content_length - offset);
  63. if (length > 0)
  64. {
  65. write(fd, ptr, length);
  66. total_length += length;
  67. rt_kprintf(">");
  68. }
  69. else
  70. break;
  71. offset += length;
  72. }
  73. }
  74. if (total_length)
  75. {
  76. rt_kprintf("\nSave %d bytes\n", total_length);
  77. }
  78. __exit:
  79. if (fd >= 0) close(fd);
  80. if (session != NULL) webclient_close(session);
  81. if (ptr != NULL) web_free(ptr);
  82. return 0;
  83. }
  84. int webclient_post_file(const char* URI, const char* filename,
  85. const char* form_data)
  86. {
  87. size_t length;
  88. char boundary[60];
  89. int fd = -1, rc = WEBCLIENT_OK;
  90. char *header = NULL, *header_ptr;
  91. unsigned char *buffer = NULL, *buffer_ptr;
  92. struct webclient_session* session = NULL;
  93. fd = open(filename, O_RDONLY, 0);
  94. if (fd < 0)
  95. {
  96. rc = -WEBCLIENT_FILE_ERROR;
  97. goto __exit;
  98. }
  99. /* get the size of file */
  100. length = lseek(fd, 0, SEEK_END);
  101. lseek(fd, 0, SEEK_SET);
  102. buffer = web_malloc(WEBCLIENT_RESPONSE_BUFSZ);
  103. if (buffer == NULL)
  104. {
  105. rc = -WEBCLIENT_NOMEM;
  106. goto __exit;
  107. }
  108. session = (struct webclient_session*) web_malloc(sizeof(struct webclient_session));
  109. if (!session)
  110. {
  111. rc = -WEBCLIENT_NOMEM;
  112. goto __exit;
  113. }
  114. memset(session, 0x0, sizeof(struct webclient_session));
  115. rc = webclient_connect(session, URI);
  116. if (rc < 0)
  117. goto __exit;
  118. header = (char*) web_malloc(WEBCLIENT_HEADER_BUFSZ);
  119. if (header == NULL)
  120. {
  121. rc = -WEBCLIENT_NOMEM;
  122. goto __exit;
  123. }
  124. header_ptr = header;
  125. /* build boundary */
  126. rt_snprintf(boundary, sizeof(boundary), "----------------------------%012d",
  127. rt_tick_get());
  128. /* build encapsulated mime_multipart information*/
  129. buffer_ptr = buffer;
  130. /* first boundary */
  131. buffer_ptr += rt_snprintf((char*) buffer_ptr,
  132. WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer), "--%s\r\n", boundary);
  133. buffer_ptr += rt_snprintf((char*) buffer_ptr,
  134. WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer),
  135. "Content-Disposition: form-data; %s\r\n", form_data);
  136. buffer_ptr += rt_snprintf((char*) buffer_ptr,
  137. WEBCLIENT_RESPONSE_BUFSZ - (buffer_ptr - buffer),
  138. "Content-Type: application/octet-stream\r\n\r\n");
  139. /* calculate content-length */
  140. length += buffer_ptr - buffer;
  141. length += strlen(boundary) + 6; /* add the last boundary */
  142. /* build header for upload */
  143. header_ptr += rt_snprintf(header_ptr,
  144. WEBCLIENT_HEADER_BUFSZ - (header_ptr - header),
  145. "Content-Length: %d\r\n", length);
  146. header_ptr += rt_snprintf(header_ptr,
  147. WEBCLIENT_HEADER_BUFSZ - (header_ptr - header),
  148. "Content-Type: multipart/form-data; boundary=%s\r\n", boundary);
  149. /* send header */
  150. rc = webclient_send_header(session, WEBCLIENT_POST, header,
  151. header_ptr - header);
  152. if (rc < 0)
  153. goto __exit;
  154. /* send mime_multipart */
  155. webclient_write(session, buffer, buffer_ptr - buffer);
  156. /* send file data */
  157. while (1)
  158. {
  159. length = read(fd, buffer, WEBCLIENT_RESPONSE_BUFSZ);
  160. if (length <= 0)
  161. break;
  162. webclient_write(session, buffer, length);
  163. }
  164. /* send last boundary */
  165. rt_snprintf((char*) buffer, WEBCLIENT_RESPONSE_BUFSZ, "\r\n--%s--\r\n", boundary);
  166. webclient_write(session, buffer, strlen(boundary) + 6);
  167. __exit:
  168. if (fd >= 0) close(fd);
  169. if (session != NULL) webclient_close(session);
  170. if (buffer != NULL) web_free(buffer);
  171. if (header != NULL) web_free(header);
  172. return 0;
  173. }
  174. int wget(int argc, char** argv)
  175. {
  176. if (argc != 3)
  177. {
  178. rt_kprintf("wget URI filename\n");
  179. return 0;
  180. }
  181. webclient_get_file(argv[1], argv[2]);
  182. return 0;
  183. }
  184. MSH_CMD_EXPORT(wget, web download file);
  185. #endif