Fix Indentations and Shutdown Handling

This commit is contained in:
Elia Zammuto
2021-05-11 22:19:29 +02:00
parent fd8cbf0c7d
commit 04421d84a3
+113 -77
View File
@@ -30,27 +30,27 @@ std::string read_file(std::string path);
namespace confighttp namespace confighttp
{ {
using namespace std::literals; using namespace std::literals;
constexpr auto PORT_HTTP = 47990; constexpr auto PORT_HTTP = 47990;
namespace fs = std::filesystem; namespace fs = std::filesystem;
namespace pt = boost::property_tree; namespace pt = boost::property_tree;
using https_server_t = SimpleWeb::Server<SimpleWeb::HTTPS>; using https_server_t = SimpleWeb::Server<SimpleWeb::HTTPS>;
using args_t = SimpleWeb::CaseInsensitiveMultimap; using args_t = SimpleWeb::CaseInsensitiveMultimap;
using resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Response>; using resp_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Response>;
using req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Request>; using req_https_t = std::shared_ptr<typename SimpleWeb::ServerBase<SimpleWeb::HTTPS>::Request>;
enum class op_e enum class op_e
{ {
ADD, ADD,
REMOVE REMOVE
}; };
template <class T> template <class T>
void not_found(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) void not_found(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request)
{ {
pt::ptree tree; pt::ptree tree;
tree.put("root.<xmlattr>.status_code", 404); tree.put("root.<xmlattr>.status_code", 404);
@@ -61,55 +61,55 @@ namespace confighttp
*response << "HTTP/1.1 404 NOT FOUND\r\n" *response << "HTTP/1.1 404 NOT FOUND\r\n"
<< data.str(); << data.str();
} }
void getIndexPage(resp_https_t response, req_https_t request) void getIndexPage(resp_https_t response, req_https_t request)
{ {
std::string header = read_file(WEB_DIR "header.html"); std::string header = read_file(WEB_DIR "header.html");
std::string content = read_file(WEB_DIR "index.html"); std::string content = read_file(WEB_DIR "index.html");
response->write(header + content); response->write(header + content);
} }
template <class T> template <class T>
void getPinPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) void getPinPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request)
{ {
std::string header = read_file(WEB_DIR "header.html"); std::string header = read_file(WEB_DIR "header.html");
std::string content = read_file(WEB_DIR "pin.html"); std::string content = read_file(WEB_DIR "pin.html");
response->write(header + content); response->write(header + content);
} }
template <class T> template <class T>
void getAppsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) void getAppsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request)
{ {
std::string header = read_file(WEB_DIR "header.html"); std::string header = read_file(WEB_DIR "header.html");
std::string content = read_file(WEB_DIR "apps.html"); std::string content = read_file(WEB_DIR "apps.html");
response->write(header + content); response->write(header + content);
} }
template <class T> template <class T>
void getClientsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) void getClientsPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request)
{ {
std::string header = read_file(WEB_DIR "header.html"); std::string header = read_file(WEB_DIR "header.html");
std::string content = read_file(WEB_DIR "clients.html"); std::string content = read_file(WEB_DIR "clients.html");
response->write(header + content); response->write(header + content);
} }
template <class T> template <class T>
void getConfigPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) void getConfigPage(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request)
{ {
std::string header = read_file(WEB_DIR "header.html"); std::string header = read_file(WEB_DIR "header.html");
std::string content = read_file(WEB_DIR "config.html"); std::string content = read_file(WEB_DIR "config.html");
response->write(header + content); response->write(header + content);
} }
void getApps(resp_https_t response, req_https_t request) void getApps(resp_https_t response, req_https_t request)
{ {
std::string content = read_file(SUNSHINE_ASSETS_DIR "/" APPS_JSON); std::string content = read_file(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
response->write(content); response->write(content);
} }
void saveApp(resp_https_t response, req_https_t request) void saveApp(resp_https_t response, req_https_t request)
{ {
std::stringstream ss; std::stringstream ss;
ss << request->content.rdbuf(); ss << request->content.rdbuf();
pt::ptree outputTree; pt::ptree outputTree;
@@ -119,46 +119,57 @@ namespace confighttp
pt::write_json(data, outputTree); pt::write_json(data, outputTree);
response->write(data.str()); response->write(data.str());
}); });
pt::ptree inputTree,fileTree; pt::ptree inputTree, fileTree;
try { try
{
//TODO: Input Validation //TODO: Input Validation
pt::read_json(ss, inputTree); pt::read_json(ss, inputTree);
pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
auto &apps_node = fileTree.get_child("apps"s); auto &apps_node = fileTree.get_child("apps"s);
int index = inputTree.get<int>("index"); int index = inputTree.get<int>("index");
BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty(); BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty();
if(inputTree.get_child("prep-cmd").empty())inputTree.erase("prep-cmd"); if (inputTree.get_child("prep-cmd").empty())
inputTree.erase("prep-cmd");
inputTree.erase("index"); inputTree.erase("index");
if(index == -1){ if (index == -1)
apps_node.push_back(std::make_pair("",inputTree)); {
} else { apps_node.push_back(std::make_pair("", inputTree));
}
else
{
//Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick //Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick
pt::ptree newApps; pt::ptree newApps;
int i = 0; int i = 0;
for (const auto& kv : apps_node) { for (const auto &kv : apps_node)
if(i == index){ {
newApps.push_back(std::make_pair("",inputTree)); if (i == index)
} else { {
newApps.push_back(std::make_pair("",kv.second)); newApps.push_back(std::make_pair("", inputTree));
}
else
{
newApps.push_back(std::make_pair("", kv.second));
} }
i++; i++;
} }
fileTree.erase("apps"); fileTree.erase("apps");
fileTree.push_back(std::make_pair("apps",newApps)); fileTree.push_back(std::make_pair("apps", newApps));
} }
pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
outputTree.put("status","true"); outputTree.put("status", "true");
proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON); proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
} catch (std::exception &e) { }
catch (std::exception &e)
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status","false"); outputTree.put("status", "false");
outputTree.put("error","Invalid Input JSON"); outputTree.put("error", "Invalid Input JSON");
return; return;
} }
} }
void deleteApp(resp_https_t response, req_https_t request) void deleteApp(resp_https_t response, req_https_t request)
{ {
pt::ptree outputTree; pt::ptree outputTree;
auto g = util::fail_guard([&]() { auto g = util::fail_guard([&]() {
std::ostringstream data; std::ostringstream data;
@@ -167,45 +178,53 @@ namespace confighttp
response->write(data.str()); response->write(data.str());
}); });
pt::ptree fileTree; pt::ptree fileTree;
try { try
{
pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
auto &apps_node = fileTree.get_child("apps"s); auto &apps_node = fileTree.get_child("apps"s);
int index = stoi(request->path_match[1]); int index = stoi(request->path_match[1]);
BOOST_LOG(info) << index; BOOST_LOG(info) << index;
if(index <= 0){ if (index <= 0)
outputTree.put("status","false"); {
outputTree.put("error","Invalid Index"); outputTree.put("status", "false");
outputTree.put("error", "Invalid Index");
return; return;
} else { }
else
{
//Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick //Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick
pt::ptree newApps; pt::ptree newApps;
int i = 0; int i = 0;
for (const auto& kv : apps_node) { for (const auto &kv : apps_node)
if(i != index){ {
newApps.push_back(std::make_pair("",kv.second)); if (i != index)
{
newApps.push_back(std::make_pair("", kv.second));
} }
i++; i++;
} }
fileTree.erase("apps"); fileTree.erase("apps");
fileTree.push_back(std::make_pair("apps",newApps)); fileTree.push_back(std::make_pair("apps", newApps));
} }
pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
outputTree.put("status","true"); outputTree.put("status", "true");
proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON); proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
} catch (std::exception &e) { }
catch (std::exception &e)
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status","false"); outputTree.put("status", "false");
outputTree.put("error","Invalid File JSON"); outputTree.put("error", "Invalid File JSON");
return; return;
} }
} }
void start(std::shared_ptr<safe::signal_t> shutdown_event) void start(std::shared_ptr<safe::signal_t> shutdown_event)
{ {
auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls); auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls);
ctx->use_certificate_chain_file(config::nvhttp.cert); ctx->use_certificate_chain_file(config::nvhttp.cert);
ctx->use_private_key_file(config::nvhttp.pkey, boost::asio::ssl::context::pem); ctx->use_private_key_file(config::nvhttp.pkey, boost::asio::ssl::context::pem);
https_server_t http_server { ctx, 0 }; https_server_t http_server{ctx, 0};
http_server.default_resource = not_found<SimpleWeb::HTTPS>; http_server.default_resource = not_found<SimpleWeb::HTTPS>;
http_server.resource["^/$"]["GET"] = getIndexPage; http_server.resource["^/$"]["GET"] = getIndexPage;
http_server.resource["^/pin$"]["GET"] = getPinPage<SimpleWeb::HTTPS>; http_server.resource["^/pin$"]["GET"] = getPinPage<SimpleWeb::HTTPS>;
@@ -232,8 +251,25 @@ namespace confighttp
shutdown_event->raise(true); shutdown_event->raise(true);
return; return;
} }
auto accept_and_run = [&](auto *http_server) {
try
{
http_server->accept_and_run();
}
catch (boost::system::system_error &err)
{
// It's possible the exception gets thrown after calling http_server->stop() from a different thread
if (shutdown_event->peek())
{
return;
}
std::thread tcp{&https_server_t::accept_and_run, &http_server}; BOOST_LOG(fatal) << "Couldn't start Configuration HTTP server to ports ["sv << PORT_HTTPS << ", "sv << PORT_HTTP << "]: "sv << err.what();
shutdown_event->raise(true);
return;
}
};
std::thread tcp{accept_and_run, &http_server};
// Wait for any event // Wait for any event
shutdown_event->view(); shutdown_event->view();
@@ -241,7 +277,7 @@ namespace confighttp
http_server.stop(); http_server.stop();
tcp.join(); tcp.join();
} }
} }
std::string read_file(std::string path) std::string read_file(std::string path)