mirror of
https://github.com/esphome/esphome.git
synced 2024-12-11 14:49:20 +01:00
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:
parent
226e3b1dad
commit
74878276fc
@ -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,41 +137,37 @@ 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
|
||||||
|
|
||||||
stream->print(F("</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for "
|
stream->print(F("</tbody></table><p>See <a href=\"https://esphome.io/web-api/index.html\">ESPHome Web API</a> for "
|
||||||
|
Loading…
Reference in New Issue
Block a user