mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
Fix ESP8266WebServer::serveStatic to work for both files and directories
This commit is contained in:
parent
c8b12fd72c
commit
e62d5a92b9
@ -40,8 +40,7 @@ ESP8266WebServer::ESP8266WebServer(int port)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP8266WebServer::~ESP8266WebServer()
|
ESP8266WebServer::~ESP8266WebServer() {
|
||||||
{
|
|
||||||
if (!_firstHandler)
|
if (!_firstHandler)
|
||||||
return;
|
return;
|
||||||
RequestHandler* handler = _firstHandler;
|
RequestHandler* handler = _firstHandler;
|
||||||
@ -56,14 +55,11 @@ void ESP8266WebServer::begin() {
|
|||||||
_server.begin();
|
_server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler) {
|
||||||
void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler)
|
|
||||||
{
|
|
||||||
on(uri, HTTP_ANY, handler);
|
on(uri, HTTP_ANY, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn)
|
void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn) {
|
||||||
{
|
|
||||||
_addRequestHandler(new FunctionRequestHandler(fn, uri, method));
|
_addRequestHandler(new FunctionRequestHandler(fn, uri, method));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,11 +75,10 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) {
|
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) {
|
||||||
_addRequestHandler(new StaticRequestHandler(fs, uri));
|
_addRequestHandler(new StaticRequestHandler(fs, path, uri));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESP8266WebServer::handleClient()
|
void ESP8266WebServer::handleClient() {
|
||||||
{
|
|
||||||
WiFiClient client = _server.available();
|
WiFiClient client = _server.available();
|
||||||
if (!client) {
|
if (!client) {
|
||||||
return;
|
return;
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
class RequestHandler {
|
class RequestHandler {
|
||||||
public:
|
public:
|
||||||
RequestHandler(const char* uri, HTTPMethod method)
|
RequestHandler(const char* uri, HTTPMethod method)
|
||||||
: uri(uri)
|
: _uri(uri)
|
||||||
, method(method)
|
, _method(method)
|
||||||
, next(NULL)
|
, next(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -15,8 +15,8 @@ public:
|
|||||||
RequestHandler* next;
|
RequestHandler* next;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
String uri;
|
String _uri;
|
||||||
HTTPMethod method;
|
HTTPMethod _method;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -25,47 +25,59 @@ class FunctionRequestHandler : public RequestHandler {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, const char* uri, HTTPMethod method)
|
FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, const char* uri, HTTPMethod method)
|
||||||
: fn(fn)
|
: _fn(fn)
|
||||||
, base(uri, method)
|
, base(uri, method)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
|
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
|
||||||
if (method != HTTP_ANY && method != requestMethod)
|
if (_method != HTTP_ANY && _method != requestMethod)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (requestUri != uri)
|
if (requestUri != _uri)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fn();
|
_fn();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
ESP8266WebServer::THandlerFunction fn;
|
ESP8266WebServer::THandlerFunction _fn;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StaticRequestHandler : public RequestHandler {
|
class StaticRequestHandler : public RequestHandler {
|
||||||
typedef RequestHandler base;
|
typedef RequestHandler base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StaticRequestHandler(FS& fs, const char* uri)
|
StaticRequestHandler(FS& fs, const char* path, const char* uri)
|
||||||
: fs(fs)
|
: _fs(fs)
|
||||||
, base(uri, HTTP_GET)
|
, base(uri, HTTP_GET)
|
||||||
|
, _path(path)
|
||||||
{
|
{
|
||||||
|
_isFile = fs.exists(path);
|
||||||
|
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile);
|
||||||
|
_baseUriLength = _uri.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
|
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
|
||||||
if (requestMethod != method)
|
if (requestMethod != _method)
|
||||||
return false;
|
return false;
|
||||||
DEBUGV("StaticRequestHandler::handle: %s\r\n", requestUri.c_str());
|
DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
|
||||||
if (!requestUri.startsWith(uri))
|
if (!requestUri.startsWith(_uri))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto prefixLength = uri.length() - 1;
|
String path(_path);
|
||||||
String path = requestUri.substring(prefixLength);
|
if (!_isFile) {
|
||||||
DEBUGV("StaticRequestHandler::handle: %d %s\r\n", prefixLength, path.c_str());
|
// Base URI doesn't point to a file. Append whatever follows this
|
||||||
File f = fs.open(path, "r");
|
// URI in request to get the file path.
|
||||||
|
path += requestUri.substring(_baseUriLength);
|
||||||
|
}
|
||||||
|
else if (requestUri != _uri) {
|
||||||
|
// Base URI points to a file but request doesn't match this URI exactly
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
|
||||||
|
File f = _fs.open(path, "r");
|
||||||
if (!f)
|
if (!f)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -90,7 +102,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
FS fs;
|
FS _fs;
|
||||||
|
String _path;
|
||||||
|
bool _isFile;
|
||||||
|
size_t _baseUriLength;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //REQUESTHANDLER_H
|
#endif //REQUESTHANDLER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user