From 3867a38ff92a15faa0096698119ef592df733153 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 18 Aug 2024 16:56:05 +0200 Subject: [PATCH] Perform HTTP request on backend to retrieve secure endpoint location --- esp32_device/.vscode/settings.json | 4 +- esp32_device/main/CMakeLists.txt | 2 +- esp32_device/main/http_client.c | 154 +++++++++++++++++++++++++++++ esp32_device/main/http_client.h | 29 ++++++ esp32_device/main/unsecure_api.c | 17 +++- 5 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 esp32_device/main/http_client.c create mode 100644 esp32_device/main/http_client.h diff --git a/esp32_device/.vscode/settings.json b/esp32_device/.vscode/settings.json index de080cf..27643ed 100644 --- a/esp32_device/.vscode/settings.json +++ b/esp32_device/.vscode/settings.json @@ -30,6 +30,8 @@ "task.h": "c", "freertos.h": "c", "unsecure_api.h": "c", - "esp_log.h": "c" + "esp_log.h": "c", + "http_client.h": "c", + "string.h": "c" } } diff --git a/esp32_device/main/CMakeLists.txt b/esp32_device/main/CMakeLists.txt index c2eb4f6..5135053 100755 --- a/esp32_device/main/CMakeLists.txt +++ b/esp32_device/main/CMakeLists.txt @@ -1,3 +1,3 @@ -idf_component_register(SRCS "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" +idf_component_register(SRCS "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" "dev_name.c" INCLUDE_DIRS ".") diff --git a/esp32_device/main/http_client.c b/esp32_device/main/http_client.c new file mode 100644 index 0000000..e55b18c --- /dev/null +++ b/esp32_device/main/http_client.c @@ -0,0 +1,154 @@ +#include "http_client.h" +#include "esp_http_client.h" +#include "esp_tls.h" +#include "esp_log.h" + +#include + +#include + +#define MAX_HTTP_RECV_BUFFER 512 +#define MAX_HTTP_OUTPUT_BUFFER 2048 +static const char *TAG = "http_client"; + +esp_err_t _http_event_handler(esp_http_client_event_t *evt) +{ + static char *output_buffer; // Buffer to store response of http request from event handler + static int output_len; // Stores number of bytes read + switch (evt->event_id) + { + case HTTP_EVENT_ERROR: + ESP_LOGD(TAG, "HTTP_EVENT_ERROR"); + break; + case HTTP_EVENT_ON_CONNECTED: + ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED"); + break; + case HTTP_EVENT_HEADER_SENT: + ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT"); + break; + case HTTP_EVENT_ON_HEADER: + ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value); + break; + case HTTP_EVENT_ON_DATA: + ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); + // Clean the buffer in case of a new request + if (output_len == 0 && evt->user_data) + { + // we are just starting to copy the output data into the use + memset(evt->user_data, 0, MAX_HTTP_OUTPUT_BUFFER); + } + /* + * Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data. + * However, event handler can also be used in case chunked encoding is used. + */ + if (!esp_http_client_is_chunked_response(evt->client)) + { + // If user_data buffer is configured, copy the response into the buffer + int copy_len = 0; + if (evt->user_data) + { + // The last byte in evt->user_data is kept for the NULL character in case of out-of-bound access. + copy_len = MIN(evt->data_len, (MAX_HTTP_OUTPUT_BUFFER - output_len)); + if (copy_len) + { + memcpy(evt->user_data + output_len, evt->data, copy_len); + } + } + else + { + int content_len = esp_http_client_get_content_length(evt->client); + if (output_buffer == NULL) + { + // We initialize output_buffer with 0 because it is used by strlen() and similar functions therefore should be null terminated. + output_buffer = (char *)calloc(content_len + 1, sizeof(char)); + output_len = 0; + if (output_buffer == NULL) + { + ESP_LOGE(TAG, "Failed to allocate memory for output buffer"); + return ESP_FAIL; + } + } + copy_len = MIN(evt->data_len, (content_len - output_len)); + if (copy_len) + { + memcpy(output_buffer + output_len, evt->data, copy_len); + } + } + output_len += copy_len; + } + + break; + case HTTP_EVENT_ON_FINISH: + ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH"); + if (output_buffer != NULL) + { + // Print buffer + ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len); + + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); + int mbedtls_err = 0; + esp_err_t err = esp_tls_get_and_clear_last_error((esp_tls_error_handle_t)evt->data, &mbedtls_err, NULL); + if (err != 0) + { + ESP_LOGI(TAG, "Last esp error code: 0x%x", err); + ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); + } + if (output_buffer != NULL) + { + free(output_buffer); + output_buffer = NULL; + } + output_len = 0; + break; + case HTTP_EVENT_REDIRECT: + ESP_LOGD(TAG, "HTTP_EVENT_REDIRECT"); + esp_http_client_set_header(evt->client, "From", "user@example.com"); + esp_http_client_set_header(evt->client, "Accept", "text/html"); + esp_http_client_set_redirection(evt->client); + break; + } + return ESP_OK; +} + +char *http_client_exec(const http_request_opts *opts) +{ + char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER + 1] = {0}; + + ESP_LOGI(TAG, "Perform HTTP request on %s", opts->url); + + esp_http_client_config_t config = { + .event_handler = _http_event_handler, + .user_data = local_response_buffer, + .url = opts->url, + .disable_auto_redirect = true, + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + esp_err_t err = esp_http_client_perform(client); + + const int status = esp_http_client_get_status_code(client); + const int64_t resp_len = esp_http_client_get_content_length(client); + + esp_http_client_cleanup(client); + + if (err != ESP_OK) + { + ESP_LOGE(TAG, "HTTP request failed with code %xd!", err); + return NULL; + } + + if (status < 200 || status > 299) + { + ESP_LOGE(TAG, "HTTP request failed with status %d!", status); + return NULL; + } + + local_response_buffer[resp_len] = 0; + + return strdup(local_response_buffer); +} \ No newline at end of file diff --git a/esp32_device/main/http_client.h b/esp32_device/main/http_client.h new file mode 100644 index 0000000..e70dc33 --- /dev/null +++ b/esp32_device/main/http_client.h @@ -0,0 +1,29 @@ +/** + * HTTP client + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + typedef struct + { + char *url; + } http_request_opts; + + /** + * Perform an HTTP request. + * + * Returns NULL in case of failure or the response else. The memory + * must be released by the caller. + */ + char *http_client_exec(const http_request_opts *opts); + +#ifdef __cplusplus +} +#endif diff --git a/esp32_device/main/unsecure_api.c b/esp32_device/main/unsecure_api.c index 65123d6..4a07215 100644 --- a/esp32_device/main/unsecure_api.c +++ b/esp32_device/main/unsecure_api.c @@ -1,11 +1,22 @@ #include "unsecure_api.h" #include "constants.h" -#include "esp_http_client.h" +#include "http_client.h" +#include "esp_log.h" -#include +static const char *TAG = "unsecure_api"; char *unsecure_api_get_secure_origin() { const char *url = BACKEND_UNSECURE_URL "/secure_origin"; - return strdup(url); + + http_request_opts opts = {.url = url}; + char *res = http_client_exec(&opts); + + if (!res) + { + ESP_LOGE(TAG, "Failed to query api secure origin!"); + return NULL; + } + + return res; } \ No newline at end of file