webclient.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-05-05 Bernard the first version
  9. * 2013-06-10 Bernard fix the slow speed issue when download file.
  10. * 2015-11-14 aozima add content_length_remainder.
  11. * 2017-12-23 aozima update gethostbyname to getaddrinfo.
  12. * 2018-01-04 aozima add ipv6 address support.
  13. * 2018-07-26 chenyong modify log information
  14. * 2018-08-07 chenyong modify header processing
  15. */
  16. #ifndef __WEBCLIENT_H__
  17. #define __WEBCLIENT_H__
  18. #include <stddef.h>
  19. #include "pika_adapter_rtt.h"
  20. /* depend on the socket module */
  21. #include "../socket/PikaPlatform_socket.h"
  22. #define RT_USING_SAL 1
  23. #if defined(WEBCLIENT_USING_MBED_TLS) || defined(WEBCLIENT_USING_SAL_TLS)
  24. #include <tls_client.h>
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #ifndef web_malloc
  30. #define web_malloc rt_malloc
  31. #endif
  32. #ifndef web_calloc
  33. #define web_calloc rt_calloc
  34. #endif
  35. #ifndef web_realloc
  36. #define web_realloc rt_realloc
  37. #endif
  38. #ifndef web_free
  39. #define web_free rt_free
  40. #endif
  41. /**
  42. * The gcc libc api is not threadsafe,
  43. * especially the float type operation.
  44. * So, use rt_xxx whose RT-Thread threadsafe api to instead of strandard libc
  45. * api.
  46. */
  47. #ifndef web_memset
  48. #define web_memset rt_memset
  49. #endif
  50. #ifndef web_memcpy
  51. #define web_memcpy rt_memcpy
  52. #endif
  53. #ifndef web_memcmp
  54. #define web_memcmp rt_memcmp
  55. #endif
  56. #ifndef web_snprintf
  57. #define web_snprintf rt_snprintf
  58. #endif
  59. #ifndef web_vsnprintf
  60. #define web_vsnprintf rt_vsnprintf
  61. #endif
  62. #ifndef web_strdup
  63. #define web_strdup rt_strdup
  64. #endif
  65. #define WEBCLIENT_SW_VERSION "2.3.0"
  66. #define WEBCLIENT_SW_VERSION_NUM 0x20300
  67. #define WEBCLIENT_HEADER_BUFSZ 4096
  68. #define WEBCLIENT_RESPONSE_BUFSZ 4096
  69. enum WEBCLIENT_STATUS {
  70. WEBCLIENT_OK,
  71. WEBCLIENT_ERROR,
  72. WEBCLIENT_TIMEOUT,
  73. WEBCLIENT_NOMEM,
  74. WEBCLIENT_NOSOCKET,
  75. WEBCLIENT_NOBUFFER,
  76. WEBCLIENT_CONNECT_FAILED,
  77. WEBCLIENT_DISCONNECT,
  78. WEBCLIENT_FILE_ERROR,
  79. };
  80. enum WEBCLIENT_METHOD {
  81. WEBCLIENT_USER_METHOD,
  82. WEBCLIENT_GET,
  83. WEBCLIENT_POST,
  84. WEBCLIENT_HEAD
  85. };
  86. struct webclient_header {
  87. char* buffer;
  88. size_t length; /* content header buffer size */
  89. size_t size; /* maximum support header size */
  90. };
  91. struct webclient_session {
  92. struct webclient_header* header; /* webclient response header information */
  93. int socket;
  94. int resp_status;
  95. char* host; /* server host */
  96. char* req_url; /* HTTP request address*/
  97. int chunk_sz;
  98. int chunk_offset;
  99. int content_length;
  100. size_t content_remainder; /* remainder of content length */
  101. int (*handle_function)(char* buffer, int size); /* handle function */
  102. rt_bool_t is_tls; /* HTTPS connect */
  103. #ifdef WEBCLIENT_USING_MBED_TLS
  104. MbedTLSSession* tls_session; /* mbedtls connect session */
  105. #endif
  106. };
  107. /* create webclient session and set header response size */
  108. struct webclient_session* webclient_session_create(size_t header_sz);
  109. /* send HTTP GET request */
  110. int webclient_get(struct webclient_session* session, const char* URI);
  111. /* send HTTP GET request */
  112. int webclient_get2(struct webclient_session* session, const char* URI);
  113. /* send HTTP HEAD request */
  114. int webclient_shard_head_function(struct webclient_session* session,
  115. const char* URI,
  116. int* length);
  117. /* send HTTP Range parameter, shard download */
  118. int webclient_shard_position_function(struct webclient_session* session,
  119. const char* URI,
  120. int start,
  121. int length,
  122. int mem_size);
  123. int* webclient_register_shard_position_function(
  124. struct webclient_session* session,
  125. int (*handle_function)(char* buffer, int size));
  126. /* send HTTP POST request */
  127. int webclient_post(struct webclient_session* session,
  128. const char* URI,
  129. const void* post_data,
  130. size_t data_len);
  131. /* send HTTP POST request */
  132. int webclient_post2(struct webclient_session* session,
  133. const char* URI,
  134. const void* post_data,
  135. size_t data_len);
  136. /* close and release wenclient session */
  137. int webclient_close(struct webclient_session* session);
  138. int webclient_set_timeout(struct webclient_session* session, int millisecond);
  139. /* send or receive data from server */
  140. int webclient_read(struct webclient_session* session,
  141. void* buffer,
  142. size_t size);
  143. int webclient_write(struct webclient_session* session,
  144. const void* buffer,
  145. size_t size);
  146. /* webclient GET/POST header buffer operate by the header fields */
  147. int webclient_header_fields_add(struct webclient_session* session,
  148. const char* fmt,
  149. ...);
  150. const char* webclient_header_fields_get(struct webclient_session* session,
  151. const char* fields);
  152. /* send HTTP POST/GET request, and get response data */
  153. int webclient_response(struct webclient_session* session,
  154. void** response,
  155. size_t* resp_len);
  156. int webclient_request(const char* URI,
  157. const char* header,
  158. const void* post_data,
  159. size_t data_len,
  160. void** response,
  161. size_t* resp_len);
  162. int webclient_request_header_add(char** request_header, const char* fmt, ...);
  163. int webclient_resp_status_get(struct webclient_session* session);
  164. int webclient_content_length_get(struct webclient_session* session);
  165. #ifdef RT_USING_DFS
  166. /* file related operations */
  167. int webclient_get_file(const char* URI, const char* filename);
  168. int webclient_post_file(const char* URI,
  169. const char* filename,
  170. const char* form_data);
  171. #endif
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #endif