Skip to content

Commit

Permalink
Issue #7277 - SocketAddress as connector.getLocalAddress
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Dec 20, 2021
1 parent a386068 commit a498aea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.util.ArrayList;
Expand Down Expand Up @@ -171,10 +172,10 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
*/
private HostPort _serverUriAuthority;
/**
* The authority used to define the local authority for the connection
* The address used to define the local address for the connection
* (see ServletRequest.getLocalName(), ServletRequest.getLocalAddr(), and ServletRequest.getLocalPort()).
*/
private HostPort _localAuthority;
private SocketAddress _localAddress;
private int _acceptorPriorityDelta = -2;
private boolean _accepting = true;
private ThreadPoolBudget.Lease _lease;
Expand Down Expand Up @@ -311,25 +312,22 @@ public int getAcceptors()
return _acceptors.length;
}

public HostPort getLocalAuthority()
public SocketAddress getLocalAddress()
{
return _localAuthority;
return _localAddress;
}

public void setLocalAuthority(HostPort authority)
public void setLocalAddress(SocketAddress localAddress)
{
Objects.requireNonNull(authority, "Authority");
Objects.requireNonNull(localAddress, "Local Address");

if (isStarted())
throw new IllegalStateException(getState());

if (!authority.hasHost() || !authority.hasPort())
throw new IllegalStateException("Local Authority must have both host and port declared");
else
_localAuthority = authority;
_localAddress = localAddress;
}

@ManagedAttribute("server uri authority")
@ManagedAttribute("server authority")
public HostPort getServerAuthority()
{
return _serverUriAuthority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.eclipse.jetty.server;

import java.net.SocketAddress;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -105,19 +106,19 @@ public interface Connector extends LifeCycle, Container, Graceful
String getName();

/**
* Get the Local Authority of the connection
* Get the Local Address of the connection
*
* @return Returns the connection local authority (name/port).
* @return Returns the connection local address.
*/
HostPort getLocalAuthority();
SocketAddress getLocalAddress();

/**
* Specify the connection local authority (name/port) used within application API layer
* Specify the connection local address used within application API layer
* when identifying the local host name/port of a connected endpoint.
*
* @param authority the full authority including host and port, or null to reset to default behavior
* @param localAddress the address to use for host/addr/port, or null to reset to default behavior
*/
void setLocalAuthority(HostPort authority);
void setLocalAddress(SocketAddress localAddress);

/**
* Get the optional Server URI authority default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.EventListener;
Expand Down Expand Up @@ -307,7 +308,7 @@ public EndPoint getEndPoint()
* if the local address is unavailable.
* </p>
* <p>
* Value can be overridden by {@link Connector#setLocalAuthority(HostPort)} for
* Value can be overridden by {@link Connector#setLocalAddress(SocketAddress)} for
* scenarios where Jetty is being an intermediary and the local name
* needs to be changed to satisfy public (pre-intermediary) HTTP behaviors
* such as absolute-URI creation (eg: Location response header).
Expand All @@ -319,9 +320,12 @@ public EndPoint getEndPoint()
*/
public String getLocalName()
{
HostPort localAuthority = getConnector().getLocalAuthority();
if (localAuthority != null)
return localAuthority.getHost();
SocketAddress localAddress = getConnector().getLocalAddress();
if (localAddress != null)
{
if (localAddress instanceof InetSocketAddress)
return ((InetSocketAddress)localAddress).getHostName();
}

InetSocketAddress local = getLocalAddress();
if (local != null)
Expand All @@ -337,7 +341,7 @@ public String getLocalName()
* This is the port the connector is bound to and is accepting connection on.
* </p>
* <p>
* Value can be overridden by {@link Connector#setLocalAuthority(HostPort)} for
* Value can be overridden by {@link Connector#setLocalAddress(SocketAddress)} for
* scenarios where Jetty is being an intermediary and the local port
* needs to be changed to satisfy public (pre-intermediary) HTTP behaviors
* such as absolute-URI creation (eg: Location response header).
Expand All @@ -348,16 +352,20 @@ public String getLocalName()
*/
public int getLocalPort()
{
HostPort localAuthority = getConnector().getLocalAuthority();
if (localAuthority != null)
return localAuthority.getPort();
SocketAddress localAddress = getConnector().getLocalAddress();
if (localAddress instanceof InetSocketAddress)
return ((InetSocketAddress)localAddress).getPort();

InetSocketAddress local = getLocalAddress();
return local == null ? 0 : local.getPort();
}

public InetSocketAddress getLocalAddress()
{
SocketAddress localAddress = getConnector().getLocalAddress();
if (localAddress instanceof InetSocketAddress)
return ((InetSocketAddress)localAddress);

return _endPoint.getLocalAddress();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
Expand Down Expand Up @@ -50,9 +51,9 @@ public class ConnectorAuthorityOverrideTest
@Test
public void testLocalAuthorityHttp10NoHostDump() throws Exception
{
HostPort localAuthority = new HostPort("foo.local.authority:80");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("foo.local.authority", 80);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /dump HTTP/1.0\r\n" +
"\r\n";
Expand All @@ -64,7 +65,7 @@ public void testLocalAuthorityHttp10NoHostDump() throws Exception
assertThat("response content", responseContent, allOf(
containsString("ServerName=[foo.local.authority]"),
containsString("ServerPort=[80]"),
containsString("LocalAddr=[" + server.getConnectorLocalAddr() + "]"), // TODO: how to modify address?
containsString("LocalAddr=[foo.local.authority]"),
containsString("LocalName=[foo.local.authority]"),
containsString("LocalPort=[80]"),
containsString("RequestURL=[http://foo.local.authority/dump]")
Expand All @@ -75,9 +76,9 @@ public void testLocalAuthorityHttp10NoHostDump() throws Exception
@Test
public void testLocalAuthorityHttp10NoHostRedirect() throws Exception
{
HostPort localAuthority = new HostPort("foo.local.authority:80");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("foo.local.authority", 80);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /redirect HTTP/1.0\r\n" +
"\r\n";
Expand All @@ -93,9 +94,9 @@ public void testLocalAuthorityHttp10NoHostRedirect() throws Exception
@Test
public void testLocalAuthorityHttp10NotFound() throws Exception
{
HostPort localAuthority = new HostPort("foo.local.authority:7777");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("foo.local.authority", 777);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /bogus HTTP/1.0\r\n" +
"\r\n";
Expand All @@ -105,16 +106,16 @@ public void testLocalAuthorityHttp10NotFound() throws Exception
// because of the custom error handler, we actually expect a redirect
assertThat(response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
String location = response.get(HttpHeader.LOCATION);
assertThat(location, is("http://foo.local.authority:7777/error"));
assertThat(location, is("http://foo.local.authority:777/error"));
}
}

@Test
public void testLocalAuthorityHttp11EmptyHostDump() throws Exception
{
HostPort localAuthority = new HostPort("foo.local.authority:80");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("foo.local.authority", 80);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /dump HTTP/1.1\r\n" +
"Host: \r\n" +
Expand All @@ -128,7 +129,7 @@ public void testLocalAuthorityHttp11EmptyHostDump() throws Exception
assertThat("response content", responseContent, allOf(
containsString("ServerName=[foo.local.authority]"),
containsString("ServerPort=[80]"),
containsString("LocalAddr=[" + server.getConnectorLocalAddr() + "]"), // TODO: how to modify address?
containsString("LocalAddr=[foo.local.authority]"),
containsString("LocalName=[foo.local.authority]"),
containsString("LocalPort=[80]"),
containsString("RequestURL=[http://foo.local.authority/dump]")
Expand All @@ -139,9 +140,9 @@ public void testLocalAuthorityHttp11EmptyHostDump() throws Exception
@Test
public void testLocalAuthorityHttp11EmptyHostRedirect() throws Exception
{
HostPort localAuthority = new HostPort("foo.local.authority:80");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("foo.local.authority", 80);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /redirect HTTP/1.1\r\n" +
"Host: \r\n" +
Expand All @@ -159,9 +160,9 @@ public void testLocalAuthorityHttp11EmptyHostRedirect() throws Exception
@Test
public void testLocalAuthorityHttp11ValidHostDump() throws Exception
{
HostPort localAuthority = new HostPort("zed.local.authority:9999");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("zed.local.authority", 9999);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /dump HTTP/1.1\r\n" +
"Host: jetty.eclipse.org:8888\r\n" +
Expand All @@ -175,7 +176,7 @@ public void testLocalAuthorityHttp11ValidHostDump() throws Exception
assertThat("response content", responseContent, allOf(
containsString("ServerName=[jetty.eclipse.org]"),
containsString("ServerPort=[8888]"),
containsString("LocalAddr=[" + server.getConnectorLocalAddr() + "]"), // TODO: how to modify address?
containsString("LocalAddr=[zed.local.authority]"),
containsString("LocalName=[zed.local.authority]"),
containsString("LocalPort=[9999]"),
containsString("RequestURL=[http://jetty.eclipse.org:8888/dump]")
Expand All @@ -186,9 +187,9 @@ public void testLocalAuthorityHttp11ValidHostDump() throws Exception
@Test
public void testLocalAuthorityHttp11ValidHostRedirect() throws Exception
{
HostPort localAuthority = new HostPort("zed.local.authority:9999");
InetSocketAddress localAddress = InetSocketAddress.createUnresolved("zed.local.authority", 9999);

try (CloseableServer server = startServer(null, localAuthority))
try (CloseableServer server = startServer(null, localAddress))
{
String rawRequest = "GET /redirect HTTP/1.1\r\n" +
"Host: jetty.eclipse.org:8888\r\n" +
Expand Down Expand Up @@ -596,14 +597,14 @@ private HttpTester.Response issueRequest(CloseableServer server, String rawReque
}
}

private CloseableServer startServer(HostPort serverUriAuthority, HostPort localAuthority) throws Exception
private CloseableServer startServer(HostPort serverUriAuthority, InetSocketAddress localAddress) throws Exception
{
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
if (serverUriAuthority != null)
connector.setServerAuthority(serverUriAuthority);
if (localAuthority != null)
connector.setLocalAuthority(localAuthority);
if (localAddress != null)
connector.setLocalAddress(localAddress);
connector.setPort(0);
server.addConnector(connector);

Expand Down

0 comments on commit a498aea

Please sign in to comment.