forked from me-no-dev/ESPAsyncWebServer
-
Notifications
You must be signed in to change notification settings - Fork 70
Add ETag handling and cache for pre-compressed .gz files #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
JosePineiro
wants to merge
7
commits into
ESP32Async:main
from
JosePineiro:feature/asyncfileresponse-gzip-etag
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c34ba69
Add ETag handling and cache for pre-compressed .gz files
JosePineiro ea78624
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] 4ad6659
_getEtag tostatic. Speed up functions
JosePineiro 4add248
Revert "_getEtag tostatic. Speed up functions"
JosePineiro a1eebbd
Static and speed up
JosePineiro de1ebe0
Merge branch 'main' into feature/asyncfileresponse-gzip-etag
JosePineiro 50c7e89
extract definitions to AsyncWebServerRequest
JosePineiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include <ESPAsyncWebServer.h> | ||
|
|
||
| void AsyncWebServerRequest::send(FS &fs, const String &path, const char *contentType, bool download, AwsTemplateProcessor callback) { | ||
| const String gzPath = path + asyncsrv::T__gz; | ||
| const bool useCompressedVersion = !download && fs.exists(gzPath); | ||
|
|
||
| // If-None-Match header | ||
| if (useCompressedVersion && this->hasHeader(asyncsrv::T_INM)) { | ||
| // CRC32-based ETag of the trailer, bytes 4-7 from the end | ||
| File file = fs.open(gzPath, fs::FileOpenMode::read); | ||
| if (file && file.size() >= 18) { // 18 is the minimum size of valid gzip file | ||
| file.seek(file.size() - 8); | ||
|
|
||
| uint8_t crcFromGzipTrailer[4]; | ||
| if (file.read(crcFromGzipTrailer, sizeof(crcFromGzipTrailer)) == sizeof(crcFromGzipTrailer)) { | ||
| char serverETag[11]; // " + 8 hex chars + " + '\0' | ||
| _getEtag(crcFromGzipTrailer, serverETag); | ||
|
|
||
| // Compare with client's If-None-Match header | ||
| const AsyncWebHeader* inmHeader = this->getHeader(asyncsrv::T_INM); | ||
| if (inmHeader && inmHeader->value().equals(serverETag)) { | ||
| file.close(); | ||
| this->send(304); // Not Modified | ||
| return; | ||
| } | ||
| } | ||
| file.close(); | ||
| } | ||
| } | ||
|
|
||
| // If we get here, create and send the normal response | ||
| if (fs.exists(path) || useCompressedVersion) { | ||
| send(beginResponse(fs, path, contentType, download, callback)); | ||
| } else { | ||
| send(404); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Generates an ETag string from a 4-byte trailer using basic loop implementation | ||
| * | ||
| * This function converts a 4-byte array into a hexadecimal ETag string enclosed in quotes. | ||
| * | ||
| * @param trailer[4] Input array of 4 bytes to convert to hexadecimal | ||
| * @param serverETag Output buffer that must be at least 11 bytes long to store the ETag | ||
| * Must be pre-allocated with minimum 11 bytes (8 hex + 2 quotes + 1 null terminator) | ||
| */ | ||
| void AsyncWebServerRequest::_getEtag(uint8_t trailer[4], char* serverETag) { | ||
| static constexpr char hexChars[] = "0123456789ABCDEF"; | ||
|
|
||
| uint32_t data; | ||
| memcpy(&data, trailer, 4); | ||
|
|
||
| serverETag[0] = '"'; | ||
| serverETag[1] = hexChars[(data >> 4) & 0x0F]; | ||
| serverETag[2] = hexChars[data & 0x0F]; | ||
| serverETag[3] = hexChars[(data >> 12) & 0x0F]; | ||
| serverETag[4] = hexChars[(data >> 8) & 0x0F]; | ||
| serverETag[5] = hexChars[(data >> 20) & 0x0F]; | ||
| serverETag[6] = hexChars[(data >> 16) & 0x0F]; | ||
| serverETag[7] = hexChars[(data >> 28)]; | ||
| serverETag[8] = hexChars[(data >> 24) & 0x0F]; | ||
| serverETag[9] = '"'; | ||
| serverETag[10] = '\0'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why public ? should be private