Skip to content

Commit 8dbf85e

Browse files
committed
feat: added support for filters in webserver
1 parent e382746 commit 8dbf85e

File tree

5 files changed

+75
-33
lines changed

5 files changed

+75
-33
lines changed

libraries/WebServer/src/Parsing.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bool WebServer::_parseRequest(NetworkClient &client) {
124124
//attach handler
125125
RequestHandler *handler;
126126
for (handler = _firstHandler; handler; handler = handler->next()) {
127-
if (handler->canHandle(_currentMethod, _currentUri)) {
127+
if (handler->canHandle(*this, _currentMethod, _currentUri)) {
128128
break;
129129
}
130130
}
@@ -176,7 +176,7 @@ bool WebServer::_parseRequest(NetworkClient &client) {
176176
}
177177
}
178178

179-
if (!isForm && _currentHandler && _currentHandler->canRaw(_currentUri)) {
179+
if (!isForm && _currentHandler && _currentHandler->canRaw(*this, _currentUri)) {
180180
log_v("Parse raw");
181181
_currentRaw.reset(new HTTPRaw());
182182
_currentRaw->status = RAW_START;
@@ -334,7 +334,7 @@ void WebServer::_parseArguments(String data) {
334334

335335
void WebServer::_uploadWriteByte(uint8_t b) {
336336
if (_currentUpload->currentSize == HTTP_UPLOAD_BUFLEN) {
337-
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
337+
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
338338
_currentHandler->upload(*this, _currentUri, *_currentUpload);
339339
}
340340
_currentUpload->totalSize += _currentUpload->currentSize;
@@ -449,7 +449,7 @@ bool WebServer::_parseForm(NetworkClient &client, String boundary, uint32_t len)
449449
_currentUpload->totalSize = 0;
450450
_currentUpload->currentSize = 0;
451451
log_v("Start File: %s Type: %s", _currentUpload->filename.c_str(), _currentUpload->type.c_str());
452-
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
452+
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
453453
_currentHandler->upload(*this, _currentUri, *_currentUpload);
454454
}
455455
_currentUpload->status = UPLOAD_FILE_WRITE;
@@ -488,12 +488,12 @@ bool WebServer::_parseForm(NetworkClient &client, String boundary, uint32_t len)
488488
}
489489
}
490490
// Found the boundary string, finish processing this file upload
491-
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
491+
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
492492
_currentHandler->upload(*this, _currentUri, *_currentUpload);
493493
}
494494
_currentUpload->totalSize += _currentUpload->currentSize;
495495
_currentUpload->status = UPLOAD_FILE_END;
496-
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
496+
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
497497
_currentHandler->upload(*this, _currentUri, *_currentUpload);
498498
}
499499
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) {
567567

568568
bool WebServer::_parseFormUploadAborted() {
569569
_currentUpload->status = UPLOAD_FILE_ABORTED;
570-
if (_currentHandler && _currentHandler->canUpload(_currentUri)) {
570+
if (_currentHandler && _currentHandler->canUpload(*this, _currentUri)) {
571571
_currentHandler->upload(*this, _currentUri, *_currentUpload);
572572
}
573573
return false;

libraries/WebServer/src/WebServer.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,18 @@ void WebServer::requestAuthentication(HTTPAuthMethod mode, const char *realm, co
306306
send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg);
307307
}
308308

309-
void WebServer::on(const Uri &uri, WebServer::THandlerFunction handler) {
310-
on(uri, HTTP_ANY, handler);
309+
RequestHandler& WebServer::on(const Uri &uri, WebServer::THandlerFunction handler) {
310+
return on(uri, HTTP_ANY, handler);
311311
}
312312

313-
void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn) {
314-
on(uri, method, fn, _fileUploadHandler);
313+
RequestHandler& WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn) {
314+
return on(uri, method, fn, _fileUploadHandler);
315315
}
316316

317-
void WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn) {
318-
_addRequestHandler(new FunctionRequestHandler(fn, ufn, uri, method));
317+
RequestHandler& WebServer::on(const Uri &uri, HTTPMethod method, WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn) {
318+
FunctionRequestHandler *handler = new FunctionRequestHandler(fn, ufn, uri, method);
319+
_addRequestHandler(handler);
320+
return *handler;
319321
}
320322

321323
void WebServer::addHandler(RequestHandler *handler) {
@@ -796,3 +798,14 @@ String WebServer::_responseCodeToString(int code) {
796798
default: return F("");
797799
}
798800
}
801+
802+
bool ON_STA_FILTER(WebServer &server) {
803+
return (
804+
(WiFi.localIP() != IPAddress(0, 0, 0, 0) && server.client().localIP() != IPAddress(0, 0, 0, 0))
805+
&& WiFi.localIP() == server.client().localIP()
806+
);
807+
}
808+
809+
bool ON_AP_FILTER(WebServer &server) {
810+
return WiFi.localIP() == server.client().localIP();
811+
}

libraries/WebServer/src/WebServer.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <functional>
2727
#include <memory>
2828
#include "FS.h"
29+
#include "WiFi.h"
2930
#include "Network.h"
3031
#include "HTTP_Method.h"
3132
#include "Uri.h"
@@ -144,9 +145,10 @@ class WebServer {
144145
void requestAuthentication(HTTPAuthMethod mode = BASIC_AUTH, const char *realm = NULL, const String &authFailMsg = String(""));
145146

146147
typedef std::function<void(void)> THandlerFunction;
147-
void on(const Uri &uri, THandlerFunction fn);
148-
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn);
149-
void on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads
148+
typedef std::function<bool(WebServer &server)> FilterFunction;
149+
RequestHandler& on(const Uri &uri, THandlerFunction fn);
150+
RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn);
151+
RequestHandler& on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn); //ufn handles file uploads
150152
void addHandler(RequestHandler *handler);
151153
void serveStatic(const char *uri, fs::FS &fs, const char *path, const char *cache_header = NULL);
152154
void onNotFound(THandlerFunction fn); //called when handler is not assigned
@@ -292,4 +294,12 @@ class WebServer {
292294
String _srealm; // Store the Auth realm between Calls
293295
};
294296

