webclient.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * File : webclient.h
  3. * COPYRIGHT (C) 2011-2018, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2013-05-05 Bernard the first version
  8. * 2013-06-10 Bernard fix the slow speed issue when download file.
  9. * 2015-11-14 aozima add content_length_remainder.
  10. * 2017-12-23 aozima update gethostbyname to getaddrinfo.
  11. * 2018-01-04 aozima add ipv6 address support.
  12. */
  13. #ifndef __WEBCLIENT_H__
  14. #define __WEBCLIENT_H__
  15. #include <stddef.h>
  16. #include <rtthread.h>
  17. #ifdef WEBCLIENT_USING_TLS
  18. #include <tls_client.h>
  19. #endif
  20. #define WEBCLIENT_HEADER_BUFSZ 4096
  21. #define WEBCLIENT_RESPONSE_BUFSZ 4096
  22. #define WEBCLIENT_TLS_READ_BUFFER 4096
  23. //typedef unsigned int size_t;
  24. enum WEBCLIENT_STATUS
  25. {
  26. WEBCLIENT_OK,
  27. WEBCLIENT_NOMEM,
  28. WEBCLIENT_NOSOCKET,
  29. WEBCLIENT_NOBUFFER,
  30. WEBCLIENT_CONNECT_FAILED,
  31. WEBCLIENT_DISCONNECT,
  32. WEBCLIENT_FILE_ERROR,
  33. WEBCLIENT_TIMEOUT,
  34. };
  35. enum WEBCLIENT_METHOD
  36. {
  37. WEBCLIENT_USER_METHOD,
  38. WEBCLIENT_GET,
  39. WEBCLIENT_POST,
  40. };
  41. struct webclient_session
  42. {
  43. /* the session socket */
  44. int socket;
  45. /* the response code of HTTP request */
  46. int response;
  47. /* transfer encoding */
  48. char *transfer_encoding;
  49. int chunk_sz;
  50. int chunk_offset;
  51. /* content_type of HTTP response */
  52. char *content_type;
  53. /* content_length of HTTP response */
  54. int content_length;
  55. /* last modified timestamp of resource */
  56. char *last_modified;
  57. /* location */
  58. char *location;
  59. /* server host */
  60. char *host;
  61. /* HTTP request */
  62. char *request;
  63. /* private for webclient session. */
  64. /* position of reading */
  65. unsigned int position;
  66. /* remainder of content reading */
  67. size_t content_length_remainder;
  68. #ifdef WEBCLIENT_USING_TLS
  69. /* mbedtls session struct*/
  70. MbedTLSSession *tls_session;
  71. #endif
  72. };
  73. struct webclient_session *webclient_open(const char *URI);
  74. struct webclient_session *webclient_open_position(const char *URI, int position);
  75. struct webclient_session *webclient_open_header(const char *URI, int method, const char *header, size_t header_sz);
  76. int webclient_close(struct webclient_session *session);
  77. int webclient_set_timeout(struct webclient_session *session, int millisecond);
  78. int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t size);
  79. int webclient_write(struct webclient_session *session, const unsigned char *buffer, size_t size);
  80. int webclient_send_header(struct webclient_session *session, int method,
  81. const char *header, size_t header_sz);
  82. int webclient_connect(struct webclient_session *session, const char *URI);
  83. int webclient_handle_response(struct webclient_session *session);
  84. /* hight level APIs for HTTP client */
  85. int webclient_response(struct webclient_session *session, void **response);
  86. struct webclient_session *webclient_open_custom(const char *URI, int method,
  87. const char *header, size_t header_sz,
  88. const char *data, size_t data_sz);
  89. int webclient_transfer(const char *URI, const char *header, size_t header_sz,
  90. const char *data, size_t data_sz,
  91. char *result, size_t result_sz);
  92. #ifdef RT_USING_DFS
  93. int webclient_get_file(const char *URI, const char *filename);
  94. int webclient_post_file(const char *URI, const char *filename, const char *form_data);
  95. #endif
  96. #endif