mirror of
https://github.com/esphome/esphome.git
synced 2025-11-01 15:41:52 +00:00
Compare commits
7 Commits
jesserockz
...
web_server
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0b2d9c34c | ||
|
|
4321fc86c2 | ||
|
|
ef0e93a9cb | ||
|
|
ae3f4ad919 | ||
|
|
6d70417cae | ||
|
|
6e24048a90 | ||
|
|
f67c5fbab2 |
@@ -39,12 +39,45 @@ namespace web_server {
|
||||
|
||||
static const char *const TAG = "web_server";
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
// Common strings used multiple times - deduplicated in PROGMEM on ESP8266
|
||||
static const char CONTENT_TYPE_JSON[] PROGMEM = "application/json";
|
||||
static const char CONTENT_TYPE_HTML[] PROGMEM = "text/html";
|
||||
static const char CONTENT_TYPE_CSS[] PROGMEM = "text/css";
|
||||
static const char CONTENT_TYPE_JS[] PROGMEM = "text/javascript";
|
||||
static const char CONTENT_TYPE_PLAIN[] PROGMEM = "text/plain";
|
||||
static const char HEADER_CONTENT_ENCODING[] PROGMEM = "Content-Encoding";
|
||||
static const char ENCODING_GZIP[] PROGMEM = "gzip";
|
||||
static const char EVENT_STATE[] PROGMEM = "state";
|
||||
static const char MSG_NOT_FOUND[] PROGMEM = "Not Found";
|
||||
#else
|
||||
// For all other platforms, regular string constants
|
||||
static const char *const CONTENT_TYPE_JSON = "application/json";
|
||||
static const char *const CONTENT_TYPE_HTML = "text/html";
|
||||
static const char *const CONTENT_TYPE_CSS = "text/css";
|
||||
static const char *const CONTENT_TYPE_JS = "text/javascript";
|
||||
static const char *const CONTENT_TYPE_PLAIN = "text/plain";
|
||||
static const char *const HEADER_CONTENT_ENCODING = "Content-Encoding";
|
||||
static const char *const ENCODING_GZIP = "gzip";
|
||||
static const char *const EVENT_STATE = "state";
|
||||
static const char *const MSG_NOT_FOUND = "Not Found";
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS
|
||||
#ifdef USE_ESP8266
|
||||
// Store single copy in PROGMEM on ESP8266
|
||||
static const char HEADER_PNA_NAME[] PROGMEM = "Private-Network-Access-Name";
|
||||
static const char HEADER_PNA_ID[] PROGMEM = "Private-Network-Access-ID";
|
||||
static const char HEADER_CORS_REQ_PNA[] PROGMEM = "Access-Control-Request-Private-Network";
|
||||
static const char HEADER_CORS_ALLOW_PNA[] PROGMEM = "Access-Control-Allow-Private-Network";
|
||||
#else
|
||||
// For all other platforms, use regular strings
|
||||
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
|
||||
#endif
|
||||
|
||||
// Parse URL and return match info
|
||||
static UrlMatch match_url(const char *url_ptr, size_t url_len, bool only_domain) {
|
||||
@@ -122,7 +155,7 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
|
||||
while (!deferred_queue_.empty()) {
|
||||
DeferredEvent &de = deferred_queue_.front();
|
||||
std::string message = de.message_generator_(web_server_, de.source_);
|
||||
if (this->send(message.c_str(), "state") != DISCARDED) {
|
||||
if (this->send(message.c_str(), EVENT_STATE) != DISCARDED) {
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
@@ -171,7 +204,7 @@ void DeferredUpdateEventSource::deferrable_send_state(void *source, const char *
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
std::string message = message_generator(web_server_, source);
|
||||
if (this->send(message.c_str(), "state") == DISCARDED) {
|
||||
if (this->send(message.c_str(), EVENT_STATE) == DISCARDED) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
@@ -206,7 +239,7 @@ void DeferredUpdateEventSourceList::try_send_nodefer(const char *message, const
|
||||
}
|
||||
|
||||
void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServerRequest *request) {
|
||||
DeferredUpdateEventSource *es = new DeferredUpdateEventSource(ws, "/events");
|
||||
DeferredUpdateEventSource *es = new DeferredUpdateEventSource(ws, F("/events"));
|
||||
this->push_back(es);
|
||||
|
||||
es->onConnect([this, ws, es](AsyncEventSourceClient *client) {
|
||||
@@ -316,21 +349,21 @@ float WebServer::get_setup_priority() const { return setup_priority::WIFI - 1.0f
|
||||
#ifdef USE_WEBSERVER_LOCAL
|
||||
void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||
#ifndef USE_ESP8266
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, CONTENT_TYPE_HTML, INDEX_GZ, sizeof(INDEX_GZ));
|
||||
#else
|
||||
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
|
||||
AsyncWebServerResponse *response = request->beginResponse_P(200, CONTENT_TYPE_HTML, INDEX_GZ, sizeof(INDEX_GZ));
|
||||
#endif
|
||||
response->addHeader("Content-Encoding", "gzip");
|
||||
response->addHeader(HEADER_CONTENT_ENCODING, ENCODING_GZIP);
|
||||
request->send(response);
|
||||
}
|
||||
#elif USE_WEBSERVER_VERSION >= 2
|
||||
void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||
#ifndef USE_ESP8266
|
||||
AsyncWebServerResponse *response =
|
||||
request->beginResponse(200, "text/html", ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
|
||||
request->beginResponse(200, CONTENT_TYPE_HTML, ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
|
||||
#else
|
||||
AsyncWebServerResponse *response =
|
||||
request->beginResponse_P(200, "text/html", ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
|
||||
request->beginResponse_P(200, CONTENT_TYPE_HTML, ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
|
||||
#endif
|
||||
// No gzip header here because the HTML file is so small
|
||||
request->send(response);
|
||||
@@ -339,8 +372,8 @@ 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");
|
||||
AsyncWebServerResponse *response = request->beginResponse(200, F(""));
|
||||
response->addHeader(HEADER_CORS_ALLOW_PNA, F("true"));
|
||||
response->addHeader(HEADER_PNA_NAME, App.get_name().c_str());
|
||||
std::string mac = get_mac_address_pretty();
|
||||
response->addHeader(HEADER_PNA_ID, mac.c_str());
|
||||
@@ -352,12 +385,12 @@ void WebServer::handle_pna_cors_request(AsyncWebServerRequest *request) {
|
||||
void WebServer::handle_css_request(AsyncWebServerRequest *request) {
|
||||
#ifndef USE_ESP8266
|
||||
AsyncWebServerResponse *response =
|
||||
request->beginResponse(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
||||
request->beginResponse(200, CONTENT_TYPE_CSS, ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
||||
#else
|
||||
AsyncWebServerResponse *response =
|
||||
request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
||||
AsyncWebServerResponse *response = request->beginResponse_P(200, CONTENT_TYPE_CSS, ESPHOME_WEBSERVER_CSS_INCLUDE,
|
||||
ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
||||
#endif
|
||||
response->addHeader("Content-Encoding", "gzip");
|
||||
response->addHeader(HEADER_CONTENT_ENCODING, ENCODING_GZIP);
|
||||
request->send(response);
|
||||
}
|
||||
#endif
|
||||
@@ -366,12 +399,12 @@ void WebServer::handle_css_request(AsyncWebServerRequest *request) {
|
||||
void WebServer::handle_js_request(AsyncWebServerRequest *request) {
|
||||
#ifndef USE_ESP8266
|
||||
AsyncWebServerResponse *response =
|
||||
request->beginResponse(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
||||
request->beginResponse(200, CONTENT_TYPE_JS, ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
||||
#else
|
||||
AsyncWebServerResponse *response =
|
||||
request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
||||
request->beginResponse_P(200, CONTENT_TYPE_JS, ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
||||
#endif
|
||||
response->addHeader("Content-Encoding", "gzip");
|
||||
response->addHeader(HEADER_CONTENT_ENCODING, ENCODING_GZIP);
|
||||
request->send(response);
|
||||
}
|
||||
#endif
|
||||
@@ -422,7 +455,7 @@ void WebServer::handle_sensor_request(AsyncWebServerRequest *request, const UrlM
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->sensor_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -467,7 +500,7 @@ void WebServer::handle_text_sensor_request(AsyncWebServerRequest *request, const
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->text_sensor_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -506,7 +539,7 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->switch_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -571,7 +604,7 @@ void WebServer::handle_button_request(AsyncWebServerRequest *request, const UrlM
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->button_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
} else if (match.method_equals("press")) {
|
||||
this->defer([obj]() { obj->press(); });
|
||||
request->send(200);
|
||||
@@ -612,7 +645,7 @@ void WebServer::handle_binary_sensor_request(AsyncWebServerRequest *request, con
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->binary_sensor_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -651,7 +684,7 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->fan_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
} else if (match.method_equals("toggle")) {
|
||||
this->defer([obj]() { obj->toggle().perform(); });
|
||||
request->send(200);
|
||||
@@ -725,7 +758,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->light_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
} else if (match.method_equals("toggle")) {
|
||||
this->defer([obj]() { obj->toggle().perform(); });
|
||||
request->send(200);
|
||||
@@ -798,7 +831,7 @@ void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMa
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->cover_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -869,7 +902,7 @@ void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlM
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->number_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
if (!match.method_equals("set")) {
|
||||
@@ -935,7 +968,7 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->date_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
if (!match.method_equals("set")) {
|
||||
@@ -991,7 +1024,7 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->time_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
if (!match.method_equals("set")) {
|
||||
@@ -1046,7 +1079,7 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->datetime_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
if (!match.method_equals("set")) {
|
||||
@@ -1103,7 +1136,7 @@ void WebServer::handle_text_request(AsyncWebServerRequest *request, const UrlMat
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->text_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
if (!match.method_equals("set")) {
|
||||
@@ -1161,7 +1194,7 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->select_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1216,7 +1249,7 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->climate_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1354,7 +1387,7 @@ void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMat
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->lock_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1425,7 +1458,7 @@ void WebServer::handle_valve_request(AsyncWebServerRequest *request, const UrlMa
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->valve_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1492,7 +1525,7 @@ void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *reques
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->alarm_control_panel_json(obj, obj->get_state(), detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1557,7 +1590,7 @@ void WebServer::handle_event_request(AsyncWebServerRequest *request, const UrlMa
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->event_json(obj, "", detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1621,7 +1654,7 @@ void WebServer::handle_update_request(AsyncWebServerRequest *request, const UrlM
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->update_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
request->send(200, CONTENT_TYPE_JSON, data.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1907,7 +1940,7 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
||||
|
||||
// No matching handler found - send 404
|
||||
ESP_LOGV(TAG, "Request for unknown URL: %s", url.c_str());
|
||||
request->send(404, "text/plain", "Not Found");
|
||||
request->send(404, CONTENT_TYPE_PLAIN, MSG_NOT_FOUND); // NOLINT(readability-suspicious-call-argument)
|
||||
}
|
||||
|
||||
bool WebServer::isRequestHandlerTrivial() const { return false; }
|
||||
|
||||
Reference in New Issue
Block a user