From ee0b8621f3d49ab45db53aeda054f45e7bea6af9 Mon Sep 17 00:00:00 2001 From: luc Date: Wed, 28 Oct 2015 13:58:53 +0800 Subject: [PATCH] Add header access using same method as arguments 2 based on @brianensor PR +sample and some sanity check --- .../SimpleAuthentification.ino | 126 ++++++++++++++++++ .../ESP8266WebServer/src/ESP8266WebServer.cpp | 47 +++++++ .../ESP8266WebServer/src/ESP8266WebServer.h | 9 ++ libraries/ESP8266WebServer/src/Parsing.cpp | 15 +++ 4 files changed, 197 insertions(+) create mode 100644 libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino diff --git a/libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino b/libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino new file mode 100644 index 000000000..19fc69c7d --- /dev/null +++ b/libraries/ESP8266WebServer/examples/SimpleAuthentification/SimpleAuthentification.ino @@ -0,0 +1,126 @@ +#include +#include +#include + +const char* ssid = "........"; +const char* password = "........"; + +ESP8266WebServer server(80); + +//Check if header is present and correct +bool is_authentified(){ + Serial.println("Enter is_authentified"); + if (server.hasHeader("Cookie")){ + Serial.print("Found cookie: "); + String cookie = server.header("Cookie"); + Serial.println(cookie); + if (cookie.indexOf("ESPSESSIONID=1") != -1) { + Serial.println("Authentification Successful"); + return true; + } + } + Serial.println("Authentification Failed"); + return false; +} + +//login page, also called for disconnect +void handleLogin(){ + String msg; + if (server.hasHeader("Cookie")){ + Serial.print("Found cookie: "); + String cookie = server.header("Cookie"); + Serial.println(cookie); + } + if (server.hasArg("DISCONNECT")){ + Serial.println("Disconnection"); + String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; + server.sendContent(header); + return; + } + if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){ + if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin" ){ + String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n"; + server.sendContent(header); + Serial.println("Log in Successful"); + return; + } + msg = "Wrong username/password! try again."; + Serial.println("Log in Failed"); + } + String content = "
To log in, please use : admin/admin
"; + content += "User:
"; + content += "Password:
"; + content += "
" + msg + "
"; + content += "You also can go here"; + server.send(200, "text/html", content); +} + +//root page can be accessed only if authentification is ok +void handleRoot(){ + Serial.println("Enter handleRoot"); + String header; + if (!is_authentified()){ + String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; + server.sendContent(header); + return; + } + String content = "

hello, you successfully connected to esp8266!


"; + if (server.hasHeader("User-Agent")){ + content += "the user agent used is : " + server.header("User-Agent") + "

"; + } + content += "You can access this page until you disconnect"; + server.send(200, "text/html", content); +} + +//no need authentification +void handleNotFound(){ + String message = "File Not Found\n\n"; + message += "URI: "; + message += server.uri(); + message += "\nMethod: "; + message += (server.method() == HTTP_GET)?"GET":"POST"; + message += "\nArguments: "; + message += server.args(); + message += "\n"; + for (uint8_t i=0; i 0)) + return true; + } + return false; +} + String ESP8266WebServer::hostHeader() { return _hostHeader; } diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 8a640a935..40216f482 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -83,6 +83,12 @@ public: String argName(int i); // get request argument name by number int args(); // get arguments count bool hasArg(const char* name); // check if argument exists + void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect + String header(const char* name); // get request header value by name + String header(int i); // get request header value by number + String headerName(int i); // get request header name by number + int headers(); // get header count + bool hasHeader(const char* name); // check if header exists String hostHeader(); // get request host header if available or empty String if not @@ -124,6 +130,7 @@ protected: void _uploadWriteByte(uint8_t b); uint8_t _uploadReadByte(WiFiClient& client); void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength); + bool _collectHeader(const char* headerName, const char* headerValue); struct RequestArgument { String key; @@ -140,6 +147,8 @@ protected: RequestArgument* _currentArgs; HTTPUpload _currentUpload; + RequestArgument* _currentHeaders; + size_t _headerKeysCount; size_t _contentLength; String _responseHeaders; diff --git a/libraries/ESP8266WebServer/src/Parsing.cpp b/libraries/ESP8266WebServer/src/Parsing.cpp index 216f20c3e..0458a9980 100644 --- a/libraries/ESP8266WebServer/src/Parsing.cpp +++ b/libraries/ESP8266WebServer/src/Parsing.cpp @@ -31,6 +31,10 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { // Read the first line of HTTP request String req = client.readStringUntil('\r'); client.readStringUntil('\n'); + //reset header value + for (int i = 0; i < _headerKeysCount; ++i) { + _currentHeaders[i].value =String(); + } // First line of HTTP request looks like "GET /path HTTP/1.1" // Retrieve the "/path" part by finding the spaces @@ -96,6 +100,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); + _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG DEBUG_OUTPUT.print("headerName: "); @@ -161,6 +166,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { } headerName = req.substring(0, headerDiv); headerValue = req.substring(headerDiv + 2); + _collectHeader(headerName.c_str(),headerValue.c_str()); #ifdef DEBUG DEBUG_OUTPUT.print("headerName: "); @@ -187,6 +193,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) { return true; } +bool ESP8266WebServer::_collectHeader(const char* headerName, const char* headerValue) { + for (size_t i = 0; i < _headerKeysCount; i++) { + if (_currentHeaders[i].key==headerName) { + _currentHeaders[i].value=headerValue; + return true; + } + } + return false; +} void ESP8266WebServer::_parseArguments(String data) { #ifdef DEBUG