forked from me-no-dev/ESPAsyncWebServer
    
        
        - 
                Notifications
    You must be signed in to change notification settings 
- Fork 69
Support for pre-compressed and ETag in download #222
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
          
     Merged
      
        
      
            mathieucarbou
  merged 4 commits into
  ESP32Async:main
from
JosePineiro:feature/etag-caching-downloads
  
      
      
   
  Jul 10, 2025 
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|  | @@ -4,54 +4,58 @@ | |||||||||||||||||
| * @brief Sends a file from the filesystem to the client, with optional gzip compression and ETag-based caching. | ||||||||||||||||||
| * | ||||||||||||||||||
| * This method serves files over HTTP from the provided filesystem. If a compressed version of the file | ||||||||||||||||||
| * (with a `.gz` extension) exists and the `download` flag is not set, it serves the compressed file. | ||||||||||||||||||
| * (with a `.gz` extension) exists and uncompressed version do not exist, it serves the compressed file. | ||||||||||||||||||
| * It also handles ETag caching using the CRC32 value from the gzip trailer, responding with `304 Not Modified` | ||||||||||||||||||
| * if the client's `If-None-Match` header matches the generated ETag. | ||||||||||||||||||
| * | ||||||||||||||||||
| * @param fs Reference to the filesystem (SPIFFS, LittleFS, etc.). | ||||||||||||||||||
| * @param path Path to the file to be served. | ||||||||||||||||||
| * @param contentType Optional MIME type of the file to be sent. | ||||||||||||||||||
| * If contentType is "" it will be obtained from the file extension | ||||||||||||||||||
| * @param download If true, forces the file to be sent as a download (disables gzip compression). | ||||||||||||||||||
| * @param download If true, forces the file to be sent as a download. | ||||||||||||||||||
| * @param callback Optional template processor for dynamic content generation. | ||||||||||||||||||
| * Templates will not be processed in compressed files. | ||||||||||||||||||
| * | ||||||||||||||||||
| * @note If neither the file nor its compressed version exists, responds with `404 Not Found`. | ||||||||||||||||||
| */ | ||||||||||||||||||
| 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); | ||||||||||||||||||
| // Check uncompressed file first | ||||||||||||||||||
| if (fs.exists(path)) { | ||||||||||||||||||
| send(beginResponse(fs, path, contentType, download, callback)); | ||||||||||||||||||
|         
                  mathieucarbou marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|  | ||||||||||||||||||
| // 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); | ||||||||||||||||||
| // Handle compressed version | ||||||||||||||||||
| const String gzPath = path + asyncsrv::T__gz; | ||||||||||||||||||
|          | ||||||||||||||||||
| const String gzPath = path + asyncsrv::T__gz; | |
| const String gzPath = path + asyncsrv::T__gz; | |
| // Skip serving compressed file if download is true | |
| if (download) { | |
| send(404); | |
| return; | |
| } | 
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.