diff options
author | cflip <cflip@cflip.net> | 2023-03-26 10:21:47 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-03-26 10:21:47 -0600 |
commit | f8ac67c943244aa57e5f848ca6b1f66eca32e138 (patch) | |
tree | 2bf4f6695393b993ad3a50528304fdb6e6a0b86a /src/ClientConnection.cpp | |
parent | 83fb0b96c94e7f596f81d5bc346150904457ed64 (diff) |
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.
Diffstat (limited to 'src/ClientConnection.cpp')
-rw-r--r-- | src/ClientConnection.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
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 <cstring> #include <iostream> #include <sstream> -#include <unistd.h> + +#ifdef _WIN32 + #include <winsock2.h> + #pragma comment(lib, "ws2_32.lib") +#else + #include <unistd.h> +#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 } |