mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Add header access using same method as arguments 2
based on @brianensor PR +sample and some sanity check
This commit is contained in:
parent
fbae22f86b
commit
ee0b8621f3
@ -0,0 +1,126 @@
|
|||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
|
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 = "<html><body><form action='/login' method='POST'>To log in, please use : admin/admin<br>";
|
||||||
|
content += "User:<input type='text' name='USERNAME' placeholder='user name'><br>";
|
||||||
|
content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
|
||||||
|
content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
|
||||||
|
content += "You also can go <a href='/inline'>here</a></body></html>";
|
||||||
|
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 = "<html><body><H2>hello, you successfully connected to esp8266!</H2><br>";
|
||||||
|
if (server.hasHeader("User-Agent")){
|
||||||
|
content += "the user agent used is : " + server.header("User-Agent") + "<br><br>";
|
||||||
|
}
|
||||||
|
content += "You can access this page until you <a href=\"/login?DISCONNECT=YES\">disconnect</a></body></html>";
|
||||||
|
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<server.args(); i++){
|
||||||
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
||||||
|
}
|
||||||
|
server.send(404, "text/plain", message);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup(void){
|
||||||
|
Serial.begin(115200);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
Serial.println("");
|
||||||
|
|
||||||
|
// Wait for connection
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("");
|
||||||
|
Serial.print("Connected to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
Serial.print("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
|
||||||
|
server.on("/", handleRoot);
|
||||||
|
server.on("/login", handleLogin);
|
||||||
|
server.on("/inline", [](){
|
||||||
|
server.send(200, "text/plain", "this works without need of authentification");
|
||||||
|
});
|
||||||
|
|
||||||
|
server.onNotFound(handleNotFound);
|
||||||
|
//here the list of headers to be recorded
|
||||||
|
const char * headerkeys[] = {"User-Agent","Cookie"} ;
|
||||||
|
size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
|
||||||
|
//ask server to track these headers
|
||||||
|
server.collectHeaders(headerkeys, headerkeyssize );
|
||||||
|
server.begin();
|
||||||
|
Serial.println("HTTP server started");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop(void){
|
||||||
|
server.handleClient();
|
||||||
|
}
|
@ -37,10 +37,15 @@ ESP8266WebServer::ESP8266WebServer(int port)
|
|||||||
, _lastHandler(0)
|
, _lastHandler(0)
|
||||||
, _currentArgCount(0)
|
, _currentArgCount(0)
|
||||||
, _currentArgs(0)
|
, _currentArgs(0)
|
||||||
|
,_headerKeysCount(0)
|
||||||
|
,_currentHeaders(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP8266WebServer::~ESP8266WebServer() {
|
ESP8266WebServer::~ESP8266WebServer() {
|
||||||
|
if (_currentHeaders)
|
||||||
|
delete[]_currentHeaders;
|
||||||
|
_headerKeysCount = 0;
|
||||||
if (!_firstHandler)
|
if (!_firstHandler)
|
||||||
return;
|
return;
|
||||||
RequestHandler* handler = _firstHandler;
|
RequestHandler* handler = _firstHandler;
|
||||||
@ -283,6 +288,48 @@ bool ESP8266WebServer::hasArg(const char* name) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String ESP8266WebServer::header(const char* name) {
|
||||||
|
for (int i = 0; i < _headerKeysCount; ++i) {
|
||||||
|
if (_currentHeaders[i].key == name)
|
||||||
|
return _currentHeaders[i].value;
|
||||||
|
}
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP8266WebServer::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
|
||||||
|
_headerKeysCount = headerKeysCount;
|
||||||
|
if (_currentHeaders)
|
||||||
|
delete[]_currentHeaders;
|
||||||
|
_currentHeaders = new RequestArgument[_headerKeysCount];
|
||||||
|
for (int i = 0; i < _headerKeysCount; i++){
|
||||||
|
_currentHeaders[i].key = headerKeys[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String ESP8266WebServer::header(int i) {
|
||||||
|
if (i < _headerKeysCount)
|
||||||
|
return _currentHeaders[i].value;
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
String ESP8266WebServer::headerName(int i) {
|
||||||
|
if (i < _headerKeysCount)
|
||||||
|
return _currentHeaders[i].key;
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
int ESP8266WebServer::headers() {
|
||||||
|
return _headerKeysCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ESP8266WebServer::hasHeader(const char* name) {
|
||||||
|
for (int i = 0; i < _headerKeysCount; ++i) {
|
||||||
|
if ((_currentHeaders[i].key == name) && (_currentHeaders[i].value.length() > 0))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
String ESP8266WebServer::hostHeader() {
|
String ESP8266WebServer::hostHeader() {
|
||||||
return _hostHeader;
|
return _hostHeader;
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,12 @@ public:
|
|||||||
String argName(int i); // get request argument name by number
|
String argName(int i); // get request argument name by number
|
||||||
int args(); // get arguments count
|
int args(); // get arguments count
|
||||||
bool hasArg(const char* name); // check if argument exists
|
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
|
String hostHeader(); // get request host header if available or empty String if not
|
||||||
|
|
||||||
@ -124,6 +130,7 @@ protected:
|
|||||||
void _uploadWriteByte(uint8_t b);
|
void _uploadWriteByte(uint8_t b);
|
||||||
uint8_t _uploadReadByte(WiFiClient& client);
|
uint8_t _uploadReadByte(WiFiClient& client);
|
||||||
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
|
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
|
||||||
|
bool _collectHeader(const char* headerName, const char* headerValue);
|
||||||
|
|
||||||
struct RequestArgument {
|
struct RequestArgument {
|
||||||
String key;
|
String key;
|
||||||
@ -140,6 +147,8 @@ protected:
|
|||||||
RequestArgument* _currentArgs;
|
RequestArgument* _currentArgs;
|
||||||
HTTPUpload _currentUpload;
|
HTTPUpload _currentUpload;
|
||||||
|
|
||||||
|
RequestArgument* _currentHeaders;
|
||||||
|
size_t _headerKeysCount;
|
||||||
size_t _contentLength;
|
size_t _contentLength;
|
||||||
String _responseHeaders;
|
String _responseHeaders;
|
||||||
|
|
||||||
|
@ -31,6 +31,10 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
|
|||||||
// Read the first line of HTTP request
|
// Read the first line of HTTP request
|
||||||
String req = client.readStringUntil('\r');
|
String req = client.readStringUntil('\r');
|
||||||
client.readStringUntil('\n');
|
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"
|
// First line of HTTP request looks like "GET /path HTTP/1.1"
|
||||||
// Retrieve the "/path" part by finding the spaces
|
// Retrieve the "/path" part by finding the spaces
|
||||||
@ -96,6 +100,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
|
|||||||
}
|
}
|
||||||
headerName = req.substring(0, headerDiv);
|
headerName = req.substring(0, headerDiv);
|
||||||
headerValue = req.substring(headerDiv + 2);
|
headerValue = req.substring(headerDiv + 2);
|
||||||
|
_collectHeader(headerName.c_str(),headerValue.c_str());
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
DEBUG_OUTPUT.print("headerName: ");
|
DEBUG_OUTPUT.print("headerName: ");
|
||||||
@ -161,6 +166,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
|
|||||||
}
|
}
|
||||||
headerName = req.substring(0, headerDiv);
|
headerName = req.substring(0, headerDiv);
|
||||||
headerValue = req.substring(headerDiv + 2);
|
headerValue = req.substring(headerDiv + 2);
|
||||||
|
_collectHeader(headerName.c_str(),headerValue.c_str());
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
DEBUG_OUTPUT.print("headerName: ");
|
DEBUG_OUTPUT.print("headerName: ");
|
||||||
@ -187,6 +193,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
|
|||||||
return true;
|
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) {
|
void ESP8266WebServer::_parseArguments(String data) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
Loading…
x
Reference in New Issue
Block a user