Skip to content

Commit ebd832c

Browse files
committed
fix: make request handlers backward compatible
1 parent 229d693 commit ebd832c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

libraries/WebServer/src/detail/RequestHandler.h

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@
77
class RequestHandler {
88
public:
99
virtual ~RequestHandler() {}
10+
11+
/*
12+
note: old handler API for backward compatibility
13+
*/
14+
15+
virtual bool canHandle(HTTPMethod method, String uri) {
16+
(void)method;
17+
(void)uri;
18+
return false;
19+
}
20+
virtual bool canUpload(String uri) {
21+
(void)uri;
22+
return false;
23+
}
24+
virtual bool canRaw(String uri) {
25+
(void)uri;
26+
return false;
27+
}
28+
29+
/*
30+
note: new handler API with support for filters etc.
31+
*/
32+
1033
virtual bool canHandle(WebServer &server, HTTPMethod method, String uri) {
1134
(void)server;
1235
(void)method;

libraries/WebServer/src/detail/RequestHandlersImpl.h

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ class FunctionRequestHandler : public RequestHandler {
2121
delete _uri;
2222
}
2323

24+
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
25+
if (_method != HTTP_ANY && _method != requestMethod) {
26+
return false;
27+
}
28+
29+
return _uri->canHandle(requestUri, pathArgs);
30+
}
31+
32+
bool canUpload(String requestUri) override {
33+
if (!_ufn || !canHandle(HTTP_POST, requestUri)) {
34+
return false;
35+
}
36+
37+
return true;
38+
}
39+
40+
bool canRaw(String requestUri) override {
41+
if (!_ufn || _method == HTTP_GET) {
42+
return false;
43+
}
44+
45+
return true;
46+
}
47+
2448
bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
2549
if (_method != HTTP_ANY && _method != requestMethod) {
2650
return false;
@@ -94,6 +118,18 @@ class StaticRequestHandler : public RequestHandler {
94118
_baseUriLength = _uri.length();
95119
}
96120

121+
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
122+
if (requestMethod != HTTP_GET) {
123+
return false;
124+
}
125+
126+
if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri)) {
127+
return false;
128+
}
129+
130+
return true;
131+
}
132+
97133
bool canHandle(WebServer &server, HTTPMethod requestMethod, String requestUri) override {
98134
if (requestMethod != HTTP_GET) {
99135
return false;

0 commit comments

Comments
 (0)