[web_server] Move HTTP header strings to flash on ESP8266 (#12668)

This commit is contained in:
J. Nick Koston
2025-12-26 08:44:17 -10:00
committed by GitHub
parent 0919017d49
commit f1fecd22e3
2 changed files with 9 additions and 16 deletions

View File

@@ -45,13 +45,6 @@ static const char *const TAG = "web_server";
static constexpr size_t PSTR_LOCAL_SIZE = 18;
#define PSTR_LOCAL(mode_s) ESPHOME_strncpy_P(buf, (ESPHOME_PGM_P) ((mode_s)), PSTR_LOCAL_SIZE - 1)
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
static const char *const HEADER_PNA_NAME = "Private-Network-Access-Name";
static const char *const HEADER_PNA_ID = "Private-Network-Access-ID";
static const char *const HEADER_CORS_REQ_PNA = "Access-Control-Request-Private-Network";
static const char *const HEADER_CORS_ALLOW_PNA = "Access-Control-Allow-Private-Network";
#endif
// Parse URL and return match info
static UrlMatch match_url(const char *url_ptr, size_t url_len, bool only_domain) {
UrlMatch match{};
@@ -348,7 +341,7 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
#else
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
#endif
response->addHeader("Content-Encoding", "gzip");
response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
request->send(response);
}
#elif USE_WEBSERVER_VERSION >= 2
@@ -368,10 +361,10 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
void WebServer::handle_pna_cors_request(AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(200, "");
response->addHeader(HEADER_CORS_ALLOW_PNA, "true");
response->addHeader(HEADER_PNA_NAME, App.get_name().c_str());
response->addHeader(ESPHOME_F("Access-Control-Allow-Private-Network"), ESPHOME_F("true"));
response->addHeader(ESPHOME_F("Private-Network-Access-Name"), App.get_name().c_str());
char mac_s[18];
response->addHeader(HEADER_PNA_ID, get_mac_address_pretty_into_buffer(mac_s));
response->addHeader(ESPHOME_F("Private-Network-Access-ID"), get_mac_address_pretty_into_buffer(mac_s));
request->send(response);
}
#endif
@@ -385,7 +378,7 @@ void WebServer::handle_css_request(AsyncWebServerRequest *request) {
AsyncWebServerResponse *response =
request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
#endif
response->addHeader("Content-Encoding", "gzip");
response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
request->send(response);
}
#endif
@@ -399,7 +392,7 @@ void WebServer::handle_js_request(AsyncWebServerRequest *request) {
AsyncWebServerResponse *response =
request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
#endif
response->addHeader("Content-Encoding", "gzip");
response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
request->send(response);
}
#endif
@@ -1841,7 +1834,7 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) const {
}
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
if (method == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA))
if (method == HTTP_OPTIONS && request->hasHeader(ESPHOME_F("Access-Control-Request-Private-Network")))
return true;
#endif
@@ -1974,7 +1967,7 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
#endif
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
if (request->method() == HTTP_OPTIONS && request->hasHeader(HEADER_CORS_REQ_PNA)) {
if (request->method() == HTTP_OPTIONS && request->hasHeader(ESPHOME_F("Access-Control-Request-Private-Network"))) {
this->handle_pna_cors_request(request);
return;
}

View File

@@ -100,7 +100,7 @@ class WebServerBase : public Component {
}
this->server_ = std::make_unique<AsyncWebServer>(this->port_);
// All content is controlled and created by user - so allowing all origins is fine here.
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
DefaultHeaders::Instance().addHeader(ESPHOME_F("Access-Control-Allow-Origin"), ESPHOME_F("*"));
this->server_->begin();
for (auto *handler : this->handlers_)