From 210ec42021cd08f1f096d81ebd0ee2ddb2866810 Mon Sep 17 00:00:00 2001 From: Ryan Schmitt Date: Thu, 22 May 2025 20:24:16 -0700 Subject: [PATCH] SingleCoreIOReactor: Add support for Unix domain sockets This commit teaches `SingleCoreIOReactor` to recognize when the remote address of a session request is for a JEP 380 Unix-domain socket and open the corresponding type of `SocketChannel`. This is done using reflection, since the underlying feature was added in JDK16. --- .../hc/core5/reactor/SingleCoreIOReactor.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java index 2442f355b3..c56d3adc70 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java @@ -29,9 +29,11 @@ import java.io.IOException; import java.net.InetSocketAddress; +import java.net.ProtocolFamily; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketOption; +import java.net.StandardProtocolFamily; import java.net.StandardSocketOptions; import java.net.UnknownHostException; import java.nio.channels.CancelledKeyException; @@ -346,7 +348,7 @@ private void processPendingConnectionRequests() { if (!sessionRequest.isCancelled()) { final SocketChannel socketChannel; try { - socketChannel = SocketChannel.open(); + socketChannel = openSocketFor(sessionRequest.remoteAddress); } catch (final IOException ex) { sessionRequest.failed(ex); return; @@ -361,6 +363,18 @@ private void processPendingConnectionRequests() { } } + private static SocketChannel openSocketFor(final SocketAddress remoteAddress) throws IOException { + if (remoteAddress instanceof InetSocketAddress) { + return SocketChannel.open(); + } + try { + return (SocketChannel) SocketChannel.class.getMethod("open", ProtocolFamily.class) + .invoke(null, StandardProtocolFamily.valueOf("UNIX")); + } catch (final ReflectiveOperationException e) { + throw new UnsupportedOperationException("UNIX-family socket channels not supported", e); + } + } + private void processConnectionRequest(final SocketChannel socketChannel, final IOSessionRequest sessionRequest) throws IOException { socketChannel.configureBlocking(false); prepareSocket(socketChannel);