From 61a7e85e522ed498c1dff0bdfc8dd45e489e841c Mon Sep 17 00:00:00 2001 From: sha512sum Date: Thu, 27 Jun 2024 01:12:31 +0000 Subject: [PATCH] HTTP -> Http --- examples/http.cpp | 8 ++--- include/cserver/clients/http/component.hpp | 6 ++-- include/cserver/clients/http/request.hpp | 2 +- include/cserver/clients/http/response.hpp | 6 ++-- .../server/handlers/http_handler_base.hpp | 28 +++++++-------- include/cserver/server/http/http_request.hpp | 4 +-- .../server/http/http_request_parser.hpp | 36 +++++++++---------- include/cserver/server/http/http_response.hpp | 4 +-- include/cserver/server/http/http_stream.hpp | 4 +-- include/cserver/server/server/server.hpp | 12 +++---- 10 files changed, 55 insertions(+), 55 deletions(-) diff --git a/examples/http.cpp b/examples/http.cpp index c724db1..6413975 100644 --- a/examples/http.cpp +++ b/examples/http.cpp @@ -2,15 +2,15 @@ #include #include -struct SomeComponent : public cserver::server::handlers::HTTPHandlerBaseWithAdder { +struct SomeComponent : public cserver::server::handlers::HttpHandlerBaseWithAdder { static constexpr utempl::ConstexprString kPath = "/v1/some/"; static constexpr utempl::ConstexprString kName = "name"; static constexpr utempl::ConstexprString kHandlerManagerName = "server"; inline constexpr SomeComponent(auto name, auto& context) : - HTTPHandlerBaseWithAdder(name, context) {}; + HttpHandlerBaseWithAdder(name, context) {}; - inline auto HandleRequestThrow(const cserver::server::http::HTTPRequest& request) -> cserver::Task { - co_return cserver::server::http::HTTPResponse{.body = 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/clients/http/component.hpp b/include/cserver/clients/http/component.hpp index 32b7195..30858f7 100644 --- a/include/cserver/clients/http/component.hpp +++ b/include/cserver/clients/http/component.hpp @@ -52,16 +52,16 @@ private: std::unreachable(); }(); } else { - return [] -> server::http::HTTPResponse { + return [] -> server::http::HttpResponse { std::unreachable(); }(); }; }; template - inline auto ReadHeaders(Socket&& socket, auto&&...) const -> cserver::Task { + inline auto ReadHeaders(Socket&& socket, auto&&...) const -> cserver::Task { std::string serverResponse; co_await boost::asio::async_read_until(socket, boost::asio::dynamic_buffer(serverResponse), "\r\n\r\n", boost::asio::use_awaitable); - server::http::HTTPResponse response; + server::http::HttpResponse response; std::istringstream responseStream(std::move(serverResponse)); std::string httpVersion; responseStream >> httpVersion >> response.statusCode; diff --git a/include/cserver/clients/http/request.hpp b/include/cserver/clients/http/request.hpp index 13a08b8..51319d1 100644 --- a/include/cserver/clients/http/request.hpp +++ b/include/cserver/clients/http/request.hpp @@ -7,7 +7,7 @@ namespace cserver::clients::http { template struct Request { HttpClient& client; - server::http::HTTPRequest request; + server::http::HttpRequest request; Request(HttpClient& client) : client(client) { this->AddHeader("User-Agent", "cserver/1"); diff --git a/include/cserver/clients/http/response.hpp b/include/cserver/clients/http/response.hpp index fecefec..3bbc07a 100644 --- a/include/cserver/clients/http/response.hpp +++ b/include/cserver/clients/http/response.hpp @@ -6,7 +6,7 @@ namespace cserver::clients::http { template -class Response : public server::http::HTTPResponse { +class Response : public server::http::HttpResponse { HttpClient& client; Socket socket; @@ -25,10 +25,10 @@ public: }; inline constexpr Response(Response&&) = default; inline constexpr Response(const Response&) = default; - inline constexpr Response(HttpClient& client, Socket socket, server::http::HTTPResponse response) : + inline constexpr Response(HttpClient& client, Socket socket, server::http::HttpResponse response) : client(client), socket(std::move(socket)), - HTTPResponse(std::move(response)) {}; + HttpResponse(std::move(response)) {}; }; } // namespace cserver::clients::http diff --git a/include/cserver/server/handlers/http_handler_base.hpp b/include/cserver/server/handlers/http_handler_base.hpp index f4652a4..e849571 100644 --- a/include/cserver/server/handlers/http_handler_base.hpp +++ b/include/cserver/server/handlers/http_handler_base.hpp @@ -7,16 +7,16 @@ namespace cserver::server::handlers { -struct HTTPHandlerBase { +struct HttpHandlerBase { template - static consteval auto Adder(const auto& context) { + static consteval auto HttpHandlerAdder(const auto& context) { return context.TransformComponents([](const ComponentConfig) { return ComponentConfig>, Options>{}; }); }; template - inline auto HandleRequest(this Self&& self, http::HTTPRequest&& request - ) -> Task requires requires{self.HandleRequestThrow(std::move(request));} { + 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)); @@ -25,11 +25,11 @@ struct HTTPHandlerBase { } catch(...) { fmt::println("Error in handler with default name {}: Unknown Error", T::kName); }; - co_return http::HTTPResponse{.statusCode = 500, .statusMessage = "Internal Server Error", .body = "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, - cserver::server::http::HTTPStream& stream) -> Task requires requires{self.HandleRequestStreamThrow(std::move(request), stream);} { + inline auto HandleRequestStream(this Self&& self, cserver::server::http::HttpRequest&& request, + cserver::server::http::HttpStream& stream) -> Task requires requires{self.HandleRequestStreamThrow(std::move(request), stream);} { using T = std::remove_cvref_t; try { co_await std::forward(self).HandleRequestStreamThrow(std::move(request), stream); @@ -40,20 +40,20 @@ struct HTTPHandlerBase { }; co_await stream.Close(); }; - inline constexpr HTTPHandlerBase(auto, auto&) {}; + inline constexpr HttpHandlerBase(auto, auto&) {}; }; template -struct HTTPHandlerAdder { +struct HttpHandlerAdderType { template static consteval auto Adder(const auto& context) { - return HTTPHandlerBase::template Adder(context); + return HttpHandlerBase::template HttpHandlerAdder(context); }; }; template -struct HTTPHandlerBaseWithAdder : HTTPHandlerBase, HTTPHandlerAdder { - inline constexpr HTTPHandlerBaseWithAdder(auto name, auto& context) : - HTTPHandlerBase(name, context), - HTTPHandlerAdder{} {}; +struct HttpHandlerBaseWithAdder : HttpHandlerBase, HttpHandlerAdderType { + inline constexpr HttpHandlerBaseWithAdder(auto name, auto& context) : + HttpHandlerBase(name, context), + HttpHandlerAdderType{} {}; }; } // namespace cserver::server::handlers diff --git a/include/cserver/server/http/http_request.hpp b/include/cserver/server/http/http_request.hpp index c58c065..80528f1 100644 --- a/include/cserver/server/http/http_request.hpp +++ b/include/cserver/server/http/http_request.hpp @@ -6,14 +6,14 @@ namespace cserver::server::http { -struct HTTPRequest { +struct HttpRequest { std::string method = {}; boost::urls::url url = {}; std::unordered_map headers = {}; std::string body = {}; inline auto ToString() const -> std::string { std::ostringstream stream; - stream << fmt::format("{} {} HTTP/1.1\r\n", this->method, this->url.path()); + stream << fmt::format("{} {} Http/1.1\r\n", this->method, this->url.path()); for(const auto& header : this->headers) { stream << fmt::format("{}: {}\r\n", header.first, header.second); }; diff --git a/include/cserver/server/http/http_request_parser.hpp b/include/cserver/server/http/http_request_parser.hpp index 368781e..bf5afd9 100644 --- a/include/cserver/server/http/http_request_parser.hpp +++ b/include/cserver/server/http/http_request_parser.hpp @@ -4,65 +4,65 @@ namespace cserver::server::http { -struct HTTPRequestParser : private llhttp_t, public HTTPRequest { +struct HttpRequestParser : private llhttp_t, public HttpRequest { bool err = false; bool done = false; std::string headerField = {}; std::string headerValue = {}; std::string urlString = {}; - inline HTTPRequestParser(std::string_view data) { + inline HttpRequestParser(std::string_view data) { llhttp_settings_t settings; llhttp_settings_init(&settings); - settings.on_method = HTTPRequestParser::OnMethod; - settings.on_url = HTTPRequestParser::OnUrl; - settings.on_url_complete = HTTPRequestParser::OnUrlComplete; - settings.on_header_field = HTTPRequestParser::OnHeaderField; - settings.on_header_value = HTTPRequestParser::OnHeaderValue; - settings.on_headers_complete = HTTPRequestParser::OnHeaderComplete; - settings.on_body = HTTPRequestParser::OnBody; - settings.on_message_complete = HTTPRequestParser::OnMessageComplete; + settings.on_method = HttpRequestParser::OnMethod; + settings.on_url = HttpRequestParser::OnUrl; + settings.on_url_complete = HttpRequestParser::OnUrlComplete; + settings.on_header_field = HttpRequestParser::OnHeaderField; + settings.on_header_value = HttpRequestParser::OnHeaderValue; + settings.on_headers_complete = HttpRequestParser::OnHeaderComplete; + settings.on_body = HttpRequestParser::OnBody; + settings.on_message_complete = HttpRequestParser::OnMessageComplete; llhttp_init(this, HTTP_BOTH, &settings); llhttp_execute(this, data.data(), data.size()); }; static inline auto OnMethod(llhttp_t* parser, const char* data, std::size_t size) -> int { - auto* self = static_cast(static_cast(parser)); + auto* self = static_cast(static_cast(parser)); self->method.append(data, size); return 0; }; static inline auto OnUrl(llhttp_t* parser, const char* data, std::size_t size) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->urlString.append(data, size); return 0; }; static inline auto OnUrlComplete(llhttp_t* parser) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->url = boost::urls::url(self->urlString); return 0; }; static inline auto OnHeaderField(llhttp_t* parser, const char* data, std::size_t size) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->headerField.append(data, size); return 0; }; static inline auto OnHeaderValue(llhttp_t* parser, const char* data, std::size_t size) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->headerValue.append(data, size); return 0; }; static inline auto OnHeaderComplete(llhttp_t* parser) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->headers.emplace(std::move(self->headerField), std::move(self->headerValue)); self->headerValue.clear(); self->headerField.clear(); return 0; }; static inline auto OnBody(llhttp_t* parser, const char* data, std::size_t size) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->body.append(data, size); return 0; }; static inline auto OnMessageComplete(llhttp_t* parser) -> int { - auto* self = static_cast(parser); + auto* self = static_cast(parser); self->done = true; return 0; }; diff --git a/include/cserver/server/http/http_response.hpp b/include/cserver/server/http/http_response.hpp index e059508..274676f 100644 --- a/include/cserver/server/http/http_response.hpp +++ b/include/cserver/server/http/http_response.hpp @@ -5,14 +5,14 @@ namespace cserver::server::http { -struct HTTPResponse { +struct HttpResponse { unsigned short statusCode = 200; std::string statusMessage = "OK"; std::unordered_map headers = {}; std::string body = {}; inline auto ToString() const -> std::string { std::ostringstream stream; - stream << fmt::format("HTTP/1.1 {} {}\r\n", this->statusCode, this->statusMessage); + stream << fmt::format("Http/1.1 {} {}\r\n", this->statusCode, this->statusMessage); for(const auto& header : this->headers) { stream << fmt::format("{}: {}\r\n", header.first, header.second); }; diff --git a/include/cserver/server/http/http_stream.hpp b/include/cserver/server/http/http_stream.hpp index 5701bfc..757718b 100644 --- a/include/cserver/server/http/http_stream.hpp +++ b/include/cserver/server/http/http_stream.hpp @@ -5,7 +5,7 @@ namespace cserver::server::http { -struct HTTPStream { +struct HttpStream { boost::asio::ip::tcp::socket socket; std::stringstream stream = {}; inline auto SetMethod(std::string method) -> Task { @@ -13,7 +13,7 @@ struct HTTPStream { co_await boost::asio::async_write(this->socket, boost::asio::buffer(method.data(), method.size()), boost::asio::transfer_all(), boost::asio::use_awaitable); }; inline auto SetStatus(std::string status) -> Task { - status = fmt::format("HTTP/1.1 {}\r\n", std::move(status)); + status = fmt::format("Http/1.1 {}\r\n", std::move(status)); co_await boost::asio::async_write(this->socket, boost::asio::buffer(status.data(), status.size()), boost::asio::transfer_all(), boost::asio::use_awaitable); }; diff --git a/include/cserver/server/server/server.hpp b/include/cserver/server/server/server.hpp index ada0d7c..483ffa0 100644 --- a/include/cserver/server/server/server.hpp +++ b/include/cserver/server/server/server.hpp @@ -49,13 +49,13 @@ struct Server : StopBlocker { Server(std::index_sequence_for{}, name, context) { }; template - auto ProcessHandler(Socket&& socket, http::HTTPRequest request) -> Task { - if constexpr(requires(http::HTTPStream& stream){Get(this->handlers).HandleRequestStream(std::move(request), stream);}) { - http::HTTPStream stream{std::move(socket)}; + auto ProcessHandler(Socket&& socket, http::HttpRequest request) -> Task { + if constexpr(requires(http::HttpStream& stream){Get(this->handlers).HandleRequestStream(std::move(request), stream);}) { + http::HttpStream stream{std::move(socket)}; co_await Get(this->handlers).HandleRequestStream(std::move(request), stream); co_return; } else { - http::HTTPResponse response = co_await Get(this->handlers).HandleRequest(std::move(request)); + 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(); @@ -67,7 +67,7 @@ struct Server : StopBlocker { std::string buffer; buffer.reserve(socket.available()); co_await boost::asio::async_read_until(socket, boost::asio::dynamic_buffer(buffer), "\r\n\r\n", boost::asio::use_awaitable); - http::HTTPRequest request = http::HTTPRequestParser{buffer}; + http::HttpRequest request = http::HttpRequestParser{buffer}; bool flag = false; co_await [&](std::index_sequence) -> cserver::Task { (co_await [&] -> cserver::Task { @@ -76,7 +76,7 @@ struct Server : StopBlocker { }; }(), ...); }(std::index_sequence_for()); - constexpr std::string_view error404 = "HTTP/1.1 404 Not Found\r\n" + constexpr std::string_view error404 = "Http/1.1 404 Not Found\r\n" "Content-Length: 0\r\n" "\r\n"; if(!flag) {