Skip to content

Commit

Permalink
Updated code to use new UnixDomain APIs explicitly, rather than via r…
Browse files Browse the repository at this point in the history
…eflection.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Feb 23, 2024
1 parent 9b64130 commit 535e772
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ProtocolFamily;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketOption;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.NetworkChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
Expand All @@ -33,7 +33,6 @@
import java.util.concurrent.Executor;

import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.JavaVersion;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
Expand Down Expand Up @@ -677,21 +676,11 @@ private static Configurator forUnixDomain(Path path)
return new Configurator()
{
@Override
public ChannelWithAddress newChannelWithAddress(ClientConnector clientConnector, SocketAddress address, Map<String, Object> context)
public ChannelWithAddress newChannelWithAddress(ClientConnector clientConnector, SocketAddress address, Map<String, Object> context) throws IOException
{
try
{
ProtocolFamily family = Enum.valueOf(StandardProtocolFamily.class, "UNIX");
SocketChannel socketChannel = (SocketChannel)SocketChannel.class.getMethod("open", ProtocolFamily.class).invoke(null, family);
Class<?> addressClass = Class.forName("java.net.UnixDomainSocketAddress");
SocketAddress socketAddress = (SocketAddress)addressClass.getMethod("of", Path.class).invoke(null, path);
return new ChannelWithAddress(socketChannel, socketAddress);
}
catch (Throwable x)
{
String message = "Unix-Domain SocketChannels are available starting from Java 16, your Java version is: " + JavaVersion.VERSION;
throw new UnsupportedOperationException(message, x);
}
SocketChannel socketChannel = SocketChannel.open(StandardProtocolFamily.UNIX);
UnixDomainSocketAddress socketAddress = UnixDomainSocketAddress.of(path);
return new ChannelWithAddress(socketChannel, socketAddress);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ReadPendingException;
import java.nio.channels.WritePendingException;
Expand Down Expand Up @@ -636,11 +637,11 @@ private void parseBodyAndUpgrade() throws IOException
{
byte[] addr = new byte[108];
byteBuffer.get(addr);
String src = UnixDomain.toPath(addr);
String src = toUnixDomainPath(addr);
byteBuffer.get(addr);
String dst = UnixDomain.toPath(addr);
local = UnixDomain.newSocketAddress(dst);
remote = UnixDomain.newSocketAddress(src);
String dst = toUnixDomainPath(addr);
local = UnixDomainSocketAddress.of(dst);
remote = UnixDomainSocketAddress.of(src);
}
default -> throw new IllegalStateException("Unsupported family " + _family);
}
Expand Down Expand Up @@ -765,6 +766,18 @@ private void releaseAndClose()
_buffer.release();
close();
}

private static String toUnixDomainPath(byte[] bytes)
{
// Unix-Domain paths are zero-terminated.
int i = 0;
while (i < bytes.length)
{
if (bytes[i++] == 0)
break;
}
return new String(bytes, 0, i, StandardCharsets.US_ASCII).trim();
}
}
}

Expand Down Expand Up @@ -973,51 +986,4 @@ public void write(Callback callback, ByteBuffer... buffers) throws WritePendingE
_endPoint.write(callback, buffers);
}
}

private static class UnixDomain
{
private static final Class<?> unixDomainSocketAddress = probe();

private static Class<?> probe()
{
try
{
return ClassLoader.getPlatformClassLoader().loadClass("java.net.UnixDomainSocketAddress");
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("ignored", x);
return null;
}
}

private static SocketAddress newSocketAddress(String path)
{
try
{
if (unixDomainSocketAddress != null)
return (SocketAddress)unixDomainSocketAddress.getMethod("of", String.class).invoke(null, path);
return null;
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug("ignored", x);
return null;
}
}

private static String toPath(byte[] bytes)
{
// Unix-Domain paths are zero-terminated.
int i = 0;
while (i < bytes.length)
{
if (bytes[i++] == 0)
break;
}
return new String(bytes, 0, i, StandardCharsets.US_ASCII).trim();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.net.SocketAddress;
import java.net.UnixDomainSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
Expand All @@ -40,11 +41,7 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

import static org.eclipse.jetty.client.ProxyProtocolClientConnectionFactory.V1;
import static org.eclipse.jetty.client.ProxyProtocolClientConnectionFactory.V2;
Expand All @@ -56,33 +53,12 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnabledForJreRange(min = JRE.JAVA_16)
public class UnixDomainTest
{
private static final Class<?> unixDomainSocketAddressClass = probe();

private static Class<?> probe()
{
try
{
return ClassLoader.getPlatformClassLoader().loadClass("java.net.UnixDomainSocketAddress");
}
catch (Throwable x)
{
return null;
}
}

private ConnectionFactory[] factories = new ConnectionFactory[]{new HttpConnectionFactory()};
private Server server;
private Path unixDomainPath;

@BeforeEach
public void prepare()
{
Assumptions.assumeTrue(unixDomainSocketAddressClass != null);
}

private void start(Handler handler) throws Exception
{
server = new Server();
Expand Down Expand Up @@ -120,9 +96,9 @@ public boolean handle(Request request, Response response, Callback callback)

// Verify the SocketAddresses.
SocketAddress local = endPoint.getLocalSocketAddress();
assertThat(local, Matchers.instanceOf(unixDomainSocketAddressClass));
assertThat(local, Matchers.instanceOf(UnixDomainSocketAddress.class));
SocketAddress remote = endPoint.getRemoteSocketAddress();
assertThat(remote, Matchers.instanceOf(unixDomainSocketAddressClass));
assertThat(remote, Matchers.instanceOf(UnixDomainSocketAddress.class));

// Verify that other address methods don't throw.
local = assertDoesNotThrow(endPoint::getLocalAddress);
Expand Down Expand Up @@ -205,8 +181,8 @@ public boolean handle(Request request, Response response, Callback callback)
{
EndPoint endPoint = request.getConnectionMetaData().getConnection().getEndPoint();
assertThat(endPoint, Matchers.instanceOf(ProxyConnectionFactory.ProxyEndPoint.class));
assertThat(endPoint.getLocalSocketAddress(), Matchers.instanceOf(unixDomainSocketAddressClass));
assertThat(endPoint.getRemoteSocketAddress(), Matchers.instanceOf(unixDomainSocketAddressClass));
assertThat(endPoint.getLocalSocketAddress(), Matchers.instanceOf(UnixDomainSocketAddress.class));
assertThat(endPoint.getRemoteSocketAddress(), Matchers.instanceOf(UnixDomainSocketAddress.class));
String target = Request.getPathInContext(request);
if ("/v1".equals(target))
{
Expand Down Expand Up @@ -273,15 +249,8 @@ public void testInvalidUnixDomainPath()

private static Path toUnixDomainPath(SocketAddress address)
{
try
{
Assertions.assertNotNull(unixDomainSocketAddressClass);
return (Path)unixDomainSocketAddressClass.getMethod("getPath").invoke(address);
}
catch (Throwable x)
{
Assertions.fail(x);
throw new AssertionError();
}
if (address instanceof UnixDomainSocketAddress unix)
return unix.getPath();
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,6 @@ public void testDefaultLoggingProviderNotActiveWhenExplicitProviderIsPresent() t
}

@Test
@EnabledForJreRange(min = JRE.JAVA_16)
public void testUnixDomain() throws Exception
{
Path jettyBase = newTestJettyBaseDirectory();
Expand Down

0 comments on commit 535e772

Please sign in to comment.