From 7d4f2792061938f54a0ebee5c434e46b9d5c2d25 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 12 Nov 2019 19:03:59 +0100 Subject: [PATCH] Web server CORS headers (#840) * Add CORS header to web server * Refactor * Cleanup See also https://github.com/esphome/issues/issues/806 --- esphome/components/web_server/web_server.cpp | 22 +++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 4fdbbbce7d..c6204533d4 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -18,6 +18,8 @@ namespace web_server { static const char *TAG = "web_server"; void write_row(AsyncResponseStream *stream, Nameable *obj, const std::string &klass, const std::string &action) { + if (obj->is_internal()) + return; stream->print("print(klass.c_str()); stream->print("\" id=\""); @@ -135,41 +137,37 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) { stream->print(F("\">

")); stream->print(title.c_str()); stream->print(F("

States

")); + // All content is controlled and created by user - so allowing all origins is fine here. + stream->addHeader("Access-Control-Allow-Origin", "*"); #ifdef USE_SENSOR for (auto *obj : App.get_sensors()) - if (!obj->is_internal()) - write_row(stream, obj, "sensor", ""); + write_row(stream, obj, "sensor", ""); #endif #ifdef USE_SWITCH for (auto *obj : App.get_switches()) - if (!obj->is_internal()) - write_row(stream, obj, "switch", ""); + write_row(stream, obj, "switch", ""); #endif #ifdef USE_BINARY_SENSOR for (auto *obj : App.get_binary_sensors()) - if (!obj->is_internal()) - write_row(stream, obj, "binary_sensor", ""); + write_row(stream, obj, "binary_sensor", ""); #endif #ifdef USE_FAN for (auto *obj : App.get_fans()) - if (!obj->is_internal()) - write_row(stream, obj, "fan", ""); + write_row(stream, obj, "fan", ""); #endif #ifdef USE_LIGHT for (auto *obj : App.get_lights()) - if (!obj->is_internal()) - write_row(stream, obj, "light", ""); + write_row(stream, obj, "light", ""); #endif #ifdef USE_TEXT_SENSOR for (auto *obj : App.get_text_sensors()) - if (!obj->is_internal()) - write_row(stream, obj, "text_sensor", ""); + write_row(stream, obj, "text_sensor", ""); #endif stream->print(F("
NameStateActions

See ESPHome Web API for "