Fix bug in AsyncStaticWebHandler (#37)

* 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/");
This commit is contained in:
Hagai Shatz 2016-06-18 17:43:52 +01:00 committed by Me No Dev
parent e46d4d7418
commit 11b7bd1d3a
3 changed files with 9 additions and 13 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
.idea/

View File

@ -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;

View File

@ -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");