Skip to content

Commit d171fcf

Browse files
committed
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.
1 parent 85c55ad commit d171fcf

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929

3030
import java.io.IOException;
3131
import java.net.InetSocketAddress;
32+
import java.net.ProtocolFamily;
3233
import java.net.Socket;
3334
import java.net.SocketAddress;
3435
import java.net.SocketOption;
36+
import java.net.StandardProtocolFamily;
3537
import java.net.StandardSocketOptions;
3638
import java.net.UnknownHostException;
3739
import java.nio.channels.CancelledKeyException;
@@ -346,7 +348,7 @@ private void processPendingConnectionRequests() {
346348
if (!sessionRequest.isCancelled()) {
347349
final SocketChannel socketChannel;
348350
try {
349-
socketChannel = SocketChannel.open();
351+
socketChannel = openSocketFor(sessionRequest.remoteAddress);
350352
} catch (final IOException ex) {
351353
sessionRequest.failed(ex);
352354
return;
@@ -361,6 +363,18 @@ private void processPendingConnectionRequests() {
361363
}
362364
}
363365

366+
private static SocketChannel openSocketFor(final SocketAddress remoteAddress) throws IOException {
367+
if (remoteAddress instanceof InetSocketAddress) {
368+
return SocketChannel.open();
369+
}
370+
try {
371+
return (SocketChannel) SocketChannel.class.getMethod("open", ProtocolFamily.class)
372+
.invoke(null, StandardProtocolFamily.valueOf("UNIX"));
373+
} catch (final ReflectiveOperationException e) {
374+
throw new UnsupportedOperationException("UNIX-family socket channels not supported", e);
375+
}
376+
}
377+
364378
private void processConnectionRequest(final SocketChannel socketChannel, final IOSessionRequest sessionRequest) throws IOException {
365379
socketChannel.configureBlocking(false);
366380
prepareSocket(socketChannel);

0 commit comments

Comments
 (0)