From e62d5a92b981392d6f65fcec4d27a78b80c5bc1d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 14 Sep 2015 12:47:39 +0300 Subject: [PATCH] Fix ESP8266WebServer::serveStatic to work for both files and directories --- .../ESP8266WebServer/src/ESP8266WebServer.cpp | 15 ++---- .../src/detail/RequestHandler.h | 53 ++++++++++++------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index 3af856184..75474ae70 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -40,8 +40,7 @@ ESP8266WebServer::ESP8266WebServer(int port) { } -ESP8266WebServer::~ESP8266WebServer() -{ +ESP8266WebServer::~ESP8266WebServer() { if (!_firstHandler) return; RequestHandler* handler = _firstHandler; @@ -56,14 +55,11 @@ void ESP8266WebServer::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); } -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)); } @@ -79,11 +75,10 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) { } 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(); if (!client) { return; diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandler.h b/libraries/ESP8266WebServer/src/detail/RequestHandler.h index 702e24e0b..8cb4e61e1 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandler.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandler.h @@ -4,8 +4,8 @@ class RequestHandler { public: RequestHandler(const char* uri, HTTPMethod method) - : uri(uri) - , method(method) + : _uri(uri) + , _method(method) , next(NULL) { } @@ -15,8 +15,8 @@ public: RequestHandler* next; protected: - String uri; - HTTPMethod method; + String _uri; + HTTPMethod _method; }; @@ -25,47 +25,59 @@ class FunctionRequestHandler : public RequestHandler { public: FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, const char* uri, HTTPMethod method) - : fn(fn) + : _fn(fn) , base(uri, method) { } bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override { - if (method != HTTP_ANY && method != requestMethod) + if (_method != HTTP_ANY && _method != requestMethod) return false; - if (requestUri != uri) + if (requestUri != _uri) return false; - fn(); + _fn(); return true; } protected: - ESP8266WebServer::THandlerFunction fn; + ESP8266WebServer::THandlerFunction _fn; }; class StaticRequestHandler : public RequestHandler { typedef RequestHandler base; public: - StaticRequestHandler(FS& fs, const char* uri) - : fs(fs) + StaticRequestHandler(FS& fs, const char* path, const char* uri) + : _fs(fs) , 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 { - if (requestMethod != method) + if (requestMethod != _method) return false; - DEBUGV("StaticRequestHandler::handle: %s\r\n", requestUri.c_str()); - if (!requestUri.startsWith(uri)) + DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str()); + if (!requestUri.startsWith(_uri)) return false; - auto prefixLength = uri.length() - 1; - String path = requestUri.substring(prefixLength); - DEBUGV("StaticRequestHandler::handle: %d %s\r\n", prefixLength, path.c_str()); - File f = fs.open(path, "r"); + String path(_path); + if (!_isFile) { + // Base URI doesn't point to a file. Append whatever follows this + // 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) return false; @@ -90,7 +102,10 @@ public: } protected: - FS fs; + FS _fs; + String _path; + bool _isFile; + size_t _baseUriLength; }; #endif //REQUESTHANDLER_H