Skip to content

Commit a6cb40f

Browse files
committed
Merge pull request #1007 from sticilface/master
Changes to static request handler
2 parents d0a944e + 44900b4 commit a6cb40f

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) {
8383
}
8484
}
8585

86-
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) {
87-
_addRequestHandler(new StaticRequestHandler(fs, path, uri));
86+
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path, const char* cache_header) {
87+
_addRequestHandler(new StaticRequestHandler(fs, path, uri, cache_header));
8888
}
8989

9090
void ESP8266WebServer::handleClient() {

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ESP8266WebServer
6969
void on(const char* uri, THandlerFunction handler);
7070
void on(const char* uri, HTTPMethod method, THandlerFunction fn);
7171
void addHandler(RequestHandler* handler);
72-
void serveStatic(const char* uri, fs::FS& fs, const char* path);
72+
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
7373
void onNotFound(THandlerFunction fn); //called when handler is not assigned
7474
void onFileUpload(THandlerFunction fn); //handle file uploads
7575

libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

+29-9
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,57 @@ class FunctionRequestHandler : public RequestHandler {
3131

3232
class StaticRequestHandler : public RequestHandler {
3333
public:
34-
StaticRequestHandler(FS& fs, const char* path, const char* uri)
34+
StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
3535
: _fs(fs)
3636
, _uri(uri)
3737
, _path(path)
38+
, _cache_header(cache_header)
3839
{
3940
_isFile = fs.exists(path);
40-
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile);
41-
_baseUriLength = _uri.length();
41+
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header);
42+
_baseUriLength = _uri.length();
4243
}
43-
4444
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
4545
if (requestMethod != HTTP_GET)
4646
return false;
4747
DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
4848
if (!requestUri.startsWith(_uri))
49-
return false;
49+
return false;
50+
51+
String path(_path);
52+
53+
if(path.endsWith("/")) path += "index.htm";
5054

51-
String path(_path);
52-
if (!_isFile) {
55+
if (!_isFile) {
5356
// Base URI doesn't point to a file. Append whatever follows this
5457
// URI in request to get the file path.
55-
path += requestUri.substring(_baseUriLength);
58+
path += requestUri.substring(_baseUriLength);
5659
}
60+
5761
else if (requestUri != _uri) {
5862
// Base URI points to a file but request doesn't match this URI exactly
5963
return false;
6064
}
6165
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
66+
67+
String contentType = getContentType(path);
68+
69+
// 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
70+
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
71+
if (!path.endsWith(".gz") && !SPIFFS.exists(path)) {
72+
String pathWithGz = path + ".gz";
73+
if(SPIFFS.exists(pathWithGz))
74+
path += ".gz";
75+
}
76+
6277
File f = _fs.open(path, "r");
6378
if (!f)
6479
return false;
6580

66-
server.streamFile(f, getContentType(path));
81+
if (_cache_header.length() != 0)
82+
server.sendHeader("Cache-Control", _cache_header);
83+
84+
server.streamFile(f, contentType);
6785
return true;
6886
}
6987

@@ -81,13 +99,15 @@ class StaticRequestHandler : public RequestHandler {
8199
else if (path.endsWith(".xml")) return "text/xml";
82100
else if (path.endsWith(".pdf")) return "application/pdf";
83101
else if (path.endsWith(".zip")) return "application/zip";
102+
else if(path.endsWith(".gz")) return "application/x-gzip";
84103
return "application/octet-stream";
85104
}
86105

87106
protected:
88107
FS _fs;
89108
String _uri;
90109
String _path;
110+
String _cache_header;
91111
bool _isFile;
92112
size_t _baseUriLength;
93113
};

0 commit comments

Comments
 (0)