Web server CORS headers (#840)

* Add CORS header to web server

* Refactor

* Cleanup

See also https://github.com/esphome/issues/issues/806
This commit is contained in:
Otto Winter 2019-11-12 19:03:59 +01:00 committed by GitHub
parent 51233e1931
commit 7d4f279206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,8 @@ namespace web_server {
static const char *TAG = "web_server"; static const char *TAG = "web_server";
void write_row(AsyncResponseStream *stream, Nameable *obj, const std::string &klass, const std::string &action) { void write_row(AsyncResponseStream *stream, Nameable *obj, const std::string &klass, const std::string &action) {
if (obj->is_internal())
return;
stream->print("<tr class=\""); stream->print("<tr class=\"");
stream->print(klass.c_str()); stream->print(klass.c_str());
stream->print("\" id=\""); stream->print("\" id=\"");
@ -135,40 +137,36 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
stream->print(F("\"></head><body><article class=\"markdown-body\"><h1>")); stream->print(F("\"></head><body><article class=\"markdown-body\"><h1>"));
stream->print(title.c_str()); stream->print(title.c_str());
stream->print(F("</h1><h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>")); stream->print(F("</h1><h2>States</h2><table id=\"states\"><thead><tr><th>Name<th>State<th>Actions<tbody>"));
// All content is controlled and created by user - so allowing all origins is fine here.
stream->addHeader("Access-Control-Allow-Origin", "*");
#ifdef USE_SENSOR #ifdef USE_SENSOR
for (auto *obj : App.get_sensors()) for (auto *obj : App.get_sensors())
if (!obj->is_internal())
write_row(stream, obj, "sensor", ""); write_row(stream, obj, "sensor", "");
#endif #endif
#ifdef USE_SWITCH #ifdef USE_SWITCH
for (auto *obj : App.get_switches()) for (auto *obj : App.get_switches())
if (!obj->is_internal())
write_row(stream, obj, "switch", "<button>Toggle</button>"); write_row(stream, obj, "switch", "<button>Toggle</button>");
#endif #endif
#ifdef USE_BINARY_SENSOR #ifdef USE_BINARY_SENSOR
for (auto *obj : App.get_binary_sensors()) for (auto *obj : App.get_binary_sensors())
if (!obj->is_internal())
write_row(stream, obj, "binary_sensor", ""); write_row(stream, obj, "binary_sensor", "");
#endif #endif
#ifdef USE_FAN #ifdef USE_FAN
for (auto *obj : App.get_fans()) for (auto *obj : App.get_fans())
if (!obj->is_internal())
write_row(stream, obj, "fan", "<button>Toggle</button>"); write_row(stream, obj, "fan", "<button>Toggle</button>");
#endif #endif
#ifdef USE_LIGHT #ifdef USE_LIGHT
for (auto *obj : App.get_lights()) for (auto *obj : App.get_lights())
if (!obj->is_internal())
write_row(stream, obj, "light", "<button>Toggle</button>"); write_row(stream, obj, "light", "<button>Toggle</button>");
#endif #endif
#ifdef USE_TEXT_SENSOR #ifdef USE_TEXT_SENSOR
for (auto *obj : App.get_text_sensors()) for (auto *obj : App.get_text_sensors())
if (!obj->is_internal())
write_row(stream, obj, "text_sensor", ""); write_row(stream, obj, "text_sensor", "");
#endif #endif