#557 - Endpoint to restart device over http(s)

This commit is contained in:
Jm Casler 2020-12-12 12:36:16 -08:00
parent 5852caa61c
commit 696255c1f7

View File

@ -56,6 +56,7 @@ 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 handleRestart(HTTPRequest *req, HTTPResponse *res);
void handle404(HTTPRequest *req, HTTPResponse *res);
void handleFormUpload(HTTPRequest *req, HTTPResponse *res);
@ -70,10 +71,11 @@ uint32_t timeSpeedUp = 0;
// We need to specify some content-type mapping, so the resources get delivered with the
// right content type and are displayed correctly in the browser
char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"}, {".js", "text/javascript"},
{".png", "image/png"}, {".jpg", "image/jpg"}, {".gz", "application/gzip"},
{".gif", "image/gif"}, {".json", "application/json"}, {".css", "text/css"},
{"", ""}};
char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"},
{".js", "text/javascript"}, {".png", "image/png"},
{".jpg", "image/jpg"}, {".gz", "application/gzip"},
{".gif", "image/gif"}, {".json", "application/json"},
{".css", "text/css"}, {"", ""}};
void handleWebResponse()
{
@ -233,6 +235,7 @@ void initWebServer()
ResourceNode *nodeStaticBrowse = new ResourceNode("/static", "GET", &handleStaticBrowse);
ResourceNode *nodeStaticPOST = new ResourceNode("/static", "POST", &handleStaticPost);
ResourceNode *nodeStatic = new ResourceNode("/static/*", "GET", &handleStatic);
ResourceNode *nodeRestart = new ResourceNode("/restart", "GET", &handleRestart);
ResourceNode *node404 = new ResourceNode("", "GET", &handle404);
ResourceNode *nodeFormUpload = new ResourceNode("/upload", "POST", &handleFormUpload);
@ -246,6 +249,7 @@ void initWebServer()
secureServer->registerNode(nodeStaticBrowse);
secureServer->registerNode(nodeStaticPOST);
secureServer->registerNode(nodeStatic);
secureServer->registerNode(nodeRestart);
secureServer->setDefaultNode(node404);
secureServer->setDefaultNode(nodeFormUpload);
@ -261,6 +265,7 @@ void initWebServer()
insecureServer->registerNode(nodeStaticBrowse);
insecureServer->registerNode(nodeStaticPOST);
insecureServer->registerNode(nodeStatic);
insecureServer->registerNode(nodeRestart);
insecureServer->setDefaultNode(node404);
insecureServer->setDefaultNode(nodeFormUpload);
@ -512,7 +517,6 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res)
// Get access to the parameters
ResourceParameters *params = req->getParams();
std::string parameter1;
// Print the first parameter value
if (params->getPathParameter(0, parameter1)) {
@ -560,7 +564,7 @@ void handleStatic(HTTPRequest *req, HTTPResponse *res)
cTypeIdx += 1;
} while (strlen(contentTypes[cTypeIdx][0]) > 0);
if(!has_set_content_type) {
if (!has_set_content_type) {
// Set a default content type
res->setHeader("Content-Type", "application/octet-stream");
}
@ -845,9 +849,9 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res)
"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());
// 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";
@ -860,7 +864,8 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res)
res->printf("<p>File not found: %s</p>\n", filename.c_str());
res->printf("<p></p>\n");
res->printf("<p>You have gotten this error because the filesystem for the web server has not been loaded.</p>\n");
res->printf("<p>Please review the 'Common Problems' section of the <a href=https://github.com/meshtastic/Meshtastic-device/issues/552>web interface</a> documentation.</p>\n");
res->printf("<p>Please review the 'Common Problems' section of the <a "
"href=https://github.com/meshtastic/Meshtastic-device/issues/552>web interface</a> documentation.</p>\n");
return;
}
@ -881,7 +886,6 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res)
}
}
// Read the file from SPIFFS and write it to the HTTP response body
size_t length = 0;
do {
@ -892,6 +896,16 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res)
} while (length > 0);
}
void handleRestart(HTTPRequest *req, HTTPResponse *res)
{
res->setHeader("Content-Type", "text/html");
DEBUG_MSG("***** Restarted on HTTP(s) Request *****\n");
res->println("Restarting");
ESP.restart();
}
void handleFavicon(HTTPRequest *req, HTTPResponse *res)
{
// Set Content-Type
@ -900,7 +914,6 @@ void handleFavicon(HTTPRequest *req, HTTPResponse *res)
res->write(FAVICON_DATA, FAVICON_LENGTH);
}
void replaceAll(std::string &str, const std::string &from, const std::string &to)
{
if (from.empty())
@ -911,4 +924,3 @@ void replaceAll(std::string &str, const std::string &from, const std::string &to
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}