@@ -31,39 +31,57 @@ class FunctionRequestHandler : public RequestHandler {
3131
3232class StaticRequestHandler : public RequestHandler {
3333public:
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
87106protected:
88107 FS _fs;
89108 String _uri;
90109 String _path;
110+ String _cache_header;
91111 bool _isFile;
92112 size_t _baseUriLength;
93113};
0 commit comments