diff --git a/src/network/HttpDownloader.cpp b/src/network/HttpDownloader.cpp index 017c6870..c4de3a05 100644 --- a/src/network/HttpDownloader.cpp +++ b/src/network/HttpDownloader.cpp @@ -2,13 +2,23 @@ #include #include +#include #include #include +#include "util/UrlUtils.h" + bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent) { - const std::unique_ptr client(new WiFiClientSecure()); - client->setInsecure(); + // Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP + std::unique_ptr client; + if (UrlUtils::isHttpsUrl(url)) { + auto* secureClient = new WiFiClientSecure(); + secureClient->setInsecure(); + client.reset(secureClient); + } else { + client.reset(new WiFiClient()); + } HTTPClient http; Serial.printf("[%lu] [HTTP] Fetching: %s\n", millis(), url.c_str()); @@ -33,8 +43,15 @@ bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent) { HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& url, const std::string& destPath, ProgressCallback progress) { - const std::unique_ptr client(new WiFiClientSecure()); - client->setInsecure(); + // Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP + std::unique_ptr client; + if (UrlUtils::isHttpsUrl(url)) { + auto* secureClient = new WiFiClientSecure(); + secureClient->setInsecure(); + client.reset(secureClient); + } else { + client.reset(new WiFiClient()); + } HTTPClient http; Serial.printf("[%lu] [HTTP] Downloading: %s\n", millis(), url.c_str()); diff --git a/src/util/UrlUtils.cpp b/src/util/UrlUtils.cpp index 0eeeae3a..bbf5ac15 100644 --- a/src/util/UrlUtils.cpp +++ b/src/util/UrlUtils.cpp @@ -2,6 +2,8 @@ namespace UrlUtils { +bool isHttpsUrl(const std::string& url) { return url.rfind("https://", 0) == 0; } + std::string ensureProtocol(const std::string& url) { if (url.find("://") == std::string::npos) { return "http://" + url; diff --git a/src/util/UrlUtils.h b/src/util/UrlUtils.h index d88ca13c..6428161b 100644 --- a/src/util/UrlUtils.h +++ b/src/util/UrlUtils.h @@ -3,6 +3,11 @@ namespace UrlUtils { +/** + * Check if URL uses HTTPS protocol + */ +bool isHttpsUrl(const std::string& url); + /** * Prepend http:// if no protocol specified (server will redirect to https if needed) */