esp_http_client.c 62 KB

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