mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-28 05:36:37 +00:00
Cache css, favicon and edit.htm in the browser
This commit is contained in:
parent
5380276ea1
commit
243a9aac31
@ -107,8 +107,8 @@ const char HTTP_DOCTYPE[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><met
|
|||||||
"user-scalable=no\"/>";
|
"user-scalable=no\"/>";
|
||||||
const char HTTP_META_GO_BACK[] PROGMEM = "<meta http-equiv='refresh' content='15;url=/'/>";
|
const char HTTP_META_GO_BACK[] PROGMEM = "<meta http-equiv='refresh' content='15;url=/'/>";
|
||||||
const char HTTP_HEADER[] PROGMEM = "<title>%s</title>";
|
const char HTTP_HEADER[] PROGMEM = "<title>%s</title>";
|
||||||
const char HTTP_STYLE[] PROGMEM =
|
const char HTTP_STYLE[] PROGMEM = "<link rel=\"stylesheet\" href=\"/css\">";
|
||||||
"<style>"
|
const char HTTP_CSS[] PROGMEM =
|
||||||
"body,.c{text-align:center;}"
|
"body,.c{text-align:center;}"
|
||||||
"div,input{padding:5px;font-size:1em;}"
|
"div,input{padding:5px;font-size:1em;}"
|
||||||
"a{color:" D_HTTP_COLOR_TEXT "}"
|
"a{color:" D_HTTP_COLOR_TEXT "}"
|
||||||
@ -120,16 +120,14 @@ const char HTTP_STYLE[] PROGMEM =
|
|||||||
"body{font-family:verdana;width:60%;margin:auto;background:" D_HTTP_COLOR_BACKGROUND ";color:" D_HTTP_COLOR_TEXT
|
"body{font-family:verdana;width:60%;margin:auto;background:" D_HTTP_COLOR_BACKGROUND ";color:" D_HTTP_COLOR_TEXT
|
||||||
";}"
|
";}"
|
||||||
"button{border:0;border-radius:0.6rem;background-color:" D_HTTP_COLOR_BUTTON ";color:" D_HTTP_COLOR_BUTTON_TEXT
|
"button{border:0;border-radius:0.6rem;background-color:" D_HTTP_COLOR_BUTTON ";color:" D_HTTP_COLOR_BUTTON_TEXT
|
||||||
";line-height:2.4rem;font-size:1.2rem;"
|
";line-height:2.4rem;font-size:1.2rem;width:100%;}"
|
||||||
"width:100%;}"
|
|
||||||
//".q{float:right;width:64px;text-align:right;}"
|
//".q{float:right;width:64px;text-align:right;}"
|
||||||
".red{background-color:" D_HTTP_COLOR_BUTTON_RESET ";}"
|
".red{background-color:" D_HTTP_COLOR_BUTTON_RESET ";}"
|
||||||
// ".button3{background-color:#f44336;}"
|
// ".button3{background-color:#f44336;}"
|
||||||
// ".button4{background-color:#e7e7e7;color:black;}"
|
// ".button4{background-color:#e7e7e7;color:black;}"
|
||||||
// ".button5{background-color:#555555;}"
|
// ".button5{background-color:#555555;}"
|
||||||
// ".button6{background-color:#4CAF50;}"
|
// ".button6{background-color:#4CAF50;}"
|
||||||
"td{font-size:0.87rem;padding-bottom:0px;padding-top:0px;}th{padding-top:0.5em;}"
|
"td{font-size:0.87rem;padding-bottom:0px;padding-top:0px;}th{padding-top:0.5em;}";
|
||||||
"</style>";
|
|
||||||
const char HTTP_SCRIPT[] PROGMEM = "<script>function "
|
const char HTTP_SCRIPT[] PROGMEM = "<script>function "
|
||||||
"c(l){document.getElementById('s').value=l.innerText||l.textContent;document."
|
"c(l){document.getElementById('s').value=l.innerText||l.textContent;document."
|
||||||
"getElementById('p').focus();}</script>";
|
"getElementById('p').focus();}</script>";
|
||||||
@ -265,6 +263,17 @@ void webSendFooter()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int webSendCached(int statuscode, char* contenttype, char* data, size_t size)
|
||||||
|
{
|
||||||
|
webServer.sendHeader(F("Cache-Control"), F("public, max-age=604800, immutable"));
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
|
||||||
|
webServer.send_P(statuscode, contenttype, data, size);
|
||||||
|
#else
|
||||||
|
webServer.send(statuscode, contenttype, data);
|
||||||
|
#endif
|
||||||
|
return statuscode;
|
||||||
|
}
|
||||||
|
|
||||||
void webSendPage(const char* nodename, uint32_t httpdatalength, bool gohome = false)
|
void webSendPage(const char* nodename, uint32_t httpdatalength, bool gohome = false)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@ -989,15 +998,12 @@ int handleFileRead(String path)
|
|||||||
if(path == F("/edit.htm")) {
|
if(path == F("/edit.htm")) {
|
||||||
size_t size = EDIT_HTM_GZ_END - EDIT_HTM_GZ_START;
|
size_t size = EDIT_HTM_GZ_END - EDIT_HTM_GZ_START;
|
||||||
webServer.sendHeader(F("Content-Encoding"), F("gzip"));
|
webServer.sendHeader(F("Content-Encoding"), F("gzip"));
|
||||||
webServer.send_P(200, PSTR("text/html"), (const char*)EDIT_HTM_GZ_START, size);
|
return webSendCached(200, PSTR("text/html"), (const char*)EDIT_HTM_GZ_START, size); // OK
|
||||||
return 200; // OK
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(!strcasecmp_P(path.c_str(), PSTR("/favicon.ico"))) {
|
if(!strcasecmp_P(path.c_str(), PSTR("/favicon.ico")))
|
||||||
webServer.send_P(204, PSTR("image/bmp"), "", 0); // No content
|
return webSendCached(204, PSTR("image/bmp"), "", 0); // No content
|
||||||
return 204;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 404; // Not found
|
return 404; // Not found
|
||||||
}
|
}
|
||||||
@ -1908,17 +1914,15 @@ void httpHandleNotFound()
|
|||||||
int statuscode = 404;
|
int statuscode = 404;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(statuscode == 204) return; // No content
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
|
||||||
LOG_TRACE(TAG_HTTP, F("Sending %d %s to client connected from: %s"), statuscode, webServer.uri().c_str(),
|
LOG_TRACE(TAG_HTTP, F("Sending %d %s to client connected from: %s"), statuscode, webServer.uri().c_str(),
|
||||||
webServer.client().remoteIP().toString().c_str());
|
webServer.client().remoteIP().toString().c_str());
|
||||||
#else
|
#else
|
||||||
// LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"),
|
// LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"),
|
||||||
// String(webServer.client().remoteIP()).c_str());
|
// String(webServer.client().remoteIP()).c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(statuscode == 200) return; // OK
|
if(statuscode < 300) return; // OK
|
||||||
|
|
||||||
String httpMessage((char*)0);
|
String httpMessage((char*)0);
|
||||||
httpMessage.reserve(HTTP_PAGE_SIZE);
|
httpMessage.reserve(HTTP_PAGE_SIZE);
|
||||||
@ -2112,6 +2116,7 @@ void httpSetup()
|
|||||||
|
|
||||||
// Shared pages
|
// Shared pages
|
||||||
webServer.on(F("/about"), webHandleAbout);
|
webServer.on(F("/about"), webHandleAbout);
|
||||||
|
webServer.on(F("/css"), []() { webSendCached(200, PSTR("text/css"), HTTP_CSS, sizeof(HTTP_CSS) - 1); });
|
||||||
webServer.onNotFound(httpHandleNotFound);
|
webServer.onNotFound(httpHandleNotFound);
|
||||||
|
|
||||||
#if HASP_USE_WIFI > 0
|
#if HASP_USE_WIFI > 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user