Skip to content

Suggested improvement to Arduino/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h #3274

Closed
@elfalko

Description

@elfalko

Basic Infos

request Handler for .gz files works wrong if specified file path is to same file without .gz ending.

Hardware

Module: Generic ESP8266 Module

Description

I suggest wrapping the part looking for a gzipped version of a requested path in the same part that looks for the index.htm if the given path isn't found.
See sketch below

Problem description

eg. specified file path for "/web/index.html" file while /web/index.html.gz file exists
Expected behaviour:

  1. StaticRequestHandler::handle(...) should set content type to "txt/html"
  2. realize there is no such file
  3. try to attach ".gz" to path, try to find file again, serve if ok (with Content-Encoding:gzip Header
  4. if still not found, attach index.htm and try to find that

Real behaviour

  1. StaticRequestHandler::handle(...) should set content type to "txt/html"
  2. realize there is no such file
  3. try to attach index.htm to path, try to find file, serve if ok
  4. since that(in my case "/web/index.htmlindexhtm") wasn't found either, it attaches ".gz" and tries to find that
  5. /web/index.htmlindex.htm.gz of course isn't found and nothing happens

Settings in IDE

Module: Generic ESP8266 Module
Flash Size: 4MB
CPU Frequency: ?80Mhz?
Flash Mode: ?qio?
Flash Frequency: ?40Mhz?
Upload Using: SERIAL
Reset Method: nodemcu

Sketch

Current code (in Arduino/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h):

class StaticRequestHandler : public RequestHandler {

    {...}

    if (!_isFile) {
        // Base URI doesn't point to a file.
        // If a directory is requested, look for index file.
        if (requestUri.endsWith("/")) requestUri += "index.htm";

        // Append whatever follows this URI in request to get the file path.
        path += requestUri.substring(_baseUriLength);
    }
    DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);

    String contentType = getContentType(path);

    // look for gz file, only if the original specified path is not a gz.  So part only works to send gzip via content encoding when a non compressed is asked for
    // if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
    if (!path.endsWith(".gz") && !_fs.exists(path))  {
        String pathWithGz = path + ".gz";
        if(_fs.exists(pathWithGz))
            path += ".gz";
    }

{...}

};

Suggestion:

if (!_isFile) {
// look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
if (!path.endsWith(".gz")) { //no !_fs.exists(path) needed since that is done by !_isFile
String pathWithGz = path + ".gz";
DEBUGV("StaticRequestHandler::handle: Looking for .gz file at %s\r\n", pathWithGz.c_str());
if (_fs.exists(pathWithGz)) {
path += ".gz";
DEBUGV("StaticRequestHandler::handle: .gz file found.\n");
} else {
DEBUGV("StaticRequestHandler::handle: NO .gz file found, checking for index.htm\n");

      // Base URI doesn't point to a file.
      // If a directory is requested, look for index file.
      if (requestUri.endsWith("/")) requestUri += "index.htm";
      DEBUGV("expanding path\n");
      // Append whatever follows this URI in request to get the file path.
      path += requestUri.substring(_baseUriLength);
    }
  }
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions