webclient.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2018, 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. #ifdef WEBCLIENT_USING_TLS
  20. #include <tls_client.h>
  21. #endif
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #undef DBG_SECTION_NAME
  26. #undef DBG_LEVEL
  27. #undef DBG_COLOR
  28. #undef DBG_ENABLE
  29. #define DBG_ENABLE
  30. #define DBG_SECTION_NAME "WEB"
  31. #ifdef WEBCLIENT_DEBUG
  32. #define DBG_LEVEL DBG_LOG
  33. #else
  34. #define DBG_LEVEL DBG_INFO
  35. #endif /* WEBCLIENT_DEBUG */
  36. #define DBG_COLOR
  37. #include <rtdbg.h>
  38. #ifndef web_malloc
  39. #define web_malloc rt_malloc
  40. #endif
  41. #ifndef web_calloc
  42. #define web_calloc rt_calloc
  43. #endif
  44. #ifndef web_realloc
  45. #define web_realloc rt_realloc
  46. #endif
  47. #ifndef web_free
  48. #define web_free rt_free
  49. #endif
  50. #ifndef web_strdup
  51. #define web_strdup rt_strdup
  52. #endif
  53. #define WEBCLIENT_SW_VERSION "2.0.0"
  54. #define WEBCLIENT_SW_VERSION_NUM 0x20000
  55. #define WEBCLIENT_HEADER_BUFSZ 4096
  56. #define WEBCLIENT_RESPONSE_BUFSZ 4096
  57. enum WEBCLIENT_STATUS
  58. {
  59. WEBCLIENT_OK,
  60. WEBCLIENT_ERROR,
  61. WEBCLIENT_TIMEOUT,
  62. WEBCLIENT_NOMEM,
  63. WEBCLIENT_NOSOCKET,
  64. WEBCLIENT_NOBUFFER,
  65. WEBCLIENT_CONNECT_FAILED,
  66. WEBCLIENT_DISCONNECT,
  67. WEBCLIENT_FILE_ERROR,
  68. };
  69. enum WEBCLIENT_METHOD
  70. {
  71. WEBCLIENT_USER_METHOD,
  72. WEBCLIENT_GET,
  73. WEBCLIENT_POST,
  74. };
  75. struct webclient_header
  76. {
  77. char *buffer;
  78. size_t length; /* content header buffer size */
  79. size_t size; /* maximum support header size */
  80. };
  81. struct webclient_session
  82. {
  83. struct webclient_header *header; /* webclient response header information */
  84. int socket;
  85. int resp_status;
  86. char *host; /* server host */
  87. char *req_url; /* HTTP request address*/
  88. int chunk_sz;
  89. int chunk_offset;
  90. int content_length;
  91. size_t content_remainder; /* remainder of content length */
  92. #ifdef WEBCLIENT_USING_TLS
  93. MbedTLSSession *tls_session; /* mbedtls connect session */
  94. #endif
  95. };
  96. /* create webclient session and set header response size */
  97. struct webclient_session *webclient_session_create(size_t header_sz);
  98. /* send HTTP GET request */
  99. int webclient_get(struct webclient_session *session, const char *URI);
  100. int webclient_get_position(struct webclient_session *session, const char *URI, int position);
  101. /* send HTTP POST request */
  102. int webclient_post(struct webclient_session *session, const char *URI, const char *post_data);
  103. /* close and release wenclient session */
  104. int webclient_close(struct webclient_session *session);
  105. int webclient_set_timeout(struct webclient_session *session, int millisecond);
  106. /* send or receive data from server */
  107. int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size);
  108. int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size);
  109. /* webclient GET/POST header buffer operate by the header fields */
  110. int webclient_header_fields_add(struct webclient_session *session, const char *fmt, ...);
  111. const char *webclient_header_fields_get(struct webclient_session *session, const char *fields);
  112. /* send HTTP POST/GET request, and get response data */
  113. int webclient_response(struct webclient_session *session, unsigned char **response);
  114. int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **response);
  115. int webclient_resp_status_get(struct webclient_session *session);
  116. int webclient_content_length_get(struct webclient_session *session);
  117. #ifdef RT_USING_DFS
  118. /* file related operations */
  119. int webclient_get_file(const char *URI, const char *filename);
  120. int webclient_post_file(const char *URI, const char *filename, const char *form_data);
  121. #endif
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif