Fixes #6990 - UnixDomainServerConnector throws misleading exception on invalid socket path.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2021-11-12 13:19:52 +01:00
parent b06ccf286d
commit 6aedf4af6e
2 changed files with 30 additions and 5 deletions

View File

@ -233,24 +233,37 @@ public class UnixDomainServerConnector extends AbstractConnector
return serverChannel;
}
private ServerSocketChannel bindServerSocketChannel()
private ServerSocketChannel bindServerSocketChannel() throws IOException
{
Path unixDomainPath = getUnixDomainPath();
ServerSocketChannel serverChannel;
SocketAddress socketAddress;
try
{
ProtocolFamily family = Enum.valueOf(StandardProtocolFamily.class, "UNIX");
Class<?> channelClass = Class.forName("java.nio.channels.ServerSocketChannel");
ServerSocketChannel serverChannel = (ServerSocketChannel)channelClass.getMethod("open", ProtocolFamily.class).invoke(null, family);
serverChannel = (ServerSocketChannel)channelClass.getMethod("open", ProtocolFamily.class).invoke(null, family);
// Unix-Domain does not support SO_REUSEADDR.
Class<?> addressClass = Class.forName("java.net.UnixDomainSocketAddress");
SocketAddress socketAddress = (SocketAddress)addressClass.getMethod("of", Path.class).invoke(null, getUnixDomainPath());
serverChannel.bind(socketAddress, getAcceptQueueSize());
return serverChannel;
socketAddress = (SocketAddress)addressClass.getMethod("of", Path.class).invoke(null, unixDomainPath);
}
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);
}
try
{
serverChannel.bind(socketAddress, getAcceptQueueSize());
return serverChannel;
}
catch (IOException x)
{
String message = String.format("Could not bind %s to %s", UnixDomainServerConnector.class.getSimpleName(), unixDomainPath);
throw new IOException(message, x);
}
}
@Override

View File

@ -13,6 +13,7 @@
package org.eclipse.jetty.unixdomain.server;
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
@ -53,6 +54,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledForJreRange(min = JRE.JAVA_16)
@ -256,6 +258,16 @@ public class UnixDomainTest
}
}
@Test
public void testInvalidUnixDomainPath()
{
server = new Server();
UnixDomainServerConnector connector = new UnixDomainServerConnector(server, factories);
connector.setUnixDomainPath(Path.of("/does/not/exist"));
server.addConnector(connector);
assertThrows(IOException.class, () -> server.start());
}
private static Path toUnixDomainPath(SocketAddress address)
{
try