esp_http_client.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <inttypes.h>
  8. #include "esp_log.h"
  9. #include "esp_check.h"
  10. #include "http_parser.h"
  11. #include "http_header.h"
  12. #include "esp_transport.h"
  13. #include "esp_transport_tcp.h"
  14. #include "http_utils.h"
  15. #include "http_auth.h"
  16. #include "sdkconfig.h"
  17. #include "esp_http_client.h"
  18. #include "errno.h"
  19. #include "esp_random.h"
  20. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  21. #include "esp_transport_ssl.h"
  22. #endif
  23. static const char *TAG = "HTTP_CLIENT";
  24. /**
  25. * HTTP Buffer
  26. */
  27. typedef struct {
  28. char *data; /*!< The HTTP data received from the server */
  29. int len; /*!< The HTTP data len received from the server */
  30. char *raw_data; /*!< The HTTP data after decoding */
  31. char *orig_raw_data;/*!< The Original pointer to HTTP data after decoding */
  32. int raw_len; /*!< The HTTP data len after decoding */
  33. char *output_ptr; /*!< The destination address of the data to be copied to after decoding */
  34. } esp_http_buffer_t;
  35. /**
  36. * private HTTP Data structure
  37. */
  38. typedef struct {
  39. http_header_handle_t headers; /*!< http header */
  40. esp_http_buffer_t *buffer; /*!< data buffer as linked list */
  41. int status_code; /*!< status code (integer) */
  42. int64_t content_length; /*!< data length */
  43. int chunk_length; /*!< chunk length */
  44. int data_offset; /*!< offset to http data (Skip header) */
  45. int64_t data_process; /*!< data processed */
  46. int method; /*!< http method */
  47. bool is_chunked;
  48. } esp_http_data_t;
  49. typedef struct {
  50. char *url;
  51. char *scheme;
  52. char *host;
  53. int port;
  54. char *username;
  55. char *password;
  56. char *path;
  57. char *query;
  58. char *cert_pem;
  59. esp_http_client_method_t method;
  60. esp_http_client_auth_type_t auth_type;
  61. esp_http_client_transport_t transport_type;
  62. int max_store_header_size;
  63. } connection_info_t;
  64. typedef enum {
  65. HTTP_STATE_UNINIT = 0,
  66. HTTP_STATE_INIT,
  67. HTTP_STATE_CONNECTED,
  68. HTTP_STATE_REQ_COMPLETE_HEADER,
  69. HTTP_STATE_REQ_COMPLETE_DATA,
  70. HTTP_STATE_RES_COMPLETE_HEADER,
  71. HTTP_STATE_RES_ON_DATA_START,
  72. HTTP_STATE_RES_COMPLETE_DATA,
  73. HTTP_STATE_CLOSE
  74. } esp_http_state_t;
  75. /**
  76. * HTTP client class
  77. */
  78. struct esp_http_client {
  79. int redirect_counter;
  80. int max_redirection_count;
  81. int max_authorization_retries;
  82. int process_again;
  83. struct http_parser *parser;
  84. struct http_parser_settings *parser_settings;
  85. esp_transport_list_handle_t transport_list;
  86. esp_transport_handle_t transport;
  87. esp_http_data_t *request;
  88. esp_http_data_t *response;
  89. void *user_data;
  90. esp_http_auth_data_t *auth_data;
  91. char *post_data;
  92. char *location;
  93. char *auth_header;
  94. char *current_header_key;
  95. char *current_header_value;
  96. int post_len;
  97. connection_info_t connection_info;
  98. bool is_chunk_complete;
  99. esp_http_state_t state;
  100. http_event_handle_cb event_handler;
  101. int timeout_ms;
  102. int buffer_size_rx;
  103. int buffer_size_tx;
  104. bool disable_auto_redirect;
  105. esp_http_client_event_t event;
  106. int data_written_index;
  107. int data_write_left;
  108. bool first_line_prepared;
  109. int header_index;
  110. bool is_async;
  111. esp_transport_keep_alive_t keep_alive_cfg;
  112. struct ifreq *if_name;
  113. unsigned cache_data_in_fetch_hdr: 1;
  114. };
  115. typedef struct esp_http_client esp_http_client_t;
  116. static esp_err_t _clear_connection_info(esp_http_client_handle_t client);
  117. /**
  118. * Default settings
  119. */
  120. #define DEFAULT_HTTP_PORT (80)
  121. #define DEFAULT_HTTPS_PORT (443)
  122. #define ASYNC_TRANS_CONNECT_FAIL -1
  123. #define ASYNC_TRANS_CONNECTING 0
  124. #define ASYNC_TRANS_CONNECT_PASS 1
  125. static const char *DEFAULT_HTTP_USER_AGENT = "ESP32 HTTP Client/1.0";
  126. static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1";
  127. static const char *DEFAULT_HTTP_PATH = "/";
  128. static const int DEFAULT_MAX_REDIRECT = 10;
  129. static const int DEFAULT_MAX_AUTH_RETRIES = 10;
  130. static const int DEFAULT_TIMEOUT_MS = 5000;
  131. static const int DEFAULT_KEEP_ALIVE_IDLE = 5;
  132. static const int DEFAULT_KEEP_ALIVE_INTERVAL= 5;
  133. static const int DEFAULT_KEEP_ALIVE_COUNT= 3;
  134. static const char *HTTP_METHOD_MAPPING[] = {
  135. "GET",
  136. "POST",
  137. "PUT",
  138. "PATCH",
  139. "DELETE",
  140. "HEAD",
  141. "NOTIFY",
  142. "SUBSCRIBE",
  143. "UNSUBSCRIBE",
  144. "OPTIONS",
  145. "COPY",
  146. "MOVE",
  147. "LOCK",
  148. "UNLOCK",
  149. "PROPFIND",
  150. "PROPPATCH",
  151. "MKCOL"
  152. };
  153. static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len);
  154. static esp_err_t esp_http_client_connect(esp_http_client_handle_t client);
  155. static esp_err_t esp_http_client_send_post_data(esp_http_client_handle_t client);
  156. static esp_err_t http_dispatch_event(esp_http_client_t *client, esp_http_client_event_id_t event_id, void *data, int len)
  157. {
  158. esp_http_client_event_t *event = &client->event;
  159. if (client->event_handler) {
  160. event->event_id = event_id;
  161. event->user_data = client->user_data;
  162. event->data = data;
  163. event->data_len = len;
  164. return client->event_handler(event);
  165. }
  166. return ESP_OK;
  167. }
  168. static int http_on_message_begin(http_parser *parser)
  169. {
  170. esp_http_client_t *client = parser->data;
  171. ESP_LOGD(TAG, "on_message_begin");
  172. client->response->is_chunked = false;
  173. client->is_chunk_complete = false;
  174. return 0;
  175. }
  176. static int http_on_url(http_parser *parser, const char *at, size_t length)
  177. {
  178. ESP_LOGD(TAG, "http_on_url");
  179. return 0;
  180. }
  181. static int http_on_status(http_parser *parser, const char *at, size_t length)
  182. {
  183. return 0;
  184. }
  185. static int http_on_header_event(esp_http_client_handle_t client)
  186. {
  187. if (client->current_header_key != NULL && client->current_header_value != NULL) {
  188. ESP_LOGD(TAG, "HEADER=%s:%s", client->current_header_key, client->current_header_value);
  189. client->event.header_key = client->current_header_key;
  190. client->event.header_value = client->current_header_value;
  191. http_dispatch_event(client, HTTP_EVENT_ON_HEADER, NULL, 0);
  192. free(client->current_header_key);
  193. free(client->current_header_value);
  194. client->current_header_key = NULL;
  195. client->current_header_value = NULL;
  196. }
  197. return 0;
  198. }
  199. static int http_on_header_field(http_parser *parser, const char *at, size_t length)
  200. {
  201. esp_http_client_t *client = parser->data;
  202. http_on_header_event(client);
  203. http_utils_append_string(&client->current_header_key, at, length);
  204. return 0;
  205. }
  206. static int http_on_header_value(http_parser *parser, const char *at, size_t length)
  207. {
  208. esp_http_client_handle_t client = parser->data;
  209. if (client->current_header_key == NULL) {
  210. return 0;
  211. }
  212. if (strcasecmp(client->current_header_key, "Location") == 0) {
  213. http_utils_append_string(&client->location, at, length);
  214. } else if (strcasecmp(client->current_header_key, "Transfer-Encoding") == 0
  215. && memcmp(at, "chunked", length) == 0) {
  216. client->response->is_chunked = true;
  217. } else if (strcasecmp(client->current_header_key, "WWW-Authenticate") == 0) {
  218. http_utils_append_string(&client->auth_header, at, length);
  219. }
  220. http_utils_append_string(&client->current_header_value, at, length);
  221. return 0;
  222. }
  223. static int http_on_headers_complete(http_parser *parser)
  224. {
  225. esp_http_client_handle_t client = parser->data;
  226. http_on_header_event(client);
  227. client->response->status_code = parser->status_code;
  228. client->response->data_offset = parser->nread;
  229. client->response->content_length = parser->content_length;
  230. client->response->data_process = 0;
  231. ESP_LOGD(TAG, "http_on_headers_complete, status=%d, offset=%d, nread=%" PRId32, parser->status_code, client->response->data_offset, parser->nread);
  232. client->state = HTTP_STATE_RES_COMPLETE_HEADER;
  233. if (client->connection_info.method == HTTP_METHOD_HEAD) {
  234. /* In a HTTP_RESPONSE parser returning '1' from on_headers_complete will tell the
  235. parser that it should not expect a body. This is used when receiving a response
  236. to a HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: chunked'
  237. headers that indicate the presence of a body.*/
  238. return 1;
  239. }
  240. return 0;
  241. }
  242. static int http_on_body(http_parser *parser, const char *at, size_t length)
  243. {
  244. esp_http_client_t *client = parser->data;
  245. ESP_LOGD(TAG, "http_on_body %zu", length);
  246. if (client->response->buffer->output_ptr) {
  247. memcpy(client->response->buffer->output_ptr, (char *)at, length);
  248. client->response->buffer->output_ptr += length;
  249. } else {
  250. /* Do not cache body when http_on_body is called from esp_http_client_perform */
  251. if (client->state < HTTP_STATE_RES_ON_DATA_START && client->cache_data_in_fetch_hdr) {
  252. ESP_LOGI(TAG, "Body received in fetch header state, %p, %zu", at, length);
  253. esp_http_buffer_t *res_buffer = client->response->buffer;
  254. assert(res_buffer->orig_raw_data == res_buffer->raw_data);
  255. res_buffer->orig_raw_data = (char *)realloc(res_buffer->orig_raw_data, res_buffer->raw_len + length);
  256. if (!res_buffer->orig_raw_data) {
  257. ESP_LOGE(TAG, "Failed to allocate memory for storing decoded data");
  258. return -1;
  259. }
  260. memcpy(res_buffer->orig_raw_data + res_buffer->raw_len, at, length);
  261. res_buffer->raw_data = res_buffer->orig_raw_data;
  262. }
  263. }
  264. client->response->data_process += length;
  265. client->response->buffer->raw_len += length;
  266. http_dispatch_event(client, HTTP_EVENT_ON_DATA, (void *)at, length);
  267. return 0;
  268. }
  269. static int http_on_message_complete(http_parser *parser)
  270. {
  271. ESP_LOGD(TAG, "http_on_message_complete, parser=%p", parser);
  272. esp_http_client_handle_t client = parser->data;
  273. client->is_chunk_complete = true;
  274. return 0;
  275. }
  276. static int http_on_chunk_complete(http_parser *parser)
  277. {
  278. ESP_LOGD(TAG, "http_on_chunk_complete");
  279. return 0;
  280. }
  281. static int http_on_chunk_header(http_parser *parser)
  282. {
  283. esp_http_client_handle_t client = parser->data;
  284. client->response->chunk_length = parser->content_length;
  285. ESP_LOGD(TAG, "http_on_chunk_header, chunk_length");
  286. return 0;
  287. }
  288. esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value)
  289. {
  290. return http_header_set(client->request->headers, key, value);
  291. }
  292. esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value)
  293. {
  294. return http_header_get(client->request->headers, key, value);
  295. }
  296. esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const char *key)
  297. {
  298. return http_header_delete(client->request->headers, key);
  299. }
  300. esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **value)
  301. {
  302. if (client == NULL || value == NULL) {
  303. ESP_LOGE(TAG, "client or value must not be NULL");
  304. return ESP_ERR_INVALID_ARG;
  305. }
  306. *value = client->connection_info.username;
  307. return ESP_OK;
  308. }
  309. esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const char *username)
  310. {
  311. if (client == NULL) {
  312. ESP_LOGE(TAG, "client must not be NULL");
  313. return ESP_ERR_INVALID_ARG;
  314. }
  315. if (client->connection_info.username != NULL) {
  316. free(client->connection_info.username);
  317. }
  318. client->connection_info.username = username ? strdup(username) : NULL;
  319. return ESP_OK;
  320. }
  321. esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value)
  322. {
  323. if (client == NULL || value == NULL) {
  324. ESP_LOGE(TAG, "client or value must not be NULL");
  325. return ESP_ERR_INVALID_ARG;
  326. }
  327. *value = client->connection_info.password;
  328. return ESP_OK;
  329. }
  330. esp_err_t esp_http_client_cancel_request(esp_http_client_handle_t client)
  331. {
  332. if (client == NULL) {
  333. ESP_LOGD(TAG, "Client handle is NULL");
  334. return ESP_ERR_INVALID_ARG;
  335. }
  336. if (client->state < HTTP_STATE_CONNECTED) {
  337. ESP_LOGD(TAG, "Invalid State: %d", client->state);
  338. return ESP_ERR_INVALID_STATE;
  339. }
  340. if (esp_transport_close(client->transport) != 0) {
  341. return ESP_FAIL;
  342. }
  343. esp_err_t err = esp_http_client_connect(client);
  344. // esp_http_client_connect() will return ESP_ERR_HTTP_CONNECTING in case of non-blocking mode and if the connection has not been established.
  345. if (err == ESP_OK || (client->is_async && err == ESP_ERR_HTTP_CONNECTING)) {
  346. client->response->data_process = client->response->content_length;
  347. return ESP_OK;
  348. }
  349. return ESP_FAIL;
  350. }
  351. esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, const char *password)
  352. {
  353. if (client == NULL) {
  354. ESP_LOGE(TAG, "client must not be NULL");
  355. return ESP_ERR_INVALID_ARG;
  356. }
  357. if (client->connection_info.password != NULL) {
  358. memset(client->connection_info.password, 0, strlen(client->connection_info.password));
  359. free(client->connection_info.password);
  360. }
  361. client->connection_info.password = password ? strdup(password) : NULL;
  362. return ESP_OK;
  363. }
  364. esp_err_t esp_http_client_set_authtype(esp_http_client_handle_t client, esp_http_client_auth_type_t auth_type)
  365. {
  366. if (client == NULL) {
  367. ESP_LOGE(TAG, "client must not be NULL");
  368. return ESP_ERR_INVALID_ARG;
  369. }
  370. client->connection_info.auth_type = auth_type;
  371. return ESP_OK;
  372. }
  373. static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_client_config_t *config)
  374. {
  375. esp_err_t ret = ESP_OK;
  376. client->connection_info.method = config->method;
  377. client->connection_info.port = config->port;
  378. client->connection_info.auth_type = config->auth_type;
  379. client->event_handler = config->event_handler;
  380. client->timeout_ms = config->timeout_ms;
  381. client->max_redirection_count = config->max_redirection_count;
  382. client->max_authorization_retries = config->max_authorization_retries;
  383. client->user_data = config->user_data;
  384. client->buffer_size_rx = config->buffer_size;
  385. client->buffer_size_tx = config->buffer_size_tx;
  386. client->disable_auto_redirect = config->disable_auto_redirect;
  387. if (config->buffer_size == 0) {
  388. client->buffer_size_rx = DEFAULT_HTTP_BUF_SIZE;
  389. }
  390. if (config->buffer_size_tx == 0) {
  391. client->buffer_size_tx = DEFAULT_HTTP_BUF_SIZE;
  392. }
  393. if (client->max_redirection_count == 0) {
  394. client->max_redirection_count = DEFAULT_MAX_REDIRECT;
  395. }
  396. if (client->max_authorization_retries == 0) {
  397. client->max_authorization_retries = DEFAULT_MAX_AUTH_RETRIES;
  398. } else if (client->max_authorization_retries == -1) {
  399. client->max_authorization_retries = 0;
  400. }
  401. if (config->path) {
  402. client->connection_info.path = strdup(config->path);
  403. } else {
  404. client->connection_info.path = strdup(DEFAULT_HTTP_PATH);
  405. }
  406. ESP_RETURN_ON_FALSE(client->connection_info.path, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  407. if (config->host) {
  408. client->connection_info.host = strdup(config->host);
  409. ESP_GOTO_ON_FALSE(client->connection_info.host, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
  410. }
  411. if (config->query) {
  412. client->connection_info.query = strdup(config->query);
  413. ESP_GOTO_ON_FALSE(client->connection_info.query, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
  414. }
  415. if (config->username) {
  416. client->connection_info.username = strdup(config->username);
  417. ESP_GOTO_ON_FALSE(client->connection_info.username, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
  418. }
  419. if (config->password) {
  420. client->connection_info.password = strdup(config->password);
  421. ESP_GOTO_ON_FALSE(client->connection_info.password, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
  422. }
  423. if (config->transport_type == HTTP_TRANSPORT_OVER_SSL) {
  424. http_utils_assign_string(&client->connection_info.scheme, "https", -1);
  425. if (client->connection_info.port == 0) {
  426. client->connection_info.port = DEFAULT_HTTPS_PORT;
  427. }
  428. } else {
  429. http_utils_assign_string(&client->connection_info.scheme, "http", -1);
  430. if (client->connection_info.port == 0) {
  431. client->connection_info.port = DEFAULT_HTTP_PORT;
  432. }
  433. }
  434. if (client->timeout_ms == 0) {
  435. client->timeout_ms = DEFAULT_TIMEOUT_MS;
  436. }
  437. if (config->is_async) {
  438. client->is_async = true;
  439. }
  440. return ret;
  441. error:
  442. _clear_connection_info(client);
  443. return ret;
  444. }
  445. static esp_err_t _clear_connection_info(esp_http_client_handle_t client)
  446. {
  447. free(client->connection_info.path);
  448. free(client->connection_info.host);
  449. free(client->connection_info.query);
  450. free(client->connection_info.username);
  451. if (client->connection_info.password) {
  452. memset(client->connection_info.password, 0, strlen(client->connection_info.password));
  453. free(client->connection_info.password);
  454. }
  455. free(client->connection_info.scheme);
  456. free(client->connection_info.url);
  457. memset(&client->connection_info, 0, sizeof(connection_info_t));
  458. return ESP_OK;
  459. }
  460. static esp_err_t _clear_auth_data(esp_http_client_handle_t client)
  461. {
  462. if (client->auth_data == NULL) {
  463. return ESP_FAIL;
  464. }
  465. free(client->auth_data->method);
  466. free(client->auth_data->realm);
  467. free(client->auth_data->algorithm);
  468. free(client->auth_data->qop);
  469. free(client->auth_data->nonce);
  470. free(client->auth_data->opaque);
  471. memset(client->auth_data, 0, sizeof(esp_http_auth_data_t));
  472. return ESP_OK;
  473. }
  474. static esp_err_t esp_http_client_prepare(esp_http_client_handle_t client)
  475. {
  476. client->process_again = 0;
  477. client->response->data_process = 0;
  478. client->first_line_prepared = false;
  479. /**
  480. * Clear location field before making a new HTTP request. Location
  481. * field should not be cleared in http_on_header* callbacks because
  482. * callbacks can be invoked multiple times for same header, and
  483. * hence can lead to data corruption.
  484. */
  485. if (client->location != NULL) {
  486. free(client->location);
  487. client->location = NULL;
  488. }
  489. if (client->auth_header != NULL) {
  490. free(client->auth_header);
  491. client->auth_header = NULL;
  492. }
  493. http_parser_init(client->parser, HTTP_RESPONSE);
  494. if (client->connection_info.username) {
  495. char *auth_response = NULL;
  496. if (client->connection_info.auth_type == HTTP_AUTH_TYPE_BASIC) {
  497. auth_response = http_auth_basic(client->connection_info.username, client->connection_info.password);
  498. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  499. } else if (client->connection_info.auth_type == HTTP_AUTH_TYPE_DIGEST && client->auth_data) {
  500. client->auth_data->uri = client->connection_info.path;
  501. client->auth_data->cnonce = ((uint64_t)esp_random() << 32) + esp_random();
  502. auth_response = http_auth_digest(client->connection_info.username, client->connection_info.password, client->auth_data);
  503. client->auth_data->nc ++;
  504. #endif
  505. }
  506. if (auth_response) {
  507. ESP_LOGD(TAG, "auth_response=%s", auth_response);
  508. esp_http_client_set_header(client, "Authorization", auth_response);
  509. free(auth_response);
  510. }
  511. }
  512. return ESP_OK;
  513. }
  514. static char *_get_host_header(char *host, int port)
  515. {
  516. int err = 0;
  517. char *host_name;
  518. if (port != DEFAULT_HTTP_PORT && port != DEFAULT_HTTPS_PORT) {
  519. err = asprintf(&host_name, "%s:%d", host, port);
  520. } else {
  521. err = asprintf(&host_name, "%s", host);
  522. }
  523. if (err == -1) {
  524. return NULL;
  525. }
  526. return host_name;
  527. }
  528. esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config)
  529. {
  530. esp_http_client_handle_t client;
  531. esp_err_t ret = ESP_OK;
  532. esp_transport_handle_t tcp = NULL;
  533. char *host_name;
  534. bool _success;
  535. _success = (
  536. (client = calloc(1, sizeof(esp_http_client_t))) &&
  537. (client->parser = calloc(1, sizeof(struct http_parser))) &&
  538. (client->parser_settings = calloc(1, sizeof(struct http_parser_settings))) &&
  539. (client->auth_data = calloc(1, sizeof(esp_http_auth_data_t))) &&
  540. (client->request = calloc(1, sizeof(esp_http_data_t))) &&
  541. (client->request->headers = http_header_init()) &&
  542. (client->request->buffer = calloc(1, sizeof(esp_http_buffer_t))) &&
  543. (client->response = calloc(1, sizeof(esp_http_data_t))) &&
  544. (client->response->headers = http_header_init()) &&
  545. (client->response->buffer = calloc(1, sizeof(esp_http_buffer_t)))
  546. );
  547. if (!_success) {
  548. ESP_LOGE(TAG, "Error allocate memory");
  549. goto error;
  550. }
  551. _success = (
  552. (client->transport_list = esp_transport_list_init()) &&
  553. (tcp = esp_transport_tcp_init()) &&
  554. (esp_transport_set_default_port(tcp, DEFAULT_HTTP_PORT) == ESP_OK) &&
  555. (esp_transport_list_add(client->transport_list, tcp, "http") == ESP_OK)
  556. );
  557. if (!_success) {
  558. ESP_LOGE(TAG, "Error initialize transport");
  559. goto error;
  560. }
  561. if (config->keep_alive_enable == true) {
  562. client->keep_alive_cfg.keep_alive_enable = true;
  563. client->keep_alive_cfg.keep_alive_idle = (config->keep_alive_idle == 0) ? DEFAULT_KEEP_ALIVE_IDLE : config->keep_alive_idle;
  564. client->keep_alive_cfg.keep_alive_interval = (config->keep_alive_interval == 0) ? DEFAULT_KEEP_ALIVE_INTERVAL : config->keep_alive_interval;
  565. client->keep_alive_cfg.keep_alive_count = (config->keep_alive_count == 0) ? DEFAULT_KEEP_ALIVE_COUNT : config->keep_alive_count;
  566. esp_transport_tcp_set_keep_alive(tcp, &client->keep_alive_cfg);
  567. }
  568. if (config->if_name) {
  569. client->if_name = calloc(1, sizeof(struct ifreq) + 1);
  570. ESP_GOTO_ON_FALSE(client->if_name, ESP_FAIL, error, TAG, "Memory exhausted");
  571. memcpy(client->if_name, config->if_name, sizeof(struct ifreq));
  572. esp_transport_tcp_set_interface_name(tcp, client->if_name);
  573. }
  574. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  575. esp_transport_handle_t ssl = NULL;
  576. _success = (
  577. (ssl = esp_transport_ssl_init()) &&
  578. (esp_transport_set_default_port(ssl, DEFAULT_HTTPS_PORT) == ESP_OK) &&
  579. (esp_transport_list_add(client->transport_list, ssl, "https") == ESP_OK)
  580. );
  581. if (!_success) {
  582. ESP_LOGE(TAG, "Error initialize SSL Transport");
  583. goto error;
  584. }
  585. if (config->crt_bundle_attach != NULL) {
  586. #ifdef CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
  587. esp_transport_ssl_crt_bundle_attach(ssl, config->crt_bundle_attach);
  588. #else //CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
  589. ESP_LOGE(TAG, "use_crt_bundle configured but not enabled in menuconfig: Please enable MBEDTLS_CERTIFICATE_BUNDLE option");
  590. #endif
  591. } else if (config->use_global_ca_store == true) {
  592. esp_transport_ssl_enable_global_ca_store(ssl);
  593. } else if (config->cert_pem) {
  594. if (!config->cert_len) {
  595. esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem));
  596. } else {
  597. esp_transport_ssl_set_cert_data_der(ssl, config->cert_pem, config->cert_len);
  598. }
  599. }
  600. if (config->client_cert_pem) {
  601. if (!config->client_cert_len) {
  602. esp_transport_ssl_set_client_cert_data(ssl, config->client_cert_pem, strlen(config->client_cert_pem));
  603. } else {
  604. esp_transport_ssl_set_client_cert_data_der(ssl, config->client_cert_pem, config->client_cert_len);
  605. }
  606. }
  607. #if CONFIG_ESP_TLS_USE_SECURE_ELEMENT
  608. if (config->use_secure_element) {
  609. esp_transport_ssl_use_secure_element(ssl);
  610. }
  611. #endif
  612. if (config->client_key_pem) {
  613. if (!config->client_key_len) {
  614. esp_transport_ssl_set_client_key_data(ssl, config->client_key_pem, strlen(config->client_key_pem));
  615. } else {
  616. esp_transport_ssl_set_client_key_data_der(ssl, config->client_key_pem, config->client_key_len);
  617. }
  618. }
  619. if (config->client_key_password && config->client_key_password_len > 0) {
  620. esp_transport_ssl_set_client_key_password(ssl, config->client_key_password, config->client_key_password_len);
  621. }
  622. if (config->skip_cert_common_name_check) {
  623. esp_transport_ssl_skip_common_name_check(ssl);
  624. }
  625. if (config->common_name) {
  626. esp_transport_ssl_set_common_name(ssl, config->common_name);
  627. }
  628. #endif
  629. if (_set_config(client, config) != ESP_OK) {
  630. ESP_LOGE(TAG, "Error set configurations");
  631. goto error;
  632. }
  633. _success = (
  634. (client->request->buffer->data = malloc(client->buffer_size_tx)) &&
  635. (client->response->buffer->data = malloc(client->buffer_size_rx))
  636. );
  637. if (!_success) {
  638. ESP_LOGE(TAG, "Allocation failed");
  639. goto error;
  640. }
  641. const char *user_agent = config->user_agent == NULL ? DEFAULT_HTTP_USER_AGENT : config->user_agent;
  642. if (config->host != NULL && config->path != NULL) {
  643. if (client->connection_info.host == NULL) {
  644. ESP_LOGE(TAG, "invalid host");
  645. goto error;
  646. }
  647. host_name = _get_host_header(client->connection_info.host, client->connection_info.port);
  648. if (host_name == NULL) {
  649. ESP_LOGE(TAG, "Failed to allocate memory for host header");
  650. goto error;
  651. }
  652. _success = (
  653. (esp_http_client_set_header(client, "User-Agent", user_agent) == ESP_OK) &&
  654. (esp_http_client_set_header(client, "Host", host_name) == ESP_OK)
  655. );
  656. free(host_name);
  657. if (!_success) {
  658. ESP_LOGE(TAG, "Error while setting default configurations");
  659. goto error;
  660. }
  661. } else if (config->url != NULL) {
  662. if (esp_http_client_set_url(client, config->url) != ESP_OK) {
  663. ESP_LOGE(TAG, "Failed to set URL");
  664. goto error;
  665. }
  666. if (client->connection_info.host == NULL) {
  667. ESP_LOGE(TAG, "invalid host");
  668. goto error;
  669. }
  670. host_name = _get_host_header(client->connection_info.host, client->connection_info.port);
  671. if (host_name == NULL) {
  672. ESP_LOGE(TAG, "Failed to allocate memory for host header");
  673. goto error;
  674. }
  675. _success = (
  676. (esp_http_client_set_header(client, "User-Agent", user_agent) == ESP_OK) &&
  677. (esp_http_client_set_header(client, "Host", host_name) == ESP_OK)
  678. );
  679. free(host_name);
  680. if (!_success) {
  681. ESP_LOGE(TAG, "Error while setting default configurations");
  682. goto error;
  683. }
  684. } else {
  685. ESP_LOGE(TAG, "config should have either URL or host & path");
  686. goto error;
  687. }
  688. /* As default behavior, cache data received in fetch header state. This will be
  689. * used in esp_http_client_read API only. For esp_http_perform we shall disable
  690. * this as data will be processed by event handler */
  691. client->cache_data_in_fetch_hdr = 1;
  692. client->parser_settings->on_message_begin = http_on_message_begin;
  693. client->parser_settings->on_url = http_on_url;
  694. client->parser_settings->on_status = http_on_status;
  695. client->parser_settings->on_header_field = http_on_header_field;
  696. client->parser_settings->on_header_value = http_on_header_value;
  697. client->parser_settings->on_headers_complete = http_on_headers_complete;
  698. client->parser_settings->on_body = http_on_body;
  699. client->parser_settings->on_message_complete = http_on_message_complete;
  700. client->parser_settings->on_chunk_complete = http_on_chunk_complete;
  701. client->parser_settings->on_chunk_header = http_on_chunk_header;
  702. client->parser->data = client;
  703. client->event.client = client;
  704. client->state = HTTP_STATE_INIT;
  705. if (ret == ESP_OK) {
  706. return client;
  707. }
  708. error:
  709. esp_http_client_cleanup(client);
  710. return NULL;
  711. }
  712. static void esp_http_client_cached_buf_cleanup(esp_http_buffer_t *res_buffer)
  713. {
  714. /* Free cached data if any, that was received during fetch header stage */
  715. if (res_buffer && res_buffer->orig_raw_data) {
  716. free(res_buffer->orig_raw_data);
  717. res_buffer->orig_raw_data = NULL;
  718. res_buffer->raw_data = NULL;
  719. res_buffer->raw_len = 0;
  720. }
  721. }
  722. esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)
  723. {
  724. if (client == NULL) {
  725. return ESP_FAIL;
  726. }
  727. esp_http_client_close(client);
  728. if (client->transport_list) {
  729. esp_transport_list_destroy(client->transport_list);
  730. }
  731. if (client->request) {
  732. http_header_destroy(client->request->headers);
  733. if (client->request->buffer) {
  734. free(client->request->buffer->data);
  735. }
  736. free(client->request->buffer);
  737. free(client->request);
  738. }
  739. if (client->response) {
  740. http_header_destroy(client->response->headers);
  741. if (client->response->buffer) {
  742. free(client->response->buffer->data);
  743. esp_http_client_cached_buf_cleanup(client->response->buffer);
  744. }
  745. free(client->response->buffer);
  746. free(client->response);
  747. }
  748. if (client->if_name) {
  749. free(client->if_name);
  750. }
  751. free(client->parser);
  752. free(client->parser_settings);
  753. _clear_connection_info(client);
  754. _clear_auth_data(client);
  755. free(client->auth_data);
  756. free(client->current_header_key);
  757. free(client->location);
  758. free(client->auth_header);
  759. free(client);
  760. return ESP_OK;
  761. }
  762. esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client)
  763. {
  764. if (client == NULL) {
  765. return ESP_ERR_INVALID_ARG;
  766. }
  767. if (client->location == NULL) {
  768. return ESP_ERR_INVALID_ARG;
  769. }
  770. ESP_LOGD(TAG, "Redirect to %s", client->location);
  771. return esp_http_client_set_url(client, client->location);
  772. }
  773. static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
  774. {
  775. if (client->response->status_code >= HttpStatus_Ok && client->response->status_code < HttpStatus_MultipleChoices) {
  776. return ESP_OK;
  777. }
  778. if (client->redirect_counter >= client->max_redirection_count) {
  779. ESP_LOGE(TAG, "Error, reach max_redirection_count count=%d", client->redirect_counter);
  780. return ESP_ERR_HTTP_MAX_REDIRECT;
  781. }
  782. switch (client->response->status_code) {
  783. case HttpStatus_MovedPermanently:
  784. case HttpStatus_Found:
  785. case HttpStatus_SeeOther:
  786. case HttpStatus_TemporaryRedirect:
  787. case HttpStatus_PermanentRedirect:
  788. if (client->disable_auto_redirect) {
  789. http_dispatch_event(client, HTTP_EVENT_REDIRECT, NULL, 0);
  790. } else {
  791. ESP_ERROR_CHECK(esp_http_client_set_redirection(client));
  792. }
  793. client->redirect_counter ++;
  794. client->process_again = 1;
  795. break;
  796. case HttpStatus_Unauthorized:
  797. esp_http_client_add_auth(client);
  798. }
  799. return ESP_OK;
  800. }
  801. esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url)
  802. {
  803. esp_err_t ret = ESP_OK;
  804. char *old_host = NULL;
  805. struct http_parser_url purl;
  806. int old_port;
  807. if (client == NULL || url == NULL) {
  808. ESP_LOGE(TAG, "client or url must not NULL");
  809. return ESP_ERR_INVALID_ARG;
  810. }
  811. http_parser_url_init(&purl);
  812. int parser_status = http_parser_parse_url(url, strlen(url), 0, &purl);
  813. if (parser_status != 0) {
  814. ESP_LOGE(TAG, "Error parse url %s", url);
  815. return ESP_ERR_INVALID_ARG;
  816. }
  817. if (client->connection_info.host) {
  818. old_host = strdup(client->connection_info.host);
  819. }
  820. old_port = client->connection_info.port;
  821. if (purl.field_data[UF_HOST].len) {
  822. http_utils_assign_string(&client->connection_info.host, url + purl.field_data[UF_HOST].off, purl.field_data[UF_HOST].len);
  823. ESP_GOTO_ON_FALSE(client->connection_info.host, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
  824. }
  825. // Close the connection if host was changed
  826. if (old_host && client->connection_info.host
  827. && strcasecmp(old_host, (const void *)client->connection_info.host) != 0) {
  828. ESP_LOGD(TAG, "New host assign = %s", client->connection_info.host);
  829. if (esp_http_client_set_header(client, "Host", client->connection_info.host) != ESP_OK) {
  830. free(old_host);
  831. return ESP_ERR_NO_MEM;
  832. }
  833. /* Free cached data if any, as we are closing this connection */
  834. esp_http_client_cached_buf_cleanup(client->response->buffer);
  835. esp_http_client_close(client);
  836. }
  837. if (old_host) {
  838. free(old_host);
  839. old_host = NULL;
  840. }
  841. if (purl.field_data[UF_SCHEMA].len) {
  842. http_utils_assign_string(&client->connection_info.scheme, url + purl.field_data[UF_SCHEMA].off, purl.field_data[UF_SCHEMA].len);
  843. ESP_RETURN_ON_FALSE(client->connection_info.scheme, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  844. if (strcasecmp(client->connection_info.scheme, "http") == 0) {
  845. client->connection_info.port = DEFAULT_HTTP_PORT;
  846. } else if (strcasecmp(client->connection_info.scheme, "https") == 0) {
  847. client->connection_info.port = DEFAULT_HTTPS_PORT;
  848. }
  849. }
  850. if (purl.field_data[UF_PORT].len) {
  851. client->connection_info.port = strtol((const char*)(url + purl.field_data[UF_PORT].off), NULL, 10);
  852. }
  853. if (old_port != client->connection_info.port) {
  854. /* Free cached data if any, as we are closing this connection */
  855. esp_http_client_cached_buf_cleanup(client->response->buffer);
  856. esp_http_client_close(client);
  857. }
  858. if (purl.field_data[UF_USERINFO].len) {
  859. char *user_info = NULL;
  860. http_utils_assign_string(&user_info, url + purl.field_data[UF_USERINFO].off, purl.field_data[UF_USERINFO].len);
  861. if (user_info) {
  862. char *username = user_info;
  863. char *password = strchr(user_info, ':');
  864. if (password) {
  865. *password = 0;
  866. password ++;
  867. http_utils_assign_string(&client->connection_info.password, password, -1);
  868. ESP_RETURN_ON_FALSE(client->connection_info.password, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  869. }
  870. http_utils_assign_string(&client->connection_info.username, username, -1);
  871. ESP_RETURN_ON_FALSE(client->connection_info.username, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  872. free(user_info);
  873. } else {
  874. return ESP_ERR_NO_MEM;
  875. }
  876. }
  877. //Reset path and query if there are no information
  878. if (purl.field_data[UF_PATH].len) {
  879. http_utils_assign_string(&client->connection_info.path, url + purl.field_data[UF_PATH].off, purl.field_data[UF_PATH].len);
  880. } else {
  881. http_utils_assign_string(&client->connection_info.path, "/", -1);
  882. }
  883. ESP_RETURN_ON_FALSE(client->connection_info.path, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  884. if (purl.field_data[UF_QUERY].len) {
  885. http_utils_assign_string(&client->connection_info.query, url + purl.field_data[UF_QUERY].off, purl.field_data[UF_QUERY].len);
  886. ESP_RETURN_ON_FALSE(client->connection_info.query, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  887. } else if (client->connection_info.query) {
  888. free(client->connection_info.query);
  889. client->connection_info.query = NULL;
  890. }
  891. return ret;
  892. error:
  893. free(old_host);
  894. return ret;
  895. }
  896. int esp_http_client_get_errno(esp_http_client_handle_t client)
  897. {
  898. if (!client) {
  899. ESP_LOGE(TAG, "Invalid client handle");
  900. return -1;
  901. }
  902. return esp_transport_get_errno(client->transport);
  903. }
  904. esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method)
  905. {
  906. client->connection_info.method = method;
  907. return ESP_OK;
  908. }
  909. esp_err_t esp_http_client_set_timeout_ms(esp_http_client_handle_t client, int timeout_ms)
  910. {
  911. if (client == NULL) {
  912. return ESP_ERR_INVALID_ARG;
  913. }
  914. client->timeout_ms = timeout_ms;
  915. return ESP_OK;
  916. }
  917. static int esp_http_client_get_data(esp_http_client_handle_t client)
  918. {
  919. if (client->state < HTTP_STATE_RES_ON_DATA_START) {
  920. return ESP_FAIL;
  921. }
  922. if (client->connection_info.method == HTTP_METHOD_HEAD) {
  923. return 0;
  924. }
  925. esp_http_buffer_t *res_buffer = client->response->buffer;
  926. ESP_LOGD(TAG, "data_process=%"PRId64", content_length=%"PRId64, client->response->data_process, client->response->content_length);
  927. int rlen = esp_transport_read(client->transport, res_buffer->data, client->buffer_size_rx, client->timeout_ms);
  928. if (rlen >= 0) {
  929. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen);
  930. }
  931. return rlen;
  932. }
  933. bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client)
  934. {
  935. if (client->response->is_chunked) {
  936. if (!client->is_chunk_complete) {
  937. ESP_LOGD(TAG, "Chunks were not completely read");
  938. return false;
  939. }
  940. } else {
  941. if (client->response->data_process != client->response->content_length) {
  942. ESP_LOGD(TAG, "Data processed %"PRId64" != Data specified in content length %"PRId64, client->response->data_process, client->response->content_length);
  943. return false;
  944. }
  945. }
  946. return true;
  947. }
  948. int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
  949. {
  950. esp_http_buffer_t *res_buffer = client->response->buffer;
  951. int rlen = ESP_FAIL, ridx = 0;
  952. if (res_buffer->raw_len) {
  953. int remain_len = client->response->buffer->raw_len;
  954. if (remain_len > len) {
  955. remain_len = len;
  956. }
  957. memcpy(buffer, res_buffer->raw_data, remain_len);
  958. res_buffer->raw_len -= remain_len;
  959. res_buffer->raw_data += remain_len;
  960. ridx = remain_len;
  961. if (res_buffer->raw_len == 0) {
  962. esp_http_client_cached_buf_cleanup(res_buffer);
  963. }
  964. }
  965. int need_read = len - ridx;
  966. bool is_data_remain = true;
  967. while (need_read > 0 && is_data_remain) {
  968. if (client->response->is_chunked) {
  969. is_data_remain = !client->is_chunk_complete;
  970. } else {
  971. is_data_remain = client->response->data_process < client->response->content_length;
  972. }
  973. ESP_LOGD(TAG, "is_data_remain=%"PRId8", is_chunked=%d"PRId8", content_length=%"PRId64, is_data_remain, client->response->is_chunked, client->response->content_length);
  974. if (!is_data_remain) {
  975. break;
  976. }
  977. int byte_to_read = need_read;
  978. if (byte_to_read > client->buffer_size_rx) {
  979. byte_to_read = client->buffer_size_rx;
  980. }
  981. errno = 0;
  982. rlen = esp_transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms);
  983. ESP_LOGD(TAG, "need_read=%d, byte_to_read=%d, rlen=%d, ridx=%d", need_read, byte_to_read, rlen, ridx);
  984. if (rlen <= 0) {
  985. if (errno != 0) {
  986. esp_log_level_t sev = ESP_LOG_WARN;
  987. /* Check for cleanly closed connection */
  988. if (rlen == ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN && client->response->is_chunked) {
  989. /* Explicit call to parser for invoking `message_complete` callback */
  990. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, 0);
  991. /* ...and lowering the message severity, as closed connection from server side is expected in chunked transport */
  992. sev = ESP_LOG_DEBUG;
  993. }
  994. ESP_LOG_LEVEL(sev, TAG, "esp_transport_read returned:%d and errno:%d ", rlen, errno);
  995. }
  996. if (rlen == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {
  997. ESP_LOGD(TAG, "Connection timed out before data was ready!");
  998. /* Returning the number of bytes read upto the point where connection timed out */
  999. if (ridx) {
  1000. return ridx;
  1001. }
  1002. return -ESP_ERR_HTTP_EAGAIN;
  1003. }
  1004. if (rlen != ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN) {
  1005. esp_err_t err = esp_transport_translate_error(rlen);
  1006. ESP_LOGE(TAG, "transport_read: error - %d | %s", err, esp_err_to_name(err));
  1007. }
  1008. if (rlen < 0 && ridx == 0 && !esp_http_client_is_complete_data_received(client)) {
  1009. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1010. return ESP_FAIL;
  1011. }
  1012. return ridx;
  1013. }
  1014. res_buffer->output_ptr = buffer + ridx;
  1015. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen);
  1016. ridx += res_buffer->raw_len;
  1017. need_read -= res_buffer->raw_len;
  1018. res_buffer->raw_len = 0; //clear
  1019. res_buffer->output_ptr = NULL;
  1020. }
  1021. return ridx;
  1022. }
  1023. esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
  1024. {
  1025. esp_err_t err;
  1026. do {
  1027. if (client->process_again) {
  1028. esp_http_client_prepare(client);
  1029. }
  1030. switch (client->state) {
  1031. /* In case of blocking esp_http_client_perform(), the following states will fall through one after the after;
  1032. in case of non-blocking esp_http_client_perform(), if there is an error condition, like EINPROGRESS or EAGAIN,
  1033. then the esp_http_client_perform() API will return ESP_ERR_HTTP_EAGAIN error. The user may call
  1034. esp_http_client_perform API again, and for this reason, we maintain the states */
  1035. case HTTP_STATE_INIT:
  1036. if ((err = esp_http_client_connect(client)) != ESP_OK) {
  1037. if (client->is_async && err == ESP_ERR_HTTP_CONNECTING) {
  1038. return ESP_ERR_HTTP_EAGAIN;
  1039. }
  1040. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1041. return err;
  1042. }
  1043. /* falls through */
  1044. case HTTP_STATE_CONNECTED:
  1045. if ((err = esp_http_client_request_send(client, client->post_len)) != ESP_OK) {
  1046. if (client->is_async && errno == EAGAIN) {
  1047. return ESP_ERR_HTTP_EAGAIN;
  1048. }
  1049. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1050. return err;
  1051. }
  1052. /* falls through */
  1053. case HTTP_STATE_REQ_COMPLETE_HEADER:
  1054. if ((err = esp_http_client_send_post_data(client)) != ESP_OK) {
  1055. if (client->is_async && errno == EAGAIN) {
  1056. return ESP_ERR_HTTP_EAGAIN;
  1057. }
  1058. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1059. return err;
  1060. }
  1061. /* falls through */
  1062. case HTTP_STATE_REQ_COMPLETE_DATA:
  1063. /* Disable caching response body, as data should
  1064. * be handled by application event handler */
  1065. client->cache_data_in_fetch_hdr = 0;
  1066. int64_t ret = esp_http_client_fetch_headers(client);
  1067. if (ret < 0) {
  1068. if ((client->is_async && errno == EAGAIN) || ret == -ESP_ERR_HTTP_EAGAIN) {
  1069. return ESP_ERR_HTTP_EAGAIN;
  1070. }
  1071. /* Enable caching after error condition because next
  1072. * request could be performed using native APIs */
  1073. client->cache_data_in_fetch_hdr = 1;
  1074. if (esp_transport_get_errno(client->transport) == ENOTCONN) {
  1075. ESP_LOGW(TAG, "Close connection due to FIN received");
  1076. esp_http_client_close(client);
  1077. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1078. return ESP_ERR_HTTP_CONNECTION_CLOSED;
  1079. }
  1080. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1081. return ESP_ERR_HTTP_FETCH_HEADER;
  1082. }
  1083. /* falls through */
  1084. case HTTP_STATE_RES_ON_DATA_START:
  1085. /* Enable caching after fetch headers state because next
  1086. * request could be performed using native APIs */
  1087. client->cache_data_in_fetch_hdr = 1;
  1088. if ((err = esp_http_check_response(client)) != ESP_OK) {
  1089. ESP_LOGE(TAG, "Error response");
  1090. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1091. return err;
  1092. }
  1093. while (client->response->is_chunked && !client->is_chunk_complete) {
  1094. if (esp_http_client_get_data(client) <= 0) {
  1095. if (client->is_async && errno == EAGAIN) {
  1096. return ESP_ERR_HTTP_EAGAIN;
  1097. }
  1098. ESP_LOGD(TAG, "Read finish or server requests close");
  1099. break;
  1100. }
  1101. }
  1102. while (client->response->data_process < client->response->content_length) {
  1103. if (esp_http_client_get_data(client) <= 0) {
  1104. if (client->is_async && errno == EAGAIN) {
  1105. return ESP_ERR_HTTP_EAGAIN;
  1106. }
  1107. ESP_LOGD(TAG, "Read finish or server requests close");
  1108. break;
  1109. }
  1110. }
  1111. http_dispatch_event(client, HTTP_EVENT_ON_FINISH, NULL, 0);
  1112. client->response->buffer->raw_len = 0;
  1113. if (!http_should_keep_alive(client->parser)) {
  1114. ESP_LOGD(TAG, "Close connection");
  1115. esp_http_client_close(client);
  1116. } else {
  1117. if (client->state > HTTP_STATE_CONNECTED) {
  1118. client->state = HTTP_STATE_CONNECTED;
  1119. client->first_line_prepared = false;
  1120. }
  1121. }
  1122. break;
  1123. default:
  1124. break;
  1125. }
  1126. } while (client->process_again);
  1127. return ESP_OK;
  1128. }
  1129. int64_t esp_http_client_fetch_headers(esp_http_client_handle_t client)
  1130. {
  1131. if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
  1132. return ESP_FAIL;
  1133. }
  1134. client->state = HTTP_STATE_REQ_COMPLETE_DATA;
  1135. esp_http_buffer_t *buffer = client->response->buffer;
  1136. client->response->status_code = -1;
  1137. while (client->state < HTTP_STATE_RES_COMPLETE_HEADER) {
  1138. buffer->len = esp_transport_read(client->transport, buffer->data, client->buffer_size_rx, client->timeout_ms);
  1139. if (buffer->len <= 0) {
  1140. if (buffer->len == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {
  1141. ESP_LOGW(TAG, "Connection timed out before data was ready!");
  1142. return -ESP_ERR_HTTP_EAGAIN;
  1143. }
  1144. return ESP_FAIL;
  1145. }
  1146. http_parser_execute(client->parser, client->parser_settings, buffer->data, buffer->len);
  1147. }
  1148. client->state = HTTP_STATE_RES_ON_DATA_START;
  1149. ESP_LOGD(TAG, "content_length = %"PRId64, client->response->content_length);
  1150. if (client->response->content_length <= 0) {
  1151. client->response->is_chunked = true;
  1152. return 0;
  1153. }
  1154. return client->response->content_length;
  1155. }
  1156. static esp_err_t esp_http_client_connect(esp_http_client_handle_t client)
  1157. {
  1158. esp_err_t err;
  1159. if (client->state == HTTP_STATE_UNINIT) {
  1160. ESP_LOGE(TAG, "Client has not been initialized");
  1161. return ESP_ERR_INVALID_STATE;
  1162. }
  1163. if ((err = esp_http_client_prepare(client)) != ESP_OK) {
  1164. ESP_LOGE(TAG, "Failed to initialize request data");
  1165. esp_http_client_close(client);
  1166. return err;
  1167. }
  1168. if (client->state < HTTP_STATE_CONNECTED) {
  1169. ESP_LOGD(TAG, "Begin connect to: %s://%s:%d", client->connection_info.scheme, client->connection_info.host, client->connection_info.port);
  1170. client->transport = esp_transport_list_get_transport(client->transport_list, client->connection_info.scheme);
  1171. if (client->transport == NULL) {
  1172. ESP_LOGE(TAG, "No transport found");
  1173. #ifndef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  1174. if (strcasecmp(client->connection_info.scheme, "https") == 0) {
  1175. ESP_LOGE(TAG, "Please enable HTTPS at menuconfig to allow requesting via https");
  1176. }
  1177. #endif
  1178. return ESP_ERR_HTTP_INVALID_TRANSPORT;
  1179. }
  1180. if (!client->is_async) {
  1181. if (esp_transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) {
  1182. ESP_LOGE(TAG, "Connection failed, sock < 0");
  1183. return ESP_ERR_HTTP_CONNECT;
  1184. }
  1185. } else {
  1186. int ret = esp_transport_connect_async(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms);
  1187. if (ret == ASYNC_TRANS_CONNECT_FAIL) {
  1188. ESP_LOGE(TAG, "Connection failed");
  1189. if (strcasecmp(client->connection_info.scheme, "http") == 0) {
  1190. ESP_LOGE(TAG, "Asynchronous mode doesn't work for HTTP based connection");
  1191. return ESP_ERR_INVALID_ARG;
  1192. }
  1193. return ESP_ERR_HTTP_CONNECT;
  1194. } else if (ret == ASYNC_TRANS_CONNECTING) {
  1195. ESP_LOGD(TAG, "Connection not yet established");
  1196. return ESP_ERR_HTTP_CONNECTING;
  1197. }
  1198. }
  1199. client->state = HTTP_STATE_CONNECTED;
  1200. http_dispatch_event(client, HTTP_EVENT_ON_CONNECTED, NULL, 0);
  1201. }
  1202. return ESP_OK;
  1203. }
  1204. static int http_client_prepare_first_line(esp_http_client_handle_t client, int write_len)
  1205. {
  1206. if (write_len >= 0) {
  1207. http_header_set_format(client->request->headers, "Content-Length", "%d", write_len);
  1208. } else {
  1209. esp_http_client_set_header(client, "Transfer-Encoding", "chunked");
  1210. }
  1211. const char *method = HTTP_METHOD_MAPPING[client->connection_info.method];
  1212. int first_line_len = snprintf(client->request->buffer->data,
  1213. client->buffer_size_tx, "%s %s",
  1214. method,
  1215. client->connection_info.path);
  1216. if (first_line_len >= client->buffer_size_tx) {
  1217. ESP_LOGE(TAG, "Out of buffer");
  1218. return -1;
  1219. }
  1220. if (client->connection_info.query) {
  1221. first_line_len += snprintf(client->request->buffer->data + first_line_len,
  1222. client->buffer_size_tx - first_line_len, "?%s", client->connection_info.query);
  1223. if (first_line_len >= client->buffer_size_tx) {
  1224. ESP_LOGE(TAG, "Out of buffer");
  1225. return -1;
  1226. }
  1227. }
  1228. first_line_len += snprintf(client->request->buffer->data + first_line_len,
  1229. client->buffer_size_tx - first_line_len, " %s\r\n", DEFAULT_HTTP_PROTOCOL);
  1230. if (first_line_len >= client->buffer_size_tx) {
  1231. ESP_LOGE(TAG, "Out of buffer");
  1232. return -1;
  1233. }
  1234. return first_line_len;
  1235. }
  1236. static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len)
  1237. {
  1238. int first_line_len = 0;
  1239. if (!client->first_line_prepared) {
  1240. if ((first_line_len = http_client_prepare_first_line(client, write_len)) < 0) {
  1241. return first_line_len;
  1242. }
  1243. client->first_line_prepared = true;
  1244. client->header_index = 0;
  1245. client->data_written_index = 0;
  1246. client->data_write_left = 0;
  1247. }
  1248. if (client->data_write_left > 0) {
  1249. /* sending leftover data from previous call to esp_http_client_request_send() API */
  1250. int wret = 0;
  1251. if (((wret = esp_http_client_write(client, client->request->buffer->data + client->data_written_index, client->data_write_left)) < 0)) {
  1252. ESP_LOGE(TAG, "Error write request");
  1253. return ESP_ERR_HTTP_WRITE_DATA;
  1254. }
  1255. client->data_write_left -= wret;
  1256. client->data_written_index += wret;
  1257. if (client->is_async && client->data_write_left > 0) {
  1258. return ESP_ERR_HTTP_WRITE_DATA; /* In case of EAGAIN error, we return ESP_ERR_HTTP_WRITE_DATA,
  1259. and the handling of EAGAIN should be done in the higher level APIs. */
  1260. }
  1261. }
  1262. int wlen = client->buffer_size_tx - first_line_len;
  1263. while ((client->header_index = http_header_generate_string(client->request->headers, client->header_index, client->request->buffer->data + first_line_len, &wlen))) {
  1264. if (wlen <= 0) {
  1265. break;
  1266. }
  1267. if (first_line_len) {
  1268. wlen += first_line_len;
  1269. first_line_len = 0;
  1270. }
  1271. client->request->buffer->data[wlen] = 0;
  1272. ESP_LOGD(TAG, "Write header[%d]: %s", client->header_index, client->request->buffer->data);
  1273. client->data_write_left = wlen;
  1274. client->data_written_index = 0;
  1275. while (client->data_write_left > 0) {
  1276. int wret = esp_transport_write(client->transport, client->request->buffer->data + client->data_written_index, client->data_write_left, client->timeout_ms);
  1277. if (wret <= 0) {
  1278. ESP_LOGE(TAG, "Error write request");
  1279. esp_http_client_close(client);
  1280. return ESP_ERR_HTTP_WRITE_DATA;
  1281. }
  1282. client->data_write_left -= wret;
  1283. client->data_written_index += wret;
  1284. }
  1285. wlen = client->buffer_size_tx;
  1286. }
  1287. client->data_written_index = 0;
  1288. client->data_write_left = client->post_len;
  1289. http_dispatch_event(client, HTTP_EVENT_HEADERS_SENT, NULL, 0);
  1290. client->state = HTTP_STATE_REQ_COMPLETE_HEADER;
  1291. return ESP_OK;
  1292. }
  1293. static esp_err_t esp_http_client_send_post_data(esp_http_client_handle_t client)
  1294. {
  1295. if (client->state != HTTP_STATE_REQ_COMPLETE_HEADER) {
  1296. ESP_LOGE(TAG, "Invalid state");
  1297. return ESP_ERR_INVALID_STATE;
  1298. }
  1299. if (!(client->post_data && client->post_len)) {
  1300. goto success;
  1301. }
  1302. int wret = esp_http_client_write(client, client->post_data + client->data_written_index, client->data_write_left);
  1303. if (wret < 0) {
  1304. return wret;
  1305. }
  1306. client->data_write_left -= wret;
  1307. client->data_written_index += wret;
  1308. if (client->data_write_left <= 0) {
  1309. goto success;
  1310. } else {
  1311. return ESP_ERR_HTTP_WRITE_DATA;
  1312. }
  1313. success:
  1314. client->state = HTTP_STATE_REQ_COMPLETE_DATA;
  1315. return ESP_OK;
  1316. }
  1317. esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)
  1318. {
  1319. client->post_len = write_len;
  1320. esp_err_t err;
  1321. if ((err = esp_http_client_connect(client)) != ESP_OK) {
  1322. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1323. return err;
  1324. }
  1325. if ((err = esp_http_client_request_send(client, write_len)) != ESP_OK) {
  1326. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1327. return err;
  1328. }
  1329. return ESP_OK;
  1330. }
  1331. int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, int len)
  1332. {
  1333. if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
  1334. return ESP_FAIL;
  1335. }
  1336. int wlen = 0, widx = 0;
  1337. while (len > 0) {
  1338. wlen = esp_transport_write(client->transport, buffer + widx, len, client->timeout_ms);
  1339. /* client->async_block is initialised in case of non-blocking IO, and in this case we return how
  1340. much ever data was written by the esp_transport_write() API. */
  1341. if (client->is_async || wlen <= 0) {
  1342. return wlen;
  1343. }
  1344. widx += wlen;
  1345. len -= wlen;
  1346. }
  1347. return widx;
  1348. }
  1349. esp_err_t esp_http_client_close(esp_http_client_handle_t client)
  1350. {
  1351. if (client->state >= HTTP_STATE_INIT) {
  1352. http_dispatch_event(client, HTTP_EVENT_DISCONNECTED, esp_transport_get_error_handle(client->transport), 0);
  1353. client->state = HTTP_STATE_INIT;
  1354. return esp_transport_close(client->transport);
  1355. }
  1356. return ESP_OK;
  1357. }
  1358. esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len)
  1359. {
  1360. esp_err_t err = ESP_OK;
  1361. client->post_data = (char *)data;
  1362. client->post_len = len;
  1363. ESP_LOGD(TAG, "set post file length = %d", len);
  1364. if (client->post_data) {
  1365. char *value = NULL;
  1366. if ((err = esp_http_client_get_header(client, "Content-Type", &value)) != ESP_OK) {
  1367. return err;
  1368. }
  1369. if (value == NULL) {
  1370. err = esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
  1371. }
  1372. } else {
  1373. client->post_len = 0;
  1374. err = esp_http_client_set_header(client, "Content-Type", NULL);
  1375. }
  1376. return err;
  1377. }
  1378. int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data)
  1379. {
  1380. if (client->post_data) {
  1381. *data = client->post_data;
  1382. return client->post_len;
  1383. }
  1384. return 0;
  1385. }
  1386. int esp_http_client_get_status_code(esp_http_client_handle_t client)
  1387. {
  1388. return client->response->status_code;
  1389. }
  1390. int64_t esp_http_client_get_content_length(esp_http_client_handle_t client)
  1391. {
  1392. return client->response->content_length;
  1393. }
  1394. bool esp_http_client_is_chunked_response(esp_http_client_handle_t client)
  1395. {
  1396. return client->response->is_chunked;
  1397. }
  1398. esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client)
  1399. {
  1400. if (!strcasecmp(client->connection_info.scheme, "https") ) {
  1401. return HTTP_TRANSPORT_OVER_SSL;
  1402. } else if (!strcasecmp(client->connection_info.scheme, "http")) {
  1403. return HTTP_TRANSPORT_OVER_TCP;
  1404. } else {
  1405. return HTTP_TRANSPORT_UNKNOWN;
  1406. }
  1407. }
  1408. void esp_http_client_add_auth(esp_http_client_handle_t client)
  1409. {
  1410. if (client == NULL) {
  1411. return;
  1412. }
  1413. if (client->state != HTTP_STATE_RES_ON_DATA_START) {
  1414. return;
  1415. }
  1416. if (client->redirect_counter >= client->max_authorization_retries) {
  1417. ESP_LOGE(TAG, "Error, reached max_authorization_retries count=%d", client->redirect_counter);
  1418. return;
  1419. }
  1420. char *auth_header = client->auth_header;
  1421. if (auth_header) {
  1422. http_utils_trim_whitespace(&auth_header);
  1423. ESP_LOGD(TAG, "UNAUTHORIZED: %s", auth_header);
  1424. client->redirect_counter++;
  1425. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  1426. if (http_utils_str_starts_with(auth_header, "Digest") == 0) {
  1427. ESP_LOGD(TAG, "type = Digest");
  1428. client->connection_info.auth_type = HTTP_AUTH_TYPE_DIGEST;
  1429. } else {
  1430. #endif
  1431. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
  1432. if (http_utils_str_starts_with(auth_header, "Basic") == 0) {
  1433. ESP_LOGD(TAG, "type = Basic");
  1434. client->connection_info.auth_type = HTTP_AUTH_TYPE_BASIC;
  1435. } else {
  1436. #endif
  1437. client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE;
  1438. ESP_LOGE(TAG, "This authentication method is not supported: %s", auth_header);
  1439. return;
  1440. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
  1441. }
  1442. #endif
  1443. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  1444. }
  1445. #endif
  1446. _clear_auth_data(client);
  1447. client->auth_data->method = strdup(HTTP_METHOD_MAPPING[client->connection_info.method]);
  1448. client->auth_data->nc = 1;
  1449. client->auth_data->realm = http_utils_get_string_between(auth_header, "realm=\"", "\"");
  1450. client->auth_data->algorithm = http_utils_get_string_between(auth_header, "algorithm=", ",");
  1451. if (client->auth_data->algorithm == NULL) {
  1452. client->auth_data->algorithm = strdup("MD5");
  1453. }
  1454. client->auth_data->qop = http_utils_get_string_between(auth_header, "qop=\"", "\"");
  1455. client->auth_data->nonce = http_utils_get_string_between(auth_header, "nonce=\"", "\"");
  1456. client->auth_data->opaque = http_utils_get_string_between(auth_header, "opaque=\"", "\"");
  1457. client->process_again = 1;
  1458. } else {
  1459. client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE;
  1460. ESP_LOGW(TAG, "This request requires authentication, but does not provide header information for that");
  1461. }
  1462. }
  1463. int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, int len)
  1464. {
  1465. int read_len = 0;
  1466. while (read_len < len) {
  1467. int data_read = esp_http_client_read(client, buffer + read_len, len - read_len);
  1468. if (data_read <= 0) {
  1469. return read_len;
  1470. }
  1471. read_len += data_read;
  1472. }
  1473. return read_len;
  1474. }
  1475. esp_err_t esp_http_client_flush_response(esp_http_client_handle_t client, int *len)
  1476. {
  1477. if (client == NULL) {
  1478. ESP_LOGE(TAG, "client must not be NULL");
  1479. return ESP_ERR_INVALID_ARG;
  1480. }
  1481. int read_len = 0;
  1482. while (!esp_http_client_is_complete_data_received(client)) {
  1483. int data_read = esp_http_client_get_data(client);
  1484. if (data_read < 0) {
  1485. return ESP_FAIL;
  1486. }
  1487. read_len += data_read;
  1488. }
  1489. if (len) {
  1490. *len = read_len;
  1491. }
  1492. return ESP_OK;
  1493. }
  1494. esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len)
  1495. {
  1496. if (client == NULL || url == NULL) {
  1497. return ESP_ERR_INVALID_ARG;
  1498. }
  1499. if (client->connection_info.host && client->connection_info.scheme && client->connection_info.path) {
  1500. snprintf(url, len, "%s://%s%s", client->connection_info.scheme, client->connection_info.host, client->connection_info.path);
  1501. return ESP_OK;
  1502. } else {
  1503. ESP_LOGE(TAG, "Failed to get URL");
  1504. }
  1505. return ESP_FAIL;
  1506. }
  1507. esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int *len)
  1508. {
  1509. if (client == NULL || len == NULL) {
  1510. return ESP_ERR_INVALID_ARG;
  1511. }
  1512. if (esp_http_client_is_chunked_response(client)) {
  1513. *len = client->response->chunk_length;
  1514. } else {
  1515. ESP_LOGE(TAG, "Response is not chunked");
  1516. return ESP_FAIL;
  1517. }
  1518. return ESP_OK;
  1519. }