2021-03-25 20:00:08 +01:00
|
|
|
#include "wled.h"
|
|
|
|
|
2022-08-04 16:15:49 +02:00
|
|
|
#include "html_ui.h"
|
|
|
|
#ifdef WLED_ENABLE_SIMPLE_UI
|
|
|
|
#include "html_simple.h"
|
|
|
|
#endif
|
|
|
|
#include "html_settings.h"
|
|
|
|
#include "html_other.h"
|
2023-01-20 14:40:45 +01:00
|
|
|
#ifdef WLED_ENABLE_PIXART
|
|
|
|
#include "html_pixart.h"
|
|
|
|
#endif
|
2022-08-04 16:15:49 +02:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
/*
|
|
|
|
* Integrated HTTP web server page declarations
|
|
|
|
*/
|
|
|
|
|
2021-08-21 18:12:38 +02:00
|
|
|
bool handleIfNoneMatchCacheHeader(AsyncWebServerRequest* request);
|
|
|
|
void setStaticContentCacheHeaders(AsyncWebServerResponse *response);
|
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
// define flash strings once (saves flash memory)
|
|
|
|
static const char s_redirecting[] PROGMEM = "Redirecting...";
|
|
|
|
static const char s_content_enc[] PROGMEM = "Content-Encoding";
|
|
|
|
static const char s_unlock_ota [] PROGMEM = "Please unlock OTA in security settings!";
|
2022-05-01 22:09:40 +02:00
|
|
|
static const char s_unlock_cfg [] PROGMEM = "Please unlock settings using PIN code!";
|
2022-04-27 12:31:47 +02:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
//Is this an IP?
|
|
|
|
bool isIp(String str) {
|
|
|
|
for (size_t i = 0; i < str.length(); i++) {
|
|
|
|
int c = str.charAt(i);
|
|
|
|
if (c != '.' && (c < '0' || c > '9')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:37:28 +01:00
|
|
|
void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
|
2022-05-01 22:09:40 +02:00
|
|
|
if (!correctPIN) {
|
|
|
|
if (final) request->send(500, "text/plain", FPSTR(s_unlock_cfg));
|
2021-09-21 23:37:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-08-10 17:11:17 +02:00
|
|
|
if (!index) {
|
2022-10-21 01:25:39 +02:00
|
|
|
String finalname = filename;
|
|
|
|
if (finalname.charAt(0) != '/') {
|
2022-10-24 18:49:02 +02:00
|
|
|
finalname = '/' + finalname; // prepend slash if missing
|
2022-10-21 01:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
request->_tempFile = WLED_FS.open(finalname, "w");
|
2022-10-24 18:49:02 +02:00
|
|
|
DEBUG_PRINT(F("Uploading "));
|
2022-10-21 01:25:39 +02:00
|
|
|
DEBUG_PRINTLN(finalname);
|
2022-10-24 19:12:27 +02:00
|
|
|
if (finalname.equals("/presets.json")) presetsModifiedTime = toki.second();
|
2021-06-04 18:25:33 +02:00
|
|
|
}
|
|
|
|
if (len) {
|
|
|
|
request->_tempFile.write(data,len);
|
|
|
|
}
|
2021-08-10 17:11:17 +02:00
|
|
|
if (final) {
|
2021-06-04 18:25:33 +02:00
|
|
|
request->_tempFile.close();
|
2022-10-24 18:49:02 +02:00
|
|
|
if (filename.indexOf(F("cfg.json")) >= 0) { // check for filename with or without slash
|
2022-03-01 23:37:28 +01:00
|
|
|
doReboot = true;
|
|
|
|
request->send(200, "text/plain", F("Configuration restore successful.\nRebooting..."));
|
|
|
|
} else
|
|
|
|
request->send(200, "text/plain", F("File Uploaded!"));
|
2021-08-11 21:28:31 +02:00
|
|
|
cacheInvalidate++;
|
2021-06-04 18:25:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 23:37:28 +01:00
|
|
|
void createEditHandler(bool enable) {
|
2022-04-27 12:31:47 +02:00
|
|
|
if (editHandler != nullptr) server.removeHandler(editHandler);
|
2022-03-01 23:37:28 +01:00
|
|
|
if (enable) {
|
|
|
|
#ifdef WLED_ENABLE_FS_EDITOR
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
editHandler = &server.addHandler(new SPIFFSEditor(WLED_FS));//http_username,http_password));
|
|
|
|
#else
|
|
|
|
editHandler = &server.addHandler(new SPIFFSEditor("","",WLED_FS));//http_username,http_password));
|
|
|
|
#endif
|
|
|
|
#else
|
2022-12-18 18:37:05 +01:00
|
|
|
editHandler = &server.on("/edit", HTTP_GET, [](AsyncWebServerRequest *request){
|
2022-03-01 23:37:28 +01:00
|
|
|
serveMessage(request, 501, "Not implemented", F("The FS editor is disabled in this build."), 254);
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
} else {
|
2022-04-27 12:31:47 +02:00
|
|
|
editHandler = &server.on("/edit", HTTP_ANY, [](AsyncWebServerRequest *request){
|
2022-05-01 22:09:40 +02:00
|
|
|
serveMessage(request, 500, "Access Denied", FPSTR(s_unlock_cfg), 254);
|
2022-03-01 23:37:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
bool captivePortal(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (ON_STA_FILTER(request)) return false; //only serve captive in AP mode
|
|
|
|
String hostH;
|
|
|
|
if (!request->hasHeader("Host")) return false;
|
|
|
|
hostH = request->getHeader("Host")->value();
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
if (!isIp(hostH) && hostH.indexOf("wled.me") < 0 && hostH.indexOf(cmDNS) < 0) {
|
|
|
|
DEBUG_PRINTLN("Captive portal");
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse(302);
|
|
|
|
response->addHeader(F("Location"), F("http://4.3.2.1"));
|
|
|
|
request->send(response);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initServer()
|
|
|
|
{
|
|
|
|
//CORS compatiblity
|
|
|
|
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Origin"), "*");
|
|
|
|
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Methods"), "*");
|
|
|
|
DefaultHeaders::Instance().addHeader(F("Access-Control-Allow-Headers"), "*");
|
|
|
|
|
2021-12-06 20:53:09 +01:00
|
|
|
#ifdef WLED_ENABLE_WEBSOCKETS
|
|
|
|
server.on("/liveview", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_liveviewws, PAGE_liveviewws_length);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-12-06 20:53:09 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
//request->send_P(200, "text/html", PAGE_liveviewws);
|
|
|
|
});
|
2022-08-03 14:23:24 +02:00
|
|
|
#ifndef WLED_DISABLE_2D
|
2022-07-26 11:08:26 +02:00
|
|
|
server.on("/liveview2D", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_liveviewws2D, PAGE_liveviewws2D_length);
|
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
//request->send_P(200, "text/html", PAGE_liveviewws);
|
|
|
|
});
|
2022-08-03 14:23:24 +02:00
|
|
|
#endif
|
2021-12-06 20:53:09 +01:00
|
|
|
#else
|
|
|
|
server.on("/liveview", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_liveview, PAGE_liveview_length);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-12-06 20:53:09 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
//request->send_P(200, "text/html", PAGE_liveview);
|
|
|
|
});
|
|
|
|
#endif
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
//settings page
|
|
|
|
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveSettings(request);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
// "/settings/settings.js&p=x" request also handled by serveSettings()
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2022-03-01 23:37:28 +01:00
|
|
|
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
2022-04-27 12:31:47 +02:00
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", PAGE_settingsCss, PAGE_settingsCss_length);
|
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2022-03-01 23:37:28 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if(!handleFileRead(request, "/favicon.ico"))
|
|
|
|
{
|
|
|
|
request->send_P(200, "image/x-icon", favicon, 156);
|
|
|
|
}
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/sliders", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveIndex(request);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/welcome", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveSettings(request);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/reset", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveMessage(request, 200,F("Rebooting now..."),F("Please wait ~10 seconds..."),129);
|
|
|
|
doReboot = true;
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/settings", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
serveSettings(request, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.on("/json", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveJson(request);
|
|
|
|
});
|
|
|
|
|
|
|
|
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request) {
|
|
|
|
bool verboseResponse = false;
|
2021-05-11 01:11:16 +02:00
|
|
|
bool isConfig = false;
|
2021-11-03 14:52:22 +01:00
|
|
|
|
2022-01-01 12:52:50 +01:00
|
|
|
if (!requestJSONBufferLock(14)) return;
|
|
|
|
|
|
|
|
DeserializationError error = deserializeJson(doc, (uint8_t*)(request->_tempObject));
|
|
|
|
JsonObject root = doc.as<JsonObject>();
|
|
|
|
if (error || root.isNull()) {
|
2021-11-12 23:33:10 +01:00
|
|
|
releaseJSONBufferLock();
|
2022-01-01 12:52:50 +01:00
|
|
|
request->send(400, "application/json", F("{\"error\":9}"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const String& url = request->url();
|
|
|
|
isConfig = url.indexOf("cfg") > -1;
|
|
|
|
if (!isConfig) {
|
2022-06-01 22:11:25 +02:00
|
|
|
/*
|
2022-01-01 12:52:50 +01:00
|
|
|
#ifdef WLED_DEBUG
|
|
|
|
DEBUG_PRINTLN(F("Serialized HTTP"));
|
|
|
|
serializeJson(root,Serial);
|
|
|
|
DEBUG_PRINTLN();
|
|
|
|
#endif
|
2022-06-01 22:11:25 +02:00
|
|
|
*/
|
2022-01-01 12:52:50 +01:00
|
|
|
verboseResponse = deserializeState(root);
|
|
|
|
} else {
|
|
|
|
verboseResponse = deserializeConfig(root); //use verboseResponse to determine whether cfg change should be saved immediately
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
2022-01-01 12:52:50 +01:00
|
|
|
releaseJSONBufferLock();
|
|
|
|
|
2021-05-11 01:11:16 +02:00
|
|
|
if (verboseResponse) {
|
|
|
|
if (!isConfig) {
|
2021-06-19 23:16:40 +02:00
|
|
|
serveJson(request); return; //if JSON contains "v"
|
2021-05-11 01:11:16 +02:00
|
|
|
} else {
|
2022-09-14 22:28:06 +02:00
|
|
|
doSerializeConfig = true; //serializeConfig(); //Save new settings to FS
|
2021-05-11 01:11:16 +02:00
|
|
|
}
|
2023-01-06 09:24:29 +01:00
|
|
|
}
|
2021-03-25 20:00:08 +01:00
|
|
|
request->send(200, "application/json", F("{\"success\":true}"));
|
2023-04-30 17:30:36 +02:00
|
|
|
}, JSON_BUFFER_SIZE);
|
2021-03-25 20:00:08 +01:00
|
|
|
server.addHandler(handler);
|
|
|
|
|
|
|
|
server.on("/version", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(200, "text/plain", (String)VERSION);
|
2021-12-06 20:53:09 +01:00
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/uptime", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(200, "text/plain", (String)millis());
|
2021-12-06 20:53:09 +01:00
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/freeheap", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(200, "text/plain", (String)ESP.getFreeHeap());
|
2021-12-06 20:53:09 +01:00
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/u", HTTP_GET, [](AsyncWebServerRequest *request){
|
2021-12-06 20:53:09 +01:00
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_usermod, PAGE_usermod_length);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-12-06 20:53:09 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
//request->send_P(200, "text/html", PAGE_usermod);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2022-02-20 22:24:11 +01:00
|
|
|
//Deprecated, use of /json/state and presets recommended instead
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/url", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
URL_response(request);
|
2021-12-06 20:53:09 +01:00
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/teapot", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveMessage(request, 418, F("418. I'm a teapot."), F("(Tangible Embedded Advanced Project Of Twinkling)"), 254);
|
2021-12-06 20:53:09 +01:00
|
|
|
});
|
2021-06-04 18:25:33 +02:00
|
|
|
|
|
|
|
server.on("/upload", HTTP_POST, [](AsyncWebServerRequest *request) {},
|
|
|
|
[](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data,
|
|
|
|
size_t len, bool final) {handleUpload(request, filename, index, data, len, final);}
|
|
|
|
);
|
|
|
|
|
2022-03-31 21:44:11 +02:00
|
|
|
#ifdef WLED_ENABLE_SIMPLE_UI
|
2021-08-21 18:12:38 +02:00
|
|
|
server.on("/simple.htm", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (handleFileRead(request, "/simple.htm")) return;
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_simple, PAGE_simple_L);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-08-21 18:12:38 +02:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
});
|
2022-03-31 21:44:11 +02:00
|
|
|
#endif
|
|
|
|
|
2021-11-30 16:27:55 +01:00
|
|
|
server.on("/iro.js", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", iroJs, iroJs_length);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-11-30 16:27:55 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-11-30 16:27:55 +01:00
|
|
|
server.on("/rangetouch.js", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", rangetouchJs, rangetouchJs_length);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-11-30 16:27:55 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2022-05-01 22:09:40 +02:00
|
|
|
createEditHandler(correctPIN);
|
2022-03-01 23:37:28 +01:00
|
|
|
|
|
|
|
#ifndef WLED_DISABLE_OTA
|
2022-04-27 12:31:47 +02:00
|
|
|
//init ota page
|
|
|
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (otaLock) {
|
|
|
|
serveMessage(request, 500, "Access Denied", FPSTR(s_unlock_ota), 254);
|
|
|
|
} else
|
|
|
|
serveSettings(request); // checks for "upd" in URL and handles PIN
|
|
|
|
});
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
if (!correctPIN) {
|
|
|
|
serveSettings(request, true); // handle PIN page POST request
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Update.hasError() || otaLock) {
|
|
|
|
serveMessage(request, 500, F("Update failed!"), F("Please check your file and retry!"), 254);
|
|
|
|
} else {
|
2023-01-06 09:24:29 +01:00
|
|
|
serveMessage(request, 200, F("Update successful!"), F("Rebooting..."), 131);
|
2022-04-27 12:31:47 +02:00
|
|
|
doReboot = true;
|
|
|
|
}
|
|
|
|
},[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
|
|
|
|
if (!correctPIN || otaLock) return;
|
|
|
|
if(!index){
|
|
|
|
DEBUG_PRINTLN(F("OTA Update Start"));
|
2022-05-24 13:45:35 +02:00
|
|
|
WLED::instance().disableWatchdog();
|
2022-06-11 00:50:29 +02:00
|
|
|
usermods.onUpdateBegin(true); // notify usermods that update is about to begin (some may require task de-init)
|
2022-04-27 12:31:47 +02:00
|
|
|
lastEditTime = millis(); // make sure PIN does not lock during update
|
|
|
|
#ifdef ESP8266
|
|
|
|
Update.runAsync(true);
|
|
|
|
#endif
|
|
|
|
Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
|
|
|
|
}
|
|
|
|
if(!Update.hasError()) Update.write(data, len);
|
|
|
|
if(final){
|
|
|
|
if(Update.end(true)){
|
|
|
|
DEBUG_PRINTLN(F("Update Success"));
|
2022-03-01 23:37:28 +01:00
|
|
|
} else {
|
2022-04-27 12:31:47 +02:00
|
|
|
DEBUG_PRINTLN(F("Update Failed"));
|
2022-06-11 00:50:29 +02:00
|
|
|
usermods.onUpdateBegin(false); // notify usermods that update has failed (some may require task init)
|
2022-07-22 14:34:02 +02:00
|
|
|
WLED::instance().enableWatchdog();
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
2022-04-27 12:31:47 +02:00
|
|
|
}
|
|
|
|
});
|
2022-03-01 23:37:28 +01:00
|
|
|
#else
|
|
|
|
server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveMessage(request, 501, "Not implemented", F("OTA updating is disabled in this build."), 254);
|
|
|
|
});
|
|
|
|
#endif
|
2021-03-25 20:00:08 +01:00
|
|
|
|
|
|
|
|
2021-03-28 17:15:26 +02:00
|
|
|
#ifdef WLED_ENABLE_DMX
|
|
|
|
server.on("/dmxmap", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send_P(200, "text/html", PAGE_dmxmap , dmxProcessor);
|
|
|
|
});
|
|
|
|
#else
|
|
|
|
server.on("/dmxmap", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
serveMessage(request, 501, "Not implemented", F("DMX support is not enabled in this build."), 254);
|
|
|
|
});
|
|
|
|
#endif
|
2021-03-25 20:00:08 +01:00
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (captivePortal(request)) return;
|
|
|
|
serveIndexOrWelcome(request);
|
|
|
|
});
|
|
|
|
|
2023-01-20 14:40:45 +01:00
|
|
|
#ifdef WLED_ENABLE_PIXART
|
|
|
|
server.on("/pixart.htm", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
if (handleFileRead(request, "/pixart.htm")) return;
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_pixart, PAGE_pixart_L);
|
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
#ifdef WLED_ENABLE_WEBSOCKETS
|
|
|
|
server.addHandler(&ws);
|
|
|
|
#endif
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
//called when the url is not defined here, ajax-in; get-settings
|
|
|
|
server.onNotFound([](AsyncWebServerRequest *request){
|
|
|
|
DEBUG_PRINTLN("Not-Found HTTP call:");
|
|
|
|
DEBUG_PRINTLN("URI: " + request->url());
|
|
|
|
if (captivePortal(request)) return;
|
|
|
|
|
|
|
|
//make API CORS compatible
|
|
|
|
if (request->method() == HTTP_OPTIONS)
|
|
|
|
{
|
2021-05-27 11:09:57 +02:00
|
|
|
AsyncWebServerResponse *response = request->beginResponse(200);
|
|
|
|
response->addHeader(F("Access-Control-Max-Age"), F("7200"));
|
|
|
|
request->send(response);
|
|
|
|
return;
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
if(handleSet(request, request->url())) return;
|
|
|
|
#ifndef WLED_DISABLE_ALEXA
|
|
|
|
if(espalexa.handleAlexaApiCall(request)) return;
|
|
|
|
#endif
|
|
|
|
if(handleFileRead(request, request->url())) return;
|
2021-12-06 20:53:09 +01:00
|
|
|
AsyncWebServerResponse *response = request->beginResponse_P(404, "text/html", PAGE_404, PAGE_404_length);
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-12-06 20:53:09 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
//request->send_P(404, "text/html", PAGE_404);
|
2021-03-25 20:00:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void serveIndexOrWelcome(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (!showWelcomePage){
|
|
|
|
serveIndex(request);
|
|
|
|
} else {
|
|
|
|
serveSettings(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool handleIfNoneMatchCacheHeader(AsyncWebServerRequest* request)
|
|
|
|
{
|
|
|
|
AsyncWebHeader* header = request->getHeader("If-None-Match");
|
|
|
|
if (header && header->value() == String(VERSION)) {
|
|
|
|
request->send(304);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setStaticContentCacheHeaders(AsyncWebServerResponse *response)
|
|
|
|
{
|
2021-08-10 21:52:07 +02:00
|
|
|
char tmp[12];
|
2021-08-11 21:28:31 +02:00
|
|
|
// https://medium.com/@codebyamir/a-web-developers-guide-to-browser-caching-cc41f3b73e7c
|
2021-03-28 17:15:26 +02:00
|
|
|
#ifndef WLED_DEBUG
|
2021-07-26 00:10:36 +02:00
|
|
|
//this header name is misleading, "no-cache" will not disable cache,
|
|
|
|
//it just revalidates on every load using the "If-None-Match" header with the last ETag value
|
2021-03-21 02:40:12 +01:00
|
|
|
response->addHeader(F("Cache-Control"),"no-cache");
|
2021-03-28 17:15:26 +02:00
|
|
|
#else
|
|
|
|
response->addHeader(F("Cache-Control"),"no-store,max-age=0"); // prevent caching if debug build
|
|
|
|
#endif
|
2021-08-10 21:52:07 +02:00
|
|
|
sprintf_P(tmp, PSTR("%8d-%02x"), VERSION, cacheInvalidate);
|
|
|
|
response->addHeader(F("ETag"), tmp);
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void serveIndex(AsyncWebServerRequest* request)
|
|
|
|
{
|
|
|
|
if (handleFileRead(request, "/index.htm")) return;
|
|
|
|
|
|
|
|
if (handleIfNoneMatchCacheHeader(request)) return;
|
|
|
|
|
2021-08-10 17:11:17 +02:00
|
|
|
AsyncWebServerResponse *response;
|
2022-03-31 21:44:11 +02:00
|
|
|
#ifdef WLED_ENABLE_SIMPLE_UI
|
2021-08-10 17:11:17 +02:00
|
|
|
if (simplifiedUI)
|
|
|
|
response = request->beginResponse_P(200, "text/html", PAGE_simple, PAGE_simple_L);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L);
|
2021-03-25 20:00:08 +01:00
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-03-25 20:00:08 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String msgProcessor(const String& var)
|
|
|
|
{
|
|
|
|
if (var == "MSG") {
|
|
|
|
String messageBody = messageHead;
|
|
|
|
messageBody += F("</h2>");
|
|
|
|
messageBody += messageSub;
|
|
|
|
uint32_t optt = optionType;
|
|
|
|
|
|
|
|
if (optt < 60) //redirect to settings after optionType seconds
|
|
|
|
{
|
|
|
|
messageBody += F("<script>setTimeout(RS,");
|
|
|
|
messageBody +=String(optt*1000);
|
|
|
|
messageBody += F(")</script>");
|
|
|
|
} else if (optt < 120) //redirect back after optionType-60 seconds, unused
|
|
|
|
{
|
|
|
|
//messageBody += "<script>setTimeout(B," + String((optt-60)*1000) + ")</script>";
|
|
|
|
} else if (optt < 180) //reload parent after optionType-120 seconds
|
|
|
|
{
|
|
|
|
messageBody += F("<script>setTimeout(RP,");
|
|
|
|
messageBody += String((optt-120)*1000);
|
|
|
|
messageBody += F(")</script>");
|
|
|
|
} else if (optt == 253)
|
|
|
|
{
|
|
|
|
messageBody += F("<br><br><form action=/settings><button class=\"bt\" type=submit>Back</button></form>"); //button to settings
|
|
|
|
} else if (optt == 254)
|
|
|
|
{
|
|
|
|
messageBody += F("<br><br><button type=\"button\" class=\"bt\" onclick=\"B()\">Back</button>");
|
|
|
|
}
|
|
|
|
return messageBody;
|
|
|
|
}
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void serveMessage(AsyncWebServerRequest* request, uint16_t code, const String& headl, const String& subl, byte optionT)
|
|
|
|
{
|
|
|
|
messageHead = headl;
|
|
|
|
messageSub = subl;
|
|
|
|
optionType = optionT;
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
request->send_P(code, "text/html", PAGE_msg, msgProcessor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-28 16:31:28 +01:00
|
|
|
#ifdef WLED_ENABLE_DMX
|
2021-03-25 20:00:08 +01:00
|
|
|
String dmxProcessor(const String& var)
|
|
|
|
{
|
|
|
|
String mapJS;
|
|
|
|
#ifdef WLED_ENABLE_DMX
|
|
|
|
if (var == "DMXVARS") {
|
|
|
|
mapJS += "\nCN=" + String(DMXChannels) + ";\n";
|
|
|
|
mapJS += "CS=" + String(DMXStart) + ";\n";
|
|
|
|
mapJS += "CG=" + String(DMXGap) + ";\n";
|
2021-10-31 11:57:41 +01:00
|
|
|
mapJS += "LC=" + String(strip.getLengthTotal()) + ";\n";
|
2021-03-25 20:00:08 +01:00
|
|
|
mapJS += "var CH=[";
|
|
|
|
for (int i=0;i<15;i++) {
|
|
|
|
mapJS += String(DMXFixtureMap[i]) + ",";
|
|
|
|
}
|
|
|
|
mapJS += "0];";
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
return mapJS;
|
|
|
|
}
|
2022-01-28 16:31:28 +01:00
|
|
|
#endif
|
2021-03-25 20:00:08 +01:00
|
|
|
|
|
|
|
|
2021-12-06 20:53:09 +01:00
|
|
|
void serveSettingsJS(AsyncWebServerRequest* request)
|
|
|
|
{
|
|
|
|
char buf[SETTINGS_STACK_BUF_SIZE+37];
|
|
|
|
buf[0] = 0;
|
|
|
|
byte subPage = request->arg(F("p")).toInt();
|
2022-05-08 10:50:48 +02:00
|
|
|
if (subPage > 10) {
|
2021-12-06 20:53:09 +01:00
|
|
|
strcpy_P(buf, PSTR("alert('Settings for this request are not implemented.');"));
|
|
|
|
request->send(501, "application/javascript", buf);
|
|
|
|
return;
|
|
|
|
}
|
2022-04-27 12:31:47 +02:00
|
|
|
if (subPage > 0 && !correctPIN && strlen(settingsPIN)>0) {
|
|
|
|
strcpy_P(buf, PSTR("alert('PIN incorrect.');"));
|
|
|
|
request->send(403, "application/javascript", buf);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-06 20:53:09 +01:00
|
|
|
strcat_P(buf,PSTR("function GetV(){var d=document;"));
|
|
|
|
getSettingsJS(subPage, buf+strlen(buf)); // this may overflow by 35bytes!!!
|
|
|
|
strcat_P(buf,PSTR("}"));
|
|
|
|
request->send(200, "application/javascript", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-25 20:00:08 +01:00
|
|
|
void serveSettings(AsyncWebServerRequest* request, bool post)
|
|
|
|
{
|
2022-04-27 12:31:47 +02:00
|
|
|
byte subPage = 0, originalSubPage = 0;
|
2021-03-25 20:00:08 +01:00
|
|
|
const String& url = request->url();
|
2022-04-27 12:31:47 +02:00
|
|
|
|
2023-01-06 09:24:29 +01:00
|
|
|
if (url.indexOf("sett") >= 0)
|
2021-03-25 20:00:08 +01:00
|
|
|
{
|
2021-12-06 20:53:09 +01:00
|
|
|
if (url.indexOf(".js") > 0) subPage = 254;
|
|
|
|
else if (url.indexOf(".css") > 0) subPage = 253;
|
|
|
|
else if (url.indexOf("wifi") > 0) subPage = 1;
|
2021-03-25 20:00:08 +01:00
|
|
|
else if (url.indexOf("leds") > 0) subPage = 2;
|
|
|
|
else if (url.indexOf("ui") > 0) subPage = 3;
|
|
|
|
else if (url.indexOf("sync") > 0) subPage = 4;
|
|
|
|
else if (url.indexOf("time") > 0) subPage = 5;
|
|
|
|
else if (url.indexOf("sec") > 0) subPage = 6;
|
|
|
|
else if (url.indexOf("dmx") > 0) subPage = 7;
|
2022-01-28 16:31:28 +01:00
|
|
|
else if (url.indexOf("um") > 0) subPage = 8;
|
2022-05-08 10:50:48 +02:00
|
|
|
else if (url.indexOf("2D") > 0) subPage = 10;
|
2022-04-27 12:31:47 +02:00
|
|
|
else if (url.indexOf("lock") > 0) subPage = 251;
|
|
|
|
}
|
|
|
|
else if (url.indexOf("/update") >= 0) subPage = 9; // update page, for PIN check
|
|
|
|
//else if (url.indexOf("/edit") >= 0) subPage = 10;
|
|
|
|
else subPage = 255; // welcome page
|
2021-03-25 20:00:08 +01:00
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
if (!correctPIN && strlen(settingsPIN) > 0 && (subPage > 0 && subPage < 11)) {
|
|
|
|
originalSubPage = subPage;
|
|
|
|
subPage = 252; // require PIN
|
2022-03-01 23:37:28 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 18:49:32 +01:00
|
|
|
// if OTA locked or too frequent PIN entry requests fail hard
|
|
|
|
if ((subPage == 1 && wifiLock && otaLock) || (post && !correctPIN && millis()-lastEditTime < 3000))
|
2021-03-25 20:00:08 +01:00
|
|
|
{
|
2022-04-27 12:31:47 +02:00
|
|
|
serveMessage(request, 500, "Access Denied", FPSTR(s_unlock_ota), 254); return;
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (post) { //settings/set POST request, saving
|
|
|
|
if (subPage != 1 || !(wifiLock && otaLock)) handleSettingsSet(request, subPage);
|
|
|
|
|
|
|
|
char s[32];
|
|
|
|
char s2[45] = "";
|
|
|
|
|
|
|
|
switch (subPage) {
|
|
|
|
case 1: strcpy_P(s, PSTR("WiFi")); strcpy_P(s2, PSTR("Please connect to the new IP (if changed)")); forceReconnect = true; break;
|
|
|
|
case 2: strcpy_P(s, PSTR("LED")); break;
|
|
|
|
case 3: strcpy_P(s, PSTR("UI")); break;
|
|
|
|
case 4: strcpy_P(s, PSTR("Sync")); break;
|
|
|
|
case 5: strcpy_P(s, PSTR("Time")); break;
|
2022-03-01 23:37:28 +01:00
|
|
|
case 6: strcpy_P(s, PSTR("Security")); if (doReboot) strcpy_P(s2, PSTR("Rebooting, please wait ~10 seconds...")); break;
|
2021-03-25 20:00:08 +01:00
|
|
|
case 7: strcpy_P(s, PSTR("DMX")); break;
|
2021-04-10 00:17:15 +02:00
|
|
|
case 8: strcpy_P(s, PSTR("Usermods")); break;
|
2022-05-08 10:50:48 +02:00
|
|
|
case 10: strcpy_P(s, PSTR("2D")); break;
|
2022-03-03 18:49:32 +01:00
|
|
|
case 252: strcpy_P(s, correctPIN ? PSTR("PIN accepted") : PSTR("PIN rejected")); break;
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
if (subPage == 252) {
|
2022-05-01 22:09:40 +02:00
|
|
|
createEditHandler(correctPIN);
|
2022-04-27 12:31:47 +02:00
|
|
|
} else
|
|
|
|
strcat_P(s, PSTR(" settings saved."));
|
2021-03-25 20:00:08 +01:00
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
if (subPage == 252 && correctPIN) {
|
|
|
|
subPage = originalSubPage; // on correct PIN load settings page the user intended
|
|
|
|
} else {
|
|
|
|
if (!s2[0]) strcpy_P(s2, s_redirecting);
|
2021-03-25 20:00:08 +01:00
|
|
|
|
2022-04-27 12:31:47 +02:00
|
|
|
serveMessage(request, 200, s, s2, (subPage == 1 || (subPage == 6 && doReboot)) ? 129 : (correctPIN ? 1 : 3));
|
|
|
|
return;
|
|
|
|
}
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
2023-01-06 09:24:29 +01:00
|
|
|
|
2021-12-06 20:53:09 +01:00
|
|
|
AsyncWebServerResponse *response;
|
2021-03-25 20:00:08 +01:00
|
|
|
switch (subPage)
|
|
|
|
{
|
2021-12-06 20:53:09 +01:00
|
|
|
case 1: response = request->beginResponse_P(200, "text/html", PAGE_settings_wifi, PAGE_settings_wifi_length); break;
|
|
|
|
case 2: response = request->beginResponse_P(200, "text/html", PAGE_settings_leds, PAGE_settings_leds_length); break;
|
|
|
|
case 3: response = request->beginResponse_P(200, "text/html", PAGE_settings_ui, PAGE_settings_ui_length); break;
|
|
|
|
case 4: response = request->beginResponse_P(200, "text/html", PAGE_settings_sync, PAGE_settings_sync_length); break;
|
|
|
|
case 5: response = request->beginResponse_P(200, "text/html", PAGE_settings_time, PAGE_settings_time_length); break;
|
|
|
|
case 6: response = request->beginResponse_P(200, "text/html", PAGE_settings_sec, PAGE_settings_sec_length); break;
|
2023-01-16 21:55:12 +01:00
|
|
|
#ifdef WLED_ENABLE_DMX
|
2021-12-06 20:53:09 +01:00
|
|
|
case 7: response = request->beginResponse_P(200, "text/html", PAGE_settings_dmx, PAGE_settings_dmx_length); break;
|
2023-01-16 21:55:12 +01:00
|
|
|
#endif
|
2021-12-06 20:53:09 +01:00
|
|
|
case 8: response = request->beginResponse_P(200, "text/html", PAGE_settings_um, PAGE_settings_um_length); break;
|
2022-04-27 12:31:47 +02:00
|
|
|
case 9: response = request->beginResponse_P(200, "text/html", PAGE_update, PAGE_update_length); break;
|
2023-01-16 21:55:12 +01:00
|
|
|
#ifndef WLED_DISABLE_2D
|
2022-05-08 10:50:48 +02:00
|
|
|
case 10: response = request->beginResponse_P(200, "text/html", PAGE_settings_2D, PAGE_settings_2D_length); break;
|
2023-01-16 21:55:12 +01:00
|
|
|
#endif
|
2022-04-27 12:31:47 +02:00
|
|
|
case 251: {
|
|
|
|
correctPIN = !strlen(settingsPIN); // lock if a pin is set
|
2022-05-01 22:09:40 +02:00
|
|
|
createEditHandler(correctPIN);
|
2022-04-27 12:31:47 +02:00
|
|
|
serveMessage(request, 200, strlen(settingsPIN) > 0 ? PSTR("Settings locked") : PSTR("No PIN set"), FPSTR(s_redirecting), 1);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-01 23:37:28 +01:00
|
|
|
case 252: response = request->beginResponse_P(200, "text/html", PAGE_settings_pin, PAGE_settings_pin_length); break;
|
2021-12-06 20:53:09 +01:00
|
|
|
case 253: response = request->beginResponse_P(200, "text/css", PAGE_settingsCss, PAGE_settingsCss_length); break;
|
|
|
|
case 254: serveSettingsJS(request); return;
|
|
|
|
case 255: response = request->beginResponse_P(200, "text/html", PAGE_welcome, PAGE_welcome_length); break;
|
2022-01-28 16:31:28 +01:00
|
|
|
default: response = request->beginResponse_P(200, "text/html", PAGE_settings, PAGE_settings_length); break;
|
2021-03-25 20:00:08 +01:00
|
|
|
}
|
2022-04-27 12:31:47 +02:00
|
|
|
response->addHeader(FPSTR(s_content_enc),"gzip");
|
2021-12-06 20:53:09 +01:00
|
|
|
setStaticContentCacheHeaders(response);
|
|
|
|
request->send(response);
|
2022-10-24 18:44:11 +02:00
|
|
|
}
|