webclient.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * File : webclient.h
  3. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2013-05-05 Bernard the first version
  22. * 2013-06-10 Bernard fix the slow speed issue when download file.
  23. * 2015-11-14 aozima add content_length_remainder.
  24. * 2017-12-23 aozima update gethostbyname to getaddrinfo.
  25. * 2018-01-04 aozima add ipv6 address support.
  26. * 2018-07-26 chenyong modify log information
  27. */
  28. #ifndef __WEBCLIENT_H__
  29. #define __WEBCLIENT_H__
  30. #include <rtthread.h>
  31. #ifdef WEBCLIENT_USING_TLS
  32. #include <tls_client.h>
  33. #endif
  34. #undef DBG_SECTION_NAME
  35. #undef DBG_LEVEL
  36. #undef DBG_COLOR
  37. #undef DBG_ENABLE
  38. #define DBG_ENABLE
  39. #define DBG_SECTION_NAME "WEB"
  40. #ifdef WEBCLIENT_DEBUG
  41. #define DBG_LEVEL DBG_LOG
  42. #else
  43. #define DBG_LEVEL DBG_INFO
  44. #endif /* WEBCLIENT_DEBUG */
  45. #define DBG_COLOR
  46. #include <rtdbg.h>
  47. #ifndef web_malloc
  48. #define web_malloc rt_malloc
  49. #endif
  50. #ifndef web_calloc
  51. #define web_calloc rt_calloc
  52. #endif
  53. #ifndef web_realloc
  54. #define web_realloc rt_realloc
  55. #endif
  56. #ifndef web_free
  57. #define web_free rt_free
  58. #endif
  59. #ifndef web_strdup
  60. #define web_strdup rt_strdup
  61. #endif
  62. #define WEBCLIENT_SW_VERSION "2.0.0"
  63. #define WEBCLIENT_SW_VERSION_NUM 0x20000
  64. #define WEBCLIENT_HEADER_BUFSZ 4096
  65. #define WEBCLIENT_RESPONSE_BUFSZ 4096
  66. enum WEBCLIENT_STATUS
  67. {
  68. WEBCLIENT_OK,
  69. WEBCLIENT_ERROR,
  70. WEBCLIENT_TIMEOUT,
  71. WEBCLIENT_NOMEM,
  72. WEBCLIENT_NOSOCKET,
  73. WEBCLIENT_NOBUFFER,
  74. WEBCLIENT_CONNECT_FAILED,
  75. WEBCLIENT_DISCONNECT,
  76. WEBCLIENT_FILE_ERROR,
  77. };
  78. enum WEBCLIENT_METHOD
  79. {
  80. WEBCLIENT_USER_METHOD,
  81. WEBCLIENT_GET,
  82. WEBCLIENT_POST,
  83. };
  84. struct webclient_session
  85. {
  86. /* the session socket */
  87. int socket;
  88. /* the response code of HTTP request */
  89. int response;
  90. /* transfer encoding */
  91. char *transfer_encoding;
  92. int chunk_sz;
  93. int chunk_offset;
  94. /* content_type of HTTP response */
  95. char *content_type;
  96. /* content_length of HTTP response */
  97. int content_length;
  98. /* last modified timestamp of resource */
  99. char *last_modified;
  100. /* location */
  101. char *location;
  102. /* server host */
  103. char *host;
  104. /* HTTP request */
  105. char *request;
  106. /* position of reading */
  107. unsigned int position;
  108. /* remainder of content reading */
  109. size_t content_length_remainder;
  110. int header_sz;
  111. int resp_sz;
  112. #ifdef WEBCLIENT_USING_TLS
  113. /* mbedtls connect session */
  114. MbedTLSSession *tls_session;
  115. #endif
  116. };
  117. /* create webclient session and set header response size */
  118. struct webclient_session *webclient_session_create(size_t header_sz, size_t resp_sz);
  119. /* send HTTP GET request */
  120. int webclient_get(struct webclient_session *session, const char *URI, const char *header);
  121. int webclient_get_position(struct webclient_session *session, const char *URI, int position);
  122. /* send HTTP POST request */
  123. int webclient_post(struct webclient_session *session, const char *URI,
  124. const char *header, const char *post_data);
  125. /* close and release wenclient session */
  126. int webclient_close(struct webclient_session *session);
  127. int webclient_set_timeout(struct webclient_session *session, int millisecond);
  128. /* send or receive data from server */
  129. int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size);
  130. int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size);
  131. /* send HTTP POST/GET request, and get response data */
  132. int webclient_response(struct webclient_session *session, void **response);
  133. int webclient_request(const char *URI, const char *header, const char *post_data, unsigned char **result);
  134. #ifdef RT_USING_DFS
  135. /* file related operations */
  136. int webclient_get_file(const char *URI, const char *filename);
  137. int webclient_post_file(const char *URI, const char *filename, const char *form_data);
  138. #endif
  139. #endif