297+
/*
298+
Request Filters
299+
*/
300+
301+
bool ON_STA_FILTER(WebServer &server);
302+
303+
bool ON_AP_FILTER(WebServer &server);
304+
295305
#endif //ESP8266WEBSERVER_H

libraries/WebServer/src/detail/RequestHandler.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
class RequestHandler {
88
public:
99
virtual ~RequestHandler() {}
10-
virtual bool canHandle(HTTPMethod method, String uri) {
10+
virtual bool canHandle(WebServer &server, HTTPMethod method, String uri) {
11+
(void)server;
1112
(void)method;
1213
(void)uri;
1314
return false;
1415
}
15-
virtual bool canUpload(String uri) {
16+
virtual bool canUpload(WebServer &server, String uri) {
17+
(void)server;
1618
(void)uri;
1719
return false;
1820
}
19-
virtual bool canRaw(String uri) {
21+
virtual bool canRaw(WebServer &server, String uri) {
22+
(void)server;
2023
(void)uri;
2124
return false;
2225
}
@@ -37,6 +40,11 @@ class RequestHandler {
3740
(void)raw;
3841
}
3942

43+
virtual RequestHandler& setFilter(std::function<bool(WebServer&)> filter) {
44+
(void)filter;
45+
return *this;
46+
}
47+
4048
RequestHandler *next() {
4149
return _next;
4250
}

libraries/WebServer/src/detail/RequestHandlersImpl.h

+25-14
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,32 @@ class FunctionRequestHandler : public RequestHandler {
2121
delete _uri;
2222
}
2323

24-
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
24+
bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
2525
if (_method != HTTP_ANY && _method != requestMethod) {
2626
return false;
2727
}
2828

29-
return _uri->canHandle(requestUri, pathArgs);
29+
return _uri->canHandle(requestUri, pathArgs) && (_filter != NULL ? _filter(server) : true);
3030
}
3131

32-
bool canUpload(String requestUri) override {
33-
if (!_ufn || !canHandle(HTTP_POST, requestUri)) {
32+
bool canUpload(WebServer &server, String requestUri) override {
33+
if (!_ufn || !canHandle(server, HTTP_POST, requestUri)) {
3434
return false;
3535
}
3636

3737
return true;
3838
}
39-
bool canRaw(String requestUri) override {
40-
if (!_ufn || _method == HTTP_GET) {
39+
40+
bool canRaw(WebServer &server, String requestUri) override {
41+
if (!_ufn || _method == HTTP_GET || (_filter != NULL ? _filter(server) == false : false)) {
4142
return false;
4243
}
4344

4445
return true;
4546
}
4647

4748
bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
48-
(void)server;
49-
if (!canHandle(requestMethod, requestUri)) {
49+
if (!canHandle(server, requestMethod, requestUri)) {
5050
return false;
5151
}
5252

@@ -55,24 +55,30 @@ class FunctionRequestHandler : public RequestHandler {
5555
}
5656

5757
void upload(WebServer &server, String requestUri, HTTPUpload &upload) override {
58-
(void)server;
5958
(void)upload;
60-
if (canUpload(requestUri)) {
59+
if (canUpload(server, requestUri)) {
6160
_ufn();
6261
}
6362
}
6463

6564
void raw(WebServer &server, String requestUri, HTTPRaw &raw) override {
66-
(void)server;
6765
(void)raw;
68-
if (canRaw(requestUri)) {
66+
if (canRaw(server, requestUri)) {
6967
_ufn();
7068
}
7169
}
7270

71+
FunctionRequestHandler& setFilter(WebServer::FilterFunction filter) {
72+
_filter = filter;
73+
return *this;
74+
}
75+
7376
protected:
7477
WebServer::THandlerFunction _fn;
7578
WebServer::THandlerFunction _ufn;
79+
// _filter should return 'true' when the request should be handled
80+
// and 'false' when the request should be ignored
81+
WebServer::FilterFunction _filter;
7682
Uri *_uri;
7783
HTTPMethod _method;
7884
};
@@ -88,7 +94,7 @@ class StaticRequestHandler : public RequestHandler {
8894
_baseUriLength = _uri.length();
8995
}
9096

91-
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
97+
bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
9298
if (requestMethod != HTTP_GET) {
9399
return false;
94100
}
@@ -97,11 +103,15 @@ class StaticRequestHandler : public RequestHandler {
97103
return false;
98104
}
99105

106+
if (_filter != NULL ? _filter(server) == false : false) {
107+
return false;
108+
}
109+
100110
return true;
101111
}
102112

103113
bool handle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
104-
if (!canHandle(requestMethod, requestUri)) {
114+
if (!canHandle(server, requestMethod, requestUri)) {
105115
return false;
106116
}
107117

@@ -198,6 +208,7 @@ class StaticRequestHandler : public RequestHandler {
198208
} // calcETag
199209

200210
protected:
211+
WebServer::FilterFunction _filter;
201212
FS _fs;
202213
String _uri;
203214
String _path;

0 commit comments

Comments
 (0)