From a232f1c647c4d446b78d25e2bb61c11d3394822a Mon Sep 17 00:00:00 2001 From: Scott M Griffis Date: Tue, 16 Apr 2024 23:52:11 -0500 Subject: [PATCH] Improved admin stability --- .DS_Store | Bin 0 -> 6148 bytes lib/.DS_Store | Bin 0 -> 6148 bytes src/main.cpp | 84 ++++++++++++++++++++------------------------------ 3 files changed, 33 insertions(+), 51 deletions(-) create mode 100644 .DS_Store create mode 100644 lib/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9369f96cee47f734f82e64e06377e114aa8d9c2e GIT binary patch literal 6148 zcmeHK%}T>S5Z-O7O(;SS3Oz1(E!aPb;w9Aj0!H+pQWH}&7_-u(=1>Yb>kIiLK94iI zTd`K@O{C7i?6*5RlQ3Vx&JJUY_a|YCu^MB{21P76FnlApkGdo!V<`i2d`9{h#4>=@ zYw)<_O@_b70De1*#Vle$kbnMuag=7w<{K|r<&DjXU9qe7m3Jp|FY~iW+V{s7xH^?G z4(4+|IEzO4u(o|9lgy8j(O3mU;RswV&!Z%id0$SFFjcXh-mo3p8P@94X{Xz2h<0y( z))3Q!cC#V6o!)Hb*gLy>hbP1P_#u(cnnDhLo02Vqb9jM?$>zx$C$UU!!GhivOWBy+ ztWAz8n=d)eSs)|^hyh|?6$9oJwX3TfX`dJ%2L2rbcs>YFMAu-UQ5_vHs1^VauaQb% zj=coN2!pP{LL=;ea2*P$L%C@&xDE%qFn+GVLZc36+%zBDyqTL0g{!y2^M!I}+|@`U zF+dC~GGOUt4eS5$&;9>m5Uq#-VqmQp;N^ig=tD}Twk{=ywN`*$gQB2cq46UGCaM&J fFP7pus1mRXoB+B83yoj_p^Jc`fd*pWR~dK*xt4^F literal 0 HcmV?d00001 diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..775034b177b0ed67eb6c1bd17988af5b906515ad GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8-BN@e6nb3nTCjf<#Y?F51&ruHr6wk5FlI}W+CwSitS{t~_&m<+ zZiH$No+f`8&b#dc9sJWYY*(@D;(<>aE zD;4{N?)eu{I3G85PgIh5Q4&sdLKK7$a(x*kftnB1ED2Jb>l%lsiQ2f)Tr7J1PD^$N zhs%~+9Ch0*+3yXO%bM8RKR7-cKgLgqdNEXTU|q|O#R6VI`BK%hJ56Gh+=HjcDxwLA z0b+m{AO<#z0ecRJ`ev0))e-~5zz-O}{Xs%QbS&lu_0|C$UZ2rlK|}!^-x7$@qGK^P z2p$k_QUOgWw@(ai(!nlmoMSOJXwn(iE5kf?<>K+e_3B`kI-GIGAoaumF|f)&*$f+a z{-44xv-Xj{nnELDfEf5^4Diax9SxxSettings update Successful!

Device will reboot now...

"); - Serial.println("Sending '/admin' content to client, then rebooting to apply changes."); - webServer.send(200, "text/html", htmlPageTemplate(settings.getTitle(), F("Device Settings"), content)); + sendHtmlPageUsingTemplate(200, settings.getTitle(), F("Device Settings"), content); yield(); delay(1000); ESP.restart(); } else { // No reboot needed; Send to home page... - Serial.println("Sending '/admin' content to client."); content = F("
Settings update Successful!

Home

"); - webServer.send( - 200, - "text/html", - htmlPageTemplate( - settings.getTitle(), - F("Device Settings"), - content, - "/", - 5 - ) + sendHtmlPageUsingTemplate( + 200, + settings.getTitle(), + F("Device Settings"), + content, + "/", + 5 ); yield(); return; } } else { // Error... - Serial.println("Sending '/admin' content to client; An error prevented saving settings!"); content = F("
Error Saving Settings!!!
"); - webServer.send( - 500, - "text/html", - htmlPageTemplate( - F("500 - Server Error"), - F("Server Error!"), - content, - "/", - 5 - ) + sendHtmlPageUsingTemplate( + 500, + F("500 - Server Error"), + F("Server Error!"), + content, + "/", + 5 ); yield(); @@ -638,11 +624,7 @@ void endpointHandlerAdmin() { } } - Serial.println(F("Seinding '/admin' content to client.")); - String htmlPage = htmlPageTemplate(settings.getTitle(), F("Device Settings"), content); - - webServer.send(200, "text/html", htmlPage); - yield(); + sendHtmlPageUsingTemplate(200, settings.getTitle(), F("Device Settings"), content); } /** @@ -651,9 +633,8 @@ void endpointHandlerAdmin() { * */ void notFoundHandler() { - Serial.printf("Client requested '%s'; 404 - Not Found send to client!\n", webServer.uri().c_str()); String content = F("Just kidding...
But seriously what you were looking for doesn't exist."); - webServer.send(404, "text/html", htmlPageTemplate(F("404 Not Found"), F("OOPS! You broke it!!!"), content)); + sendHtmlPageUsingTemplate(404, F("404 Not Found"), F("OOPS! You broke it!!!"), content); } /** @@ -661,9 +642,8 @@ void notFoundHandler() { * This function handles file upload requests. */ void fileUploadHandler() { - Serial.println(F("Client attempted to upload a file and I was like Wuuuut!?")); String content = F("Um, I don't want your nasty files, go peddle that junk elsewhere!"); - webServer.send(400, "text/html", htmlPageTemplate(F("400 Bad Request"), F("Uhhh, Wuuuuut!?"), content)); + sendHtmlPageUsingTemplate(400, F("400 Bad Request"), F("Uhhh, Wuuuuut!?"), content); } /** @@ -672,17 +652,18 @@ void fileUploadHandler() { * This function is used to Generate the HTML for a web page where the * title, heading and content is provided to the function as parameters. * - * @param title - The page's title as String. - * @param heading - The heading that appears on the info page as String. - * @param content - The main content of the web page as String. - * @param redirectUrl - OPTIONAL PARAM, used to specify a page that this page should + * @param code The HTTP Code as int. + * @param title The page's title as String. + * @param heading The heading that appears on the info page as String. + * @param content The main content of the web page as String. + * @param redirectUrl OPTIONAL PARAM, used to specify a page that this page should * redirect to after a specified amount of time. - * @param delaySeconds - OPTIONAL PARAM, the number of seconds to delay before sending + * @param delaySeconds OPTIONAL PARAM, the number of seconds to delay before sending * the client to the redirectUrl, as int. */ -String htmlPageTemplate(String title, String heading, String &content, String redirectUrl, int delaySeconds) { +void sendHtmlPageUsingTemplate(int code, String title, String heading, String &content, String redirectUrl, int delaySeconds) { String result = HTML_PAGE_TEMPLATE; - if (!result.reserve(8000U)) { + if (!result.reserve(6000U)) { Serial.println(F("WARNING!!! htmlPageTemplate() failed to reserve desired memory!")); } @@ -703,5 +684,6 @@ String htmlPageTemplate(String title, String heading, String &content, String r result.replace("${metainsert}", temp); } - return result; + webServer.send(code, "text/html", result); + yield(); } \ No newline at end of file