Fixes issue with Calibre web expecting SSL (#347)
http urls now work with Calibre web --------- Co-authored-by: Dave Allie <dave@daveallie.com>
This commit is contained in:
parent
c2fb8ce55d
commit
847786e342
@ -2,13 +2,23 @@
|
|||||||
|
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "util/UrlUtils.h"
|
||||||
|
|
||||||
bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent) {
|
bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent) {
|
||||||
const std::unique_ptr<WiFiClientSecure> client(new WiFiClientSecure());
|
// Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP
|
||||||
client->setInsecure();
|
std::unique_ptr<WiFiClient> client;
|
||||||
|
if (UrlUtils::isHttpsUrl(url)) {
|
||||||
|
auto* secureClient = new WiFiClientSecure();
|
||||||
|
secureClient->setInsecure();
|
||||||
|
client.reset(secureClient);
|
||||||
|
} else {
|
||||||
|
client.reset(new WiFiClient());
|
||||||
|
}
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
Serial.printf("[%lu] [HTTP] Fetching: %s\n", millis(), url.c_str());
|
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,
|
HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& url, const std::string& destPath,
|
||||||
ProgressCallback progress) {
|
ProgressCallback progress) {
|
||||||
const std::unique_ptr<WiFiClientSecure> client(new WiFiClientSecure());
|
// Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP
|
||||||
client->setInsecure();
|
std::unique_ptr<WiFiClient> client;
|
||||||
|
if (UrlUtils::isHttpsUrl(url)) {
|
||||||
|
auto* secureClient = new WiFiClientSecure();
|
||||||
|
secureClient->setInsecure();
|
||||||
|
client.reset(secureClient);
|
||||||
|
} else {
|
||||||
|
client.reset(new WiFiClient());
|
||||||
|
}
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
|
|
||||||
Serial.printf("[%lu] [HTTP] Downloading: %s\n", millis(), url.c_str());
|
Serial.printf("[%lu] [HTTP] Downloading: %s\n", millis(), url.c_str());
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace UrlUtils {
|
namespace UrlUtils {
|
||||||
|
|
||||||
|
bool isHttpsUrl(const std::string& url) { return url.rfind("https://", 0) == 0; }
|
||||||
|
|
||||||
std::string ensureProtocol(const std::string& url) {
|
std::string ensureProtocol(const std::string& url) {
|
||||||
if (url.find("://") == std::string::npos) {
|
if (url.find("://") == std::string::npos) {
|
||||||
return "http://" + url;
|
return "http://" + url;
|
||||||
|
|||||||
@ -3,6 +3,11 @@
|
|||||||
|
|
||||||
namespace UrlUtils {
|
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)
|
* Prepend http:// if no protocol specified (server will redirect to https if needed)
|
||||||
*/
|
*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user