Skip to content

Commit f693411

Browse files
authored
Refactor ETag to use GZIP CRC32 trailer
Refactor ETag generation to read CRC32 checksum from GZIP file trailer - Changed _getEtag() signature to accept File handle instead of byte array - Function now seeks to GZIP trailer and extracts CRC32 checksum - Added error handling for file operations (seek/read failures) - Returns boolean to indicate success/failure of ETag generation - Maintains same hex conversion logic for consistent ETag format - Simplifies code following DRY principle by eliminating duplicate read file This refactoring maintains full backward compatibility
1 parent 80af245 commit f693411

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/AsyncWebServerRequest.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,16 @@ void AsyncWebServerRequest::send(FS &fs, const String &path, const char *content
2929
const String gzPath = path + asyncsrv::T__gz;
3030
File gzFile = fs.open(gzPath, fs::FileOpenMode::read);
3131

32-
// Compressed file not found or invalid
33-
if (!gzFile.seek(gzFile.size() - 8)) {
34-
send(404);
35-
gzFile.close();
36-
return;
37-
}
38-
3932
// ETag validation
4033
if (this->hasHeader(asyncsrv::T_INM)) {
4134
// Generate server ETag from CRC in gzip trailer
42-
uint8_t crcInTrailer[4];
43-
gzFile.read(crcInTrailer, 4);
4435
char serverETag[9];
45-
_getEtag(crcInTrailer, serverETag);
36+
if (!_getEtag(gzFile, serverETag)) {
37+
// Compressed file not found or invalid
38+
send(404);
39+
gzFile.close();
40+
return;
41+
}
4642

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

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

72+
// Compressed file not found or invalid
73+
if (!gzFile.seek(gzFile.size() - 8))
74+
return false;
75+
76+
uint8_t crcInTrailer[4];
77+
gzFile.read(crcInTrailer, 4);
78+
7379
uint32_t data;
74-
memcpy(&data, trailer, 4);
80+
memcpy(&data, crcInTrailer, 4);
81+
82+
etag[0] = hexChars[(data >> 4) & 0x0F];
83+
etag[1] = hexChars[data & 0x0F];
84+
etag[2] = hexChars[(data >> 12) & 0x0F];
85+
etag[3] = hexChars[(data >> 8) & 0x0F];
86+
etag[4] = hexChars[(data >> 20) & 0x0F];
87+
etag[5] = hexChars[(data >> 16) & 0x0F];
88+
etag[6] = hexChars[(data >> 28)];
89+
etag[7] = hexChars[(data >> 24) & 0x0F];
90+
etag[8] = '\0';
7591

76-
serverETag[0] = hexChars[(data >> 4) & 0x0F];
77-
serverETag[1] = hexChars[data & 0x0F];
78-
serverETag[2] = hexChars[(data >> 12) & 0x0F];
79-
serverETag[3] = hexChars[(data >> 8) & 0x0F];
80-
serverETag[4] = hexChars[(data >> 20) & 0x0F];
81-
serverETag[5] = hexChars[(data >> 16) & 0x0F];
82-
serverETag[6] = hexChars[(data >> 28)];
83-
serverETag[7] = hexChars[(data >> 24) & 0x0F];
84-
serverETag[8] = '\0';
92+
return true;
8593
}

src/ESPAsyncWebServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class AsyncWebServerRequest {
275275
void _send();
276276
void _runMiddlewareChain();
277277

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

280280
public:
281281
File _tempFile;

src/WebResponses.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,24 +711,19 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
711711
_content = fs.open(path, fs::FileOpenMode::read);
712712
if (_content.available()) {
713713
_path = path;
714-
_contentLength = _content.size();
715714
} else {
716715
// Try to open the compressed version (.gz)
717716
_path = path + asyncsrv::T__gz;
718717
_content = fs.open(_path, fs::FileOpenMode::read);
719-
_contentLength = _content.size();
720718

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

727726
// Add ETag and cache headers
728-
uint8_t crcInTrailer[4];
729-
_content.read(crcInTrailer, sizeof(crcInTrailer));
730-
char serverETag[9];
731-
AsyncWebServerRequest::_getEtag(crcInTrailer, serverETag);
732727
addHeader(T_ETag, serverETag, true);
733728
addHeader(T_Cache_Control, T_no_cache, true);
734729

@@ -739,6 +734,9 @@ AsyncFileResponse::AsyncFileResponse(FS &fs, const String &path, const char *con
739734
return;
740735
}
741736
}
737+
738+
_contentLength = _content.size();
739+
742740

743741
if (*contentType == '\0') {
744742
_setContentTypeFromPath(path);

0 commit comments

Comments
 (0)