[web_server] Move HTTP header strings to flash on ESP8266 (#12668)
This commit is contained in:
@@ -45,13 +45,6 @@ static const char *const TAG = "web_server";
|
|||||||
static constexpr size_t PSTR_LOCAL_SIZE = 18;
|
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)
|
#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
|
// Parse URL and return match info
|
||||||
static UrlMatch match_url(const char *url_ptr, size_t url_len, bool only_domain) {
|
static UrlMatch match_url(const char *url_ptr, size_t url_len, bool only_domain) {
|
||||||
UrlMatch match{};
|
UrlMatch match{};
|
||||||
@@ -348,7 +341,7 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
|||||||
#else
|
#else
|
||||||
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
|
||||||
#endif
|
#endif
|
||||||
response->addHeader("Content-Encoding", "gzip");
|
response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#elif USE_WEBSERVER_VERSION >= 2
|
#elif USE_WEBSERVER_VERSION >= 2
|
||||||
@@ -368,10 +361,10 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
|||||||
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
|
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
|
||||||
void WebServer::handle_pna_cors_request(AsyncWebServerRequest *request) {
|
void WebServer::handle_pna_cors_request(AsyncWebServerRequest *request) {
|
||||||
AsyncWebServerResponse *response = request->beginResponse(200, "");
|
AsyncWebServerResponse *response = request->beginResponse(200, "");
|
||||||
response->addHeader(HEADER_CORS_ALLOW_PNA, "true");
|
response->addHeader(ESPHOME_F("Access-Control-Allow-Private-Network"), ESPHOME_F("true"));
|
||||||
response->addHeader(HEADER_PNA_NAME, App.get_name().c_str());
|
response->addHeader(ESPHOME_F("Private-Network-Access-Name"), App.get_name().c_str());
|
||||||
char mac_s[18];
|
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);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -385,7 +378,7 @@ void WebServer::handle_css_request(AsyncWebServerRequest *request) {
|
|||||||
AsyncWebServerResponse *response =
|
AsyncWebServerResponse *response =
|
||||||
request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
||||||
#endif
|
#endif
|
||||||
response->addHeader("Content-Encoding", "gzip");
|
response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -399,7 +392,7 @@ void WebServer::handle_js_request(AsyncWebServerRequest *request) {
|
|||||||
AsyncWebServerResponse *response =
|
AsyncWebServerResponse *response =
|
||||||
request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
||||||
#endif
|
#endif
|
||||||
response->addHeader("Content-Encoding", "gzip");
|
response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -1841,7 +1834,7 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
|
#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;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -1974,7 +1967,7 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
|
#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);
|
this->handle_pna_cors_request(request);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ class WebServerBase : public Component {
|
|||||||
}
|
}
|
||||||
this->server_ = std::make_unique<AsyncWebServer>(this->port_);
|
this->server_ = std::make_unique<AsyncWebServer>(this->port_);
|
||||||
// All content is controlled and created by user - so allowing all origins is fine here.
|
// 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();
|
this->server_->begin();
|
||||||
|
|
||||||
for (auto *handler : this->handlers_)
|
for (auto *handler : this->handlers_)
|
||||||
|
|||||||
Reference in New Issue
Block a user