-
Notifications
You must be signed in to change notification settings - Fork 67
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
Add ETag handling and cache for pre-compressed .gz files #213
Conversation
Add ETag handling and cache for pre-compressed .gz files in AsyncFileResponse This patch improves AsyncFileResponse by adding ETag and cache support for pre-compressed .gz files. Pre-compressed files (e.g., index.html.gz) were already being served when available. This update introduces automatic generation of an ETag for such files, based on the CRC32 checksum located in the gzip trailer (last 4 bytes). When a .gz file from FS is served: - An `ETag` header is included, derived from the gzip trailer CRC32 - A `Cache-Control: no-cache` header is added to ensure proper validation This enhancement enables better caching and validation mechanisms for clients, improving efficiency while ensuring updated content is properly detected.
|
@JosePineiro : thank you for this improvement! We will review and merge soon |
|
@vortigont : would you be able to have a look since you worked on this part ? |
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.
Pull Request Overview
This PR introduces ETag handling and caching improvements for serving pre‐compressed .gz files, enabling conditional GET responses to optimize bandwidth and server resource usage.
- Adds ETag header generation based on the CRC32 checksum from the gzip trailer.
- Implements conditional 304 responses in the send() method when the client's ETag matches the server's.
- Enhances both asynchronous file response handling and web server request processing for .gz files.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/WebResponses.cpp | Adds ETag generation and cache-control headers for .gz files |
| src/ESPAsyncWebServer.h | Integrates conditional response logic based on the ETag and implements similar ETag extraction logic |
|
Please have a look at the copilot suggestions. Also please move the implementations of |
|
I'm torn between these two versions of the _getEtag function. Version 2 is 25% faster, but requires a 32-bit CPU and uses 20 more bits of flash memory.
What do you consider a priority? |
|
For a web server I would Favour speed over flash size |
| void addInterestingHeader(__unused const String &name) { | ||
| } | ||
|
|
||
| static void _getEtag(uint8_t trailer[4], char* serverETag); |
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
This update brings improved standards-compliant handling of pre-compressed .gz files, providing ETag support and conditional responses, helping optimize client-side caching and server resource usage. Fully backward compatible.
ETag Generation for Pre-compressed .gz Files
Conditional Request Support in send()
These changes only apply to pre-compressed .gz files and do not alter the behavior for uncompressed files.
Why this is useful?