Description
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:
- StaticRequestHandler::handle(...) should set content type to "txt/html"
- realize there is no such file
- try to attach ".gz" to path, try to find file again, serve if ok (with Content-Encoding:gzip Header
- if still not found, attach index.htm and try to find that
Real behaviour
- StaticRequestHandler::handle(...) should set content type to "txt/html"
- realize there is no such file
- try to attach index.htm to path, try to find file, serve if ok
- since that(in my case "/web/index.htmlindexhtm") wasn't found either, it attaches ".gz" and tries to find that
- /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);
}
}
}