esp_http_client.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_HEADERS_SENT, /*!< After sending all the headers to the server */
  32. HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatability
  33. and will be deprecated in future versions esp-idf */
  34. HTTP_EVENT_ON_HEADER, /*!< Occurs when receiving each header sent from the server */
  35. HTTP_EVENT_ON_DATA, /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
  36. HTTP_EVENT_ON_FINISH, /*!< Occurs when finish a HTTP session */
  37. HTTP_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
  38. } esp_http_client_event_id_t;
  39. /**
  40. * @brief HTTP Client events data
  41. */
  42. typedef struct esp_http_client_event {
  43. esp_http_client_event_id_t event_id; /*!< event_id, to know the cause of the event */
  44. esp_http_client_handle_t client; /*!< esp_http_client_handle_t context */
  45. void *data; /*!< data of the event */
  46. int data_len; /*!< data length of data */
  47. void *user_data; /*!< user_data context, from esp_http_client_config_t user_data */
  48. char *header_key; /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header key */
  49. char *header_value; /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header value */
  50. } esp_http_client_event_t;
  51. /**
  52. * @brief HTTP Client transport
  53. */
  54. typedef enum {
  55. HTTP_TRANSPORT_UNKNOWN = 0x0, /*!< Unknown */
  56. HTTP_TRANSPORT_OVER_TCP, /*!< Transport over tcp */
  57. HTTP_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
  58. } esp_http_client_transport_t;
  59. typedef esp_err_t (*http_event_handle_cb)(esp_http_client_event_t *evt);
  60. /**
  61. * @brief HTTP method
  62. */
  63. typedef enum {
  64. HTTP_METHOD_GET = 0, /*!< HTTP GET Method */
  65. HTTP_METHOD_POST, /*!< HTTP POST Method */
  66. HTTP_METHOD_PUT, /*!< HTTP PUT Method */
  67. HTTP_METHOD_PATCH, /*!< HTTP PATCH Method */
  68. HTTP_METHOD_DELETE, /*!< HTTP DELETE Method */
  69. HTTP_METHOD_HEAD, /*!< HTTP HEAD Method */
  70. HTTP_METHOD_NOTIFY, /*!< HTTP NOTIFY Method */
  71. HTTP_METHOD_SUBSCRIBE, /*!< HTTP SUBSCRIBE Method */
  72. HTTP_METHOD_UNSUBSCRIBE,/*!< HTTP UNSUBSCRIBE Method */
  73. HTTP_METHOD_OPTIONS, /*!< HTTP OPTIONS Method */
  74. HTTP_METHOD_MAX,
  75. } esp_http_client_method_t;
  76. /**
  77. * @brief HTTP Authentication type
  78. */
  79. typedef enum {
  80. HTTP_AUTH_TYPE_NONE = 0, /*!< No authention */
  81. HTTP_AUTH_TYPE_BASIC, /*!< HTTP Basic authentication */
  82. HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Disgest authentication */
  83. } esp_http_client_auth_type_t;
  84. /**
  85. * @brief HTTP configuration
  86. */
  87. typedef struct {
  88. const char *url; /*!< HTTP URL, the information on the URL is most important, it overrides the other fields below, if any */
  89. const char *host; /*!< Domain or IP as string */
  90. int port; /*!< Port to connect, default depend on esp_http_client_transport_t (80 or 443) */
  91. const char *username; /*!< Using for Http authentication */
  92. const char *password; /*!< Using for Http authentication */
  93. esp_http_client_auth_type_t auth_type; /*!< Http authentication type, see `esp_http_client_auth_type_t` */
  94. const char *path; /*!< HTTP Path, if not set, default is `/` */
  95. const char *query; /*!< HTTP query */
  96. const char *cert_pem; /*!< SSL server certification, PEM format as string, if the client requires to verify server */
  97. const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */
  98. const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */
  99. esp_http_client_method_t method; /*!< HTTP Method */
  100. int timeout_ms; /*!< Network timeout in milliseconds */
  101. bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
  102. int max_redirection_count; /*!< Max redirection number, using default value if zero*/
  103. http_event_handle_cb event_handler; /*!< HTTP Event Handle */
  104. esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */
  105. int buffer_size; /*!< HTTP buffer size (both send and receive) */
  106. void *user_data; /*!< HTTP user_data context */
  107. bool is_async; /*!< Set asynchronous mode, only supported with HTTPS for now */
  108. bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
  109. bool skip_cert_common_name_check; /*!< Skip any validation of server certificate CN field */
  110. } esp_http_client_config_t;
  111. /**
  112. * Enum for the HTTP status codes.
  113. */
  114. typedef enum {
  115. /* 3xx - Redirection */
  116. HttpStatus_MovedPermanently = 301,
  117. HttpStatus_Found = 302,
  118. /* 4xx - Client Error */
  119. HttpStatus_Unauthorized = 401
  120. } HttpStatus_Code;
  121. #define ESP_ERR_HTTP_BASE (0x7000) /*!< Starting number of HTTP error codes */
  122. #define ESP_ERR_HTTP_MAX_REDIRECT (ESP_ERR_HTTP_BASE + 1) /*!< The error exceeds the number of HTTP redirects */
  123. #define ESP_ERR_HTTP_CONNECT (ESP_ERR_HTTP_BASE + 2) /*!< Error open the HTTP connection */
  124. #define ESP_ERR_HTTP_WRITE_DATA (ESP_ERR_HTTP_BASE + 3) /*!< Error write HTTP data */
  125. #define ESP_ERR_HTTP_FETCH_HEADER (ESP_ERR_HTTP_BASE + 4) /*!< Error read HTTP header from server */
  126. #define ESP_ERR_HTTP_INVALID_TRANSPORT (ESP_ERR_HTTP_BASE + 5) /*!< There are no transport support for the input scheme */
  127. #define ESP_ERR_HTTP_CONNECTING (ESP_ERR_HTTP_BASE + 6) /*!< HTTP connection hasn't been established yet */
  128. #define ESP_ERR_HTTP_EAGAIN (ESP_ERR_HTTP_BASE + 7) /*!< Mapping of errno EAGAIN to esp_err_t */
  129. /**
  130. * @brief Start a HTTP session
  131. * This function must be the first function to call,
  132. * and it returns a esp_http_client_handle_t that you must use as input to other functions in the interface.
  133. * This call MUST have a corresponding call to esp_http_client_cleanup when the operation is complete.
  134. *
  135. * @param[in] config The configurations, see `http_client_config_t`
  136. *
  137. * @return
  138. * - `esp_http_client_handle_t`
  139. * - NULL if any errors
  140. */
  141. esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config);
  142. /**
  143. * @brief Invoke this function after `esp_http_client_init` and all the options calls are made, and will perform the
  144. * 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.
  145. * esp_http_client_perform performs the entire request in either blocking or non-blocking manner. By default, the API performs request in a blocking manner and returns when done,
  146. * or if it failed, and in non-blocking manner, it returns if EAGAIN/EWOULDBLOCK or EINPROGRESS is encountered, or if it failed. And in case of non-blocking request,
  147. * the user may call this API multiple times unless request & response is complete or there is a failure. To enable non-blocking esp_http_client_perform(), `is_async` member of esp_http_client_config_t
  148. * must be set while making a call to esp_http_client_init() API.
  149. * 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.
  150. * If you intend to transfer more than one file, you are even encouraged to do so.
  151. * 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.
  152. * 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.
  153. *
  154. * @note You must never call this function simultaneously from two places using the same client handle.
  155. * Let the function return first before invoking it another time.
  156. * If you want parallel transfers, you must use several esp_http_client_handle_t.
  157. * 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`.
  158. *
  159. * @param client The esp_http_client handle
  160. *
  161. * @return
  162. * - ESP_OK on successful
  163. * - ESP_FAIL on error
  164. */
  165. esp_err_t esp_http_client_perform(esp_http_client_handle_t client);
  166. /**
  167. * @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones
  168. *
  169. * @param[in] client The esp_http_client handle
  170. * @param[in] url The url
  171. *
  172. * @return
  173. * - ESP_OK
  174. * - ESP_FAIL
  175. */
  176. esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url);
  177. /**
  178. * @brief Set post data, this function must be called before `esp_http_client_perform`.
  179. * Note: The data parameter passed to this function is a pointer and this function will not copy the data
  180. *
  181. * @param[in] client The esp_http_client handle
  182. * @param[in] data post data pointer
  183. * @param[in] len post length
  184. *
  185. * @return
  186. * - ESP_OK
  187. * - ESP_FAIL
  188. */
  189. esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len);
  190. /**
  191. * @brief Get current post field information
  192. *
  193. * @param[in] client The client
  194. * @param[out] data Point to post data pointer
  195. *
  196. * @return Size of post data
  197. */
  198. int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data);
  199. /**
  200. * @brief Set http request header, this function must be called after esp_http_client_init and before any
  201. * perform function
  202. *
  203. * @param[in] client The esp_http_client handle
  204. * @param[in] key The header key
  205. * @param[in] value The header value
  206. *
  207. * @return
  208. * - ESP_OK
  209. * - ESP_FAIL
  210. */
  211. esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value);
  212. /**
  213. * @brief Get http request header.
  214. * The value parameter will be set to NULL if there is no header which is same as
  215. * the key specified, otherwise the address of header value will be assigned to value parameter.
  216. * This function must be called after `esp_http_client_init`.
  217. *
  218. * @param[in] client The esp_http_client handle
  219. * @param[in] key The header key
  220. * @param[out] value The header value
  221. *
  222. * @return
  223. * - ESP_OK
  224. * - ESP_FAIL
  225. */
  226. esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value);
  227. /**
  228. * @brief Get http request username.
  229. * The address of username buffer will be assigned to value parameter.
  230. * This function must be called after `esp_http_client_init`.
  231. *
  232. * @param[in] client The esp_http_client handle
  233. * @param[out] value The username value
  234. *
  235. * @return
  236. * - ESP_OK
  237. * - ESP_ERR_INVALID_ARG
  238. */
  239. esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **value);
  240. /**
  241. * @brief Get http request password.
  242. * The address of password buffer will be assigned to value parameter.
  243. * This function must be called after `esp_http_client_init`.
  244. *
  245. * @param[in] client The esp_http_client handle
  246. * @param[out] value The password value
  247. *
  248. * @return
  249. * - ESP_OK
  250. * - ESP_ERR_INVALID_ARG
  251. */
  252. esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value);
  253. /**
  254. * @brief Set http request method
  255. *
  256. * @param[in] client The esp_http_client handle
  257. * @param[in] method The method
  258. *
  259. * @return
  260. * - ESP_OK
  261. * - ESP_ERR_INVALID_ARG
  262. */
  263. esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method);
  264. /**
  265. * @brief Delete http request header
  266. *
  267. * @param[in] client The esp_http_client handle
  268. * @param[in] key The key
  269. *
  270. * @return
  271. * - ESP_OK
  272. * - ESP_FAIL
  273. */
  274. esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const char *key);
  275. /**
  276. * @brief This function will be open the connection, write all header strings and return
  277. *
  278. * @param[in] client The esp_http_client handle
  279. * @param[in] write_len HTTP Content length need to write to the server
  280. *
  281. * @return
  282. * - ESP_OK
  283. * - ESP_FAIL
  284. */
  285. esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len);
  286. /**
  287. * @brief This function will write data to the HTTP connection previously opened by esp_http_client_open()
  288. *
  289. * @param[in] client The esp_http_client handle
  290. * @param buffer The buffer
  291. * @param[in] len This value must not be larger than the write_len parameter provided to esp_http_client_open()
  292. *
  293. * @return
  294. * - (-1) if any errors
  295. * - Length of data written
  296. */
  297. int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, int len);
  298. /**
  299. * @brief This function need to call after esp_http_client_open, it will read from http stream, process all receive headers
  300. *
  301. * @param[in] client The esp_http_client handle
  302. *
  303. * @return
  304. * - (0) if stream doesn't contain content-length header, or chunked encoding (checked by `esp_http_client_is_chunked` response)
  305. * - (-1: ESP_FAIL) if any errors
  306. * - Download data length defined by content-length header
  307. */
  308. int esp_http_client_fetch_headers(esp_http_client_handle_t client);
  309. /**
  310. * @brief Check response data is chunked
  311. *
  312. * @param[in] client The esp_http_client handle
  313. *
  314. * @return true or false
  315. */
  316. bool esp_http_client_is_chunked_response(esp_http_client_handle_t client);
  317. /**
  318. * @brief Read data from http stream
  319. *
  320. * @param[in] client The esp_http_client handle
  321. * @param buffer The buffer
  322. * @param[in] len The length
  323. *
  324. * @return
  325. * - (-1) if any errors
  326. * - Length of data was read
  327. */
  328. int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len);
  329. /**
  330. * @brief Get http response status code, the valid value if this function invoke after `esp_http_client_perform`
  331. *
  332. * @param[in] client The esp_http_client handle
  333. *
  334. * @return Status code
  335. */
  336. int esp_http_client_get_status_code(esp_http_client_handle_t client);
  337. /**
  338. * @brief Get http response content length (from header Content-Length)
  339. * the valid value if this function invoke after `esp_http_client_perform`
  340. *
  341. * @param[in] client The esp_http_client handle
  342. *
  343. * @return
  344. * - (-1) Chunked transfer
  345. * - Content-Length value as bytes
  346. */
  347. int esp_http_client_get_content_length(esp_http_client_handle_t client);
  348. /**
  349. * @brief Close http connection, still kept all http request resources
  350. *
  351. * @param[in] client The esp_http_client handle
  352. *
  353. * @return
  354. * - ESP_OK
  355. * - ESP_FAIL
  356. */
  357. esp_err_t esp_http_client_close(esp_http_client_handle_t client);
  358. /**
  359. * @brief This function must be the last function to call for an session.
  360. * 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.
  361. * This might close all connections this handle has used and possibly has kept open until now.
  362. * 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.
  363. *
  364. * @param[in] client The esp_http_client handle
  365. *
  366. * @return
  367. * - ESP_OK
  368. * - ESP_FAIL
  369. */
  370. esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client);
  371. /**
  372. * @brief Get transport type
  373. *
  374. * @param[in] client The esp_http_client handle
  375. *
  376. * @return
  377. * - HTTP_TRANSPORT_UNKNOWN
  378. * - HTTP_TRANSPORT_OVER_TCP
  379. * - HTTP_TRANSPORT_OVER_SSL
  380. */
  381. esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client);
  382. /**
  383. * @brief Set redirection URL.
  384. * When received the 30x code from the server, the client stores the redirect URL provided by the server.
  385. * This function will set the current URL to redirect to enable client to execute the redirection request.
  386. *
  387. * @param[in] client The esp_http_client handle
  388. *
  389. * @return
  390. * - ESP_OK
  391. * - ESP_FAIL
  392. */
  393. esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client);
  394. /**
  395. * @brief On receiving HTTP Status code 401, this API can be invoked to add authorization
  396. * information.
  397. *
  398. * @note There is a possibility of receiving body message with redirection status codes, thus make sure
  399. * to flush off body data after calling this API.
  400. *
  401. * @param[in] client The esp_http_client handle
  402. */
  403. void esp_http_client_add_auth(esp_http_client_handle_t client);
  404. #ifdef __cplusplus
  405. }
  406. #endif
  407. #endif