From 5c6f98643b09190de96b85515fd36dd3d4d82f7b Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 12 Nov 2015 07:27:21 +0000 Subject: [PATCH] Changes to static request handler --- .../ESP8266WebServer/src/ESP8266WebServer.cpp | 4 +- .../ESP8266WebServer/src/ESP8266WebServer.h | 2 +- .../src/detail/RequestHandlersImpl.h | 38 ++++++++++++++----- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index 075437706..cc8410ece 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -83,8 +83,8 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) { } } -void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) { - _addRequestHandler(new StaticRequestHandler(fs, path, uri)); +void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path, const char* cache_header) { + _addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header)); } void ESP8266WebServer::handleClient() { diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 40216f482..e2a8da46c 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -69,7 +69,7 @@ public: void on(const char* uri, THandlerFunction handler); void on(const char* uri, HTTPMethod method, THandlerFunction fn); void addHandler(RequestHandler* handler); - void serveStatic(const char* uri, fs::FS& fs, const char* path); + void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL ); void onNotFound(THandlerFunction fn); //called when handler is not assigned void onFileUpload(THandlerFunction fn); //handle file uploads diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h index e5da410f1..accddee21 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h @@ -31,39 +31,57 @@ protected: class StaticRequestHandler : public RequestHandler { public: - StaticRequestHandler(FS& fs, const char* path, const char* uri) + StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header) : _fs(fs) , _uri(uri) , _path(path) + , _cache_header(cache_header) { _isFile = fs.exists(path); - DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile); - _baseUriLength = _uri.length(); + DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header); + _baseUriLength = _uri.length(); } - bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override { if (requestMethod != HTTP_GET) return false; DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str()); if (!requestUri.startsWith(_uri)) - return false; + return false; - String path(_path); - if (!_isFile) { + String path(_path); + + if(path.endsWith("/")) path += "index.htm"; + + 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); + 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); + + String contentType = getContentType(path); + + // look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for + // if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc... + if (!path.endsWith(".gz") && !SPIFFS.exists(path)) { + String pathWithGz = path + ".gz"; + if(SPIFFS.exists(pathWithGz)) + path += ".gz"; + } + File f = _fs.open(path, "r"); if (!f) return false; - server.streamFile(f, getContentType(path)); + if (_cache_header.length() != 0) + server.sendHeader("Cache-Control", _cache_header); + + server.streamFile(f, contentType); return true; } @@ -81,6 +99,7 @@ public: else if (path.endsWith(".xml")) return "text/xml"; else if (path.endsWith(".pdf")) return "application/pdf"; else if (path.endsWith(".zip")) return "application/zip"; + else if(path.endsWith(".gz")) return "application/x-gzip"; return "application/octet-stream"; } @@ -88,6 +107,7 @@ protected: FS _fs; String _uri; String _path; + String _cache_header; bool _isFile; size_t _baseUriLength; };