mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Merge branch 'web_server_8266_store_flash' into integration
This commit is contained in:
		| @@ -39,12 +39,58 @@ 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"; | ||||
|  | ||||
| // Helper macros to get __FlashStringHelper* from PROGMEM strings | ||||
| #define CONTENT_JSON_F FPSTR(CONTENT_TYPE_JSON) | ||||
| #define CONTENT_HTML_F FPSTR(CONTENT_TYPE_HTML) | ||||
| #define CONTENT_CSS_F FPSTR(CONTENT_TYPE_CSS) | ||||
| #define CONTENT_JS_F FPSTR(CONTENT_TYPE_JS) | ||||
| #define CONTENT_PLAIN_F FPSTR(CONTENT_TYPE_PLAIN) | ||||
| #define HEADER_ENCODING_F FPSTR(HEADER_CONTENT_ENCODING) | ||||
| #define GZIP_F FPSTR(ENCODING_GZIP) | ||||
| #define STATE_F FPSTR(EVENT_STATE) | ||||
| #else | ||||
| // For all other platforms, define the same names as regular string literals | ||||
| #define CONTENT_JSON_F "application/json" | ||||
| #define CONTENT_HTML_F "text/html" | ||||
| #define CONTENT_CSS_F "text/css" | ||||
| #define CONTENT_JS_F "text/javascript" | ||||
| #define CONTENT_PLAIN_F "text/plain" | ||||
| #define HEADER_ENCODING_F "Content-Encoding" | ||||
| #define GZIP_F "gzip" | ||||
| #define STATE_F "state" | ||||
| #endif | ||||
|  | ||||
| #ifdef USE_WEBSERVER_PRIVATE_NETWORK_ACCESS | ||||
| #ifdef USE_ESP8266 | ||||
| // Store single copy in PROGMEM on ESP8266 | ||||
| static const char HEADER_PNA_NAME_P[] PROGMEM = "Private-Network-Access-Name"; | ||||
| static const char HEADER_PNA_ID_P[] PROGMEM = "Private-Network-Access-ID"; | ||||
| static const char HEADER_CORS_REQ_PNA_P[] PROGMEM = "Access-Control-Request-Private-Network"; | ||||
| static const char HEADER_CORS_ALLOW_PNA_P[] PROGMEM = "Access-Control-Allow-Private-Network"; | ||||
| // Use FPSTR() to get __FlashStringHelper* from PROGMEM data | ||||
| #define HEADER_PNA_NAME FPSTR(HEADER_PNA_NAME_P) | ||||
| #define HEADER_PNA_ID FPSTR(HEADER_PNA_ID_P) | ||||
| #define HEADER_CORS_REQ_PNA FPSTR(HEADER_CORS_REQ_PNA_P) | ||||
| #define HEADER_CORS_ALLOW_PNA FPSTR(HEADER_CORS_ALLOW_PNA_P) | ||||
| #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 +168,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(), STATE_F) != 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 +217,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(), STATE_F) == DISCARDED) { | ||||
|       deq_push_back_with_dedup_(source, message_generator); | ||||
|     } else { | ||||
|       this->consecutive_send_failures_ = 0;  // Reset failure count on successful send | ||||
| @@ -206,7 +252,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 +362,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_HTML_F, 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_HTML_F, INDEX_GZ, sizeof(INDEX_GZ)); | ||||
| #endif | ||||
|   response->addHeader("Content-Encoding", "gzip"); | ||||
|   response->addHeader(HEADER_ENCODING_F, GZIP_F); | ||||
|   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_HTML_F, 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_HTML_F, 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 +385,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 +398,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_CSS_F, 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); | ||||
|       request->beginResponse_P(200, CONTENT_CSS_F, ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE); | ||||
| #endif | ||||
|   response->addHeader("Content-Encoding", "gzip"); | ||||
|   response->addHeader(HEADER_ENCODING_F, GZIP_F); | ||||
|   request->send(response); | ||||
| } | ||||
| #endif | ||||
| @@ -366,12 +412,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_JS_F, 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_JS_F, ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE); | ||||
| #endif | ||||
|   response->addHeader("Content-Encoding", "gzip"); | ||||
|   response->addHeader(HEADER_ENCODING_F, GZIP_F); | ||||
|   request->send(response); | ||||
| } | ||||
| #endif | ||||
| @@ -422,7 +468,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| @@ -467,7 +513,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| @@ -506,7 +552,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -571,7 +617,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_JSON_F, data.c_str()); | ||||
|     } else if (match.method_equals("press")) { | ||||
|       this->defer([obj]() { obj->press(); }); | ||||
|       request->send(200); | ||||
| @@ -612,7 +658,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| @@ -651,7 +697,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_JSON_F, data.c_str()); | ||||
|     } else if (match.method_equals("toggle")) { | ||||
|       this->defer([obj]() { obj->toggle().perform(); }); | ||||
|       request->send(200); | ||||
| @@ -725,7 +771,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_JSON_F, data.c_str()); | ||||
|     } else if (match.method_equals("toggle")) { | ||||
|       this->defer([obj]() { obj->toggle().perform(); }); | ||||
|       request->send(200); | ||||
| @@ -798,7 +844,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -869,7 +915,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|     if (!match.method_equals("set")) { | ||||
| @@ -935,7 +981,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|     if (!match.method_equals("set")) { | ||||
| @@ -991,7 +1037,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|     if (!match.method_equals("set")) { | ||||
| @@ -1046,7 +1092,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|     if (!match.method_equals("set")) { | ||||
| @@ -1103,7 +1149,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|     if (!match.method_equals("set")) { | ||||
| @@ -1161,7 +1207,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -1216,7 +1262,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -1354,7 +1400,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -1425,7 +1471,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -1492,7 +1538,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -1557,7 +1603,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|   } | ||||
| @@ -1621,7 +1667,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_JSON_F, data.c_str()); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -1907,7 +1953,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_PLAIN_F, F("Not Found")); | ||||
| } | ||||
|  | ||||
| bool WebServer::isRequestHandlerTrivial() const { return false; } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user