From 951b4293c44fa87c2f302c90fa7a345c7e183111 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Fri, 6 Nov 2020 22:21:20 -0800 Subject: [PATCH] Set web cookie and web text editor. --- src/meshwifi/meshhttp.cpp | 157 +++++++++++++++++++++++++++++++++++++- src/meshwifi/meshhttp.h | 2 + 2 files changed, 157 insertions(+), 2 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index fb4dc059..b9f46226 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -54,6 +54,7 @@ void handleHotspot(HTTPRequest *req, HTTPResponse *res); void handleFavicon(HTTPRequest *req, HTTPResponse *res); void handleRoot(HTTPRequest *req, HTTPResponse *res); void handleStaticBrowse(HTTPRequest *req, HTTPResponse *res); +void handleStaticPost(HTTPRequest *req, HTTPResponse *res); void handleStatic(HTTPRequest *req, HTTPResponse *res); void handle404(HTTPRequest *req, HTTPResponse *res); void handleFormUpload(HTTPRequest *req, HTTPResponse *res); @@ -229,6 +230,7 @@ void initWebServer() ResourceNode *nodeFavicon = new ResourceNode("/favicon.ico", "GET", &handleFavicon); ResourceNode *nodeRoot = new ResourceNode("/", "GET", &handleRoot); ResourceNode *nodeStaticBrowse = new ResourceNode("/static", "GET", &handleStaticBrowse); + ResourceNode *nodeStaticPOST = new ResourceNode("/static", "POST", &handleStaticPost); ResourceNode *nodeStatic = new ResourceNode("/static/*", "GET", &handleStatic); ResourceNode *node404 = new ResourceNode("", "GET", &handle404); ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload); @@ -241,6 +243,7 @@ void initWebServer() secureServer->registerNode(nodeFavicon); secureServer->registerNode(nodeRoot); secureServer->registerNode(nodeStaticBrowse); + secureServer->registerNode(nodeStaticPOST); secureServer->registerNode(nodeStatic); secureServer->setDefaultNode(node404); secureServer->setDefaultNode(nodeFormUpload); @@ -255,6 +258,7 @@ void initWebServer() insecureServer->registerNode(nodeFavicon); insecureServer->registerNode(nodeRoot); insecureServer->registerNode(nodeStaticBrowse); + insecureServer->registerNode(nodeStaticPOST); insecureServer->registerNode(nodeStatic); insecureServer->setDefaultNode(node404); insecureServer->setDefaultNode(nodeFormUpload); @@ -294,11 +298,82 @@ void middlewareSpeedUp160(HTTPRequest *req, HTTPResponse *res, std::functionprintln("File Edited

File Edited

"); + + // The form is submitted with the x-www-form-urlencoded content type, so we need the + // HTTPURLEncodedBodyParser to read the fields. + // Note that the content of the file's content comes from a
"); + res->println(""); + res->println(""); + res->println(""); + + return; + } + res->println("

Upload new file

"); res->println("

*** This interface is experimental ***

"); res->println("

This form allows you to upload files. Keep your filenames very short and files small. Big filenames and big " @@ -339,6 +455,15 @@ void handleStaticBrowse(HTTPRequest *req, HTTPResponse *res) res->println(""); res->println(""); + res->println(""); + res->println(""); + res->println(""); + res->println(""); + res->println(""); + File file = root.openNextFile(); while (file) { String filePath = String(file.name()); @@ -360,8 +485,14 @@ void handleStaticBrowse(HTTPRequest *req, HTTPResponse *res) res->println(""); res->println(""); + res->println(""); + res->println(""); } file = root.openNextFile(); @@ -369,7 +500,7 @@ void handleStaticBrowse(HTTPRequest *req, HTTPResponse *res) res->println("
File"); + res->println("Size"); + res->println("Actions"); + res->println("
"); res->print("X"); + "\" onclick=\"return confirm_delete()\">Delete "); res->println(""); + if (!String(file.name()).substring(1).endsWith(".gz")) { + res->print("Edit"); + } + res->println("
"); res->print("
"); - res->print("Total : " + String(SPIFFS.totalBytes()) + " Bytes
"); + // res->print("Total : " + String(SPIFFS.totalBytes()) + " Bytes
"); res->print("Used : " + String(SPIFFS.usedBytes()) + " Bytes
"); res->print("Free : " + String(SPIFFS.totalBytes() - SPIFFS.usedBytes()) + " Bytes
"); } @@ -700,8 +831,19 @@ void handleAPIv1ToRadio(HTTPRequest *req, HTTPResponse *res) */ void handleRoot(HTTPRequest *req, HTTPResponse *res) { + res->setHeader("Content-Type", "text/html"); + randomSeed(millis()); + + res->setHeader("Set-Cookie", + "mt_session=" + httpsserver::intToString(random(1, 9999999)) + "; Expires=Wed, 20 Apr 2049 4:20:00 PST"); + + std::string cookie = req->getHeader("Cookie"); + //String cookieString = cookie.c_str(); + //uint8_t nameIndex = cookieString.indexOf("mt_session"); + //DEBUG_MSG(cookie.c_str()); + std::string filename = "/static/index.html"; std::string filenameGzip = "/static/index.html.gz"; @@ -748,3 +890,14 @@ void handleFavicon(HTTPRequest *req, HTTPResponse *res) // Write data from header file res->write(FAVICON_DATA, FAVICON_LENGTH); } + +void replaceAll(std::string &str, const std::string &from, const std::string &to) +{ + if (from.empty()) + return; + size_t start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' + } +} \ No newline at end of file diff --git a/src/meshwifi/meshhttp.h b/src/meshwifi/meshhttp.h index ce4a346c..b7a84475 100644 --- a/src/meshwifi/meshhttp.h +++ b/src/meshwifi/meshhttp.h @@ -22,6 +22,8 @@ void handleRoot(); void handleScriptsScriptJS(); void handleJSONChatHistoryDummy(); +void replaceAll(std::string& str, const std::string& from, const std::string& to); + class HttpAPI : public PhoneAPI {