summaryrefslogtreecommitdiff
path: root/src/ClientConnection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ClientConnection.cpp')
-rw-r--r--src/ClientConnection.cpp49
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);
+}