Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions src/AsyncWebServerRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ void AsyncWebServerRequest::send(FS &fs, const String &path, const char *content
const String gzPath = path + asyncsrv::T__gz;
File gzFile = fs.open(gzPath, fs::FileOpenMode::read);

// Compressed file not found or invalid
if (!gzFile.seek(gzFile.size() - 8)) {
send(404);
gzFile.close();
return;
}

// ETag validation
if (this->hasHeader(asyncsrv::T_INM)) {
// Generate server ETag from CRC in gzip trailer
uint8_t crcInTrailer[4];
gzFile.read(crcInTrailer, 4);
char serverETag[9];
_getEtag(crcInTrailer, serverETag);
if (!_getEtag(gzFile, serverETag)) {
// Compressed file not found or invalid
send(404);
gzFile.close();
return;
}

// Compare with client's ETag
const AsyncWebHeader *inmHeader = this->getHeader(asyncsrv::T_INM);
Expand All @@ -59,27 +55,36 @@ void AsyncWebServerRequest::send(FS &fs, const String &path, const char *content
}

/**
* @brief Generates an ETag string from a 4-byte trailer
* @brief Generates an ETag string from the CRC32 trailer of a GZIP file.
*
* This function reads the CRC32 checksum (4 bytes) located at the end of a GZIP-compressed file
* and converts it into an 8-character hexadecimal ETag string (null-terminated).
*
* This function converts a 4-byte array into a hexadecimal ETag string enclosed in quotes.
* @param gzFile Opened file handle pointing to the GZIP file.
* @param eTag Output buffer to store the generated ETag.
* Must be pre-allocated with at least 9 bytes (8 for hex digits + 1 for null terminator).
*
* @param trailer[4] Input array of 4 bytes to convert to hexadecimal
* @param serverETag Output buffer to store the ETag
* Must be pre-allocated with minimum 9 bytes (8 hex + 1 null terminator)
* @return true if the ETag was successfully generated, false otherwise (e.g., file too short or seek failed).
*/
void AsyncWebServerRequest::_getEtag(uint8_t trailer[4], char *serverETag) {
bool AsyncWebServerRequest::_getEtag(File gzFile, char *etag) {
static constexpr char hexChars[] = "0123456789ABCDEF";

uint32_t data;
memcpy(&data, trailer, 4);
if (!gzFile.seek(gzFile.size() - 8)) {
return false;
}

uint32_t crc;
gzFile.read(reinterpret_cast<uint8_t *>(&crc), sizeof(crc));

etag[0] = hexChars[(crc >> 4) & 0x0F];
etag[1] = hexChars[crc & 0x0F];
etag[2] = hexChars[(crc >> 12) & 0x0F];
etag[3] = hexChars[(crc >> 8) & 0x0F];
etag[4] = hexChars[(crc >> 20) & 0x0F];
etag[5] = hexChars[(crc >> 16) & 0x0F];
etag[6] = hexChars[(crc >> 28)];
etag[7] = hexChars[(crc >> 24) & 0x0F];
etag[8] = '\0';

serverETag[0] = hexChars[(data >> 4) & 0x0F];
serverETag[1] = hexChars[data & 0x0F];
serverETag[2] = hexChars[(data >> 12) & 0x0F];
serverETag[3] = hexChars[(data >> 8) & 0x0F];
serverETag[4] = hexChars[(data >> 20) & 0x0F];
serverETag[5] = hexChars[(data >> 16) & 0x0F];
serverETag[6] = hexChars[(data >> 28)];
serverETag[7] = hexChars[(data >> 24) & 0x0F];
serverETag[8] = '\0';
return true;
}
2 changes: 1 addition & 1 deletion src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class AsyncWebServerRequest {
void _send();
void _runMiddlewareChain();

static void _getEtag(uint8_t trailer[4], char *serverETag);
static bool _getEtag(File gzFile, char *eTag);

public:
File _tempFile;
Expand Down
16 changes: 6 additions & 10 deletions src/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,29 +699,23 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con

// Try to open the uncompressed version first
_content = fs.open(path, fs::FileOpenMode::read);
if (_content.available()) {
_contentLength = _content.size();
} else {
// Try to open the compressed version (.gz)
if (!_content.available()) {
// If not available try to open the compressed version (.gz)
String gzPath;
uint16_t pathLen = path.length();
gzPath.reserve(pathLen + 3);
gzPath.concat(path);
gzPath.concat(asyncsrv::T__gz);
_content = fs.open(gzPath, fs::FileOpenMode::read);
_contentLength = _content.size();

if (_content.seek(_contentLength - 8)) {
char serverETag[9];
if (AsyncWebServerRequest::_getEtag(_content, serverETag)) {
addHeader(T_Content_Encoding, T_gzip, false);
_callback = nullptr; // Unable to process zipped templates
_sendContentLength = true;
_chunked = false;

// Add ETag and cache headers
uint8_t crcInTrailer[4];
_content.read(crcInTrailer, sizeof(crcInTrailer));
char serverETag[9];
AsyncWebServerRequest::_getEtag(crcInTrailer, serverETag);
addHeader(T_ETag, serverETag, true);
addHeader(T_Cache_Control, T_no_cache, true);

Expand All @@ -733,6 +727,8 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
}
}

_contentLength = _content.size();

if (*contentType == '\0') {
_setContentTypeFromPath(path);
} else {
Expand Down