mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-09-13 13:44:10 +00:00
use http_request for version cache
This commit is contained in:
+1
-1
@@ -504,7 +504,7 @@ void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
|||||||
// MQTT on_connect - when an MQTT connect is established
|
// MQTT on_connect - when an MQTT connect is established
|
||||||
void Mqtt::on_connect() {
|
void Mqtt::on_connect() {
|
||||||
if (connecting_) {
|
if (connecting_) {
|
||||||
return; // prevent duplicated connections and unsuccessful tries
|
return; // prevent duplicated connections
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("MQTT connected");
|
LOG_INFO("MQTT connected");
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include <ESP_SSLClient.h>
|
#include <ESP_SSLClient.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include "shuntingYard.h"
|
||||||
|
|
||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
@@ -421,91 +422,14 @@ bool WebStatusService::refresh_versions_cache() {
|
|||||||
#ifdef EMSESP_STANDALONE
|
#ifdef EMSESP_STANDALONE
|
||||||
return false;
|
return false;
|
||||||
#else
|
#else
|
||||||
// detect scheme from VERSIONS_URL (case-insensitive). One code path for HTTP and HTTPS,
|
std::string result;
|
||||||
// using ESP_SSLClient as a plain TCP passthrough when SSL is disabled.
|
JsonDocument doc;
|
||||||
String url = VERSIONS_URL;
|
auto http_code = http_request(VERSIONS_URL, "GET", "", doc.as<JsonObjectConst>(), result);
|
||||||
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;
|
|
||||||
if (http_code != 200) {
|
if (http_code != 200) {
|
||||||
ssl_client.stop();
|
EMSESP::logger().warning("versions.json: HTTP error code %d", http_code);
|
||||||
#if defined(EMSESP_DEBUG)
|
|
||||||
EMSESP::logger().debug("versions.json: HTTP error code %d", http_code);
|
|
||||||
#endif
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
DeserializationError err = deserializeJson(doc, result);
|
||||||
// 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();
|
|
||||||
if (err) {
|
if (err) {
|
||||||
#if defined(EMSESP_DEBUG)
|
#if defined(EMSESP_DEBUG)
|
||||||
EMSESP::logger().debug("versions.json: parse error");
|
EMSESP::logger().debug("versions.json: parse error");
|
||||||
|
|||||||
Reference in New Issue
Block a user