webclient.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <rtthread.h>
  19. #include <stddef.h>
  20. #if defined(WEBCLIENT_USING_MBED_TLS) || defined(WEBCLIENT_USING_SAL_TLS)
  21. #include <tls_client.h>
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #ifndef web_malloc
  27. #define web_malloc rt_malloc
  28. #endif
  29. #ifndef web_calloc
  30. #define web_calloc rt_calloc
  31. #endif
  32. #ifndef web_realloc
  33. #define web_realloc rt_realloc
  34. #endif
  35. #ifndef web_free
  36. #define web_free rt_free
  37. #endif
  38. /**
  39. * The gcc libc api is not threadsafe,
  40. * especially the float type operation.
  41. * So, use rt_xxx whose RT-Thread threadsafe api to instead of strandard libc api.
  42. */
  43. #ifndef web_memset
  44. #define web_memset rt_memset
  45. #endif
  46. #ifndef web_memcpy
  47. #define web_memcpy rt_memcpy
  48. #endif
  49. #ifndef web_memcmp
  50. #define web_memcmp rt_memcmp
  51. #endif
  52. #ifndef web_snprintf
  53. #define web_snprintf rt_snprintf
  54. #endif
  55. #ifndef web_vsnprintf
  56. #define web_vsnprintf rt_vsnprintf
  57. #endif
  58. #ifndef web_strdup
  59. #define web_strdup rt_strdup
  60. #endif
  61. #define WEBCLIENT_SW_VERSION "2.3.0"
  62. #define WEBCLIENT_SW_VERSION_NUM 0x20300
  63. #define WEBCLIENT_HEADER_BUFSZ 4096
  64. #define WEBCLIENT_RESPONSE_BUFSZ 4096
  65. enum WEBCLIENT_STATUS
  66. {
  67. WEBCLIENT_OK,
  68. WEBCLIENT_ERROR,
  69. WEBCLIENT_TIMEOUT,
  70. WEBCLIENT_NOMEM,
  71. WEBCLIENT_NOSOCKET,
  72. WEBCLIENT_NOBUFFER,
  73. WEBCLIENT_CONNECT_FAILED,
  74. WEBCLIENT_DISCONNECT,
  75. WEBCLIENT_FILE_ERROR,
  76. };
  77. enum WEBCLIENT_METHOD
  78. {
  79. WEBCLIENT_USER_METHOD,
  80. WEBCLIENT_GET,
  81. WEBCLIENT_POST,
  82. WEBCLIENT_HEAD
  83. };
  84. struct webclient_header
  85. {
  86. char *buffer;
  87. size_t length; /* content header buffer size */
  88. size_t size; /* maximum support header size */
  89. };
  90. struct webclient_session
  91. {
  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 HEAD request */
  112. int webclient_shard_head_function(struct webclient_session *session, const char *URI, int *length);
  113. /* send HTTP Range parameter, shard download */
  114. int webclient_shard_position_function(struct webclient_session *session, const char *URI, int start, int length, int mem_size);
  115. int *webclient_register_shard_position_function(struct webclient_session *session, int (*handle_function)(char *buffer, int size));
  116. /* send HTTP POST request */
  117. int webclient_post(struct webclient_session *session, const char *URI, const void *post_data, size_t data_len);
  118. /* close and release wenclient session */
  119. int webclient_close(struct webclient_session *session);
  120. int webclient_set_timeout(struct webclient_session *session, int millisecond);
  121. /* send or receive data from server */
  122. int webclient_read(struct webclient_session *session, void *buffer, size_t size);
  123. int webclient_write(struct webclient_session *session, const void *buffer, size_t size);
  124. /* webclient GET/POST header buffer operate by the header fields */
  125. int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...);
  126. const char *webclient_header_fields_get(struct webclient_session *session, const char *fields);
  127. /* send HTTP POST/GET request, and get response data */
  128. int webclient_response(struct webclient_session *session, void **response, size_t *resp_len);
  129. int webclient_request(const char *URI, const char *header, const void *post_data, size_t data_len, void **response, size_t *resp_len);
  130. int webclient_request_header_add(char **request_header, const char *fmt, ...);
  131. int webclient_resp_status_get(struct webclient_session *session);
  132. int webclient_content_length_get(struct webclient_session *session);
  133. #ifdef RT_USING_DFS
  134. /* file related operations */
  135. int webclient_get_file(const char *URI, const char *filename);
  136. int webclient_post_file(const char *URI, const char *filename, const char *form_data);
  137. #endif
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif