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 21 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 @@ -161,6 +161,7 @@ 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;
private int _acceptorPriorityDelta = -2;
private boolean _accepting = true;
Expand Down Expand Up @@ -318,6 +319,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 @@ -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 HttpConfiguration#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 = getHttpConfiguration().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 HttpConfiguration#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 = getHttpConfiguration().getLocalAddress();
if (localAddress instanceof InetSocketAddress)
return ((InetSocketAddress)localAddress).getPort();

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

public InetSocketAddress getLocalAddress()
{
SocketAddress localAddress = getHttpConfiguration().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 @@ -19,13 +19,16 @@
package org.eclipse.jetty.server;

import java.io.IOException;
import java.net.SocketAddress;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.jetty.http.CookieCompliance;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.util.HostPort;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.TreeTrie;
import org.eclipse.jetty.util.Trie;
Expand Down Expand Up @@ -78,6 +81,9 @@ public class HttpConfiguration implements Dumpable
private boolean _notifyRemoteAsyncErrors = true;
private boolean _relativeRedirectAllowed;

private HostPort _serverAuthority;
private SocketAddress _localAddress;

/**
* <p>An interface that allows a request object to be customized
* for a particular HTTP connector configuration. Unlike Filters, customizer are
Expand Down Expand Up @@ -662,6 +668,71 @@ public boolean isRelativeRedirectAllowed()
return _relativeRedirectAllowed;
}

/**
* Get the Local Address of the connection
*
* @return Returns the connection local address.
*/
@ManagedAttribute("Local address override")
public SocketAddress getLocalAddress()
{
return _localAddress;
}

/**
* <p>
* Specify the connection local address used within application API layer
* when identifying the local host name/port of a connected endpoint.
* </p>
* <p>
* This allows an override of higher level APIs, such as
* {@code ServletRequest.getLocalName()}, {@code ServletRequest.getLocalAddr()},
* and {@code ServletRequest.getLocalPort()}.
* </p>
*
* @param localAddress the address to use for host/addr/port, or null to reset to default behavior
*/
public void setLocalAddress(SocketAddress localAddress)
{
Objects.requireNonNull(localAddress, "Local Address");

_localAddress = localAddress;
}

/**
* Get the optional Server URI authority default
*
* @return Returns the connection server authority (name/port).
*/
@ManagedAttribute("Server authority override")
public HostPort getServerAuthority()
{
return _serverAuthority;
}

/**
* <p>
* Specify the connection server uri authority (name/port) used within application API layer
* when identifying the server host name/port of a connected endpoint.
* </p>
*
* <p>
* This allows an override of higher level APIs, such as
* {@code ServletRequest.getServerName()}, and {@code ServletRequest.getServerPort()}.
* </p>
*
* @param authority the authority host (and optional port), or null to reset to default behavior
*/
public void setServerAuthority(HostPort authority)
{
if (authority == null)
_serverAuthority = null;
else if (!authority.hasHost())
throw new IllegalStateException("Server URI Authority must have host declared");
else
_serverAuthority = authority;
}

@Override
public String dump()
{
Expand Down
Loading