From 6ef558320a20de12f124573aeec0a06c2ac6da04 Mon Sep 17 00:00:00 2001 From: Tuan PM Date: Mon, 23 Jul 2018 08:59:49 +0700 Subject: [PATCH] esp_http_client: Resolve some bugs from the github community - Closes https://github.com/espressif/esp-idf/issues/2135 - Closes https://github.com/espressif/esp-idf/issues/2208 - Closes https://github.com/espressif/esp-idf/issues/2213 --- components/esp_http_client/esp_http_client.c | 8 ++++++++ components/esp_http_client/lib/http_auth.c | 14 +++++++------- .../esp_http_client/main/esp_http_client_example.c | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/components/esp_http_client/esp_http_client.c b/components/esp_http_client/esp_http_client.c index 9597021cb..f504ba1d2 100644 --- a/components/esp_http_client/esp_http_client.c +++ b/components/esp_http_client/esp_http_client.c @@ -759,6 +759,9 @@ int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len) esp_err_t esp_http_client_perform(esp_http_client_handle_t client) { esp_err_t err; + if (client == NULL) { + return ESP_ERR_INVALID_ARG; + } do { if ((err = esp_http_client_open(client, client->post_len)) != ESP_OK) { return err; @@ -849,6 +852,11 @@ esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len) client->transport = transport_list_get_transport(client->transport_list, client->connection_info.scheme); if (client->transport == NULL) { ESP_LOGE(TAG, "No transport found"); +#ifndef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS + if (strcasecmp(client->connection_info.scheme, "https") == 0) { + ESP_LOGE(TAG, "Please enable HTTPS at menuconfig to allow requesting via https"); + } +#endif return ESP_ERR_HTTP_INVALID_TRANSPORT; } if (transport_connect(client->transport, client->connection_info.host, client->connection_info.port, client->timeout_ms) < 0) { diff --git a/components/esp_http_client/lib/http_auth.c b/components/esp_http_client/lib/http_auth.c index cd1d4ef72..c406937c9 100644 --- a/components/esp_http_client/lib/http_auth.c +++ b/components/esp_http_client/lib/http_auth.c @@ -136,15 +136,15 @@ char *http_auth_basic(const char *username, const char *password) { int out; char *user_info = NULL; - char *digest = calloc(1, MD5_MAX_LEN + 7); - HTTP_MEM_CHECK(TAG, digest, goto _basic_exit); + char *digest = NULL; + size_t n = 0; asprintf(&user_info, "%s:%s", username, password); - HTTP_MEM_CHECK(TAG, user_info, goto _basic_exit); - if (user_info == NULL) { - goto _basic_exit; - } + HTTP_MEM_CHECK(TAG, user_info, return NULL); + mbedtls_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info)); + digest = calloc(1, 6 + n + 1); + HTTP_MEM_CHECK(TAG, digest, goto _basic_exit); strcpy(digest, "Basic "); - mbedtls_base64_encode((unsigned char *)digest + 6, MD5_MAX_LEN, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info)); + mbedtls_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info)); _basic_exit: free(user_info); return digest; diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index 66abb86d8..4a489080b 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -296,7 +296,7 @@ static void http_download_chunk() static void http_perform_as_stream_reader() { - char *buffer = malloc(MAX_HTTP_RECV_BUFFER); + char *buffer = malloc(MAX_HTTP_RECV_BUFFER + 1); if (buffer == NULL) { ESP_LOGE(TAG, "Cannot malloc http receive buffer"); return;