Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #7277 - Allow Request.getLocalName() and .getLocalPort() to be overridden (Split authority approach) #7316

Merged
merged 23 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
603feae
Issue #7277 - ServletRequest.getLocalName() override
joakime Dec 14, 2021
d802ea0
Issue #7277 - Allow Request.getLocalName() to be overridden
joakime Dec 14, 2021
e2ceb36
Issue #7277 - Updating based on review
joakime Dec 14, 2021
52f144f
Issue #7277 - More updates based on review
joakime Dec 14, 2021
9724765
Issue #7277 - Switch from setLocalName to setLocalAuthority per review
joakime Dec 16, 2021
18eadf2
Issue #7277 - Fixing NPE from updated getLocalPort
joakime Dec 16, 2021
8b41067
Issue #7277 - Updates based on review
joakime Dec 17, 2021
c467d8f
Issue #7277 - Split Authority Approach
joakime Dec 20, 2021
b066c8e
Issue #7277 - Fixing null return in Request.getServerName() usage
joakime Dec 20, 2021
a386068
Issue #7277 - Addressing review comments
joakime Dec 20, 2021
a498aea
Issue #7277 - SocketAddress as connector.getLocalAddress
joakime Dec 20, 2021
7490e84
Issue #7277 - Correcting field name
joakime Dec 20, 2021
7ec0008
Issue #7277 - Fixing MavenServerConnector compilation issue
joakime Dec 20, 2021
1e63b96
Issue #7277 - Adding test cases for abs-uri
joakime Dec 22, 2021
b32393a
Issue #7277 - Use delegate in MavenServerConnector
joakime Dec 22, 2021
034128d
Issue #7277 - Remove redundant javadoc
joakime Dec 22, 2021
00f4931
Issue #7277 - Restore AbstractNetworkConnector to original
joakime Dec 22, 2021
0f0d751
Issue #7277 - Improve APIdoc
joakime Dec 22, 2021
42bb5bf
Issue #7277 - revert ServerConnector.localName changes (no longer nee…
joakime Dec 22, 2021
a1a806d
Issue #7277 - Using HttpConfiguration for configuration
joakime Dec 22, 2021
5e085da
Issue #7277 - Removing dead code
joakime Dec 22, 2021
2eaec36
Issue #7277 - Addressing review
joakime Jan 5, 2022
3199438
Issue #7277 - Adding missing null check
joakime Jan 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

package org.eclipse.jetty.maven.plugin;

import java.net.SocketAddress;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

Expand All @@ -29,6 +31,7 @@
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.thread.Scheduler;
Expand All @@ -55,6 +58,8 @@ public class MavenServerConnector extends ContainerLifeCycle implements Connecto
private String name;
private int port;
private long idleTimeout;
private HostPort serverAuthority;
private SocketAddress localAddress;

public MavenServerConnector()
{
Expand Down Expand Up @@ -253,6 +258,43 @@ public String getName()
return this.name;
}

@Override
public SocketAddress getLocalAddress()
{
return this.localAddress;
}

@Override
public void setLocalAddress(SocketAddress localAddress)
{
Objects.requireNonNull(localAddress, "Local Address");

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

this.localAddress = localAddress;
}

@Override
public HostPort getServerAuthority()
{
return this.serverAuthority;
}

@Override
public void setServerAuthority(HostPort authority)
{
if (isStarted())
throw new IllegalStateException(getState());

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

public int getLocalPort()
{
return this.delegate.getLocalPort();
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.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.util.ArrayList;
Expand All @@ -29,6 +30,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 All @@ -42,6 +44,7 @@
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.ssl.SslConnection;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.ProcessorUtils;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
Expand Down Expand Up @@ -161,7 +164,18 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
private long _idleTimeout = 30000;
private String _defaultProtocol;
private ConnectionFactory _defaultConnectionFactory;
/* The name used to link up virtual host configuration to named connectors */
private String _name;
/**
* The authority used to define the server uri authority fallback for the connection
* (see ServletRequest.getServerName(), and ServletRequest.getServerPort()).
*/
private HostPort _serverAuthority;
/**
* The address used to define the local address for the connection
* (see ServletRequest.getLocalName(), ServletRequest.getLocalAddr(), and ServletRequest.getLocalPort()).
*/
private SocketAddress _localAddress;
private int _acceptorPriorityDelta = -2;
private boolean _accepting = true;
private ThreadPoolBudget.Lease _lease;
Expand Down Expand Up @@ -298,6 +312,40 @@ public int getAcceptors()
return _acceptors.length;
}

public SocketAddress getLocalAddress()
{
return _localAddress;
}

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

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

_localAddress = localAddress;
}

@ManagedAttribute("server authority")
public HostPort getServerAuthority()
{
return _serverAuthority;
}

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

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

@Override
protected void doStart() throws Exception
{
Expand All @@ -318,6 +366,7 @@ protected void doStart() throws Exception
}

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

super.doStart();

_stopping = new CountDownLatch(_acceptors.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
@ManagedObject("AbstractNetworkConnector")
public abstract class AbstractNetworkConnector extends AbstractConnector implements NetworkConnector
{

private volatile String _host;
private volatile int _port = 0;

Expand Down Expand Up @@ -74,6 +73,11 @@ public int getLocalPort()
return -1;
}

/**
* @return the local address host name, never null
*/
protected abstract String getLocalName();

@Override
protected void doStart() throws Exception
{
Expand Down
32 changes: 32 additions & 0 deletions jetty-server/src/main/java/org/eclipse/jetty/server/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

package org.eclipse.jetty.server;

import java.net.SocketAddress;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;

import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.handler.ContextHandler;
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.component.Container;
Expand Down Expand Up @@ -102,4 +104,34 @@ public interface Connector extends LifeCycle, Container, Graceful
* @return The connector name or null.
*/
String getName();

/**
* Get the Local Address of the connection
*
* @return Returns the connection local address.
*/
SocketAddress getLocalAddress();

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

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

/**
* 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 setServerAuthority(HostPort authority);
}
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 @@ -298,8 +299,73 @@ public EndPoint getEndPoint()
return _endPoint;
}

/**
* <p>Return the Local Name of the connected channel.</p>
*
* <p>
* This is the name after the connector is bound and the connection is accepted.
* The value is typically the local address and optionally the local host name
* if the local address is unavailable.
* </p>
* <p>
* 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).
* </p>
*
* @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()
{
SocketAddress localAddress = getConnector().getLocalAddress();
if (localAddress != null)
{
if (localAddress instanceof InetSocketAddress)
return ((InetSocketAddress)localAddress).getHostName();
}

InetSocketAddress local = getLocalAddress();
if (local != null)
return local.getHostString();

return null;
}

/**
* <p>Return the Local Port of the connected channel.</p>
*
* <p>
* This is the port the connector is bound to and is accepting connection on.
* </p>
* <p>
* 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).
* </p>
*
* @return the local authority port if overridden, or the local address port, or
* 0 in the case of no local address (usually seen in connectors not based on IP networking).
*/
public int getLocalPort()
{
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
Loading