Fixed http URLs not working

This commit is contained in:
Katie Paxton-Fear 2026-01-13 12:16:44 +00:00
parent 15727935fd
commit fbc8af0dba

View File

@ -2,13 +2,25 @@
#include <HTTPClient.h>
#include <HardwareSerial.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <memory>
namespace {
bool isHttpsUrl(const std::string& url) { return url.rfind("https://", 0) == 0; }
} // namespace
bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent) {
const std::unique_ptr<WiFiClientSecure> client(new WiFiClientSecure());
client->setInsecure();
// Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP
std::unique_ptr<WiFiClient> client;
if (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 +45,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<WiFiClientSecure> client(new WiFiClientSecure());
client->setInsecure();
// Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP
std::unique_ptr<WiFiClient> client;
if (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());
@ -125,4 +144,4 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string&
}
return OK;
}
}