From 8dbf85e1a8cb56ffe6b8278b8d875f87baea4cc3 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 12 Jun 2024 21:54:25 +0530 Subject: [PATCH 01/10] feat: added support for filters in webserver --- libraries/WebServer/src/Parsing.cpp | 14 +++---- libraries/WebServer/src/WebServer.cpp | 25 +++++++++--- libraries/WebServer/src/WebServer.h | 16 ++++++-- .../WebServer/src/detail/RequestHandler.h | 14 +++++-- .../src/detail/RequestHandlersImpl.h | 39 ++++++++++++------- 5 files changed, 75 insertions(+), 33 deletions(-) diff --git a/libraries/WebServer/src/Parsing.cpp b/libraries/WebServer/src/Parsing.cpp index 3d3e7d1c55a..200244e6848 100644 --- a/libraries/WebServer/src/Parsing.cpp +++ b/libraries/WebServer/src/Parsing.cpp @@ -124,7 +124,7 @@ bool WebServer::_parseRequest(NetworkClient &client) { //attach handler RequestHandler *handler; for (handler = _firstHandler; handler; handler = handler->next()) { - if (handler->canHandle(_currentMethod, _currentUri)) { + if (handler->canHandle(*this, _currentMethod, _currentUri)) { break; } } @@ -176,7 +176,7 @@ bool WebServer::_parseRequest(NetworkClient &client) { } } - if (!isForm && _currentHandler && _currentHandler->canRaw(_currentUri)) { + if (!isForm && _currentHandler && _currentHandler->canRaw(*this, _currentUri)) { log_v("Parse raw"); _currentRaw.reset(new HTTPRaw()); _currentRaw->status = RAW_START; @@ -334,7 +334,7 @@ void WebServer::_parseArguments(String data) { void WebServer::_uploadWriteByte(uint8_t b) { if (_currentUpload->currentSize == HTTP_UPLOAD_BUFLEN) { - if (_currentHandler && _currentHandler->canUpload(_currentUri)) { + if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) { _currentHandler->upload(*this, _currentUri, *_currentUpload); } _currentUpload->totalSize += _currentUpload->currentSize; @@ -449,7 +449,7 @@ bool WebServer::_parseForm(NetworkClient &client, String boundary, uint32_t len) _currentUpload->totalSize = 0; _currentUpload->currentSize = 0; log_v("Start File: %s Type: %s", _currentUpload->filename.c_str(), _currentUpload->type.c_str()); - if (_currentHandler && _currentHandler->canUpload(_currentUri)) { + if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) { _currentHandler->upload(*this, _currentUri, *_currentUpload); } _currentUpload->status = UPLOAD_FILE_WRITE; @@ -488,12 +488,12 @@ bool WebServer::_parseForm(NetworkClient &client, String boundary, uint32_t len) } } // Found the boundary string, finish processing this file upload - if (_currentHandler && _currentHandler->canUpload(_currentUri)) { + if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) { _currentHandler->upload(*this, _currentUri, *_currentUpload); } _currentUpload->totalSize += _currentUpload->currentSize; _currentUpload->status = UPLOAD_FILE_END; - if (_currentHandler && _currentHandler->canUpload(_currentUri)) { + if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) { _currentHandler->upload(*this, _currentUri, *_currentUpload); } log_v("End File: %s Type: %s Size: %d", _currentUpload->filename.c_str(), _currentUpload->type.c_str(), (int)_currentUpload->totalSize); @@ -567,7 +567,7 @@ String WebServer::urlDecode(const String &text) { bool WebServer::_parseFormUploadAborted() { _currentUpload->status = UPLOAD_FILE_ABORTED; - if (_currentHandler && _currentHandler->canUpload(_currentUri)) { + if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) { _currentHandler->upload(*this, _currentUri, *_currentUpload); } return false; diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index 84b4de33518..a291ff527fd 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -306,16 +306,18 @@ void WebServer::requestAuthentication(HTTPAuthMethod mode, const char *realm, co send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg); } -void WebServer::on(const Uri &uri, WebServer::THandlerFunction handler) { - on(uri, HTTP_ANY, handler); +RequestHandler& WebServer::on(const Uri &uri, WebServer::THandlerFunction handler) { + return on(uri, HTTP_ANY, handler); } -void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn) { - on(uri, method, fn, _fileUploadHandler); +RequestHandler& WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn) { + return on(uri, method, fn, _fileUploadHandler); } -void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn) { - _addRequestHandler(new FunctionRequestHandler(fn, ufn, uri, method)); +RequestHandler& WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn) { + FunctionRequestHandler *handler = new FunctionRequestHandler(fn, ufn, uri, method); + _addRequestHandler(handler); + return *handler; } void WebServer::addHandler(RequestHandler *handler) { @@ -796,3 +798,14 @@ String WebServer::_responseCodeToString(int code) { default: return F(""); } } + +bool ON_STA_FILTER(WebServer &server) { + return ( + (WiFi.localIP() != IPAddress(0, 0, 0, 0) && server.client().localIP() != IPAddress(0, 0, 0, 0)) + && WiFi.localIP() == server.client().localIP() + ); +} + +bool ON_AP_FILTER(WebServer &server) { + return WiFi.localIP() == server.client().localIP(); +} diff --git a/libraries/WebServer/src/WebServer.h b/libraries/WebServer/src/WebServer.h index 1382885e1d9..531236acf62 100644 --- a/libraries/WebServer/src/WebServer.h +++ b/libraries/WebServer/src/WebServer.h @@ -26,6 +26,7 @@ #include #include #include "FS.h" +#include "WiFi.h" #include "Network.h" #include "HTTP_Method.h" #include "Uri.h" @@ -144,9 +145,10 @@ class WebServer { void requestAuthentication(HTTPAuthMethod mode = BASIC_AUTH, const char *realm = NULL, const String &authFailMsg = String("")); typedef std::function THandlerFunction; - void on(const Uri &uri, THandlerFunction fn); - void on(const Uri &uri, HTTPMethod method, THandlerFunction fn); - void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads + typedef std::function FilterFunction; + RequestHandler& on(const Uri &uri, THandlerFunction fn); + RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn); + RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads void addHandler(RequestHandler *handler); void serveStatic(const char *uri, fs::FS &fs, const char *path, const char *cache_header = NULL); void onNotFound(THandlerFunction fn); //called when handler is not assigned @@ -292,4 +294,12 @@ class WebServer { String _srealm; // Store the Auth realm between Calls }; +/* + Request Filters +*/ + +bool ON_STA_FILTER(WebServer &server); + +bool ON_AP_FILTER(WebServer &server); + #endif //ESP8266WEBSERVER_H diff --git a/libraries/WebServer/src/detail/RequestHandler.h b/libraries/WebServer/src/detail/RequestHandler.h index 4ef4b1d0075..e80cd407ce6 100644 --- a/libraries/WebServer/src/detail/RequestHandler.h +++ b/libraries/WebServer/src/detail/RequestHandler.h @@ -7,16 +7,19 @@ class RequestHandler { public: virtual ~RequestHandler() {} - virtual bool canHandle(HTTPMethod method, String uri) { + virtual bool canHandle(WebServer &server, HTTPMethod method, String uri) { + (void)server; (void)method; (void)uri; return false; } - virtual bool canUpload(String uri) { + virtual bool canUpload(WebServer &server, String uri) { + (void)server; (void)uri; return false; } - virtual bool canRaw(String uri) { + virtual bool canRaw(WebServer &server, String uri) { + (void)server; (void)uri; return false; } @@ -37,6 +40,11 @@ class RequestHandler { (void)raw; } + virtual RequestHandler& setFilter(std::function filter) { + (void)filter; + return *this; + } + RequestHandler *next() { return _next; } diff --git a/libraries/WebServer/src/detail/RequestHandlersImpl.h b/libraries/WebServer/src/detail/RequestHandlersImpl.h index d24d36fd9d4..d56262ba04a 100644 --- a/libraries/WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/WebServer/src/detail/RequestHandlersImpl.h @@ -21,23 +21,24 @@ class FunctionRequestHandler : public RequestHandler { delete _uri; } - bool canHandle(HTTPMethod requestMethod, String requestUri) override { + bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override { if (_method != HTTP_ANY && _method != requestMethod) { return false; } - return _uri->canHandle(requestUri, pathArgs); + return _uri->canHandle(requestUri, pathArgs) && (_filter != NULL ? _filter(server) : true); } - bool canUpload(String requestUri) override { - if (!_ufn || !canHandle(HTTP_POST, requestUri)) { + bool canUpload(WebServer &server, String requestUri) override { + if (!_ufn || !canHandle(server, HTTP_POST, requestUri)) { return false; } return true; } - bool canRaw(String requestUri) override { - if (!_ufn || _method == HTTP_GET) { + + bool canRaw(WebServer &server, String requestUri) override { + if (!_ufn || _method == HTTP_GET || (_filter != NULL ? _filter(server) == false : false)) { return false; } @@ -45,8 +46,7 @@ class FunctionRequestHandler : public RequestHandler { } bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override { - (void)server; - if (!canHandle(requestMethod, requestUri)) { + if (!canHandle(server, requestMethod, requestUri)) { return false; } @@ -55,24 +55,30 @@ class FunctionRequestHandler : public RequestHandler { } void upload(WebServer &server, String requestUri, HTTPUpload &upload) override { - (void)server; (void)upload; - if (canUpload(requestUri)) { + if (canUpload(server, requestUri)) { _ufn(); } } void raw(WebServer &server, String requestUri, HTTPRaw &raw) override { - (void)server; (void)raw; - if (canRaw(requestUri)) { + if (canRaw(server, requestUri)) { _ufn(); } } + FunctionRequestHandler& setFilter(WebServer::FilterFunction filter) { + _filter = filter; + return *this; + } + protected: WebServer::THandlerFunction _fn; WebServer::THandlerFunction _ufn; + // _filter should return 'true' when the request should be handled + // and 'false' when the request should be ignored + WebServer::FilterFunction _filter; Uri *_uri; HTTPMethod _method; }; @@ -88,7 +94,7 @@ class StaticRequestHandler : public RequestHandler { _baseUriLength = _uri.length(); } - bool canHandle(HTTPMethod requestMethod, String requestUri) override { + bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override { if (requestMethod != HTTP_GET) { return false; } @@ -97,11 +103,15 @@ class StaticRequestHandler : public RequestHandler { return false; } + if (_filter != NULL ? _filter(server) == false : false) { + return false; + } + return true; } bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override { - if (!canHandle(requestMethod, requestUri)) { + if (!canHandle(server, requestMethod, requestUri)) { return false; } @@ -198,6 +208,7 @@ class StaticRequestHandler : public RequestHandler { } // calcETag protected: + WebServer::FilterFunction _filter; FS _fs; String _uri; String _path; From 51bdde4a778edaddfe8f85d126628e2f7488b670 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 12 Jun 2024 22:17:27 +0530 Subject: [PATCH 02/10] feat: add setFilter function in StaticRequestHandler --- libraries/WebServer/src/detail/RequestHandlersImpl.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libraries/WebServer/src/detail/RequestHandlersImpl.h b/libraries/WebServer/src/detail/RequestHandlersImpl.h index d56262ba04a..882f020c8cc 100644 --- a/libraries/WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/WebServer/src/detail/RequestHandlersImpl.h @@ -207,7 +207,14 @@ class StaticRequestHandler : public RequestHandler { return (result); } // calcETag + StaticRequestHandler& setFilter(WebServer::FilterFunction filter) { + _filter = filter; + return *this; + } + protected: + // _filter should return 'true' when the request should be handled + // and 'false' when the request should be ignored WebServer::FilterFunction _filter; FS _fs; String _uri; From 229d693932cf460091d7cec69479dd23ef95ba37 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 12 Jun 2024 23:58:34 +0530 Subject: [PATCH 03/10] fix: ON_STA_FILTER & ON_AP_FILTER --- libraries/WebServer/src/WebServer.cpp | 22 +++++++++++++--------- libraries/WebServer/src/WebServer.h | 1 - 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index a291ff527fd..9ac58308409 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -21,6 +21,11 @@ */ #include + +#if SOC_WIFI_SUPPORTED + #include "WiFi.h" +#endif + #include #include #include @@ -799,13 +804,12 @@ String WebServer::_responseCodeToString(int code) { } } -bool ON_STA_FILTER(WebServer &server) { - return ( - (WiFi.localIP() != IPAddress(0, 0, 0, 0) && server.client().localIP() != IPAddress(0, 0, 0, 0)) - && WiFi.localIP() == server.client().localIP() - ); -} +#if SOC_WIFI_SUPPORTED + bool ON_STA_FILTER(WebServer &server) { + return WiFi.STA.localIP() == server.client().localIP(); + } -bool ON_AP_FILTER(WebServer &server) { - return WiFi.localIP() == server.client().localIP(); -} + bool ON_AP_FILTER(WebServer &server) { + return WiFi.AP.localIP() == server.client().localIP(); + } +#endif diff --git a/libraries/WebServer/src/WebServer.h b/libraries/WebServer/src/WebServer.h index 531236acf62..c8b3df10ebd 100644 --- a/libraries/WebServer/src/WebServer.h +++ b/libraries/WebServer/src/WebServer.h @@ -26,7 +26,6 @@ #include #include #include "FS.h" -#include "WiFi.h" #include "Network.h" #include "HTTP_Method.h" #include "Uri.h" From ebd832c89f5661c682039f6420c50acaa8cebd75 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 01:24:30 +0530 Subject: [PATCH 04/10] fix: make request handlers backward compatible --- .../WebServer/src/detail/RequestHandler.h | 23 ++++++++++++ .../src/detail/RequestHandlersImpl.h | 36 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/libraries/WebServer/src/detail/RequestHandler.h b/libraries/WebServer/src/detail/RequestHandler.h index e80cd407ce6..d4a28cce0a3 100644 --- a/libraries/WebServer/src/detail/RequestHandler.h +++ b/libraries/WebServer/src/detail/RequestHandler.h @@ -7,6 +7,29 @@ class RequestHandler { public: virtual ~RequestHandler() {} + + /* + note: old handler API for backward compatibility + */ + + virtual bool canHandle(HTTPMethod method, String uri) { + (void)method; + (void)uri; + return false; + } + virtual bool canUpload(String uri) { + (void)uri; + return false; + } + virtual bool canRaw(String uri) { + (void)uri; + return false; + } + + /* + note: new handler API with support for filters etc. + */ + virtual bool canHandle(WebServer &server, HTTPMethod method, String uri) { (void)server; (void)method; diff --git a/libraries/WebServer/src/detail/RequestHandlersImpl.h b/libraries/WebServer/src/detail/RequestHandlersImpl.h index 882f020c8cc..684455393ca 100644 --- a/libraries/WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/WebServer/src/detail/RequestHandlersImpl.h @@ -21,6 +21,30 @@ class FunctionRequestHandler : public RequestHandler { delete _uri; } + bool canHandle(HTTPMethod requestMethod, String requestUri) override { + if (_method != HTTP_ANY && _method != requestMethod) { + return false; + } + + return _uri->canHandle(requestUri, pathArgs); + } + + bool canUpload(String requestUri) override { + if (!_ufn || !canHandle(HTTP_POST, requestUri)) { + return false; + } + + return true; + } + + bool canRaw(String requestUri) override { + if (!_ufn || _method == HTTP_GET) { + return false; + } + + return true; + } + bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override { if (_method != HTTP_ANY && _method != requestMethod) { return false; @@ -94,6 +118,18 @@ class StaticRequestHandler : public RequestHandler { _baseUriLength = _uri.length(); } + bool canHandle(HTTPMethod requestMethod, String requestUri) override { + if (requestMethod != HTTP_GET) { + return false; + } + + if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri)) { + return false; + } + + return true; + } + bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override { if (requestMethod != HTTP_GET) { return false; From e92f4e640c4aebb47235f5d290e39ea283957b5b Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 01:36:06 +0530 Subject: [PATCH 05/10] fix: ON_STA_FILTER & ON_AP_FILTER --- libraries/WebServer/src/WebServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index 9ac58308409..dc83e43d32a 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -806,10 +806,10 @@ String WebServer::_responseCodeToString(int code) { #if SOC_WIFI_SUPPORTED bool ON_STA_FILTER(WebServer &server) { - return WiFi.STA.localIP() == server.client().localIP(); + return WiFi.STA.hasIP() && WiFi.STA.localIP() == server.client().localIP(); } bool ON_AP_FILTER(WebServer &server) { - return WiFi.AP.localIP() == server.client().localIP(); + return WiFi.AP.hasIP() && WiFi.AP.localIP() == server.client().localIP(); } #endif From 686b9778d7d35e9b0a497471b8743b5d6a573336 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 03:13:45 +0530 Subject: [PATCH 06/10] fix: more filters to their own example --- .../WebServer/examples/Filters/Filters.ino | 100 ++++++++++++++++++ libraries/WebServer/examples/Filters/ci.json | 5 + libraries/WebServer/src/WebServer.cpp | 15 --- 3 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 libraries/WebServer/examples/Filters/Filters.ino create mode 100644 libraries/WebServer/examples/Filters/ci.json diff --git a/libraries/WebServer/examples/Filters/Filters.ino b/libraries/WebServer/examples/Filters/Filters.ino new file mode 100644 index 00000000000..2631d044d82 --- /dev/null +++ b/libraries/WebServer/examples/Filters/Filters.ino @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +// Your STA WiFi Credentials +// ( This is the AP your ESP will connect to ) +const char *ssid = "........"; +const char *password = "........"; + +// Your AP WiFi Credentials +// ( This is the AP your ESP will broadcast ) +const char *ap_ssid = "ESP32_Demo"; +const char *ap_password = ""; + +WebServer server(80); + +const int led = 13; + +// ON_STA_FILTER - Accept requests coming only from the STA interface +bool ON_STA_FILTER(WebServer &server) { + return WiFi.STA.hasIP() && WiFi.STA.localIP() == server.client().localIP(); +} + +// ON_AP_FILTER - Accept requests coming only from the AP interface +bool ON_AP_FILTER(WebServer &server) { + return WiFi.AP.hasIP() && WiFi.AP.localIP() == server.client().localIP(); +} + +void handleNotFound() { + digitalWrite(led, 1); + 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); + digitalWrite(led, 0); +} + +void setup(void) { + pinMode(led, OUTPUT); + digitalWrite(led, 0); + Serial.begin(115200); + WiFi.mode(WIFI_AP_STA); + // Connect to STA + WiFi.begin(ssid, password); + // Start AP + WiFi.softAP(ap_ssid, ap_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()); + + if (MDNS.begin("esp32")) { + Serial.println("MDNS responder started"); + } + + // This route will be accessible by STA clients only + server.on("/sta", [&]() { + digitalWrite(led, 1); + server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only"); + digitalWrite(led, 0); + }).setFilter(ON_STA_FILTER); + + // This route will be accessible by AP clients only + server.on("/ap", [&]() { + digitalWrite(led, 1); + server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only"); + digitalWrite(led, 0); + }).setFilter(ON_AP_FILTER); + + server.on("/inline", []() { + server.send(200, "text/plain", "this works as well"); + }); + + server.onNotFound(handleNotFound); + + server.begin(); + Serial.println("HTTP server started"); +} + +void loop(void) { + server.handleClient(); + delay(2); //allow the cpu to switch to other tasks +} diff --git a/libraries/WebServer/examples/Filters/ci.json b/libraries/WebServer/examples/Filters/ci.json new file mode 100644 index 00000000000..d8b3664bc65 --- /dev/null +++ b/libraries/WebServer/examples/Filters/ci.json @@ -0,0 +1,5 @@ +{ + "targets": { + "esp32h2": false + } +} diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index dc83e43d32a..ad1764f0660 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -21,11 +21,6 @@ */ #include - -#if SOC_WIFI_SUPPORTED - #include "WiFi.h" -#endif - #include #include #include @@ -803,13 +798,3 @@ String WebServer::_responseCodeToString(int code) { default: return F(""); } } - -#if SOC_WIFI_SUPPORTED - bool ON_STA_FILTER(WebServer &server) { - return WiFi.STA.hasIP() && WiFi.STA.localIP() == server.client().localIP(); - } - - bool ON_AP_FILTER(WebServer &server) { - return WiFi.AP.hasIP() && WiFi.AP.localIP() == server.client().localIP(); - } -#endif From 26f7feb015599a9fe83a73bed08bb308697a0893 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 03:19:37 +0530 Subject: [PATCH 07/10] chore: grammar --- libraries/WebServer/examples/Filters/Filters.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/WebServer/examples/Filters/Filters.ino b/libraries/WebServer/examples/Filters/Filters.ino index 2631d044d82..eb65438d032 100644 --- a/libraries/WebServer/examples/Filters/Filters.ino +++ b/libraries/WebServer/examples/Filters/Filters.ino @@ -17,12 +17,12 @@ WebServer server(80); const int led = 13; -// ON_STA_FILTER - Accept requests coming only from the STA interface +// ON_STA_FILTER - Only accept requests coming from STA interface bool ON_STA_FILTER(WebServer &server) { return WiFi.STA.hasIP() && WiFi.STA.localIP() == server.client().localIP(); } -// ON_AP_FILTER - Accept requests coming only from the AP interface +// ON_AP_FILTER - Only accept requests coming from AP interface bool ON_AP_FILTER(WebServer &server) { return WiFi.AP.hasIP() && WiFi.AP.localIP() == server.client().localIP(); } From 3dec34e9ac889002d3a80b10f4bac67f3c8a5f52 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 12:51:08 +0530 Subject: [PATCH 08/10] fix: remove filters from header file --- libraries/WebServer/src/WebServer.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/libraries/WebServer/src/WebServer.h b/libraries/WebServer/src/WebServer.h index c8b3df10ebd..f58fe68b4fb 100644 --- a/libraries/WebServer/src/WebServer.h +++ b/libraries/WebServer/src/WebServer.h @@ -293,12 +293,4 @@ class WebServer { String _srealm; // Store the Auth realm between Calls }; -/* - Request Filters -*/ - -bool ON_STA_FILTER(WebServer &server); - -bool ON_AP_FILTER(WebServer &server); - #endif //ESP8266WEBSERVER_H From 1f498a12ecba76e03ad8a6bbd7e3a88e5e747bf2 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 13 Jun 2024 13:07:58 +0530 Subject: [PATCH 09/10] fix: use same root route for both interfaces --- libraries/WebServer/examples/Filters/Filters.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/WebServer/examples/Filters/Filters.ino b/libraries/WebServer/examples/Filters/Filters.ino index eb65438d032..220a1088939 100644 --- a/libraries/WebServer/examples/Filters/Filters.ino +++ b/libraries/WebServer/examples/Filters/Filters.ino @@ -71,14 +71,14 @@ void setup(void) { } // This route will be accessible by STA clients only - server.on("/sta", [&]() { + server.on("/", [&]() { digitalWrite(led, 1); server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only"); digitalWrite(led, 0); }).setFilter(ON_STA_FILTER); // This route will be accessible by AP clients only - server.on("/ap", [&]() { + server.on("/", [&]() { digitalWrite(led, 1); server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only"); digitalWrite(led, 0); From 7f11be6d135309ba4ed36e55cebcc72a00d9d90a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 13 Jun 2024 19:25:51 +0000 Subject: [PATCH 10/10] ci(pre-commit): Apply automatic fixes --- .../WebServer/examples/Filters/Filters.ino | 30 ++++++++++++------- libraries/WebServer/src/WebServer.cpp | 6 ++-- libraries/WebServer/src/WebServer.h | 6 ++-- .../WebServer/src/detail/RequestHandler.h | 2 +- .../src/detail/RequestHandlersImpl.h | 4 +-- 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/libraries/WebServer/examples/Filters/Filters.ino b/libraries/WebServer/examples/Filters/Filters.ino index 220a1088939..8974e55d322 100644 --- a/libraries/WebServer/examples/Filters/Filters.ino +++ b/libraries/WebServer/examples/Filters/Filters.ino @@ -71,18 +71,28 @@ void setup(void) { } // This route will be accessible by STA clients only - server.on("/", [&]() { - digitalWrite(led, 1); - server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only"); - digitalWrite(led, 0); - }).setFilter(ON_STA_FILTER); + server + .on( + "/", + [&]() { + digitalWrite(led, 1); + server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only"); + digitalWrite(led, 0); + } + ) + .setFilter(ON_STA_FILTER); // This route will be accessible by AP clients only - server.on("/", [&]() { - digitalWrite(led, 1); - server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only"); - digitalWrite(led, 0); - }).setFilter(ON_AP_FILTER); + server + .on( + "/", + [&]() { + digitalWrite(led, 1); + server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only"); + digitalWrite(led, 0); + } + ) + .setFilter(ON_AP_FILTER); server.on("/inline", []() { server.send(200, "text/plain", "this works as well"); diff --git a/libraries/WebServer/src/WebServer.cpp b/libraries/WebServer/src/WebServer.cpp index 96062b710ba..048bd529d7b 100644 --- a/libraries/WebServer/src/WebServer.cpp +++ b/libraries/WebServer/src/WebServer.cpp @@ -306,15 +306,15 @@ void WebServer::requestAuthentication(HTTPAuthMethod mode, const char *realm, co send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg); } -RequestHandler& WebServer::on(const Uri &uri, WebServer::THandlerFunction handler) { +RequestHandler &WebServer::on(const Uri &uri, WebServer::THandlerFunction handler) { return on(uri, HTTP_ANY, handler); } -RequestHandler& WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn) { +RequestHandler &WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn) { return on(uri, method, fn, _fileUploadHandler); } -RequestHandler& WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn) { +RequestHandler &WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn) { FunctionRequestHandler *handler = new FunctionRequestHandler(fn, ufn, uri, method); _addRequestHandler(handler); return *handler; diff --git a/libraries/WebServer/src/WebServer.h b/libraries/WebServer/src/WebServer.h index 232d18f5014..c43dd4542ea 100644 --- a/libraries/WebServer/src/WebServer.h +++ b/libraries/WebServer/src/WebServer.h @@ -145,9 +145,9 @@ class WebServer { typedef std::function THandlerFunction; typedef std::function FilterFunction; - RequestHandler& on(const Uri &uri, THandlerFunction fn); - RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn); - RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads + RequestHandler &on(const Uri &uri, THandlerFunction fn); + RequestHandler &on(const Uri &uri, HTTPMethod method, THandlerFunction fn); + RequestHandler &on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads bool removeRoute(const char *uri); bool removeRoute(const char *uri, HTTPMethod method); bool removeRoute(const String &uri); diff --git a/libraries/WebServer/src/detail/RequestHandler.h b/libraries/WebServer/src/detail/RequestHandler.h index d4a28cce0a3..f19e7ab4613 100644 --- a/libraries/WebServer/src/detail/RequestHandler.h +++ b/libraries/WebServer/src/detail/RequestHandler.h @@ -63,7 +63,7 @@ class RequestHandler { (void)raw; } - virtual RequestHandler& setFilter(std::function filter) { + virtual RequestHandler &setFilter(std::function filter) { (void)filter; return *this; } diff --git a/libraries/WebServer/src/detail/RequestHandlersImpl.h b/libraries/WebServer/src/detail/RequestHandlersImpl.h index 684455393ca..b6eae6adea0 100644 --- a/libraries/WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/WebServer/src/detail/RequestHandlersImpl.h @@ -92,7 +92,7 @@ class FunctionRequestHandler : public RequestHandler { } } - FunctionRequestHandler& setFilter(WebServer::FilterFunction filter) { + FunctionRequestHandler &setFilter(WebServer::FilterFunction filter) { _filter = filter; return *this; } @@ -243,7 +243,7 @@ class StaticRequestHandler : public RequestHandler { return (result); } // calcETag - StaticRequestHandler& setFilter(WebServer::FilterFunction filter) { + StaticRequestHandler &setFilter(WebServer::FilterFunction filter) { _filter = filter; return *this; }