This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.h
314 lines (305 loc) · 10.5 KB
/
web.h
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
#pragma once
#include <ESP8266WebServer.h>
#include <detail/RequestHandlersImpl.h> // for StaticRequestHandler::getContentType(path);
void handleGenericFile();
void handlePrivate();
void listFile();
uint32_t restartESP();
uint32_t formatSPIFFS();
void handleReboot();
void handleFormat();
void handleFile();
void handleFileUpload();
#ifdef ESP8266
#undef F
#define F String
#endif
class Web : public ESP8266WebServer {
public:
Web(uint16_t port) : ESP8266WebServer(port) {
begin();
onNotFound(handleGenericFile);
}
/*
String getContentType(String filename){
if(hasArg(F("download"))) return F("application/octet-stream");
else if(filename.endsWith(F(".htm"))) return F("text/html");
else if(filename.endsWith(F(".html"))) return F("text/html");
else if(filename.endsWith(F(".css"))) return F("text/css");
else if(filename.endsWith(F(".js"))) return F("application/javascript");
else if(filename.endsWith(F(".png"))) return F("image/png");
else if(filename.endsWith(F(".gif"))) return F("image/gif");
else if(filename.endsWith(F(".jpg"))) return F("image/jpeg");
else if(filename.endsWith(F(".ico"))) return F("image/x-icon");
else if(filename.endsWith(F(".xml"))) return F("text/xml");
else if(filename.endsWith(F(".pdf"))) return F("application/x-pdf");
else if(filename.endsWith(F(".zip"))) return F("application/x-zip");
else if(filename.endsWith(F(".gz"))) return F("application/x-gzip");
return "text/plain";
}
*/
bool handleFileRead(String path){
if(path.endsWith(F("/"))) path += F("index.html");
String contentType = StaticRequestHandler::getContentType(path);
String pathWithGz = path + F(".gz");
if(SPIFFS.exists(pathWithGz))
path += F(".gz");
if(SPIFFS.exists(path)){
sendHeader("Connection", "close");
sendHeader("Cache-Control", "no-store, must-revalidate");
sendHeader("Access-Control-Allow-Origin", "*");
Serial.println(1);
File file = SPIFFS.open(path, "r");
Serial.println(2);
size_t sent = streamFile(file, contentType);
Serial.println(sent);
file.close();
Serial.println(4);
return true;
}
return false;
}
};
Web* web;
uint32_t webLoop() {
web->handleClient();
return 100;
}
uint32_t webInit() {
web = new Web(80);
web->on("/private", handlePrivate);
web->on("/list", listFile);
web->on("/reboot", handleReboot);
web->on("/format", handleFormat);
web->on("/edit", HTTP_POST, handleFile, handleFileUpload);
event.webReady++;
taskAdd(webLoop);
Serial.print("[HTTP started]");
return RUN_DELETE;
}
void handleGenericFile() {
BUSY
if(!web->handleFileRead(web->uri()))
web->send(404, "text/plain", "FileNotFound");
IDLE
}
void handlePrivate() {
BUSY
char data[400];
char* xml = ("<?xml version = \"1.0\" encoding=\"UTF-8\" ?><ctrl><private><heap>%d</heap><rssi>%d</rssi><uptime>%ld</uptime></private></ctrl>");
//sprintf(data, ("<?xml version = \"1.0\" encoding=\"UTF-8\" ?><ctrl><private><heap>%d</heap><rssi>%d</rssi><uptime>%ld</uptime></private></ctrl>"), ESP.getFreeHeap(), WiFi.RSSI(),(uint32_t)millis()/1000);
sprintf(data, xml, ESP.getFreeHeap(), WiFi.RSSI(),millis()/1000);
web->sendHeader("Connection", "close");
web->sendHeader("Cache-Control", "no-store, must-revalidate");
web->send(200, "text/xml", data);
IDLE
}
void listFile() {
if(!web->authenticate(adminUsername.c_str(), adminPassword.c_str())) {
return web->requestAuthentication();
}
String output = F("<html><head><meta charset='utf-8'>\
<title>WiFiSocket - Maintains</title>\
\
<style>\
.well{background-image:-webkit-linear-gradient(top,#e8e8e8 0,#f5f5f5 100%);\
background-image:-o-linear-gradient(top,#e8e8e8 0,#f5f5f5 100%);\
background-image:-webkit-gradient(linear,left top,left bottom,from(#e8e8e8),to(#f5f5f5));\
background-image:linear-gradient(to bottom,#e8e8e8 0,#f5f5f5 100%);\
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffe8e8e8', endColorstr='#fff5f5f5', GradientType=0);\
background-repeat:repeat-x;\
border-color:#dcdcdc;\
-webkit-box-shadow:inset 0 1px 3px rgba(0,0,0,.05),0 1px 0 rgba(255,255,255,.1);\
box-shadow:inset 0 1px 3px rgba(0,0,0,.05),0 1px 0 rgba(255,255,255,.1)}\
.well{min-height:20px;padding:19px;margin-bottom:20px;background-color:#f5f5f5;border:1px solid #e3e3e3;border-radius:4px;-webkit-box-shadow:inset 0 1px 1px rgba(0,0,0,.05);box-shadow:inset 0 1px 1px rgba(0,0,0,.05)}\
.well blockquote{border-color:#ddd;border-color:rgba(0,0,0,.15)}\
.container{padding-right:15px;padding-left:15px;margin-right:auto;margin-left:auto}\
.col-xs-9{position:relative;min-height:1px;padding-right:15px;padding-left:15px}\
body{font-family:\"Helvetica Neue\",Helvetica,Arial,sans-serif;font-size:14px;line-height:1.42857143;color:#333;background-color:#fff}\
h4,h4{font-size:18px}\
</style>\
\
</head>\
<body>\
\
<div class='col-xs-9'><h4>Wifi Socket Control - Maintains</h4></div>\
<div class='container'><div class='well'>\
<b>Network settings</b>\
<hr>\
<form method='POST' action='/net' enctype='multipart/form-data'>\
<table>\
<tr><td>IP</td><td>");
output += WiFi.localIP().toString();
output += F("</td></tr>\
<tr><td>Unit Name</td><td><input name=\"name\" value=\"");
output += name;
output += F("\"></td></tr>\
<tr><td>NTP Server 1</td><td><input name=\"ntp1\" value=\"");
output += ntp[0];
output += F("\"></td></tr>\
<tr><td>NTP Server 2</td><td><input name=\"ntp2\" value=\"");
output += ntp[1];
output += F("\"></td></tr>\
<tr><td>NTP Server 3</td><td><input name=\"ntp3\" value=\"");
output += ntp[2];
output += F("\"></td></tr>\
<tr><td>TimeZone</td><td><select name=\"tz\">");
for (int8_t t = -11; t <= 11; t++) {
output += F("<option");
if (t == tz) {
output += F(" selected");
}
output += F(">");
output += t;
output += F("</option>");
}
output += F("</select></td></tr>\
</table>\
<input type='submit' value='Apply'>\
</table>\
</form>\
</div></div>\
\
<div class='container'><div class='well'>\
<b>Local file system</b>\
<hr>\
<form method='POST' action='/edit' enctype='multipart/form-data'>\
Upload file to local filesystem:<br>\
<input type='file' name='update'>\
<input type='submit' value='Upload file'>\
</form>");
String path = web->hasArg("dir")?web->arg("dir"):"/";
#ifdef ESP8266
Dir dir = SPIFFS.openDir(path);
while(dir.next()){
File entry = dir.openFile("r");
#else
File dir = SPIFFS.open(path);
File entry;
while (entry = dir.openNextFile()) {
#endif
String filename = String(entry.name());
output += F("<br>");
output += F("<a href='");
output += filename;
output += F("'>");
output += filename;
output += F("</a> <a href='/delete?file=");
output += filename;
output += F("'><font color=red>delete</font></a>");
output += F("<br>");
entry.close();
}
#ifndef ESP8266
output += F("Used/Total ");
output += SPIFFS.usedBytes();
output += F("/");
output += SPIFFS.totalBytes();
#endif
output += F("</div></div>\
\
<div class='container'><div class='well'>\
<b>Firmware update</b>\
<hr>\
Current version: ");
output += VERSION;
output += F("\
<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update' accept='.tar'><input type='submit' value='Update'></form>\
</div></div>\
\
<div class='container'><div class='well'>\
<b>Resets</b>\
<hr>\
<form method='POST' action='/list' enctype='multipart/form-data'>\
<input type='button' value='Reboot' onClick='if(confirm(\'Reboot device?\')) window.location=\'/reboot\';return true;'><br>\
<input type='button' value='Format filesystem' onClick='if(confirm(\'Format file system and destroy all stored data?\')) window.location=\'/format\';return true;'></form>\
</div></div>\
</body><html>");
web->sendHeader("Connection", "close");
web->sendHeader("Cache-Control", "no-store, must-revalidate");
web->sendHeader("Access-Control-Allow-Origin", "*");
web->send(200, "text/html", output);
}
// Delete file callback
void handleDelete() {
if(!web->authenticate(adminUsername.c_str(), adminPassword.c_str())) {
return web->requestAuthentication();
}
web->sendHeader("Connection", "close");
web->sendHeader("Cache-Control", "no-store, must-revalidate");
web->sendHeader("Refresh", "5; url=/list");
String path;
if(web->args() != 0) {
path = web->arg(0);
if(path != "/" && SPIFFS.exists(path)) {
if (SPIFFS.remove(path)) {
web->send(200, "text/plain", "OK");
IDLE
return;
} else {
web->send(404, "text/plain", "FileNotFound");
IDLE
return;
}
}
}
web->send(500, "text/plain", "ERROR");
IDLE
}
void handleReboot() {
if(!web->authenticate(adminUsername.c_str(), adminPassword.c_str())) {
return web->requestAuthentication();
}
web->sendHeader("Connection", "close");
web->sendHeader("Cache-Control", "no-store, must-revalidate");
web->sendHeader("Refresh", "7; url=/");
taskAddWithDelay(restartESP, 500);
web->send(200, "text/plain", "Rebooting...");
}
void handleFormat() {
if(!web->authenticate(adminUsername.c_str(), adminPassword.c_str())) {
return web->requestAuthentication();
}
web->sendHeader("Connection", "close");
web->sendHeader("Cache-Control", "no-store, must-revalidate");
web->sendHeader("Refresh", "15; url=/list");
taskAdd(formatSPIFFS);
web->send(200, "text/plain", "Formatting...");
}
void handleFile() {
if(!web->authenticate(adminUsername.c_str(), adminPassword.c_str())) {
return web->requestAuthentication();
}
web->sendHeader("Connection", "close");
web->sendHeader("Cache-Control", "no-store, must-revalidate");
web->sendHeader("Access-Control-Allow-Origin", "*");
web->sendHeader("Refresh", "5; url=/list");
web->send(200, "text/plain", "OK");
}
File fsUploadFile;
void handleFileUpload(){
if(!web->authenticate(adminUsername.c_str(), adminPassword.c_str())) {
return web->requestAuthentication();
}
if(web->uri() != "/edit") return;
BUSY
HTTPUpload& upload = web->upload();
if(upload.status == UPLOAD_FILE_START){
String filename = upload.filename;
if(!filename.startsWith("/")) filename = "/"+filename;
fsUploadFile = SPIFFS.open(filename, "w");
filename = String();
} else if(upload.status == UPLOAD_FILE_WRITE){
if(fsUploadFile) {
Serial.print(".");
fsUploadFile.write(upload.buf, upload.currentSize);
} else {
Serial.print("X");
}
} else if(upload.status == UPLOAD_FILE_END){
Serial.println(upload.currentSize);
if(fsUploadFile)
fsUploadFile.close();
}
IDLE
}