Skip to content

Commit

Permalink
Created an interface for transport context
Browse files Browse the repository at this point in the history
  • Loading branch information
maddsua committed Feb 12, 2024
1 parent 0842125 commit 7207bee
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 31 deletions.
46 changes: 20 additions & 26 deletions core/http/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> 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;
};
Expand Down
45 changes: 45 additions & 0 deletions core/http/transport_impl.hpp
Original file line number Diff line number Diff line change
@@ -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 <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <optional>

namespace Lambda::HTTP::Transport {

class TransportContextV1 : public TransportContext {
private:
Network::TCP::Connection& m_conn;
const TransportOptions& m_topts;

std::vector<uint8_t> 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
1 change: 1 addition & 0 deletions core/http/transport_v1.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "./transport.hpp"
#include "./transport_impl.hpp"
#include "../polyfill/polyfill.hpp"
#include "../compression/compression.hpp"
#include "../network/network.hpp"
Expand Down
2 changes: 2 additions & 0 deletions core/server/handler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "./server_impl.hpp"
#include "../http/transport.hpp"
#include "../http/transport_impl.hpp"
#include "../crypto/crypto.hpp"

using namespace Lambda;
Expand Down
1 change: 0 additions & 1 deletion core/server/server_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define __LIB_MADDSUA_LAMBDA_CORE_SERVER_IMPL__

#include "./server.hpp"
#include "../http/transport.hpp"
#include "../network/tcp/connection.hpp"

#include <string>
Expand Down
2 changes: 1 addition & 1 deletion core/sse/sse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion core/sse/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion core/websocket/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion core/websocket/websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7207bee

Please sign in to comment.