esp_http_client.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ESP_HTTP_CLIENT_H
  14. #define _ESP_HTTP_CLIENT_H
  15. #include "freertos/FreeRTOS.h"
  16. #include "http_parser.h"
  17. #include "sdkconfig.h"
  18. #include "esp_err.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define DEFAULT_HTTP_BUF_SIZE (512)
  23. typedef struct esp_http_client *esp_http_client_handle_t;
  24. typedef struct esp_http_client_event *esp_http_client_event_handle_t;
  25. /**
  26. * @brief HTTP Client events id
  27. */
  28. typedef enum {
  29. HTTP_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */
  30. HTTP_EVENT_ON_CONNECTED, /*!< Once the HTTP has been connected to the server, no data exchange has been performed */
  31. HTTP_EVENT_HEADER_SENT, /*!< After sending all the headers to the server */
  32. HTTP_EVENT_ON_HEADER, /*!< Occurs when receiving each header sent from the server */
  33. HTTP_EVENT_ON_DATA, /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
  34. HTTP_EVENT_ON_FINISH, /*!< Occurs when finish a HTTP session */
  35. HTTP_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
  36. } esp_http_client_event_id_t;
  37. /**
  38. * @brief HTTP Client events data
  39. */
  40. typedef struct esp_http_client_event {
  41. esp_http_client_event_id_t event_id; /*!< event_id, to know the cause of the event */
  42. esp_http_client_handle_t client; /*!< esp_http_client_handle_t context */
  43. void *data; /*!< data of the event */
  44. int data_len; /*!< data length of data */
  45. void *user_data; /*!< user_data context, from esp_http_client_config_t user_data */
  46. char *header_key; /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header key */
  47. char *header_value; /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header value */
  48. } esp_http_client_event_t;
  49. /**
  50. * @brief HTTP Client transport
  51. */
  52. typedef enum {
  53. HTTP_TRANSPORT_UNKNOWN = 0x0, /*!< Unknown */
  54. HTTP_TRANSPORT_OVER_TCP, /*!< Transport over tcp */
  55. HTTP_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
  56. } esp_http_client_transport_t;
  57. typedef esp_err_t (*http_event_handle_cb)(esp_http_client_event_t *evt);
  58. /**
  59. * @brief HTTP method
  60. */
  61. typedef enum {
  62. HTTP_METHOD_GET = 0, /*!< HTTP GET Method */
  63. HTTP_METHOD_POST, /*!< HTTP POST Method */
  64. HTTP_METHOD_PUT, /*!< HTTP PUT Method */
  65. HTTP_METHOD_PATCH, /*!< HTTP PATCH Method */
  66. HTTP_METHOD_DELETE, /*!< HTTP DELETE Method */
  67. HTTP_METHOD_HEAD, /*!< HTTP HEAD Method */
  68. HTTP_METHOD_MAX,
  69. } esp_http_client_method_t;
  70. /**
  71. * @brief HTTP Authentication type
  72. */
  73. typedef enum {
  74. HTTP_AUTH_TYPE_NONE = 0, /*!< No authention */
  75. HTTP_AUTH_TYPE_BASIC, /*!< HTTP Basic authentication */
  76. HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Disgest authentication */
  77. } esp_http_client_auth_type_t;
  78. /**
  79. * @brief HTTP configuration
  80. */
  81. typedef struct {
  82. const char *url; /*!< HTTP URL, the information on the URL is most important, it overrides the other fields below, if any */
  83. const char *host; /*!< Domain or IP as string */
  84. int port; /*!< Port to connect, default depend on esp_http_client_transport_t (80 or 443) */
  85. const char *username; /*!< Using for Http authentication */
  86. const char *password; /*!< Using for Http authentication */
  87. esp_http_client_auth_type_t auth_type; /*!< Http authentication type, see `esp_http_client_auth_type_t` */
  88. const char *path; /*!< HTTP Path, if not set, default is `/` */
  89. const char *query; /*!< HTTP query */
  90. const char *cert_pem; /*!< SSL Certification, PEM format as string, if the client requires to verify server */
  91. esp_http_client_method_t method; /*!< HTTP Method */
  92. int timeout_ms; /*!< Network timeout in milliseconds */
  93. bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
  94. int max_redirection_count; /*!< Max redirection number, using default value if zero*/
  95. http_event_handle_cb event_handler; /*!< HTTP Event Handle */
  96. esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */
  97. int buffer_size; /*!< HTTP buffer size (both send and receive) */
  98. void *user_data; /*!< HTTP user_data context */
  99. } esp_http_client_config_t;
  100. #define ESP_ERR_HTTP_BASE (0x7000) /*!< Starting number of HTTP error codes */
  101. #define ESP_ERR_HTTP_MAX_REDIRECT (ESP_ERR_HTTP_BASE + 1) /*!< The error exceeds the number of HTTP redirects */
  102. #define ESP_ERR_HTTP_CONNECT (ESP_ERR_HTTP_BASE + 2) /*!< Error open the HTTP connection */
  103. #define ESP_ERR_HTTP_WRITE_DATA (ESP_ERR_HTTP_BASE + 3) /*!< Error write HTTP data */
  104. #define ESP_ERR_HTTP_FETCH_HEADER (ESP_ERR_HTTP_BASE + 4) /*!< Error read HTTP header from server */
  105. #define ESP_ERR_HTTP_INVALID_TRANSPORT (ESP_ERR_HTTP_BASE + 5) /*!< There are no transport support for the input scheme */
  106. /**
  107. * @brief Start a HTTP session
  108. * This function must be the first function to call,
  109. * and it returns a esp_http_client_handle_t that you must use as input to other functions in the interface.
  110. * This call MUST have a corresponding call to esp_http_client_cleanup when the operation is complete.
  111. *
  112. * @param[in] config The configurations, see `http_client_config_t`
  113. *
  114. * @return
  115. * - `esp_http_client_handle_t`
  116. * - NULL if any errors
  117. */
  118. esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config);
  119. /**
  120. * @brief Invoke this function after `esp_http_client_init` and all the options calls are made, and will perform the
  121. * transfer as described in the options. It must be called with the same esp_http_client_handle_t as input as the esp_http_client_init call returned.
  122. * esp_http_client_perform performs the entire request in a blocking manner and returns when done, or if it failed.
  123. * You can do any amount of calls to esp_http_client_perform while using the same esp_http_client_handle_t. The underlying connection may be kept open if the server allows it.
  124. * If you intend to transfer more than one file, you are even encouraged to do so.
  125. * esp_http_client will then attempt to re-use the same connection for the following transfers, thus making the operations faster, less CPU intense and using less network resources.
  126. * Just note that you will have to use `esp_http_client_set_**` between the invokes to set options for the following esp_http_client_perform.
  127. *
  128. * @note You must never call this function simultaneously from two places using the same client handle.
  129. * Let the function return first before invoking it another time.
  130. * If you want parallel transfers, you must use several esp_http_client_handle_t.
  131. * This function include `esp_http_client_open` -> `esp_http_client_write` -> `esp_http_client_fetch_headers` -> `esp_http_client_read` (and option) `esp_http_client_close`.
  132. *
  133. * @param client The esp_http_client handle
  134. *
  135. * @return
  136. * - ESP_OK on successful
  137. * - ESP_FAIL on error
  138. */
  139. esp_err_t esp_http_client_perform(esp_http_client_handle_t client);
  140. /**
  141. * @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones
  142. *
  143. * @param[in] client The esp_http_client handle
  144. * @param[in] url The url
  145. *
  146. * @return
  147. * - ESP_OK
  148. * - ESP_FAIL
  149. */
  150. esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url);
  151. /**
  152. * @brief Set post data, this function must be called before `esp_http_client_perform`.
  153. * Note: The data parameter passed to this function is a pointer and this function will not copy the data
  154. *
  155. * @param[in] client The esp_http_client handle
  156. * @param[in] data post data pointer
  157. * @param[in] len post length
  158. *
  159. * @return
  160. * - ESP_OK
  161. * - ESP_FAIL
  162. */
  163. esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len);
  164. /**
  165. * @brief Get current post field information
  166. *
  167. * @param[in] client The client
  168. * @param[out] data Point to post data pointer
  169. *
  170. * @return Size of post data
  171. */
  172. int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data);
  173. /**
  174. * @brief Set http request header, this function must be called after esp_http_client_init and before any
  175. * perform function
  176. *
  177. * @param[in] client The esp_http_client handle
  178. * @param[in] key The header key
  179. * @param[in] value The header value
  180. *
  181. * @return
  182. * - ESP_OK
  183. * - ESP_FAIL
  184. */
  185. esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value);
  186. /**
  187. * @brief Get http request header.
  188. * The value parameter will be set to NULL if there is no header which is same as
  189. * the key specified, otherwise the address of header value will be assigned to value parameter.
  190. * This function must be called after `esp_http_client_init`.
  191. *
  192. * @param[in] client The esp_http_client handle
  193. * @param[in] key The header key
  194. * @param[out] value The header value
  195. *
  196. * @return
  197. * - ESP_OK
  198. * - ESP_FAIL
  199. */
  200. esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value);
  201. /**
  202. * @brief Set http request method
  203. *
  204. * @param[in] client The esp_http_client handle
  205. * @param[in] method The method
  206. *
  207. * @return ESP_OK
  208. */
  209. esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method);
  210. /**
  211. * @brief Delete http request header
  212. *
  213. * @param[in] client The esp_http_client handle
  214. * @param[in] key The key
  215. *
  216. * @return
  217. * - ESP_OK
  218. * - ESP_FAIL
  219. */
  220. esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const char *key);
  221. /**
  222. * @brief This function will be open the connection, write all header strings and return
  223. *
  224. * @param[in] client The esp_http_client handle
  225. * @param[in] write_len HTTP Content length need to write to the server
  226. *
  227. * @return
  228. * - ESP_OK
  229. * - ESP_FAIL
  230. */
  231. esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len);
  232. /**
  233. * @brief This function will write data to the HTTP connection previously opened by esp_http_client_open()
  234. *
  235. * @param[in] client The esp_http_client handle
  236. * @param buffer The buffer
  237. * @param[in] len This value must not be larger than the write_len parameter provided to esp_http_client_open()
  238. *
  239. * @return
  240. * - (-1) if any errors
  241. * - Length of data written
  242. */
  243. int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, int len);
  244. /**
  245. * @brief This function need to call after esp_http_client_open, it will read from http stream, process all receive headers
  246. *
  247. * @param[in] client The esp_http_client handle
  248. *
  249. * @return
  250. * - (0) if stream doesn't contain content-length header, or chunked encoding (checked by `esp_http_client_is_chunked` response)
  251. * - (-1: ESP_FAIL) if any errors
  252. * - Download data length defined by content-length header
  253. */
  254. int esp_http_client_fetch_headers(esp_http_client_handle_t client);
  255. /**
  256. * @brief Check response data is chunked
  257. *
  258. * @param[in] client The esp_http_client handle
  259. *
  260. * @return true or false
  261. */
  262. bool esp_http_client_is_chunked_response(esp_http_client_handle_t client);
  263. /**
  264. * @brief Read data from http stream
  265. *
  266. * @param[in] client The esp_http_client handle
  267. * @param buffer The buffer
  268. * @param[in] len The length
  269. *
  270. * @return
  271. * - (-1) if any errors
  272. * - Length of data was read
  273. */
  274. int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len);
  275. /**
  276. * @brief Get http response status code, the valid value if this function invoke after `esp_http_client_perform`
  277. *
  278. * @param[in] client The esp_http_client handle
  279. *
  280. * @return Status code
  281. */
  282. int esp_http_client_get_status_code(esp_http_client_handle_t client);
  283. /**
  284. * @brief Get http response content length (from header Content-Length)
  285. * the valid value if this function invoke after `esp_http_client_perform`
  286. *
  287. * @param[in] client The esp_http_client handle
  288. *
  289. * @return
  290. * - (-1) Chunked transfer
  291. * - Content-Length value as bytes
  292. */
  293. int esp_http_client_get_content_length(esp_http_client_handle_t client);
  294. /**
  295. * @brief Close http connection, still kept all http request resources
  296. *
  297. * @param[in] client The esp_http_client handle
  298. *
  299. * @return
  300. * - ESP_OK
  301. * - ESP_FAIL
  302. */
  303. esp_err_t esp_http_client_close(esp_http_client_handle_t client);
  304. /**
  305. * @brief This function must be the last function to call for an session.
  306. * It is the opposite of the esp_http_client_init function and must be called with the same handle as input that a esp_http_client_init call returned.
  307. * This might close all connections this handle has used and possibly has kept open until now.
  308. * Don't call this function if you intend to transfer more files, re-using handles is a key to good performance with esp_http_client.
  309. *
  310. * @param[in] client The esp_http_client handle
  311. *
  312. * @return
  313. * - ESP_OK
  314. * - ESP_FAIL
  315. */
  316. esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client);
  317. /**
  318. * @brief Get transport type
  319. *
  320. * @param[in] client The esp_http_client handle
  321. *
  322. * @return
  323. * - HTTP_TRANSPORT_UNKNOWN
  324. * - HTTP_TRANSPORT_OVER_TCP
  325. * - HTTP_TRANSPORT_OVER_SSL
  326. */
  327. esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client);
  328. #ifdef __cplusplus
  329. }
  330. #endif
  331. #endif