4
4
class RequestHandler {
5
5
public:
6
6
RequestHandler (const char * uri, HTTPMethod method)
7
- : uri (uri)
8
- , method (method)
7
+ : _uri (uri)
8
+ , _method (method)
9
9
, next(NULL )
10
10
{
11
11
}
@@ -15,8 +15,8 @@ class RequestHandler {
15
15
RequestHandler* next;
16
16
17
17
protected:
18
- String uri ;
19
- HTTPMethod method ;
18
+ String _uri ;
19
+ HTTPMethod _method ;
20
20
};
21
21
22
22
@@ -25,47 +25,59 @@ class FunctionRequestHandler : public RequestHandler {
25
25
26
26
public:
27
27
FunctionRequestHandler (ESP8266WebServer::THandlerFunction fn, const char * uri, HTTPMethod method)
28
- : fn (fn)
28
+ : _fn (fn)
29
29
, base(uri, method)
30
30
{
31
31
}
32
32
33
33
bool handle (ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
34
- if (method != HTTP_ANY && method != requestMethod)
34
+ if (_method != HTTP_ANY && _method != requestMethod)
35
35
return false ;
36
36
37
- if (requestUri != uri )
37
+ if (requestUri != _uri )
38
38
return false ;
39
39
40
- fn ();
40
+ _fn ();
41
41
return true ;
42
42
}
43
43
44
44
protected:
45
- ESP8266WebServer::THandlerFunction fn ;
45
+ ESP8266WebServer::THandlerFunction _fn ;
46
46
};
47
47
48
48
class StaticRequestHandler : public RequestHandler {
49
49
typedef RequestHandler base;
50
50
51
51
public:
52
- StaticRequestHandler (FS& fs, const char * uri)
53
- : fs (fs)
52
+ StaticRequestHandler (FS& fs, const char * path, const char * uri)
53
+ : _fs (fs)
54
54
, base(uri, HTTP_GET)
55
+ , _path(path)
55
56
{
57
+ _isFile = fs.exists (path);
58
+ DEBUGV (" StaticRequestHandler: path=%s uri=%s isFile=%d\r\n " , path, uri, _isFile);
59
+ _baseUriLength = _uri.length ();
56
60
}
57
61
58
62
bool handle (ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
59
- if (requestMethod != method )
63
+ if (requestMethod != _method )
60
64
return false ;
61
- DEBUGV (" StaticRequestHandler::handle: %s \r\n " , requestUri.c_str ());
62
- if (!requestUri.startsWith (uri ))
65
+ DEBUGV (" StaticRequestHandler::handle: request=%s _uri=%s \r\n " , requestUri. c_str (), _uri .c_str ());
66
+ if (!requestUri.startsWith (_uri ))
63
67
return false ;
64
68
65
- auto prefixLength = uri.length () - 1 ;
66
- String path = requestUri.substring (prefixLength);
67
- DEBUGV (" StaticRequestHandler::handle: %d %s\r\n " , prefixLength, path.c_str ());
68
- File f = fs.open (path, " r" );
69
+ String path (_path);
70
+ if (!_isFile) {
71
+ // Base URI doesn't point to a file. Append whatever follows this
72
+ // URI in request to get the file path.
73
+ path += requestUri.substring (_baseUriLength);
74
+ }
75
+ else if (requestUri != _uri) {
76
+ // Base URI points to a file but request doesn't match this URI exactly
77
+ return false ;
78
+ }
79
+ DEBUGV (" StaticRequestHandler::handle: path=%s, isFile=%d\r\n " , path.c_str (), _isFile);
80
+ File f = _fs.open (path, " r" );
69
81
if (!f)
70
82
return false ;
71
83
@@ -90,7 +102,10 @@ class StaticRequestHandler : public RequestHandler {
90
102
}
91
103
92
104
protected:
93
- FS fs;
105
+ FS _fs;
106
+ String _path;
107
+ bool _isFile;
108
+ size_t _baseUriLength;
94
109
};
95
110
96
111
#endif // REQUESTHANDLER_H
0 commit comments