Sitelet https://github.com/ESP32Async/ESPAsyncWebServer/commit/11b7bd1d3a0402b64249106dbf51b6f250ded6a1
Skip to content

Commit 11b7bd1

Browse files
hagai-shatzme-no-dev
authored andcommitted
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 1621206, reversing changes made to a01972c. * Revert "HTTP 302 and 304 Support" This reverts commit a01972c. * 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/");
1 parent e46d4d7 commit 11b7bd1

3 files changed

Lines changed: 9 additions & 13 deletions

File tree

‎.gitignore‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎src/WebHandlers.cpp‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ AsyncStaticWebHandler::AsyncStaticWebHandler(FS& fs, const char* path, const cha
2828
if (_uri.length() == 0 || _uri[0] != '/') _uri = "/" + _uri;
2929
if (_path.length() == 0 || _path[0] != '/') _path = "/" + _path;
3030

31-
// If uri and path ends with '/' we assume a hint that this is a directory to improve performance.
32-
// However - if one do not end '/' we, can't assume they are files, they can still be directory.
33-
_isDir = _uri[_uri.length()-1] == '/' && _path[_path.length()-1] == '/';
31+
// If path ends with '/' we assume a hint that this is a directory to improve performance.
32+
// However - if it does not end with '/' we, can't assume a file, path can still be a directory.
33+
_isDir = _path[_path.length()-1] == '/';
3434

35-
// If we serving directory - remove the trailing '/' so we can handle default file
35+
// Remove the trailing '/' so we can handle default file
3636
// Notice that root will be "" not "/"
37-
if (_isDir) {
38-
_uri = _uri.substring(0, _uri.length()-1);
39-
_path = _path.substring(0, _path.length()-1);
40-
}
37+
if (_uri[_uri.length()-1] == '/') _uri = _uri.substring(0, _uri.length()-1);
38+
if (_path[_path.length()-1] == '/') _path = _path.substring(0, _path.length()-1);
4139

4240
// Reset stats
4341
_gzipFirst = false;
@@ -63,8 +61,8 @@ bool AsyncStaticWebHandler::_getFile(AsyncWebServerRequest *request)
6361
// Remove the found uri
6462
String path = request->url().substring(_uri.length());
6563

66-
// We can skip the file check if we serving a directory and (we have full match or we end with '/')
67-
bool canSkipFileCheck = _isDir && (path.length() == 0 || path[path.length()-1] == '/');
64+
// 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 '/'
65+
bool canSkipFileCheck = (_isDir && path.length() == 0) || (path.length() && path[path.length()-1] == '/');
6866

6967
path = _path + path;
7068

‎src/WebResponses.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ void AsyncFileResponse::_setContentType(String path){
357357
AsyncFileResponse::AsyncFileResponse(FS &fs, String path, String contentType, bool download){
358358
_code = 200;
359359
_path = path;
360-
360+
361361
if(!download && !fs.exists(_path) && fs.exists(_path+".gz")){
362362
_path = _path+".gz";
363363
addHeader("Content-Encoding", "gzip");

0 commit comments

Comments
 (0)