From f8ac67c943244aa57e5f848ca6b1f66eca32e138 Mon Sep 17 00:00:00 2001 From: cflip Date: Sun, 26 Mar 2023 10:21:47 -0600 Subject: Add support for building on Windows This makes it possible to compile cfws with Visual Studio. Since winsock and POSIX use very similar APIs, porting is mostly just a matter of placing ifdefs around #includes and functions with slightly different names. However, CGI scripts and command line arguments are not available in this port yet, since they used the Unix-exclusive getopt.h and popen. --- src/ClientConnection.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/ClientConnection.cpp') diff --git a/src/ClientConnection.cpp b/src/ClientConnection.cpp index c434212..e8e6692 100644 --- a/src/ClientConnection.cpp +++ b/src/ClientConnection.cpp @@ -3,7 +3,13 @@ #include #include #include -#include + +#ifdef _WIN32 + #include + #pragma comment(lib, "ws2_32.lib") +#else + #include +#endif ClientConnection::ClientConnection(int socket) : m_socket_fd(socket) @@ -19,7 +25,12 @@ HttpRequest ClientConnection::read_request() const int n = 0; memset(buffer, 0, BUFFER_SIZE); - while ((n = read(m_socket_fd, buffer, BUFFER_SIZE - 1)) > 0) { +#ifdef _WIN32 + n = recv(m_socket_fd, buffer, BUFFER_SIZE - 1, 0); +#else + n = read(m_socket_fd, buffer, BUFFER_SIZE - 1); +#endif + while (n > 0) { if (buffer[n - 1] == '\n') break; @@ -35,7 +46,11 @@ bool ClientConnection::send(const HttpResponse& response) const return false; std::string result = response.to_string(); +#ifdef _WIN32 + ::send(m_socket_fd, result.c_str(), result.length(), 0); +#else write(m_socket_fd, result.c_str(), result.length()); +#endif return true; } @@ -43,5 +58,9 @@ bool ClientConnection::send(const HttpResponse& response) const void ClientConnection::close_connection() { m_is_open = false; +#ifdef _WIN32 + closesocket(m_socket_fd); +#else close(m_socket_fd); +#endif } -- cgit v1.2.3