esp_http_client.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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_LOGD(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. esp_err_t err = esp_http_client_set_url(client, client->location);
  772. if (err == ESP_OK) {
  773. client->redirect_counter ++;
  774. client->process_again = 1; // used only in the blocking mode (when esp_http_client_perform() is called)
  775. }
  776. return err;
  777. }
  778. static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
  779. {
  780. if (client->response->status_code >= HttpStatus_Ok && client->response->status_code < HttpStatus_MultipleChoices) {
  781. return ESP_OK;
  782. }
  783. if (client->redirect_counter >= client->max_redirection_count) {
  784. ESP_LOGE(TAG, "Error, reach max_redirection_count count=%d", client->redirect_counter);
  785. return ESP_ERR_HTTP_MAX_REDIRECT;
  786. }
  787. switch (client->response->status_code) {
  788. case HttpStatus_MovedPermanently:
  789. case HttpStatus_Found:
  790. case HttpStatus_SeeOther:
  791. case HttpStatus_TemporaryRedirect:
  792. case HttpStatus_PermanentRedirect:
  793. if (client->disable_auto_redirect) {
  794. http_dispatch_event(client, HTTP_EVENT_REDIRECT, NULL, 0);
  795. } else {
  796. if (esp_http_client_set_redirection(client) != ESP_OK){
  797. return ESP_FAIL;
  798. };
  799. }
  800. break;
  801. case HttpStatus_Unauthorized:
  802. esp_http_client_add_auth(client);
  803. }
  804. return ESP_OK;
  805. }
  806. esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url)
  807. {
  808. esp_err_t ret = ESP_OK;
  809. char *old_host = NULL;
  810. struct http_parser_url purl;
  811. int old_port;
  812. if (client == NULL || url == NULL) {
  813. ESP_LOGE(TAG, "client or url must not NULL");
  814. return ESP_ERR_INVALID_ARG;
  815. }
  816. http_parser_url_init(&purl);
  817. int parser_status = http_parser_parse_url(url, strlen(url), 0, &purl);
  818. if (parser_status != 0) {
  819. ESP_LOGE(TAG, "Error parse url %s", url);
  820. return ESP_ERR_INVALID_ARG;
  821. }
  822. if (client->connection_info.host) {
  823. old_host = strdup(client->connection_info.host);
  824. }
  825. old_port = client->connection_info.port;
  826. if (purl.field_data[UF_HOST].len) {
  827. http_utils_assign_string(&client->connection_info.host, url + purl.field_data[UF_HOST].off, purl.field_data[UF_HOST].len);
  828. ESP_GOTO_ON_FALSE(client->connection_info.host, ESP_ERR_NO_MEM, error, TAG, "Memory exhausted");
  829. }
  830. // Close the connection if host was changed
  831. if (old_host && client->connection_info.host
  832. && strcasecmp(old_host, (const void *)client->connection_info.host) != 0) {
  833. ESP_LOGD(TAG, "New host assign = %s", client->connection_info.host);
  834. if (esp_http_client_set_header(client, "Host", client->connection_info.host) != ESP_OK) {
  835. free(old_host);
  836. return ESP_ERR_NO_MEM;
  837. }
  838. /* Free cached data if any, as we are closing this connection */
  839. esp_http_client_cached_buf_cleanup(client->response->buffer);
  840. esp_http_client_close(client);
  841. }
  842. if (old_host) {
  843. free(old_host);
  844. old_host = NULL;
  845. }
  846. if (purl.field_data[UF_SCHEMA].len) {
  847. http_utils_assign_string(&client->connection_info.scheme, url + purl.field_data[UF_SCHEMA].off, purl.field_data[UF_SCHEMA].len);
  848. ESP_RETURN_ON_FALSE(client->connection_info.scheme, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  849. if (strcasecmp(client->connection_info.scheme, "http") == 0) {
  850. client->connection_info.port = DEFAULT_HTTP_PORT;
  851. } else if (strcasecmp(client->connection_info.scheme, "https") == 0) {
  852. client->connection_info.port = DEFAULT_HTTPS_PORT;
  853. }
  854. }
  855. if (purl.field_data[UF_PORT].len) {
  856. client->connection_info.port = strtol((const char*)(url + purl.field_data[UF_PORT].off), NULL, 10);
  857. }
  858. if (old_port != client->connection_info.port) {
  859. /* Free cached data if any, as we are closing this connection */
  860. esp_http_client_cached_buf_cleanup(client->response->buffer);
  861. esp_http_client_close(client);
  862. }
  863. if (purl.field_data[UF_USERINFO].len) {
  864. char *user_info = NULL;
  865. http_utils_assign_string(&user_info, url + purl.field_data[UF_USERINFO].off, purl.field_data[UF_USERINFO].len);
  866. if (user_info) {
  867. char *username = user_info;
  868. char *password = strchr(user_info, ':');
  869. if (password) {
  870. *password = 0;
  871. password ++;
  872. http_utils_assign_string(&client->connection_info.password, password, -1);
  873. ESP_RETURN_ON_FALSE(client->connection_info.password, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  874. }
  875. http_utils_assign_string(&client->connection_info.username, username, -1);
  876. ESP_RETURN_ON_FALSE(client->connection_info.username, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  877. free(user_info);
  878. } else {
  879. return ESP_ERR_NO_MEM;
  880. }
  881. }
  882. //Reset path and query if there are no information
  883. if (purl.field_data[UF_PATH].len) {
  884. http_utils_assign_string(&client->connection_info.path, url + purl.field_data[UF_PATH].off, purl.field_data[UF_PATH].len);
  885. } else {
  886. http_utils_assign_string(&client->connection_info.path, "/", -1);
  887. }
  888. ESP_RETURN_ON_FALSE(client->connection_info.path, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  889. if (purl.field_data[UF_QUERY].len) {
  890. http_utils_assign_string(&client->connection_info.query, url + purl.field_data[UF_QUERY].off, purl.field_data[UF_QUERY].len);
  891. ESP_RETURN_ON_FALSE(client->connection_info.query, ESP_ERR_NO_MEM, TAG, "Memory exhausted");
  892. } else if (client->connection_info.query) {
  893. free(client->connection_info.query);
  894. client->connection_info.query = NULL;
  895. }
  896. return ret;
  897. error:
  898. free(old_host);
  899. return ret;
  900. }
  901. int esp_http_client_get_errno(esp_http_client_handle_t client)
  902. {
  903. if (!client) {
  904. ESP_LOGE(TAG, "Invalid client handle");
  905. return -1;
  906. }
  907. return esp_transport_get_errno(client->transport);
  908. }
  909. esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method)
  910. {
  911. client->connection_info.method = method;
  912. return ESP_OK;
  913. }
  914. esp_err_t esp_http_client_set_timeout_ms(esp_http_client_handle_t client, int timeout_ms)
  915. {
  916. if (client == NULL) {
  917. return ESP_ERR_INVALID_ARG;
  918. }
  919. client->timeout_ms = timeout_ms;
  920. return ESP_OK;
  921. }
  922. static int esp_http_client_get_data(esp_http_client_handle_t client)
  923. {
  924. if (client->state < HTTP_STATE_RES_ON_DATA_START) {
  925. return ESP_FAIL;
  926. }
  927. if (client->connection_info.method == HTTP_METHOD_HEAD) {
  928. return 0;
  929. }
  930. esp_http_buffer_t *res_buffer = client->response->buffer;
  931. ESP_LOGD(TAG, "data_process=%"PRId64", content_length=%"PRId64, client->response->data_process, client->response->content_length);
  932. int rlen = esp_transport_read(client->transport, res_buffer->data, client->buffer_size_rx, client->timeout_ms);
  933. if (rlen >= 0) {
  934. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen);
  935. }
  936. return rlen;
  937. }
  938. bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client)
  939. {
  940. if (client->response->is_chunked) {
  941. if (!client->is_chunk_complete) {
  942. ESP_LOGD(TAG, "Chunks were not completely read");
  943. return false;
  944. }
  945. } else {
  946. if (client->response->data_process != client->response->content_length) {
  947. ESP_LOGD(TAG, "Data processed %"PRId64" != Data specified in content length %"PRId64, client->response->data_process, client->response->content_length);
  948. return false;
  949. }
  950. }
  951. return true;
  952. }
  953. int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
  954. {
  955. esp_http_buffer_t *res_buffer = client->response->buffer;
  956. int rlen = ESP_FAIL, ridx = 0;
  957. if (res_buffer->raw_len) {
  958. int remain_len = client->response->buffer->raw_len;
  959. if (remain_len > len) {
  960. remain_len = len;
  961. }
  962. memcpy(buffer, res_buffer->raw_data, remain_len);
  963. res_buffer->raw_len -= remain_len;
  964. res_buffer->raw_data += remain_len;
  965. ridx = remain_len;
  966. if (res_buffer->raw_len == 0) {
  967. esp_http_client_cached_buf_cleanup(res_buffer);
  968. }
  969. }
  970. int need_read = len - ridx;
  971. bool is_data_remain = true;
  972. while (need_read > 0 && is_data_remain) {
  973. if (client->response->is_chunked) {
  974. is_data_remain = !client->is_chunk_complete;
  975. } else {
  976. is_data_remain = client->response->data_process < client->response->content_length;
  977. }
  978. 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);
  979. if (!is_data_remain) {
  980. break;
  981. }
  982. int byte_to_read = need_read;
  983. if (byte_to_read > client->buffer_size_rx) {
  984. byte_to_read = client->buffer_size_rx;
  985. }
  986. errno = 0;
  987. rlen = esp_transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms);
  988. ESP_LOGD(TAG, "need_read=%d, byte_to_read=%d, rlen=%d, ridx=%d", need_read, byte_to_read, rlen, ridx);
  989. if (rlen <= 0) {
  990. if (errno != 0) {
  991. esp_log_level_t sev = ESP_LOG_WARN;
  992. /* Check for cleanly closed connection */
  993. if (rlen == ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN && client->response->is_chunked) {
  994. /* Explicit call to parser for invoking `message_complete` callback */
  995. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, 0);
  996. /* ...and lowering the message severity, as closed connection from server side is expected in chunked transport */
  997. sev = ESP_LOG_DEBUG;
  998. }
  999. ESP_LOG_LEVEL(sev, TAG, "esp_transport_read returned:%d and errno:%d ", rlen, errno);
  1000. }
  1001. if (rlen == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {
  1002. ESP_LOGD(TAG, "Connection timed out before data was ready!");
  1003. /* Returning the number of bytes read upto the point where connection timed out */
  1004. if (ridx) {
  1005. return ridx;
  1006. }
  1007. return -ESP_ERR_HTTP_EAGAIN;
  1008. }
  1009. if (rlen != ERR_TCP_TRANSPORT_CONNECTION_CLOSED_BY_FIN) {
  1010. esp_err_t err = esp_transport_translate_error(rlen);
  1011. ESP_LOGE(TAG, "transport_read: error - %d | %s", err, esp_err_to_name(err));
  1012. }
  1013. if (rlen < 0 && ridx == 0 && !esp_http_client_is_complete_data_received(client)) {
  1014. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1015. return ESP_FAIL;
  1016. }
  1017. return ridx;
  1018. }
  1019. res_buffer->output_ptr = buffer + ridx;
  1020. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen);
  1021. ridx += res_buffer->raw_len;
  1022. need_read -= res_buffer->raw_len;
  1023. res_buffer->raw_len = 0; //clear
  1024. res_buffer->output_ptr = NULL;
  1025. }
  1026. return ridx;
  1027. }
  1028. esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
  1029. {
  1030. esp_err_t err;
  1031. do {
  1032. if (client->process_again) {
  1033. esp_http_client_prepare(client);
  1034. }
  1035. switch (client->state) {
  1036. /* In case of blocking esp_http_client_perform(), the following states will fall through one after the after;
  1037. in case of non-blocking esp_http_client_perform(), if there is an error condition, like EINPROGRESS or EAGAIN,
  1038. then the esp_http_client_perform() API will return ESP_ERR_HTTP_EAGAIN error. The user may call
  1039. esp_http_client_perform API again, and for this reason, we maintain the states */
  1040. case HTTP_STATE_INIT:
  1041. if ((err = esp_http_client_connect(client)) != ESP_OK) {
  1042. if (client->is_async && err == ESP_ERR_HTTP_CONNECTING) {
  1043. return ESP_ERR_HTTP_EAGAIN;
  1044. }
  1045. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1046. return err;
  1047. }
  1048. /* falls through */
  1049. case HTTP_STATE_CONNECTED:
  1050. if ((err = esp_http_client_request_send(client, client->post_len)) != ESP_OK) {
  1051. if (client->is_async && errno == EAGAIN) {
  1052. return ESP_ERR_HTTP_EAGAIN;
  1053. }
  1054. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1055. return err;
  1056. }
  1057. /* falls through */
  1058. case HTTP_STATE_REQ_COMPLETE_HEADER:
  1059. if ((err = esp_http_client_send_post_data(client)) != ESP_OK) {
  1060. if (client->is_async && errno == EAGAIN) {
  1061. return ESP_ERR_HTTP_EAGAIN;
  1062. }
  1063. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1064. return err;
  1065. }
  1066. /* falls through */
  1067. case HTTP_STATE_REQ_COMPLETE_DATA:
  1068. /* Disable caching response body, as data should
  1069. * be handled by application event handler */
  1070. client->cache_data_in_fetch_hdr = 0;
  1071. int64_t ret = esp_http_client_fetch_headers(client);
  1072. if (ret < 0) {
  1073. if ((client->is_async && errno == EAGAIN) || ret == -ESP_ERR_HTTP_EAGAIN) {
  1074. return ESP_ERR_HTTP_EAGAIN;
  1075. }
  1076. /* Enable caching after error condition because next
  1077. * request could be performed using native APIs */
  1078. client->cache_data_in_fetch_hdr = 1;
  1079. if (esp_transport_get_errno(client->transport) == ENOTCONN) {
  1080. ESP_LOGW(TAG, "Close connection due to FIN received");
  1081. esp_http_client_close(client);
  1082. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1083. return ESP_ERR_HTTP_CONNECTION_CLOSED;
  1084. }
  1085. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1086. return ESP_ERR_HTTP_FETCH_HEADER;
  1087. }
  1088. /* falls through */
  1089. case HTTP_STATE_RES_ON_DATA_START:
  1090. /* Enable caching after fetch headers state because next
  1091. * request could be performed using native APIs */
  1092. client->cache_data_in_fetch_hdr = 1;
  1093. if ((err = esp_http_check_response(client)) != ESP_OK) {
  1094. ESP_LOGE(TAG, "Error response");
  1095. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1096. return err;
  1097. }
  1098. while (client->response->is_chunked && !client->is_chunk_complete) {
  1099. if (esp_http_client_get_data(client) <= 0) {
  1100. if (client->is_async && errno == EAGAIN) {
  1101. return ESP_ERR_HTTP_EAGAIN;
  1102. }
  1103. ESP_LOGD(TAG, "Read finish or server requests close");
  1104. break;
  1105. }
  1106. }
  1107. while (client->response->data_process < client->response->content_length) {
  1108. if (esp_http_client_get_data(client) <= 0) {
  1109. if (client->is_async && errno == EAGAIN) {
  1110. return ESP_ERR_HTTP_EAGAIN;
  1111. }
  1112. ESP_LOGD(TAG, "Read finish or server requests close");
  1113. break;
  1114. }
  1115. }
  1116. http_dispatch_event(client, HTTP_EVENT_ON_FINISH, NULL, 0);
  1117. client->response->buffer->raw_len = 0;
  1118. if (!http_should_keep_alive(client->parser)) {
  1119. ESP_LOGD(TAG, "Close connection");
  1120. esp_http_client_close(client);
  1121. } else {
  1122. if (client->state > HTTP_STATE_CONNECTED) {
  1123. client->state = HTTP_STATE_CONNECTED;
  1124. client->first_line_prepared = false;
  1125. }
  1126. }
  1127. break;
  1128. default:
  1129. break;
  1130. }
  1131. } while (client->process_again);
  1132. return ESP_OK;
  1133. }
  1134. int64_t esp_http_client_fetch_headers(esp_http_client_handle_t client)
  1135. {
  1136. if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
  1137. return ESP_FAIL;
  1138. }
  1139. client->state = HTTP_STATE_REQ_COMPLETE_DATA;
  1140. esp_http_buffer_t *buffer = client->response->buffer;
  1141. client->response->status_code = -1;
  1142. while (client->state < HTTP_STATE_RES_COMPLETE_HEADER) {
  1143. buffer->len = esp_transport_read(client->transport, buffer->data, client->buffer_size_rx, client->timeout_ms);
  1144. if (buffer->len <= 0) {
  1145. if (buffer->len == ERR_TCP_TRANSPORT_CONNECTION_TIMEOUT) {
  1146. ESP_LOGW(TAG, "Connection timed out before data was ready!");
  1147. return -ESP_ERR_HTTP_EAGAIN;
  1148. }
  1149. return ESP_FAIL;
  1150. }
  1151. http_parser_execute(client->parser, client->parser_settings, buffer->data, buffer->len);
  1152. }
  1153. client->state = HTTP_STATE_RES_ON_DATA_START;
  1154. ESP_LOGD(TAG, "content_length = %"PRId64, client->response->content_length);
  1155. if (client->response->content_length <= 0) {
  1156. client->response->is_chunked = true;
  1157. return 0;
  1158. }
  1159. return client->response->content_length;
  1160. }
  1161. static esp_err_t esp_http_client_connect(esp_http_client_handle_t client)
  1162. {
  1163. esp_err_t err;
  1164. if (client->state == HTTP_STATE_UNINIT) {
  1165. ESP_LOGE(TAG, "Client has not been initialized");
  1166. return ESP_ERR_INVALID_STATE;
  1167. }
  1168. if ((err = esp_http_client_prepare(client)) != ESP_OK) {
  1169. ESP_LOGE(TAG, "Failed to initialize request data");
  1170. esp_http_client_close(client);
  1171. return err;
  1172. }
  1173. if (client->state < HTTP_STATE_CONNECTED) {
  1174. ESP_LOGD(TAG, "Begin connect to: %s://%s:%d", client->connection_info.scheme, client->connection_info.host, client->connection_info.port);
  1175. client->transport = esp_transport_list_get_transport(client->transport_list, client->connection_info.scheme);
  1176. if (client->transport == NULL) {
  1177. ESP_LOGE(TAG, "No transport found");
  1178. #ifndef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  1179. if (strcasecmp(client->connection_info.scheme, "https") == 0) {
  1180. ESP_LOGE(TAG, "Please enable HTTPS at menuconfig to allow requesting via https");
  1181. }
  1182. #endif
  1183. return ESP_ERR_HTTP_INVALID_TRANSPORT;
  1184. }
  1185. if (!client->is_async) {
  1186. if (esp_transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) {
  1187. ESP_LOGE(TAG, "Connection failed, sock < 0");
  1188. return ESP_ERR_HTTP_CONNECT;
  1189. }
  1190. } else {
  1191. int ret = esp_transport_connect_async(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms);
  1192. if (ret == ASYNC_TRANS_CONNECT_FAIL) {
  1193. ESP_LOGE(TAG, "Connection failed");
  1194. if (strcasecmp(client->connection_info.scheme, "http") == 0) {
  1195. ESP_LOGE(TAG, "Asynchronous mode doesn't work for HTTP based connection");
  1196. return ESP_ERR_INVALID_ARG;
  1197. }
  1198. return ESP_ERR_HTTP_CONNECT;
  1199. } else if (ret == ASYNC_TRANS_CONNECTING) {
  1200. ESP_LOGD(TAG, "Connection not yet established");
  1201. return ESP_ERR_HTTP_CONNECTING;
  1202. }
  1203. }
  1204. client->state = HTTP_STATE_CONNECTED;
  1205. http_dispatch_event(client, HTTP_EVENT_ON_CONNECTED, NULL, 0);
  1206. }
  1207. return ESP_OK;
  1208. }
  1209. static int http_client_prepare_first_line(esp_http_client_handle_t client, int write_len)
  1210. {
  1211. if (write_len >= 0) {
  1212. http_header_set_format(client->request->headers, "Content-Length", "%d", write_len);
  1213. } else {
  1214. esp_http_client_set_header(client, "Transfer-Encoding", "chunked");
  1215. }
  1216. const char *method = HTTP_METHOD_MAPPING[client->connection_info.method];
  1217. int first_line_len = snprintf(client->request->buffer->data,
  1218. client->buffer_size_tx, "%s %s",
  1219. method,
  1220. client->connection_info.path);
  1221. if (first_line_len >= client->buffer_size_tx) {
  1222. ESP_LOGE(TAG, "Out of buffer");
  1223. return -1;
  1224. }
  1225. if (client->connection_info.query) {
  1226. first_line_len += snprintf(client->request->buffer->data + first_line_len,
  1227. client->buffer_size_tx - first_line_len, "?%s", client->connection_info.query);
  1228. if (first_line_len >= client->buffer_size_tx) {
  1229. ESP_LOGE(TAG, "Out of buffer");
  1230. return -1;
  1231. }
  1232. }
  1233. first_line_len += snprintf(client->request->buffer->data + first_line_len,
  1234. client->buffer_size_tx - first_line_len, " %s\r\n", DEFAULT_HTTP_PROTOCOL);
  1235. if (first_line_len >= client->buffer_size_tx) {
  1236. ESP_LOGE(TAG, "Out of buffer");
  1237. return -1;
  1238. }
  1239. return first_line_len;
  1240. }
  1241. static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len)
  1242. {
  1243. int first_line_len = 0;
  1244. if (!client->first_line_prepared) {
  1245. if ((first_line_len = http_client_prepare_first_line(client, write_len)) < 0) {
  1246. return first_line_len;
  1247. }
  1248. client->first_line_prepared = true;
  1249. client->header_index = 0;
  1250. client->data_written_index = 0;
  1251. client->data_write_left = 0;
  1252. }
  1253. if (client->data_write_left > 0) {
  1254. /* sending leftover data from previous call to esp_http_client_request_send() API */
  1255. int wret = 0;
  1256. if (((wret = esp_http_client_write(client, client->request->buffer->data + client->data_written_index, client->data_write_left)) < 0)) {
  1257. ESP_LOGE(TAG, "Error write request");
  1258. return ESP_ERR_HTTP_WRITE_DATA;
  1259. }
  1260. client->data_write_left -= wret;
  1261. client->data_written_index += wret;
  1262. if (client->is_async && client->data_write_left > 0) {
  1263. return ESP_ERR_HTTP_WRITE_DATA; /* In case of EAGAIN error, we return ESP_ERR_HTTP_WRITE_DATA,
  1264. and the handling of EAGAIN should be done in the higher level APIs. */
  1265. }
  1266. }
  1267. int wlen = client->buffer_size_tx - first_line_len;
  1268. while ((client->header_index = http_header_generate_string(client->request->headers, client->header_index, client->request->buffer->data + first_line_len, &wlen))) {
  1269. if (wlen <= 0) {
  1270. break;
  1271. }
  1272. if (first_line_len) {
  1273. wlen += first_line_len;
  1274. first_line_len = 0;
  1275. }
  1276. client->request->buffer->data[wlen] = 0;
  1277. ESP_LOGD(TAG, "Write header[%d]: %s", client->header_index, client->request->buffer->data);
  1278. client->data_write_left = wlen;
  1279. client->data_written_index = 0;
  1280. while (client->data_write_left > 0) {
  1281. int wret = esp_transport_write(client->transport, client->request->buffer->data + client->data_written_index, client->data_write_left, client->timeout_ms);
  1282. if (wret <= 0) {
  1283. ESP_LOGE(TAG, "Error write request");
  1284. esp_http_client_close(client);
  1285. return ESP_ERR_HTTP_WRITE_DATA;
  1286. }
  1287. client->data_write_left -= wret;
  1288. client->data_written_index += wret;
  1289. }
  1290. wlen = client->buffer_size_tx;
  1291. }
  1292. client->data_written_index = 0;
  1293. client->data_write_left = client->post_len;
  1294. http_dispatch_event(client, HTTP_EVENT_HEADERS_SENT, NULL, 0);
  1295. client->state = HTTP_STATE_REQ_COMPLETE_HEADER;
  1296. return ESP_OK;
  1297. }
  1298. static esp_err_t esp_http_client_send_post_data(esp_http_client_handle_t client)
  1299. {
  1300. if (client->state != HTTP_STATE_REQ_COMPLETE_HEADER) {
  1301. ESP_LOGE(TAG, "Invalid state");
  1302. return ESP_ERR_INVALID_STATE;
  1303. }
  1304. if (!(client->post_data && client->post_len)) {
  1305. goto success;
  1306. }
  1307. int wret = esp_http_client_write(client, client->post_data + client->data_written_index, client->data_write_left);
  1308. if (wret < 0) {
  1309. return wret;
  1310. }
  1311. client->data_write_left -= wret;
  1312. client->data_written_index += wret;
  1313. if (client->data_write_left <= 0) {
  1314. goto success;
  1315. } else {
  1316. return ESP_ERR_HTTP_WRITE_DATA;
  1317. }
  1318. success:
  1319. client->state = HTTP_STATE_REQ_COMPLETE_DATA;
  1320. return ESP_OK;
  1321. }
  1322. esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)
  1323. {
  1324. client->post_len = write_len;
  1325. esp_err_t err;
  1326. if ((err = esp_http_client_connect(client)) != ESP_OK) {
  1327. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1328. return err;
  1329. }
  1330. if ((err = esp_http_client_request_send(client, write_len)) != ESP_OK) {
  1331. http_dispatch_event(client, HTTP_EVENT_ERROR, esp_transport_get_error_handle(client->transport), 0);
  1332. return err;
  1333. }
  1334. return ESP_OK;
  1335. }
  1336. int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, int len)
  1337. {
  1338. if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
  1339. return ESP_FAIL;
  1340. }
  1341. int wlen = 0, widx = 0;
  1342. while (len > 0) {
  1343. wlen = esp_transport_write(client->transport, buffer + widx, len, client->timeout_ms);
  1344. /* client->async_block is initialised in case of non-blocking IO, and in this case we return how
  1345. much ever data was written by the esp_transport_write() API. */
  1346. if (client->is_async || wlen <= 0) {
  1347. return wlen;
  1348. }
  1349. widx += wlen;
  1350. len -= wlen;
  1351. }
  1352. return widx;
  1353. }
  1354. esp_err_t esp_http_client_close(esp_http_client_handle_t client)
  1355. {
  1356. if (client->state >= HTTP_STATE_INIT) {
  1357. http_dispatch_event(client, HTTP_EVENT_DISCONNECTED, esp_transport_get_error_handle(client->transport), 0);
  1358. client->state = HTTP_STATE_INIT;
  1359. return esp_transport_close(client->transport);
  1360. }
  1361. return ESP_OK;
  1362. }
  1363. esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len)
  1364. {
  1365. esp_err_t err = ESP_OK;
  1366. client->post_data = (char *)data;
  1367. client->post_len = len;
  1368. ESP_LOGD(TAG, "set post file length = %d", len);
  1369. if (client->post_data) {
  1370. char *value = NULL;
  1371. if ((err = esp_http_client_get_header(client, "Content-Type", &value)) != ESP_OK) {
  1372. return err;
  1373. }
  1374. if (value == NULL) {
  1375. err = esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
  1376. }
  1377. } else {
  1378. client->post_len = 0;
  1379. err = esp_http_client_set_header(client, "Content-Type", NULL);
  1380. }
  1381. return err;
  1382. }
  1383. int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data)
  1384. {
  1385. if (client->post_data) {
  1386. *data = client->post_data;
  1387. return client->post_len;
  1388. }
  1389. return 0;
  1390. }
  1391. int esp_http_client_get_status_code(esp_http_client_handle_t client)
  1392. {
  1393. return client->response->status_code;
  1394. }
  1395. int64_t esp_http_client_get_content_length(esp_http_client_handle_t client)
  1396. {
  1397. return client->response->content_length;
  1398. }
  1399. bool esp_http_client_is_chunked_response(esp_http_client_handle_t client)
  1400. {
  1401. return client->response->is_chunked;
  1402. }
  1403. esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client)
  1404. {
  1405. if (!strcasecmp(client->connection_info.scheme, "https") ) {
  1406. return HTTP_TRANSPORT_OVER_SSL;
  1407. } else if (!strcasecmp(client->connection_info.scheme, "http")) {
  1408. return HTTP_TRANSPORT_OVER_TCP;
  1409. } else {
  1410. return HTTP_TRANSPORT_UNKNOWN;
  1411. }
  1412. }
  1413. void esp_http_client_add_auth(esp_http_client_handle_t client)
  1414. {
  1415. if (client == NULL) {
  1416. return;
  1417. }
  1418. if (client->state != HTTP_STATE_RES_ON_DATA_START) {
  1419. return;
  1420. }
  1421. if (client->redirect_counter >= client->max_authorization_retries) {
  1422. ESP_LOGE(TAG, "Error, reached max_authorization_retries count=%d", client->redirect_counter);
  1423. return;
  1424. }
  1425. char *auth_header = client->auth_header;
  1426. if (auth_header) {
  1427. http_utils_trim_whitespace(&auth_header);
  1428. ESP_LOGD(TAG, "UNAUTHORIZED: %s", auth_header);
  1429. client->redirect_counter++;
  1430. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  1431. if (http_utils_str_starts_with(auth_header, "Digest") == 0) {
  1432. ESP_LOGD(TAG, "type = Digest");
  1433. client->connection_info.auth_type = HTTP_AUTH_TYPE_DIGEST;
  1434. } else {
  1435. #endif
  1436. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
  1437. if (http_utils_str_starts_with(auth_header, "Basic") == 0) {
  1438. ESP_LOGD(TAG, "type = Basic");
  1439. client->connection_info.auth_type = HTTP_AUTH_TYPE_BASIC;
  1440. } else {
  1441. #endif
  1442. client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE;
  1443. ESP_LOGE(TAG, "This authentication method is not supported: %s", auth_header);
  1444. return;
  1445. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_BASIC_AUTH
  1446. }
  1447. #endif
  1448. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
  1449. }
  1450. #endif
  1451. _clear_auth_data(client);
  1452. client->auth_data->method = strdup(HTTP_METHOD_MAPPING[client->connection_info.method]);
  1453. client->auth_data->nc = 1;
  1454. client->auth_data->realm = http_utils_get_string_between(auth_header, "realm=\"", "\"");
  1455. client->auth_data->algorithm = http_utils_get_string_between(auth_header, "algorithm=", ",");
  1456. if (client->auth_data->algorithm == NULL) {
  1457. client->auth_data->algorithm = strdup("MD5");
  1458. }
  1459. client->auth_data->qop = http_utils_get_string_between(auth_header, "qop=\"", "\"");
  1460. client->auth_data->nonce = http_utils_get_string_between(auth_header, "nonce=\"", "\"");
  1461. client->auth_data->opaque = http_utils_get_string_between(auth_header, "opaque=\"", "\"");
  1462. client->process_again = 1;
  1463. } else {
  1464. client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE;
  1465. ESP_LOGW(TAG, "This request requires authentication, but does not provide header information for that");
  1466. }
  1467. }
  1468. int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, int len)
  1469. {
  1470. int read_len = 0;
  1471. while (read_len < len) {
  1472. int data_read = esp_http_client_read(client, buffer + read_len, len - read_len);
  1473. if (data_read <= 0) {
  1474. return read_len;
  1475. }
  1476. read_len += data_read;
  1477. }
  1478. return read_len;
  1479. }
  1480. esp_err_t esp_http_client_flush_response(esp_http_client_handle_t client, int *len)
  1481. {
  1482. if (client == NULL) {
  1483. ESP_LOGE(TAG, "client must not be NULL");
  1484. return ESP_ERR_INVALID_ARG;
  1485. }
  1486. int read_len = 0;
  1487. while (!esp_http_client_is_complete_data_received(client)) {
  1488. int data_read = esp_http_client_get_data(client);
  1489. if (data_read < 0) {
  1490. return ESP_FAIL;
  1491. }
  1492. read_len += data_read;
  1493. }
  1494. if (len) {
  1495. *len = read_len;
  1496. }
  1497. return ESP_OK;
  1498. }
  1499. esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len)
  1500. {
  1501. if (client == NULL || url == NULL) {
  1502. return ESP_ERR_INVALID_ARG;
  1503. }
  1504. if (client->connection_info.host && client->connection_info.scheme && client->connection_info.path) {
  1505. snprintf(url, len, "%s://%s%s", client->connection_info.scheme, client->connection_info.host, client->connection_info.path);
  1506. return ESP_OK;
  1507. } else {
  1508. ESP_LOGE(TAG, "Failed to get URL");
  1509. }
  1510. return ESP_FAIL;
  1511. }
  1512. esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int *len)
  1513. {
  1514. if (client == NULL || len == NULL) {
  1515. return ESP_ERR_INVALID_ARG;
  1516. }
  1517. if (esp_http_client_is_chunked_response(client)) {
  1518. *len = client->response->chunk_length;
  1519. } else {
  1520. ESP_LOGE(TAG, "Response is not chunked");
  1521. return ESP_FAIL;
  1522. }
  1523. return ESP_OK;
  1524. }