";
-const uint8_t HTTP_VARS_CSS[] PROGMEM = ":root{"
- "--txt:" D_HTTP_COLOR_TEXT ";"
- "--bg:" D_HTTP_COLOR_BACKGROUND ";"
- "--btnfg:" D_HTTP_COLOR_BUTTON_TEXT ";"
- "--btnbg:" D_HTTP_COLOR_BUTTON ";"
- "--btnbghi:" D_HTTP_COLOR_BUTTON_HOVER ";"
- "--btnred:" D_HTTP_COLOR_BUTTON_RESET ";"
- "--btnredhi:" D_HTTP_COLOR_BUTTON_RESET_HOVER ";"
- "--btnbrd: transparent;"
- "--grpfg:" D_HTTP_COLOR_GROUP_TEXT ";"
- "--grpbg:" D_HTTP_COLOR_GROUP ";"
- "--fldbg:" D_HTTP_COLOR_INPUT ";"
- "--fldfg:" D_HTTP_COLOR_INPUT_TEXT ";"
- "--fldred:" D_HTTP_COLOR_INPUT_WARNING ";"
- "--footfg:" D_HTTP_COLOR_FOOTER_TEXT ";"
- "}";
+const char HTTP_HEADER_END[] PROGMEM =
+ "
)";
+const char HTTP_FOOTER[] PROGMEM = "
";
+const uint8_t HTTP_VARS_CSS[] PROGMEM = ":root{"
+ "--txt:" D_HTTP_COLOR_TEXT ";"
+ "--bg:" D_HTTP_COLOR_BACKGROUND ";"
+ "--btnfg:" D_HTTP_COLOR_BUTTON_TEXT ";"
+ "--btnbg:" D_HTTP_COLOR_BUTTON ";"
+ "--btnbghi:" D_HTTP_COLOR_BUTTON_HOVER ";"
+ "--btnred:" D_HTTP_COLOR_BUTTON_RESET ";"
+ "--btnredhi:" D_HTTP_COLOR_BUTTON_RESET_HOVER ";"
+ "--btnbrd: transparent;"
+ "--grpfg:" D_HTTP_COLOR_GROUP_TEXT ";"
+ "--grpbg:" D_HTTP_COLOR_GROUP ";"
+ "--fldbg:" D_HTTP_COLOR_INPUT ";"
+ "--fldfg:" D_HTTP_COLOR_INPUT_TEXT ";"
+ "--fldred:" D_HTTP_COLOR_INPUT_WARNING ";"
+ "--footfg:" D_HTTP_COLOR_FOOTER_TEXT ";"
+ "--toolbg:" D_HTTP_COLOR_TOOLBAR ";"
+ "--treebg:" D_HTTP_COLOR_TREE ";"
+ "--preeviewbg:" D_HTTP_COLOR_PREVIEW ";"
+ "}";
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -165,18 +171,21 @@ static void add_form_button(String& str, const __FlashStringHelper* label, const
static String http_get_content_type(const String& path)
{
- char buff[sizeof(mime::mimeTable[0].mimeType)];
- // Check all entries but last one for match, return if found
- for(size_t i = 0; i < sizeof(mime::mimeTable) / sizeof(mime::mimeTable[0]) - 1; i++) {
- strcpy_P(buff, mime::mimeTable[i].endsWith);
- if(path.endsWith(buff)) {
- strcpy_P(buff, mime::mimeTable[i].mimeType);
- return String(buff);
+ char buffer[sizeof(mime::mimeTable[0].mimeType)];
+ int len = sizeof(mime::mimeTable) / sizeof(mime::mimeTable[0]) - 1;
+
+ // Check all entries except the last one for match, return if found
+ for(size_t i = 0; i < len; i++) {
+ strcpy_P(buffer, mime::mimeTable[i].endsWith);
+ if(path.endsWith(buffer)) {
+ strcpy_P(buffer, mime::mimeTable[i].mimeType);
+ return String(buffer);
}
}
- // Fall-through and just return default type
- strcpy_P(buff, mime::mimeTable[sizeof(mime::mimeTable) / sizeof(mime::mimeTable[0]) - 1].mimeType);
- return String(buff);
+
+ // Fall-through and return default (=last) mime-type
+ strcpy_P(buffer, mime::mimeTable[len].mimeType);
+ return String(buffer);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -230,7 +239,7 @@ static void webSendFooter()
static void http_send_cache_header(int size, int age = 3600)
{
- webServer.sendHeader(F("Content-Length"), (String)(size));
+ webServer.sendHeader("Content-Length", (String)(size));
webServer.sendHeader(F("Cache-Control"), (String)(F("public, max-age=")) + (String)(age));
}
@@ -258,52 +267,68 @@ static int http_send_static_gzip_file(const uint8_t* start, const uint8_t* end,
return http_send_static_file(start, end, contentType);
}
-static void webSendHtmlHeader(const char* nodename, uint32_t httpdatalength, uint8_t gohome = 0)
+static void webSendHtmlHeader(const char* title, uint32_t httpdatalength, uint8_t gohome = 0)
{
- {
- char buffer[64];
+ char buffer[64];
- /* Calculate Content Length upfront */
- uint32_t contentLength = strlen(haspDevice.get_version()); // version length
- contentLength += sizeof(HTTP_DOCTYPE) - 1;
- contentLength += sizeof(HTTP_HEADER) - 1 - 2 + strlen(nodename); // -2 for %s
- contentLength += sizeof(HTTP_STYLESHEET) - 1 - 2 + strlen("vars"); // -2 for %s
- if(gohome > 0) {
- snprintf_P(buffer, sizeof(buffer), HTTP_META_GO_BACK, gohome);
- contentLength += strlen(buffer); // gohome
- } else {
- buffer[0] = '\0';
- }
- contentLength += sizeof(HTTP_HEADER_END) - 1;
- contentLength += sizeof(HTTP_FOOTER) - 1;
- contentLength += sizeof(HTTP_END) - 1;
-
- if(httpdatalength > HTTP_PAGE_SIZE) {
- LOG_WARNING(TAG_HTTP, F("Sending page with %u static and %u dynamic bytes"), contentLength, httpdatalength);
- }
-
- webServer.setContentLength(contentLength + httpdatalength);
-#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
- webServer.send_P(200, PSTR("text/html"), HTTP_DOCTYPE); // 122
-#else
- webServer.send(200, ("text/html"), HTTP_DOCTYPE); // 122
-#endif
- webServer.sendContent(buffer); // gohome
-
- snprintf_P(buffer, sizeof(buffer), HTTP_STYLESHEET, "vars");
- webServer.sendContent(buffer); // stylesheet
-
- snprintf_P(buffer, sizeof(buffer), HTTP_HEADER, nodename);
- webServer.sendContent(buffer); // 17-2+len
+ /* Calculate Content Length upfront */
+ uint32_t contentLength = strlen(haspDevice.get_version()); // version length
+ contentLength += sizeof(HTTP_DOCTYPE) - 1;
+ contentLength += sizeof(HTTP_HEADER) - 1 - 2 + strlen(title); // -2 for %s
+ contentLength += sizeof(HTTP_STYLESHEET) - 1 - 2 + strlen("static/vars"); // -2 for %s
+ if(gohome > 0) {
+ snprintf_P(buffer, sizeof(buffer), HTTP_META_GO_BACK, gohome);
+ contentLength += strlen(buffer); // gohome
+ } else {
+ buffer[0] = '\0';
}
+ contentLength += sizeof(HTTP_HEADER_END) - 1;
+ contentLength += sizeof(HTTP_FOOTER) - 1;
+ contentLength += sizeof(HTTP_END) - 1;
+
+ if(httpdatalength > HTTP_PAGE_SIZE) {
+ LOG_WARNING(TAG_HTTP, F("Sending page with %u static and %u dynamic bytes"), contentLength, httpdatalength);
+ }
+
+ webServer.setContentLength(contentLength + httpdatalength);
+ // webServer.sendHeader("Set-Cookie", "lang=nl; SameSite=None; path=/");
+#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
+ webServer.send_P(200, PSTR("text/html"), HTTP_DOCTYPE); // 122
+#else
+ webServer.send(200, ("text/html"), HTTP_DOCTYPE); // 122
+#endif
+ webServer.sendContent(buffer); // gohome
+
+ snprintf_P(buffer, sizeof(buffer), HTTP_STYLESHEET, "static/vars");
+ webServer.sendContent(buffer); // stylesheet
+
+ snprintf_P(buffer, sizeof(buffer), HTTP_HEADER, title);
+ webServer.sendContent(buffer); // 17-2+len
#if defined(STM32F4xx)
webServer.sendContent(HTTP_HEADER_END); // 80
#else
- webServer.sendContent_P(HTTP_HEADER_END); // 80
+ webServer.sendContent_P(HTTP_HEADER_END); // 80
#endif
}
+static void http_send_content(const char* form[], int count, uint8_t gohome = 0)
+{
+ size_t total = 0;
+ size_t len[count];
+ for(int i = 0; i < count; i++) {
+ if(form[i]) {
+ len[i] = strlen(form[i]);
+ total += len[i];
+ }
+ }
+ webSendHtmlHeader(haspDevice.get_hostname(), total, gohome);
+ for(int i = 0; i < count; i++) {
+ if(form[i]) webServer.sendContent(form[i], len[i]);
+ }
+ webSendFooter();
+}
+
// Allows caching of this file, BUT browser must validate Etag before using cached versions
static void http_send_etag(String& etag)
{
@@ -321,13 +346,13 @@ bool http_save_config()
{
bool updated = false;
- if(webServer.method() == HTTP_POST && webServer.hasArg(PSTR("save"))) {
- String save = webServer.arg(PSTR("save"));
+ if(webServer.method() == HTTP_POST && webServer.hasArg("save")) {
+ String save = webServer.arg("save");
StaticJsonDocument<256> settings;
for(int i = 0; i < webServer.args(); i++) settings[webServer.argName(i)] = webServer.arg(i);
- if(save == String(PSTR("hasp"))) {
+ if(save == FP_HASP) {
updated = haspSetConfig(settings.as
());
#if HASP_USE_MQTT > 0
@@ -339,24 +364,24 @@ bool http_save_config()
updated = ftpSetConfig(settings.as());
#endif
- } else if(save == String(PSTR("gui"))) {
- settings[FPSTR(FP_GUI_POINTER)] = webServer.hasArg(PSTR("cursor"));
- settings[FPSTR(FP_GUI_INVERT)] = webServer.hasArg(PSTR("invert"));
- settings[FPSTR(FP_GUI_BACKLIGHTINVERT)] = webServer.hasArg(PSTR("bcklinv"));
+ } else if(save == FP_GUI) {
+ settings[FPSTR(FP_GUI_POINTER)] = webServer.hasArg("cursor");
+ settings[FPSTR(FP_GUI_INVERT)] = webServer.hasArg("invert");
+ settings[FPSTR(FP_GUI_BACKLIGHTINVERT)] = webServer.hasArg("bcklinv");
updated = guiSetConfig(settings.as());
- } else if(save == String(PSTR("debug"))) {
- settings[FPSTR(FP_DEBUG_ANSI)] = webServer.hasArg(PSTR("ansi"));
+ } else if(save == FP_DEBUG) {
+ settings[FPSTR(FP_DEBUG_ANSI)] = webServer.hasArg("ansi");
updated = debugSetConfig(settings.as());
- } else if(save == String(PSTR(FP_HTTP))) {
+ } else if(save == FP_HTTP) {
updated = httpSetConfig(settings.as());
// Password might have changed
if(!http_is_authenticated(F("config"))) return updated;
#if HASP_USE_WIFI > 0
- } else if(save == String(PSTR("wifi"))) {
+ } else if(save == FP_WIFI) {
updated = wifiSetConfig(settings.as());
#endif
}
@@ -365,73 +390,56 @@ bool http_save_config()
return updated;
}
-static void webHandleRoot()
+static void http_handle_root()
{
if(!http_is_authenticated(F("root"))) return;
bool updated = http_save_config();
- {
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- if(updated) {
- httpMessage += F("" D_HTTP_CONFIG_CHANGED "
");
- }
-
- httpMessage += F("" D_HTTP_HASP_DESIGN " ");
- httpMessage += F("" D_HTTP_SCREENSHOT " ");
- httpMessage += F("" D_HTTP_INFORMATION " ");
- httpMessage += F("" D_HTTP_CONFIGURATION " ");
- httpMessage += F("" D_HTTP_FIRMWARE_UPGRADE " ");
-
-#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
-#ifdef ARDUINO_ARCH_ESP32
- bool flashfile = true;
-#else
- bool flashfile = false;
-#endif
- if(flashfile || HASP_FS.exists(F("/edit.htm.gz")) || HASP_FS.exists(F("/edit.htm"))) {
- httpMessage += F("" D_HTTP_FILE_BROWSER " ");
- }
-#endif
-
- httpMessage += F("" D_HTTP_REBOOT " ");
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ if(updated) {
+ html[min(i++, len)] = R"(" D_HTTP_CONFIG_CHANGED "
)";
}
- webSendFooter();
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+#ifdef ARDUINO_ARCH_ESP32
+ html[min(i++, len)] = R"( )";
+#endif
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-static void httpHandleReboot()
+static void http_handle_reboot()
{ // http://plate01/reboot
if(!http_is_authenticated(F("reboot"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage = F(D_DISPATCH_REBOOT);
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 6);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
)";
+ http_send_content(html, min(i, len), 10);
{ // Execute Actions
- // delay(200);
+ // delay(200);
dispatch_reboot(true);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-static void webHandleScreenshot()
+static void http_handle_screenshot()
{ // http://plate01/screenshot
if(!http_is_authenticated(F("screenshot"))) return;
@@ -465,44 +473,40 @@ static void webHandleScreenshot()
LOG_DEBUG(TAG_HTTP, F("If-None-Match: %s"), etag.c_str());
if(modified > 0 && modified == atol(etag.c_str())) { // Not Changed
http_send_etag(etag); // Reuse same ETag
- webServer.send_P(304, PSTR("image/bmp"), ""); // Use correct mimetype
- return; // 304 ot Modified
+ webServer.send(304, "image/bmp", ""); // Use correct mimetype
+ return; // 304 not Modified
}
}
- etag = (String)(modified);
- http_send_etag(etag); // Send new tag with modification version
-
// Send actual bitmap
if(webServer.hasArg(F("q"))) {
lv_disp_t* disp = lv_disp_get_default();
+ etag = (String)(modified);
+ http_send_etag(etag); // Send new tag with modification version
webServer.setContentLength(66 + disp->driver.hor_res * disp->driver.ver_res * sizeof(lv_color_t));
- webServer.send_P(200, PSTR("image/bmp"), "");
+ webServer.send(200, "image/bmp", "");
guiTakeScreenshot();
webServer.client().stop();
return;
}
}
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- httpMessage += F("
"); // Automatic refresh
-
- httpMessage += F("");
- httpMessage += FPSTR(MAIN_MENU_BUTTON);
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+)";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -537,10 +541,11 @@ static void webHandleApi()
String endpoint((char*)0);
endpoint = webServer.pathArg(0);
- if(!strcasecmp_P(endpoint.c_str(), PSTR("files"))) {
- webServer.send(200, contentType.c_str(), filesystem_list(HASP_FS, "/", 5).c_str());
+ if(!strcasecmp(endpoint.c_str(), "files")) {
+ String path = webServer.arg(F("dir"));
+ webServer.send(200, contentType.c_str(), filesystem_list(HASP_FS, path.c_str(), 5).c_str());
- } else if(!strcasecmp_P(endpoint.c_str(), PSTR("info"))) {
+ } else if(!strcasecmp(endpoint.c_str(), "info")) {
String jsondata((char*)0);
jsondata.reserve(HTTP_PAGE_SIZE);
jsondata = "{";
@@ -566,7 +571,7 @@ static void webHandleApi()
webServer.send(200, contentType, jsondata);
return;
- } else if(!strcasecmp_P(endpoint.c_str(), PSTR("license"))) {
+ } else if(!strcasecmp(endpoint.c_str(), "credits")) {
{
JsonObject obj;
@@ -612,7 +617,7 @@ static void webHandleApi()
webServer.send(200, contentType.c_str(), output);
}
- } else if(!strcasecmp_P(endpoint.c_str(), PSTR("config"))) {
+ } else if(!strcasecmp(endpoint.c_str(), "config")) {
JsonObject settings;
String postBody((char*)0);
@@ -720,7 +725,7 @@ static void webHandleApiConfig()
StaticJsonDocument<1024> doc;
JsonObject settings;
- String contentType = http_get_content_type(F(".json"));
+ // String contentType = http_get_content_type(F(".json"));
String endpoint((char*)0);
endpoint = webServer.pathArg(0);
const char* endpoint_key = endpoint.c_str();
@@ -739,44 +744,44 @@ static void webHandleApiConfig()
}
settings = doc.as();
} else {
- webServer.send(400, contentType, "Bad Request");
+ webServer.send(400, "application/json", "Bad Request");
return;
}
if(webServer.method() == HTTP_POST || webServer.method() == HTTP_PUT) {
configOutput(settings, TAG_HTTP); // Log input JSON config
- if(!strcasecmp_P(endpoint_key, PSTR("hasp"))) {
+ if(!strcasecmp(endpoint_key, FP_HASP)) {
haspSetConfig(settings);
- } else if(!strcasecmp_P(endpoint_key, PSTR("gui"))) {
+ } else if(!strcasecmp(endpoint_key, FP_GUI)) {
guiSetConfig(settings);
- } else if(!strcasecmp_P(endpoint_key, PSTR("debug"))) {
+ } else if(!strcasecmp(endpoint_key, FP_DEBUG)) {
debugSetConfig(settings);
} else
#if HASP_USE_WIFI > 0
- if(!strcasecmp_P(endpoint_key, PSTR("wifi"))) {
+ if(!strcasecmp(endpoint_key, FP_WIFI)) {
wifiSetConfig(settings);
- } else if(!strcasecmp_P(endpoint_key, PSTR("time"))) {
+ } else if(!strcasecmp(endpoint_key, FP_TIME)) {
timeSetConfig(settings);
} else
#endif
#if HASP_USE_MQTT > 0
- if(!strcasecmp_P(endpoint_key, PSTR(FP_MQTT))) {
+ if(!strcasecmp(endpoint_key, FP_MQTT)) {
mqttSetConfig(settings);
} else
#endif
#if HASP_USE_FTP > 0
- if(!strcasecmp_P(endpoint_key, PSTR(FP_FTP))) {
+ if(!strcasecmp(endpoint_key, FP_FTP)) {
ftpSetConfig(settings);
} else
#endif
#if HASP_USE_HTTP > 0
- if(!strcasecmp_P(endpoint_key, PSTR(FP_HTTP))) {
+ if(!strcasecmp(endpoint_key, FP_HTTP)) {
httpSetConfig(settings);
} else
#endif
#if HASP_USE_ARDUINOOTA > 0 || HASP_USE_HTTP_UPDATE > 0
- if(!strcasecmp_P(endpoint_key, PSTR("ota"))) {
+ if(!strcasecmp(endpoint_key, FP_OTA)) {
otaSetConfig(settings);
} else
#endif
@@ -787,42 +792,42 @@ static void webHandleApiConfig()
}
settings = doc.to();
- if(!strcasecmp_P(endpoint_key, PSTR("hasp"))) {
+ if(!strcasecmp(endpoint_key, FP_HASP)) {
haspGetConfig(settings);
- } else if(!strcasecmp_P(endpoint_key, PSTR("gui"))) {
+ } else if(!strcasecmp(endpoint_key, FP_GUI)) {
guiGetConfig(settings);
- } else if(!strcasecmp_P(endpoint_key, PSTR("debug"))) {
+ } else if(!strcasecmp(endpoint_key, FP_DEBUG)) {
debugGetConfig(settings);
} else
#if HASP_USE_WIFI > 0
- if(!strcasecmp_P(endpoint_key, PSTR("wifi"))) {
+ if(!strcasecmp(endpoint_key, FP_WIFI)) {
wifiGetConfig(settings);
- } else if(!strcasecmp_P(endpoint_key, PSTR("time"))) {
+ } else if(!strcasecmp(endpoint_key, FP_TIME)) {
timeGetConfig(settings);
} else
#endif
#if HASP_USE_MQTT > 0
- if(!strcasecmp_P(endpoint_key, PSTR(FP_MQTT))) {
+ if(!strcasecmp(endpoint_key, FP_MQTT)) {
mqttGetConfig(settings);
} else
#endif
#if HASP_USE_FTP > 0
- if(!strcasecmp_P(endpoint_key, PSTR(FP_FTP))) {
+ if(!strcasecmp(endpoint_key, FP_FTP)) {
ftpGetConfig(settings);
} else
#endif
#if HASP_USE_HTTP > 0
- if(!strcasecmp_P(endpoint_key, PSTR(FP_HTTP))) {
+ if(!strcasecmp(endpoint_key, FP_HTTP)) {
httpGetConfig(settings);
} else
#endif
#if HASP_USE_ARDUINOOTA > 0 || HASP_USE_HTTP_UPDATE > 0
- if(!strcasecmp_P(endpoint_key, PSTR("ota"))) {
+ if(!strcasecmp(endpoint_key, FP_OTA)) {
otaGetConfig(settings);
} else
#endif
{
- webServer.send(400, contentType, "Bad Request");
+ webServer.send(400, "application/json", "Bad Request");
return;
}
configOutput(settings, TAG_HTTP); // Log current JSON config
@@ -836,7 +841,7 @@ static void webHandleApiConfig()
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
// doc.shrinkToFit();
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
- const size_t size = measureJson(doc) + 1;
+ const size_t size = measureJson(doc);
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
char jsondata[size];
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
@@ -844,50 +849,74 @@ static void webHandleApiConfig()
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
serializeJson(doc, jsondata, size);
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
- webServer.send(200, contentType, jsondata);
+ // webServer.send(200, contentType.c_str(), jsondata);
+ webServer.setContentLength(size);
+ LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
+ webServer.send(200, "application/json", "");
+ LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
+ webServer.sendContent(jsondata, size);
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-static void webHandleAbout()
+static void http_handle_about()
{ // http://plate01/about
if(!http_is_authenticated(F("about"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
+ const char* form = R"(
+
+{{model.t}}
+
+{{model.y}} {{model.a}}
+
+
+
- httpMessage += "
";
- httpMessage += FPSTR(MAIN_MENU_BUTTON);
- httpMessage += "
";
- // TOREMOVE httpMessage += "";
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+openHASP Copyright 2019-2023 Francis Van RoieMIT License
+
+
+
+
+
+
+
+)";
+ http_send_content(&form, 1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-static void webHandleInfoJson()
+static void http_handle_info()
{ // http://plate01/
- if(!http_is_authenticated(F("infojson"))) return;
+ if(!http_is_authenticated(F("info"))) return;
- { // Send Content
- String htmldata((char*)0);
- htmldata.reserve(HTTP_PAGE_SIZE);
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- htmldata += F("");
- htmldata += haspDevice.get_hostname();
- htmldata += F(" ");
-
- htmldata += "
";
- htmldata += FPSTR(MAIN_MENU_BUTTON);
-
- webSendHtmlHeader(haspDevice.get_hostname(), htmldata.length(), 0);
- webServer.sendContent(htmldata);
- }
- webSendFooter();
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+openHASP
+{{ item }}
+Device Memory
+{{ item }}
+LVGL Memory
+{{ item }}
+MQTT
+{{ item }}
+Wifi
+{{ item }}
+Module
+{{ item }}
+
)";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -953,10 +982,10 @@ static void webUpdateReboot()
{ // Send Content
String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
+ httpMessage.reserve(256);
+ httpMessage += "";
httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ httpMessage += " ";
httpMessage += F("Upload complete. Rebooting device, please wait... ");
webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 10);
@@ -1129,27 +1158,33 @@ static void handleFileUpload()
fsUploadFile = HASP_FS.open(filename, "w");
if(fsUploadFile) {
if(!fsUploadFile || fsUploadFile.isDirectory()) {
- LOG_WARNING(TAG_HTTP, F(D_FILE_SAVE_FAILED), filename.c_str());
+ // Clear upload filesize, fix Response Content-Length
+ webServer.setContentLength(CONTENT_LENGTH_NOT_SET);
webServer.send_P(400, PSTR("text/plain"), PSTR("Invalid filename"));
fsUploadFile.close();
fsUploadFile = File();
+ LOG_WARNING(TAG_HTTP, F(D_FILE_SAVE_FAILED), filename.c_str());
} else {
LOG_TRACE(TAG_HTTP, F("handleFileUpload Name: %s"), filename.c_str());
haspProgressMsg(fsUploadFile.name());
}
} else {
- LOG_ERROR(TAG_HTTP, F("Could not open file %s for writing"), filename.c_str());
+ // Clear upload filesize, fix Response Content-Length
+ webServer.setContentLength(CONTENT_LENGTH_NOT_SET);
webServer.send_P(400, PSTR("text/plain"), PSTR("Could not open file for writing"));
+ LOG_ERROR(TAG_HTTP, F("Could not open file %s for writing"), filename.c_str());
}
break;
}
case UPLOAD_FILE_WRITE: {
if(fsUploadFile) {
if(fsUploadFile.write(upload->buf, upload->currentSize) != upload->currentSize) {
- LOG_ERROR(TAG_HTTP, F("Failed to write received data to file"));
+ // Clear upload filesize, fix Response Content-Length
+ webServer.setContentLength(CONTENT_LENGTH_NOT_SET);
webServer.send_P(400, PSTR("text/plain"), PSTR("Failed to write received data to file"));
fsUploadFile.close();
fsUploadFile = File();
+ LOG_ERROR(TAG_HTTP, F("Failed to write received data to file"));
} else {
http_upload_progress(); // Moved to httpEverySecond Loop
}
@@ -1163,7 +1198,10 @@ static void handleFileUpload()
// Redirect to /config/hasp page. This flushes the web buffer and frees the memory
// webServer.sendHeader(String(F("Location")), String(F("/config/hasp")), true);
- webServer.send_P(200, PSTR("text/plain"), "Upload OK");
+
+ // Clear upload filesize, fix Response Content-Length
+ webServer.setContentLength(CONTENT_LENGTH_NOT_SET);
+ webServer.send_P(200, PSTR("text/plain"), PSTR("Upload OK"));
}
haspProgressVal(255);
break;
@@ -1195,7 +1233,7 @@ static void handleFileDelete()
return webServer.send_P(404, mimetype, PSTR("FileNotFound"));
}
HASP_FS.remove(path);
- webServer.send_P(200, mimetype, PSTR(""));
+ webServer.send(200, mimetype, String(""));
}
static void handleFileCreate()
@@ -1250,10 +1288,10 @@ static void handleFileList()
String path = webServer.arg(F("dir"));
// LOG_TRACE(TAG_HTTP, F("handleFileList: %s"), path.c_str());
- path.clear();
+ // path.clear();
#if defined(ARDUINO_ARCH_ESP32)
- File root = HASP_FS.open("/", FILE_READ);
+ File root = HASP_FS.open(path.c_str(), FILE_READ);
File file = root.openNextFile();
String output((char*)0);
output.reserve(HTTP_PAGE_SIZE);
@@ -1312,7 +1350,7 @@ static void handleFileList()
////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_CONFIG > 0
-static void webHandleConfig()
+static void http_handle_config()
{ // http://plate01/config
if(!http_is_authenticated(F("config"))) return;
@@ -1321,212 +1359,188 @@ static void webHandleConfig()
// Reboot after saving wifi config in AP mode
#if HASP_USE_WIFI > 0 && !defined(STM32F4xx)
if(WiFi.getMode() != WIFI_STA) {
- httpHandleReboot();
+ http_handle_reboot();
}
#endif
- {
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- if(updated) {
- httpMessage += F("" D_HTTP_CONFIG_CHANGED "
");
- }
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ if(updated) {
+ html[min(i++, len)] = R"()" D_HTTP_CONFIG_CHANGED "
";
+ }
#if HASP_USE_WIFI > 0
- httpMessage += F("" D_HTTP_WIFI_SETTINGS " ");
+ html[min(i++, len)] = R"( )";
#endif
#if HASP_USE_MQTT > 0
- httpMessage += F("" D_HTTP_MQTT_SETTINGS " ");
+ html[min(i++, len)] = R"( )";
#endif
- httpMessage += F("" D_HTTP_HTTP_SETTINGS " ");
+ html[min(i++, len)] = R"( )";
#if HASP_USE_FTP > 0
- httpMessage += F("" D_HTTP_FTP_SETTINGS " ");
+ html[min(i++, len)] = R"( )";
#endif
- httpMessage += F("" D_HTTP_GUI_SETTINGS " ");
-
+ html[min(i++, len)] = R"( )";
#if HASP_USE_GPIO > 0
- httpMessage += F("" D_HTTP_GPIO_SETTINGS " ");
+ html[min(i++, len)] = R"( )";
#endif
-
- httpMessage += F("" D_HTTP_DEBUG_SETTINGS " ");
- httpMessage += F("" D_HTTP_FACTORY_RESET " ");
- httpMessage += FPSTR(MAIN_MENU_BUTTON);
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_MQTT > 0
-static void webHandleMqttConfig()
+static void http_handle_mqtt()
{ // http://plate01/config/mqtt
if(!http_is_authenticated(F("config/mqtt"))) return;
- { // Send Content
- StaticJsonDocument<256> settings;
- mqttGetConfig(settings.to());
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_MQTT_SETTINGS " ");
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
- // Form
- httpMessage += F(")";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
-static void webHandleGuiConfig()
+static void http_handle_gui()
{ // http://plate01/config/wifi
if(!http_is_authenticated(F("config/gui"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_GUI_SETTINGS " ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- // Form
- httpMessage += F("");
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+
+
+
+
+
+
Orientation
+
+0 degrees
+90 degrees
+180 degrees
+270 degrees
+0 degrees - mirrored
+90 degrees - mirrored
+180 degrees - mirrored
+270 degrees - mirrored
+
+
+
+
+
+
Backlight Pin
+
)";
- // Short Idle
- httpMessage += F("Short Idle
");
- httpMessage += F("
");
-
- // Long Idle
- httpMessage += F("Long Idle
");
- httpMessage += F("
");
-
- // Rotation
- httpMessage += F("Orientation
");
- httpMessage += F("
");
- httpMessage += getOption(0, F("0 degrees"));
- httpMessage += getOption(1, F("90 degrees"));
- httpMessage += getOption(2, F("180 degrees"));
- httpMessage += getOption(3, F("270 degrees"));
- httpMessage += getOption(6, F("0 degrees - mirrored"));
- httpMessage += getOption(7, F("90 degrees - mirrored"));
- httpMessage += getOption(4, F("180 degrees - mirrored"));
- httpMessage += getOption(5, F("270 degrees - mirrored"));
- httpMessage += F("
");
-
- // Invert
- httpMessage += F("
");
- httpMessage += F("
Invert Colors
");
-
- // Cursor
- httpMessage += F("
");
- httpMessage += F("
Show Pointer
");
-
- // Backlight Pin
- httpMessage += F("Backlight Pin
");
- httpMessage += F("
");
- httpMessage += getOption(-1, F("None"));
+ String httpGpio((char*)0);
+ httpGpio.reserve(256);
+ httpGpio += getOption(-1, F("None"));
#if defined(ARDUINO_ARCH_ESP32)
- char buffer[10];
- // uint8_t pins[] = {0, 5, 12, 13, 15, 16, 17, 18, 19, 21, 22, 23, 26, 27, 32};
- // for(uint8_t i = 0; i < sizeof(pins); i++) {
- for(uint8_t gpio = 0; gpio < NUM_DIGITAL_PINS; gpio++) {
- if(!gpioIsSystemPin(gpio)) {
- snprintf_P(buffer, sizeof(buffer), PSTR("GPIO %d"), gpio);
- httpMessage += getOption(gpio, buffer);
- } else {
- LOG_WARNING(TAG_HTTP, F("pin %d"), gpio);
- }
+ char buffer[10];
+ for(uint8_t gpio = 0; gpio < NUM_DIGITAL_PINS; gpio++) {
+ if(!gpioIsSystemPin(gpio)) {
+ snprintf_P(buffer, sizeof(buffer), PSTR("GPIO %d"), gpio);
+ httpGpio += getOption(gpio, buffer);
+ } else {
+ LOG_WARNING(TAG_HTTP, F("pin %d"), gpio);
}
-#else
- httpMessage += getOption(5, F("D1 - GPIO 5"));
- httpMessage += getOption(4, F("D2 - GPIO 4"));
- httpMessage += getOption(0, F("D3 - GPIO 0"));
- httpMessage += getOption(2, F("D4 - GPIO 2"));
-#endif
- httpMessage += F("
");
-
- // Backlight Invert
- httpMessage += F("
");
- httpMessage += F("
Invert Backlight
");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_SAVE_SETTINGS " ");
- httpMessage += F(" ");
-
-#if TOUCH_DRIVER == 0x2046 && defined(TOUCH_CS)
- add_form_button(httpMessage, F(D_HTTP_CALIBRATE), F("/config/gui?cal=1"));
-#endif
-
- add_form_button(httpMessage, F(D_HTTP_ANTIBURN), F("/config/gui?brn=1"));
- add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config"));
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
}
- webSendFooter();
+#endif
+ html[min(i++, len)] = httpGpio.c_str();
+ html[min(i++, len)] = R"(
+
+
+)";
+ html[min(i++, len)] = R"( )";
+#if TOUCH_DRIVER == 0x2046 && defined(TOUCH_CS)
+ html[min(i++, len)] = R"( )";
+#endif
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
{ // Execute Actions
if(webServer.hasArg(F("cal"))) dispatch_calibrate(NULL, NULL, TAG_HTTP);
@@ -1539,91 +1553,138 @@ static void webHandleHttpConfig()
{ // http://plate01/config/http
if(!http_is_authenticated(F("config/http"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_HTTP_SETTINGS " ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- // Form
- httpMessage += F("");
-
- // Username
- httpMessage += F("Username
");
- httpMessage += F("
");
-
- // Password
- httpMessage += F("Password
");
- httpMessage += F("
");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_SAVE_SETTINGS " ");
- httpMessage += F(" ");
-
- httpMessage += F("" D_HTTP_CONFIGURATION " ");
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+
+
+
+)";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
-static void webHandleFtpConfig()
+static void http_handle_ftp()
{ // http://plate01/config/http
if(!http_is_authenticated(F("config/ftp"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_FTP_SETTINGS " ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- // Form
- httpMessage += F("");
-
- // Username
- httpMessage += F("Username
");
- httpMessage += F("
");
-
- // Password
- httpMessage += F("Password
");
- httpMessage += F("
");
-
- // Ftp Port
- httpMessage += F("Port
");
- httpMessage += F("
");
-
- // Passiv Port
- httpMessage += F("Passif Port
");
- httpMessage += F("
");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_SAVE_SETTINGS " ");
- httpMessage += F(" ");
-
- httpMessage += F("" D_HTTP_CONFIGURATION " ");
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+
+
+
+
+
+)";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
+////////////////////////////////////////////////////////////////////////////////////////////////////
+static void http_handle_time()
+{ // http://plate01/config/time
+ if(!http_is_authenticated(F("config/time"))) return;
+
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
+
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+)";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
+}
////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_GPIO > 0
@@ -1636,7 +1697,7 @@ static void webHandleGpioConfig()
uint8_t id = webServer.arg(F("id")).toInt();
uint8_t pin = webServer.arg(F("pin")).toInt();
- if(webServer.hasArg(PSTR("save"))) {
+ if(webServer.hasArg("save")) {
uint8_t type = webServer.arg(F("type")).toInt();
uint8_t group = webServer.arg(F("group")).toInt();
uint8_t pinfunc = webServer.arg(F("func")).toInt();
@@ -1644,7 +1705,7 @@ static void webHandleGpioConfig()
gpioSavePinConfig(id, pin, type, group, pinfunc, inverted);
}
- if(webServer.hasArg(PSTR("del"))) {
+ if(webServer.hasArg("del")) {
gpioSavePinConfig(id, pin, hasp_gpio_type_t::FREE, 0, 0, false);
}
}
@@ -1652,9 +1713,9 @@ static void webHandleGpioConfig()
{ // Send Content
String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
+ httpMessage += "";
httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ httpMessage += " ";
httpMessage += F("" D_HTTP_GPIO_SETTINGS " ");
httpMessage += F("");
@@ -1786,7 +1847,7 @@ static void webHandleGpioConfig()
httpMessage += id;
httpMessage += ("&pin=");
httpMessage += conf.pin;
- httpMessage += ("' class='trash'>");
+ httpMessage += ("' class='icon trash'> ");
configCount++;
}
}
@@ -1822,9 +1883,9 @@ static void webHandleGpioOutput()
String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
+ httpMessage += "";
httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ httpMessage += " ";
httpMessage += F("");
httpMessage += F(" ");
httpMessage += F(" ");
-
- // Baudrate
- httpMessage += F("Serial Port
");
- httpMessage += F("
");
- httpMessage += getOption(-1, F(D_SETTING_DISABLED)); // Don't use 0 here which is default 115200
- httpMessage += getOption(0, F(D_SETTING_DEFAULT)); // Don't use 0 here which is default 115200
- httpMessage += getOption(9600, F("9600"));
- httpMessage += getOption(19200, F("19200"));
- httpMessage += getOption(38400, F("38400"));
- httpMessage += getOption(57600, F("57600"));
- httpMessage += getOption(74880, F("74880"));
- httpMessage += getOption(115200, F("115200"));
- httpMessage += getOption(230400, F("230400"));
- httpMessage += getOption(460800, F("460800"));
- httpMessage += getOption(921600, F("921600"));
- httpMessage += F("
");
-
- // Telemetry Period
- httpMessage += F("Telemetry Period
");
- httpMessage += F("
");
-
- // Invert
- httpMessage += F("
");
- httpMessage += F("
Use ANSI Colors
");
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+
+
+
+
+
+
+
+9600
+19200
+38400
+57600
+74880
+115200
+230400
+460800
+921600
+
+
+
+
+)";
#if HASP_USE_SYSLOG > 0
- // Syslog host
- httpMessage += F("Syslog Server
");
- httpMessage += F("
");
-
- // Syslog Port
- httpMessage += F("Syslog Port
");
- httpMessage += F("
");
-
- // Syslog Facility
- httpMessage += F("Syslog Facility
");
- httpMessage += F("
");
- for(int i = 0; i < 8; i++) {
- httpMessage += getOption(i, String(F("Local")) + i);
- }
- httpMessage += F("
");
-
- // Syslog Protocol
- httpMessage += F("");
+ html[min(i++, len)] = R"(
+
+
+
+
+
+Local0
+Local1
+Local2
+Local3
+Local4
+Local5
+Local6
+Local7
+
+
+)";
#endif
- // Submit & End Form
- httpMessage += F("" D_HTTP_SAVE_SETTINGS " ");
- httpMessage += F(" ");
-
- // *******************************************************************
-
- add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config"));
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2073,72 +2127,34 @@ static void webHandleHaspConfig()
{ // http://plate01/config/http
if(!http_is_authenticated(F("config/hasp"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_HASP_DESIGN " ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
-#if defined(ARDUINO_ARCH_ESP8266)
- // Form
- httpMessage += F("");
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
- // File
- httpMessage +=
- F("From File
");
- httpMessage += F("
");
-
- // Destination
- httpMessage += F("");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_UPLOAD_FILE " ");
- httpMessage += F(" ");
-#endif
-
- // Form
- httpMessage += F("");
-
- // Theme
- httpMessage += F("UI Theme
");
- httpMessage += F("
");
-#if LV_USE_THEME_HASP == 1
+ /*
+ #if LV_USE_THEME_HASP == 1
httpMessage += getOption(2, F("Hasp Dark"));
httpMessage += getOption(1, F("Hasp Light"));
-#endif
-#if LV_USE_THEME_EMPTY == 1
+ #endif
+ #if LV_USE_THEME_EMPTY == 1
httpMessage += getOption(0, F("Empty"));
-#endif
-#if LV_USE_THEME_MONO == 1
+ #endif
+ #if LV_USE_THEME_MONO == 1
httpMessage += getOption(3, F("Mono"));
-#endif
-#if LV_USE_THEME_MATERIAL == 1
+ #endif
+ #if LV_USE_THEME_MATERIAL == 1
httpMessage += getOption(5, F("Material Dark"));
httpMessage += getOption(4, F("Material Light"));
-#endif
-#if LV_USE_THEME_TEMPLATE == 1
+ #endif
+ #if LV_USE_THEME_TEMPLATE == 1
httpMessage += getOption(7, F("Template"));
-#endif
- httpMessage += F("
");
+ #endif
- // Primary Color
- httpMessage += F("Primary Color
");
- httpMessage += F("
");
-
- // Secondary Color
- httpMessage += F("Secondary Color
");
- httpMessage += F("
");
-
- // Font
- httpMessage += F("Default Font
");
- httpMessage += F("
None ");
-#if defined(ARDUINO_ARCH_ESP32)
+ #if defined(ARDUINO_ARCH_ESP32)
File root = HASP_FS.open("/");
File file = root.openNextFile();
@@ -2146,42 +2162,57 @@ static void webHandleHaspConfig()
String filename = file.name();
file = root.openNextFile();
}
-#elif defined(ARDUINO_ARCH_ESP8266)
- Dir dir = HASP_FS.openDir("/");
+ #endif
+ */
- while(dir.next()) {
- File file = dir.openFile("r");
- String filename = file.name();
- file.close();
- }
-#endif
- httpMessage += F("
");
-
- // Pages.jsonl
- httpMessage += F("Startup Layout
");
- httpMessage += F("
");
-
- // Startup Page
- httpMessage += F("Startup Page
");
- httpMessage += F("
");
-
- // Startup Brightness
- httpMessage += F("Startup Brightness
");
- httpMessage += F("
");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_SAVE_SETTINGS " ");
- httpMessage += F(" ");
-
- httpMessage += FPSTR(MAIN_MENU_BUTTON);
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ html[min(i++, len)] = R"(
+
+
+
+
+
+
+
+Hasp Dark
+Hasp Light
+Mono
+Material Dark
+Material Light
+
+
+
+
+
+
+
+
+)";
+ html[min(i++, len)] = R"( )";
+ html[min(i++, len)] = R"( )";
+ // html[min(i++, len)] = "";
+ http_send_content(html, min(i, len));
}
#endif // HASP_USE_CONFIG
@@ -2189,53 +2220,50 @@ static void webHandleHaspConfig()
////////////////////////////////////////////////////////////////////////////////////////////////////
#if HASP_USE_WIFI > 0
-static void webHandleWifiConfig()
+static void http_handle_wifi()
{ // http://plate01/config/wifi
if(!http_is_authenticated(F("config/wifi"))) return;
- { // Send Content
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_WIFI_SETTINGS " ");
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
- // Form
- httpMessage += F("");
-
- // Wifi SSID
- httpMessage += F("SSID
");
- httpMessage += F("
");
-
- // Wifi Password
- httpMessage += F("Password
");
- httpMessage += F("
");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_SAVE_SETTINGS " ");
- httpMessage += F(" ");
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+ html[min(i++, len)] = R"(
+
+
+
+
+
+)";
+ html[min(i++, len)] = R"( )";
#if HASP_USE_WIFI > 0 && !defined(STM32F4xx)
- if(WiFi.getMode() == WIFI_STA) {
- add_form_button(httpMessage, F(D_BACK_ICON D_HTTP_CONFIGURATION), F("/config"));
+ if(WiFi.getMode() == WIFI_STA) {
+ html[min(i++, len)] = R"( )";
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
- } else {
- add_form_button(httpMessage, F(D_HTTP_FIRMWARE_UPGRADE), F("/firmware"));
+ } else {
+ html[min(i++, len)] = R"( )";
#endif // ARDUINO_ARCH_ESP
- }
+ }
#endif // HASP_USE_WIFI
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
- }
- webSendFooter();
+ http_send_content(html, min(i, len));
}
#endif // HASP_USE_WIFI
@@ -2259,8 +2287,8 @@ static inline int handleFirmwareFile(String path)
return http_send_static_gzip_file(STYLE_CSS_GZ_START, STYLE_CSS_GZ_END, contentType);
} else if(path == F("/vars.css")) {
return http_send_static_file(HTTP_VARS_CSS, HTTP_VARS_CSS + sizeof(HTTP_VARS_CSS) - 1, contentType);
- } else if(path == F("/script.js")) { // 3 kB
- return http_send_static_gzip_file(SCRIPT_JS_GZ_START, SCRIPT_JS_GZ_END, contentType);
+ // } else if(path == F("/script.js")) { // 3 kB
+ // return http_send_static_gzip_file(SCRIPT_JS_GZ_START, SCRIPT_JS_GZ_END, contentType);
} else if(path == F("/en.json")) { // 2 kB
return http_send_static_gzip_file(EN_JSON_GZ_START, EN_JSON_GZ_END, contentType);
} else if(path == F("/main.js")) { // 9 kB
@@ -2342,88 +2370,62 @@ static void webHandleFirmware()
{
if(!http_is_authenticated(F("firmware"))) return;
- if(webServer.method() == HTTP_POST && webServer.hasArg(PSTR("url"))) {
+ const char* html[20];
+ int i = 0;
+ int len = (sizeof(html) / sizeof(html[0])) - 1;
+
+ html[min(i++, len)] = "";
+ html[min(i++, len)] = haspDevice.get_hostname();
+ html[min(i++, len)] = " ";
+
+ if(webServer.method() == HTTP_POST && webServer.hasArg("url")) {
StaticJsonDocument<512> settings;
for(int i = 0; i < webServer.args(); i++) settings[webServer.argName(i)] = webServer.arg(i);
bool updated = otaSetConfig(settings.as());
+ String url = webServer.arg("url");
- String url = webServer.arg(PSTR("url"));
- {
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ html[min(i++, len)] = R"(Updating firmware from: )";
+ html[min(i++, len)] = url.c_str();
+ html[min(i++, len)] = R"(
Please wait...
)";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
- httpMessage += F("" D_HTTP_FIRMWARE_UPGRADE " ");
- httpMessage += F("Updating firmware from: ");
- httpMessage += url;
- httpMessage += F("
Please wait...
");
-
- httpMessage += FPSTR(MAIN_MENU_BUTTON);
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 60);
- webServer.sendContent(httpMessage);
- }
-
- webSendFooter();
dispatch_web_update(NULL, url.c_str(), TAG_HTTP);
- return;
+
} else {
- // Send Firmware page
- String httpMessage((char*)0);
- httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
- httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
- httpMessage += F("" D_HTTP_FIRMWARE_UPGRADE " ");
- // Form
- httpMessage +=
- F("");
-
- // File
- httpMessage +=
- F("OTA File
");
- httpMessage += F("
");
-
- // Destination
- httpMessage += F("");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_UPDATE_FIRMWARE " ");
- httpMessage += F(" ");
-
- // Update from URL
- // Form
- httpMessage += F("");
-
- // URL
- httpMessage +=
- F("OTA URL
");
- httpMessage += F("
");
-
- // Redirect
- httpMessage += F("Follow Redirects
");
- httpMessage += F("
");
- httpMessage += getOption(0, F("Disabled"));
- httpMessage += getOption(1, F("Strict"));
- httpMessage += getOption(2, F("Always"));
- httpMessage += F("
");
-
- // Submit & End Form
- httpMessage += F("" D_HTTP_UPDATE_FIRMWARE " ");
- httpMessage += F(" ");
-
- httpMessage += FPSTR(MAIN_MENU_BUTTON);
-
- webSendHtmlHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
- webServer.sendContent(httpMessage);
+ html[min(i++, len)] = R"(
+
+
+
+
+
+
Follow Redirects
+
+
+
+
+
+
+
+
+
+
+)";
+ html[min(i++, len)] = R"( )";
+ http_send_content(html, min(i, len));
}
- webSendFooter();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -2438,9 +2440,9 @@ static void httpHandleResetConfig()
{ // Send Content
String httpMessage((char*)0);
httpMessage.reserve(HTTP_PAGE_SIZE);
- httpMessage += F("");
+ httpMessage += "";
httpMessage += haspDevice.get_hostname();
- httpMessage += F(" ");
+ httpMessage += " ";
httpMessage += F("" D_HTTP_FACTORY_RESET " ");
if(resetConfirmed) { // User has confirmed, so reset everything
@@ -2483,7 +2485,7 @@ static void httpHandleResetConfig()
void httpStart()
{
- webServer.begin();
+ webServer.begin(80);
webServerStarted = true;
#if HASP_USE_WIFI > 0
#if defined(STM32F4xx)
@@ -2565,12 +2567,12 @@ static inline void webStartConfigPortal()
dnsServer.start(DNS_PORT, "*", apIP);
#endif // HASP_USE_CAPTIVE_PORTAL
- webServer.on(F("/vars.css"), httpHandleFileUri);
- webServer.on(F("/style.css"), httpHandleFileUri);
- webServer.on(F("/script.js"), httpHandleFileUri);
- // reply to all requests with same HTML
+ webServer.on("/vars.css", httpHandleFileUri);
+ webServer.on(UriBraces("/static/{}/"), httpHandleFileUri);
+ // webServer.on("/script.js", httpHandleFileUri);
+// reply to all requests with same HTML
#if HASP_USE_WIFI > 0
- webServer.onNotFound(webHandleWifiConfig);
+ webServer.onNotFound(http_handle_wifi);
#endif
LOG_TRACE(TAG_HTTP, F("Wifi access point"));
}
@@ -2584,21 +2586,23 @@ void httpSetup()
LOG_DEBUG(TAG_HTTP, F(D_BULLET "Read %s => %s (%d bytes)"), FP_CONFIG_PASS, password.c_str(), password.length());
// ask server to track these headers
- const char* headerkeys[] = {"Content-Length", "If-None-Match"}; // "Authentication" is automatically checked
+ const char* headerkeys[] = {"Content-Length", "If-None-Match",
+ "Cookie"}; // "Authentication" is automatically checked
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
webServer.collectHeaders(headerkeys, headerkeyssize);
// Shared pages between STA and AP
- webServer.on(F("/about"), webHandleAbout);
- // webServer.on(F("/vars.css"), webSendCssVars);
- // webServer.on(F("/js"), webSendJavascript);
- webServer.on(UriBraces(F("/api/{}/")), webHandleApi);
- webServer.on(UriBraces(F("/api/config/{}/")), webHandleApiConfig);
- webServer.on(UriBraces(F("/{}/")), HTTP_GET, []() { httpHandleFile(F("/hasp.htm")); });
- webServer.on(UriBraces(F("/config/{}/")), HTTP_GET, []() { httpHandleFile(F("/hasp.htm")); });
+ webServer.on("/about", http_handle_about);
+ // webServer.on("/vars.css", webSendCssVars);
+ // webServer.on("/js", webSendJavascript);
+ webServer.on(UriBraces("/api/config/{}/"), webHandleApiConfig);
+ webServer.on(UriBraces("/api/{}/"), webHandleApi);
+
+ webServer.on(UriBraces("/config/{}/"), HTTP_GET, []() { httpHandleFile(F("/hasp.htm")); }); // SPA Route
+ webServer.on(UriBraces("/{}/"), HTTP_GET, []() { httpHandleFile(F("/hasp.htm")); }); // SPA Route
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
- webServer.on(F("/firmware"), webHandleFirmware);
+ webServer.on("/firmware", webHandleFirmware);
webServer.on(
F("/update"), HTTP_POST,
[]() {
@@ -2609,7 +2613,7 @@ void httpSetup()
#endif
#ifdef HTTP_LEGACY
- webServer.on(F("/config"), webHandleConfig);
+ webServer.on("/config", http_handle_config);
#endif
#if HASP_USE_WIFI > 0
@@ -2626,7 +2630,7 @@ void httpSetup()
#endif // HASP_USE_WIFI
// The following endpoints are only needed in STA mode
- webServer.on(F("/page/"), []() {
+ webServer.on("/page/", []() {
String pageid = webServer.arg(F("page"));
webServer.send(200, PSTR("text/plain"), "Page: '" + pageid + "'");
dispatch_page(NULL, webServer.arg(F("page")).c_str(), TAG_HTTP);
@@ -2634,51 +2638,53 @@ void httpSetup()
});
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
- webServer.on(F("/list"), HTTP_GET, handleFileList);
+ webServer.on("/list", HTTP_GET, handleFileList);
// load editor
- webServer.on(F("/edit"), HTTP_GET, []() { httpHandleFile(F("/edit.htm")); });
- webServer.on(F("/edit"), HTTP_PUT, handleFileCreate);
- webServer.on(F("/edit"), HTTP_DELETE, handleFileDelete);
+ webServer.on("/edit", HTTP_GET, []() { httpHandleFile(F("/edit.htm")); });
+ webServer.on("/edit", HTTP_PUT, handleFileCreate);
+ webServer.on("/edit", HTTP_DELETE, handleFileDelete);
// first callback is called after the request has ended with all parsed arguments
// second callback handles file uploads at that location
webServer.on(
F("/edit"), HTTP_POST,
[]() {
- webServer.send(200, "text/plain", "");
+ webServer.setContentLength(CONTENT_LENGTH_NOT_SET);
+ webServer.send(200, "text/plain", "OK");
LOG_VERBOSE(TAG_HTTP, F("Headers: %d"), webServer.headers());
},
handleFileUpload);
#endif
- webServer.on(F("/"), webHandleRoot);
- webServer.on(F("/screenshot"), webHandleScreenshot);
+ webServer.on("/", http_handle_root);
+ webServer.on("/screenshot", http_handle_screenshot);
#ifdef HTTP_LEGACY
- webServer.on(F("/info"), webHandleInfoJson);
- webServer.on(F("/reboot"), httpHandleReboot);
+ webServer.on("/info", http_handle_info);
+ webServer.on("/reboot", http_handle_reboot);
#endif
#if HASP_USE_CONFIG > 0
#ifdef HTTP_LEGACY
- webServer.on(F("/config/hasp"), webHandleHaspConfig);
- webServer.on(F("/config/http"), webHandleHttpConfig);
- webServer.on(F("/config/gui"), webHandleGuiConfig);
- webServer.on(F("/config/debug"), webHandleDebugConfig);
+ webServer.on("/config/hasp", webHandleHaspConfig);
+ webServer.on("/config/http", webHandleHttpConfig);
+ webServer.on("/config/gui", http_handle_gui);
+ webServer.on("/config/time", http_handle_time);
+ webServer.on("/config/debug", http_handle_debug);
#if HASP_USE_MQTT > 0
- webServer.on(F("/config/mqtt"), webHandleMqttConfig);
+ webServer.on("/config/mqtt", http_handle_mqtt);
#endif
#if HASP_USE_FTP > 0
- webServer.on(F("/config/ftp"), webHandleFtpConfig);
+ webServer.on("/config/ftp", http_handle_ftp);
#endif
#if HASP_USE_WIFI > 0
- webServer.on(F("/config/wifi"), webHandleWifiConfig);
+ webServer.on("/config/wifi", http_handle_wifi);
#endif
#if HASP_USE_GPIO > 0
- webServer.on(F("/config/gpio"), webHandleGpioConfig);
- webServer.on(F("/config/gpio/options"), webHandleGpioOutput);
- webServer.on(F("/config/gpio/input"), webHandleGpioInput);
+ webServer.on("/config/gpio", webHandleGpioConfig);
+ webServer.on("/config/gpio/options", webHandleGpioOutput);
+ webServer.on("/config/gpio/input", webHandleGpioInput);
#endif
#endif // HTTP_LEGACY
- webServer.on(F("/config/reset"), httpHandleResetConfig);
+ webServer.on("/config/reset", httpHandleResetConfig);
#endif // HASP_USE_CONFIG
webServer.onNotFound(httpHandleFileUri);
@@ -2757,7 +2763,7 @@ size_t httpClientWrite(const uint8_t* buf, size_t size)
/***** Sending 16Kb at once freezes on STM32 EthernetClient *****/
size_t bytes_sent = 0;
while(bytes_sent < size) {
- if(!webServer.client()) return bytes_sent;
+ if(!webServer.client() || !webServer.client().connected()) return bytes_sent;
if(size - bytes_sent >= 20480) {
bytes_sent += webServer.client().write(buf + bytes_sent, 20480); // 2048
delay(1); // Fixes the freeze
diff --git a/src/sys/svc/hasp_http.h b/src/sys/svc/hasp_http.h
index a09fcf4b..bdbe13c4 100644
--- a/src/sys/svc/hasp_http.h
+++ b/src/sys/svc/hasp_http.h
@@ -77,6 +77,16 @@ bool httpSetConfig(const JsonObject& settings);
#ifndef D_HTTP_COLOR_FOOTER_TEXT
#define D_HTTP_COLOR_FOOTER_TEXT "#0083cc" // Text color of the page footer
#endif
+#ifndef D_HTTP_COLOR_TOOLBAR
+#define D_HTTP_COLOR_TOOLBAR D_HTTP_COLOR_BUTTON
+#endif
+#ifndef D_HTTP_COLOR_TREE
+#define D_HTTP_COLOR_TREE D_HTTP_COLOR_GROUP
+#endif
+#ifndef D_HTTP_COLOR_PREVIEW
+#define D_HTTP_COLOR_PREVIEW "#888"
+#endif
+
/* clang-format on */
#endif
\ No newline at end of file
diff --git a/tools/auto_firmware_version.py b/tools/auto_firmware_version.py
index 6d6fc3a6..9a80b0d0 100644
--- a/tools/auto_firmware_version.py
+++ b/tools/auto_firmware_version.py
@@ -1,4 +1,4 @@
-import pkg_resources
+import gzip, pkg_resources
Import("env")
@@ -33,4 +33,18 @@ def get_flash_size():
env.Append(
BUILD_FLAGS=[get_firmware_commit_hash(),get_flash_size()]
-)
\ No newline at end of file
+)
+
+r = Repo('.')
+commit_hash = r.head().decode("utf-8")[0:7]
+with open("data/editor.htm", "r", encoding="utf-8") as f:
+ html=f.read()
+html = html.replace("COMMIT_HASH", commit_hash)
+with gzip.open('data/static/edit.htm.gz', 'wb') as f:
+ f.write(html.encode('utf-8'))
+
+with open("data/main.js", "r", encoding="utf-8") as f:
+ html=f.read()
+html = html.replace("COMMIT_HASH", commit_hash)
+with gzip.open('data/static/main.js.gz', 'wb') as f:
+ f.write(html.encode('utf-8'))