fix build: Copy a handful of files over from spark_crypto.dev
This fixes the last of the boost-related errors
This commit is contained in:
1 parent
c9b5ff844f
commit
a82932652b
26 files changed
+2445
-2392
No files matched your search
@@ -1,13 +1,15 @@
|
||||
// #define BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT 1
|
||||
|
||||
#include "WebServer.h"
|
||||
#include "ApiGateway.h"
|
||||
#include "MultipartFormDecoder.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
#include "lib/api/core/ApiClientInterop.h"
|
||||
#include "lib/api/apigateway.pb.h"
|
||||
#include "lib/api/core/ApiClientInterop.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/tracing/PerfLog.h"
|
||||
#include "lib/utils/Dispatcher.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
@@ -16,26 +18,26 @@
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
#include <mutex>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> WsServer;
|
||||
typedef WsServer::endpoint::connection_ptr connection_ptr;
|
||||
using websocketpp::connection_hdl;
|
||||
using websocketpp::connection_hdl;
|
||||
using websocketpp::lib::bind;
|
||||
using websocketpp::lib::placeholders::_1;
|
||||
using websocketpp::lib::placeholders::_2;
|
||||
using websocketpp::lib::bind;
|
||||
|
||||
using namespace bts::api::gateway;
|
||||
|
||||
std::string read_file(const std::string& path) {
|
||||
std::ifstream f{ path };
|
||||
std::string read_file(const std::string &path) {
|
||||
std::ifstream f{path};
|
||||
auto sz = boost::filesystem::file_size(path);
|
||||
std::string contents(sz, ' ');
|
||||
f.read(&contents[0], sz);
|
||||
@@ -45,364 +47,377 @@ std::string read_file(const std::string& path) {
|
||||
namespace bts {
|
||||
namespace http {
|
||||
|
||||
struct ServerState {
|
||||
std::mutex _m;
|
||||
utils::Dispatcher& _dispatcher;
|
||||
std::map<connection_hdl, ApiGatewaySession, std::owner_less<connection_hdl>> _session;
|
||||
WsServer _server;
|
||||
int _port;
|
||||
std::string _www;
|
||||
std::string _conf;
|
||||
const api::ServiceContext& _ctx;
|
||||
metrics::MetricsContainer& _metrics;
|
||||
std::shared_ptr<metrics::HistogramMetric<4>> _mtimer;
|
||||
trace::PerfLog &_perf_log;
|
||||
|
||||
ServerState(const api::ServiceContext& ctx, metrics::MetricsContainer& metrics, trace::PerfLog &l,int port) :
|
||||
_m(),
|
||||
_dispatcher(utils::Dispatcher::get_named("Webserver")),
|
||||
_session(),
|
||||
_server(),
|
||||
_port(port),
|
||||
_www(),
|
||||
_ctx(ctx),
|
||||
struct ServerState {
|
||||
std::mutex _m;
|
||||
utils::Dispatcher &_dispatcher;
|
||||
std::map<connection_hdl, ApiGatewaySession, std::owner_less<connection_hdl>>
|
||||
_session;
|
||||
WsServer _server;
|
||||
int _port;
|
||||
std::string _www;
|
||||
std::string _conf;
|
||||
const api::ServiceContext &_ctx;
|
||||
metrics::MetricsContainer &_metrics;
|
||||
std::shared_ptr<metrics::HistogramMetric<4>> _mtimer;
|
||||
trace::PerfLog &_perf_log;
|
||||
|
||||
ServerState(
|
||||
const api::ServiceContext &ctx,
|
||||
metrics::MetricsContainer &metrics,
|
||||
trace::PerfLog &l,
|
||||
int port)
|
||||
: _m(), _dispatcher(utils::Dispatcher::get_named("Webserver")),
|
||||
_session(), _server(), _port(port), _www(), _ctx(ctx),
|
||||
_metrics(metrics),
|
||||
_mtimer(std::make_shared<metrics::HistogramMetric<4>>("spark_metrics_query_duration_ms",
|
||||
std::array<double,4>({ 0.5, 1.0, 5.0, 10.0 }))),
|
||||
_perf_log(l)
|
||||
{
|
||||
_www = os::sparkdata_path("www");
|
||||
_conf = os::sparkdata_path("conf");
|
||||
_mtimer(std::make_shared<metrics::HistogramMetric<4>>(
|
||||
"spark_metrics_query_duration_ms",
|
||||
std::array<double, 4>({0.5, 1.0, 5.0, 10.0}))),
|
||||
_perf_log(l) {
|
||||
_www = os::sparkdata_path("www");
|
||||
_conf = os::sparkdata_path("conf");
|
||||
|
||||
_metrics.publish(_mtimer);
|
||||
_metrics.publish(_mtimer);
|
||||
|
||||
_server.init_asio(_dispatcher.io_service());
|
||||
_server.set_open_handler(bind(&ServerState::on_open, this, ::_1));
|
||||
_server.set_close_handler(bind(&ServerState::on_close, this, ::_1));
|
||||
_server.set_message_handler(bind(&ServerState::on_message, this, ::_1, ::_2));
|
||||
_server.set_http_handler(bind(&ServerState::on_http, this, ::_1));
|
||||
// Turn off logging
|
||||
_server.clear_access_channels(websocketpp::log::alevel::all);
|
||||
_server.clear_error_channels(websocketpp::log::alevel::all);
|
||||
_server.set_open_handshake_timeout(0);
|
||||
run();
|
||||
}
|
||||
_server.init_asio(_dispatcher.io_service());
|
||||
_server.set_open_handler(bind(&ServerState::on_open, this, ::_1));
|
||||
_server.set_close_handler(bind(&ServerState::on_close, this, ::_1));
|
||||
_server.set_message_handler(
|
||||
bind(&ServerState::on_message, this, ::_1, ::_2));
|
||||
_server.set_http_handler(bind(&ServerState::on_http, this, ::_1));
|
||||
// Turn off logging
|
||||
_server.clear_access_channels(websocketpp::log::alevel::all);
|
||||
_server.clear_error_channels(websocketpp::log::alevel::all);
|
||||
_server.set_open_handshake_timeout(0);
|
||||
run();
|
||||
}
|
||||
|
||||
// starts listening
|
||||
void run() {
|
||||
_server.set_reuse_addr(true);
|
||||
_server.listen(_port);
|
||||
_server.start_accept();
|
||||
}
|
||||
// starts listening
|
||||
void run() {
|
||||
_server.set_reuse_addr(true);
|
||||
_server.listen(_port);
|
||||
_server.start_accept();
|
||||
}
|
||||
|
||||
void on_open(connection_hdl hdl) {
|
||||
auto con = _server.get_con_from_hdl(hdl);
|
||||
if (con) {
|
||||
LOG(Http,Info, "Web client connection from {}", con->get_remote_endpoint());
|
||||
std::lock_guard<std::mutex> lock(_m);
|
||||
_session[hdl] = ApiGatewaySession(_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
void on_close(connection_hdl hdl) {
|
||||
auto con = _server.get_con_from_hdl(hdl);
|
||||
if (con) {
|
||||
LOG(Http,Info, "Web client disconnected: {}", con->get_remote_endpoint());
|
||||
std::lock_guard<std::mutex> lock(_m);
|
||||
_session.erase(hdl);
|
||||
}
|
||||
}
|
||||
|
||||
void on_message(connection_hdl hdl, WsServer::message_ptr msg) {
|
||||
void on_open(connection_hdl hdl) {
|
||||
auto con = _server.get_con_from_hdl(hdl);
|
||||
if (con) {
|
||||
LOG(Http, Info, "Web client connection from {}",
|
||||
con->get_remote_endpoint());
|
||||
std::lock_guard<std::mutex> lock(_m);
|
||||
auto iter = _session.find(hdl);
|
||||
if (iter != _session.end()) {
|
||||
GatewayRequest request;
|
||||
if(request.ParseFromString(msg->get_payload())) {
|
||||
iter->second.handle(request, [this,hdl](const GatewayResponse& response) {
|
||||
websocketpp::lib::error_code ec;
|
||||
_server.send(hdl, response.SerializeAsString(), websocketpp::frame::opcode::binary, ec);
|
||||
if (ec) {
|
||||
LOG(Http,Warn, "Error sending from websocket: '{}'", ec.message());
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
LOG(Http,Info, "Could not parse request as GatewayRequest: {}", msg->get_payload());
|
||||
_session[hdl] = ApiGatewaySession(_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
void on_close(connection_hdl hdl) {
|
||||
auto con = _server.get_con_from_hdl(hdl);
|
||||
if (con) {
|
||||
LOG(Http, Info, "Web client disconnected: {}",
|
||||
con->get_remote_endpoint());
|
||||
std::lock_guard<std::mutex> lock(_m);
|
||||
_session.erase(hdl);
|
||||
}
|
||||
}
|
||||
|
||||
void on_message(connection_hdl hdl, WsServer::message_ptr msg) {
|
||||
std::lock_guard<std::mutex> lock(_m);
|
||||
auto iter = _session.find(hdl);
|
||||
if (iter != _session.end()) {
|
||||
GatewayRequest request;
|
||||
if (request.ParseFromString(msg->get_payload())) {
|
||||
iter->second.handle(
|
||||
request, [this, hdl](const GatewayResponse &response) {
|
||||
websocketpp::lib::error_code ec;
|
||||
_server.send(
|
||||
hdl, response.SerializeAsString(),
|
||||
websocketpp::frame::opcode::binary, ec);
|
||||
if (ec) {
|
||||
LOG(Http, Warn, "Error sending from websocket: '{}'",
|
||||
ec.message());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
LOG(Http, Info, "Could not parse request as GatewayRequest: {}",
|
||||
msg->get_payload());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void return_dotall(connection_ptr &con) {
|
||||
std::vector<boost::filesystem::path> files;
|
||||
auto iter = boost::filesystem::directory_iterator(_conf);
|
||||
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 return_metrics(connection_ptr &con) {
|
||||
utils::TimeStamp start = utils::TimeStamp::now();
|
||||
std::stringstream ss;
|
||||
_metrics.print(ss);
|
||||
con->set_body(ss.str());
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
_mtimer->observe(
|
||||
(utils::TimeStamp::now() - start).microseconds() / 1000.0);
|
||||
}
|
||||
|
||||
void return_perf_trace_data(connection_ptr &con, size_t max_rows) {
|
||||
utils::TimeStamp start = utils::TimeStamp::now();
|
||||
std::stringstream ss;
|
||||
_perf_log.print_latest_traces(ss, max_rows);
|
||||
con->set_body(ss.str());
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
_mtimer->observe(
|
||||
(utils::TimeStamp::now() - start).microseconds() / 1000.0);
|
||||
}
|
||||
|
||||
void bad_request(connection_ptr &con, const std::string &message) {
|
||||
con->set_body(message);
|
||||
con->set_status(websocketpp::http::status_code::bad_request);
|
||||
}
|
||||
|
||||
std::vector<std::string>
|
||||
split_string(std::string body, std::string &deliniator) {
|
||||
std::vector<std::string> result;
|
||||
size_t i = body.find(deliniator);
|
||||
while (i != std::string::npos) {
|
||||
result.push_back(body.substr(0, i));
|
||||
body = body.substr(i + deliniator.size());
|
||||
i = body.find(deliniator);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void handle_file_upload(connection_ptr &con) {
|
||||
auto content_type = con->get_request_header("Content-Type");
|
||||
auto data = decode_form_data(content_type, con->get_request_body());
|
||||
if (data) {
|
||||
for (const auto &part : data.value().parts) {
|
||||
if (!part.filename.empty()) {
|
||||
LOG(Http, Info, "Received file upload: {}", part.filename);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LOG(Http, Warn,
|
||||
"Failed to decode form data. Ensure you are using "
|
||||
"'enctype=\"multipart/form-data\"'");
|
||||
}
|
||||
|
||||
con->set_body("got it");
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
}
|
||||
|
||||
bool require_authentication(
|
||||
connection_ptr &con,
|
||||
const std::string &username,
|
||||
const std::string &password) {
|
||||
auto auth = con->get_request_header("Authorization");
|
||||
if (!auth.empty() && auth.size() >= 7 &&
|
||||
boost::iequals(auth.substr(0, 6), "Basic ")) {
|
||||
auto b64 = auth.substr(6);
|
||||
auto decoded = websocketpp::base64_decode(b64);
|
||||
auto colon = decoded.find(':');
|
||||
if (colon != std::string::npos) {
|
||||
auto user = decoded.substr(0, colon);
|
||||
auto pass = decoded.substr(colon + 1);
|
||||
if (user == username && pass == password)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void return_dotall(connection_ptr& con) {
|
||||
std::vector<boost::filesystem::path> files;
|
||||
auto iter = boost::filesystem::directory_iterator(_conf);
|
||||
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());
|
||||
}
|
||||
// Failed
|
||||
con->append_header("WWW-Authenticate", "Basic realm=\"BTS\"");
|
||||
con->set_status(websocketpp::http::status_code::unauthorized);
|
||||
con->set_body("Not Authorized");
|
||||
return false;
|
||||
}
|
||||
|
||||
void check_auth_and_return_info(connection_ptr &con) {
|
||||
if (require_authentication(
|
||||
con, "bts", "bbb835af-b1ea-42d3-8be1-fd9d5555530d")) {
|
||||
auto horrible_json = get_server_info(_ctx);
|
||||
con->set_body(horrible_json);
|
||||
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 = "<!doctype html>\n";
|
||||
body += "<head>";
|
||||
body += "<script defer "
|
||||
"src=\"https://use.fontawesome.com/releases/v5.0.10/js/all.js\" "
|
||||
"integrity=\"sha384-"
|
||||
"slN8GvtUJGnv6ca26v8EzVaR9DC58QEwsIk9q1QXdCU8Yu8ck/tL/"
|
||||
"5szYlBbqmS+\" crossorigin=\"anonymous\"></script>\n";
|
||||
body += "</head>\n";
|
||||
body += "<body>\n";
|
||||
body += "<h1>Index of " + resource + "</h1>\n";
|
||||
auto iter = boost::filesystem::directory_iterator(path);
|
||||
auto end = boost::filesystem::directory_iterator();
|
||||
body += "<ul style=\"list-style-type: none;\">\n";
|
||||
for (; iter != end; ++iter) {
|
||||
auto x = iter->path();
|
||||
body += "<li>";
|
||||
if (boost::filesystem::is_regular_file(x)) {
|
||||
body += "<i class=\"far fa-file\"></i> ";
|
||||
} else if (boost::filesystem::is_directory(x)) {
|
||||
body += "<i class=\"far fa-folder\"></i> ";
|
||||
}
|
||||
if (resource == "/") {
|
||||
body += "<a href=" + x.filename().string() + ">";
|
||||
} else {
|
||||
body += "<a href=" + resource + "/" + x.filename().string() + ">";
|
||||
}
|
||||
body += x.filename().string();
|
||||
body += "</a></li>\n";
|
||||
}
|
||||
body += "</ul>\n";
|
||||
body += "</body></html>\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;
|
||||
reldir = root / "ui";
|
||||
if (boost::filesystem::exists(reldir))
|
||||
return reldir;
|
||||
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";
|
||||
std::string perf_trace = "/perf_trace";
|
||||
std::string docs = "/docs";
|
||||
|
||||
if (resource.compare(0, config.size(), config) == 0) {
|
||||
return _conf + resource.substr(config.size());
|
||||
} else if (resource.compare(0, files.size(), files) == 0) {
|
||||
return _www + resource.substr(files.size());
|
||||
} else if (resource.compare(0, perf_trace.size(), perf_trace) == 0) {
|
||||
return os::sparkd_wd() + "/data_files" + resource;
|
||||
} else if (resource.compare(0, docs.size(), docs) == 0) {
|
||||
return os::sparkd_wd() + "/web" + resource;
|
||||
}
|
||||
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 return_bts_installer(connection_ptr &con) {
|
||||
std::string installer_bits = read_file(_www + "/bts_installer.exe");
|
||||
const std::string installer_config = _www + "install_config.json";
|
||||
if (boost::filesystem::is_regular_file(installer_config)) {
|
||||
auto conf = read_file(installer_config);
|
||||
std::string needle = "$HOST";
|
||||
std::size_t pos;
|
||||
do {
|
||||
pos = conf.find(needle, 0);
|
||||
if (pos != std::string::npos) {
|
||||
auto host = con->get_uri()->get_host();
|
||||
conf.replace(pos, needle.size(), host);
|
||||
}
|
||||
} while (pos != std::string::npos);
|
||||
installer_bits += conf;
|
||||
}
|
||||
con->set_body(installer_bits);
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
}
|
||||
|
||||
std::string strip_query_str(const std::string &request) {
|
||||
auto position = request.find("?");
|
||||
if (position != std::string::npos) {
|
||||
return request.substr(0, position);
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
void on_http(connection_hdl hdl) {
|
||||
auto con = _server.get_con_from_hdl(hdl);
|
||||
auto resource = strip_query_str(con->get_resource());
|
||||
LOG(Http, Diag, "HTTP GET: {}", resource);
|
||||
if (boost::iequals(resource, "/config/.all")) {
|
||||
return_dotall(con);
|
||||
} else if (boost::iequals(resource, "/metrics")) {
|
||||
return_metrics(con);
|
||||
} else if (boost::iequals(resource, "/perf_trace_data")) {
|
||||
return_perf_trace_data(con, 1000);
|
||||
} else if (std::regex_match(
|
||||
resource, std::regex("/perf_trace_data/(\\d++)"))) {
|
||||
std::regex rx = std::regex("/perf_trace_data/(\\d++)");
|
||||
std::smatch matches;
|
||||
if (std::regex_match(resource, matches, rx)) {
|
||||
auto max_rows = std::atoi(matches[1].str().c_str());
|
||||
return_perf_trace_data(con, max_rows);
|
||||
} else
|
||||
return_perf_trace_data(con, 1000);
|
||||
} else if (boost::iequals(resource, "/server_info")) {
|
||||
check_auth_and_return_info(con);
|
||||
} else if (
|
||||
boost::iequals(resource, "/bts_installer.exe") &&
|
||||
boost::filesystem::is_regular_file(_www + "/bts_installer.exe")) {
|
||||
return_bts_installer(con);
|
||||
} else if (boost::iequals(resource, "/robots.txt")) {
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
std::string body = "User-agent: *\nDisallow: /";
|
||||
con->set_body(body);
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
}
|
||||
|
||||
void return_metrics(connection_ptr& con) {
|
||||
utils::TimeStamp start = utils::TimeStamp::now();
|
||||
std::stringstream ss;
|
||||
_metrics.print(ss);
|
||||
con->set_body(ss.str());
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
_mtimer->observe((utils::TimeStamp::now() - start).microseconds() / 1000.0);
|
||||
}
|
||||
|
||||
void return_perf_trace_data(connection_ptr& con,size_t max_rows) {
|
||||
utils::TimeStamp start = utils::TimeStamp::now();
|
||||
std::stringstream ss;
|
||||
_perf_log.print_latest_traces(ss,max_rows);
|
||||
con->set_body(ss.str());
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
_mtimer->observe((utils::TimeStamp::now() - start).microseconds() / 1000.0);
|
||||
}
|
||||
|
||||
void bad_request(connection_ptr& con, const std::string& message) {
|
||||
con->set_body(message);
|
||||
con->set_status(websocketpp::http::status_code::bad_request);
|
||||
}
|
||||
|
||||
std::vector<std::string> split_string(std::string body, std::string& deliniator) {
|
||||
std::vector<std::string> result;
|
||||
size_t i = body.find(deliniator);
|
||||
while(i != std::string::npos) {
|
||||
result.push_back(body.substr(0,i));
|
||||
body = body.substr(i + deliniator.size());
|
||||
i = body.find(deliniator);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void handle_file_upload(connection_ptr& con) {
|
||||
auto content_type = con->get_request_header("Content-Type");
|
||||
auto data = decode_form_data(content_type, con->get_request_body());
|
||||
if (data) {
|
||||
for(const auto& part : data.value().parts) {
|
||||
if (!part.filename.empty()) {
|
||||
LOG(Http,Info,"Received file upload: {}", part.filename);
|
||||
}
|
||||
} else if (boost::iequals(resource, "/upload")) {
|
||||
handle_file_upload(con);
|
||||
} else {
|
||||
auto path = map_path_to_filesystem(resource);
|
||||
if (boost::filesystem::is_directory(path)) {
|
||||
auto index = path / "index.html";
|
||||
if (boost::filesystem::is_regular_file(index)) {
|
||||
return_file(con, index);
|
||||
} else {
|
||||
directory_index(con, resource, path);
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG(Http,Warn, "Failed to decode form data. Ensure you are using 'enctype=\"multipart/form-data\"'");
|
||||
}
|
||||
|
||||
con->set_body("got it");
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
}
|
||||
|
||||
bool require_authentication(connection_ptr& con, const std::string& username, const std::string& password) {
|
||||
auto auth = con->get_request_header("Authorization");
|
||||
if (!auth.empty() && auth.size() >= 7 && boost::iequals(auth.substr(0, 6), "Basic ")) {
|
||||
auto b64 = auth.substr(6);
|
||||
auto decoded = websocketpp::base64_decode(b64);
|
||||
auto colon = decoded.find(':');
|
||||
if (colon != std::string::npos) {
|
||||
auto user = decoded.substr(0, colon);
|
||||
auto pass = decoded.substr(colon+1);
|
||||
if (user == username && pass == password) return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Failed
|
||||
con->append_header("WWW-Authenticate", "Basic realm=\"BTS\"");
|
||||
con->set_status(websocketpp::http::status_code::unauthorized);
|
||||
con->set_body("Not Authorized");
|
||||
return false;
|
||||
}
|
||||
|
||||
void check_auth_and_return_info(connection_ptr& con) {
|
||||
if (require_authentication(con, "bts", "bbb835af-b1ea-42d3-8be1-fd9d5555530d")) {
|
||||
auto horrible_json = get_server_info(_ctx);
|
||||
con->set_body(horrible_json);
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
} else if (boost::filesystem::is_regular_file(path)) {
|
||||
return_file(con, path);
|
||||
} else {
|
||||
con->set_status(websocketpp::http::status_code::not_found);
|
||||
con->set_body("Cannot find " + resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void directory_index(connection_ptr& con, const std::string& resource, const boost::filesystem::path& path) {
|
||||
// directory listing
|
||||
std::string body = "<!doctype html>\n";
|
||||
body += "<head>";
|
||||
body += "<script defer src=\"https://use.fontawesome.com/releases/v5.0.10/js/all.js\" integrity=\"sha384-slN8GvtUJGnv6ca26v8EzVaR9DC58QEwsIk9q1QXdCU8Yu8ck/tL/5szYlBbqmS+\" crossorigin=\"anonymous\"></script>\n";
|
||||
body += "</head>\n";
|
||||
body += "<body>\n";
|
||||
body += "<h1>Index of " + resource + "</h1>\n";
|
||||
auto iter = boost::filesystem::directory_iterator(path);
|
||||
auto end = boost::filesystem::directory_iterator();
|
||||
body += "<ul style=\"list-style-type: none;\">\n";
|
||||
for (; iter != end; ++iter) {
|
||||
auto x = iter->path();
|
||||
body += "<li>";
|
||||
if (boost::filesystem::is_regular_file(x)) {
|
||||
body += "<i class=\"far fa-file\"></i> ";
|
||||
}
|
||||
else if (boost::filesystem::is_directory(x)) {
|
||||
body += "<i class=\"far fa-folder\"></i> ";
|
||||
}
|
||||
if (resource == "/") {
|
||||
body += "<a href=" + x.filename().string() + ">";
|
||||
}
|
||||
else {
|
||||
body += "<a href=" + resource + "/" + x.filename().string() + ">";
|
||||
}
|
||||
body += x.filename().string();
|
||||
body += "</a></li>\n";
|
||||
}
|
||||
body += "</ul>\n";
|
||||
body += "</body></html>\n";
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
con->set_body(body);
|
||||
}
|
||||
SparkWebServer::SparkWebServer(
|
||||
const api::ServiceContext &ctx,
|
||||
metrics::MetricsContainer &metrics,
|
||||
trace::PerfLog &l,
|
||||
int port)
|
||||
: _me(std::make_shared<ServerState>(ctx, metrics, l, port)) {}
|
||||
|
||||
boost::filesystem::path web_ui_path(const boost::filesystem::path& root) {
|
||||
auto reldir = root / "web/ui";
|
||||
if (boost::filesystem::exists(reldir)) return reldir;
|
||||
reldir = root / "ui";
|
||||
if (boost::filesystem::exists(reldir)) return reldir;
|
||||
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";
|
||||
std::string perf_trace = "/perf_trace";
|
||||
std::string docs = "/docs";
|
||||
|
||||
if (resource.compare(0, config.size(), config) == 0) {
|
||||
return _conf + resource.substr(config.size());
|
||||
}
|
||||
else if (resource.compare(0, files.size(), files) == 0) {
|
||||
return _www + resource.substr(files.size());
|
||||
}
|
||||
else if (resource.compare(0, perf_trace.size(), perf_trace) == 0) {
|
||||
return os::sparkd_wd() + "/data_files" + resource;
|
||||
}
|
||||
else if (resource.compare(0, docs.size(), docs) == 0) {
|
||||
return os::sparkd_wd() + "/web" + resource;
|
||||
}
|
||||
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 return_bts_installer(connection_ptr& con) {
|
||||
std::string installer_bits = read_file(_www+"/bts_installer.exe");
|
||||
const std::string installer_config = _www+"install_config.json";
|
||||
if (boost::filesystem::is_regular_file(installer_config)) {
|
||||
auto conf = read_file(installer_config);
|
||||
std::string needle = "$HOST";
|
||||
std::size_t pos;
|
||||
do {
|
||||
pos = conf.find(needle,0);
|
||||
if (pos != std::string::npos) {
|
||||
auto host = con->get_uri()->get_host();
|
||||
conf.replace(pos, needle.size(), host);
|
||||
}
|
||||
} while(pos != std::string::npos);
|
||||
installer_bits += conf;
|
||||
}
|
||||
con->set_body(installer_bits);
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
}
|
||||
|
||||
std::string strip_query_str(const std::string& request) {
|
||||
auto position = request.find("?");
|
||||
if (position != std::string::npos) {
|
||||
return request.substr(0, position);
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
void on_http(connection_hdl hdl) {
|
||||
auto con = _server.get_con_from_hdl(hdl);
|
||||
auto resource = strip_query_str(con->get_resource());
|
||||
LOG(Http,Diag, "HTTP GET: {}", resource);
|
||||
if (boost::iequals(resource, "/config/.all")) {
|
||||
return_dotall(con);
|
||||
}
|
||||
else if (boost::iequals(resource, "/metrics")) {
|
||||
return_metrics(con);
|
||||
}
|
||||
else if (boost::iequals(resource, "/perf_trace_data")) {
|
||||
return_perf_trace_data(con,1000);
|
||||
}
|
||||
else if (std::regex_match(resource, std::regex("/perf_trace_data/(\\d++)"))) {
|
||||
std::regex rx = std::regex("/perf_trace_data/(\\d++)");
|
||||
std::smatch matches;
|
||||
if (std::regex_match(resource, matches, rx)) {
|
||||
auto max_rows = std::atoi(matches[1].str().c_str());
|
||||
return_perf_trace_data(con,max_rows);
|
||||
}
|
||||
else
|
||||
return_perf_trace_data(con,1000);
|
||||
}
|
||||
else if (boost::iequals(resource, "/server_info")) {
|
||||
check_auth_and_return_info(con);
|
||||
}
|
||||
else if (boost::iequals(resource, "/bts_installer.exe") && boost::filesystem::is_regular_file(_www+"/bts_installer.exe")) {
|
||||
return_bts_installer(con);
|
||||
}
|
||||
else if (boost::iequals(resource, "/robots.txt")) {
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
std::string body = "User-agent: *\nDisallow: /";
|
||||
con->set_body(body);
|
||||
}
|
||||
else if (boost::iequals(resource, "/upload")) {
|
||||
handle_file_upload(con);
|
||||
}
|
||||
else {
|
||||
auto path = map_path_to_filesystem(resource);
|
||||
if (boost::filesystem::is_directory(path)) {
|
||||
auto index = path / "index.html";
|
||||
if (boost::filesystem::is_regular_file(index)) {
|
||||
return_file(con, index);
|
||||
}
|
||||
else {
|
||||
directory_index(con, resource, path);
|
||||
}
|
||||
}
|
||||
else if (boost::filesystem::is_regular_file(path)) {
|
||||
return_file(con, path);
|
||||
}
|
||||
else {
|
||||
con->set_status(websocketpp::http::status_code::not_found);
|
||||
con->set_body("Cannot find " + resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
SparkWebServer::SparkWebServer(const api::ServiceContext& ctx, metrics::MetricsContainer& metrics, trace::PerfLog &l,int port) :
|
||||
_me(std::make_shared<ServerState>(ctx, metrics, l,port))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
} // namespace http
|
||||
} // namespace bts
|
||||
Reference in new issue
Block a user