Consider the next simple program that just connects to a wifi network and open an HTTPS connection for testing purposes:
Code: Select all
/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"
#include <esp_log.h>
#include "esp_http_client.h"
#include <esp_wifi_types.h>
#include <esp_wifi.h>
#include "esp_crt_bundle.h"
#include "nvs.h"
#include "nvs_flash.h"
static const char *FIRMWARE_REMOTE_UPGRADE_URL = "https://www.lavnetremote.com/rest/utils/download-firmware?fileToDownload=MY-FIRMWARE.bin&username=MY-USERNAME&password=MY-PASSWORD";
static const int FIRMWARE_REMOTE_UPGRADE_TIMEOUT = 5000; // 5 seconds
static const char *TAG = "test-http";
static void ip_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
}
}
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_id == WIFI_EVENT_STA_START)
{
ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
}
else if (event_id == WIFI_EVENT_STA_CONNECTED)
{
ESP_LOGI(TAG, "sta connected to ssid");
vTaskDelay(3000 / portTICK_PERIOD_MS);
esp_http_client_config_t config = {
.url = FIRMWARE_REMOTE_UPGRADE_URL,
.crt_bundle_attach = esp_crt_bundle_attach,
.timeout_ms = FIRMWARE_REMOTE_UPGRADE_TIMEOUT,
.keep_alive_enable = true,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
if (client == NULL)
{
ESP_LOGE(TAG, "Failed to initialise HTTP connection");
}
esp_err_t err = esp_http_client_open(client, 0);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
}
esp_http_client_fetch_headers(client);
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED)
{
ESP_LOGI(TAG, "connect to the AP failed");
}
}
static void init_nvs(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
nvs_handle_t handle;
ESP_ERROR_CHECK(nvs_open("NVS_NAMESPACE", NVS_READWRITE, &handle));
}
static void init_wifi(void)
{
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_init());
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_loop_create_default());
esp_netif_t *sta = esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_init(&cfg));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&ip_event_handler,
NULL,
NULL));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_start());
wifi_config_t wifi_config = {
.sta = {
.ssid = "wifi-ssid",
.password = "wifi-password",
},
};
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_dhcpc_start(sta));
esp_wifi_connect();
}
void app_main(void)
{
init_nvs();
init_wifi();
}
This works fine but I have the following problem.
As you can see the URL has embedded the user and password for server verification:
Code: Select all
static const char *FIRMWARE_REMOTE_UPGRADE_URL = "https://www.lavnetremote.com/rest/utils/download-firmware?fileToDownload=MY-FIRMWARE.bin&username=MY-USERNAME&password=MY-PASSWORD";
I've tried to set log level to NONE or playing with the ".username" and ".password" properties of the http connection but the URL with this data is still in the .bin file.
Could you please let me know how to handle this?
Thank you.