/
LoadFromFS.cpp
356 lines (291 loc) · 10.8 KB
/
LoadFromFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "../WebServer/LoadFromFS.h"
#include "../CustomBuild/CompiletimeDefines.h"
#include "../Globals/Cache.h"
#include "../Globals/RamTracker.h"
#include "../Helpers/ESPEasy_Storage.h"
#include "../Helpers/Network.h"
#include "../Helpers/Numerical.h"
#include "../WebServer/CustomPage.h"
#include "../WebServer/HTML_wrappers.h"
#include "../WebServer/ESPEasy_WebServer.h"
#include "../Static/WebStaticData.h"
// ********************************************************************************
// Helper functions to match filenames
// ********************************************************************************
bool gzipEncoded(const String& path) {
return path.endsWith(F(".gz"));
}
bool match_ext(const String& path, const __FlashStringHelper *ext) {
return path.endsWith(ext) ||
(gzipEncoded(path) && path.endsWith(concat(ext, F(".gz"))));
}
String fileFromUrl(String path) {
const int questionmarkPos = path.indexOf('?');
if (questionmarkPos >= 0) {
path = path.substring(0, questionmarkPos);
}
// First prepend slash
if (!path.startsWith(F("/"))) {
path = String('/') + path;
}
if (path.endsWith(F("/"))) { path += F("index.htm"); }
#ifdef ESP8266
// Remove leading slash to generate filename from it.
if (path.startsWith(F("/"))) {
path = path.substring(1);
}
#endif // ifdef ESP8266
return path;
}
bool matchFilename(const String& path, const __FlashStringHelper *fname) {
return fileFromUrl(path).equalsIgnoreCase(fileFromUrl(fname));
}
// ********************************************************************************
// Handle embedded files
// ********************************************************************************
bool fileIsEmbedded(const String& path) {
#if defined(EMBED_ESPEASY_DEFAULT_MIN_CSS) || defined(WEBSERVER_EMBED_CUSTOM_CSS)
if (matchFilename(path, F("esp.css"))) {
return true;
}
#endif // if defined(EMBED_ESPEASY_DEFAULT_MIN_CSS) || defined(WEBSERVER_EMBED_CUSTOM_CSS)
#ifdef WEBSERVER_FAVICON
if (matchFilename(path, F("favicon.ico"))) {
return true;
}
#endif // ifdef WEBSERVER_FAVICON
return false;
}
void do_serveEmbedded(const __FlashStringHelper* contentType, PGM_P content, int length, bool serve_inline) {
// Serve using our own Web_StreamingBuffer
// Serving via web_server.send_P may cause memory allocation issues when sending large flash strings.
if (!serve_inline) {
TXBuffer.startStream(contentType, F("*"), 200);
}
TXBuffer.addFlashString(content, length);
if (!serve_inline) {
TXBuffer.endStream();
}
}
void serveEmbedded(const String& path, const __FlashStringHelper* contentType, bool serve_inline) {
#if defined(EMBED_ESPEASY_DEFAULT_MIN_CSS) || defined(WEBSERVER_EMBED_CUSTOM_CSS)
if (matchFilename(path, F("esp.css"))) {
do_serveEmbedded(contentType, (PGM_P)FPSTR(DATA_ESPEASY_DEFAULT_MIN_CSS), -1, serve_inline);
return;
}
#endif // if defined(EMBED_ESPEASY_DEFAULT_MIN_CSS) || defined(WEBSERVER_EMBED_CUSTOM_CSS)
#ifdef WEBSERVER_FAVICON
if (matchFilename(path, F("favicon.ico"))) {
do_serveEmbedded(contentType, (PGM_P)FPSTR(favicon_8b_ico), favicon_8b_ico_len, false);
return;
}
#endif // ifdef WEBSERVER_FAVICON
addLog(LOG_LEVEL_ERROR, concat(F("serveEmbedded failed: "), path));
}
void serve_CSS_inline() {
const __FlashStringHelper* fname = F("esp.css");
addHtml(F("<style>"));
if (fileExists(fname)) {
streamFromFS(fname);
} else {
serveEmbedded(fname, F("text/css"), true);
}
addHtml(F("</style>"));
}
// ********************************************************************************
// Match static files and strip "static_xxx_" prefix
// ********************************************************************************
bool isStaticFile_StripPrefix(String& path) {
// Strip any "static_xxx_" prepended for cache purposes
const int index_static = path.indexOf(F("static_"));
if (index_static != -1) {
const int index_1 = path.indexOf('_');
const int index_2 = path.indexOf('_', index_1 + 1);
if ((index_1 > 0) && (index_2 > index_1)) {
path = fileFromUrl(path.substring(index_2 + 1));
return true;
}
}
if (matchFilename(path, F("favicon.ico"))) {
return true;
}
return false;
}
// ********************************************************************************
// Check whether a static file is requested again
// and therefore should be read from browser cache
// ********************************************************************************
bool reply_304_not_modified(const String& path) {
if (path.endsWith(F("favicon.ico"))) {
// No need in serving 304 for the favicon
return false;
}
const String ifNoneMatch = stripQuotes(web_server.header(F("If-None-Match")));
unsigned int etag_num = 0;
bool res = false;
if (validUIntFromString(ifNoneMatch, etag_num)) {
if (fileExists(path) || fileIsEmbedded(path)) {
// call fileExists first, as this may update Cache.fileCacheClearMoment
if (Cache.fileCacheClearMoment == etag_num) {
// We have a request for the same file we served earlier.
// Reply with a 304 Not Modified
res = true;
}
}
}
#ifndef BUILD_NO_DEBUG
if (res) {
addLog(LOG_LEVEL_INFO, concat(F("Serve 304: "), String(etag_num)) + ' ' + path);
}
#endif // ifndef BUILD_NO_DEBUG
return res;
}
// ********************************************************************************
// Determine HTTP content type
// ********************************************************************************
const __FlashStringHelper* get_ContentType(String& path, bool& mustCheckCredentials)
{
mustCheckCredentials = false;
const __FlashStringHelper *contentType = F("text/plain");
if (path.endsWith(F(".src"))) {
// FIXME TD-er: is this ".src" still used by anyone? Better remove it.
path = path.substring(0, path.lastIndexOf("."));
// Must check credentials, or else someone may download any file by simply adding .src in the GET url.
mustCheckCredentials = true;
}
else if (match_ext(path, F(".html")) ||
match_ext(path, F(".htm"))) { contentType = F("text/html"); }
else if (match_ext(path, F(".css"))) { contentType = F("text/css"); }
else if (match_ext(path, F(".js"))) { contentType = F("application/javascript"); }
else if (match_ext(path, F(".png"))) { contentType = F("image/png"); }
else if (match_ext(path, F(".gif"))) { contentType = F("image/gif"); }
else if (match_ext(path, F(".jpg"))) { contentType = F("image/jpeg"); }
else if (path.endsWith(F(".ico"))) { contentType = F("image/x-icon"); }
else if (path.endsWith(F(".svg"))) { contentType = F("image/svg+xml"); }
else if (path.endsWith(F(".json"))) { contentType = F("application/json"); }
else if (path.endsWith(F(".txt")) ||
path.endsWith(F(".dat"))) {
mustCheckCredentials = true;
contentType = F("application/octet-stream");
} else {
mustCheckCredentials = true;
}
return contentType;
}
// ********************************************************************************
// Web Interface server web file from FS
// ********************************************************************************
bool loadFromFS(String path) {
// path is a deepcopy, since it will be changed here.
#ifndef BUILD_NO_RAM_TRACKER
checkRAM(F("loadFromFS"));
#endif // ifndef BUILD_NO_RAM_TRACKER
statusLED(true);
path = fileFromUrl(path);
// isStaticFile_StripPrefix may alter the path by stripping off the "static_" prefix.
// Thus do this before checking whether a file is present on the file system.
const bool static_file = isStaticFile_StripPrefix(path);
#ifndef BUILD_NO_DEBUG
if (static_file) {
addLog(LOG_LEVEL_INFO, concat(F("static_file: "), path));
}
#endif // ifndef BUILD_NO_DEBUG
const bool fileEmbedded = fileIsEmbedded(path);
if (!fileExists(path) && !fileEmbedded) {
return false;
}
#ifdef WEBSERVER_CUSTOM
if (path.endsWith(F(".esp"))) {
return handle_custom(path);
}
#endif // ifdef WEBSERVER_CUSTOM
bool mustCheckCredentials = false;
const __FlashStringHelper* contentType = get_ContentType(path, mustCheckCredentials);
const bool serve_304 = static_file &&
reply_304_not_modified(path); // Reply with a 304 Not Modified
#ifndef BUILD_NO_DEBUG
if (loglevelActiveFor(LOG_LEVEL_DEBUG)) {
String log = F("HTML : Request file ");
log += path;
addLogMove(LOG_LEVEL_DEBUG, log);
}
#endif // ifndef BUILD_NO_DEBUG
// prevent reloading stuff on every click
if (static_file) {
sendHeader(F("Cache-Control"), F("public, max-age=31536000, immutable"));
// sendHeader(F("Cache-Control"), F("max-age=86400"));
sendHeader(F("Expires"), F("-1"));
if (fileEmbedded && !fileExists(path)) {
sendHeader(F("Last-Modified"), get_build_date_RFC1123());
}
sendHeader(F("Age"), F("100"));
sendHeader(F("ETag"), wrap_String(String(Cache.fileCacheClearMoment) + F("-a"), '"')); // added "-a" to the ETag to
// match the same encoding
} else {
sendHeader(F("Cache-Control"), F("no-cache"));
sendHeader(F("ETag"), F("\"2.0.0\""));
}
sendHeader(F("Vary"), "*");
if (path.endsWith(F(".dat"))) {
sendHeader(F("Content-Disposition"), F("attachment;"));
}
if (serve_304) {
web_server.send(304, String(contentType), EMPTY_STRING);
} else {
if (fileExists(path)) {
fs::File f = tryOpenFile(path.c_str(), "r");
if (!f) {
return false;
}
web_server.streamFile(f, String(contentType));
f.close();
}
if (!fileEmbedded) {
return false;
}
serveEmbedded(path, contentType, false);
}
statusLED(true);
return true;
}
size_t streamFromFS(String path, bool htmlEscape) {
// path is a deepcopy, since it will be changed here.
path = fileFromUrl(path);
statusLED(true);
size_t bytesStreamed = 0;
fs::File f = tryOpenFile(path.c_str(), "r");
if (!f) {
return bytesStreamed;
}
size_t available = f.available();
String escaped;
while (available > 0) {
size_t chunksize = 64;
if (available < chunksize) {
chunksize = available;
}
uint8_t buf[64] = { 0 };
const size_t read = f.read(buf, chunksize);
if (read == chunksize) {
for (size_t i = 0; i < chunksize; ++i) {
const char c = (char)buf[i];
if (htmlEscape && htmlEscapeChar(c, escaped)) {
addHtml(escaped);
} else {
addHtml(c);
}
}
bytesStreamed += read;
available = f.available();
} else {
available = 0;
}
}
// FIXME TD-er: Is this while loop still useful?
while (f.available()) {
addHtml((char)f.read());
}
statusLED(true);
f.close();
return bytesStreamed;
}