From ea7a593b4eaea72838d55e671de5a30e7cb87a2e Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Wed, 23 Oct 2024 10:27:55 +0200 Subject: [PATCH] Fix AsyncStaticWebHandler to remove the wrong gzip stat feature which is not taking in consideration the frequency of each request. Expose instead a boolean flag to let the user control that. --- src/WebHandlerImpl.h | 4 ++-- src/WebHandlers.cpp | 18 +++++------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/WebHandlerImpl.h b/src/WebHandlerImpl.h index c92290d..5b18687 100644 --- a/src/WebHandlerImpl.h +++ b/src/WebHandlerImpl.h @@ -47,13 +47,13 @@ class AsyncStaticWebHandler : public AsyncWebHandler { String _last_modified; AwsTemplateProcessor _callback; bool _isDir; - bool _gzipFirst; - uint8_t _gzipStats; + bool _tryGzipFirst = true; public: AsyncStaticWebHandler(const char* uri, FS& fs, const char* path, const char* cache_control); virtual bool canHandle(AsyncWebServerRequest* request) const override final; virtual void handleRequest(AsyncWebServerRequest* request) override final; + AsyncStaticWebHandler& setTryGzipFirst(bool value); AsyncStaticWebHandler& setIsDir(bool isDir); AsyncStaticWebHandler& setDefaultFile(const char* filename); AsyncStaticWebHandler& setCacheControl(const char* cache_control); diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index ad0716a..bd3be20 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -56,10 +56,11 @@ AsyncStaticWebHandler::AsyncStaticWebHandler(const char* uri, FS& fs, const char _uri = _uri.substring(0, _uri.length() - 1); if (_path[_path.length() - 1] == '/') _path = _path.substring(0, _path.length() - 1); +} - // Reset stats - _gzipFirst = false; - _gzipStats = 0xF8; +AsyncStaticWebHandler& AsyncStaticWebHandler::setTryGzipFirst(bool value) { + _tryGzipFirst = value; + return *this; } AsyncStaticWebHandler& AsyncStaticWebHandler::setIsDir(bool isDir) { @@ -148,7 +149,7 @@ bool AsyncStaticWebHandler::_fileExists(AsyncWebServerRequest* request, const St String gzip = path + T__gz; - if (_gzipFirst) { + if (_tryGzipFirst) { if (_fs.exists(gzip)) { request->_tempFile = _fs.open(gzip, fs::FileOpenMode::read); gzipFound = FILE_IS_REAL(request->_tempFile); @@ -180,15 +181,6 @@ bool AsyncStaticWebHandler::_fileExists(AsyncWebServerRequest* request, const St char* _tempPath = (char*)malloc(pathLen + 1); snprintf_P(_tempPath, pathLen + 1, PSTR("%s"), path.c_str()); request->_tempObject = (void*)_tempPath; - - // Calculate gzip statistic - _gzipStats = (_gzipStats << 1) + (gzipFound ? 1 : 0); - if (_gzipStats == 0x00) - _gzipFirst = false; // All files are not gzip - else if (_gzipStats == 0xFF) - _gzipFirst = true; // All files are gzip - else - _gzipFirst = _countBits(_gzipStats) > 4; // IF we have more gzip files - try gzip first } return found;