From 2677df4bdfd8aba65c2e34466c69eefc248a1fd1 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Thu, 11 Apr 2024 12:51:47 +0000 Subject: [PATCH] Change interface HTTPHandlerBase --- examples/http.cpp | 5 ++--- include/cserver/engine/basic/task_processor.hpp | 2 +- include/cserver/server/handlers/http_handler_base.hpp | 10 ++++------ include/cserver/server/server/server.hpp | 3 +-- 4 files changed, 8 insertions(+), 12 deletions(-) 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();