@@ -31,39 +31,57 @@ class FunctionRequestHandler : public RequestHandler {
31
31
32
32
class StaticRequestHandler : public RequestHandler {
33
33
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 )
35
35
: _fs(fs)
36
36
, _uri(uri)
37
37
, _path(path)
38
+ , _cache_header(cache_header)
38
39
{
39
40
_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 ();
42
43
}
43
-
44
44
bool handle (ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
45
45
if (requestMethod != HTTP_GET)
46
46
return false ;
47
47
DEBUGV (" StaticRequestHandler::handle: request=%s _uri=%s\r\n " , requestUri.c_str (), _uri.c_str ());
48
48
if (!requestUri.startsWith (_uri))
49
- return false ;
49
+ return false ;
50
+
51
+ String path (_path);
52
+
53
+ if (path.endsWith (" /" )) path += " index.htm" ;
50
54
51
- String path (_path);
52
- if (!_isFile) {
55
+ if (!_isFile) {
53
56
// Base URI doesn't point to a file. Append whatever follows this
54
57
// URI in request to get the file path.
55
- path += requestUri.substring (_baseUriLength);
58
+ path += requestUri.substring (_baseUriLength);
56
59
}
60
+
57
61
else if (requestUri != _uri) {
58
62
// Base URI points to a file but request doesn't match this URI exactly
59
63
return false ;
60
64
}
61
65
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
+
62
77
File f = _fs.open (path, " r" );
63
78
if (!f)
64
79
return false ;
65
80
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);
67
85
return true ;
68
86
}
69
87
@@ -81,13 +99,15 @@ class StaticRequestHandler : public RequestHandler {
81
99
else if (path.endsWith (" .xml" )) return " text/xml" ;
82
100
else if (path.endsWith (" .pdf" )) return " application/pdf" ;
83
101
else if (path.endsWith (" .zip" )) return " application/zip" ;
102
+ else if (path.endsWith (" .gz" )) return " application/x-gzip" ;
84
103
return " application/octet-stream" ;
85
104
}
86
105
87
106
protected:
88
107
FS _fs;
89
108
String _uri;
90
109
String _path;
110
+ String _cache_header;
91
111
bool _isFile;
92
112
size_t _baseUriLength;
93
113
};
0 commit comments