From 0fbc5bad52a1ecb17390ba26770b194a5e1a93a0 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 2 Nov 2025 15:00:53 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 + LICENSE | 65 ++++++ README.md | 3 + .../Crafter.Network-ClientHTTP.cpp | 187 +++++++++++++++ implementations/Crafter.Network-ClientTCP.cpp | 161 +++++++++++++ .../Crafter.Network-ListenerHTTP.cpp | 217 ++++++++++++++++++ .../Crafter.Network-ListenerTCP.cpp | 131 +++++++++++ interfaces/Crafter.Network-ClientHTTP.cppm | 36 +++ interfaces/Crafter.Network-ClientTCP.cppm | 45 ++++ interfaces/Crafter.Network-HTTP.cppm | 84 +++++++ interfaces/Crafter.Network-ListenerHTTP.cppm | 41 ++++ interfaces/Crafter.Network-ListenerTCP.cppm | 43 ++++ interfaces/Crafter.Network.cppm | 27 +++ project.json | 79 +++++++ tests/ShouldCompile.cpp | 29 +++ tests/ShouldRecieveHTTP.cpp | 53 +++++ tests/ShouldSendHTTP.cpp | 34 +++ tests/ShouldSendRecieveHTTP.cpp | 52 +++++ 18 files changed, 1289 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 implementations/Crafter.Network-ClientHTTP.cpp create mode 100755 implementations/Crafter.Network-ClientTCP.cpp create mode 100644 implementations/Crafter.Network-ListenerHTTP.cpp create mode 100755 implementations/Crafter.Network-ListenerTCP.cpp create mode 100644 interfaces/Crafter.Network-ClientHTTP.cppm create mode 100755 interfaces/Crafter.Network-ClientTCP.cppm create mode 100644 interfaces/Crafter.Network-HTTP.cppm create mode 100644 interfaces/Crafter.Network-ListenerHTTP.cppm create mode 100755 interfaces/Crafter.Network-ListenerTCP.cppm create mode 100755 interfaces/Crafter.Network.cppm create mode 100644 project.json create mode 100644 tests/ShouldCompile.cpp create mode 100644 tests/ShouldRecieveHTTP.cpp create mode 100644 tests/ShouldSendHTTP.cpp create mode 100644 tests/ShouldSendRecieveHTTP.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e9c87a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +bin/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..79b8aa7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,65 @@ +GNU LESSER GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright © 2007 Free Software Foundation, Inc. + +Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + +This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. +0. Additional Definitions. + +As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License. + +“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. + +An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. + +A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”. + +The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. + +The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. +1. Exception to Section 3 of the GNU GPL. + +You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. +2. Conveying Modified Versions. + +If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: + + a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or + b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. + +3. Object Code Incorporating Material from Library Header Files. + +The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. + b) Accompany the object code with a copy of the GNU GPL and this license document. + +4. Combined Works. + +You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: + + a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. + b) Accompany the Combined Work with a copy of the GNU GPL and this license document. + c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. + d) Do one of the following: + 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. + 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. + e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) + +5. Combined Libraries. + +You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. + b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. + +6. Revised Versions of the GNU Lesser General Public License. + +The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. + +If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab00155 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# About + +C++ networking library \ No newline at end of file diff --git a/implementations/Crafter.Network-ClientHTTP.cpp b/implementations/Crafter.Network-ClientHTTP.cpp new file mode 100644 index 0000000..f23dbba --- /dev/null +++ b/implementations/Crafter.Network-ClientHTTP.cpp @@ -0,0 +1,187 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module; + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +module Crafter.Network:ClientHTTP_impl; +import :ClientHTTP; +import Crafter.Thread; +import std; + +using namespace Crafter; + +ClientHTTP::ClientHTTP(const char* host, std::uint16_t port): client(host, port) { + +} + +ClientHTTP::ClientHTTP(std::string host, std::uint16_t port): ClientHTTP(host.c_str(), port) { + +} + +HTTPResponse ClientHTTP::Send(const char* request, std::uint32_t length) const { + client.Send(request, length); + std::vector buffer = client.RecieveSync(1024); + HTTPResponse response; + std::uint32_t i = 0; + std::uint32_t statusStart = 0; + for(; i < 1024; i++) { + if(buffer[i] == ' ') { + statusStart = i; + break; + } + } + for(; i < 1024; i++) { + if(buffer[i] == '\r') { + response.status.assign(buffer.data()+statusStart+1, i-statusStart-1); + break; + } + } + i+=2; + while(i < 1024) { + std::uint32_t headerStart = i; + std::string headerName; + for(; i < 1024; i++) { + if(buffer[i] == ':') { + headerName.assign(buffer.data()+headerStart, i-headerStart); + i+=2; + break; + } + } + headerStart = i; + std::string headerValue; + for(; i < 1024; i++) { + if(buffer[i] == '\r' && buffer[i+1] == '\n') { + headerValue.assign(buffer.data()+headerStart, i-headerStart); + response.headers.insert({headerName, headerValue}); + if(buffer[i+2] == '\r'){ + goto headersComplete; + } else{ + i+=2; + break; + } + } + } + } + headersComplete:; + i+=4; + std::unordered_map::iterator it = response.headers.find("Content-Length"); + if(it != response.headers.end()) + { + const int lenght = std::stoi(it->second); + response.body.resize(lenght, 0); + if(i < buffer.size()){ + std::memcpy(&response.body[0], buffer.data()+i, buffer.size()-i); + } + const int remaining = lenght-(buffer.size()-i); + if(remaining > 0){ + int index = buffer.size()-i; + while(index < lenght) { + std::vector bodyBuffer = client.RecieveSync(remaining); + std::memcpy(&response.body[index], bodyBuffer.data(), bodyBuffer.size()); + index+=bodyBuffer.size(); + } + } + } else { + std::unordered_map::iterator it = response.headers.find("Transfer-Encoding"); + if(it != response.headers.end() && it->second == "chunked") { + while(i < buffer.size()){ + std::string lenght; + int lenghtStart = i; + for(; i < buffer.size(); i++) { + if(buffer[i] == '\r') { + lenght.assign(buffer.data()+lenghtStart, i-lenghtStart); + break; + } + } + i+=2; + std::cout << lenght << "bro" << std::endl; + int lenghtInt = stoi(lenght, 0, 8); + if(lenghtInt != 0){ + int oldSize = response.body.size(); + response.body.resize(oldSize+lenghtInt, 0); + if(buffer.size() < lenghtInt) { + std::memcpy(&response.body[oldSize], buffer.data()+i, buffer.size()-i); + std::vector bodyBuffer2 = client.RecieveUntilFullSync(lenghtInt-buffer.size()); + std::memcpy(&response.body[oldSize+(buffer.size()-i)], buffer.data(), buffer.size()); + } else { + std::memcpy(&response.body[oldSize], buffer.data()+i, lenghtInt); + i+=lenghtInt; + } + } else{ + goto bodyFinished; + } + } + std::cout << response.body << std::endl; + while(true) { + std::vector bodyBuffer = client.RecieveSync(); + int i2 = 0; + while(i2 < bodyBuffer.size()){ + std::string lenght; + int lenghtStart = i2; + for(; i2 < bodyBuffer.size(); i2++) { + if(buffer[i2] == '\r') { + lenght.assign(bodyBuffer.data()+lenghtStart, i2-lenghtStart); + break; + } + } + i2+=2; + int lenghtInt = stoi(lenght, 0, 8); + std::cout < bodyBuffer2 = client.RecieveUntilFullSync(lenghtInt-bodyBuffer.size()); + std::memcpy(&response.body[oldSize+(bodyBuffer.size()-i2)], bodyBuffer2.data(), bodyBuffer2.size()); + } else { + std::memcpy(&response.body[oldSize], bodyBuffer.data()+i2, lenghtInt); + i2+=lenghtInt; + } + } else{ + goto bodyFinished; + } + } + } + bodyFinished:; + } else { + std::vector bodyBuffer = client.RecieveUntilCloseSync(); + response.body.resize((buffer.size()-i)+(bodyBuffer.size()), 0); + if(i < buffer.size()){ + std::memcpy(&response.body[0], buffer.data()+i, buffer.size()-i); + } + std::memcpy(&response.body[buffer.size()-i], bodyBuffer.data(), bodyBuffer.size()); + } + } + return response; +} +HTTPResponse ClientHTTP::Send(std::string request) const { + return Send(request.c_str(), request.size()); +} diff --git a/implementations/Crafter.Network-ClientTCP.cpp b/implementations/Crafter.Network-ClientTCP.cpp new file mode 100755 index 0000000..0ee81dd --- /dev/null +++ b/implementations/Crafter.Network-ClientTCP.cpp @@ -0,0 +1,161 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +module Crafter.Network:ClientTCP_impl; +import :ClientTCP; +import Crafter.Thread; +import std; + +using namespace Crafter; + +ClientTCP::ClientTCP(int socketid) : socketid(socketid) +{ + +} + +ClientTCP::ClientTCP(const char* hostName, std::uint16_t port) +{ + hostent *host; + sockaddr_in serv_addr; + + host=gethostbyname(hostName); + if((socketid = socket(AF_INET, SOCK_STREAM, 0)) == -1){ + std::cerr << "Could not open socket" << std::endl; + } + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + serv_addr.sin_addr = *((struct in_addr *)host->h_addr); + bzero(&(serv_addr.sin_zero),8); + + if(connect(socketid,(sockaddr*)&serv_addr, sizeof(sockaddr)) == -1){ + std::cerr << "Could not connect to server" << std::endl; + } +} + +ClientTCP::ClientTCP(std::string hostName, std::uint16_t port): ClientTCP(hostName.c_str(), port) +{ + +} + +ClientTCP::~ClientTCP() +{ + close(socketid); +} + +void ClientTCP::Send(const void* buffer, std::uint32_t size) const { + send(socketid, reinterpret_cast(buffer), size, 0); +} + +std::vector ClientTCP::RecieveSync(std::uint32_t bufferSize) const { + std::vector totalBuffer(bufferSize); + int read = recv(socketid, totalBuffer.data(), bufferSize, 0); + if(read < bufferSize){ + totalBuffer.resize(read); + } + return totalBuffer; +} +int ClientTCP::RecieveSync(std::uint32_t bufferSize, void* buffer) const { + return recv(socketid, reinterpret_cast(buffer), bufferSize, 0); +} + +std::vector ClientTCP::RecieveSync() const { + int count; + ioctl(socketid, FIONREAD, &count); + std::vector buffer(count); + recv(socketid, buffer.data(), count, 0); + return buffer; +} + +std::vector ClientTCP::RecieveUntilCloseSync() const { + int count; + ioctl(socketid, FIONREAD, &count); + std::vector buffer(count); + recv(socketid, buffer.data(), count, 0); + while(true) { + ioctl(socketid, FIONREAD, &count); + unsigned int oldSize = buffer.size(); + buffer.resize(buffer.size()+count); + if(recv(socketid, buffer.data()+oldSize, count, 0) == -1) { + break; + } + } + return buffer; +} + +std::vector ClientTCP::RecieveUntilFullSync(std::uint32_t bufferSize) const { + std::vector buffer(bufferSize); + int read = 0; + while(read < bufferSize) { + int newRead = recv(socketid, buffer.data()+read, bufferSize-read, 0); + if(newRead == -1) { + break; + } else{ + read+=newRead; + } + } + buffer.resize(read); + return buffer; +} + +void ClientTCP::RecieveAsync(std::uint32_t bufferSize, std::function)> recieveCallback) const { + ThreadPool::Enqueue([recieveCallback, this, bufferSize](){ + recieveCallback(this->RecieveSync(bufferSize)); + }); +} + +void ClientTCP::RecieveAsync(std::uint32_t bufferSize, std::function recieveCallback, char* buffer) const { + ThreadPool::Enqueue([recieveCallback, this, bufferSize, buffer](){ + recieveCallback(this->RecieveSync(bufferSize, buffer)); + }); +} + +void ClientTCP::RecieveUntilFullAsync(std::uint32_t bufferSize, std::function)> recieveCallback) const { + ThreadPool::Enqueue([recieveCallback, this, bufferSize](){ + recieveCallback(this->RecieveUntilFullSync(bufferSize)); + }); +} + +void ClientTCP::RecieveAsync(std::function)> recieveCallback) const { + ThreadPool::Enqueue([this, recieveCallback](){ + recieveCallback(this->RecieveSync()); + }); +} + +void ClientTCP::RecieveUntilCloseAsync(std::function)> recieveCallback) const { + ThreadPool::Enqueue([this, recieveCallback](){ + recieveCallback(this->RecieveUntilCloseSync()); + }); +} diff --git a/implementations/Crafter.Network-ListenerHTTP.cpp b/implementations/Crafter.Network-ListenerHTTP.cpp new file mode 100644 index 0000000..3ad163e --- /dev/null +++ b/implementations/Crafter.Network-ListenerHTTP.cpp @@ -0,0 +1,217 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +module Crafter.Network:ListenerHTTP_impl; +import :ListenerHTTP; +import :ClientTCP; +import std; +import Crafter.Thread; + +using namespace Crafter; + +ListenerHTTP::ListenerHTTP(std::uint16_t port, std::unordered_map> routes): routes(routes) { + sockaddr_in servAddr; + bzero((char*)&servAddr, sizeof(servAddr)); + servAddr.sin_family = AF_INET; + servAddr.sin_addr.s_addr = htonl(INADDR_ANY); + servAddr.sin_port = htons(port); + + s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) { + throw std::runtime_error("Error establishing the server socket"); + } + + int opt = 1; + if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { + throw std::runtime_error("Error setting SO_REUSEADDR"); + } + + int bindStatus = bind(s, (struct sockaddr*)&servAddr, sizeof(servAddr)); + if (bindStatus < 0) { + throw std::runtime_error(std::format("Error binding the server socket: {}", std::strerror(errno))); + } + + if (listen(s, 5) < 0) { + throw std::runtime_error("Error starting to listen on the server socket"); + } +} + +void ListenerHTTP::Stop() { + running = false; + shutdown(s, SHUT_RDWR); + close(s); + s = -1; +} + +void ListenerHTTP::ListenSyncSync() const { + while(running) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + CallRoute(client); + } else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } +} + +void ListenerHTTP::ListenSyncAsync() const { + while(running) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + ThreadPool::Enqueue([this, client]() {CallRoute(client); }); + } + else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } +} + +void ListenerHTTP::ListenAsyncSync() const { + ThreadPool::Enqueue([this]() { + while(running) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + CallRoute(client); + } + else if(running) { + std::cerr << "Error accepting request from client: "<< strerror(errno) << " (errno: " << errno << ")"<< std::endl; + } + } + }); +} + +void ListenerHTTP::ListenAsyncAsync() const { + ThreadPool::Enqueue([this]() { + while(running) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + ThreadPool::Enqueue([this, client]() {CallRoute(client); }); + } + else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } + }); +} + +void ListenerHTTP::CallRoute(int clientid) const { + ClientTCP client(clientid); + std::vector buffer = client.RecieveSync(1024); + HTTPRequest request; + std::string route; + std::uint32_t i = 0; + std::uint32_t routeStart = 0; + for(; i < 1024; i++) { + if(buffer[i] == ' ') { + request.method.assign(buffer.data(), i); + break; + } + } + for(; i < 1024; i++) { + if(buffer[i] == '/') { + routeStart = i; + break; + } + } + for(; i < 1024; i++) { + if(buffer[i] == ' ') { + route.assign(buffer.data()+routeStart, i-routeStart); + break; + } + } + for(; i < 1024; i++) { + if(buffer[i] == '\r' && buffer[i+1] == '\n') { + break; + } + } + i+=2; + while(i < 1024) { + std::uint32_t headerStart = i; + std::string headerName; + for(; i < 1024-i; i++) { + if(buffer[i] == ':') { + headerName.assign(buffer.data()+headerStart, i-headerStart); + i++; + break; + } + } + headerStart = i; + std::string headerValue; + for(; i < 1024; i++) { + if(buffer[i] == '\r' && buffer[i+1] == '\n') { + headerValue.assign(buffer.data()+headerStart, i-headerStart); + request.headers.insert({headerName, headerValue}); + if(buffer[i+2] == '\r'){ + goto headersComplete; + } else{ + i+=2; + break; + } + } + } + } + headersComplete:; + i+=4; + std::unordered_map::iterator it = request.headers.find("Content-Length"); + if(it != request.headers.end()) + { + const int lenght = std::stoi(it->second); + const int remaining = 1024-lenght-i; + request.body.resize(lenght, 0); + if(remaining > 0){ + std::memcpy(&request.body[0], buffer.data()+i, lenght); + } + if(remaining < lenght){ + std::vector bodyBuffer = client.RecieveSync(lenght-remaining); + std::memcpy(&request.body[remaining], bodyBuffer.data(), lenght-remaining); + } + } + std::string response = routes.at(route)(request); + client.Send(&response[0], response.size()); +} + +ListenerHTTP::~ListenerHTTP() { + if(s != -1) { + close(s); + } +} diff --git a/implementations/Crafter.Network-ListenerTCP.cpp b/implementations/Crafter.Network-ListenerTCP.cpp new file mode 100755 index 0000000..00b051b --- /dev/null +++ b/implementations/Crafter.Network-ListenerTCP.cpp @@ -0,0 +1,131 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +module; + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +module Crafter.Network:ListenerTCP_impl; +import :ListenerTCP; +import std; +import Crafter.Thread; + +using namespace Crafter; + +ListenerTCP::ListenerTCP(std::uint16_t port, std::function connectCallback, std::uint32_t concurrentClientLimit, std::uint32_t totalClientLimit) : connectCallback(connectCallback), concurrentClientLimit(concurrentClientLimit), totalClientLimit(totalClientLimit) { + sockaddr_in servAddr; + bzero((char*)&servAddr, sizeof(servAddr)); + servAddr.sin_family = AF_INET; + servAddr.sin_addr.s_addr = htonl(INADDR_ANY); + servAddr.sin_port = htons(port); + + s = socket(AF_INET, SOCK_STREAM, 0); + if(s < 0) + { + throw std::runtime_error("Error establishing the server socket"); + } + int bindStatus = bind(s, (struct sockaddr*) &servAddr, sizeof(servAddr)); + listen(s, 5); +} + +void ListenerTCP::Stop() { + running = false; + shutdown(s, SHUT_RDWR); + close(s); + s = -1; +} + +void ListenerTCP::ListenSyncSync() { + while (running && totalClientCounter < totalClientLimit) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + connectCallback(new ClientTCP(client)); + this->totalClientCounter++; + } + else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } +} + +void ListenerTCP::ListenSyncAsync() { + while (running && totalClientCounter < totalClientLimit) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + ThreadPool::Enqueue([this, client]() {connectCallback(new ClientTCP(client)); }); + this->totalClientCounter++; + } + else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } +} + +void ListenerTCP::ListenAsyncSync() { + ThreadPool::Enqueue([this]() { + while (running && totalClientCounter < totalClientLimit) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + connectCallback(new ClientTCP(client)); + this->totalClientCounter++; + } + else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } + }); +} + +void ListenerTCP::ListenAsyncAsync() { + ThreadPool::Enqueue([this]() { + while (running && totalClientCounter < totalClientLimit) { + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + int client = accept(s, (sockaddr*)&newSockAddr, &newSockAddrSize); + if (client > 0) { + ThreadPool::Enqueue([this, client]() {connectCallback(new ClientTCP(client)); }); + this->totalClientCounter++; + } + else { + std::cerr << "Error accepting request from client!" << std::endl; + } + } + }); +} + +ListenerTCP::~ListenerTCP() { + if(s != -1) { + close(s); + } +} \ No newline at end of file diff --git a/interfaces/Crafter.Network-ClientHTTP.cppm b/interfaces/Crafter.Network-ClientHTTP.cppm new file mode 100644 index 0000000..32e89cd --- /dev/null +++ b/interfaces/Crafter.Network-ClientHTTP.cppm @@ -0,0 +1,36 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Network:ClientHTTP; +import std; +import :ClientTCP; +import :HTTP; + +namespace Crafter { + export class ClientHTTP { + public: + ClientHTTP(const char* host, std::uint16_t port); + ClientHTTP(std::string host, std::uint16_t port); + HTTPResponse Send(const char* request, std::uint32_t length) const; + HTTPResponse Send(std::string request) const; + private: + ClientTCP client; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Network-ClientTCP.cppm b/interfaces/Crafter.Network-ClientTCP.cppm new file mode 100755 index 0000000..6e7a4e0 --- /dev/null +++ b/interfaces/Crafter.Network-ClientTCP.cppm @@ -0,0 +1,45 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Network:ClientTCP; +import std; + +namespace Crafter { + export class ClientTCP { + public: + ClientTCP(int socket); + ClientTCP(const char* host, std::uint16_t port); + ClientTCP(std::string host, std::uint16_t port); + ~ClientTCP(); + void Send(const void* buffer, std::uint32_t size) const; + std::vector RecieveSync() const; + std::vector RecieveUntilCloseSync() const; + std::vector RecieveUntilFullSync(std::uint32_t bufferSize) const; + std::vector RecieveSync(std::uint32_t bufferSize) const; + int RecieveSync(std::uint32_t bufferSize, void* buffer) const; + void RecieveAsync(std::function)> recieveCallback) const; + void RecieveUntilCloseAsync(std::function)> recieveCallback) const; + void RecieveAsync(std::uint32_t bufferSize, std::function)> recieveCallback) const; + void RecieveUntilFullAsync(std::uint32_t bufferSize, std::function)> recieveCallback) const; + void RecieveAsync(std::uint32_t bufferSize, std::function recieveCallback, char* buffer) const; + private: + int socketid; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Network-HTTP.cppm b/interfaces/Crafter.Network-HTTP.cppm new file mode 100644 index 0000000..6e3b324 --- /dev/null +++ b/interfaces/Crafter.Network-HTTP.cppm @@ -0,0 +1,84 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Network:HTTP; +import std; + +namespace Crafter { + export struct HTTPRequest { + std::string method; + std::unordered_map headers; + std::string body; + }; + + export struct HTTPResponse { + std::string status; + std::unordered_map headers; + std::string body; + }; + + export constexpr std::string CreateResponseHTTP(std::string status) { + return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n\r\n", status); + } + + export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map headers) { + std::string headersString; + for (auto const& [key, val] : headers) { + headersString+=std::format("{}: {}\r\n", key, val); + } + return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n{}\r\n", status, headersString); + } + + export constexpr std::string CreateResponseHTTP(std::string status, std::string body) { + return std::format("HTTP/1.1 {}\r\nContent-Length: {}\r\nConnection: keep-alive\r\n\r\n{}", status, body.size(), body); + } + + export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map headers, std::string body) { + std::string headersString; + for (auto const& [key, val] : headers) { + headersString+=std::format("{}: {}\r\n", key, val); + } + return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\nContent-Length: {}\r\n{}\r\n{}", status, body.size(), headersString, body); + } + + export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host) { + return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nHost: {}\r\n\r\n", method, route, host); + } + + export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::unordered_map headers, std::string host) { + std::string headersString; + for (auto const& [key, val] : headers) { + headersString+=std::format("{}: {}\r\n", key, val); + } + return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\nAccept-Encoding: identity\r\nHost: {}\r\n{}\r\n", method, route, host, headersString); + } + + export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::string body) { + return std::format("{} {} HTTP/1.1\r\nContent-Length: {}\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nHost: {}\r\n\r\n{}", method, route, body.size(), host, body); + } + + export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::unordered_map headers, std::string body) { + std::string headersString; + for (auto const& [key, val] : headers) { + headersString+=std::format("{}: {}\r\n", key, val); + } + return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: {}\r\nHost: {}\r\nAccept-Encoding: identity\r\n{}\r\n{}", method, route, body.size(), host, headersString, body); + } +} \ No newline at end of file diff --git a/interfaces/Crafter.Network-ListenerHTTP.cppm b/interfaces/Crafter.Network-ListenerHTTP.cppm new file mode 100644 index 0000000..a95822e --- /dev/null +++ b/interfaces/Crafter.Network-ListenerHTTP.cppm @@ -0,0 +1,41 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Network:ListenerHTTP; +import std; +import :HTTP; + +namespace Crafter { + export class ListenerHTTP { + public: + bool running = true; + std::unordered_map> routes; + ListenerHTTP(std::uint16_t port, std::unordered_map> routes); + ~ListenerHTTP(); + void Stop(); + void ListenSyncSync() const; + void ListenSyncAsync() const; + void ListenAsyncSync() const; + void ListenAsyncAsync() const; + private: + void CallRoute(int client) const; + int s; + }; +} diff --git a/interfaces/Crafter.Network-ListenerTCP.cppm b/interfaces/Crafter.Network-ListenerTCP.cppm new file mode 100755 index 0000000..e8f3828 --- /dev/null +++ b/interfaces/Crafter.Network-ListenerTCP.cppm @@ -0,0 +1,43 @@ +/* +Crafter®.Network +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Network:ListenerTCP; +import std; +import :ClientTCP; + +namespace Crafter { + export class ListenerTCP { + public: + bool running = true; + std::function connectCallback; + std::uint32_t concurrentClientLimit; + std::uint32_t totalClientLimit; + ListenerTCP(std::uint16_t port, std::function connectCallback, std::uint32_t concurrentClientLimit = std::numeric_limits::max(), std::uint32_t totalClientLimit = std::numeric_limits::max()); + ~ListenerTCP(); + void Stop(); + void ListenSyncSync(); + void ListenSyncAsync(); + void ListenAsyncSync(); + void ListenAsyncAsync(); + private: + std::uint32_t totalClientCounter = 0; + int s; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Network.cppm b/interfaces/Crafter.Network.cppm new file mode 100755 index 0000000..2648a10 --- /dev/null +++ b/interfaces/Crafter.Network.cppm @@ -0,0 +1,27 @@ +/* +Crafter®.Thread +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 3.0 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +export module Crafter.Network; + +export import :ClientTCP; +export import :ListenerTCP; +export import :ClientHTTP; +export import :ListenerHTTP; +export import :HTTP; \ No newline at end of file diff --git a/project.json b/project.json new file mode 100644 index 0000000..b598976 --- /dev/null +++ b/project.json @@ -0,0 +1,79 @@ +{ + "name": "crafter-network", + "configurations": [ + { + "name": "base", + "interfaces": ["interfaces/Crafter.Network-ClientTCP", "interfaces/Crafter.Network-ListenerTCP", "interfaces/Crafter.Network", "interfaces/Crafter.Network-ListenerHTTP", "interfaces/Crafter.Network-ClientHTTP", "interfaces/Crafter.Network-HTTP"], + "implementations": ["implementations/Crafter.Network-ClientTCP", "implementations/Crafter.Network-ListenerTCP", "implementations/Crafter.Network-ListenerHTTP", "implementations/Crafter.Network-ClientHTTP"] + }, + { + "name": "lib", + "extends": ["base"], + "type":"library", + "dependencies": [ + { + "path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Thread.git", + "configuration":"lib" + } + ] + }, + { + "name": "lib-debug", + "extends": ["lib"], + "debug": true + }, + { + "name": "lib-shared", + "extends": ["base"], + "type":"shared-library", + "dependencies": [ + { + "path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Thread.git", + "configuration":"lib-shared" + } + ] + } + ], + "tests":[ + { + "name": "should-compile", + "implementations": ["tests/ShouldCompile"], + "dependencies": [ + { + "path":"./project.json", + "configuration":"lib-shared" + } + ] + }, + { + "name": "should-recieve-http", + "implementations": ["tests/ShouldRecieveHTTP"], + "dependencies": [ + { + "path":"./project.json", + "configuration":"lib-shared" + } + ] + }, + { + "name": "should-send-http", + "implementations": ["tests/ShouldSendHTTP"], + "dependencies": [ + { + "path":"./project.json", + "configuration":"lib-shared" + } + ] + }, + { + "name": "should-send-recieve-http", + "implementations": ["tests/ShouldSendRecieveHTTP"], + "dependencies": [ + { + "path":"./project.json", + "configuration":"lib-shared" + } + ] + } + ] +} diff --git a/tests/ShouldCompile.cpp b/tests/ShouldCompile.cpp new file mode 100644 index 0000000..5da2770 --- /dev/null +++ b/tests/ShouldCompile.cpp @@ -0,0 +1,29 @@ +/* +Crafter® Build +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +import Crafter.Network; +import std; +using namespace Crafter; + +extern "C" { + std::string* RunTest() { + return nullptr; + } +} + + diff --git a/tests/ShouldRecieveHTTP.cpp b/tests/ShouldRecieveHTTP.cpp new file mode 100644 index 0000000..5bab4b8 --- /dev/null +++ b/tests/ShouldRecieveHTTP.cpp @@ -0,0 +1,53 @@ +/* +Crafter® Build +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include +#include +import Crafter.Thread; +import Crafter.Network; +import std; +using namespace Crafter; + +extern "C" { + std::string* RunTest() { + bool success = false; + ThreadPool::Start(2); + ListenerHTTP listener(8080, {{"/", [&](const HTTPRequest& request) { + success = true; + return CreateResponseHTTP("200 OK", "Hello World!"); + }}}); + try { + listener.ListenAsyncSync(); + system("curl http://localhost:8080 > /dev/null 2>&1"); + + if (success) { + listener.Stop(); + ThreadPool::Stop(); + return nullptr; + } else { + listener.Stop(); + ThreadPool::Stop(); + return new std::string("Did not receive"); + } + } catch(std::exception& e) { + listener.Stop(); + ThreadPool::Stop(); + return new std::string(e.what()); + } + } +} \ No newline at end of file diff --git a/tests/ShouldSendHTTP.cpp b/tests/ShouldSendHTTP.cpp new file mode 100644 index 0000000..1395fb8 --- /dev/null +++ b/tests/ShouldSendHTTP.cpp @@ -0,0 +1,34 @@ +/* +Crafter® Build +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +import Crafter.Thread; +import Crafter.Network; +import std; +using namespace Crafter; + +extern "C" { + std::string* RunTest() { + ClientHTTP client("httpbin.org", 80); + HTTPResponse response = client.Send(CreateRequestHTTP("GET", "/get", "httpbin.org")); + if(response.status == "200 OK") { + return nullptr; + } else { + return new std::string(response.body); + } + } +} \ No newline at end of file diff --git a/tests/ShouldSendRecieveHTTP.cpp b/tests/ShouldSendRecieveHTTP.cpp new file mode 100644 index 0000000..699f0f9 --- /dev/null +++ b/tests/ShouldSendRecieveHTTP.cpp @@ -0,0 +1,52 @@ +/* +Crafter® Build +Copyright (C) 2025 Catcrafts® +Catcrafts.net + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License version 3.0 as published by the Free Software Foundation; + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include +#include +import Crafter.Thread; +import Crafter.Network; +import std; +using namespace Crafter; + +extern "C" { + std::string* RunTest() { + bool success = false; + ThreadPool::Start(2); + ListenerHTTP listener(8080, {{"/", [&](const HTTPRequest& request) { + return CreateResponseHTTP("200 OK", "Hello World!"); + }}}); + try { + listener.ListenAsyncSync(); + ClientHTTP client("localhost", 8080); + HTTPResponse response = client.Send(CreateRequestHTTP("GET", "/", "localhost")); + if(response.status == "200 OK" && response.body == "Hello World!") { + listener.Stop(); + ThreadPool::Stop(); + return nullptr; + } else { + listener.Stop(); + ThreadPool::Stop(); + return new std::string(response.status +response.body); + } + } catch(std::exception& e) { + listener.Stop(); + ThreadPool::Stop(); + return new std::string(e.what()); + } + } +} \ No newline at end of file