Skip to content

Commit

Permalink
new http server architecture
Browse files Browse the repository at this point in the history
yup that smells like v3. I hate my adhd!
  • Loading branch information
maddsua committed Feb 1, 2025
1 parent f6af01f commit 0d10cbc
Show file tree
Hide file tree
Showing 42 changed files with 2,512 additions and 65 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ lambda_sfi.hpp
*.gz
*.tgz
*.so

# artifacts
**/.artifacts
3 changes: 3 additions & 0 deletions core/http/status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static const std::map <int, std::string> statusCodeTable = {
{ 206, "Partial Content" },
{ 207, "Multi-Status" },
{ 226, "IM Used" },

{ 300, "Multiple Choices" },
{ 301, "Moved Permanently" },
{ 302, "Found" },
Expand All @@ -28,6 +29,7 @@ static const std::map <int, std::string> statusCodeTable = {
{ 305, "Use Proxy" },
{ 307, "Temporary Redirect" },
{ 308, "Permanent Redirect" },

{ 400, "Bad Request" },
{ 401, "Unauthorized" },
{ 402, "Payment Required" },
Expand Down Expand Up @@ -55,6 +57,7 @@ static const std::map <int, std::string> statusCodeTable = {
{ 429, "Too Many Requests" },
{ 431, "Request Header Fields Too Large" },
{ 451, "Unavailable For Legal Reasons" },

{ 500, "Internal Server Error" },
{ 501, "Not Implemented" },
{ 502, "Bad Gateway" },
Expand Down
4 changes: 2 additions & 2 deletions core/http/transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ namespace Lambda::HTTP::Transport {

virtual const std::string& contextID() const noexcept = 0;

virtual Network::TCP::Connection& tcpconn() const noexcept = 0;
virtual const Network::ConnectionInfo& conninfo() const noexcept = 0;
virtual Net::TCP::Connection& tcpconn() const noexcept = 0;
virtual const Net::ConnectionInfo& conninfo() const noexcept = 0;
virtual const TransportOptions& options() const noexcept = 0;
virtual const ContentEncodings& getEnconding() const noexcept = 0;
virtual bool isConnected() const noexcept = 0;
Expand Down
8 changes: 4 additions & 4 deletions core/http/transport_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Lambda::HTTP::Transport {

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

const Crypto::ShortID m_id;
Expand All @@ -30,12 +30,12 @@ namespace Lambda::HTTP::Transport {
IncomingRequest* m_next = nullptr;

public:
TransportContextV1(Network::TCP::Connection& connInit, const TransportOptions& optsInit);
TransportContextV1(Net::TCP::Connection& connInit, const TransportOptions& optsInit);

const std::string& contextID() const noexcept;

Network::TCP::Connection& tcpconn() const noexcept;
const Network::ConnectionInfo& conninfo() const noexcept;
Net::TCP::Connection& tcpconn() const noexcept;
const Net::ConnectionInfo& conninfo() const noexcept;
const TransportOptions& options() const noexcept;
const ContentEncodings& getEnconding() const noexcept;
bool isConnected() const noexcept;
Expand Down
12 changes: 6 additions & 6 deletions core/http/transport_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using namespace Lambda;
using namespace Lambda::HTTP;
using namespace Lambda::HTTP::Transport;
using namespace Lambda::Network;
using namespace Lambda::Net;

static const std::map<ContentEncodings, std::string> contentEncodingMap = {
{ ContentEncodings::Brotli, "br" },
Expand All @@ -27,7 +27,7 @@ static const std::initializer_list<std::string> compressibleTypes = {
};

TransportContextV1::TransportContextV1(
Network::TCP::Connection& connInit,
Net::TCP::Connection& connInit,
const TransportOptions& optsInit
) : m_conn(connInit), m_topts(optsInit) {}

Expand Down Expand Up @@ -295,7 +295,7 @@ void TransportContextV1::respond(const ResponseContext& responsectx) {
responseHeaders.set("content-length", std::to_string(bodyBufferSize));
}

responseHeaders.set("date", Date().toUTCString());
responseHeaders.set("date", Date().to_utc_string());
responseHeaders.set("server", "maddsua/lambda");
responseHeaders.set("x-request-id", this->m_id.toString() + '-' + responsectx.id.toString());

Expand Down Expand Up @@ -355,15 +355,15 @@ const std::string& TransportContextV1::contextID() const noexcept {
return this->m_id.toString();
}

const Network::ConnectionInfo& TransportContextV1::conninfo() const noexcept {
const Net::ConnectionInfo& TransportContextV1::conninfo() const noexcept {
return this->m_conn.info();
}

const TransportOptions& TransportContextV1::options() const noexcept {
return this->m_topts;
}

Network::TCP::Connection& TransportContextV1::tcpconn() const noexcept {
Net::TCP::Connection& TransportContextV1::tcpconn() const noexcept {
return this->m_conn;
}

Expand Down Expand Up @@ -398,7 +398,7 @@ void TransportContextV1::writeRaw(const std::vector<uint8_t>& data) {
}

std::vector<uint8_t> TransportContextV1::readRaw() {
return this->readRaw(Network::TCP::Connection::DefaultChunkSize);
return this->readRaw(Net::TCP::Connection::DefaultChunkSize);
}

std::vector<uint8_t> TransportContextV1::readRaw(size_t expectedSize) {
Expand Down
2 changes: 1 addition & 1 deletion core/network/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "./sysnetw.hpp"

using namespace Lambda;
using namespace Lambda::Network;
using namespace Lambda::Net;

NetworkError::NetworkError(const std::string& message) : Error(message) {
this->m_code = GetOSErrorCode();
Expand Down
2 changes: 1 addition & 1 deletion core/network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <cstdint>
#include <string>

namespace Lambda::Network {
namespace Lambda::Net {

enum struct ConnectionTransport {
TCP, UDP
Expand Down
4 changes: 2 additions & 2 deletions core/network/tcp/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <set>

using namespace Lambda::Network;
using namespace Lambda::Network::TCP;
using namespace Lambda::Net;
using namespace Lambda::Net::TCP;

static const std::set<int> blockingEndedCodes = {
#ifdef _WIN32
Expand Down
2 changes: 1 addition & 1 deletion core/network/tcp/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <string>
#include <mutex>

namespace Lambda::Network::TCP {
namespace Lambda::Net::TCP {

struct ConnectionFlags {
/**
Expand Down
4 changes: 2 additions & 2 deletions core/network/tcp/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "./connection.hpp"
#include "../sysnetw.hpp"

using namespace Lambda::Network;
using namespace Lambda::Network::TCP;
using namespace Lambda::Net;
using namespace Lambda::Net::TCP;

ListenSocket::ListenSocket(const ListenConfig& init) : config(init) {

Expand Down
2 changes: 1 addition & 1 deletion core/network/tcp/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <optional>

namespace Lambda::Network::TCP {
namespace Lambda::Net::TCP {

struct ListenConfig {
bool allowPortReuse = false;
Expand Down
48 changes: 24 additions & 24 deletions core/polyfill/date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,57 @@
using namespace Lambda;

Date::Date() noexcept {
this->epoch = std::time(nullptr);
gmtime_r(&this->epoch, &this->timestruct);
this->m_unix = std::time(nullptr);
gmtime_r(&this->m_unix, &this->m_tms);
}

Date::Date(time_t epoch) noexcept {
this->epoch = epoch;
gmtime_r(&epoch, &this->timestruct);
this->m_unix = epoch;
gmtime_r(&epoch, &this->m_tms);
}

time_t Date::getTime() const noexcept {
return this->epoch;
time_t Date::epoch() const noexcept {
return this->m_unix;
}

std::string Date::toUTCString() const noexcept {
std::string Date::to_utc_string() const noexcept {
char timebuff[64];
strftime(timebuff, sizeof(timebuff), "%a, %d %b %Y %H:%M:%S GMT", &this->timestruct);
strftime(timebuff, sizeof(timebuff), "%a, %d %b %Y %H:%M:%S GMT", &this->m_tms);
return timebuff;
}

std::string Date::toHRTString() const noexcept {
std::string Date::to_calendar_string() const noexcept {
char timebuff[32];
strftime(timebuff, sizeof(timebuff), "%d %b %Y %H:%M:%S", &this->timestruct);
strftime(timebuff, sizeof(timebuff), "%d %b %Y %H:%M:%S", &this->m_tms);
return std::string(timebuff);
}

std::string Date::getDate() const noexcept {
std::string Date::date() const noexcept {
char timebuff[64];
strftime(timebuff, sizeof(timebuff), "%a, %d %b %Y", &this->timestruct);
strftime(timebuff, sizeof(timebuff), "%a, %d %b %Y", &this->m_tms);
return timebuff;
}

int Date::getSeconds() const noexcept {
return this->timestruct.tm_sec;
int Date::second() const noexcept {
return this->m_tms.tm_sec;
}

int Date::getMonth() const noexcept {
return this->timestruct.tm_mon;
int Date::month() const noexcept {
return this->m_tms.tm_mon;
}

int Date::getMinutes() const noexcept {
return this->timestruct.tm_min;
int Date::minute() const noexcept {
return this->m_tms.tm_min;
}

int Date::getHours() const noexcept {
return this->timestruct.tm_hour;
int Date::hour() const noexcept {
return this->m_tms.tm_hour;
}

int Date::getYear() const noexcept {
return this->timestruct.tm_year;
int Date::year() const noexcept {
return this->m_tms.tm_year;
}

int Date::getDay() const noexcept {
return this->timestruct.tm_mday;
int Date::get_date() const noexcept {
return this->m_tms.tm_mday;
}
24 changes: 12 additions & 12 deletions core/polyfill/polyfill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ namespace Lambda {

class Date {
private:
time_t epoch;
tm timestruct;
time_t m_unix;
tm m_tms;

public:
Date() noexcept;
Date(time_t epoch) noexcept;

std::string getDate() const noexcept;
int getDay() const noexcept;
int getYear() const noexcept;
int getHours() const noexcept;
int getMinutes() const noexcept;
int getMonth() const noexcept;
int getSeconds() const noexcept;
time_t getTime() const noexcept;
std::string toUTCString() const noexcept;
std::string toHRTString() const noexcept;
std::string date() const noexcept;
int get_date() const noexcept;
int year() const noexcept;
int hour() const noexcept;
int minute() const noexcept;
int month() const noexcept;
int second() const noexcept;
time_t epoch() const noexcept;
std::string to_utc_string() const noexcept;
std::string to_calendar_string() const noexcept;
};

namespace Content {
Expand Down
2 changes: 1 addition & 1 deletion core/server/handlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Lambda {
struct RequestContext {
const std::string& contextID;
const std::string& requestID;
const Network::ConnectionInfo& conninfo;
const Net::ConnectionInfo& conninfo;

const std::function<SSE::Writer()>& startEventStream;
const std::function<Websocket::WebsocketContext()>& upgrateToWebsocket;
Expand Down
2 changes: 1 addition & 1 deletion core/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace Lambda {
/**
* Set default connection timeout
*/
Network::ConnectionTimeouts connectionTimeouts;
Net::ConnectionTimeouts connectionTimeouts;
/**
* Max number of simultaneous connections
*
Expand Down
2 changes: 1 addition & 1 deletion core/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Lambda {
*/
class LambdaInstance {
private:
Network::TCP::ListenSocket listener;
Net::TCP::ListenSocket listener;
ServerConfig config;
RequestCallback httpHandler;

Expand Down
2 changes: 1 addition & 1 deletion core/server/worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Lambda {

struct WorkerContext {
Network::TCP::Connection conn;
Net::TCP::Connection conn;
std::thread worker;
bool shutdownFlag = false;
bool finished = false;
Expand Down
2 changes: 1 addition & 1 deletion core/sse/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../polyfill/polyfill.hpp"

using namespace Lambda;
using namespace Lambda::Network;
using namespace Lambda::Net;
using namespace Lambda::SSE;
using namespace Lambda::HTTP;
using namespace Lambda::HTTP::Transport;
Expand Down
2 changes: 1 addition & 1 deletion core/websocket/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ WebsocketContext::WebsocketContext(WebsocketInit init) :
this->m_transport.reset();

this->m_transport.tcpconn().flags.closeOnTimeout = false;
this->m_transport.tcpconn().setTimeouts(sockRcvTimeout, Network::SetTimeoutsDirection::Receive);
this->m_transport.tcpconn().setTimeouts(sockRcvTimeout, Net::SetTimeoutsDirection::Receive);

this->m_reader = std::async(&WebsocketContext::asyncWorker, this);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/api_server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char const *argv[]) {

// create response json
JSON::Map testMap = {
{"date", Date().toUTCString()},
{"date", Date().to_utc_string()},
{"user", username.size() ? username : "anonymous"},
{"useragent", req.headers.get("user-agent")},
{"first_visit", isFirstVisit}
Expand Down
4 changes: 2 additions & 2 deletions tests/tcp/tcp.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ using namespace Lambda;

int main(int argc, char const *argv[]) {

Network::TCP::ListenConfig listenInitOpts;
auto listener = Network::TCP::ListenSocket(listenInitOpts);
Net::TCP::ListenConfig listenInitOpts;
auto listener = Net::TCP::ListenSocket(listenInitOpts);

std::cout << "Started server at http://localhost:" + std::to_string(listener.getConfig().port) + "/\n";

Expand Down
Loading

0 comments on commit 0d10cbc

Please sign in to comment.