Skip to content

Commit 72b35be

Browse files
authored
Add AF_UNIX support on windows (#2115)
Signed-off-by: Piotr Stankiewicz <[email protected]>
1 parent 65ce51a commit 72b35be

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

httplib.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ using ssize_t = long;
187187
#include <io.h>
188188
#include <winsock2.h>
189189
#include <ws2tcpip.h>
190+
#include <afunix.h>
190191

191192
#ifndef WSA_FLAG_NO_HANDLE_INHERIT
192193
#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
@@ -3538,7 +3539,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
35383539
hints.ai_flags = socket_flags;
35393540
}
35403541

3541-
#ifndef _WIN32
35423542
if (hints.ai_family == AF_UNIX) {
35433543
const auto addrlen = host.length();
35443544
if (addrlen > sizeof(sockaddr_un::sun_path)) { return INVALID_SOCKET; }
@@ -3562,11 +3562,19 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
35623562
sizeof(addr) - sizeof(addr.sun_path) + addrlen);
35633563

35643564
#ifndef SOCK_CLOEXEC
3565+
#ifndef _WIN32
35653566
fcntl(sock, F_SETFD, FD_CLOEXEC);
3567+
#endif
35663568
#endif
35673569

35683570
if (socket_options) { socket_options(sock); }
35693571

3572+
#ifdef _WIN32
3573+
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so avoid
3574+
// setting default_socket_options.
3575+
detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
3576+
#endif
3577+
35703578
bool dummy;
35713579
if (!bind_or_connect(sock, hints, dummy)) {
35723580
close_socket(sock);
@@ -3575,7 +3583,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
35753583
}
35763584
return sock;
35773585
}
3578-
#endif
35793586

35803587
auto service = std::to_string(port);
35813588

test/test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ static void read_file(const std::string &path, std::string &out) {
7070
fs.read(&out[0], static_cast<std::streamsize>(size));
7171
}
7272

73-
#ifndef _WIN32
7473
class UnixSocketTest : public ::testing::Test {
7574
protected:
7675
void TearDown() override { std::remove(pathname_.c_str()); }
@@ -167,6 +166,7 @@ TEST_F(UnixSocketTest, abstract) {
167166
}
168167
#endif
169168

169+
#ifndef _WIN32
170170
TEST(SocketStream, wait_writable_UNIX) {
171171
int fds[2];
172172
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));

0 commit comments

Comments
 (0)