1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00
This commit is contained in:
daud 2018-09-30 11:28:13 -06:00 committed by Develo
parent 270788bedb
commit 678c7beab5

View File

@ -8,18 +8,18 @@ const char* password = "........";
ESP8266WebServer server(80); ESP8266WebServer server(80);
//Check if header is present and correct //Check if header is present and correct
bool is_authentified() { bool is_authenticated() {
Serial.println("Enter is_authentified"); Serial.println("Enter is_authenticated");
if (server.hasHeader("Cookie")) { if (server.hasHeader("Cookie")) {
Serial.print("Found cookie: "); Serial.print("Found cookie: ");
String cookie = server.header("Cookie"); String cookie = server.header("Cookie");
Serial.println(cookie); Serial.println(cookie);
if (cookie.indexOf("ESPSESSIONID=1") != -1) { if (cookie.indexOf("ESPSESSIONID=1") != -1) {
Serial.println("Authentification Successful"); Serial.println("Authentication Successful");
return true; return true;
} }
} }
Serial.println("Authentification Failed"); Serial.println("Authentication Failed");
return false; return false;
} }
@ -59,11 +59,11 @@ void handleLogin() {
server.send(200, "text/html", content); server.send(200, "text/html", content);
} }
//root page can be accessed only if authentification is ok //root page can be accessed only if authentication is ok
void handleRoot() { void handleRoot() {
Serial.println("Enter handleRoot"); Serial.println("Enter handleRoot");
String header; String header;
if (!is_authentified()) { if (!is_authenticated()) {
server.sendHeader("Location", "/login"); server.sendHeader("Location", "/login");
server.sendHeader("Cache-Control", "no-cache"); server.sendHeader("Cache-Control", "no-cache");
server.send(301); server.send(301);
@ -77,7 +77,7 @@ void handleRoot() {
server.send(200, "text/html", content); server.send(200, "text/html", content);
} }
//no need authentification //no need authentication
void handleNotFound() { void handleNotFound() {
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
@ -114,7 +114,7 @@ void setup(void) {
server.on("/", handleRoot); server.on("/", handleRoot);
server.on("/login", handleLogin); server.on("/login", handleLogin);
server.on("/inline", []() { server.on("/inline", []() {
server.send(200, "text/plain", "this works without need of authentification"); server.send(200, "text/plain", "this works without need of authentication");
}); });
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);