From 76e6c3d690caac3faa664b7b8b79cbc7c6a58394 Mon Sep 17 00:00:00 2001 From: cflip Date: Thu, 7 Jul 2022 09:50:38 -0600 Subject: Initial commit --- src/ClientConnection.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/ClientConnection.cpp (limited to 'src/ClientConnection.cpp') diff --git a/src/ClientConnection.cpp b/src/ClientConnection.cpp new file mode 100644 index 0000000..3bf93c3 --- /dev/null +++ b/src/ClientConnection.cpp @@ -0,0 +1,49 @@ +#include "ClientConnection.h" + +#include +#include +#include + +ClientConnection::ClientConnection(int socket) + : m_socket_fd(socket) +{ +} + +void ClientConnection::dump_request_data() +{ + constexpr int BUFFER_SIZE = 4096; + char buffer[BUFFER_SIZE+1]; + int n; + + memset(buffer, 0, BUFFER_SIZE); + while ((n = read(m_socket_fd, buffer, BUFFER_SIZE-1)) > 0) { + std::cout << buffer; + + if (buffer[n-1] == '\n') + break; + + memset(buffer, 0, BUFFER_SIZE); + } +} + +bool ClientConnection::send(const HttpResponse& response, const char* message) +{ + if (!m_is_open) + return false; + + std::stringstream ss; + ss << response.to_string() + << "\r\n\r\n" + << message; + + std::string result = ss.str(); + write(m_socket_fd, result.c_str(), result.length()); + + return true; +} + +void ClientConnection::close() +{ + m_is_open = false; + ::close(m_socket_fd); +} -- cgit v1.2.3