webclient.h 3.0 KB

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