refactor: improved web cache

This commit is contained in:
Yurii
2024-11-14 22:30:34 +03:00
parent d3b28c5bfb
commit 34eabca64a
5 changed files with 49 additions and 16 deletions

View File

@@ -218,7 +218,6 @@ protected:
CanHandleCallback canHandleCallback;
BeforeSendCallback beforeSendCallback;
TemplateCallback templateCallback;
String eTag;
const char* uri = nullptr;
const char* path = nullptr;
const char* cacheHeader = nullptr;

View File

@@ -1,5 +1,8 @@
#include <FS.h>
#include <detail/mimetable.h>
#if defined(ARDUINO_ARCH_ESP32)
#include <detail/RequestHandlersImpl.h>
#endif
using namespace mime;
@@ -47,21 +50,25 @@ public:
return true;
}
#if defined(ARDUINO_ARCH_ESP8266)
if (server._eTagEnabled) {
if (server._eTagFunction) {
this->eTag = (server._eTagFunction)(*this->fs, this->path);
if (this->eTag.isEmpty()) {
if (server._eTagFunction) {
this->eTag = (server._eTagFunction)(*this->fs, this->path);
} else if (this->eTag.isEmpty()) {
this->eTag = esp8266webserver::calcETag(*this->fs, this->path);
} else {
#if defined(ARDUINO_ARCH_ESP8266)
this->eTag = esp8266webserver::calcETag(*this->fs, this->path);
#elif defined(ARDUINO_ARCH_ESP32)
this->eTag = StaticRequestHandler::calcETag(*this->fs, this->path);
#endif
}
}
if (server.header(F("If-None-Match")).equals(this->eTag.c_str())) {
if (!this->eTag.isEmpty() && server.header(F("If-None-Match")).equals(this->eTag.c_str())) {
server.send(304);
return true;
}
}
#endif
if (!this->path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !this->fs->exists(path)) {
String pathWithGz = this->path + FPSTR(mimeTable[gz].endsWith);
@@ -84,11 +91,11 @@ public:
server.sendHeader(F("Cache-Control"), this->cacheHeader);
}
#if defined(ARDUINO_ARCH_ESP8266)
if (server._eTagEnabled && this->eTag.length() > 0) {
if (server._eTagEnabled && !this->eTag.isEmpty()) {
server.sendHeader(F("ETag"), this->eTag);
}
#if defined(ARDUINO_ARCH_ESP8266)
server.streamFile(file, F("text/html"), method);
#else
server.streamFile(file, F("text/html"), 200);