use http_request for version cache

This commit is contained in:
MichaelDvP
2026-08-20 12:44:21 +02:00
parent c45441e3be
commit 88f258d7e9
2 changed files with 7 additions and 83 deletions
+1 -1
View File
@@ -504,7 +504,7 @@ void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
// MQTT on_connect - when an MQTT connect is established
void Mqtt::on_connect() {
if (connecting_) {
return; // prevent duplicated connections and unsuccessful tries
return; // prevent duplicated connections
}
LOG_INFO("MQTT connected");
+6 -82
View File
@@ -23,6 +23,7 @@
#include <WiFiClient.h>
#include <ESP_SSLClient.h>
#endif
#include "shuntingYard.h"
namespace emsesp {
@@ -421,91 +422,14 @@ bool WebStatusService::refresh_versions_cache() {
#ifdef EMSESP_STANDALONE
return false;
#else
// detect scheme from VERSIONS_URL (case-insensitive). One code path for HTTP and HTTPS,
// using ESP_SSLClient as a plain TCP passthrough when SSL is disabled.
String url = VERSIONS_URL;
String lower = url;
lower.toLowerCase();
const bool is_https = lower.startsWith("https://");
if (!is_https && !lower.startsWith("http://")) {
#if defined(EMSESP_DEBUG)
EMSESP::logger().debug("versions.json: unsupported scheme");
#endif
return false;
}
const int scheme_len = is_https ? 8 : 7;
WiFiClient basic_client;
ESP_SSLClient ssl_client;
if (is_https) {
ssl_client.setInsecure();
ssl_client.setBufferSizes(16384, 1024); // versions.json fits easily but TLS records may be full-size
ssl_client.setSessionTimeout(120);
}
basic_client.setTimeout(5000);
ssl_client.setTimeout(5);
ssl_client.setClient(&basic_client, is_https);
// split into host and path
String rest = url.substring(scheme_len);
String host;
String path;
int s = rest.indexOf('/');
if (s < 0) {
host = rest;
path = "/";
} else {
host = rest.substring(0, s);
path = rest.substring(s);
}
if (!ssl_client.connect(host.c_str(), is_https ? 443 : 80)) {
#if defined(EMSESP_DEBUG)
EMSESP::logger().debug("versions.json: connection failed");
#endif
return false;
}
// minimal HTTP/1.0 GET so we don't have to handle chunked encoding
ssl_client.print("GET ");
ssl_client.print(path);
ssl_client.println(" HTTP/1.0");
ssl_client.print("Host: ");
ssl_client.println(host);
ssl_client.println("User-Agent: EMS-ESP");
ssl_client.println("Connection: close");
ssl_client.print("\r\n");
// wait for the first byte. The 5 seconds is GitHub's SLO
uint32_t ms = millis();
while (ssl_client.connected() && !ssl_client.available() && millis() - ms < 5000) {
delay(1);
}
// parse status line
String status_line = ssl_client.readStringUntil('\n');
int sp = status_line.indexOf(' ');
int http_code = (sp >= 0) ? status_line.substring(sp + 1, sp + 4).toInt() : 0;
std::string result;
JsonDocument doc;
auto http_code = http_request(VERSIONS_URL, "GET", "", doc.as<JsonObjectConst>(), result);
if (http_code != 200) {
ssl_client.stop();
#if defined(EMSESP_DEBUG)
EMSESP::logger().debug("versions.json: HTTP error code %d", http_code);
#endif
EMSESP::logger().warning("versions.json: HTTP error code %d", http_code);
return false;
}
// skip headers
while (ssl_client.connected() || ssl_client.available()) {
String line = ssl_client.readStringUntil('\n');
line.trim();
if (line.isEmpty()) {
break;
}
}
JsonDocument doc(PSRAM_DOC);
DeserializationError err = deserializeJson(doc, ssl_client);
ssl_client.stop();
DeserializationError err = deserializeJson(doc, result);
if (err) {
#if defined(EMSESP_DEBUG)
EMSESP::logger().debug("versions.json: parse error");