Skip to content

Commit a6e01b8

Browse files
committed
Fix handling of / to load index.htm (esp8266#1085)
Thanks @sticilface
1 parent 5a2af54 commit a6e01b8

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class StaticRequestHandler : public RequestHandler {
6060
{
6161
_isFile = fs.exists(path);
6262
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header);
63-
_baseUriLength = _uri.length();
63+
_baseUriLength = _uri.length();
6464
}
6565

6666
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
@@ -75,26 +75,27 @@ class StaticRequestHandler : public RequestHandler {
7575

7676
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
7777
if (!canHandle(requestMethod, requestUri))
78-
return false;
79-
78+
return false;
79+
8080
DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
8181

82-
String path(_path);
82+
String path(_path);
8383

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

86-
if (!_isFile) {
87-
// Base URI doesn't point to a file. Append whatever follows this
88-
// URI in request to get the file path.
89-
path += requestUri.substring(_baseUriLength);
89+
// Append whatever follows this URI in request to get the file path.
90+
path += requestUri.substring(_baseUriLength);
9091
}
9192
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
9293

9394
String contentType = getContentType(path);
94-
95+
9596
// 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
96-
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
97-
if (!path.endsWith(".gz") && !SPIFFS.exists(path)) {
97+
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
98+
if (!path.endsWith(".gz") && !SPIFFS.exists(path)) {
9899
String pathWithGz = path + ".gz";
99100
if(SPIFFS.exists(pathWithGz))
100101
path += ".gz";
@@ -104,9 +105,9 @@ class StaticRequestHandler : public RequestHandler {
104105
if (!f)
105106
return false;
106107

107-
if (_cache_header.length() != 0)
108+
if (_cache_header.length() != 0)
108109
server.sendHeader("Cache-Control", _cache_header);
109-
110+
110111
server.streamFile(f, contentType);
111112
return true;
112113
}
@@ -133,7 +134,7 @@ class StaticRequestHandler : public RequestHandler {
133134
FS _fs;
134135
String _uri;
135136
String _path;
136-
String _cache_header;
137+
String _cache_header;
137138
bool _isFile;
138139
size_t _baseUriLength;
139140
};

0 commit comments

Comments
 (0)