Fix segfault in http

This commit is contained in:
Lion Kortlepel 2021-12-06 10:22:52 +01:00
parent 62cc1e9ce4
commit 279c93179c
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
3 changed files with 15 additions and 10 deletions

View File

@ -26,7 +26,7 @@ const std::string ErrorString = "-1";
namespace Server { namespace Server {
void SetupEnvironment(); void SetupEnvironment();
// todo: Add non TLS Server Instance, this one is TLS only // todo: Add non TLS Server Instance, this one is TLS only
class THttpServerInstance : IThreaded { class THttpServerInstance {
public: public:
THttpServerInstance(); THttpServerInstance();
static fs::path KeyFilePath; static fs::path KeyFilePath;
@ -42,6 +42,7 @@ namespace Server {
* So we need to able to start the server (make it "listen()") in a single Thread. * So we need to able to start the server (make it "listen()") in a single Thread.
*/ */
std::shared_ptr<httplib::SSLServer> mHttpLibServerInstancePtr; std::shared_ptr<httplib::SSLServer> mHttpLibServerInstancePtr;
std::thread mThread;
}; };
// todo: all of these functions are likely unsafe, // todo: all of these functions are likely unsafe,
// todo: replace with something that's managed by a domain specific crypto library // todo: replace with something that's managed by a domain specific crypto library

View File

@ -8,11 +8,11 @@ public:
IThreaded() IThreaded()
// invokes operator() on this object // invokes operator() on this object
: mThread() { } : mThread() { }
~IThreaded() noexcept { ~IThreaded() noexcept {
if (mThread.joinable()) { if (mThread.joinable()) {
mThread.join(); mThread.join();
}
} }
}
virtual void Start() final { virtual void Start() final {
mThread = std::thread([this] { (*this)(); }); mThread = std::thread([this] { (*this)(); });

View File

@ -233,6 +233,7 @@ void Http::Server::Tx509KeypairGenerator::GenerateAndWriteToDisk(const fs::path&
X509_free(x509); X509_free(x509);
return; return;
} }
bool Http::Server::Tx509KeypairGenerator::EnsureTLSConfigExists() { bool Http::Server::Tx509KeypairGenerator::EnsureTLSConfigExists() {
if (fs::is_regular_file(Application::Settings.SSLKeyPath) if (fs::is_regular_file(Application::Settings.SSLKeyPath)
&& fs::is_regular_file(Application::Settings.SSLCertPath)) { && fs::is_regular_file(Application::Settings.SSLCertPath)) {
@ -263,17 +264,20 @@ void Http::Server::SetupEnvironment() {
} }
Http::Server::THttpServerInstance::THttpServerInstance() { Http::Server::THttpServerInstance::THttpServerInstance() {
Start(); mThread = std::thread(&Http::Server::THttpServerInstance::operator(), this);
mThread.detach();
} }
void Http::Server::THttpServerInstance::operator()() { void Http::Server::THttpServerInstance::operator()() {
beammp_info("HTTPS Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
// todo: make this IP agnostic so people can set their own IP // todo: make this IP agnostic so people can set their own IP
this->mHttpLibServerInstancePtr = std::make_shared<httplib::SSLServer>(Application::Settings.SSLCertPath.c_str(), Application::Settings.SSLKeyPath.c_str()); mHttpLibServerInstancePtr = std::make_shared<httplib::SSLServer>(Application::Settings.SSLCertPath.c_str(), Application::Settings.SSLKeyPath.c_str());
this->mHttpLibServerInstancePtr->Get("/", [](const httplib::Request&, httplib::Response& res) { mHttpLibServerInstancePtr->Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("<!DOCTYPE html><article><h1>Hello World!</h1><section><p>BeamMP Server can now serve HTTP requests!</p></section></article></html>", "text/html"); res.set_content("<!DOCTYPE html><article><h1>Hello World!</h1><section><p>BeamMP Server can now serve HTTP requests!</p></section></article></html>", "text/html");
}); });
this->mHttpLibServerInstancePtr->Get("/health", [](const httplib::Request& req, httplib::Response& res) { mHttpLibServerInstancePtr->Get("/health", [](const httplib::Request&, httplib::Response& res) {
res.set_content("0", "text/plain"); res.set_content("0", "text/plain");
res.status = 200; res.status = 200;
}); });
this->mHttpLibServerInstancePtr->listen("0.0.0.0", Application::Settings.HTTPServerPort); mHttpLibServerInstancePtr->listen("0.0.0.0", Application::Settings.HTTPServerPort);
} }