diff options
author | cflip <cflip@cflip.net> | 2022-07-07 09:50:38 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2022-07-07 09:50:38 -0600 |
commit | 76e6c3d690caac3faa664b7b8b79cbc7c6a58394 (patch) | |
tree | 8b0ebbc6fe543a1dc0b9d8eba72efd35f8817aa6 /src/ClientConnection.cpp |
Initial commit
Diffstat (limited to 'src/ClientConnection.cpp')
-rw-r--r-- | src/ClientConnection.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
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 <unistd.h> +#include <iostream> +#include <sstream> + +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); +} |