diff --git a/examples/http.cpp b/examples/http.cpp index 38b3ee5..c724db1 100644 --- a/examples/http.cpp +++ b/examples/http.cpp @@ -9,9 +9,8 @@ struct SomeComponent : public cserver::server::handlers::HTTPHandlerBaseWithAdde inline constexpr SomeComponent(auto name, auto& context) : HTTPHandlerBaseWithAdder(name, context) {}; - inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request, - cserver::server::http::HTTPResponse&) -> cserver::Task { - co_return request.url.data(); + inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request) -> cserver::Task { + co_return cserver::server::http::HTTPResponse{.body = request.url.data()}; }; }; diff --git a/include/cserver/engine/basic/task_processor.hpp b/include/cserver/engine/basic/task_processor.hpp index 4d69dad..406fb53 100644 --- a/include/cserver/engine/basic/task_processor.hpp +++ b/include/cserver/engine/basic/task_processor.hpp @@ -28,7 +28,7 @@ struct TaskProcessor { if(this->ioContext.stopped()) { return; }; - thread = std::jthread([&]{ + thread = std::thread([&]{ if(this->ioContext.stopped()) { return; }; diff --git a/include/cserver/server/handlers/http_handler_base.hpp b/include/cserver/server/handlers/http_handler_base.hpp index 3dd9b85..f4652a4 100644 --- a/include/cserver/server/handlers/http_handler_base.hpp +++ b/include/cserver/server/handlers/http_handler_base.hpp @@ -15,19 +15,17 @@ struct HTTPHandlerBase { }); }; template - inline auto HandleRequest(this Self&& self, cserver::server::http::HTTPRequest&& request, - cserver::server::http::HTTPResponse& response) -> Task requires requires{self.HandleRequestThrow(std::move(request), response);} { + inline auto HandleRequest(this Self&& self, http::HTTPRequest&& request + ) -> Task requires requires{self.HandleRequestThrow(std::move(request));} { using T = std::remove_cvref_t; try { - co_return co_await std::forward(self).HandleRequestThrow(std::move(request), response); + co_return co_await std::forward(self).HandleRequestThrow(std::move(request)); } catch(const std::exception& err) { fmt::println("Error in handler with default name {}: {}", T::kName, err.what()); } catch(...) { fmt::println("Error in handler with default name {}: Unknown Error", T::kName); }; - response.statusCode = 500; - response.statusMessage = "Internal Server Error"; - co_return "Internal Server Error"; + co_return http::HTTPResponse{.statusCode = 500, .statusMessage = "Internal Server Error", .body = "Internal Server Error"}; }; template inline auto HandleRequestStream(this Self&& self, cserver::server::http::HTTPRequest&& request, diff --git a/include/cserver/server/server/server.hpp b/include/cserver/server/server/server.hpp index c5d2a20..ada0d7c 100644 --- a/include/cserver/server/server/server.hpp +++ b/include/cserver/server/server/server.hpp @@ -55,8 +55,7 @@ struct Server : StopBlocker { co_await Get(this->handlers).HandleRequestStream(std::move(request), stream); co_return; } else { - http::HTTPResponse response{}; - response.body = co_await Get(this->handlers).HandleRequest(std::move(request), response); + http::HTTPResponse response = co_await Get(this->handlers).HandleRequest(std::move(request)); response.headers["Content-Length"] = std::to_string(response.body.size()); response.headers["Server"] = "cserver/1"; auto data = response.ToString();