This commit is contained in:
Jorijn van der Graaf 2025-11-03 14:25:51 +01:00
commit 9bdf133d0f
12 changed files with 301 additions and 168 deletions

View file

@ -26,10 +26,12 @@ import :HTTP;
namespace Crafter {
export class ClientHTTP {
public:
std::string host;
std::uint16_t port;
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;
HTTPResponse Send(const char* request, std::uint32_t length);
HTTPResponse Send(std::string request);
private:
ClientTCP client;
};

View file

@ -22,12 +22,21 @@ export module Crafter.Network:ClientTCP;
import std;
namespace Crafter {
export class SocketClosedException : public std::exception {
public:
const char* what() const noexcept override {
return "Socket closed";
}
};
export class ClientTCP {
public:
int socketid;
ClientTCP(int socket);
ClientTCP(const char* host, std::uint16_t port);
ClientTCP(std::string host, std::uint16_t port);
~ClientTCP();
void Stop();
void Send(const void* buffer, std::uint32_t size) const;
std::vector<char> RecieveSync() const;
std::vector<char> RecieveUntilCloseSync() const;
@ -39,7 +48,5 @@ namespace Crafter {
void RecieveAsync(std::uint32_t bufferSize, std::function<void(std::vector<char>)> recieveCallback) const;
void RecieveUntilFullAsync(std::uint32_t bufferSize, std::function<void(std::vector<char>)> recieveCallback) const;
void RecieveAsync(std::uint32_t bufferSize, std::function<void(int)> recieveCallback, char* buffer) const;
private:
int socketid;
};
}

View file

@ -35,7 +35,7 @@ namespace Crafter {
};
export constexpr std::string CreateResponseHTTP(std::string status) {
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n\r\n", status);
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n", status);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map<std::string, std::string> headers) {
@ -43,7 +43,7 @@ namespace Crafter {
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);
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\nContent-Length: 0\r\n{}\r\n", status, headersString);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::string body) {

View file

@ -21,21 +21,38 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
export module Crafter.Network:ListenerHTTP;
import std;
import :HTTP;
import :ClientTCP;
namespace Crafter {
export class ListenerHTTP;
class ListenerHTTPClient {
public:
std::atomic<bool> disconnected;
ClientTCP client;
std::thread thread;
ListenerHTTP* server;
ListenerHTTPClient(ListenerHTTP* server, int s);
void ListenRoutes();
};
export class ListenerHTTP {
public:
int s;
std::vector<ListenerHTTPClient*> clients;
bool running = true;
std::unordered_map<std::string, std::function<std::string(const HTTPRequest&)>> routes;
const std::unordered_map<std::string, std::function<std::string(const HTTPRequest&)>> routes;
ListenerHTTP(std::uint16_t port, std::unordered_map<std::string, std::function<std::string(const HTTPRequest&)>> routes);
~ListenerHTTP();
void Listen();
void Stop();
};
export class ListenerAsyncHTTP {
public:
ListenerHTTP listener;
std::thread thread;
ListenerAsyncHTTP(std::uint16_t port, std::unordered_map<std::string, std::function<std::string(const HTTPRequest&)>> routes);
~ListenerAsyncHTTP();
void Stop();
void ListenSyncSync() const;
void ListenSyncAsync() const;
void ListenAsyncSync() const;
void ListenAsyncAsync() const;
private:
void CallRoute(int client) const;
int s;
};
}