Skip to content

Commit 1f8c14d

Browse files
committed
Fix ESP8266WebServer::serveStatic to work for both files and directories
1 parent 75a2928 commit 1f8c14d

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ ESP8266WebServer::ESP8266WebServer(int port)
4040
{
4141
}
4242

43-
ESP8266WebServer::~ESP8266WebServer()
44-
{
43+
ESP8266WebServer::~ESP8266WebServer() {
4544
if (!_firstHandler)
4645
return;
4746
RequestHandler* handler = _firstHandler;
@@ -56,14 +55,11 @@ void ESP8266WebServer::begin() {
5655
_server.begin();
5756
}
5857

59-
60-
void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler)
61-
{
58+
void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler) {
6259
on(uri, HTTP_ANY, handler);
6360
}
6461

65-
void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn)
66-
{
62+
void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn) {
6763
_addRequestHandler(new FunctionRequestHandler(fn, uri, method));
6864
}
6965

@@ -79,11 +75,10 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) {
7975
}
8076

8177
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) {
82-
_addRequestHandler(new StaticRequestHandler(fs, uri));
78+
_addRequestHandler(new StaticRequestHandler(fs, path, uri));
8379
}
8480

85-
void ESP8266WebServer::handleClient()
86-
{
81+
void ESP8266WebServer::handleClient() {
8782
WiFiClient client = _server.available();
8883
if (!client) {
8984
return;

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/src/detail/RequestHandler.h

+34-19
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
class RequestHandler {
55
public:
66
RequestHandler(const char* uri, HTTPMethod method)
7-
: uri(uri)
8-
, method(method)
7+
: _uri(uri)
8+
, _method(method)
99
, next(NULL)
1010
{
1111
}
@@ -15,8 +15,8 @@ class RequestHandler {
1515
RequestHandler* next;
1616

1717
protected:
18-
String uri;
19-
HTTPMethod method;
18+
String _uri;
19+
HTTPMethod _method;
2020
};
2121

2222

@@ -25,47 +25,59 @@ class FunctionRequestHandler : public RequestHandler {
2525

2626
public:
2727
FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, const char* uri, HTTPMethod method)
28-
: fn(fn)
28+
: _fn(fn)
2929
, base(uri, method)
3030
{
3131
}
3232

3333
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
34-
if (method != HTTP_ANY && method != requestMethod)
34+
if (_method != HTTP_ANY && _method != requestMethod)
3535
return false;
3636

37-
if (requestUri != uri)
37+
if (requestUri != _uri)
3838
return false;
3939

40-
fn();
40+
_fn();
4141
return true;
4242
}
4343

4444
protected:
45-
ESP8266WebServer::THandlerFunction fn;
45+
ESP8266WebServer::THandlerFunction _fn;
4646
};
4747

4848
class StaticRequestHandler : public RequestHandler {
4949
typedef RequestHandler base;
5050

5151
public:
52-
StaticRequestHandler(FS& fs, const char* uri)
53-
: fs(fs)
52+
StaticRequestHandler(FS& fs, const char* path, const char* uri)
53+
: _fs(fs)
5454
, base(uri, HTTP_GET)
55+
, _path(path)
5556
{
57+
_isFile = fs.exists(path);
58+
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile);
59+
_baseUriLength = _uri.length();
5660
}
5761

5862
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
59-
if (requestMethod != method)
63+
if (requestMethod != _method)
6064
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))
6367
return false;
6468

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");
6981
if (!f)
7082
return false;
7183

@@ -90,7 +102,10 @@ class StaticRequestHandler : public RequestHandler {
90102
}
91103

92104
protected:
93-
FS fs;
105+
FS _fs;
106+
String _path;
107+
bool _isFile;
108+
size_t _baseUriLength;
94109
};
95110

96111
#endif //REQUESTHANDLER_H

0 commit comments

Comments
 (0)