Skip to content

Commit

Permalink
Issue #7277 - Split Authority Approach
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 8b41067 commit c467d8f
Show file tree
Hide file tree
Showing 10 changed files with 796 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -164,7 +165,15 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
private ConnectionFactory _defaultConnectionFactory;
/* The name used to link up virtual host configuration to named connectors */
private String _name;
/* The authority used to define the connection local name/port (see ServletRequest.getLocalName(), and ServletRequest.getLocalPort()). */
/**
* The authority used to define the server uri authority fallback for the connection
* (see ServletRequest.getServerName(), and ServletRequest.getServerPort()).
*/
private HostPort _serverUriAuthority;
/**
* The authority used to define the local authority for the connection
* (see ServletRequest.getLocalName(), ServletRequest.getLocalAddr(), and ServletRequest.getLocalPort()).
*/
private HostPort _localAuthority;
private int _acceptorPriorityDelta = -2;
private boolean _accepting = true;
Expand Down Expand Up @@ -302,21 +311,41 @@ public int getAcceptors()
return _acceptors.length;
}

@ManagedAttribute("local authority")
public HostPort getLocalAuthority()
{
return _localAuthority;
}

public void setLocalAuthority(HostPort authority)
{
Objects.requireNonNull(authority, "Authority");

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

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

@ManagedAttribute("server uri authority")
public HostPort getServerUriAuthority()
{
return _serverUriAuthority;
}

public void setServerUriAuthority(HostPort authority)
{
if (isStarted())
throw new IllegalStateException(getState());

_localAuthority = authority;
if (authority == null)
_serverUriAuthority = null;
else if (!authority.hasHost())
throw new IllegalStateException("Server URI Authority must have host declared");
else
_serverUriAuthority = authority;
}

@Override
Expand All @@ -340,10 +369,6 @@ protected void doStart() throws Exception

_lease = ThreadPoolBudget.leaseFrom(getExecutor(), this, _acceptors.length);

// default the local authority if unset by connector implementation or user
if (_localAuthority == null)
setLocalAuthority(new HostPort("localhost"));

super.doStart();

_stopping = new CountDownLatch(_acceptors.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.concurrent.Future;

import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.thread.Scheduler;
Expand Down Expand Up @@ -83,9 +82,6 @@ public int getLocalPort()
protected void doStart() throws Exception
{
open();
// define a default local authority if unspecified by user
if (getLocalAuthority() == null)
setLocalAuthority(new HostPort(getLocalName(), getLocalPort()));
super.doStart();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public interface Connector extends LifeCycle, Container, Graceful
String getName();

/**
* Get the Local Authority of the connection
*
* @return Returns the connection local authority (name/port).
*/
HostPort getLocalAuthority();
Expand All @@ -113,7 +115,22 @@ public interface Connector extends LifeCycle, Container, Graceful
* Specify the connection local authority (name/port) 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
* @param authority the full authority including host and port, or null to reset to default behavior
*/
void setLocalAuthority(HostPort authority);

/**
* Get the optional Server URI authority default
*
* @return Returns the connection server authority (name/port).
*/
HostPort getServerUriAuthority();

/**
* Specify the connection server uri authority (name/port) used within application API layer
* when identifying the server host name/port of a connected endpoint.
*
* @param authority the authority host (and optional port), or null to reset to default behavior
*/
void setServerUriAuthority(HostPort authority);
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ public EndPoint getEndPoint()
*
* @return the local name if overridden, or the local address, or
* null in the case of no local address (usually seen in connectors not based on IP networking).
*/
public String getLocalName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.ByteArrayOutputStream2;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.Scheduler;

Expand Down Expand Up @@ -222,14 +221,6 @@ protected void accept(int acceptorID) throws IOException, InterruptedException
connection.onOpen();
}

@Override
protected void doStart() throws Exception
{
// Change the default authority name here so that it's obvious during testing
setLocalAuthority(new HostPort("connector.local."));
super.doStart();
}

/**
* Get a single response using a parser to search for the end of the message.
*
Expand Down
24 changes: 24 additions & 0 deletions jetty-server/src/main/java/org/eclipse/jetty/server/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,19 @@ private String findServerName()
}
}

if (_channel != null)
{
Connector connector = _channel.getConnector();
if (connector != null)
{
HostPort serverAuth = connector.getServerUriAuthority();
if (serverAuth != null)
{
return formatAddrOrHost(serverAuth.getHost());
}
}
}

// Return host from connection
String name = getLocalName();
if (name != null)
Expand Down Expand Up @@ -1457,6 +1470,17 @@ private int findServerPort()
}
}

if (_channel != null)
{
Connector connector = _channel.getConnector();
if (connector != null)
{
HostPort serverAuth = connector.getServerUriAuthority();
if (serverAuth != null)
return serverAuth.getPort();
}
}

// Return host from connection
return getLocalPort();
}
Expand Down
Loading

0 comments on commit c467d8f

Please sign in to comment.