diff --git a/.github/workflows/build_nix.yml b/.github/workflows/build_nix.yml index 6190464..20e5e57 100644 --- a/.github/workflows/build_nix.yml +++ b/.github/workflows/build_nix.yml @@ -9,8 +9,6 @@ on: branches: [ master ] pull_request: branches: [ master ] - schedule: - - cron: "0 12 1 * *" jobs: build: @@ -20,6 +18,6 @@ jobs: - name: "Build/Test" run: | - curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_nix + curl -fL --retry 3 --retry-delay 2 -o mkn https://github.com/mkn/mkn/releases/download/latest/mkn_nix chmod +x mkn KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC" -l -pthread diff --git a/.github/workflows/build_osx.yml b/.github/workflows/build_osx.yml index f839e40..b93b2de 100644 --- a/.github/workflows/build_osx.yml +++ b/.github/workflows/build_osx.yml @@ -9,8 +9,6 @@ on: branches: [ master ] pull_request: branches: [ master ] - schedule: - - cron: "0 12 1 * *" jobs: build: @@ -24,5 +22,5 @@ jobs: run: | set -ex export SSL_DIR=$(dirname $(dirname $(find /opt/homebrew/Cellar/openssl@3 | grep "libssl\.a"))) - curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_arm_osx && chmod +x mkn + curl -fL --retry 3 --retry-delay 2 -o mkn https://github.com/mkn/mkn/releases/download/latest/mkn_arm_osx && chmod +x mkn KLOG=3 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC" -b "${SSL_DIR}/include" -B "${SSL_DIR}/lib" diff --git a/.github/workflows/build_win.yml b/.github/workflows/build_win.yml index 886bf42..0ef71c1 100644 --- a/.github/workflows/build_win.yml +++ b/.github/workflows/build_win.yml @@ -9,8 +9,6 @@ on: branches: [ master ] pull_request: branches: [ master ] - schedule: - - cron: "0 12 1 * *" jobs: build: diff --git a/LICENSE.md b/LICENSE.md index a89b593..7002083 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,4 +1,4 @@ -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/asio/fcgi.hpp b/inc/mkn/ram/asio/fcgi.hpp index af27373..f0a9b32 100644 --- a/inc/mkn/ram/asio/fcgi.hpp +++ b/inc/mkn/ram/asio/fcgi.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/html/def.hpp b/inc/mkn/ram/html/def.hpp index dff1d2b..6fa16ee 100644 --- a/inc/mkn/ram/html/def.hpp +++ b/inc/mkn/ram/html/def.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/html/page.hpp b/inc/mkn/ram/html/page.hpp index 81ba424..d32bbb6 100644 --- a/inc/mkn/ram/html/page.hpp +++ b/inc/mkn/ram/html/page.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/html/tag.hpp b/inc/mkn/ram/html/tag.hpp index eac1659..4c99852 100644 --- a/inc/mkn/ram/html/tag.hpp +++ b/inc/mkn/ram/html/tag.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/html4.hpp b/inc/mkn/ram/html4.hpp index 782fd39..6f9c598 100644 --- a/inc/mkn/ram/html4.hpp +++ b/inc/mkn/ram/html4.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/http.hpp b/inc/mkn/ram/http.hpp index c2c0d3b..eaf5b09 100644 --- a/inc/mkn/ram/http.hpp +++ b/inc/mkn/ram/http.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -106,7 +106,7 @@ class Message { bool header(std::string const& s) const { return _hs.count(s); } }; -class KUL_PUBLISH _1_1Response : public Message { +class MKN_KUL_PUBLISH _1_1Response : public Message { protected: uint16_t _s = 200; std::string r = "OK"; @@ -154,7 +154,7 @@ inline std::ostream& operator<<(std::ostream& s, _1_1Response const& r) { return s << r.toString(); } -class KUL_PUBLISH A1_1Request : public Message { +class MKN_KUL_PUBLISH A1_1Request : public Message { protected: uint16_t _port; std::string _ip, _host, _path; @@ -206,7 +206,7 @@ class KUL_PUBLISH A1_1Request : public Message { } }; -class KUL_PUBLISH _1_1GetRequest : public A1_1Request { +class MKN_KUL_PUBLISH _1_1GetRequest : public A1_1Request { public: _1_1GetRequest(std::string const& host, std::string const& path = "", uint16_t const& port = 80, std::string const& ip = "") @@ -217,7 +217,7 @@ class KUL_PUBLISH _1_1GetRequest : public A1_1Request { }; using Get = _1_1GetRequest; -class KUL_PUBLISH _1_1PostRequest : public A1_1Request { +class MKN_KUL_PUBLISH _1_1PostRequest : public A1_1Request { public: _1_1PostRequest(std::string const& host, std::string const& path = "", uint16_t const& port = 80, std::string const& ip = "") @@ -227,7 +227,7 @@ class KUL_PUBLISH _1_1PostRequest : public A1_1Request { }; using Post = _1_1PostRequest; -class KUL_PUBLISH AServer : public mkn::ram::tcp::SocketServer { +class MKN_KUL_PUBLISH AServer : public mkn::ram::tcp::SocketServer { protected: std::function<_1_1Response(A1_1Request const&)> m_func; diff --git a/inc/mkn/ram/http/def.hpp b/inc/mkn/ram/http/def.hpp index 9e9a1ef..3c429f8 100644 --- a/inc/mkn/ram/http/def.hpp +++ b/inc/mkn/ram/http/def.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/http/session.hpp b/inc/mkn/ram/http/session.hpp index 9f7453a..b6d503d 100644 --- a/inc/mkn/ram/http/session.hpp +++ b/inc/mkn/ram/http/session.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/https.hpp b/inc/mkn/ram/https.hpp index 01af4a1..f6fb927 100644 --- a/inc/mkn/ram/https.hpp +++ b/inc/mkn/ram/https.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/mpi.hpp b/inc/mkn/ram/mpi.hpp index 9a1bd51..4362a25 100644 --- a/inc/mkn/ram/mpi.hpp +++ b/inc/mkn/ram/mpi.hpp @@ -1,6 +1,6 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/os/nixish/http.hpp b/inc/mkn/ram/os/nixish/http.hpp index dc9c7cc..a24303d 100644 --- a/inc/mkn/ram/os/nixish/http.hpp +++ b/inc/mkn/ram/os/nixish/http.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -111,7 +111,12 @@ class MultiServer : public mkn::ram::http::Server { MultiServer(short const& p = 80, uint8_t const& acceptThreads = 1, uint8_t const& workerThreads = 1) : Server(p), _acceptThreads(acceptThreads), _workerThreads(workerThreads) {} - ~MultiServer() { KUL_DBG_FUNC_ENTER } + + ~MultiServer() { + MKN_KUL_DBG_FUNC_ENTER; + _acceptPool.stop(); + _workerPool.stop(); + } virtual void start() KTHROW(mkn::ram::tcp::Exception) override; diff --git a/inc/mkn/ram/os/nixish/https.hpp b/inc/mkn/ram/os/nixish/https.hpp index cf92eb1..101e473 100644 --- a/inc/mkn/ram/os/nixish/https.hpp +++ b/inc/mkn/ram/os/nixish/https.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -112,7 +112,7 @@ class MultiServer : public mkn::ram::https::Server { mkn::kul::ChroncurrentThreadPool<> _workerPool; void operateAccept(size_t const& threadID) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; std::map fds; fds.insert(std::make_pair(0, 0)); for (size_t i = threadID; i < _MKN_RAM_TCP_MAX_CLIENT_; i += _acceptThreads) @@ -134,7 +134,7 @@ class MultiServer : public mkn::ram::https::Server { virtual void handleBuffer(std::map& fds, int const& fd, char* in, int const& read, int& e) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _workerPool.async( std::bind(&MultiServer::operateBuffer, std::ref(*this), &fds, fd, in, read, e), std::bind(&MultiServer::errorBuffer, std::ref(*this), std::placeholders::_1)); @@ -143,7 +143,7 @@ class MultiServer : public mkn::ram::https::Server { void operateBuffer(std::map* fds, int const& fd, char* in, int const& read, int& e) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; mkn::ram::https::Server::handleBuffer(*fds, fd, in, read, e); if (e <= 0) { getpeername(m_fds[fd].fd, (struct sockaddr*)&cli_addr, (socklen_t*)&clilen); diff --git a/inc/mkn/ram/os/nixish/tcp.hpp b/inc/mkn/ram/os/nixish/tcp.hpp index 4853b23..53a676d 100644 --- a/inc/mkn/ram/os/nixish/tcp.hpp +++ b/inc/mkn/ram/os/nixish/tcp.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -68,13 +68,13 @@ class Socket : public ASocket { if (this->open) close(); } virtual bool connect(std::string const& host, int16_t const& port) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; if (!SOCKET(sck) || !CONNECT(sck, host, port)) return false; this->open = true; return true; } virtual bool close() override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; bool o1 = this->open; if (this->open) { ::close(sck); @@ -88,7 +88,7 @@ class Socket : public ASocket { } virtual size_t read(T* data, size_t const& len, bool& more) KTHROW(mkn::ram::tcp::Exception) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; struct timeval tv; fd_set fds; int64_t ret = 0, iof = -1; @@ -157,13 +157,13 @@ class Socket : public ASocket { static bool SOCKET(int& sck, int16_t const& domain = AF_INET, int16_t const& type = SOCK_STREAM, int16_t const& protocol = IPPROTO_TCP) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; sck = socket(domain, type, protocol); if (sck < 0) KLOG(ERR) << "SOCKET ERROR CODE: " << sck; return sck >= 0; } static bool CONNECT(int const& sck, std::string const& host, int16_t const& port) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; struct sockaddr_in servAddr; memset(&servAddr, 0, sizeof(servAddr)); servAddr.sin_family = AF_INET; @@ -235,7 +235,7 @@ class SocketServer : public ASocketServer { } virtual bool receive(std::map& fds, int const& fd) { (void)fds; - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; T in[_MKN_RAM_TCP_READ_BUFFER_]; bzero(in, _MKN_RAM_TCP_READ_BUFFER_); int16_t e = 0, read = readFrom(fd, in); @@ -267,7 +267,7 @@ class SocketServer : public ASocketServer { return false; } void closeFDsNoCompress(std::map& fds, std::vector& del) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; for (auto const& fd : del) { ::close(m_fds[fd].fd); m_fds[fd].fd = -1; @@ -279,7 +279,7 @@ class SocketServer : public ASocketServer { closeFDsNoCompress(fds, del); } virtual void loop(std::map& fds) KTHROW(mkn::ram::tcp::Exception) { - // KUL_DBG_FUNC_ENTER + // MKN_KUL_DBG_FUNC_ENTER; auto ret = poll(); if (!s) return; if (ret < 0) @@ -332,7 +332,7 @@ class SocketServer : public ASocketServer { return ::accept(lisock, (struct sockaddr*)&cli_addr[fd], &clilen); } virtual void validAccept(std::map& fds, int const& newlisock, int const& nfd) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; KOUT(DBG) << "New connection , socket fd is " << newlisock << ", is : " << inet_ntoa(cli_addr[nfd].sin_addr) << ", port : " << ntohs(cli_addr[nfd].sin_port); @@ -380,7 +380,7 @@ class SocketServer : public ASocketServer { } } virtual void start() KTHROW(mkn::ram::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _started = mkn::kul::Now::MILLIS(); auto ret = listen(lisock, 256); if (ret < 0) KEXCEPTION("Socket Server error on listen"); @@ -402,7 +402,7 @@ class SocketServer : public ASocketServer { } } virtual void stop() { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; s = 0; ::close(lisock); for (int i = 0; i < _MKN_RAM_TCP_MAX_CLIENT_; i++) diff --git a/inc/mkn/ram/os/win/http.hpp b/inc/mkn/ram/os/win/http.hpp index 9de6598..e810a5a 100644 --- a/inc/mkn/ram/os/win/http.hpp +++ b/inc/mkn/ram/os/win/http.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -75,14 +75,14 @@ class Server : public mkn::ram::http::AServer { return inBuffers[fd].get(); } - virtual KUL_PUBLISH bool receive(std::map& fds, int const& fd) override; + virtual MKN_KUL_PUBLISH bool receive(std::map& fds, int const& fd) override; public: Server(short const& p = 80) : AServer(p) {} virtual ~Server() {} }; -class KUL_PUBLISH MultiServer : public mkn::ram::http::Server { +class MKN_KUL_PUBLISH MultiServer : public mkn::ram::http::Server { protected: uint8_t _acceptThreads, _workerThreads; mkn::kul::Mutex m_mutex; diff --git a/inc/mkn/ram/os/win/https.hpp b/inc/mkn/ram/os/win/https.hpp index 647eec8..7b14e29 100644 --- a/inc/mkn/ram/os/win/https.hpp +++ b/inc/mkn/ram/os/win/https.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -77,13 +77,13 @@ class Server : public mkn::ram::http::Server { mkn::kul::File crt, key; std::string const cs; - virtual KUL_PUBLISH void loop(std::map& fds) + virtual MKN_KUL_PUBLISH void loop(std::map& fds) KTHROW(mkn::ram::tcp::Exception) override; - virtual KUL_PUBLISH bool receive(std::map& fds, int const& fd) override; + virtual MKN_KUL_PUBLISH bool receive(std::map& fds, int const& fd) override; - virtual KUL_PUBLISH void handleBuffer(std::map& fds, int const& fd, char* in, - int const& read, int& e); + virtual MKN_KUL_PUBLISH void handleBuffer(std::map& fds, int const& fd, char* in, + int const& read, int& e); public: Server(short const& p, mkn::kul::File const& c, mkn::kul::File const& k, @@ -94,12 +94,12 @@ class Server : public mkn::ram::http::Server { virtual ~Server() { if (s) stop(); } - KUL_PUBLISH void setChain(mkn::kul::File const& f); - KUL_PUBLISH Server& init(); - KUL_PUBLISH virtual void stop() override; + MKN_KUL_PUBLISH void setChain(mkn::kul::File const& f); + MKN_KUL_PUBLISH Server& init(); + MKN_KUL_PUBLISH virtual void stop() override; }; -class KUL_PUBLISH MultiServer : public mkn::ram::https::Server { +class MKN_KUL_PUBLISH MultiServer : public mkn::ram::https::Server { protected: uint8_t _acceptThreads, _workerThreads; mkn::kul::Mutex m_mutex; diff --git a/inc/mkn/ram/os/win/tcp.hpp b/inc/mkn/ram/os/win/tcp.hpp index 54247f1..e6f7948 100644 --- a/inc/mkn/ram/os/win/tcp.hpp +++ b/inc/mkn/ram/os/win/tcp.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,6 +49,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #pragma comment(lib, "Mswsock.lib") #pragma comment(lib, "AdvApi32.lib") +#ifndef __MKN_RAM_TCP_BIND_SOCKTOPTS__ +#define __MKN_RAM_TCP_BIND_SOCKTOPTS__ SO_REUSEADDR +#endif //__MKN_RAM_TCP_BIND_SOCKTOPTS__ + namespace mkn { namespace ram { namespace tcp { @@ -67,13 +71,13 @@ class Socket : public ASocket { if (this->open) close(); } virtual bool connect(std::string const& host, int16_t const& port) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; if (!CONNECT(*this, host, port)) return false; this->open = true; return true; } virtual bool close() override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; bool o1 = this->open; if (this->open) { this->open = 0; @@ -91,7 +95,7 @@ class Socket : public ASocket { return read(data, len, more); } virtual size_t read(T* data, size_t const& len, bool& more) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; int16_t d = recv(ConnectSocket, data, len, 0); @@ -107,7 +111,7 @@ class Socket : public ASocket { protected: static bool CONNECT(Socket& sck, std::string const& host, int16_t const& port) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; int16_t e = 0; // Initialize Winsock @@ -183,7 +187,7 @@ class SocketServer : public ASocketServer { } virtual bool receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; T in[_MKN_RAM_TCP_READ_BUFFER_]; ZeroMemory(in, _MKN_RAM_TCP_READ_BUFFER_); @@ -211,12 +215,12 @@ class SocketServer : public ASocketServer { } void closeFDsNoCompress(std::map& fds, std::vector& del) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; for (auto const& fd : del) { ::closesocket(m_fds[fd].fd); m_fds[fd].fd = -1; fds[fd] = 0; - nfds--; + --nfds; } } virtual void closeFDs(std::map& fds, std::vector& del) { @@ -228,9 +232,9 @@ class SocketServer : public ASocketServer { if (ret < 0) KEXCEPTION("Socket Server error on select: " + std::to_string(errno) + " - " + std::string(strerror(errno))); - // if(ret == 0) return; + int newlisock = -1; - ; + for (auto const& pair : fds) { auto& i = pair.first; if (pair.second == 1) continue; @@ -264,7 +268,7 @@ class SocketServer : public ASocketServer { return WSAAccept(lisock, (struct sockaddr*)&cli_addr[fd], &clilen, NULL, NULL); } virtual void validAccept(std::map& fds, int const& newlisock, int const& nfd) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; KOUT(DBG) << "New connection , socket fd is " << newlisock << ", is : " << inet_ntoa(cli_addr[nfd].sin_addr) << ", port : " << ntohs(cli_addr[nfd].sin_port); @@ -298,7 +302,7 @@ class SocketServer : public ASocketServer { } virtual void bind(int sockOpt = __MKN_RAM_TCP_BIND_SOCKTOPTS__) KTHROW(kul::Exception) {} virtual void start() KTHROW(mkn::ram::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _started = mkn::kul::Now::MILLIS(); iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); @@ -310,7 +314,7 @@ class SocketServer : public ASocketServer { hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; - iResult = getaddrinfo(NULL, std::to_string(port()).c_str(), &hints, &result); + iResult = getaddrinfo(NULL, std::to_string(this->port()).c_str(), &hints, &result); if (iResult != 0) { WSACleanup(); KEXCEPTION("getaddrinfo failed with error: ") << iResult; @@ -318,7 +322,8 @@ class SocketServer : public ASocketServer { lisock = socket(result->ai_family, result->ai_socktype, result->ai_protocol); int iso = 1; - int rc = setsockopt(lisock, SOL_SOCKET, SO_REUSEADDR, (char*)&iso, sizeof(iso)); + int rc = + setsockopt(lisock, SOL_SOCKET, __MKN_RAM_TCP_BIND_SOCKTOPTS__, (char*)&iso, sizeof(iso)); if (lisock == INVALID_SOCKET) KEXCEPTION("socket failed with error: ") << WSAGetLastError(); { @@ -348,7 +353,7 @@ class SocketServer : public ASocketServer { } } virtual void stop() { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; s = 0; ::closesocket(lisock); lisock = 0; diff --git a/inc/mkn/ram/tcp.hpp b/inc/mkn/ram/tcp.hpp index e632936..6543fcb 100644 --- a/inc/mkn/ram/tcp.hpp +++ b/inc/mkn/ram/tcp.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/inc/mkn/ram/tcp/def.hpp b/inc/mkn/ram/tcp/def.hpp index 26f39ee..443f0eb 100644 --- a/inc/mkn/ram/tcp/def.hpp +++ b/inc/mkn/ram/tcp/def.hpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/mkn.yaml b/mkn.yaml index af3b0e8..8bd3964 100644 --- a/mkn.yaml +++ b/mkn.yaml @@ -6,18 +6,18 @@ parent: lib profile: - name: base - inc: ./inc - src: ./src + inc: inc + src: src/any if_src: - bsd: ./os/nixish/src - nix: ./os/nixish/src - win: ./os/win/src + bsd: src/nixish + nix: src/nixish + win: src/win - name: lib parent: base dep: mkn.kul if_arg: - win_shared: -DKUL_SHARED -DKUL_EXPORT + win_shared: -DMKN_KUL_SHARED -DMKN_KUL_EXPORT - name: https parent: lib @@ -40,27 +40,27 @@ profile: - name: usage parent: https - main: test/usage.cpp + main: tst/usage.cpp # arg: -D_MKN_RAM_INCLUDE_HTTPS_ # if_link: # win_cl: -nodefaultlib:libucrt.lib ucrt.lib - name: test_fcgi - main: test/fcgi.cpp + main: tst/fcgi.cpp self: fcgi - name: test parent: https mode: none - main: test/test.cpp + main: tst/test.cpp - name: test.client parent: lib - main: test/client.cpp + main: tst/client.cpp - name: test.server parent: lib - main: test/server.cpp + main: tst/server.cpp - name: format mod: diff --git a/os/nixish/inc/mkn/ram/http.hpp b/os/nixish/inc/mkn/ram/http.hpp deleted file mode 100644 index ae0019f..0000000 --- a/os/nixish/inc/mkn/ram/http.hpp +++ /dev/null @@ -1,138 +0,0 @@ -/** -Copyright (c) 2024, Philip Deegan. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Philip Deegan nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#ifndef _MKN_RAM_NIXISH_HTTP_HPP_ -#define _MKN_RAM_NIXISH_HTTP_HPP_ - -#include - -#include "mkn/kul/http/def.hpp" -#include "mkn/kul/threads.hpp" -#include "mkn/ram/http.hpp" -#include "mkn/ram/tcp.hpp" - -namespace mkn { -namespace ram { -namespace http { - -class Server : public mkn::ram::http::AServer { - private: - int fdSize = _MKN_RAM_TCP_READ_BUFFER_; - std::unordered_map> inBuffers; - - protected: - virtual char* getOrCreateBufferFor(int const& fd) { - if (!inBuffers.count(fd)) - inBuffers.insert(std::make_pair(fd, std::unique_ptr(new char[fdSize]))); - return inBuffers[fd].get(); - } - - virtual bool receive(std::map& fds, int const& fd) override; - - public: - Server(short const& p = 80) : AServer(p) {} - virtual ~Server() {} -}; - -class MultiServer : public mkn::ram::http::Server { - protected: - uint8_t _acceptThreads, _workerThreads; - mkn::kul::Mutex m_mutex; - ConcurrentThreadPool<> _acceptPool; - ConcurrentThreadPool<> _workerPool; - - virtual void handleBuffer(std::map& fds, int const& fd, char* in, int const& read, - int& e) override { - _workerPool.async( - std::bind(&MultiServer::operateBuffer, std::ref(*this), &fds, fd, in, read, e), - std::bind(&MultiServer::errorBuffer, std::ref(*this), std::placeholders::_1)); - e = 1; - } - - void operateBuffer(std::map* fds, int const& fd, char* in, int const& read, - int& e) { - mkn::ram::http::Server::handleBuffer(*fds, fd, in, read, e); - if (e <= 0) { - std::vector del{fd}; - closeFDs(*fds, del); - } - } - virtual void errorBuffer(mkn::kul::Exception const& e) { KERR << e.stack(); }; - - void operateAccept(size_t const& threadID) { - std::map fds; - fds.insert(std::make_pair(0, 0)); - for (size_t i = threadID; i < _MKN_RAM_TCP_MAX_CLIENT_; i += _acceptThreads) - fds.insert(std::make_pair(i, 0)); - while (s) try { - mkn::kul::ScopeLock lock(m_mutex); - loop(fds); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - } catch (std::exception const& e1) { - KERR << e1.what(); - } catch (...) { - KERR << "Loop Exception caught"; - } - } - - MultiServer(MultiServer const&) = delete; - MultiServer(MultiServer const&&) = delete; - MultiServer& operator=(MultiServer const&) = delete; - MultiServer& operator=(MultiServer const&&) = delete; - - public: - MultiServer(short const& p = 80, uint8_t const& acceptThreads = 1, - uint8_t const& workerThreads = 1) - : Server(p), _acceptThreads(acceptThreads), _workerThreads(workerThreads) {} - ~MultiServer() { KUL_DBG_FUNC_ENTER } - - virtual void start() KTHROW(kul::tcp::Exception) override; - - virtual void join() { - _acceptPool.join(); - _workerPool.join(); - } - virtual void stop() override { - mkn::ram::http::Server::stop(); - _acceptPool.stop(); - _workerPool.stop(); - } - virtual void interrupt() { - _acceptPool.interrupt(); - _workerPool.interrupt(); - } - std::exception_ptr const& exception() { return _acceptPool.exception(); } -}; -} // namespace http -} // namespace ram -} // namespace mkn - -#endif /* _MKN_RAM_NIXISH_HTTP_HPP_ */ diff --git a/os/nixish/inc/mkn/ram/https.hpp b/os/nixish/inc/mkn/ram/https.hpp deleted file mode 100644 index ef74e5c..0000000 --- a/os/nixish/inc/mkn/ram/https.hpp +++ /dev/null @@ -1,255 +0,0 @@ -/** -Copyright (c) 2024, Philip Deegan. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Philip Deegan nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#ifndef _MKN_RAM_NIXISH_HTTPS_HPP_ -#define _MKN_RAM_NIXISH_HTTPS_HPP_ - -#include -#include -#include -#include -#include -#include - -#include - -#include "mkn/ram/http.hpp" - -#define MKN_RAM_HTTPS_METHOD_APPENDER2(x, y) x##y -#define MKN_RAM_HTTPS_METHOD_APPENDER(x, y) MKN_RAM_HTTPS_METHOD_APPENDER2(x, y) - -#if !defined(_MKN_RAM_HTTPS_CLIENT_METHOD_) && !defined(_MKN_RAM_HTTPS_SERVER_METHOD_) - -#ifndef _MKN_RAM_HTTPS_METHOD_ -#if OPENSSL_VERSION_NUMBER < 0x10100000L -#define _MKN_RAM_HTTPS_METHOD_ DTLS -#else -#define _MKN_RAM_HTTPS_METHOD_ TLS -#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */ -#endif /* _MKN_RAM_HTTPS_METHOD_ */ - -#define _MKN_RAM_HTTPS_CLIENT_METHOD_ \ - MKN_RAM_HTTPS_METHOD_APPENDER(_MKN_RAM_HTTPS_METHOD_, _client_method) -#define _MKN_RAM_HTTPS_SERVER_METHOD_ \ - MKN_RAM_HTTPS_METHOD_APPENDER(_MKN_RAM_HTTPS_METHOD_, _server_method) - -#else - -#ifndef _MKN_RAM_HTTPS_CLIENT_METHOD_ -#define _MKN_RAM_HTTPS_CLIENT_METHOD_ TLS_client_method -#endif /* _MKN_RAM_HTTPS_CLIENT_METHOD_ */ -#ifndef _MKN_RAM_HTTPS_SERVER_METHOD_ -#define _MKN_RAM_HTTPS_SERVER_METHOD_ TLS_server_method -#endif /* _MKN_RAM_HTTPS_SERVER_METHOD_ */ - -#endif /* defined xyz */ - -namespace mkn { -namespace https { - -class Server : public mkn::ram::http::Server { - protected: - X509* cc = {0}; - SSL* ssl_clients[_MKN_RAM_TCP_MAX_CLIENT_] = {0}; - SSL_CTX* ctx = {0}; - mkn::kul::File crt, key; - std::string const cs; - - virtual void loop(std::map& fds) KTHROW(kul::tcp::Exception) override; - - virtual bool receive(std::map& fds, int const& fd) override; - - virtual void handleBuffer(std::map& fds, int const& fd, char* in, int const& read, - int& e) override; - - public: - Server(short const& p, mkn::kul::File const& c, mkn::kul::File const& k, - std::string const& cs = "") - : mkn::ram::http::Server(p), crt(c), key(k), cs(cs) {} - Server(kul::File const& c, mkn::kul::File const& k, std::string const& cs = "") - : mkn::ram::https::Server(443, c, k, cs) {} - virtual ~Server() { - if (s) Server::stop(); - } - void setChain(kul::File const& f); - Server& init(); - virtual void stop() override; -}; - -class MultiServer : public mkn::ram::https::Server { - protected: - uint8_t _acceptThreads, _workerThreads; - std::mutex m_mutex; - ChroncurrentThreadPool<> _acceptPool; - ChroncurrentThreadPool<> _workerPool; - - void operateAccept(size_t const& threadID) { - KUL_DBG_FUNC_ENTER - std::map fds; - fds.insert(std::make_pair(0, 0)); - for (size_t i = threadID; i < _MKN_RAM_TCP_MAX_CLIENT_; i += _acceptThreads) - fds.insert(std::make_pair(i, 0)); - while (s) try { - // mkn::kul::ScopeLock lock(m_mutex); - std::lock_guard lock(m_mutex); - loop(fds); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - } catch (std::exception const& e1) { - KERR << e1.what(); - } catch (...) { - KERR << "Loop Exception caught"; - } - KLOG(ERR) << "SHOULD NOT HAPPEN"; - KEXCEPTION("SHOULD NOT HAPPEN"); - } - - virtual void handleBuffer(std::map& fds, int const& fd, char* in, int const& read, - int& e) override { - KUL_DBG_FUNC_ENTER - _workerPool.async( - std::bind(&MultiServer::operateBuffer, std::ref(*this), &fds, fd, in, read, e), - std::bind(&MultiServer::errorBuffer, std::ref(*this), std::placeholders::_1)); - e = 1; - } - - void operateBuffer(std::map* fds, int const& fd, char* in, int const& read, - int& e) { - KUL_DBG_FUNC_ENTER - mkn::ram::https::Server::handleBuffer(*fds, fd, in, read, e); - if (e <= 0) { - getpeername(m_fds[fd].fd, (struct sockaddr*)&cli_addr, (socklen_t*)&clilen); - KOUT(DBG) << "DISCO " - << ", is : " << inet_ntoa(cli_addr[fd].sin_addr) - << ", port : " << ntohs(cli_addr[fd].sin_port); - onDisconnect(inet_ntoa(cli_addr[fd].sin_addr), ntohs(cli_addr[fd].sin_port)); - std::vector del{fd}; - closeFDs(*fds, del); - } - } - virtual void errorBuffer(mkn::kul::Exception const& e) { KERR << e.stack(); }; - - public: - MultiServer(short const& p, uint8_t const& acceptThreads, uint8_t const& workerThreads, - mkn::kul::File const& c, mkn::kul::File const& k, std::string const& cs = "") - : mkn::ram::https::Server(p, c, k, cs), - _acceptThreads(acceptThreads), - _workerThreads(workerThreads), - _acceptPool(acceptThreads), - _workerPool(workerThreads) { - if (acceptThreads < 1) - KEXCEPTION("MultiServer cannot have less than one threads for accepting"); - if (workerThreads < 1) KEXCEPTION("MultiServer cannot have less than one threads for working"); - } - MultiServer(uint8_t const& acceptThreads, uint8_t const& workerThreads, mkn::kul::File const& c, - mkn::kul::File const& k, std::string const& cs = "") - : MultiServer(443, acceptThreads, workerThreads, c, k, cs) {} - - virtual ~MultiServer() { - _acceptPool.stop(); - _workerPool.stop(); - } - - virtual void start() KTHROW(kul::tcp::Exception) override; - - virtual void join() { - _acceptPool.join(); - _workerPool.join(); - } - virtual void stop() override { - mkn::ram::https::Server::stop(); - _acceptPool.stop(); - _workerPool.stop(); - } - virtual void interrupt() { - _acceptPool.interrupt(); - _workerPool.interrupt(); - } - std::exception_ptr const& exception() { return _acceptPool.exception(); } -}; - -class A1_1Request; -class Requester; -class SSLReqHelper { - friend class A1_1Request; - friend class Requester; - - private: - SSL_CTX* ctx; - SSLReqHelper() { - SSL_library_init(); - SSL_load_error_strings(); - OpenSSL_add_all_algorithms(); - ctx = SSL_CTX_new(_MKN_RAM_HTTPS_CLIENT_METHOD_()); - if (ctx == NULL) { - ERR_print_errors_fp(stderr); - abort(); - KEXCEPTION("HTTPS Request SSL_CTX FAILED"); - } - } - ~SSLReqHelper() { SSL_CTX_free(ctx); } - static SSLReqHelper& INSTANCE() { - static SSLReqHelper i; - return i; - } -}; - -class A1_1Request { - protected: - SSL* ssl = {0}; - A1_1Request() : ssl(SSL_new(SSLReqHelper::INSTANCE().ctx)) {} - ~A1_1Request() { SSL_free(ssl); } -}; - -class Requester { - public: - static void send(std::string const& h, std::string const& req, uint16_t const& p, - std::stringstream& ss, SSL* ssl); -}; - -class _1_1GetRequest : public http::_1_1GetRequest, https::A1_1Request { - public: - _1_1GetRequest(std::string const& host, std::string const& path = "", uint16_t const& port = 443) - : http::_1_1GetRequest(host, path, port) {} - virtual ~_1_1GetRequest() {} - virtual void send() KTHROW(mkn::ram::http::Exception) override; -}; -using Get = _1_1GetRequest; - -class _1_1PostRequest : public http::_1_1PostRequest, https::A1_1Request { - public: - _1_1PostRequest(std::string const& host, std::string const& path = "", uint16_t const& port = 443) - : http::_1_1PostRequest(host, path, port) {} - virtual void send() KTHROW(mkn::ram::http::Exception) override; -}; -using Post = _1_1PostRequest; -} // namespace https -} // namespace mkn -#endif //_MKN_RAM_NIXISH_HTTPS_HPP_ diff --git a/os/nixish/inc/mkn/ram/tcp.hpp b/os/nixish/inc/mkn/ram/tcp.hpp deleted file mode 100644 index 61ae732..0000000 --- a/os/nixish/inc/mkn/ram/tcp.hpp +++ /dev/null @@ -1,415 +0,0 @@ -/** -Copyright (c) 2024, Philip Deegan. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Philip Deegan nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#ifndef _MKN_RAM_NIXISH_TCP_HPP_ -#define _MKN_RAM_NIXISH_TCP_HPP_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "mkn/kul/byte.hpp" -#include "mkn/kul/log.hpp" -#include "mkn/kul/time.hpp" -#include "mkn/ram/tcp.base.hpp" -#include "mkn/ram/tcp/def.hpp" - -#ifndef __MKN_RAM_TCP_BIND_SOCKTOPTS__ -#define __MKN_RAM_TCP_BIND_SOCKTOPTS__ SO_REUSEADDR -#endif //__MKN_RAM_TCP_BIND_SOCKTOPTS__ - -namespace mkn { -namespace ram { -namespace tcp { - -template -class Socket : public ASocket { - protected: - int sck = 0; - - public: - virtual ~Socket() { - if (this->open) close(); - } - virtual bool connect(std::string const& host, int16_t const& port) override { - KUL_DBG_FUNC_ENTER - if (!SOCKET(sck) || !CONNECT(sck, host, port)) return false; - this->open = true; - return true; - } - virtual bool close() override { - KUL_DBG_FUNC_ENTER - bool o1 = this->open; - if (this->open) { - ::close(sck); - this->open = 0; - } - return o1; - } - virtual size_t read(T* data, size_t const& len) { - bool more = false; - return read(data, len, more); - } - virtual size_t read(T* data, size_t const& len, bool& more) KTHROW(kul::tcp::Exception) override { - KUL_DBG_FUNC_ENTER - struct timeval tv; - fd_set fds; - int64_t ret = 0, iof = -1; - bool r = 0; - FD_ZERO(&fds); - FD_SET(sck, &fds); - tv.tv_sec = 1; - tv.tv_usec = 500; - ret = select(sck + 1, &fds, NULL, NULL, &tv); - std::function check_error; - check_error = [&](int64_t& ret, bool peek = false) -> bool { - ret = 0; - int error = 0; // or WSAGetLastError() - if (peek) { - ret = recv(sck, data, len, MSG_PEEK); - error = errno; // or WSAGetLastError() - } else { - ret = recv(sck, data, len, 0); - error = errno; // or WSAGetLastError() - } - if (ret > 0) { - return false; - } else if (ret == 0) { - } else { - // there is an error, let's see what it is - // error = errno; // or WSAGetLastError() - switch (error) { - // case EAGAIN: - case EWOULDBLOCK: - // Socket is O_NONBLOCK and there is no data available - case EINTR: - // an interrupt (signal) has been catched - // should be ingore in most cases - break; - default: - // socket has an error, no valid anymore - break; - } - } - return false; - }; - - if (ret < 0) - KEXCEPTION("Failed to read from Server socket " + std::string(strerror(errno))); - else if (ret > 0 && FD_ISSET(sck, &fds)) { - if ((iof = fcntl(sck, F_GETFL, 0)) != -1) fcntl(sck, F_SETFL, iof | O_NONBLOCK); - int64_t size = 0; - while (check_error(ret, false)) { - } - if (ret > 0) ret += read(data + ret, len - ret, more); - ioctl(sck, FIONREAD, &size); - more = size > 0; - if (iof != -1) fcntl(sck, F_SETFL, iof); - } else if (ret == 0 && !FD_ISSET(sck, &fds)) { - while (check_error(ret, false)) { - } - if (ret > 0) ret += read(data + ret, len - ret, more); - if (!r && !ret) KEXCEPTION("Failed to read from Server socket"); - } - return ret; - } - virtual size_t write(T const* data, size_t const& len) override { - return ::send(sck, data, len, 0); - } - - static bool SOCKET(int& sck, int16_t const& domain = AF_INET, int16_t const& type = SOCK_STREAM, - int16_t const& protocol = IPPROTO_TCP) { - KUL_DBG_FUNC_ENTER - sck = socket(domain, type, protocol); - if (sck < 0) KLOG(ERR) << "SOCKET ERROR CODE: " << sck; - return sck >= 0; - } - static bool CONNECT(int const& sck, std::string const& host, int16_t const& port) { - KUL_DBG_FUNC_ENTER - struct sockaddr_in servAddr; - memset(&servAddr, 0, sizeof(servAddr)); - servAddr.sin_family = AF_INET; - int16_t e = 0; - servAddr.sin_port = - !kul::byte::isBigEndian() ? htons(port) : mkn::kul::byte::LittleEndian::UINT32(port); - if (host == "localhost" || host == "127.0.0.1") { - servAddr.sin_addr.s_addr = INADDR_ANY; - e = ::connect(sck, (struct sockaddr*)&servAddr, sizeof(servAddr)); - if (e < 0) KLOG(ERR) << "SOCKET CONNECT ERROR CODE: " << e << " errno: " << errno; - } else if (inet_pton(AF_INET, &host[0], &(servAddr.sin_addr))) { - e = ::connect(sck, (struct sockaddr*)&servAddr, sizeof(servAddr)); - if (e < 0) KLOG(ERR) << "SOCKET CONNECT ERROR CODE: " << e << " errno: " << errno; - } else { - std::string ip; - struct addrinfo hints, *servinfo, *next; - struct sockaddr_in* in; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6 - hints.ai_socktype = SOCK_STREAM; - if ((e = getaddrinfo(host.c_str(), 0, &hints, &servinfo)) != 0) - KEXCEPTION("getaddrinfo failed for host: " + host); - for (next = servinfo; next != NULL; next = next->ai_next) { - in = (struct sockaddr_in*)next->ai_addr; - ip = inet_ntoa(in->sin_addr); - servAddr.sin_addr.s_addr = inet_addr(&ip[0]); - if (ip == "0.0.0.0") continue; - if ((e = ::connect(sck, (struct sockaddr*)&servAddr, sizeof(servAddr))) == 0) break; - } - freeaddrinfo(servinfo); - if (e < 0) KLOG(ERR) << "SOCKET CONNECT ERROR CODE: " << e << " errno: " << errno; - } - return e >= 0; - } -}; - -template -class SocketServer : public ASocketServer { - protected: - bool s = 0; - int lisock = 0, nfds = 12; - int64_t _started; - struct pollfd m_fds[_MKN_RAM_TCP_MAX_CLIENT_]; - socklen_t clilen; - struct sockaddr_in serv_addr, cli_addr[_MKN_RAM_TCP_MAX_CLIENT_]; - - virtual bool handle(T* const in, size_t const& inLen, T* const out, size_t& outLen) { - // default overridable function - (void)in; - (void)inLen; - (void)out; - (void)outLen; - return true; - } - - virtual int readFrom(int const& fd, T* in, int opts = 0) { - size_t size = 0; - int64_t val = 0; - while (1) { - val = ::recv(m_fds[fd].fd, in + size, _MKN_RAM_TCP_READ_BUFFER_ - (size + 1), - MSG_PEEK | MSG_DONTWAIT); - if (val < 0) break; - size += ::recv(m_fds[fd].fd, in + size, _MKN_RAM_TCP_READ_BUFFER_ - (size + 1), opts); - } - return size; - } - virtual int writeTo(int const& fd, T const* const out, size_t size) { - return ::send(m_fds[fd].fd, out, size, 0); - } - virtual bool receive(std::map& fds, int const& fd) { - (void)fds; - KUL_DBG_FUNC_ENTER - T in[_MKN_RAM_TCP_READ_BUFFER_]; - bzero(in, _MKN_RAM_TCP_READ_BUFFER_); - int16_t e = 0, read = readFrom(fd, in); - if (read < 0 && errno != EWOULDBLOCK) - KEXCEPTION("Socket Server error on recv - fd(" + std::to_string(fd) + - ") : " + std::to_string(errno) + " - " + std::string(strerror(errno))); - if (read == 0) { - getpeername(m_fds[fd].fd, (struct sockaddr*)&cli_addr[fd], (socklen_t*)&clilen); - KOUT(DBG) << "Host disconnected , ip: " << inet_ntoa(serv_addr.sin_addr) << ", port " - << ntohs(serv_addr.sin_port); - this->onDisconnect(inet_ntoa(cli_addr[fd].sin_addr), ntohs(cli_addr[fd].sin_port)); - return true; - } else { - bool cl = 1; - in[read] = '\0'; - try { - T out[_MKN_RAM_TCP_READ_BUFFER_]; - bzero(out, _MKN_RAM_TCP_READ_BUFFER_); - size_t outLen; - cl = handle(in, read, out, outLen); - e = writeTo(fd, out, outLen); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - e = -1; - } - if (e < 0) KLOG(ERR) << "Error replying to host errno: " << strerror(errno); - if (e < 0 || cl) return true; - } - return false; - } - void closeFDsNoCompress(std::map& fds, std::vector& del) { - KUL_DBG_FUNC_ENTER; - for (auto const& fd : del) { - ::close(m_fds[fd].fd); - m_fds[fd].fd = -1; - fds[fd] = 0; - nfds--; - } - } - virtual void closeFDs(std::map& fds, std::vector& del) { - closeFDsNoCompress(fds, del); - } - virtual void loop(std::map& fds) KTHROW(kul::tcp::Exception) { - // KUL_DBG_FUNC_ENTER - auto ret = poll(); - if (!s) return; - if (ret < 0) - KEXCEPTION("Socket Server error on select: " + std::to_string(errno) + " - " + - std::string(strerror(errno))); - // if(ret == 0) return; - int newlisock = -1; - for (auto const& pair : fds) { - auto& i = pair.first; - if (pair.second == 1) continue; - if (m_fds[i].revents == 0) continue; - if (m_fds[i].revents != POLLIN) - KEXCEPTION("HTTP Server error on pollin " + std::to_string(m_fds[i].revents)); - - if (m_fds[i].fd == lisock) { - do { - int newFD = nfds; - while (1) { - newFD++; - if (fds.count(newFD) && !fds[newFD]) break; - } - newlisock = accept(newFD); - if (newlisock < 0) { - if (errno != EWOULDBLOCK) KEXCEPTION("SockerServer error on accept"); - break; - } - validAccept(fds, newlisock, newFD); - } while (newlisock != -1); - } - } - std::vector del; - for (auto const& pair : fds) - if (pair.second == 1 && receive(fds, pair.first)) del.push_back(pair.first); - if (del.size()) closeFDs(fds, del); - } - virtual int poll(int timeout = 10) { - auto p = ::poll(m_fds, nfds, timeout); - if (errno == 11) { - mkn::kul::this_thread::sleep(timeout); - return 0; - } else if (errno == 2 || errno == 17 || errno == 32) { - } else if (errno) { - KLOG(ERR) << std::to_string(errno) << " - " << std::string(strerror(errno)); - return -1; - } - return p; - } - virtual int accept(int const& fd) { - return ::accept(lisock, (struct sockaddr*)&cli_addr[fd], &clilen); - } - virtual void validAccept(std::map& fds, int const& newlisock, int const& nfd) { - KUL_DBG_FUNC_ENTER; - KOUT(DBG) << "New connection , socket fd is " << newlisock - << ", is : " << inet_ntoa(cli_addr[nfd].sin_addr) - << ", port : " << ntohs(cli_addr[nfd].sin_port); - this->onConnect(inet_ntoa(cli_addr[nfd].sin_addr), ntohs(cli_addr[nfd].sin_port)); - m_fds[nfd].fd = newlisock; - m_fds[nfd].events = POLLIN; - fds[nfd] = 1; - nfds++; - } - - public: - SocketServer(uint16_t const& p, bool _bind = 1) : mkn::ram::tcp::ASocketServer(p) { - if (_bind) bind(__MKN_RAM_TCP_BIND_SOCKTOPTS__); - memset(m_fds, 0, sizeof(m_fds)); - } - ~SocketServer() { - for (int i = 0; i < _MKN_RAM_TCP_MAX_CLIENT_; i++) ::close(m_fds[i].fd); - } - virtual void bind(int sockOpt = __MKN_RAM_TCP_BIND_SOCKTOPTS__) KTHROW(kul::Exception) { - lisock = socket(AF_INET, SOCK_STREAM, 0); - int iso = 1; - int rc = setsockopt(lisock, SOL_SOCKET, sockOpt, (char*)&iso, sizeof(iso)); - if (rc < 0) { - KERR << std::to_string(errno) << " - " << std::string(strerror(errno)); - KEXCEPTION("Socket Server error on setsockopt"); - } - -#if defined(O_NONBLOCK) - if (-1 == (iso = fcntl(lisock, F_GETFL, 0))) iso = 0; - rc = fcntl(lisock, F_SETFL, iso | O_NONBLOCK); -#else - rc = ioctl(lisock, FIONBIO, (char*)&iso); -#endif - if (rc < 0) KEXCEPTION("Socket Server error on ioctl"); - bzero((char*)&serv_addr, sizeof(serv_addr)); - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = INADDR_ANY; - serv_addr.sin_port = !kul::byte::isBigEndian() - ? htons(this->port()) - : mkn::kul::byte::LittleEndian::UINT32(this->port()); - int16_t e = 0; - if ((e = ::bind(lisock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0) { - KERR << std::to_string(errno) << " - " << std::string(strerror(errno)); - KEXCEPTION("Socket Server error on binding, errno: " + std::to_string(errno)); - } - } - virtual void start() KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER - _started = mkn::kul::Now::MILLIS(); - auto ret = listen(lisock, 256); - if (ret < 0) KEXCEPTION("Socket Server error on listen"); - clilen = sizeof(cli_addr[0]); - s = true; - m_fds[0].fd = lisock; - m_fds[0].events = POLLIN; //|POLLPRI; - nfds = lisock + 1; - std::map fds; - for (size_t i = 0; i < _MKN_RAM_TCP_MAX_CLIENT_; i++) fds.insert(std::make_pair(i, 0)); - try { - while (s) loop(fds); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - } catch (std::exception const& e1) { - KERR << e1.what(); - } catch (...) { - KERR << "Loop Exception caught"; - } - } - virtual void stop() { - KUL_DBG_FUNC_ENTER - s = 0; - ::close(lisock); - for (int i = 0; i < _MKN_RAM_TCP_MAX_CLIENT_; i++) - if (i != lisock) shutdown(i, SHUT_RDWR); - } -}; - -} // namespace tcp -} // namespace ram -} // namespace mkn - -#endif //_MKN_RAM_NIXISH_TCP_HPP_ diff --git a/os/win/inc/mkn/ram/http.hpp b/os/win/inc/mkn/ram/http.hpp deleted file mode 100644 index 6db9190..0000000 --- a/os/win/inc/mkn/ram/http.hpp +++ /dev/null @@ -1,155 +0,0 @@ -/** -Copyright (c) 2024, Philip Deegan. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Philip Deegan nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -// -// MAY REQUIRE: runas admin - netsh http add urlacl url=http://localhost:666/ -// user=EVERYONE listen=yes delegate=no -// -#ifndef _KUL_HTTP_HPP_ -#define _KUL_HTTP_HPP_ - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif - -#include "mkn/kul/log.hpp" -#include "mkn/kul/threads.hpp" -#include "mkn/ram/http.hpp" - -#ifndef UNICODE -#define UNICODE -#endif - -#ifndef MAX_ULONG_STR -#define MAX_ULONG_STR ((ULONG)sizeof("4294967295")) -#endif - -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x0600 -#endif - -#include - -#pragma comment(lib, "httpapi.lib") -#pragma comment(lib, "ws2_32.lib") - -namespace mkn { -namespace ram { -namespace http { - -class Server : public mkn::ram::http::AServer { - private: - int fdSize = _MKN_RAM_TCP_READ_BUFFER_; - std::unordered_map> inBuffers; - - protected: - virtual char* getOrCreateBufferFor(int const& fd) { - if (!inBuffers.count(fd)) - inBuffers.insert(std::make_pair(fd, std::unique_ptr(new char[fdSize]))); - return inBuffers[fd].get(); - } - - virtual KUL_PUBLISH bool receive(std::map& fds, int const& fd) override; - - public: - Server(short const& p = 80) : AServer(p) {} - virtual ~Server() {} -}; - -class KUL_PUBLISH MultiServer : public mkn::ram::http::Server { - protected: - uint8_t _acceptThreads, _workerThreads; - mkn::kul::Mutex m_mutex; - ChroncurrentThreadPool<> _acceptPool; - ChroncurrentThreadPool<> _workerPool; - - virtual void handleBuffer(std::map& fds, int const& fd, char* in, int const& read, - int& e) override { - _workerPool.async( - std::bind(&MultiServer::operateBuffer, std::ref(*this), &fds, fd, in, read, e), - std::bind(&MultiServer::errorBuffer, std::ref(*this), std::placeholders::_1)); - e = 1; - } - - void operateBuffer(std::map* fds, int const& fd, char* in, int const& read, - int& e) { - mkn::ram::http::Server::handleBuffer(*fds, fd, in, read, e); - if (e < 0) { - std::vector del{fd}; - // closeFDs(*fds, del); - } - } - virtual void errorBuffer(mkn::kul::Exception const& e) { KERR << e.stack(); }; - - void operateAccept(size_t const& threadID) { - std::map fds; - fds.insert(std::make_pair(0, 0)); - for (int i = threadID; i < _MKN_RAM_TCP_MAX_CLIENT_; i += _acceptThreads) - fds.insert(std::make_pair(i, 0)); - while (s) try { - mkn::kul::ScopeLock lock(m_mutex); - loop(fds); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - } catch (std::exception const& e1) { - KERR << e1.what(); - } catch (...) { - KERR << "Loop Exception caught"; - } - } - - public: - MultiServer(short const& p = 80, uint8_t const& acceptThreads = 1, - uint8_t const& workerThreads = 1) - : Server(p), _acceptThreads(acceptThreads), _workerThreads(workerThreads) {} - - virtual void start() KTHROW(kul::tcp::Exception) override; - - virtual void join() { - _acceptPool.join(); - _workerPool.join(); - } - virtual void stop() override { - mkn::ram::http::Server::stop(); - _acceptPool.stop(); - _workerPool.stop(); - } - virtual void interrupt() { - _acceptPool.interrupt(); - _workerPool.interrupt(); - } - std::exception_ptr const& exception() { return _acceptPool.exception(); } -}; - -} // namespace http -} // namespace ram -} // namespace mkn - -#endif /* _KUL_HTTP_HPP_ */ diff --git a/os/win/inc/mkn/ram/https.hpp b/os/win/inc/mkn/ram/https.hpp deleted file mode 100644 index 0c0ad76..0000000 --- a/os/win/inc/mkn/ram/https.hpp +++ /dev/null @@ -1,241 +0,0 @@ -/** -Copyright (c) 2024, Philip Deegan. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Philip Deegan nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#ifndef _MKN_RAM_HTTPS_HPP_ -#define _MKN_RAM_HTTPS_HPP_ - -#include -#include -#include -#include -#include -#include - -#include "mkn/ram/http.hpp" - -#define MKN_RAM_HTTPS_METHOD_APPENDER2(x, y) x##y -#define MKN_RAM_HTTPS_METHOD_APPENDER(x, y) MKN_RAM_HTTPS_METHOD_APPENDER2(x, y) - -#if !defined(_MKN_RAM_HTTPS_CLIENT_METHOD_) && !defined(_MKN_RAM_HTTPS_SERVER_METHOD_) - -#ifndef _MKN_RAM_HTTPS_METHOD_ -#define _MKN_RAM_HTTPS_METHOD_ TLS -#endif /* _MKN_RAM_HTTPS_METHOD_ */ - -#define _MKN_RAM_HTTPS_CLIENT_METHOD_ \ - MKN_RAM_HTTPS_METHOD_APPENDER(_MKN_RAM_HTTPS_METHOD_, _client_method) -#define _MKN_RAM_HTTPS_SERVER_METHOD_ \ - MKN_RAM_HTTPS_METHOD_APPENDER(_MKN_RAM_HTTPS_METHOD_, _server_method) - -#else - -#ifndef _MKN_RAM_HTTPS_CLIENT_METHOD_ -#define _MKN_RAM_HTTPS_CLIENT_METHOD_ TLS_client_method -#endif /* _MKN_RAM_HTTPS_CLIENT_METHOD_ */ -#ifndef _MKN_RAM_HTTPS_SERVER_METHOD_ -#define _MKN_RAM_HTTPS_SERVER_METHOD_ TLS_server_method -#endif /* _MKN_RAM_HTTPS_SERVER_METHOD_ */ - -#endif /* defined xyz */ - -namespace mkn { -namespace ram { -namespace https { - -class Server : public mkn::ram::http::Server { - protected: - X509* cc = {0}; - SSL* ssl_clients[_MKN_RAM_TCP_MAX_CLIENT_] = {0}; - SSL_CTX* ctx = {0}; - mkn::kul::File crt, key; - std::string const cs; - - virtual KUL_PUBLISH void loop(std::map& fds) KTHROW(kul::tcp::Exception) override; - - virtual KUL_PUBLISH bool receive(std::map& fds, int const& fd) override; - - virtual KUL_PUBLISH void handleBuffer(std::map& fds, int const& fd, char* in, - int const& read, int& e); - - public: - Server(short const& p, mkn::kul::File const& c, mkn::kul::File const& k, - std::string const& cs = "") - : mkn::ram::http::Server(p), crt(c), key(k), cs(cs) {} - Server(mkn::kul::File const& c, mkn::kul::File const& k, std::string const& cs = "") - : mkn::ram::https::Server(443, c, k, cs) {} - virtual ~Server() { - if (s) stop(); - } - KUL_PUBLISH void setChain(mkn::kul::File const& f); - KUL_PUBLISH Server& init(); - KUL_PUBLISH virtual void stop() override; -}; - -class KUL_PUBLISH MultiServer : public mkn::ram::https::Server { - protected: - uint8_t _acceptThreads, _workerThreads; - mkn::kul::Mutex m_mutex; - ChroncurrentThreadPool<> _acceptPool; - ChroncurrentThreadPool<> _workerPool; - - void operateAccept(size_t const& threadID) { - std::map fds; - fds.insert(std::make_pair(0, 0)); - for (size_t i = threadID; i < _MKN_RAM_TCP_MAX_CLIENT_; i += _acceptThreads) - fds.insert(std::make_pair(i, 0)); - while (s) try { - mkn::kul::ScopeLock lock(m_mutex); - loop(fds); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - } catch (std::exception const& e1) { - KERR << e1.what(); - } catch (...) { - KERR << "Loop Exception caught"; - } - } - - virtual void handleBuffer(std::map& fds, int const& fd, char* in, int const& read, - int& e) override { - _workerPool.async( - std::bind(&MultiServer::operateBuffer, std::ref(*this), &fds, fd, in, read, e), - std::bind(&MultiServer::errorBuffer, std::ref(*this), std::placeholders::_1)); - e = 1; - } - - void operateBuffer(std::map* fds, int const& fd, char* in, int const& read, - int& e) { - mkn::ram::https::Server::handleBuffer(*fds, fd, in, read, e); - if (e < 0) { - std::vector del{fd}; - closeFDs(*fds, del); - } - } - virtual void errorBuffer(mkn::kul::Exception const& e) { KERR << e.stack(); }; - - public: - MultiServer(short const& p, uint8_t const& acceptThreads, uint8_t const& workerThreads, - mkn::kul::File const& c, mkn::kul::File const& k, std::string const& cs = "") - : mkn::ram::https::Server(p, c, k, cs), - _acceptThreads(acceptThreads), - _workerThreads(workerThreads), - _acceptPool(acceptThreads), - _workerPool(workerThreads) { - if (acceptThreads < 1) - KEXCEPTION("MultiServer cannot have less than one threads for accepting"); - if (workerThreads < 1) KEXCEPTION("MultiServer cannot have less than one threads for working"); - } - MultiServer(uint8_t const& acceptThreads, uint8_t const& workerThreads, mkn::kul::File const& c, - mkn::kul::File const& k, std::string const& cs = "") - : MultiServer(443, acceptThreads, workerThreads, c, k, cs) {} - - virtual ~MultiServer() { - _acceptPool.stop(); - _workerPool.stop(); - } - - virtual void start() KTHROW(kul::tcp::Exception) override; - - virtual void join() { - _acceptPool.join(); - _workerPool.join(); - } - virtual void stop() override { - mkn::ram::https::Server::stop(); - _acceptPool.stop(); - _workerPool.stop(); - } - virtual void interrupt() { - _acceptPool.interrupt(); - _workerPool.interrupt(); - } - std::exception_ptr const& exception() { return _acceptPool.exception(); } -}; - -class A1_1Request; -class Requester; -class SSLReqHelper { - friend class A1_1Request; - friend class Requester; - - private: - SSL_CTX* ctx; - SSLReqHelper() { - SSL_library_init(); - SSL_load_error_strings(); - OpenSSL_add_all_algorithms(); - ctx = SSL_CTX_new(_MKN_RAM_HTTPS_CLIENT_METHOD_()); - if (ctx == NULL) { - ERR_print_errors_fp(stderr); - abort(); - KEXCEPTION("HTTPS Request SSL_CTX FAILED"); - } - } - ~SSLReqHelper() { SSL_CTX_free(ctx); } - static SSLReqHelper& INSTANCE() { - static SSLReqHelper i; - return i; - } -}; - -class A1_1Request { - protected: - SSL* ssl = {0}; - A1_1Request() : ssl(SSL_new(SSLReqHelper::INSTANCE().ctx)) {} - ~A1_1Request() { SSL_free(ssl); } -}; - -class Requester { - public: - static void send(std::string const& h, std::string const& req, uint16_t const& p, - std::stringstream& ss, SSL* ssl); -}; - -class _1_1GetRequest : public http::_1_1GetRequest, https::A1_1Request { - public: - _1_1GetRequest(std::string const& host, std::string const& path = "", uint16_t const& port = 443) - : http::_1_1GetRequest(host, path, port) {} - virtual ~_1_1GetRequest() {} - virtual void send() KTHROW(mkn::ram::http::Exception) override; -}; -using Get = _1_1GetRequest; - -class _1_1PostRequest : public http::_1_1PostRequest, https::A1_1Request { - public: - _1_1PostRequest(std::string const& host, std::string const& path = "", uint16_t const& port = 443) - : http::_1_1PostRequest(host, path, port) {} - virtual void send() KTHROW(mkn::ram::http::Exception) override; -}; -using Post = _1_1PostRequest; - -} // namespace https -} // namespace ram -} // namespace mkn -#endif //_MKN_RAM_INCLUDE_HTTPS_HPP_ diff --git a/os/win/inc/mkn/ram/tcp.hpp b/os/win/inc/mkn/ram/tcp.hpp deleted file mode 100644 index 87defda..0000000 --- a/os/win/inc/mkn/ram/tcp.hpp +++ /dev/null @@ -1,366 +0,0 @@ -/** -Copyright (c) 2024, Philip Deegan. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Philip Deegan nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#ifndef _MKN_RAM_TCP_HPP_ -#define _MKN_RAM_TCP_HPP_ - -#include "mkn/kul/tcp/def.hpp" - -#undef UNICODE -#define WIN32_LEAN_AND_MEAN - -#include -#include -#include -#include -#include - -#include -#include - -#include "mkn/kul/tcp.base.hpp" - -#pragma comment(lib, "Ws2_32.lib") -#pragma comment(lib, "Mswsock.lib") -#pragma comment(lib, "AdvApi32.lib") - -namespace mkn { -namespace ram { -namespace tcp { - -template -class Socket : public ASocket { - protected: - int iResult; - - WSADATA wsaData; - SOCKET ConnectSocket = INVALID_SOCKET; - struct addrinfo *result = NULL, *ptr = NULL, hints; - - public: - virtual ~Socket() { - if (this->open) close(); - } - virtual bool connect(std::string const& host, int16_t const& port) override { - KUL_DBG_FUNC_ENTER - if (!CONNECT(*this, host, port)) return false; - this->open = true; - return true; - } - virtual bool close() override { - KUL_DBG_FUNC_ENTER - bool o1 = this->open; - if (this->open) { - this->open = 0; - if (ConnectSocket == INVALID_SOCKET) - KERR << "Socket shutdown: Socket is invalid ignoring"; - else { - closesocket(ConnectSocket); - WSACleanup(); - } - } - return o1; - } - virtual size_t read(T* data, size_t const& len) { - bool more = false; - return read(data, len, more); - } - virtual size_t read(T* data, size_t const& len, bool& more) { - KUL_DBG_FUNC_ENTER - - int16_t d = recv(ConnectSocket, data, len, 0); - - return d; - } - virtual size_t write(T const* data, size_t const& len) override { - iResult = send(ConnectSocket, data, len, 0); - if (iResult == SOCKET_ERROR) KEXCEPTION("Socket send failed with error: ") << WSAGetLastError(); - return iResult; - } - - SOCKET socket() { return ConnectSocket; } - - protected: - static bool CONNECT(Socket& sck, std::string const& host, int16_t const& port) { - KUL_DBG_FUNC_ENTER - int16_t e = 0; - - // Initialize Winsock - int iResult = WSAStartup(MAKEWORD(2, 2), &sck.wsaData); - if (iResult != 0) { - KEXCEPTION("WSAStartup failed with error: ") << iResult; - return 1; - } - - ZeroMemory(&sck.hints, sizeof(sck.hints)); - sck.hints.ai_family = AF_UNSPEC; - sck.hints.ai_socktype = SOCK_STREAM; - sck.hints.ai_protocol = IPPROTO_TCP; - - // Resolve the server address and port - iResult = getaddrinfo(host.c_str(), std::to_string(port).c_str(), &sck.hints, &sck.result); - if (iResult != 0) KEXCEPTION("getaddrinfo failed with error: ") << iResult; - - // Attempt to connect to an address until one succeeds - for (sck.ptr = sck.result; sck.ptr != NULL; sck.ptr = sck.ptr->ai_next) { - // Create a SOCKET for connecting to server - sck.ConnectSocket = ::socket(sck.ptr->ai_family, sck.ptr->ai_socktype, sck.ptr->ai_protocol); - if (sck.ConnectSocket == INVALID_SOCKET) - KEXCEPTION("socket failed with error: ") << WSAGetLastError(); - - // Connect to server. - iResult = ::connect(sck.ConnectSocket, sck.ptr->ai_addr, (int)sck.ptr->ai_addrlen); - if (iResult == SOCKET_ERROR) { - closesocket(sck.ConnectSocket); - sck.ConnectSocket = INVALID_SOCKET; - continue; - } - break; - } - - freeaddrinfo(sck.result); - - if (sck.ConnectSocket == INVALID_SOCKET) KEXCEPTION("Unable to connect to server!"); - - return e >= 0; - } -}; - -template -class SocketServer : public ASocketServer { - protected: - bool s = 0; - int iResult; - int nfds = 1; - int64_t _started; - - WSADATA wsaData; - WSAPOLLFD m_fds[_MKN_RAM_TCP_MAX_CLIENT_]; - - SOCKET lisock = INVALID_SOCKET; - SOCKET ClientSocket = INVALID_SOCKET; - - struct addrinfo* result = NULL; - struct addrinfo hints; - - socklen_t clilen; - struct sockaddr_in serv_addr, cli_addr[_MKN_RAM_TCP_MAX_CLIENT_]; - - virtual bool handle(T* const in, size_t const& inLen, T* const out, size_t& outLen) { - return true; - } - - virtual int readFrom(int const& fd, T* in, int opts = 0) { - return ::recv(m_fds[fd].fd, in, _MKN_RAM_TCP_READ_BUFFER_ - 1, opts); - } - virtual int writeTo(int const& fd, T const* const out, size_t size) { - return ::send(m_fds[fd].fd, out, size, 0); - } - - virtual bool receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER - T in[_MKN_RAM_TCP_READ_BUFFER_]; - ZeroMemory(in, _MKN_RAM_TCP_READ_BUFFER_); - - bool cl; - do { - cl = 0; - iResult = recv(ClientSocket, in, _MKN_RAM_TCP_READ_BUFFER_ - 1, 0); - if (iResult > 0) { - T out[_MKN_RAM_TCP_READ_BUFFER_]; - ZeroMemory(out, _MKN_RAM_TCP_READ_BUFFER_); - size_t outLen; - cl = handle(in, iResult, out, outLen); - auto sent = writeTo(ClientSocket, out, strlen(out)); - if (sent == SOCKET_ERROR) - KEXCEPTION("SocketServer send failed with error: ") << WSAGetLastError(); - if (cl) break; - } else if (iResult == 0) { - // printf("Connection closing...\n"); - } else - KEXCEPTION("SocketServer recv failed with error: ") << WSAGetLastError(); - - } while (iResult > 0); - - return true; - } - - void closeFDsNoCompress(std::map& fds, std::vector& del) { - KUL_DBG_FUNC_ENTER; - for (auto const& fd : del) { - ::closesocket(m_fds[fd].fd); - m_fds[fd].fd = -1; - fds[fd] = 0; - nfds--; - } - } - virtual void closeFDs(std::map& fds, std::vector& del) { - closeFDsNoCompress(fds, del); - } - virtual void loop(std::map& fds) KTHROW(kul::tcp::Exception) { - auto ret = poll(); - if (!s) return; - if (ret < 0) - KEXCEPTION("Socket Server error on select: " + std::to_string(errno) + " - " + - std::string(strerror(errno))); - // if(ret == 0) return; - int newlisock = -1; - ; - for (auto const& pair : fds) { - auto& i = pair.first; - if (pair.second == 1) continue; - if (m_fds[i].revents == 0) continue; - if (m_fds[i].revents != POLLIN) - KEXCEPTION("HTTP Server error on pollin " + std::to_string(m_fds[i].revents)); - - if (m_fds[i].fd == lisock) { - do { - int newFD = nfds; - while (1) { - newFD++; - if (fds.count(newFD) && !fds[newFD]) break; - } - newlisock = accept(newFD); - if (newlisock < 0) { - if (errno != EWOULDBLOCK) KEXCEPTION("SockerServer error on accept"); - break; - } - validAccept(fds, newlisock, newFD); - } while (newlisock != -1); - } - } - std::vector del; - for (auto const& pair : fds) - if (pair.second == 1 && receive(fds, pair.first)) del.push_back(pair.first); - if (del.size()) closeFDs(fds, del); - } - - virtual SOCKET accept(int const& fd) { - return WSAAccept(lisock, (struct sockaddr*)&cli_addr[fd], &clilen, NULL, NULL); - } - virtual void validAccept(std::map& fds, int const& newlisock, int const& nfd) { - KUL_DBG_FUNC_ENTER; - KOUT(DBG) << "New connection , socket fd is " << newlisock - << ", is : " << inet_ntoa(cli_addr[nfd].sin_addr) - << ", port : " << ntohs(cli_addr[nfd].sin_port); - this->onConnect(inet_ntoa(cli_addr[nfd].sin_addr), ntohs(cli_addr[nfd].sin_port)); - m_fds[nfd].fd = newlisock; - m_fds[nfd].events = POLLIN; - fds[nfd] = 1; - nfds++; - } - - virtual int poll(int timeout = 10) { - auto p = WSAPoll(m_fds, nfds, timeout); - - return p; - } - - public: - SocketServer(uint16_t const& p, bool _bind = 1) : mkn::ram::tcp::ASocketServer(p) { - // if(_bind) bind(__MKN_RAM_TCP_BIND_SOCKTOPTS__); - } - void freeaddrinfo() { - if (!result) return; - ::freeaddrinfo(result); - result = nullptr; - } - ~SocketServer() { - freeaddrinfo(); - if (lisock) closesocket(lisock); - if (ClientSocket) closesocket(ClientSocket); - WSACleanup(); - } - virtual void bind(int sockOpt = __MKN_RAM_TCP_BIND_SOCKTOPTS__) KTHROW(kul::Exception) {} - virtual void start() KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER - _started = mkn::kul::Now::MILLIS(); - - iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (iResult != 0) KEXCEPTION("WSAStartup failed with error: ") << iResult; - - ZeroMemory(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - hints.ai_flags = AI_PASSIVE; - - iResult = getaddrinfo(NULL, std::to_string(port()).c_str(), &hints, &result); - if (iResult != 0) { - WSACleanup(); - KEXCEPTION("getaddrinfo failed with error: ") << iResult; - } - - lisock = socket(result->ai_family, result->ai_socktype, result->ai_protocol); - int iso = 1; - int rc = setsockopt(lisock, SOL_SOCKET, SO_REUSEADDR, (char*)&iso, sizeof(iso)); - if (lisock == INVALID_SOCKET) KEXCEPTION("socket failed with error: ") << WSAGetLastError(); - - { - unsigned long mode = 1; - ioctlsocket(lisock, FIONBIO, &mode); - } - // Setup the TCP listening socket - iResult = ::bind(lisock, result->ai_addr, (int)result->ai_addrlen); - if (iResult == SOCKET_ERROR) KEXCEPTION("bind failed with error: ") << WSAGetLastError(); - - freeaddrinfo(); - - iResult = listen(lisock, SOMAXCONN); - if (iResult == SOCKET_ERROR) KEXCEPTION("listen failed with error: ") << WSAGetLastError(); - - s = true; - std::map fds; - for (int i = 0; i < _MKN_RAM_TCP_MAX_CLIENT_; i++) fds.insert(std::make_pair(i, 0)); - try { - while (s) loop(fds); - } catch (mkn::ram::tcp::Exception const& e1) { - KERR << e1.stack(); - } catch (std::exception const& e1) { - KERR << e1.what(); - } catch (...) { - KERR << "Loop Exception caught"; - } - } - virtual void stop() { - KUL_DBG_FUNC_ENTER - s = 0; - ::closesocket(lisock); - lisock = 0; - ::closesocket(ClientSocket); - ClientSocket = 0; - } -}; - -} // namespace tcp -} // namespace ram -} // namespace mkn - -#endif //_MKN_RAM_TCP_HPP_ diff --git a/src/fcgi/server.cpp b/src/any/fcgi/server.cpp similarity index 97% rename from src/fcgi/server.cpp rename to src/any/fcgi/server.cpp index b70f3e6..4f3bcb6 100644 --- a/src/fcgi/server.cpp +++ b/src/any/fcgi/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -34,7 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/http.hpp" void mkn::ram::asio::fcgi::Server::start() KTHROW(Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; nfds = lisock + 1; _started = mkn::kul::Now::MILLIS(); @@ -53,7 +53,7 @@ void mkn::ram::asio::fcgi::Server::start() KTHROW(Exception) { } bool mkn::ram::asio::fcgi::Server::receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; uint8_t* in = getOrCreateBufferFor(fd); bzero(in, _MKN_RAM_TCP_READ_BUFFER_); int e = 0, read = readFrom(fd, in, MSG_DONTWAIT); @@ -73,7 +73,7 @@ bool mkn::ram::asio::fcgi::Server::receive(std::map& fds, int cons void mkn::ram::asio::fcgi::Server::write(std::map& fds, int const& fd, uint8_t const* out, size_t const size) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; writeTo(fd, out, size); receive(fds, fd); std::vector del{fd}; @@ -83,7 +83,7 @@ void mkn::ram::asio::fcgi::Server::write(std::map& fds, int const& void mkn::ram::asio::fcgi::Server::PARSE_FIRST(std::map& fds, uint8_t* const in, int const& inLen, int const& fd) KTHROW(kul::fcgi::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; if (inLen < 4) KEXCEPTION("FCGI cannot parse input too short"); @@ -108,7 +108,7 @@ void mkn::ram::asio::fcgi::Server::PARSE_FIRST(std::map& fds, uint void mkn::ram::asio::fcgi::Server::PARSE(std::map& fds, uint8_t* const in, int const& inLen, int const& fd, size_t pos) KTHROW(kul::fcgi::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; if ((inLen - pos) < 4) KEXCEPTION("FCGI cannot parse input too short"); @@ -137,7 +137,7 @@ void mkn::ram::asio::fcgi::Server::PARSE(std::map& fds, uint8_t* c } size_t mkn::ram::asio::fcgi::Server::FORM_RESPONSE(FCGI_Message const& msg, uint8_t* out) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; bzero(out, _MKN_RAM_TCP_READ_BUFFER_); size_t pos = 0; diff --git a/src/http/request.cpp b/src/any/http/request.cpp similarity index 97% rename from src/http/request.cpp rename to src/any/http/request.cpp index 8b72c32..1ef76e6 100644 --- a/src/http/request.cpp +++ b/src/any/http/request.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/http.hpp" void mkn::ram::http::A1_1Request::send() KTHROW(mkn::ram::http::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; std::stringstream ss; { mkn::ram::tcp::Socket sock; @@ -79,7 +79,7 @@ class RequestHeaders { }; std::string mkn::ram::http::_1_1GetRequest::toString() const { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; std::stringstream ss; ss << method() << " /" << _path; if (atts.size() > 0) ss << "?"; @@ -99,7 +99,7 @@ std::string mkn::ram::http::_1_1GetRequest::toString() const { } std::string mkn::ram::http::_1_1PostRequest::toString() const { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; std::stringstream ss; ss << method() << " /" << _path << " " << version(); ss << "\r\nHost: " << _host; diff --git a/src/http/response.cpp b/src/any/http/response.cpp similarity index 99% rename from src/http/response.cpp rename to src/any/http/response.cpp index 6fe6de8..a15e8a6 100644 --- a/src/http/response.cpp +++ b/src/any/http/response.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/src/http/server.cpp b/src/any/http/server.cpp similarity index 98% rename from src/http/server.cpp rename to src/any/http/server.cpp index db05b0c..2e7b3f1 100644 --- a/src/http/server.cpp +++ b/src/any/http/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -32,7 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. std::shared_ptr mkn::ram::http::AServer::handleRequest( int const& fd, std::string const& b, std::string& path) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; std::string a; std::shared_ptr req; { @@ -105,7 +105,7 @@ std::shared_ptr mkn::ram::http::AServer::handleRequ void mkn::ram::http::AServer::handleBuffer(std::map& fds, int const& fd, char* in, int const& read, int& e) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; in[read] = '\0'; std::string res; try { diff --git a/os/nixish/src/http/multi.cpp b/src/nixish/http/multi.cpp similarity index 96% rename from os/nixish/src/http/multi.cpp rename to src/nixish/http/multi.cpp index 0542b34..ad95289 100644 --- a/os/nixish/src/http/multi.cpp +++ b/src/nixish/http/multi.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/http.hpp" void mkn::ram::http::MultiServer::start() KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _started = mkn::kul::Now::MILLIS(); listen(lisock, 256); clilen = sizeof(cli_addr[0]); diff --git a/os/nixish/src/http/server.cpp b/src/nixish/http/server.cpp similarity index 97% rename from os/nixish/src/http/server.cpp rename to src/nixish/http/server.cpp index 29f2512..0aa6a2a 100644 --- a/os/nixish/src/http/server.cpp +++ b/src/nixish/http/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/http.hpp" bool mkn::ram::http::Server::receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; char* in = getOrCreateBufferFor(fd); bzero(in, _MKN_RAM_TCP_READ_BUFFER_); int e = 0, read = readFrom(fd, in); diff --git a/os/win/src/https/multi.cpp b/src/nixish/https/multi.cpp similarity index 97% rename from os/win/src/https/multi.cpp rename to src/nixish/https/multi.cpp index a66d1cc..9f0c7a2 100644 --- a/os/win/src/https/multi.cpp +++ b/src/nixish/https/multi.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -32,7 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/https.hpp" void mkn::ram::https::MultiServer::start() KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _started = mkn::kul::Now::MILLIS(); listen(lisock, 256); clilen = sizeof(cli_addr); diff --git a/os/nixish/src/https/request.cpp b/src/nixish/https/request.cpp similarity index 96% rename from os/nixish/src/https/request.cpp rename to src/nixish/https/request.cpp index d6588cd..85156ed 100644 --- a/os/nixish/src/https/request.cpp +++ b/src/nixish/https/request.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -33,7 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. void mkn::ram::https::Requester::send(std::string const& h, std::string const& req, uint16_t const& p, std::stringstream& ss, SSL* ssl) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; int sck = 0; if (!mkn::ram::tcp::Socket::SOCKET(sck, PF_INET, SOCK_STREAM, 0)) KEXCEPT(mkn::ram::http::Exception, "Error opening socket"); @@ -60,7 +60,7 @@ void mkn::ram::https::Requester::send(std::string const& h, std::string const& r } void mkn::ram::https::_1_1GetRequest::send() KTHROW(mkn::ram::http::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; try { std::stringstream ss; Requester::send(_host, toString(), _port, ss, ssl); @@ -75,7 +75,7 @@ void mkn::ram::https::_1_1GetRequest::send() KTHROW(mkn::ram::http::Exception) { } void mkn::ram::https::_1_1PostRequest::send() KTHROW(mkn::ram::http::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; try { std::stringstream ss; Requester::send(_host, toString(), _port, ss, ssl); diff --git a/os/nixish/src/https/server.cpp b/src/nixish/https/server.cpp similarity index 97% rename from os/nixish/src/https/server.cpp rename to src/nixish/https/server.cpp index 28b5c11..5113e19 100644 --- a/os/nixish/src/https/server.cpp +++ b/src/nixish/https/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -32,7 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/https.hpp" void mkn::ram::https::Server::loop(std::map& fds) KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; auto ret = poll(100); @@ -104,7 +104,7 @@ void mkn::ram::https::Server::setChain(mkn::kul::File const& f) { } mkn::ram::https::Server& mkn::ram::https::Server::init() { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; if (!crt) KEXCEPTION("HTTPS Server crt file does not exist: " + crt.full()); if (!key) KEXCEPTION("HTTPS Server key file does not exist: " + key.full()); SSL_library_init(); @@ -123,7 +123,7 @@ mkn::ram::https::Server& mkn::ram::https::Server::init() { } void mkn::ram::https::Server::stop() { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; s = 0; ERR_free_strings(); EVP_cleanup(); @@ -140,7 +140,7 @@ void mkn::ram::https::Server::stop() { void mkn::ram::https::Server::handleBuffer(std::map& fds, int const& fd, char* in, int const& read, int& e) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; in[read] = '\0'; std::string res; try { @@ -166,7 +166,7 @@ void mkn::ram::https::Server::handleBuffer(std::map& fds, int cons } bool mkn::ram::https::Server::receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; char* in = getOrCreateBufferFor(fd); bzero(in, _MKN_RAM_TCP_READ_BUFFER_); int e = 0, read = ::SSL_read(ssl_clients[m_fds[fd].fd], in, _MKN_RAM_TCP_READ_BUFFER_ - 1); diff --git a/os/win/src/http/multi.cpp b/src/win/http/multi.cpp similarity index 96% rename from os/win/src/http/multi.cpp rename to src/win/http/multi.cpp index 12f4753..b82642e 100644 --- a/os/win/src/http/multi.cpp +++ b/src/win/http/multi.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/http.hpp" void mkn::ram::http::MultiServer::start() KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _started = mkn::kul::Now::MILLIS(); for (size_t i = 0; i < _acceptThreads; i++) diff --git a/os/win/src/http/server.cpp b/src/win/http/server.cpp similarity index 97% rename from os/win/src/http/server.cpp rename to src/win/http/server.cpp index 48f36fd..ee15255 100644 --- a/os/win/src/http/server.cpp +++ b/src/win/http/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/http.hpp" bool mkn::ram::http::Server::receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER; + MKN_KUL_DBG_FUNC_ENTER; char* in = getOrCreateBufferFor(fd); ZeroMemory(in, _MKN_RAM_TCP_READ_BUFFER_); int e = 0, read = readFrom(fd, in); diff --git a/os/nixish/src/https/multi.cpp b/src/win/https/multi.cpp similarity index 97% rename from os/nixish/src/https/multi.cpp rename to src/win/https/multi.cpp index a66d1cc..9f0c7a2 100644 --- a/os/nixish/src/https/multi.cpp +++ b/src/win/https/multi.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -32,7 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/https.hpp" void mkn::ram::https::MultiServer::start() KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; _started = mkn::kul::Now::MILLIS(); listen(lisock, 256); clilen = sizeof(cli_addr); diff --git a/os/win/src/https/request.cpp b/src/win/https/request.cpp similarity index 96% rename from os/win/src/https/request.cpp rename to src/win/https/request.cpp index a10b422..31d76ee 100644 --- a/os/win/src/https/request.cpp +++ b/src/win/https/request.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -33,7 +33,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. void mkn::ram::https::Requester::send(std::string const& h, std::string const& req, uint16_t const& p, std::stringstream& ss, SSL* ssl) { - KUL_DBG_FUNC_ENTER { + MKN_KUL_DBG_FUNC_ENTER; + { mkn::ram::tcp::Socket sock; if (!sock.connect(h, p)) KEXCEPTION("TCP FAILED TO CONNECT!"); SOCKET sck = sock.socket(); @@ -60,7 +61,7 @@ void mkn::ram::https::Requester::send(std::string const& h, std::string const& r } void mkn::ram::https::_1_1GetRequest::send() KTHROW(mkn::ram::http::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; try { std::stringstream ss; Requester::send(_host, toString(), _port, ss, ssl); @@ -75,7 +76,7 @@ void mkn::ram::https::_1_1GetRequest::send() KTHROW(mkn::ram::http::Exception) { } void mkn::ram::https::_1_1PostRequest::send() KTHROW(mkn::ram::http::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; try { std::stringstream ss; Requester::send(_host, toString(), _port, ss, ssl); diff --git a/os/win/src/https/server.cpp b/src/win/https/server.cpp similarity index 98% rename from os/win/src/https/server.cpp rename to src/win/https/server.cpp index 477b51b..e55f210 100644 --- a/os/win/src/https/server.cpp +++ b/src/win/https/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -32,7 +32,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mkn/ram/https.hpp" void mkn::ram::https::Server::loop(std::map& fds) KTHROW(kul::tcp::Exception) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; auto ret = poll(1000); @@ -102,7 +102,7 @@ void mkn::ram::https::Server::setChain(mkn::kul::File const& f) { } mkn::ram::https::Server& mkn::ram::https::Server::init() { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; if (!crt) KEXCEPTION("HTTPS Server crt file does not exist: " + crt.full()); if (!key) KEXCEPTION("HTTPS Server key file does not exist: " + key.full()); SSL_library_init(); @@ -121,7 +121,7 @@ mkn::ram::https::Server& mkn::ram::https::Server::init() { } void mkn::ram::https::Server::stop() { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; s = 0; ERR_free_strings(); EVP_cleanup(); @@ -162,7 +162,7 @@ void mkn::ram::https::Server::handleBuffer(std::map& fds, int cons } bool mkn::ram::https::Server::receive(std::map& fds, int const& fd) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; char* in = getOrCreateBufferFor(fd); bzero(in, _MKN_RAM_TCP_READ_BUFFER_); int e = 0, read = ::SSL_read(ssl_clients[m_fds[fd].fd], in, _MKN_RAM_TCP_READ_BUFFER_ - 1); diff --git a/test/client.cpp b/tst/client.cpp similarity index 98% rename from test/client.cpp rename to tst/client.cpp index e46465a..c07ad06 100644 --- a/test/client.cpp +++ b/tst/client.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -61,7 +61,7 @@ class HTTPS_Post : public mkn::ram::https::_1_1PostRequest { HTTPS_Post(std::string const& host, std::string const& path = "", uint16_t const& port = 80) : mkn::ram::https::_1_1PostRequest(host, path, port) {} void handleResponse(mkn::kul::hash::map::S2S const& h, std::string const& b) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; for (auto const& p : h) KOUT(NON) << "HEADER: " << p.first << " : " << p.second; KOUT(NON) << "HTTPS POST RESPONSE:\n" << b; } diff --git a/test/fcgi.cpp b/tst/fcgi.cpp similarity index 98% rename from test/fcgi.cpp rename to tst/fcgi.cpp index 7263ae9..470dd1e 100644 --- a/test/fcgi.cpp +++ b/tst/fcgi.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/test/server.cpp b/tst/server.cpp similarity index 97% rename from test/server.cpp rename to tst/server.cpp index cb194be..f9cd64d 100644 --- a/test/server.cpp +++ b/tst/server.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -60,7 +60,7 @@ class TestHTTPServer : public mkn::ram::http::Server { public: mkn::ram::http::_1_1Response respond(mkn::ram::http::A1_1Request const& req) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; mkn::ram::http::_1_1Response r; r.body("HTTP PROVIDED BY KUL"); addResponseHeaders(r); @@ -76,7 +76,7 @@ class TestMultiHTTPServer : public mkn::ram::http::MultiServer { public: mkn::ram::http::_1_1Response respond(mkn::ram::http::A1_1Request const& req) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; mkn::ram::http::_1_1Response r; r.body("MULTI HTTP PROVIDED BY KUL"); addResponseHeaders(r); @@ -95,7 +95,7 @@ class TestHTTPSServer : public mkn::ram::https::Server { public: mkn::ram::http::_1_1Response respond(mkn::ram::http::A1_1Request const& req) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; mkn::ram::http::_1_1Response r; r.body("HTTPS PROVIDED BY KUL: " + req.method()); addResponseHeaders(r); @@ -113,7 +113,7 @@ class TestMultiHTTPSServer : public mkn::ram::https::MultiServer { public: mkn::ram::http::_1_1Response respond(mkn::ram::http::A1_1Request const& req) { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; mkn::ram::http::_1_1Response r; r.body("MULTI HTTPS PROVIDED BY KUL"); addResponseHeaders(r); @@ -139,7 +139,7 @@ class HTTPS_Post : public mkn::ram::https::_1_1PostRequest { HTTPS_Post(std::string const& host, std::string const& path = "", uint16_t const& port = 80) : mkn::ram::https::_1_1PostRequest(host, path, port) {} void handleResponse(mkn::kul::hash::map::S2S const& h, std::string const& b) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; for (auto const& p : h) KOUT(NON) << "HEADER: " << p.first << " : " << p.second; KOUT(NON) << "HTTPS POST RESPONSE:\n" << b; } @@ -152,7 +152,7 @@ class TestSocketServer : public mkn::ram::tcp::SocketServer { public: bool handle(char* in, char* out) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; mkn::ram::http::_1_1Response getResponse; addResponseHeaders(getResponse); getResponse.body("magicmansion"); @@ -177,7 +177,7 @@ class Post : public mkn::ram::http::_1_1PostRequest { Post(std::string const& host, std::string const& path = "", uint16_t const& port = 80) : mkn::ram::http::_1_1PostRequest(host, path, port) {} void handleResponse(mkn::kul::hash::map::S2S const& h, std::string const& b) override { - KUL_DBG_FUNC_ENTER + MKN_KUL_DBG_FUNC_ENTER; for (auto const& p : h) KOUT(NON) << "HEADER: " << p.first << " : " << p.second; KOUT(NON) << "POST RESPONSE:\n" << b; } diff --git a/test/test.cpp b/tst/test.cpp similarity index 98% rename from test/test.cpp rename to tst/test.cpp index 761dac2..e470fd5 100644 --- a/test/test.cpp +++ b/tst/test.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/test/usage.cpp b/tst/usage.cpp similarity index 99% rename from test/usage.cpp rename to tst/usage.cpp index f1d2829..cc3bb18 100644 --- a/test/usage.cpp +++ b/tst/usage.cpp @@ -1,5 +1,5 @@ /** -Copyright (c) 2024, Philip Deegan. +Copyright (c) 2026, Philip Deegan. All rights reserved. Redistribution and use in source and binary forms, with or without