Skip to content

Commit 1f356d5

Browse files
author
Piotr Stankiewicz
committed
Add AF_UNIX support on windows
Signed-off-by: Piotr Stankiewicz <[email protected]>
1 parent 65ce51a commit 1f356d5

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

httplib.h

Lines changed: 10 additions & 3 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,10 +3562,18 @@ 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

3568-
if (socket_options) { socket_options(sock); }
3570+
if (socket_options
3571+
#ifdef _WIN32
3572+
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so avoid
3573+
// setting default_socket_options.
3574+
&& *socket_options.target<void(*)(socket_t)>() != default_socket_options
3575+
#endif
3576+
) { socket_options(sock); }
35693577

35703578
bool dummy;
35713579
if (!bind_or_connect(sock, hints, dummy)) {
@@ -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

0 commit comments

Comments
 (0)