allow CORS for api requests
This commit is contained in:
parent
2719314506
commit
8f2444caeb
@ -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.\"}");
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user