From 8f2444caeb1b925393c9212cdacefd9fb7bbd7be Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Tue, 8 Mar 2022 20:33:02 +0100 Subject: [PATCH] allow CORS for api requests --- src/WebConsole.cpp | 29 +++++++++++++++++++++-------- src/WebConsole.h | 6 ++++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/WebConsole.cpp b/src/WebConsole.cpp index 7f279ad..b8eaf3c 100644 --- a/src/WebConsole.cpp +++ b/src/WebConsole.cpp @@ -45,24 +45,31 @@ bool WebConsole::isInterceptingRfid() { return catch_rfid; } +void WebConsole::_sendCORS() +{ + _server->sendHeader("Access-Control-Allow-Origin", "*"); + _server->sendHeader("Access-Control-Max-Age", "10000"); + _server->sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS"); + _server->sendHeader("Access-Control-Allow-Headers", "*"); +} void WebConsole::_handleUnknown() { if (_server->method() == HTTP_OPTIONS) { - _server->sendHeader("Access-Control-Allow-Origin", "*"); - _server->sendHeader("Access-Control-Max-Age", "10000"); - _server->sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS"); - _server->sendHeader("Access-Control-Allow-Headers", "*"); + _sendCORS(); _server->send(204); - }else { - File src = LittleFS.open("s/index.html", "r"); - _server->streamFile(src, "text/html"); - src.close(); + } + else + { + File src = LittleFS.open("s/index.html", "r"); + _server->streamFile(src, "text/html"); + src.close(); } } void WebConsole::_getUserDb() { + _sendCORS(); File src = LittleFS.open("userdb.csv", "r"); if (src) { @@ -72,6 +79,7 @@ void WebConsole::_getUserDb() } void WebConsole::_deleteUser() { + _sendCORS(); if (userdb == nullptr) { _server->send(500, "text/json", "{\"error\":\"UserDb not initialized\"}"); @@ -92,6 +100,7 @@ void WebConsole::_deleteUser() void WebConsole::_getUser() { + _sendCORS(); if (userdb == nullptr) { _server->send(500, "text/json", "{\"error\":\"UserDb not initialized\"}"); @@ -110,6 +119,7 @@ void WebConsole::_getUser() } void WebConsole::_updateUser() { + _sendCORS(); userdb::User updated; String body = _server->arg("plain"); const int capacity = 256; @@ -161,6 +171,7 @@ void WebConsole::_updateUser() } void WebConsole::_createUser() { + _sendCORS(); userdb::User created; String body = _server->arg("plain"); const int capacity = 1024; @@ -195,6 +206,7 @@ void WebConsole::_createUser() } void WebConsole::_dropUserDb() { + _sendCORS(); if (userdb->drop()) _server->send(500, "text/json", "{\"ok\":\"UserDb dropped.\"}"); else @@ -202,6 +214,7 @@ void WebConsole::_dropUserDb() } void WebConsole::_catchRFID() { + _sendCORS(); if (rfid == nullptr) { _server->send(500, "text/json", "{\"error\":\"RFID not attached.\"}"); diff --git a/src/WebConsole.h b/src/WebConsole.h index 4b40651..2371ab5 100644 --- a/src/WebConsole.h +++ b/src/WebConsole.h @@ -23,6 +23,7 @@ namespace webconsole bool isInterceptingRfid(); private: + void _sendCORS(); void _handleUnknown(); void _getUserDb(); void _deleteUser(); @@ -35,10 +36,11 @@ namespace webconsole void _print_db_raw() { File f = LittleFS.open("userdb.csv", "r"); - while (f.available()){ + while (f.available()) + { f.sendAvailable(Serial); } - _server->send(200,"text/plain","printed to serial"); + _server->send(200, "text/plain", "printed to serial"); f.close(); } ESP8266WebServer *_server;