From 279c93179c2a70dbfa6380a3128c8fe8fa086a58 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 6 Dec 2021 10:22:52 +0100 Subject: [PATCH] Fix segfault in http --- include/Http.h | 3 ++- include/IThreaded.h | 8 ++++---- src/Http.cpp | 14 +++++++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/include/Http.h b/include/Http.h index e5c1495..92d1279 100644 --- a/include/Http.h +++ b/include/Http.h @@ -26,7 +26,7 @@ const std::string ErrorString = "-1"; namespace Server { void SetupEnvironment(); // todo: Add non TLS Server Instance, this one is TLS only - class THttpServerInstance : IThreaded { + class THttpServerInstance { public: THttpServerInstance(); 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. */ std::shared_ptr mHttpLibServerInstancePtr; + std::thread mThread; }; // todo: all of these functions are likely unsafe, // todo: replace with something that's managed by a domain specific crypto library diff --git a/include/IThreaded.h b/include/IThreaded.h index d08e428..2158b46 100644 --- a/include/IThreaded.h +++ b/include/IThreaded.h @@ -8,11 +8,11 @@ public: IThreaded() // invokes operator() on this object : mThread() { } - ~IThreaded() noexcept { - if (mThread.joinable()) { - mThread.join(); - } + ~IThreaded() noexcept { + if (mThread.joinable()) { + mThread.join(); } + } virtual void Start() final { mThread = std::thread([this] { (*this)(); }); diff --git a/src/Http.cpp b/src/Http.cpp index 598ae7b..b59a8b3 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -233,6 +233,7 @@ void Http::Server::Tx509KeypairGenerator::GenerateAndWriteToDisk(const fs::path& X509_free(x509); return; } + bool Http::Server::Tx509KeypairGenerator::EnsureTLSConfigExists() { if (fs::is_regular_file(Application::Settings.SSLKeyPath) && fs::is_regular_file(Application::Settings.SSLCertPath)) { @@ -263,17 +264,20 @@ void Http::Server::SetupEnvironment() { } Http::Server::THttpServerInstance::THttpServerInstance() { - Start(); + mThread = std::thread(&Http::Server::THttpServerInstance::operator(), this); + mThread.detach(); } + 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 - this->mHttpLibServerInstancePtr = std::make_shared(Application::Settings.SSLCertPath.c_str(), Application::Settings.SSLKeyPath.c_str()); - this->mHttpLibServerInstancePtr->Get("/", [](const httplib::Request&, httplib::Response& res) { + mHttpLibServerInstancePtr = std::make_shared(Application::Settings.SSLCertPath.c_str(), Application::Settings.SSLKeyPath.c_str()); + mHttpLibServerInstancePtr->Get("/", [](const httplib::Request&, httplib::Response& res) { res.set_content("

Hello World!

BeamMP Server can now serve HTTP requests!

", "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.status = 200; }); - this->mHttpLibServerInstancePtr->listen("0.0.0.0", Application::Settings.HTTPServerPort); + mHttpLibServerInstancePtr->listen("0.0.0.0", Application::Settings.HTTPServerPort); }