From c6adce7fad428f7591326269aa5d19340a361865 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Sat, 11 Apr 2026 20:40:52 +0200 Subject: [PATCH] Acknowledge http redirects during OTA (partial adaption of PR 1336) --- src/network/HttpDownloader.cpp | 10 ++++++++-- src/network/OtaUpdater.cpp | 9 +++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/network/HttpDownloader.cpp b/src/network/HttpDownloader.cpp index 34a39e0c..0fa1612a 100644 --- a/src/network/HttpDownloader.cpp +++ b/src/network/HttpDownloader.cpp @@ -67,7 +67,8 @@ bool HttpDownloader::fetchUrl(const std::string& url, Stream& outContent) { LOG_DBG("HTTP", "Fetching: %s", url.c_str()); http.begin(*client, url.c_str()); - http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); + http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); + http.setTimeout(30000); http.addHeader("User-Agent", "CrossPoint-ESP32-" CROSSPOINT_VERSION); // Add Basic HTTP auth if credentials are configured @@ -118,7 +119,8 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& LOG_DBG("HTTP", "Destination: %s", destPath.c_str()); http.begin(*client, url.c_str()); - http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); + http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); + http.setTimeout(65535); // max uint16_t (~65s) — HTTPClient::setTimeout takes uint16_t ms http.addHeader("User-Agent", "CrossPoint-ESP32-" CROSSPOINT_VERSION); // Add Basic HTTP auth if credentials are configured @@ -160,6 +162,10 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& FileWriteStream fileStream(file, contentLength, progress); const int writeResult = http.writeToStream(&fileStream); + // Flush before closing to ensure data is written to the SD card. + // Without this, Storage.exists() might return false immediately after + // even though the file was written (FAT not yet updated on disk). + file.flush(); file.close(); http.end(); diff --git a/src/network/OtaUpdater.cpp b/src/network/OtaUpdater.cpp index 5276d37c..871b49f9 100644 --- a/src/network/OtaUpdater.cpp +++ b/src/network/OtaUpdater.cpp @@ -71,7 +71,6 @@ OtaUpdater::OtaUpdaterError OtaUpdater::checkForUpdate() { /* Default HTTP client buffer size 512 byte only */ .buffer_size = 8192, .buffer_size_tx = 8192, - .skip_cert_common_name_check = true, .crt_bundle_attach = esp_crt_bundle_attach, .keep_alive_enable = true, }; @@ -212,14 +211,16 @@ OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate() { esp_http_client_config_t client_config = { .url = otaUrl.c_str(), - .timeout_ms = 15000, + .timeout_ms = 30000, /* Default HTTP client buffer size 512 byte only - * not sufficent to handle URL redirection cases or + * not sufficient to handle URL redirection cases or * parsing of large HTTP headers. */ + .max_redirection_count = 5, .buffer_size = 8192, .buffer_size_tx = 8192, - .skip_cert_common_name_check = true, + /* GitHub release assets redirect to objects.githubusercontent.com CDN. + * Without max_redirection_count, esp_https_ota downloads 0 bytes and stalls. */ .crt_bundle_attach = esp_crt_bundle_attach, .keep_alive_enable = true, };