-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFSWebServer.cpp
423 lines (399 loc) · 12.8 KB
/
FSWebServer.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
//
//
#define DBG_OUTPUT_PORT Serial
#define DEBUG_WEBSERVER
#include "FSWebServer.h"
#include "DynamicData.h"
#include "Config.h"
ESP8266WebServer server(80);
File fsUploadFile;
String browserMD5="";
static uint32_t updateSize = 0;
//format bytes
String formatBytes(size_t bytes) {
if (bytes < 1024) {
return String(bytes) + "B";
}
else if (bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB";
}
else if (bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB";
}
else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
}
}
String getContentType(String filename) {
if (server.hasArg("download")) return "application/octet-stream";
else if (filename.endsWith(".htm")) return "text/html";
else if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".json")) return "application/json";
else if (filename.endsWith(".png")) return "image/png";
else if (filename.endsWith(".gif")) return "image/gif";
else if (filename.endsWith(".jpg")) return "image/jpeg";
else if (filename.endsWith(".ico")) return "image/x-icon";
else if (filename.endsWith(".xml")) return "text/xml";
else if (filename.endsWith(".pdf")) return "application/x-pdf";
else if (filename.endsWith(".zip")) return "application/x-zip";
else if (filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path) {
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
#endif // DEBUG_WEBSERVER
if (CONNECTION_LED >= 0) {
flashLED(CONNECTION_LED, 1, 25); // Show activity on LED
}
if (path.endsWith("/"))
path += "index.htm";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if (SPIFFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
//DBG_OUTPUT_PORT.printf("File %s exist\n", file.name());
file.close();
return true;
}
#ifdef DEBUG_WEBSERVER
else
DBG_OUTPUT_PORT.printf("Cannot find %s\n", path.c_str());
#endif // DEBUG_WEBSERVER
return false;
}
void handleFileUpload() {
if (server.uri() != "/edit") return;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) filename = "/" + filename;
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("handleFileUpload Name: %s\n", filename.c_str());
#endif // DEBUG_WEBSERVER
fsUploadFile = SPIFFS.open(filename, "w");
filename = String();
}
else if (upload.status == UPLOAD_FILE_WRITE) {
//DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
}
else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile)
fsUploadFile.close();
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("handleFileUpload Size: %u\n", upload.totalSize);
#endif // DEBUG_WEBSERVER
}
}
void handleFileDelete() {
if (server.args() == 0) return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
#endif // DEBUG_WEBSERVER
if (path == "/")
return server.send(500, "text/plain", "BAD PATH");
if (!SPIFFS.exists(path))
return server.send(404, "text/plain", "FileNotFound");
SPIFFS.remove(path);
server.send(200, "text/plain", "");
path = String();
}
void handleFileCreate() {
if (server.args() == 0)
return server.send(500, "text/plain", "BAD ARGS");
String path = server.arg(0);
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
#endif // DEBUG_WEBSERVER
if (path == "/")
return server.send(500, "text/plain", "BAD PATH");
if (SPIFFS.exists(path))
return server.send(500, "text/plain", "FILE EXISTS");
File file = SPIFFS.open(path, "w");
if (file)
file.close();
else
return server.send(500, "text/plain", "CREATE FAILED");
server.send(200, "text/plain", "");
path = String();
}
void handleFileList() {
if (!server.hasArg("dir")) { server.send(500, "text/plain", "BAD ARGS"); return; }
String path = server.arg("dir");
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println("handleFileList: " + path);
#endif // DEBUG_WEBSERVER
Dir dir = SPIFFS.openDir(path);
path = String();
String output = "[";
while (dir.next()) {
File entry = dir.openFile("r");
if (true)//entry.name()!="secret.json") // Do not show secrets
{
if (output != "[")
output += ',';
bool isDir = false;
output += "{\"type\":\"";
output += (isDir) ? "dir" : "file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
output += "\"}";
}
entry.close();
}
output += "]";
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println(output);
#endif // DEBUG_WEBSERVER
server.send(200, "text/json", output);
}
void setUpdateMD5() {
browserMD5 = "";
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("Arg number: %d\n", server.args());
#endif // DEBUG_WEBSERVER
if (server.args() > 0) // Read hash
{
//String temp = "";
for (uint8_t i = 0; i < server.args(); i++) {
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("Arg %s: %s\n", server.argName(i).c_str(), server.arg(i).c_str());
#endif // DEBUG_WEBSERVER
if (server.argName(i) == "md5") {
browserMD5 = urldecode(server.arg(i));
Update.setMD5(browserMD5.c_str());
continue;
}if (server.argName(i) == "size") {
updateSize = server.arg(i).toInt();
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("Update size: %u\n", server.arg(i).toInt());
#endif // DEBUG_WEBSERVER
continue;
}
}
server.send(200, "text/html", "OK --> MD5: "+browserMD5);
}
}
void updateFirmware () {
// handler for the file upload, get's the sketch bytes, and writes
// them through the Update object
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
WiFiUDP::stopAll();
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("Update: %s\n", upload.filename.c_str());
#endif // DEBUG_WEBSERVER
//uint32_t maxSketchSpace = (ESP.getSketchSize() - 0x1000) & 0xFFFFF000;
uint32_t maxSketchSpace = ESP.getSketchSize();
#ifdef DEBUG_WEBSERVER
//uint32_t oldValue = (ESP.getSketchSize() - 0x1000) & 0xFFFFF000;
DBG_OUTPUT_PORT.printf("Max free scketch space: %u\n", maxSketchSpace);
DBG_OUTPUT_PORT.printf("New scketch size: %u\n", updateSize);
//DBG_OUTPUT_PORT.printf("Old value: %u\n", oldValue);
#endif // DEBUG_WEBSERVER
if (browserMD5!= NULL && browserMD5 != "") {
Update.setMD5(browserMD5.c_str());
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.printf("Hash from client: %s\n", browserMD5.c_str());
#endif // DEBUG_WEBSERVER
}
if (!Update.begin(updateSize)) {//start with max available size
#ifdef DEBUG_WEBSERVER
Update.printError(DBG_OUTPUT_PORT);
#endif // DEBUG_WEBSERVER
}
}
else if (upload.status == UPLOAD_FILE_WRITE) {
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.print(".");
#endif // DEBUG_WEBSERVER
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
#ifdef DEBUG_WEBSERVER
Update.printError(DBG_OUTPUT_PORT);
#endif // DEBUG_WEBSERVER
}
}
else if (upload.status == UPLOAD_FILE_END) {
String updateHash;
if (Update.end(true)) { //true to set the size to the current progress
#ifdef DEBUG_WEBSERVER
updateHash = Update.md5String();
DBG_OUTPUT_PORT.printf("Upload finished. Calculated MD5: %s\n", updateHash.c_str());
DBG_OUTPUT_PORT.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
#endif // DEBUG_WEBSERVER
}
else {
#ifdef DEBUG_WEBSERVER
updateHash = Update.md5String();
DBG_OUTPUT_PORT.printf("Upload failed. Calculated MD5: %s\n", updateHash.c_str());
Update.printError(DBG_OUTPUT_PORT);
#endif // DEBUG_WEBSERVER
}
}
else if (upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println("Update was aborted");
#endif // DEBUG_WEBSERVER
}
delay(2);
}
void serverInit() {
//SERVER INIT
//list directory
server.on("/list", HTTP_GET, []() {
if (!checkAuth())
return server.requestAuthentication();
handleFileList();
});
//load editor
server.on("/edit", HTTP_GET, []() {
if (!checkAuth())
return server.requestAuthentication();
if (!handleFileRead("/edit.html"))
server.send(404, "text/plain", "FileNotFound");
});
//create file
server.on("/edit", HTTP_PUT, []() {
if (!checkAuth())
return server.requestAuthentication();
handleFileCreate();
});
//delete file
server.on("/edit", HTTP_DELETE, []() {
if (!checkAuth())
return server.requestAuthentication();
handleFileDelete();
});
//first callback is called after the request has ended with all parsed arguments
//second callback handles file uploads at that location
server.on("/edit", HTTP_POST, []() { server.send(200, "text/plain", ""); }, handleFileUpload);
server.on("/admin/generalvalues", send_general_configuration_values_html);
server.on("/admin/values", send_network_configuration_values_html);
server.on("/admin/connectionstate", send_connection_state_values_html);
server.on("/admin/infovalues", send_information_values_html);
server.on("/admin/ntpvalues", send_NTP_configuration_values_html);
server.on("/config.html", []() {
if (!checkAuth())
return server.requestAuthentication();
send_network_configuration_html();
});
server.on("/general.html", []() {
if (!checkAuth())
return server.requestAuthentication();
send_general_configuration_html();
});
server.on("/ntp.html", []() {
if (!checkAuth())
return server.requestAuthentication();
send_NTP_configuration_html();
});
//server.on("/admin/devicename", send_devicename_value_html);
server.on("/admin", HTTP_GET, []() {
if (!checkAuth())
return server.requestAuthentication();
if (!handleFileRead("/admin.html"))
server.send(404, "text/plain", "FileNotFound");
});
server.on("/admin/restart", []() {
if (!checkAuth())
return server.requestAuthentication();
restart_esp();
});
server.on("/system.html", []() {
if (!checkAuth())
return server.requestAuthentication();
send_wwwauth_configuration_html();
});
server.on("/admin/wwwauth", []() {
if (!checkAuth())
return server.requestAuthentication();
send_wwwauth_configuration_values_html();
});
server.on("/update/updatepossible", []() {
if (!checkAuth())
return server.requestAuthentication();
send_update_firmware_values_html();
});
server.on("/setmd5", [&]() {
if (!checkAuth())
return server.requestAuthentication();
//DBG_OUTPUT_PORT.println("md5?");
setUpdateMD5();
});
server.on("/update", HTTP_GET, []() {
if (!checkAuth())
return server.requestAuthentication();
if (!handleFileRead("/update.html"))
server.send(404, "text/plain", "FileNotFound");
});
server.on("/update", HTTP_POST, [&]() {
if (!checkAuth())
return server.requestAuthentication();
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", (Update.hasError()) ? "FAIL" : "<META http-equiv=\"refresh\" content=\"15;URL=/update\">Update correct. Restarting...");
ESP.restart();
}, updateFirmware);
//called when the url is not defined here
//use it to load content from SPIFFS
server.onNotFound( []() {
if (!checkAuth())
return server.requestAuthentication();
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
if (!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
#ifndef HIDE_SECRET
server.on(SECRET_FILE, HTTP_GET, []() {
if (!checkAuth())
return server.requestAuthentication();
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(403, "text/plain", "Forbidden");
});
#endif // HIDE_SECRET
#ifndef HIDE_CONFIG
server.on(CONFIG_FILE, HTTP_GET, []() {
if (!checkAuth())
return server.requestAuthentication();
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(403, "text/plain", "Forbidden");
});
#endif // HIDE_CONFIG
//get heap status, analog input value and all GPIO statuses in one json call
server.on("/all", HTTP_GET, []() {
String json = "{";
json += "\"heap\":" + String(ESP.getFreeHeap());
json += ", \"analog\":" + String(analogRead(A0));
json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}";
server.send(200, "text/json", json);
json = String();
});
server.begin();
//httpUpdater.setup(&server,httpAuth.wwwUsername,httpAuth.wwwPassword);
#ifdef DEBUG_WEBSERVER
DBG_OUTPUT_PORT.println("HTTP server started");
#endif // DEBUG_WEBSERVER
}
boolean checkAuth() {
if (!httpAuth.auth) {
return true;
}
else
{
return server.authenticate(httpAuth.wwwUsername.c_str(), httpAuth.wwwPassword.c_str());
}
}