webclient.h 4.2 KB

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