diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index 655b27e24..87bf2cbac 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -4,6 +4,7 @@ #include "lib/api/core/ApiClientInterop.h" #include "lib/api/apigateway.pb.h" #include "lib/logging/logging.h" +#include "lib/os/os.h" #include #include @@ -20,6 +21,7 @@ #include typedef websocketpp::server WsServer; +typedef WsServer::endpoint::connection_ptr connection_ptr; using websocketpp::connection_hdl; using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; @@ -44,7 +46,7 @@ namespace http { std::map> _session; WsServer _server; int _port; - std::string _root; + std::string _www; const api::ServiceContext& _ctx; ServerState(const api::ServiceContext& ctx, int port, const char* web_root) : @@ -53,11 +55,11 @@ namespace http { _session(), _server(), _port(port), - _root(web_root), + _www(web_root), _ctx(ctx) { - if (_root.empty()) { - _root = std::string(getenv("HOME")) + "/www"; + if (_www.empty()) { + _www = std::string(getenv("HOME")) + "/www"; } _server.init_asio(_dispatcher.io_service()); _server.set_open_handler(bind(&ServerState::on_open, this, ::_1)); @@ -108,73 +110,116 @@ namespace http { } } + void return_dotall(connection_ptr& con) { + std::vector files; + auto iter = boost::filesystem::directory_iterator(_www + "/config/"); + auto end = boost::filesystem::directory_iterator(); + // Collect all .conf file + for (; iter != end; ++iter) { + auto ext = iter->path().filename().string().substr(iter->path().filename().stem().size()); + if(boost::iequals(ext, ".conf")) { + files.push_back(iter->path()); + } + } + // Sort them so we at least it's stable + std::sort(files.begin(), files.end()); + // Concatanate + std::string body; + for (const auto& p : files) { + body += "##########################\n"; + body += "## " + p.filename().string(); + body += "\n##########################\n"; + body += read_file(p.string()); + } + + con->set_body(body); + con->set_status(websocketpp::http::status_code::ok); + } + + void directory_index(connection_ptr& con, const std::string& resource, const boost::filesystem::path& path) { + // directory listing + std::string body = "\n"; + body += ""; + body += "\n"; + body += "\n"; + body += "\n"; + body += "

Index of " + resource + "

\n"; + auto iter = boost::filesystem::directory_iterator(path); + auto end = boost::filesystem::directory_iterator(); + body += "\n"; + body += "\n"; + con->set_status(websocketpp::http::status_code::ok); + con->set_body(body); + } + + boost::filesystem::path web_ui_path(const boost::filesystem::path& root) { + auto reldir = root / "web/ui"; + if (boost::filesystem::exists(reldir)) return reldir; + else if (root.has_parent_path()) return web_ui_path(root.parent_path()); + return _www; + } + + boost::filesystem::path map_path_to_filesystem(const std::string& resource) { + std::string config = "/config"; + std::string files = "/files"; + + if (resource.compare(0, config.size(), config) == 0) { + LOG(Api,Info,"config"); + return _www + resource; + } + else if (resource.compare(0, files.size(), files) == 0) { + LOG(Api,Info,"files"); + return _www + resource.substr(files.size()); + } + return web_ui_path(os::get_install_path()) / resource; + } + + void return_file(connection_ptr& con, const boost::filesystem::path& path) { + con->set_body(read_file(path.string())); + con->set_status(websocketpp::http::status_code::ok); + } + void on_http(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); auto resource = con->get_resource(); if (boost::iequals(resource, "/config/.all")) { - std::vector files; - auto iter = boost::filesystem::directory_iterator(_root + "/config/"); - auto end = boost::filesystem::directory_iterator(); - // Collect all .conf file - for (; iter != end; ++iter) { - auto ext = iter->path().filename().string().substr(iter->path().filename().stem().size()); - if(boost::iequals(ext, ".conf")) { - files.push_back(iter->path()); - } - } - // Sort them so we at least it's stable - std::sort(files.begin(), files.end()); - // Concatanate - std::string body; - for (const auto& p : files) { - body += "##########################\n"; - body += "## " + p.filename().string(); - body += "\n##########################\n"; - body += read_file(p.string()); - } - - con->set_body(body); - con->set_status(websocketpp::http::status_code::ok); + return_dotall(con); + } + if (boost::iequals(resource, "/bts_installer.exe") && boost::filesystem::is_regular_file(_www+"/bts_installer.exe")) { + return_file(con, _www + "/bts_installer.exe"); } else { - boost::filesystem::path path = _root + resource; + auto path = map_path_to_filesystem(resource); if (boost::filesystem::is_directory(path)) { - // directory listing - std::string body = "\n"; - body += ""; - body += "\n"; - body += "\n"; - body += "\n"; - body += "

Index of " + resource + "

\n"; - auto iter = boost::filesystem::directory_iterator(path); - auto end = boost::filesystem::directory_iterator(); - body += "
    \n"; - for (; iter != end; ++iter) { - auto x = iter->path(); - body += "
  • "; - if (boost::filesystem::is_regular_file(x)) { - body += " "; - } - else if (boost::filesystem::is_directory(x)) { - body += " "; - } - if (resource == "/") { - body += ""; - } - else { - body += ""; - } - body += x.filename().string(); - body += "
  • \n"; + auto index = path / "index.html"; + if (boost::filesystem::is_regular_file(index)) { + return_file(con, index); + } + else { + directory_index(con, resource, path); } - body += "
\n"; - body += "\n"; - con->set_status(websocketpp::http::status_code::ok); - con->set_body(body); } else if (boost::filesystem::is_regular_file(path)) { - con->set_body(read_file(path.string())); - con->set_status(websocketpp::http::status_code::ok); + return_file(con, path); } else { con->set_status(websocketpp::http::status_code::not_found);