Skip to content

WebServer serveStatic Missing Header Content-Encoding - gzip when serving gzipped files to browser in RequestHandlersImpl.h #5960

@Zwaai

Description

@Zwaai

Board: ESP32 Dev Module
Core Installation version: 3.3.2
IDE name: Platform.io
Flash Frequency: 40Mhz
PSRAM enabled: yes
Upload Speed: 115200?
Computer OS: Windows 10

Description:

This problem is regarding Webserver.h

I am trying to serve some static content (compiled vue app) from the ESP-32 using the Webserver class. The js / css files are gzipped. When using the WebServer::serveStatic handler for these files, it seems like the 'Content-Encoding' header is missing.

I solved my problem by modifying RequestHandlersImpl.h, but i'm wondering whether this is a bug, or if i'm doing something the wrong way. See the change in the snippet below; (line 105)

  if(contentType == mimeTable[gz].mimeType){
      server.sendHeader("Content-Encoding", "gzip");
  }

Not adding this line causes the browser to spew out gibberish because it doesn't know it has to unzip the files.

Adding static handlers from files on data partition

  void addStaticFileHandlers(fs::FS& fs, const char* dirname, uint8_t levels) {
    File root = fs.open(dirname);
    if (!root) {
      if (DEBUG) Serial.println("- failed to open directory");
      return;
    }
    if (!root.isDirectory()) {
      if (DEBUG) Serial.println(" - not a directory");
      return;
    }
    File file = root.openNextFile();
    while (file) {
      if (file.isDirectory()) {
        //do nothing
      }
      else {
        char* name = (char*)malloc(strlen(file.name()));
        for (int k = 0; k < strlen(file.name()); k++) {
          name[k] = file.name()[k];          
        }        
        name[strlen(file.name())] = '\0';
        String fileName = String(name);

        if(fileName.endsWith(".gz")){ // gzipped file, remove gzip extension from uri
          this->_webServer->serveStatic(fileName.substring(0, fileName.length() - 3).c_str(), fs, name, "public, max-age=31536000"); // one year
        }
        else{
          this->_webServer->serveStatic(name, fs, name, "public, max-age=31536000"); // one year
        }              
      }
      file = root.openNextFile();
    }
  }

Files in data folder:
image

image

If the above is not clear, please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions