esp_http_client.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. // Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include "esp_system.h"
  15. #include "esp_log.h"
  16. #include "http_header.h"
  17. #include "esp_transport.h"
  18. #include "esp_transport_tcp.h"
  19. #include "http_utils.h"
  20. #include "http_auth.h"
  21. #include "sdkconfig.h"
  22. #include "esp_http_client.h"
  23. #include "errno.h"
  24. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  25. #include "esp_transport_ssl.h"
  26. #endif
  27. static const char *TAG = "HTTP_CLIENT";
  28. /**
  29. * HTTP Buffer
  30. */
  31. typedef struct {
  32. char *data; /*!< The HTTP data received from the server */
  33. int len; /*!< The HTTP data len received from the server */
  34. char *raw_data; /*!< The HTTP data after decoding */
  35. int raw_len; /*!< The HTTP data len after decoding */
  36. char *output_ptr; /*!< The destination address of the data to be copied to after decoding */
  37. } esp_http_buffer_t;
  38. /**
  39. * private HTTP Data structure
  40. */
  41. typedef struct {
  42. http_header_handle_t headers; /*!< http header */
  43. esp_http_buffer_t *buffer; /*!< data buffer as linked list */
  44. int status_code; /*!< status code (integer) */
  45. int content_length; /*!< data length */
  46. int data_offset; /*!< offset to http data (Skip header) */
  47. int data_process; /*!< data processed */
  48. int method; /*!< http method */
  49. bool is_chunked;
  50. } esp_http_data_t;
  51. typedef struct {
  52. char *url;
  53. char *scheme;
  54. char *host;
  55. int port;
  56. char *username;
  57. char *password;
  58. char *path;
  59. char *query;
  60. char *cert_pem;
  61. esp_http_client_method_t method;
  62. esp_http_client_auth_type_t auth_type;
  63. esp_http_client_transport_t transport_type;
  64. int max_store_header_size;
  65. } connection_info_t;
  66. typedef enum {
  67. HTTP_STATE_UNINIT = 0,
  68. HTTP_STATE_INIT,
  69. HTTP_STATE_CONNECTED,
  70. HTTP_STATE_REQ_COMPLETE_HEADER,
  71. HTTP_STATE_REQ_COMPLETE_DATA,
  72. HTTP_STATE_RES_COMPLETE_HEADER,
  73. HTTP_STATE_RES_COMPLETE_DATA,
  74. HTTP_STATE_CLOSE
  75. } esp_http_state_t;
  76. /**
  77. * HTTP client class
  78. */
  79. struct esp_http_client {
  80. int redirect_counter;
  81. int max_redirection_count;
  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;
  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. };
  111. typedef struct esp_http_client esp_http_client_t;
  112. static esp_err_t _clear_connection_info(esp_http_client_handle_t client);
  113. /**
  114. * Default settings
  115. */
  116. #define DEFAULT_HTTP_PORT (80)
  117. #define DEFAULT_HTTPS_PORT (443)
  118. #define ASYNC_TRANS_CONNECT_FAIL -1
  119. #define ASYNC_TRANS_CONNECTING 0
  120. #define ASYNC_TRANS_CONNECT_PASS 1
  121. static const char *DEFAULT_HTTP_USER_AGENT = "ESP32 HTTP Client/1.0";
  122. static const char *DEFAULT_HTTP_PROTOCOL = "HTTP/1.1";
  123. static const char *DEFAULT_HTTP_PATH = "/";
  124. static int DEFAULT_MAX_REDIRECT = 10;
  125. static int DEFAULT_TIMEOUT_MS = 5000;
  126. static const char *HTTP_METHOD_MAPPING[] = {
  127. "GET",
  128. "POST",
  129. "PUT",
  130. "PATCH",
  131. "DELETE",
  132. "HEAD",
  133. "NOTIFY",
  134. "SUBSCRIBE",
  135. "UNSUBSCRIBE",
  136. "OPTIONS"
  137. };
  138. /**
  139. * Enum for the HTTP status codes.
  140. */
  141. enum HttpStatus_Code
  142. {
  143. /* 3xx - Redirection */
  144. HttpStatus_MovedPermanently = 301,
  145. HttpStatus_Found = 302,
  146. /* 4xx - Client Error */
  147. HttpStatus_Unauthorized = 401
  148. };
  149. static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len);
  150. static esp_err_t esp_http_client_connect(esp_http_client_handle_t client);
  151. static esp_err_t esp_http_client_send_post_data(esp_http_client_handle_t client);
  152. static esp_err_t http_dispatch_event(esp_http_client_t *client, esp_http_client_event_id_t event_id, void *data, int len)
  153. {
  154. esp_http_client_event_t *event = &client->event;
  155. if (client->event_handler) {
  156. event->event_id = event_id;
  157. event->user_data = client->user_data;
  158. event->data = data;
  159. event->data_len = len;
  160. return client->event_handler(event);
  161. }
  162. return ESP_OK;
  163. }
  164. static int http_on_message_begin(http_parser *parser)
  165. {
  166. esp_http_client_t *client = parser->data;
  167. ESP_LOGD(TAG, "on_message_begin");
  168. client->response->is_chunked = false;
  169. client->is_chunk_complete = false;
  170. return 0;
  171. }
  172. static int http_on_url(http_parser *parser, const char *at, size_t length)
  173. {
  174. ESP_LOGD(TAG, "http_on_url");
  175. return 0;
  176. }
  177. static int http_on_status(http_parser *parser, const char *at, size_t length)
  178. {
  179. return 0;
  180. }
  181. static int http_on_header_field(http_parser *parser, const char *at, size_t length)
  182. {
  183. esp_http_client_t *client = parser->data;
  184. http_utils_assign_string(&client->current_header_key, at, length);
  185. return 0;
  186. }
  187. static int http_on_header_value(http_parser *parser, const char *at, size_t length)
  188. {
  189. esp_http_client_handle_t client = parser->data;
  190. if (client->current_header_key == NULL) {
  191. return 0;
  192. }
  193. if (strcasecmp(client->current_header_key, "Location") == 0) {
  194. http_utils_assign_string(&client->location, at, length);
  195. } else if (strcasecmp(client->current_header_key, "Transfer-Encoding") == 0
  196. && memcmp(at, "chunked", length) == 0) {
  197. client->response->is_chunked = true;
  198. } else if (strcasecmp(client->current_header_key, "WWW-Authenticate") == 0) {
  199. http_utils_assign_string(&client->auth_header, at, length);
  200. }
  201. http_utils_assign_string(&client->current_header_value, at, length);
  202. ESP_LOGD(TAG, "HEADER=%s:%s", client->current_header_key, client->current_header_value);
  203. client->event.header_key = client->current_header_key;
  204. client->event.header_value = client->current_header_value;
  205. http_dispatch_event(client, HTTP_EVENT_ON_HEADER, NULL, 0);
  206. free(client->current_header_key);
  207. free(client->current_header_value);
  208. client->current_header_key = NULL;
  209. client->current_header_value = NULL;
  210. return 0;
  211. }
  212. static int http_on_headers_complete(http_parser *parser)
  213. {
  214. esp_http_client_handle_t client = parser->data;
  215. client->response->status_code = parser->status_code;
  216. client->response->data_offset = parser->nread;
  217. client->response->content_length = parser->content_length;
  218. client->response->data_process = 0;
  219. ESP_LOGD(TAG, "http_on_headers_complete, status=%d, offset=%d, nread=%d", parser->status_code, client->response->data_offset, parser->nread);
  220. client->state = HTTP_STATE_RES_COMPLETE_HEADER;
  221. return 0;
  222. }
  223. static int http_on_body(http_parser *parser, const char *at, size_t length)
  224. {
  225. esp_http_client_t *client = parser->data;
  226. ESP_LOGD(TAG, "http_on_body %d", length);
  227. client->response->buffer->raw_data = (char *)at;
  228. if (client->response->buffer->output_ptr) {
  229. memcpy(client->response->buffer->output_ptr, (char *)at, length);
  230. client->response->buffer->output_ptr += length;
  231. }
  232. client->response->data_process += length;
  233. client->response->buffer->raw_len += length;
  234. http_dispatch_event(client, HTTP_EVENT_ON_DATA, (void *)at, length);
  235. return 0;
  236. }
  237. static int http_on_message_complete(http_parser *parser)
  238. {
  239. ESP_LOGD(TAG, "http_on_message_complete, parser=%x", (int)parser);
  240. esp_http_client_handle_t client = parser->data;
  241. client->is_chunk_complete = true;
  242. return 0;
  243. }
  244. static int http_on_chunk_complete(http_parser *parser)
  245. {
  246. ESP_LOGD(TAG, "http_on_chunk_complete");
  247. return 0;
  248. }
  249. esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value)
  250. {
  251. return http_header_set(client->request->headers, key, value);
  252. }
  253. esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value)
  254. {
  255. return http_header_get(client->request->headers, key, value);
  256. }
  257. esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const char *key)
  258. {
  259. return http_header_delete(client->request->headers, key);
  260. }
  261. static esp_err_t _set_config(esp_http_client_handle_t client, const esp_http_client_config_t *config)
  262. {
  263. client->connection_info.method = config->method;
  264. client->connection_info.port = config->port;
  265. client->connection_info.auth_type = config->auth_type;
  266. client->event_handler = config->event_handler;
  267. client->timeout_ms = config->timeout_ms;
  268. client->max_redirection_count = config->max_redirection_count;
  269. client->user_data = config->user_data;
  270. client->buffer_size = config->buffer_size;
  271. client->disable_auto_redirect = config->disable_auto_redirect;
  272. if (config->buffer_size == 0) {
  273. client->buffer_size = DEFAULT_HTTP_BUF_SIZE;
  274. }
  275. if (client->max_redirection_count == 0) {
  276. client->max_redirection_count = DEFAULT_MAX_REDIRECT;
  277. }
  278. if (config->path) {
  279. client->connection_info.path = strdup(config->path);
  280. } else {
  281. client->connection_info.path = strdup(DEFAULT_HTTP_PATH);
  282. }
  283. HTTP_MEM_CHECK(TAG, client->connection_info.path, {
  284. return ESP_ERR_NO_MEM;
  285. });
  286. if (config->host) {
  287. client->connection_info.host = strdup(config->host);
  288. HTTP_MEM_CHECK(TAG, client->connection_info.host, {
  289. _clear_connection_info(client);
  290. return ESP_ERR_NO_MEM;
  291. });
  292. }
  293. if (config->query) {
  294. client->connection_info.query = strdup(config->query);
  295. HTTP_MEM_CHECK(TAG, client->connection_info.query, {
  296. _clear_connection_info(client);
  297. return ESP_ERR_NO_MEM;
  298. });
  299. }
  300. if (config->username) {
  301. client->connection_info.username = strdup(config->username);
  302. HTTP_MEM_CHECK(TAG, client->connection_info.username, {
  303. _clear_connection_info(client);
  304. return ESP_ERR_NO_MEM;
  305. });
  306. }
  307. if (config->password) {
  308. client->connection_info.password = strdup(config->password);
  309. HTTP_MEM_CHECK(TAG, client->connection_info.password, {
  310. _clear_connection_info(client);
  311. return ESP_ERR_NO_MEM;
  312. });
  313. }
  314. if (config->transport_type == HTTP_TRANSPORT_OVER_SSL) {
  315. http_utils_assign_string(&client->connection_info.scheme, "https", 0);
  316. if (client->connection_info.port == 0) {
  317. client->connection_info.port = DEFAULT_HTTPS_PORT;
  318. }
  319. } else {
  320. http_utils_assign_string(&client->connection_info.scheme, "http", 0);
  321. if (client->connection_info.port == 0) {
  322. client->connection_info.port = DEFAULT_HTTP_PORT;
  323. }
  324. }
  325. if (client->timeout_ms == 0) {
  326. client->timeout_ms = DEFAULT_TIMEOUT_MS;
  327. }
  328. if (config->is_async) {
  329. client->is_async = true;
  330. }
  331. return ESP_OK;
  332. }
  333. static esp_err_t _clear_connection_info(esp_http_client_handle_t client)
  334. {
  335. free(client->connection_info.path);
  336. free(client->connection_info.host);
  337. free(client->connection_info.query);
  338. free(client->connection_info.username);
  339. if (client->connection_info.password) {
  340. memset(client->connection_info.password, 0, strlen(client->connection_info.password));
  341. free(client->connection_info.password);
  342. }
  343. free(client->connection_info.scheme);
  344. free(client->connection_info.url);
  345. memset(&client->connection_info, 0, sizeof(connection_info_t));
  346. return ESP_OK;
  347. }
  348. static esp_err_t _clear_auth_data(esp_http_client_handle_t client)
  349. {
  350. if (client->auth_data == NULL) {
  351. return ESP_FAIL;
  352. }
  353. free(client->auth_data->method);
  354. free(client->auth_data->realm);
  355. free(client->auth_data->algorithm);
  356. free(client->auth_data->qop);
  357. free(client->auth_data->nonce);
  358. free(client->auth_data->opaque);
  359. memset(client->auth_data, 0, sizeof(esp_http_auth_data_t));
  360. return ESP_OK;
  361. }
  362. static esp_err_t esp_http_client_prepare(esp_http_client_handle_t client)
  363. {
  364. client->process_again = 0;
  365. client->response->data_process = 0;
  366. client->first_line_prepared = false;
  367. http_parser_init(client->parser, HTTP_RESPONSE);
  368. if (client->connection_info.username) {
  369. char *auth_response = NULL;
  370. if (client->connection_info.auth_type == HTTP_AUTH_TYPE_BASIC) {
  371. auth_response = http_auth_basic(client->connection_info.username, client->connection_info.password);
  372. } else if (client->connection_info.auth_type == HTTP_AUTH_TYPE_DIGEST && client->auth_data) {
  373. client->auth_data->uri = client->connection_info.path;
  374. client->auth_data->cnonce = ((uint64_t)esp_random() << 32) + esp_random();
  375. auth_response = http_auth_digest(client->connection_info.username, client->connection_info.password, client->auth_data);
  376. client->auth_data->nc ++;
  377. }
  378. if (auth_response) {
  379. ESP_LOGD(TAG, "auth_response=%s", auth_response);
  380. esp_http_client_set_header(client, "Authorization", auth_response);
  381. free(auth_response);
  382. }
  383. }
  384. return ESP_OK;
  385. }
  386. esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config)
  387. {
  388. esp_http_client_handle_t client;
  389. esp_transport_handle_t tcp;
  390. bool _success;
  391. _success = (
  392. (client = calloc(1, sizeof(esp_http_client_t))) &&
  393. (client->parser = calloc(1, sizeof(struct http_parser))) &&
  394. (client->parser_settings = calloc(1, sizeof(struct http_parser_settings))) &&
  395. (client->auth_data = calloc(1, sizeof(esp_http_auth_data_t))) &&
  396. (client->request = calloc(1, sizeof(esp_http_data_t))) &&
  397. (client->request->headers = http_header_init()) &&
  398. (client->request->buffer = calloc(1, sizeof(esp_http_buffer_t))) &&
  399. (client->response = calloc(1, sizeof(esp_http_data_t))) &&
  400. (client->response->headers = http_header_init()) &&
  401. (client->response->buffer = calloc(1, sizeof(esp_http_buffer_t)))
  402. );
  403. if (!_success) {
  404. ESP_LOGE(TAG, "Error allocate memory");
  405. esp_http_client_cleanup(client);
  406. return NULL;
  407. }
  408. _success = (
  409. (client->transport_list = esp_transport_list_init()) &&
  410. (tcp = esp_transport_tcp_init()) &&
  411. (esp_transport_set_default_port(tcp, DEFAULT_HTTP_PORT) == ESP_OK) &&
  412. (esp_transport_list_add(client->transport_list, tcp, "http") == ESP_OK)
  413. );
  414. if (!_success) {
  415. ESP_LOGE(TAG, "Error initialize transport");
  416. esp_http_client_cleanup(client);
  417. return NULL;
  418. }
  419. #ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  420. esp_transport_handle_t ssl;
  421. _success = (
  422. (ssl = esp_transport_ssl_init()) &&
  423. (esp_transport_set_default_port(ssl, DEFAULT_HTTPS_PORT) == ESP_OK) &&
  424. (esp_transport_list_add(client->transport_list, ssl, "https") == ESP_OK)
  425. );
  426. if (!_success) {
  427. ESP_LOGE(TAG, "Error initialize SSL Transport");
  428. esp_http_client_cleanup(client);
  429. return NULL;
  430. }
  431. if (config->cert_pem) {
  432. esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem));
  433. }
  434. #endif
  435. if (_set_config(client, config) != ESP_OK) {
  436. ESP_LOGE(TAG, "Error set configurations");
  437. esp_http_client_cleanup(client);
  438. return NULL;
  439. }
  440. _success = (
  441. (client->request->buffer->data = malloc(client->buffer_size)) &&
  442. (client->response->buffer->data = malloc(client->buffer_size))
  443. );
  444. if (!_success) {
  445. ESP_LOGE(TAG, "Allocation failed");
  446. esp_http_client_cleanup(client);
  447. return NULL;
  448. }
  449. _success = (
  450. (esp_http_client_set_url(client, config->url) == ESP_OK) &&
  451. (esp_http_client_set_header(client, "User-Agent", DEFAULT_HTTP_USER_AGENT) == ESP_OK) &&
  452. (esp_http_client_set_header(client, "Host", client->connection_info.host) == ESP_OK)
  453. );
  454. if (!_success) {
  455. ESP_LOGE(TAG, "Error set default configurations");
  456. esp_http_client_cleanup(client);
  457. return NULL;
  458. }
  459. client->parser_settings->on_message_begin = http_on_message_begin;
  460. client->parser_settings->on_url = http_on_url;
  461. client->parser_settings->on_status = http_on_status;
  462. client->parser_settings->on_header_field = http_on_header_field;
  463. client->parser_settings->on_header_value = http_on_header_value;
  464. client->parser_settings->on_headers_complete = http_on_headers_complete;
  465. client->parser_settings->on_body = http_on_body;
  466. client->parser_settings->on_message_complete = http_on_message_complete;
  467. client->parser_settings->on_chunk_complete = http_on_chunk_complete;
  468. client->parser->data = client;
  469. client->event.client = client;
  470. client->state = HTTP_STATE_INIT;
  471. return client;
  472. }
  473. esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client)
  474. {
  475. if (client == NULL) {
  476. return ESP_FAIL;
  477. }
  478. esp_http_client_close(client);
  479. esp_transport_list_destroy(client->transport_list);
  480. http_header_destroy(client->request->headers);
  481. free(client->request->buffer->data);
  482. free(client->request->buffer);
  483. free(client->request);
  484. http_header_destroy(client->response->headers);
  485. free(client->response->buffer->data);
  486. free(client->response->buffer);
  487. free(client->response);
  488. free(client->parser);
  489. free(client->parser_settings);
  490. _clear_connection_info(client);
  491. _clear_auth_data(client);
  492. free(client->auth_data);
  493. free(client->current_header_key);
  494. free(client->location);
  495. free(client->auth_header);
  496. free(client);
  497. return ESP_OK;
  498. }
  499. static esp_err_t esp_http_check_response(esp_http_client_handle_t client)
  500. {
  501. char *auth_header = NULL;
  502. if (client->redirect_counter >= client->max_redirection_count || client->disable_auto_redirect) {
  503. ESP_LOGE(TAG, "Error, reach max_redirection_count count=%d", client->redirect_counter);
  504. return ESP_ERR_HTTP_MAX_REDIRECT;
  505. }
  506. switch (client->response->status_code) {
  507. case HttpStatus_MovedPermanently:
  508. case HttpStatus_Found:
  509. ESP_LOGI(TAG, "Redirect to %s", client->location);
  510. esp_http_client_set_url(client, client->location);
  511. client->redirect_counter ++;
  512. client->process_again = 1;
  513. break;
  514. case HttpStatus_Unauthorized:
  515. auth_header = client->auth_header;
  516. if (auth_header) {
  517. http_utils_trim_whitespace(&auth_header);
  518. ESP_LOGD(TAG, "UNAUTHORIZED: %s", auth_header);
  519. client->redirect_counter ++;
  520. if (http_utils_str_starts_with(auth_header, "Digest") == 0) {
  521. ESP_LOGD(TAG, "type = Digest");
  522. client->connection_info.auth_type = HTTP_AUTH_TYPE_DIGEST;
  523. } else if (http_utils_str_starts_with(auth_header, "Basic") == 0) {
  524. ESP_LOGD(TAG, "type = Basic");
  525. client->connection_info.auth_type = HTTP_AUTH_TYPE_BASIC;
  526. } else {
  527. client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE;
  528. ESP_LOGE(TAG, "This authentication method is not supported: %s", auth_header);
  529. break;
  530. }
  531. _clear_auth_data(client);
  532. client->auth_data->method = strdup(HTTP_METHOD_MAPPING[client->connection_info.method]);
  533. client->auth_data->nc = 1;
  534. client->auth_data->realm = http_utils_get_string_between(auth_header, "realm=\"", "\"");
  535. client->auth_data->algorithm = http_utils_get_string_between(auth_header, "algorithm=", ",");
  536. client->auth_data->qop = http_utils_get_string_between(auth_header, "qop=\"", "\"");
  537. client->auth_data->nonce = http_utils_get_string_between(auth_header, "nonce=\"", "\"");
  538. client->auth_data->opaque = http_utils_get_string_between(auth_header, "opaque=\"", "\"");
  539. client->process_again = 1;
  540. } else {
  541. client->connection_info.auth_type = HTTP_AUTH_TYPE_NONE;
  542. ESP_LOGW(TAG, "This request requires authentication, but does not provide header information for that");
  543. }
  544. }
  545. return ESP_OK;
  546. }
  547. esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url)
  548. {
  549. char *old_host = NULL;
  550. struct http_parser_url purl;
  551. int old_port;
  552. if (client == NULL || url == NULL) {
  553. ESP_LOGE(TAG, "client or url must not NULL");
  554. return ESP_ERR_INVALID_ARG;
  555. }
  556. http_parser_url_init(&purl);
  557. int parser_status = http_parser_parse_url(url, strlen(url), 0, &purl);
  558. if (parser_status != 0) {
  559. ESP_LOGE(TAG, "Error parse url %s", url);
  560. return ESP_ERR_INVALID_ARG;
  561. }
  562. old_host = client->connection_info.host;
  563. old_port = client->connection_info.port;
  564. if (purl.field_data[UF_HOST].len) {
  565. http_utils_assign_string(&client->connection_info.host, url + purl.field_data[UF_HOST].off, purl.field_data[UF_HOST].len);
  566. HTTP_MEM_CHECK(TAG, client->connection_info.host, return ESP_ERR_NO_MEM);
  567. }
  568. // Close the connection if host was changed
  569. if (old_host && client->connection_info.host
  570. && strcasecmp(old_host, (const void *)client->connection_info.host) != 0) {
  571. ESP_LOGD(TAG, "New host assign = %s", client->connection_info.host);
  572. if (esp_http_client_set_header(client, "Host", client->connection_info.host) != ESP_OK) {
  573. return ESP_ERR_NO_MEM;
  574. }
  575. esp_http_client_close(client);
  576. }
  577. if (purl.field_data[UF_SCHEMA].len) {
  578. http_utils_assign_string(&client->connection_info.scheme, url + purl.field_data[UF_SCHEMA].off, purl.field_data[UF_SCHEMA].len);
  579. HTTP_MEM_CHECK(TAG, client->connection_info.scheme, return ESP_ERR_NO_MEM);
  580. if (strcasecmp(client->connection_info.scheme, "http") == 0) {
  581. client->connection_info.port = DEFAULT_HTTP_PORT;
  582. } else if (strcasecmp(client->connection_info.scheme, "https") == 0) {
  583. client->connection_info.port = DEFAULT_HTTPS_PORT;
  584. }
  585. }
  586. if (purl.field_data[UF_PORT].len) {
  587. client->connection_info.port = strtol((const char*)(url + purl.field_data[UF_PORT].off), NULL, 10);
  588. }
  589. if (old_port != client->connection_info.port) {
  590. esp_http_client_close(client);
  591. }
  592. if (purl.field_data[UF_USERINFO].len) {
  593. char *user_info = NULL;
  594. http_utils_assign_string(&user_info, url + purl.field_data[UF_USERINFO].off, purl.field_data[UF_USERINFO].len);
  595. if (user_info) {
  596. char *username = user_info;
  597. char *password = strchr(user_info, ':');
  598. if (password) {
  599. *password = 0;
  600. password ++;
  601. http_utils_assign_string(&client->connection_info.password, password, 0);
  602. HTTP_MEM_CHECK(TAG, client->connection_info.password, return ESP_ERR_NO_MEM);
  603. }
  604. http_utils_assign_string(&client->connection_info.username, username, 0);
  605. HTTP_MEM_CHECK(TAG, client->connection_info.username, return ESP_ERR_NO_MEM);
  606. free(user_info);
  607. } else {
  608. return ESP_ERR_NO_MEM;
  609. }
  610. } else {
  611. free(client->connection_info.username);
  612. free(client->connection_info.password);
  613. client->connection_info.username = NULL;
  614. client->connection_info.password = NULL;
  615. }
  616. //Reset path and query if there are no information
  617. if (purl.field_data[UF_PATH].len) {
  618. http_utils_assign_string(&client->connection_info.path, url + purl.field_data[UF_PATH].off, purl.field_data[UF_PATH].len);
  619. } else {
  620. http_utils_assign_string(&client->connection_info.path, "/", 0);
  621. }
  622. HTTP_MEM_CHECK(TAG, client->connection_info.path, return ESP_ERR_NO_MEM);
  623. if (purl.field_data[UF_QUERY].len) {
  624. http_utils_assign_string(&client->connection_info.query, url + purl.field_data[UF_QUERY].off, purl.field_data[UF_QUERY].len);
  625. HTTP_MEM_CHECK(TAG, client->connection_info.query, return ESP_ERR_NO_MEM);
  626. } else if (client->connection_info.query) {
  627. free(client->connection_info.query);
  628. client->connection_info.query = NULL;
  629. }
  630. return ESP_OK;
  631. }
  632. esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method)
  633. {
  634. client->connection_info.method = method;
  635. return ESP_OK;
  636. }
  637. static int esp_http_client_get_data(esp_http_client_handle_t client)
  638. {
  639. if (client->state < HTTP_STATE_RES_COMPLETE_HEADER) {
  640. return ESP_FAIL;
  641. }
  642. if (client->connection_info.method == HTTP_METHOD_HEAD) {
  643. return 0;
  644. }
  645. esp_http_buffer_t *res_buffer = client->response->buffer;
  646. ESP_LOGD(TAG, "data_process=%d, content_length=%d", client->response->data_process, client->response->content_length);
  647. int rlen = esp_transport_read(client->transport, res_buffer->data, client->buffer_size, client->timeout_ms);
  648. if (rlen >= 0) {
  649. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen);
  650. }
  651. return rlen;
  652. }
  653. int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len)
  654. {
  655. esp_http_buffer_t *res_buffer = client->response->buffer;
  656. int rlen = ESP_FAIL, ridx = 0;
  657. if (res_buffer->raw_len) {
  658. int remain_len = client->response->buffer->raw_len;
  659. if (remain_len > len) {
  660. remain_len = len;
  661. }
  662. memcpy(buffer, res_buffer->raw_data, remain_len);
  663. res_buffer->raw_len -= remain_len;
  664. res_buffer->raw_data += remain_len;
  665. ridx = remain_len;
  666. }
  667. int need_read = len - ridx;
  668. bool is_data_remain = true;
  669. while (need_read > 0 && is_data_remain) {
  670. if (client->response->is_chunked) {
  671. is_data_remain = !client->is_chunk_complete;
  672. } else {
  673. is_data_remain = client->response->data_process < client->response->content_length;
  674. }
  675. ESP_LOGD(TAG, "is_data_remain=%d, is_chunked=%d", is_data_remain, client->response->is_chunked);
  676. if (!is_data_remain) {
  677. break;
  678. }
  679. int byte_to_read = need_read;
  680. if (byte_to_read > client->buffer_size) {
  681. byte_to_read = client->buffer_size;
  682. }
  683. rlen = esp_transport_read(client->transport, res_buffer->data, byte_to_read, client->timeout_ms);
  684. ESP_LOGD(TAG, "need_read=%d, byte_to_read=%d, rlen=%d, ridx=%d", need_read, byte_to_read, rlen, ridx);
  685. if (rlen <= 0) {
  686. return ridx;
  687. }
  688. res_buffer->output_ptr = buffer + ridx;
  689. http_parser_execute(client->parser, client->parser_settings, res_buffer->data, rlen);
  690. ridx += res_buffer->raw_len;
  691. need_read -= res_buffer->raw_len;
  692. res_buffer->raw_len = 0; //clear
  693. res_buffer->output_ptr = NULL;
  694. }
  695. return ridx;
  696. }
  697. esp_err_t esp_http_client_perform(esp_http_client_handle_t client)
  698. {
  699. esp_err_t err;
  700. do {
  701. if (client->process_again) {
  702. esp_http_client_prepare(client);
  703. }
  704. switch (client->state) {
  705. /* In case of blocking esp_http_client_perform(), the following states will fall through one after the after;
  706. in case of non-blocking esp_http_client_perform(), if there is an error condition, like EINPROGRESS or EAGAIN,
  707. then the esp_http_client_perform() API will return ESP_ERR_HTTP_EAGAIN error. The user may call
  708. esp_http_client_perform API again, and for this reason, we maintain the states */
  709. case HTTP_STATE_INIT:
  710. if ((err = esp_http_client_connect(client)) != ESP_OK) {
  711. if (client->is_async && err == ESP_ERR_HTTP_CONNECTING) {
  712. return ESP_ERR_HTTP_EAGAIN;
  713. }
  714. return err;
  715. }
  716. /* falls through */
  717. case HTTP_STATE_CONNECTED:
  718. if ((err = esp_http_client_request_send(client, client->post_len)) != ESP_OK) {
  719. if (client->is_async && errno == EAGAIN) {
  720. return ESP_ERR_HTTP_EAGAIN;
  721. }
  722. return err;
  723. }
  724. /* falls through */
  725. case HTTP_STATE_REQ_COMPLETE_HEADER:
  726. if ((err = esp_http_client_send_post_data(client)) != ESP_OK) {
  727. if (client->is_async && errno == EAGAIN) {
  728. return ESP_ERR_HTTP_EAGAIN;
  729. }
  730. return err;
  731. }
  732. /* falls through */
  733. case HTTP_STATE_REQ_COMPLETE_DATA:
  734. if (esp_http_client_fetch_headers(client) < 0) {
  735. if (client->is_async && errno == EAGAIN) {
  736. return ESP_ERR_HTTP_EAGAIN;
  737. }
  738. return ESP_ERR_HTTP_FETCH_HEADER;
  739. }
  740. /* falls through */
  741. case HTTP_STATE_RES_COMPLETE_HEADER:
  742. if ((err = esp_http_check_response(client)) != ESP_OK) {
  743. ESP_LOGE(TAG, "Error response");
  744. return err;
  745. }
  746. while (client->response->is_chunked && !client->is_chunk_complete) {
  747. if (esp_http_client_get_data(client) <= 0) {
  748. if (client->is_async && errno == EAGAIN) {
  749. return ESP_ERR_HTTP_EAGAIN;
  750. }
  751. ESP_LOGD(TAG, "Read finish or server requests close");
  752. break;
  753. }
  754. }
  755. while (client->response->data_process < client->response->content_length) {
  756. if (esp_http_client_get_data(client) <= 0) {
  757. if (client->is_async && errno == EAGAIN) {
  758. return ESP_ERR_HTTP_EAGAIN;
  759. }
  760. ESP_LOGD(TAG, "Read finish or server requests close");
  761. break;
  762. }
  763. }
  764. http_dispatch_event(client, HTTP_EVENT_ON_FINISH, NULL, 0);
  765. if (!http_should_keep_alive(client->parser)) {
  766. ESP_LOGD(TAG, "Close connection");
  767. esp_http_client_close(client);
  768. } else {
  769. if (client->state > HTTP_STATE_CONNECTED) {
  770. client->state = HTTP_STATE_CONNECTED;
  771. client->first_line_prepared = false;
  772. }
  773. }
  774. break;
  775. default:
  776. break;
  777. }
  778. } while (client->process_again);
  779. return ESP_OK;
  780. }
  781. int esp_http_client_fetch_headers(esp_http_client_handle_t client)
  782. {
  783. if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
  784. return ESP_FAIL;
  785. }
  786. client->state = HTTP_STATE_REQ_COMPLETE_DATA;
  787. esp_http_buffer_t *buffer = client->response->buffer;
  788. client->response->status_code = -1;
  789. while (client->state < HTTP_STATE_RES_COMPLETE_HEADER) {
  790. buffer->len = esp_transport_read(client->transport, buffer->data, client->buffer_size, client->timeout_ms);
  791. if (buffer->len <= 0) {
  792. return ESP_FAIL;
  793. }
  794. http_parser_execute(client->parser, client->parser_settings, buffer->data, buffer->len);
  795. }
  796. ESP_LOGD(TAG, "content_length = %d", client->response->content_length);
  797. if (client->response->content_length <= 0) {
  798. client->response->is_chunked = true;
  799. return 0;
  800. }
  801. return client->response->content_length;
  802. }
  803. static esp_err_t esp_http_client_connect(esp_http_client_handle_t client)
  804. {
  805. esp_err_t err;
  806. if (client->state == HTTP_STATE_UNINIT) {
  807. ESP_LOGE(TAG, "Client has not been initialized");
  808. return ESP_ERR_INVALID_STATE;
  809. }
  810. if ((err = esp_http_client_prepare(client)) != ESP_OK) {
  811. ESP_LOGE(TAG, "Failed to initialize request data");
  812. esp_http_client_close(client);
  813. return err;
  814. }
  815. if (client->state < HTTP_STATE_CONNECTED) {
  816. ESP_LOGD(TAG, "Begin connect to: %s://%s:%d", client->connection_info.scheme, client->connection_info.host, client->connection_info.port);
  817. client->transport = esp_transport_list_get_transport(client->transport_list, client->connection_info.scheme);
  818. if (client->transport == NULL) {
  819. ESP_LOGE(TAG, "No transport found");
  820. #ifndef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
  821. if (strcasecmp(client->connection_info.scheme, "https") == 0) {
  822. ESP_LOGE(TAG, "Please enable HTTPS at menuconfig to allow requesting via https");
  823. }
  824. #endif
  825. return ESP_ERR_HTTP_INVALID_TRANSPORT;
  826. }
  827. if (!client->is_async) {
  828. if (esp_transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) {
  829. ESP_LOGE(TAG, "Connection failed, sock < 0");
  830. return ESP_ERR_HTTP_CONNECT;
  831. }
  832. } else {
  833. int ret = esp_transport_connect_async(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms);
  834. if (ret == ASYNC_TRANS_CONNECT_FAIL) {
  835. ESP_LOGE(TAG, "Connection failed");
  836. if (strcasecmp(client->connection_info.scheme, "http") == 0) {
  837. ESP_LOGE(TAG, "Asynchronous mode doesn't work for HTTP based connection");
  838. return ESP_ERR_INVALID_ARG;
  839. }
  840. return ESP_ERR_HTTP_CONNECT;
  841. } else if (ret == ASYNC_TRANS_CONNECTING) {
  842. ESP_LOGD(TAG, "Connection not yet established");
  843. return ESP_ERR_HTTP_CONNECTING;
  844. }
  845. }
  846. client->state = HTTP_STATE_CONNECTED;
  847. http_dispatch_event(client, HTTP_EVENT_ON_CONNECTED, NULL, 0);
  848. }
  849. return ESP_OK;
  850. }
  851. static int http_client_prepare_first_line(esp_http_client_handle_t client, int write_len)
  852. {
  853. if (write_len >= 0) {
  854. http_header_set_format(client->request->headers, "Content-Length", "%d", write_len);
  855. } else {
  856. esp_http_client_set_header(client, "Transfer-Encoding", "chunked");
  857. esp_http_client_set_method(client, HTTP_METHOD_POST);
  858. }
  859. const char *method = HTTP_METHOD_MAPPING[client->connection_info.method];
  860. int first_line_len = snprintf(client->request->buffer->data,
  861. client->buffer_size, "%s %s",
  862. method,
  863. client->connection_info.path);
  864. if (first_line_len >= client->buffer_size) {
  865. ESP_LOGE(TAG, "Out of buffer");
  866. return -1;
  867. }
  868. if (client->connection_info.query) {
  869. first_line_len += snprintf(client->request->buffer->data + first_line_len,
  870. client->buffer_size - first_line_len, "?%s", client->connection_info.query);
  871. if (first_line_len >= client->buffer_size) {
  872. ESP_LOGE(TAG, "Out of buffer");
  873. return -1;
  874. }
  875. }
  876. first_line_len += snprintf(client->request->buffer->data + first_line_len,
  877. client->buffer_size - first_line_len, " %s\r\n", DEFAULT_HTTP_PROTOCOL);
  878. if (first_line_len >= client->buffer_size) {
  879. ESP_LOGE(TAG, "Out of buffer");
  880. return -1;
  881. }
  882. return first_line_len;
  883. }
  884. static esp_err_t esp_http_client_request_send(esp_http_client_handle_t client, int write_len)
  885. {
  886. int first_line_len = 0;
  887. if (!client->first_line_prepared) {
  888. if ((first_line_len = http_client_prepare_first_line(client, write_len)) < 0) {
  889. return first_line_len;
  890. }
  891. client->first_line_prepared = true;
  892. client->header_index = 0;
  893. client->data_written_index = 0;
  894. client->data_write_left = 0;
  895. }
  896. if (client->data_write_left > 0) {
  897. /* sending leftover data from previous call to esp_http_client_request_send() API */
  898. int wret = 0;
  899. if (((wret = esp_http_client_write(client, client->request->buffer->data + client->data_written_index, client->data_write_left)) < 0)) {
  900. ESP_LOGE(TAG, "Error write request");
  901. return ESP_ERR_HTTP_WRITE_DATA;
  902. }
  903. client->data_write_left -= wret;
  904. client->data_written_index += wret;
  905. if (client->is_async && client->data_write_left > 0) {
  906. return ESP_ERR_HTTP_WRITE_DATA; /* In case of EAGAIN error, we return ESP_ERR_HTTP_WRITE_DATA,
  907. and the handling of EAGAIN should be done in the higher level APIs. */
  908. }
  909. }
  910. int wlen = client->buffer_size - first_line_len;
  911. while ((client->header_index = http_header_generate_string(client->request->headers, client->header_index, client->request->buffer->data + first_line_len, &wlen))) {
  912. if (wlen <= 0) {
  913. break;
  914. }
  915. if (first_line_len) {
  916. wlen += first_line_len;
  917. first_line_len = 0;
  918. }
  919. client->request->buffer->data[wlen] = 0;
  920. ESP_LOGD(TAG, "Write header[%d]: %s", client->header_index, client->request->buffer->data);
  921. client->data_write_left = wlen;
  922. client->data_written_index = 0;
  923. while (client->data_write_left > 0) {
  924. int wret = esp_transport_write(client->transport, client->request->buffer->data + client->data_written_index, client->data_write_left, client->timeout_ms);
  925. if (wret <= 0) {
  926. ESP_LOGE(TAG, "Error write request");
  927. esp_http_client_close(client);
  928. return ESP_ERR_HTTP_WRITE_DATA;
  929. }
  930. client->data_write_left -= wret;
  931. client->data_written_index += wret;
  932. }
  933. wlen = client->buffer_size;
  934. }
  935. client->data_written_index = 0;
  936. client->data_write_left = client->post_len;
  937. client->state = HTTP_STATE_REQ_COMPLETE_HEADER;
  938. return ESP_OK;
  939. }
  940. static esp_err_t esp_http_client_send_post_data(esp_http_client_handle_t client)
  941. {
  942. if (client->state != HTTP_STATE_REQ_COMPLETE_HEADER) {
  943. ESP_LOGE(TAG, "Invalid state");
  944. return ESP_ERR_INVALID_STATE;
  945. }
  946. if (!(client->post_data && client->post_len)) {
  947. goto success;
  948. }
  949. int wret = esp_http_client_write(client, client->post_data + client->data_written_index, client->data_write_left);
  950. if (wret < 0) {
  951. return wret;
  952. }
  953. client->data_write_left -= wret;
  954. client->data_written_index += wret;
  955. if (client->data_write_left <= 0) {
  956. goto success;
  957. } else {
  958. return ESP_ERR_HTTP_WRITE_DATA;
  959. }
  960. success:
  961. client->state = HTTP_STATE_REQ_COMPLETE_DATA;
  962. return ESP_OK;
  963. }
  964. esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len)
  965. {
  966. esp_err_t err;
  967. if ((err = esp_http_client_connect(client)) != ESP_OK) {
  968. return err;
  969. }
  970. if ((err = esp_http_client_request_send(client, write_len)) != ESP_OK) {
  971. return err;
  972. }
  973. return ESP_OK;
  974. }
  975. int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, int len)
  976. {
  977. if (client->state < HTTP_STATE_REQ_COMPLETE_HEADER) {
  978. return ESP_FAIL;
  979. }
  980. int wlen = 0, widx = 0;
  981. while (len > 0) {
  982. wlen = esp_transport_write(client->transport, buffer + widx, len, client->timeout_ms);
  983. /* client->async_block is initialised in case of non-blocking IO, and in this case we return how
  984. much ever data was written by the esp_transport_write() API. */
  985. if (client->is_async || wlen <= 0) {
  986. return wlen;
  987. }
  988. widx += wlen;
  989. len -= wlen;
  990. }
  991. return widx;
  992. }
  993. esp_err_t esp_http_client_close(esp_http_client_handle_t client)
  994. {
  995. if (client->state >= HTTP_STATE_INIT) {
  996. http_dispatch_event(client, HTTP_EVENT_DISCONNECTED, NULL, 0);
  997. client->state = HTTP_STATE_INIT;
  998. return esp_transport_close(client->transport);
  999. }
  1000. return ESP_OK;
  1001. }
  1002. esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len)
  1003. {
  1004. esp_err_t err = ESP_OK;
  1005. client->post_data = (char *)data;
  1006. client->post_len = len;
  1007. ESP_LOGD(TAG, "set post file length = %d", len);
  1008. if (client->post_data) {
  1009. char *value = NULL;
  1010. if ((err = esp_http_client_get_header(client, "Content-Type", &value)) != ESP_OK) {
  1011. return err;
  1012. }
  1013. if (value == NULL) {
  1014. err = esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
  1015. }
  1016. } else {
  1017. client->post_len = 0;
  1018. err = esp_http_client_set_header(client, "Content-Type", NULL);
  1019. }
  1020. return err;
  1021. }
  1022. int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data)
  1023. {
  1024. if (client->post_data) {
  1025. *data = client->post_data;
  1026. return client->post_len;
  1027. }
  1028. return 0;
  1029. }
  1030. int esp_http_client_get_status_code(esp_http_client_handle_t client)
  1031. {
  1032. return client->response->status_code;
  1033. }
  1034. int esp_http_client_get_content_length(esp_http_client_handle_t client)
  1035. {
  1036. return client->response->content_length;
  1037. }
  1038. bool esp_http_client_is_chunked_response(esp_http_client_handle_t client)
  1039. {
  1040. return client->response->is_chunked;
  1041. }
  1042. esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client)
  1043. {
  1044. if (!strcasecmp(client->connection_info.scheme, "https") ) {
  1045. return HTTP_TRANSPORT_OVER_SSL;
  1046. } else if (!strcasecmp(client->connection_info.scheme, "http")) {
  1047. return HTTP_TRANSPORT_OVER_TCP;
  1048. } else {
  1049. return HTTP_TRANSPORT_UNKNOWN;
  1050. }
  1051. }