From 7207bee2ca3881e94befaff58f7920fcf4dfb29c Mon Sep 17 00:00:00 2001 From: maddsua Date: Mon, 12 Feb 2024 08:51:52 +0200 Subject: [PATCH] Created an interface for transport context --- core/http/transport.hpp | 46 ++++++++++++++++-------------------- core/http/transport_impl.hpp | 45 +++++++++++++++++++++++++++++++++++ core/http/transport_v1.cpp | 1 + core/server/handler.cpp | 2 ++ core/server/server_impl.hpp | 1 - core/sse/sse.hpp | 2 +- core/sse/writer.cpp | 2 +- core/websocket/context.cpp | 2 +- core/websocket/websocket.hpp | 2 +- 9 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 core/http/transport_impl.hpp diff --git a/core/http/transport.hpp b/core/http/transport.hpp index e04d738cc..8a19e9daa 100644 --- a/core/http/transport.hpp +++ b/core/http/transport.hpp @@ -42,33 +42,27 @@ namespace Lambda::HTTP::Transport { bool autocompress = true; }; - class TransportContextV1 { - private: - Network::TCP::Connection& m_conn; - const TransportOptions& m_topts; - - std::vector m_readbuff; - bool m_keepalive = false; - ContentEncodings m_compress = ContentEncodings::None; - HTTP::Request* m_next = nullptr; - + class TransportContext { public: - TransportContextV1(Network::TCP::Connection& connInit, const TransportOptions& optsInit); - - TransportContextV1(const TransportContextV1& other) = delete; - TransportContextV1& operator=(const TransportContextV1& other) = delete; - - Network::TCP::Connection& tcpconn() const noexcept; - const Network::ConnectionInfo& conninfo() const noexcept; - const TransportOptions& options() const noexcept; - const ContentEncodings& getEnconding() const noexcept; - bool ok() const noexcept; - - bool awaitNext(); - HTTP::Request nextRequest(); - void respond(const HTTP::Response& response); - void reset() noexcept; - bool hasPartialData() const noexcept; + TransportContext() = default; + TransportContext(TransportContext&& other) = delete; + TransportContext(const TransportContext& other) = delete; + virtual ~TransportContext() = default; + + TransportContext& operator=(const TransportContext& other) = delete; + TransportContext& operator=(TransportContext&& other) = delete; + + virtual Network::TCP::Connection& tcpconn() const noexcept = 0; + virtual const Network::ConnectionInfo& conninfo() const noexcept = 0; + virtual const TransportOptions& options() const noexcept = 0; + virtual const ContentEncodings& getEnconding() const noexcept = 0; + virtual bool ok() const noexcept = 0; + + virtual bool awaitNext() = 0; + virtual HTTP::Request nextRequest() = 0; + virtual void respond(const HTTP::Response& response) = 0; + virtual void reset() noexcept = 0; + virtual bool hasPartialData() const noexcept = 0; TransportFlags flags; }; diff --git a/core/http/transport_impl.hpp b/core/http/transport_impl.hpp new file mode 100644 index 000000000..ce4007584 --- /dev/null +++ b/core/http/transport_impl.hpp @@ -0,0 +1,45 @@ +#ifndef __LIB_MADDSUA_LAMBDA_CORE_HTTP_TRANSPORT_IMPL__ +#define __LIB_MADDSUA_LAMBDA_CORE_HTTP_TRANSPORT_IMPL__ + +#include "./transport.hpp" + +#include "../network/tcp/connection.hpp" +#include "./http.hpp" +#include "../utils/utils.hpp" + +#include +#include +#include +#include +#include + +namespace Lambda::HTTP::Transport { + + class TransportContextV1 : public TransportContext { + private: + Network::TCP::Connection& m_conn; + const TransportOptions& m_topts; + + std::vector m_readbuff; + bool m_keepalive = false; + ContentEncodings m_compress = ContentEncodings::None; + HTTP::Request* m_next = nullptr; + + public: + TransportContextV1(Network::TCP::Connection& connInit, const TransportOptions& optsInit); + + Network::TCP::Connection& tcpconn() const noexcept; + const Network::ConnectionInfo& conninfo() const noexcept; + const TransportOptions& options() const noexcept; + const ContentEncodings& getEnconding() const noexcept; + bool ok() const noexcept; + + bool awaitNext(); + HTTP::Request nextRequest(); + void respond(const HTTP::Response& response); + void reset() noexcept; + bool hasPartialData() const noexcept; + }; +}; + +#endif diff --git a/core/http/transport_v1.cpp b/core/http/transport_v1.cpp index 57a245a83..1ec137242 100644 --- a/core/http/transport_v1.cpp +++ b/core/http/transport_v1.cpp @@ -1,4 +1,5 @@ #include "./transport.hpp" +#include "./transport_impl.hpp" #include "../polyfill/polyfill.hpp" #include "../compression/compression.hpp" #include "../network/network.hpp" diff --git a/core/server/handler.cpp b/core/server/handler.cpp index 624ce649d..2be303a33 100644 --- a/core/server/handler.cpp +++ b/core/server/handler.cpp @@ -1,4 +1,6 @@ #include "./server_impl.hpp" +#include "../http/transport.hpp" +#include "../http/transport_impl.hpp" #include "../crypto/crypto.hpp" using namespace Lambda; diff --git a/core/server/server_impl.hpp b/core/server/server_impl.hpp index 96b504835..2d8c6bf1d 100644 --- a/core/server/server_impl.hpp +++ b/core/server/server_impl.hpp @@ -2,7 +2,6 @@ #define __LIB_MADDSUA_LAMBDA_CORE_SERVER_IMPL__ #include "./server.hpp" -#include "../http/transport.hpp" #include "../network/tcp/connection.hpp" #include diff --git a/core/sse/sse.hpp b/core/sse/sse.hpp index 0fa2d7322..b962bb13c 100644 --- a/core/sse/sse.hpp +++ b/core/sse/sse.hpp @@ -22,7 +22,7 @@ namespace Lambda::SSE { Network::TCP::Connection& m_conn; public: - Writer(HTTP::Transport::TransportContextV1& httpCtx, const HTTP::Request initRequest); + Writer(HTTP::Transport::TransportContext& httpCtx, const HTTP::Request initRequest); void push(const EventMessage& event); bool connected() const noexcept; void close(); diff --git a/core/sse/writer.cpp b/core/sse/writer.cpp index 19db474a2..f1c0eced5 100644 --- a/core/sse/writer.cpp +++ b/core/sse/writer.cpp @@ -5,7 +5,7 @@ using namespace Lambda; using namespace Lambda::Network; using namespace Lambda::SSE; -Writer::Writer(HTTP::Transport::TransportContextV1& httpCtx, const HTTP::Request initRequest) : m_conn(httpCtx.tcpconn()) { +Writer::Writer(HTTP::Transport::TransportContext& httpCtx, const HTTP::Request initRequest) : m_conn(httpCtx.tcpconn()) { httpCtx.flags.autocompress = false; httpCtx.flags.forceContentLength = false; diff --git a/core/websocket/context.cpp b/core/websocket/context.cpp index b185c5dd9..6fef45a17 100644 --- a/core/websocket/context.cpp +++ b/core/websocket/context.cpp @@ -22,7 +22,7 @@ static const std::string wsMagicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; // It works, so fuck that, I'm not even selling this code to anyone. Yet. Remove when you do, the future Daniel. static const time_t sockRcvTimeout = 100; -WebsocketContext::WebsocketContext(HTTP::Transport::TransportContextV1& httpCtx, const HTTP::Request initRequest) +WebsocketContext::WebsocketContext(HTTP::Transport::TransportContext& httpCtx, const HTTP::Request initRequest) : conn(httpCtx.tcpconn()), topts(httpCtx.options()) { auto headerUpgrade = Strings::toLowerCase(initRequest.headers.get("Upgrade")); diff --git a/core/websocket/websocket.hpp b/core/websocket/websocket.hpp index 31053e36e..a820a65ed 100644 --- a/core/websocket/websocket.hpp +++ b/core/websocket/websocket.hpp @@ -55,7 +55,7 @@ namespace Lambda::Websocket { public: - WebsocketContext(HTTP::Transport::TransportContextV1& httpCtx, const HTTP::Request initRequest); + WebsocketContext(HTTP::Transport::TransportContext& httpCtx, const HTTP::Request initRequest); ~WebsocketContext(); bool awaitMessage();