From 11b7bd1d3a0402b64249106dbf51b6f250ded6a1 Mon Sep 17 00:00:00 2001 From: Hagai Shatz Date: Sat, 18 Jun 2016 17:43:52 +0100 Subject: [PATCH] Fix bug in AsyncStaticWebHandler (#37) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * HTTP 302 and 304 Support Add support for http redirection (302) and http not modified (304) to reduce the load the server. server.redirect(“url”, “location”, exclude-ip) will respond with 302 to redirect the browser to a different url, this is useful for backward compatibility and to redirect call to CDN when not no AP mode. server.serveStatic has a new optional parameter to get the Last-Modified date for all files serve for this location, when the browser request have the same If-Modified-Since header value, the server respond with 304 code instead of serving the file. * Fix path problems in static handler and improve performance. * Revert "Merge remote-tracking branch 'me-no-dev/master'" This reverts commit 1621206357843b5de0272fe4579387af3011e656, reversing changes made to a01972c9e569967dd3d761c364066518b4901e46. * Revert "HTTP 302 and 304 Support" This reverts commit a01972c9e569967dd3d761c364066518b4901e46. * Sync with me-no-dev/master * Fix AsyncStaticWebHandler Fix ambiguity of serving file or directory. The following options will all have the same outcome, the last two will server the default file ‘index.htm’ faster: server.serveStatic("/fs", SPIFFS, "/web"); server.serveStatic("/fs/", SPIFFS, "/web"); server.serveStatic("/fs", SPIFFS, "/web/"); server.serveStatic("/fs/", SPIFFS, "/web/"); --- .gitignore | 2 -- src/WebHandlers.cpp | 18 ++++++++---------- src/WebResponses.cpp | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index df30947..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ - -.idea/ diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index db21df3..e7ee62f 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -28,16 +28,14 @@ AsyncStaticWebHandler::AsyncStaticWebHandler(FS& fs, const char* path, const cha if (_uri.length() == 0 || _uri[0] != '/') _uri = "/" + _uri; if (_path.length() == 0 || _path[0] != '/') _path = "/" + _path; - // If uri and path ends with '/' we assume a hint that this is a directory to improve performance. - // However - if one do not end '/' we, can't assume they are files, they can still be directory. - _isDir = _uri[_uri.length()-1] == '/' && _path[_path.length()-1] == '/'; + // If path ends with '/' we assume a hint that this is a directory to improve performance. + // However - if it does not end with '/' we, can't assume a file, path can still be a directory. + _isDir = _path[_path.length()-1] == '/'; - // If we serving directory - remove the trailing '/' so we can handle default file + // Remove the trailing '/' so we can handle default file // Notice that root will be "" not "/" - if (_isDir) { - _uri = _uri.substring(0, _uri.length()-1); - _path = _path.substring(0, _path.length()-1); - } + if (_uri[_uri.length()-1] == '/') _uri = _uri.substring(0, _uri.length()-1); + if (_path[_path.length()-1] == '/') _path = _path.substring(0, _path.length()-1); // Reset stats _gzipFirst = false; @@ -63,8 +61,8 @@ bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest *request) // Remove the found uri String path = request->url().substring(_uri.length()); - // We can skip the file check if we serving a directory and (we have full match or we end with '/') - bool canSkipFileCheck = _isDir && (path.length() == 0 || path[path.length()-1] == '/'); + // We can skip the file check and look for default if request is to the root of a directory or that request path ends with '/' + bool canSkipFileCheck = (_isDir && path.length() == 0) || (path.length() && path[path.length()-1] == '/'); path = _path + path; diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp index 6534b6e..c74d0f9 100644 --- a/src/WebResponses.cpp +++ b/src/WebResponses.cpp @@ -357,7 +357,7 @@ void AsyncFileResponse::_setContentType(String path){ AsyncFileResponse::AsyncFileResponse(FS &fs, String path, String contentType, bool download){ _code = 200; _path = path; - + if(!download && !fs.exists(_path) && fs.exists(_path+".gz")){ _path = _path+".gz"; addHeader("Content-Encoding", "gzip");