Skip to content

Add AF_UNIX support on windows #2115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ using ssize_t = long;
#include <io.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <afunix.h>

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

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

#ifndef SOCK_CLOEXEC
#ifndef _WIN32
fcntl(sock, F_SETFD, FD_CLOEXEC);
#endif
#endif

if (socket_options) { socket_options(sock); }

#ifdef _WIN32
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so avoid
// setting default_socket_options.
detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
#endif

bool dummy;
if (!bind_or_connect(sock, hints, dummy)) {
close_socket(sock);
Expand All @@ -3575,7 +3583,6 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
}
return sock;
}
#endif

auto service = std::to_string(port);

Expand Down
2 changes: 1 addition & 1 deletion test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ static void read_file(const std::string &path, std::string &out) {
fs.read(&out[0], static_cast<std::streamsize>(size));
}

#ifndef _WIN32
class UnixSocketTest : public ::testing::Test {
protected:
void TearDown() override { std::remove(pathname_.c_str()); }
Expand Down Expand Up @@ -167,6 +166,7 @@ TEST_F(UnixSocketTest, abstract) {
}
#endif

#ifndef _WIN32
TEST(SocketStream, wait_writable_UNIX) {
int fds[2];
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
Expand Down