From 3d802094ae088b424a7c2aa3438a6f627c230734 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 13:46:13 +0200 Subject: [PATCH 01/29] updated uid class Update crypto.hpp Update uid.cpp --- core/crypto/crypto.hpp | 21 +++++++++++++--- core/crypto/uid.cpp | 55 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/core/crypto/crypto.hpp b/core/crypto/crypto.hpp index 9dfd7e08d..d8e0e4819 100644 --- a/core/crypto/crypto.hpp +++ b/core/crypto/crypto.hpp @@ -25,15 +25,30 @@ namespace Lambda::Crypto { class ShortID { private: + union { - uint32_t numeric = 0; - char buffer[sizeof(uint32_t)]; + uint32_t u32 = 0; + uint8_t buff[sizeof(uint32_t)]; } m_id; + std::string m_str; + + void m_serialize() noexcept; + public: ShortID(); ShortID(uint32_t init); - std::string toString() const; + + ShortID(const ShortID& other) noexcept; + ShortID(ShortID&& other) noexcept; + + ShortID& operator=(const ShortID& other) noexcept; + + void update() noexcept; + void update(uint32_t value) noexcept; + + uint32_t id() const noexcept; + const std::string& toString() const noexcept; }; } diff --git a/core/crypto/uid.cpp b/core/crypto/uid.cpp index 467f64e2b..4961783ec 100644 --- a/core/crypto/uid.cpp +++ b/core/crypto/uid.cpp @@ -4,26 +4,61 @@ #include +using namespace Lambda; using namespace Lambda::Crypto; ShortID::ShortID() { - time_t timeHighres = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - this->m_id.numeric = (timeHighres & ~0UL); + this->update(); } ShortID::ShortID(uint32_t init) { - this->m_id.numeric = init; + this->update(init); } -std::string ShortID::toString() const { +ShortID::ShortID(const ShortID& other) noexcept { + this->m_id.u32 = other.m_id.u32; + this->m_str = other.m_str; +} - std::string idstring; - idstring.reserve(sizeof(this->m_id.buffer) * 2); +ShortID::ShortID(ShortID&& other) noexcept { + this->m_id.u32 = other.m_id.u32; + this->m_str = std::move(other.m_str); +} + +ShortID& ShortID::operator=(const ShortID& other) noexcept { + this->m_id.u32 = other.m_id.u32; + this->m_str = other.m_str; + return *this; +} - for (size_t i = 0; i < sizeof(this->m_id.buffer); i++) { - idstring.append(Encoding::byteToHex(this->m_id.buffer[i]).string); +void ShortID::update() noexcept { + time_t timeHighres = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + this->m_id.u32 = (timeHighres & ~0UL) ^ (timeHighres & (static_cast(~0UL) << 32)); + this->m_serialize(); +} + +void ShortID::update(uint32_t value) noexcept { + this->m_id.u32 = value; + this->m_serialize(); +} + +void ShortID::m_serialize() noexcept { + + std::string temphex; + + temphex.reserve(sizeof(this->m_id) * 2); + + for (size_t i = 0; i < sizeof(this->m_id); i++) { + temphex.append(Encoding::byteToHex(this->m_id.buff[i]).string); } - Strings::toLowerCase(idstring); - return idstring; + this->m_str = Strings::toLowerCase(static_cast(temphex)); +} + +uint32_t ShortID::id() const noexcept { + return this->m_id.u32; +} + +const std::string& ShortID::toString() const noexcept { + return this->m_str; } From 8904f8c49b7fd29e9ae81133379cca4094f7c352 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 14:02:38 +0200 Subject: [PATCH 02/29] Update instance --- core/server/instance.cpp | 9 ++++----- core/server/server.hpp | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/server/instance.cpp b/core/server/instance.cpp index 24a52b922..e61061433 100644 --- a/core/server/instance.cpp +++ b/core/server/instance.cpp @@ -1,6 +1,5 @@ #include "./server.hpp" #include "./internal.hpp" -#include "../crypto/crypto.hpp" #include "../network/tcp/listener.hpp" #include @@ -45,7 +44,7 @@ void LambdaInstance::start() { this->watchdogWorker = std::async([&]() { - while (!this->terminated && this->listener.active()) { + while (!this->m_terminated && this->listener.active()) { auto nextConn = this->listener.acceptConnection(); if (!nextConn.has_value()) break; @@ -76,7 +75,7 @@ void LambdaInstance::start() { } break; default: { - this->terminated = true; + this->m_terminated = true; throw std::runtime_error("connection handler undefined"); } break; } @@ -110,7 +109,7 @@ void LambdaInstance::start() { }); if (config.loglevel.startMessage) { - syncout.log("[Service] Started server at http://localhost:" + std::to_string(this->config.service.port) + '/'); + syncout.log("[Service] Started at http://localhost:" + std::to_string(this->config.service.port) + '/'); } } @@ -120,7 +119,7 @@ void LambdaInstance::shutdownn() { } void LambdaInstance::terminate() { - this->terminated = true; + this->m_terminated = true; this->listener.stop(); this->awaitFinished(); } diff --git a/core/server/server.hpp b/core/server/server.hpp index 3f13b2aad..3ae40a1a9 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -91,7 +91,7 @@ namespace Lambda { HandlerType handlerType = HandlerType::Undefined; std::future watchdogWorker; - bool terminated = false; + bool m_terminated = false; void start(); void terminate(); From 0966808ded26abaa77261f3210414eafd22cfed2 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:08:03 +0200 Subject: [PATCH 03/29] Update server.hpp --- core/server/server.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/server/server.hpp b/core/server/server.hpp index 3ae40a1a9..2ae2077e0 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -11,6 +11,7 @@ #include "../http/transport.hpp" #include "../http/http.hpp" #include "../websocket/websocket.hpp" +#include "../crypto/crypto.hpp" #include "../utils/utils.hpp" namespace Lambda { @@ -54,6 +55,9 @@ namespace Lambda { HTTP, WS }; + Crypto::ShortID m_ctx_id; + Crypto::ShortID m_lastrq_id; + Network::TCP::Connection& conn; const ServeOptions& opts; HTTP::Transport::V1TransportContext ctx; From 07493ac51de55a42a3086631f9e0a07918d10cb8 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:08:05 +0200 Subject: [PATCH 04/29] Update uid.cpp --- core/crypto/uid.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/crypto/uid.cpp b/core/crypto/uid.cpp index 4961783ec..02511de82 100644 --- a/core/crypto/uid.cpp +++ b/core/crypto/uid.cpp @@ -2,11 +2,14 @@ #include "../encoding/encoding.hpp" #include "../polyfill/polyfill.hpp" -#include +#include using namespace Lambda; using namespace Lambda::Crypto; +static auto rng_gen = std::mt19937(std::random_device{}()); +static auto rng_dist = std::uniform_int_distribution(1, std::numeric_limits::max()); + ShortID::ShortID() { this->update(); } @@ -32,8 +35,7 @@ ShortID& ShortID::operator=(const ShortID& other) noexcept { } void ShortID::update() noexcept { - time_t timeHighres = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - this->m_id.u32 = (timeHighres & ~0UL) ^ (timeHighres & (static_cast(~0UL) << 32)); + this->m_id.u32 = rng_dist(rng_gen); this->m_serialize(); } From c1bf29a8ef5349319e1c02a406357aa8f18cba2f Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:30:39 +0200 Subject: [PATCH 05/29] Update server.hpp --- core/server/server.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/server/server.hpp b/core/server/server.hpp index 2ae2077e0..35d91fad9 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -69,6 +69,9 @@ namespace Lambda { IncomingConnection(const IncomingConnection& other) = delete; IncomingConnection& operator=(const IncomingConnection& other) = delete; + const Crypto::ShortID& contextID() const noexcept; + const Crypto::ShortID& requestID() const noexcept; + std::optional nextRequest(); void respond(const HTTP::Response& response); From 8ec51b9197a51443b5ad1f29ba745d4659cffbc5 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:30:41 +0200 Subject: [PATCH 06/29] Update connection.cpp --- core/server/connection.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/server/connection.cpp b/core/server/connection.cpp index 4729ecb7e..8cf6d20fd 100644 --- a/core/server/connection.cpp +++ b/core/server/connection.cpp @@ -18,6 +18,14 @@ IncomingConnection::IncomingConnection( const ServeOptions& optsInit ) : conn(connInit), opts(optsInit), ctx(conn, opts.transport) {} +const Crypto::ShortID& IncomingConnection::contextID() const noexcept { + return this->m_ctx_id; +} + +const Crypto::ShortID& IncomingConnection::requestID() const noexcept { + return this->m_lastrq_id; +} + std::optional IncomingConnection::nextRequest() { if (this->activeProto != ActiveProtocol::HTTP) { From 6361f32825b3b0d2201fcc7cf9950730195fe2a7 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:37:49 +0200 Subject: [PATCH 07/29] Update handlers.cpp --- core/server/handlers.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index 756643b29..e0f8a81fe 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -32,8 +32,8 @@ void Handlers::serverlessHandler( if (!nextOpt.has_value()) break; - auto& next = nextOpt.value(); - auto requestID = Crypto::ShortID().toString(); + const auto& next = nextOpt.value(); + const auto& requestID = connctx.requestID().toString(); HTTP::Response response; std::optional handlerError; From 6840de2b3ced76d45f4f70508560aa7af3d86814 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:45:04 +0200 Subject: [PATCH 08/29] Update instance.cpp --- core/server/instance.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/server/instance.cpp b/core/server/instance.cpp index e61061433..1fe20aeee 100644 --- a/core/server/instance.cpp +++ b/core/server/instance.cpp @@ -90,7 +90,7 @@ void LambdaInstance::start() { syncout.error({ "[Service] Connection to", - conninfo.remoteAddr.hostname, + conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), "terminated", '(' + handlerError.value() + ')' }); From b63cc14dd0888dfe3ad6f8d5ea663859b239a544 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 17:56:03 +0200 Subject: [PATCH 09/29] Update handlers.cpp --- core/server/handlers.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index e0f8a81fe..c8d0d6fc8 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -19,6 +19,21 @@ using namespace Lambda; using namespace Lambda::Server; using namespace Lambda::Server::Handlers; +uint32_t shorthashIpAddressString(const std::string& ipAddress) { + + union { + int32_t i32 = 0; + uint8_t buff[4]; + } id; + + for (size_t i = 0; i < ipAddress.size(); i++) { + if (ipAddress[i] == '.' || ipAddress[i] == ':') continue; + id.buff[i % 4] ^= ipAddress[i]; + } + + return id.i32; +} + void Handlers::serverlessHandler( Network::TCP::Connection& conn, const ServeOptions& config, @@ -33,7 +48,7 @@ void Handlers::serverlessHandler( if (!nextOpt.has_value()) break; const auto& next = nextOpt.value(); - const auto& requestID = connctx.requestID().toString(); + const auto& requestID = connctx.requestID().toString() + '-' + connctx.contextID().toString() + '-' + Crypto::ShortID(shorthashIpAddressString(conninfo.remoteAddr.hostname + std::to_string(conninfo.remoteAddr.port))).toString(); HTTP::Response response; std::optional handlerError; From 0fce6226cdadd514f0805fe39c1c5a40a04e8822 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:07:48 +0200 Subject: [PATCH 10/29] Update server.hpp --- core/server/server.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/server/server.hpp b/core/server/server.hpp index 35d91fad9..0df3fdff4 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -56,7 +56,6 @@ namespace Lambda { }; Crypto::ShortID m_ctx_id; - Crypto::ShortID m_lastrq_id; Network::TCP::Connection& conn; const ServeOptions& opts; @@ -70,7 +69,6 @@ namespace Lambda { IncomingConnection& operator=(const IncomingConnection& other) = delete; const Crypto::ShortID& contextID() const noexcept; - const Crypto::ShortID& requestID() const noexcept; std::optional nextRequest(); void respond(const HTTP::Response& response); From 681aa2dd31d6569c99c3daead26ede50c842708a Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:07:49 +0200 Subject: [PATCH 11/29] Update handlers.cpp --- core/server/handlers.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index c8d0d6fc8..7b1598535 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -48,7 +48,7 @@ void Handlers::serverlessHandler( if (!nextOpt.has_value()) break; const auto& next = nextOpt.value(); - const auto& requestID = connctx.requestID().toString() + '-' + connctx.contextID().toString() + '-' + Crypto::ShortID(shorthashIpAddressString(conninfo.remoteAddr.hostname + std::to_string(conninfo.remoteAddr.port))).toString(); + const auto& requestID = Crypto::ShortID().toString() + '-' + connctx.contextID().toString() + '-' + Crypto::ShortID(shorthashIpAddressString(conninfo.remoteAddr.hostname + std::to_string(conninfo.remoteAddr.port))).toString(); HTTP::Response response; std::optional handlerError; @@ -98,9 +98,18 @@ void Handlers::streamHandler( const ConnectionCallback& handlerCallback ) { + const auto& conninfo = conn.info(); auto connctx = IncomingConnection(conn, config); std::optional handlerError; + if (config.loglevel.connections) { + syncout.log({ + conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), + "created transport ", + connctx.contextID().toString() + }); + } + try { handlerCallback(connctx); @@ -113,8 +122,13 @@ void Handlers::streamHandler( if (handlerError.has_value()) { - if (config.loglevel.requests) { - syncout.error({ "tcp handler crashed:", handlerError.value() }); + if (config.loglevel.requests || config.loglevel.connections) { + syncout.error({ + "streamHandler in transport", + connctx.contextID().toString(), + "crashed:", + handlerError.value() + }); } auto errorResponse = Pages::renderErrorPage(500, handlerError.value(), config.errorResponseType); From 46f391eedd81062fe302989ea3d5bbf3a216b90b Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:07:51 +0200 Subject: [PATCH 12/29] Update connection.cpp --- core/server/connection.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/server/connection.cpp b/core/server/connection.cpp index 8cf6d20fd..29278368d 100644 --- a/core/server/connection.cpp +++ b/core/server/connection.cpp @@ -22,10 +22,6 @@ const Crypto::ShortID& IncomingConnection::contextID() const noexcept { return this->m_ctx_id; } -const Crypto::ShortID& IncomingConnection::requestID() const noexcept { - return this->m_lastrq_id; -} - std::optional IncomingConnection::nextRequest() { if (this->activeProto != ActiveProtocol::HTTP) { From db8343f7a0825e1b5ab2184f88f9520b52c88833 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:15:46 +0200 Subject: [PATCH 13/29] Update handlers.cpp --- core/server/handlers.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index 7b1598535..879b29a8b 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -102,10 +102,11 @@ void Handlers::streamHandler( auto connctx = IncomingConnection(conn, config); std::optional handlerError; - if (config.loglevel.connections) { + if (config.loglevel.requests) { syncout.log({ + "[Transport]", conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), - "created transport ", + "created", connctx.contextID().toString() }); } @@ -124,9 +125,8 @@ void Handlers::streamHandler( if (config.loglevel.requests || config.loglevel.connections) { syncout.error({ - "streamHandler in transport", - connctx.contextID().toString(), - "crashed:", + "[Transport] streamHandler crashed in", + connctx.contextID().toString() + ":", handlerError.value() }); } @@ -134,4 +134,12 @@ void Handlers::streamHandler( auto errorResponse = Pages::renderErrorPage(500, handlerError.value(), config.errorResponseType); connctx.respond(errorResponse); } + + if (config.loglevel.requests) { + syncout.error({ + "[Transport]", + connctx.contextID().toString(), + "closed ok" + }); + } } From 7ec6541c899c75bb98f61b17c9f69433de18b0ff Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:15:47 +0200 Subject: [PATCH 14/29] Update instance.cpp --- core/server/instance.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/server/instance.cpp b/core/server/instance.cpp index 1fe20aeee..85c80caeb 100644 --- a/core/server/instance.cpp +++ b/core/server/instance.cpp @@ -39,7 +39,7 @@ LambdaInstance::LambdaInstance( void LambdaInstance::start() { if (this->config.service.fastPortReuse) { - syncout.log("Warning: fast port reuse enabled"); + syncout.log("[Service] Warning: fast port reuse enabled"); } this->watchdogWorker = std::async([&]() { @@ -56,6 +56,7 @@ void LambdaInstance::start() { if (this->config.loglevel.connections) { syncout.log({ + "[Service]", conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), "connected on", conninfo.hostPort @@ -98,6 +99,7 @@ void LambdaInstance::start() { } else if (config.loglevel.connections) { syncout.log({ + "[Service]", conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), "disconnected from", conninfo.hostPort From 47170fb7a9f89591e443468a2170e4926710a20e Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:38:21 +0200 Subject: [PATCH 15/29] add it all --- core/server/connection.cpp | 8 +++++ core/server/handlers.cpp | 28 ++---------------- core/server/instance.cpp | 60 ++++++++++++++++++++++---------------- core/server/internal.hpp | 4 +-- core/server/server.hpp | 2 ++ 5 files changed, 50 insertions(+), 52 deletions(-) diff --git a/core/server/connection.cpp b/core/server/connection.cpp index 29278368d..add560419 100644 --- a/core/server/connection.cpp +++ b/core/server/connection.cpp @@ -22,6 +22,14 @@ const Crypto::ShortID& IncomingConnection::contextID() const noexcept { return this->m_ctx_id; } +Network::TCP::Connection& IncomingConnection::tcpconn() const noexcept { + return this->conn; +} + +const Network::ConnectionInfo& IncomingConnection::conninfo() const noexcept { + return this->conn.info(); +} + std::optional IncomingConnection::nextRequest() { if (this->activeProto != ActiveProtocol::HTTP) { diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index 879b29a8b..c9cec1301 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -35,13 +35,12 @@ uint32_t shorthashIpAddressString(const std::string& ipAddress) { } void Handlers::serverlessHandler( - Network::TCP::Connection& conn, + IncomingConnection& connctx, const ServeOptions& config, const ServerlessCallback& handlerCallback ) { - const auto& conninfo = conn.info(); - auto connctx = IncomingConnection(conn, config); + const auto& conninfo = connctx.conninfo(); while (auto nextOpt = connctx.nextRequest()){ @@ -93,28 +92,15 @@ void Handlers::serverlessHandler( } void Handlers::streamHandler( - Network::TCP::Connection& conn, + IncomingConnection& connctx, const ServeOptions& config, const ConnectionCallback& handlerCallback ) { - const auto& conninfo = conn.info(); - auto connctx = IncomingConnection(conn, config); std::optional handlerError; - if (config.loglevel.requests) { - syncout.log({ - "[Transport]", - conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), - "created", - connctx.contextID().toString() - }); - } - try { - handlerCallback(connctx); - } catch(const std::exception& e) { handlerError = e.what(); } catch(...) { @@ -134,12 +120,4 @@ void Handlers::streamHandler( auto errorResponse = Pages::renderErrorPage(500, handlerError.value(), config.errorResponseType); connctx.respond(errorResponse); } - - if (config.loglevel.requests) { - syncout.error({ - "[Transport]", - connctx.contextID().toString(), - "closed ok" - }); - } } diff --git a/core/server/instance.cpp b/core/server/instance.cpp index 85c80caeb..1dc5e9b4f 100644 --- a/core/server/instance.cpp +++ b/core/server/instance.cpp @@ -52,57 +52,67 @@ void LambdaInstance::start() { std::thread([&](Lambda::Network::TCP::Connection&& conn) { const auto& conninfo = conn.info(); - std::optional handlerError; + auto connctx = IncomingConnection(conn, this->config); + std::optional handlerError; if (this->config.loglevel.connections) { syncout.log({ - "[Service]", + "[Transport]", conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), - "connected on", - conninfo.hostPort + "created", + connctx.contextID().toString() }); } + const auto displayHandlerCrashMessage = [&](const std::string& message) { + + if (!(config.loglevel.connections || config.loglevel.requests)) return; + + auto transportDisplayID = connctx.contextID().toString(); + if (!this->config.loglevel.connections) { + transportDisplayID += '(' + conninfo.remoteAddr.hostname + + ':' + std::to_string(conninfo.remoteAddr.port) + ')'; + } + + syncout.error({ + "[Transport]", + transportDisplayID, + "terminated:", + handlerError.value().what() + }); + }; + try { switch (this->handlerType) { case HandlerType::Serverless: { - serverlessHandler(conn, this->config, this->httpHandler); + serverlessHandler(connctx, this->config, this->httpHandler); } break; case HandlerType::Connection: { - streamHandler(conn, this->config, this->tcpHandler); + streamHandler(connctx, this->config, this->tcpHandler); } break; default: { this->m_terminated = true; - throw std::runtime_error("connection handler undefined"); + throw Lambda::Error("Instance handler undefined"); } break; } - } catch(const std::exception& e) { - handlerError = e.what(); + } catch(const std::exception& err) { + displayHandlerCrashMessage(err.what()); + return; } catch(...) { - handlerError = "unknown error"; + displayHandlerCrashMessage("Unknown exception"); + return; } - if (handlerError.has_value() && (config.loglevel.connections || config.loglevel.requests)) { - - syncout.error({ - "[Service] Connection to", - conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), - "terminated", - '(' + handlerError.value() + ')' - }); - - } else if (config.loglevel.connections) { - + if (config.loglevel.connections) { syncout.log({ - "[Service]", - conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), - "disconnected from", - conninfo.hostPort + "[Transport]", + connctx.contextID().toString(), + "closed ok" }); } diff --git a/core/server/internal.hpp b/core/server/internal.hpp index 81e75ffdf..affdc86ff 100644 --- a/core/server/internal.hpp +++ b/core/server/internal.hpp @@ -10,8 +10,8 @@ namespace Lambda::Server { namespace Handlers { - void serverlessHandler(Network::TCP::Connection& conn, const ServeOptions& config, const ServerlessCallback& handlerCallback); - void streamHandler(Network::TCP::Connection& conn, const ServeOptions& config, const ConnectionCallback& handlerCallback); + void serverlessHandler(IncomingConnection& connctx, const ServeOptions& config, const ServerlessCallback& handlerCallback); + void streamHandler(IncomingConnection& connctx, const ServeOptions& config, const ConnectionCallback& handlerCallback); }; namespace Pages { diff --git a/core/server/server.hpp b/core/server/server.hpp index 0df3fdff4..b34259746 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -69,6 +69,8 @@ namespace Lambda { IncomingConnection& operator=(const IncomingConnection& other) = delete; const Crypto::ShortID& contextID() const noexcept; + Network::TCP::Connection& tcpconn() const noexcept; + const Network::ConnectionInfo& conninfo() const noexcept; std::optional nextRequest(); void respond(const HTTP::Response& response); From e5ab7aa7d1d22b8f558989d23e966aca13da91f6 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:50:42 +0200 Subject: [PATCH 16/29] Update handlers.cpp --- core/server/handlers.cpp | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index c9cec1301..712e7f58d 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -19,21 +19,6 @@ using namespace Lambda; using namespace Lambda::Server; using namespace Lambda::Server::Handlers; -uint32_t shorthashIpAddressString(const std::string& ipAddress) { - - union { - int32_t i32 = 0; - uint8_t buff[4]; - } id; - - for (size_t i = 0; i < ipAddress.size(); i++) { - if (ipAddress[i] == '.' || ipAddress[i] == ':') continue; - id.buff[i % 4] ^= ipAddress[i]; - } - - return id.i32; -} - void Handlers::serverlessHandler( IncomingConnection& connctx, const ServeOptions& config, @@ -47,7 +32,7 @@ void Handlers::serverlessHandler( if (!nextOpt.has_value()) break; const auto& next = nextOpt.value(); - const auto& requestID = Crypto::ShortID().toString() + '-' + connctx.contextID().toString() + '-' + Crypto::ShortID(shorthashIpAddressString(conninfo.remoteAddr.hostname + std::to_string(conninfo.remoteAddr.port))).toString(); + const auto& requestID = connctx.contextID().toString() + '-' + Crypto::ShortID().toString(); HTTP::Response response; std::optional handlerError; From 3a45d5baebf44092359b257f86d6e80415d3e2f8 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:53:05 +0200 Subject: [PATCH 17/29] Update server.hpp --- core/server/server.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/server/server.hpp b/core/server/server.hpp index b34259746..ba7297cfa 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -44,8 +44,8 @@ namespace Lambda { }; struct RequestContext { - std::string requestID; - Network::ConnectionInfo conninfo; + const std::string& requestID; + const Network::ConnectionInfo& conninfo; }; struct IncomingConnection { From 0b5421d3bfaddebfaf408e9711f2d0cea085d8a9 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 18:53:07 +0200 Subject: [PATCH 18/29] Update handlers.cpp --- core/server/handlers.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index 712e7f58d..3f690bb4d 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -32,7 +32,7 @@ void Handlers::serverlessHandler( if (!nextOpt.has_value()) break; const auto& next = nextOpt.value(); - const auto& requestID = connctx.contextID().toString() + '-' + Crypto::ShortID().toString(); + const auto& requestID = Crypto::ShortID(); HTTP::Response response; std::optional handlerError; @@ -40,7 +40,7 @@ void Handlers::serverlessHandler( try { response = handlerCallback(next, { - requestID, + requestID.toString(), conninfo }); @@ -53,19 +53,24 @@ void Handlers::serverlessHandler( if (handlerError.has_value()) { if (config.loglevel.requests) { - syncout.error({ requestID, "crashed:", handlerError.value() }); + syncout.error({ + "[Serverless]", + requestID.toString(), + "crashed:", + handlerError.value() + }); } response = Pages::renderErrorPage(500, handlerError.value(), config.errorResponseType); } - response.headers.set("x-request-id", requestID); + response.headers.set("x-request-id", connctx.contextID().toString() + '-' + requestID.toString()); connctx.respond(response); if (config.loglevel.requests) { syncout.log({ - '[' + requestID + ']', + '[' + requestID.toString() + ']', '(' + conninfo.remoteAddr.hostname + ')', next.method.toString(), next.url.pathname, From 5a14690a753c9b7eefe132ec893b38b8c4874696 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:03:21 +0200 Subject: [PATCH 19/29] Update server.hpp --- core/server/server.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/server/server.hpp b/core/server/server.hpp index ba7297cfa..e0336a8b1 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -44,6 +44,7 @@ namespace Lambda { }; struct RequestContext { + const std::string& contextID; const std::string& requestID; const Network::ConnectionInfo& conninfo; }; From df58008ce675fcf01eccf5b1f054b50b1e6cb41f Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:03:23 +0200 Subject: [PATCH 20/29] Update handlers.cpp --- core/server/handlers.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index 3f690bb4d..3471c9ed6 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -34,12 +34,22 @@ void Handlers::serverlessHandler( const auto& next = nextOpt.value(); const auto& requestID = Crypto::ShortID(); + if (config.loglevel.connections) { + syncout.log({ + "[Serverless]", + connctx.contextID().toString(), + "->", + requestID.toString() + }); + } + HTTP::Response response; std::optional handlerError; try { response = handlerCallback(next, { + connctx.contextID().toString(), requestID.toString(), conninfo }); @@ -70,8 +80,8 @@ void Handlers::serverlessHandler( if (config.loglevel.requests) { syncout.log({ - '[' + requestID.toString() + ']', - '(' + conninfo.remoteAddr.hostname + ')', + "[Serverless]", + requestID.toString(), next.method.toString(), next.url.pathname, "-->", From 5afd5234bdf4c09b27df65e0efe96c53303d2068 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:07:53 +0200 Subject: [PATCH 21/29] seems good --- core/server/handlers.cpp | 21 +++++++++++---------- core/server/instance.cpp | 8 ++++---- core/server/server.hpp | 2 +- examples/api_server/main.cpp | 1 + examples/conn_handler/main.cpp | 2 +- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index 3471c9ed6..cd84316bb 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -26,20 +26,21 @@ void Handlers::serverlessHandler( ) { const auto& conninfo = connctx.conninfo(); + const auto& ctxid = connctx.contextID().toString(); while (auto nextOpt = connctx.nextRequest()){ if (!nextOpt.has_value()) break; const auto& next = nextOpt.value(); - const auto& requestID = Crypto::ShortID(); + const auto& requestID = Crypto::ShortID().toString(); - if (config.loglevel.connections) { + if (config.loglevel.transportEvents) { syncout.log({ "[Serverless]", - connctx.contextID().toString(), + ctxid, "->", - requestID.toString() + requestID }); } @@ -49,8 +50,8 @@ void Handlers::serverlessHandler( try { response = handlerCallback(next, { - connctx.contextID().toString(), - requestID.toString(), + ctxid, + requestID, conninfo }); @@ -65,7 +66,7 @@ void Handlers::serverlessHandler( if (config.loglevel.requests) { syncout.error({ "[Serverless]", - requestID.toString(), + requestID, "crashed:", handlerError.value() }); @@ -74,14 +75,14 @@ void Handlers::serverlessHandler( response = Pages::renderErrorPage(500, handlerError.value(), config.errorResponseType); } - response.headers.set("x-request-id", connctx.contextID().toString() + '-' + requestID.toString()); + response.headers.set("x-request-id", ctxid + '-' + requestID); connctx.respond(response); if (config.loglevel.requests) { syncout.log({ "[Serverless]", - requestID.toString(), + config.loglevel.transportEvents ? requestID : conninfo.remoteAddr.hostname, next.method.toString(), next.url.pathname, "-->", @@ -109,7 +110,7 @@ void Handlers::streamHandler( if (handlerError.has_value()) { - if (config.loglevel.requests || config.loglevel.connections) { + if (config.loglevel.requests || config.loglevel.transportEvents) { syncout.error({ "[Transport] streamHandler crashed in", connctx.contextID().toString() + ":", diff --git a/core/server/instance.cpp b/core/server/instance.cpp index 1dc5e9b4f..7537c7fc4 100644 --- a/core/server/instance.cpp +++ b/core/server/instance.cpp @@ -55,7 +55,7 @@ void LambdaInstance::start() { auto connctx = IncomingConnection(conn, this->config); std::optional handlerError; - if (this->config.loglevel.connections) { + if (this->config.loglevel.transportEvents) { syncout.log({ "[Transport]", conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port), @@ -66,10 +66,10 @@ void LambdaInstance::start() { const auto displayHandlerCrashMessage = [&](const std::string& message) { - if (!(config.loglevel.connections || config.loglevel.requests)) return; + if (!(config.loglevel.transportEvents || config.loglevel.requests)) return; auto transportDisplayID = connctx.contextID().toString(); - if (!this->config.loglevel.connections) { + if (!this->config.loglevel.transportEvents) { transportDisplayID += '(' + conninfo.remoteAddr.hostname + ':' + std::to_string(conninfo.remoteAddr.port) + ')'; } @@ -108,7 +108,7 @@ void LambdaInstance::start() { return; } - if (config.loglevel.connections) { + if (config.loglevel.transportEvents) { syncout.log({ "[Transport]", connctx.contextID().toString(), diff --git a/core/server/server.hpp b/core/server/server.hpp index e0336a8b1..9c74b7f2a 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -17,7 +17,7 @@ namespace Lambda { struct LogOptions { - bool connections = true; + bool transportEvents = true; bool requests = true; bool timestamps = false; bool startMessage = true; diff --git a/examples/api_server/main.cpp b/examples/api_server/main.cpp index 09a446484..e6e7eb6a0 100644 --- a/examples/api_server/main.cpp +++ b/examples/api_server/main.cpp @@ -45,6 +45,7 @@ int main(int argc, char const *argv[]) { }; ServerConfig initparams; + initparams.loglevel.transportEvents = false; initparams.loglevel.requests = true; auto server = LambdaInstance(handler, initparams); diff --git a/examples/conn_handler/main.cpp b/examples/conn_handler/main.cpp index 2917553c0..60fbad45e 100644 --- a/examples/conn_handler/main.cpp +++ b/examples/conn_handler/main.cpp @@ -21,7 +21,7 @@ int main(int argc, char const *argv[]) { }; ServerConfig initparams; - initparams.loglevel.connections = true; + initparams.loglevel.transportEvents = true; auto server = LambdaInstance(handler, initparams); server.awaitFinished(); From 5266ba6de76fee5aa6b46c663a1b268b74333e72 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:08:19 +0200 Subject: [PATCH 22/29] Update server.hpp --- core/server/server.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/server/server.hpp b/core/server/server.hpp index 9c74b7f2a..2151872d6 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -17,9 +17,8 @@ namespace Lambda { struct LogOptions { - bool transportEvents = true; + bool transportEvents = false; bool requests = true; - bool timestamps = false; bool startMessage = true; }; From be2cfb28fac1f6e41c27f53a1206c75db66d0e36 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:08:20 +0200 Subject: [PATCH 23/29] Update main.cpp --- examples/api_server/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api_server/main.cpp b/examples/api_server/main.cpp index e6e7eb6a0..144c42618 100644 --- a/examples/api_server/main.cpp +++ b/examples/api_server/main.cpp @@ -45,7 +45,7 @@ int main(int argc, char const *argv[]) { }; ServerConfig initparams; - initparams.loglevel.transportEvents = false; + initparams.loglevel.transportEvents = true; initparams.loglevel.requests = true; auto server = LambdaInstance(handler, initparams); From 514f4c41a3ba0a8c26fbbc2a957533a6c7957ab1 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:22:25 +0200 Subject: [PATCH 24/29] Update connection.cpp --- core/server/connection.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/core/server/connection.cpp b/core/server/connection.cpp index add560419..2cf96820c 100644 --- a/core/server/connection.cpp +++ b/core/server/connection.cpp @@ -51,7 +51,6 @@ std::optional IncomingConnection::nextRequest() { Also most of the library uses exceptions to do error handling anyway so making any of that that would be just super inconsistent and confusing. */ - if (err.respondStatus.has_value()) { this->respond(Pages::renderErrorPage(err.respondStatus.value(), err.message(), this->opts.errorResponseType)); } From c1d6009bb546dfb6fddd7225a19a9bfce7e133bd Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:43:00 +0200 Subject: [PATCH 25/29] Update server.hpp --- core/server/server.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/server/server.hpp b/core/server/server.hpp index 2151872d6..df8ec6894 100644 --- a/core/server/server.hpp +++ b/core/server/server.hpp @@ -55,11 +55,11 @@ namespace Lambda { HTTP, WS }; - Crypto::ShortID m_ctx_id; - Network::TCP::Connection& conn; const ServeOptions& opts; HTTP::Transport::V1TransportContext ctx; + Crypto::ShortID m_ctx_id; + ActiveProtocol activeProto = ActiveProtocol::HTTP; public: From 949b3f8275c5c44308d4daa9b557a92c5f51daeb Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:43:01 +0200 Subject: [PATCH 26/29] Update handlers.cpp --- core/server/handlers.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/core/server/handlers.cpp b/core/server/handlers.cpp index cd84316bb..fbd92919f 100644 --- a/core/server/handlers.cpp +++ b/core/server/handlers.cpp @@ -35,15 +35,6 @@ void Handlers::serverlessHandler( const auto& next = nextOpt.value(); const auto& requestID = Crypto::ShortID().toString(); - if (config.loglevel.transportEvents) { - syncout.log({ - "[Serverless]", - ctxid, - "->", - requestID - }); - } - HTTP::Response response; std::optional handlerError; @@ -82,7 +73,7 @@ void Handlers::serverlessHandler( if (config.loglevel.requests) { syncout.log({ "[Serverless]", - config.loglevel.transportEvents ? requestID : conninfo.remoteAddr.hostname, + (config.loglevel.transportEvents ? ctxid + '-' : conninfo.remoteAddr.hostname + ' ') + requestID, next.method.toString(), next.url.pathname, "-->", From 8aa0931d786a9342f929a62e172e0dee7f00b2a4 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:43:03 +0200 Subject: [PATCH 27/29] Update connection.cpp --- core/server/connection.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/server/connection.cpp b/core/server/connection.cpp index 2cf96820c..f1c7a5577 100644 --- a/core/server/connection.cpp +++ b/core/server/connection.cpp @@ -13,10 +13,19 @@ using namespace Lambda::Websocket; static const std::string wsMagicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; +uint32_t hashConnectionData(const Network::Address& remoteAddr) { + time_t timeHighres = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + size_t hash = std::hash{}(remoteAddr.hostname + std::to_string(remoteAddr.port)); + uint32_t timefp = (timeHighres & ~0UL) ^ (timeHighres & (static_cast(~0UL) << 32)); + uint32_t infofp = (hash & ~0UL) ^ (hash & (static_cast(~0UL) << 32)); + return timefp ^ infofp; +} + IncomingConnection::IncomingConnection( Network::TCP::Connection& connInit, const ServeOptions& optsInit -) : conn(connInit), opts(optsInit), ctx(conn, opts.transport) {} +) : conn(connInit), opts(optsInit), ctx(conn, opts.transport), + m_ctx_id(hashConnectionData(connInit.info().remoteAddr)) {} const Crypto::ShortID& IncomingConnection::contextID() const noexcept { return this->m_ctx_id; From 6c887a1d4fc07bef92a5daafd7157426d3261905 Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:45:08 +0200 Subject: [PATCH 28/29] Update main.cpp --- examples/api_server/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/api_server/main.cpp b/examples/api_server/main.cpp index 144c42618..68adc0324 100644 --- a/examples/api_server/main.cpp +++ b/examples/api_server/main.cpp @@ -45,8 +45,8 @@ int main(int argc, char const *argv[]) { }; ServerConfig initparams; - initparams.loglevel.transportEvents = true; - initparams.loglevel.requests = true; + //initparams.loglevel.transportEvents = true; + //initparams.loglevel.requests = true; auto server = LambdaInstance(handler, initparams); server.awaitFinished(); From 6b6d4471014c587cac1899b72338452bc4f20cdd Mon Sep 17 00:00:00 2001 From: maddsua Date: Sat, 10 Feb 2024 19:46:27 +0200 Subject: [PATCH 29/29] Update connection.cpp --- core/server/connection.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/server/connection.cpp b/core/server/connection.cpp index f1c7a5577..05a7d06ae 100644 --- a/core/server/connection.cpp +++ b/core/server/connection.cpp @@ -16,9 +16,8 @@ static const std::string wsMagicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; uint32_t hashConnectionData(const Network::Address& remoteAddr) { time_t timeHighres = std::chrono::high_resolution_clock::now().time_since_epoch().count(); size_t hash = std::hash{}(remoteAddr.hostname + std::to_string(remoteAddr.port)); - uint32_t timefp = (timeHighres & ~0UL) ^ (timeHighres & (static_cast(~0UL) << 32)); - uint32_t infofp = (hash & ~0UL) ^ (hash & (static_cast(~0UL) << 32)); - return timefp ^ infofp; + return (timeHighres & ~0UL) ^ (timeHighres & (static_cast(~0UL) << 32)) ^ + (hash & ~0UL) ^ (hash & (static_cast(~0UL) << 32)); } IncomingConnection::IncomingConnection(