Skip to content

Commit

Permalink
Issue #7277 - ServletRequest.getLocalName() override
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
joakime committed Dec 14, 2021
1 parent c3c2f65 commit 603feae
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ 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 name used to override the connection local name (see ServletRequest.getLocalName()) */
private String _localNameOverride;
private int _acceptorPriorityDelta = -2;
private boolean _accepting = true;
private ThreadPoolBudget.Lease _lease;
Expand Down Expand Up @@ -298,6 +301,24 @@ public int getAcceptors()
return _acceptors.length;
}

/**
* @return Returns the optional local name override
*/
@ManagedAttribute("local name override")
public String getLocalNameOverride()
{
return _localNameOverride;
}

/**
* Optional override of connection local name used within application API layer
* when identifying the local host name of a connected endpoint.
*/
public void setLocalNameOverride(String localNameOverride)
{
this._localNameOverride = localNameOverride;
}

@Override
protected void doStart() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.eclipse.jetty.server;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.EventListener;
Expand Down Expand Up @@ -97,6 +99,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
private MetaData.Response _committedMetaData;
private RequestLog _requestLog;
private long _oldIdleTimeout;
private String overriddenLocalName;

/**
* Bytes written after interception (eg after compression)
Expand Down Expand Up @@ -298,6 +301,64 @@ public EndPoint getEndPoint()
return _endPoint;
}

/**
* <p>
* Override the {@link #getLocalName()} with a specific name.
* </p>
*
* <p>
* This allows the name returned by APIs like HttpServletRequest.getServerName() to be overridden with
* an arbitrary name when behind an intermediary and the incoming request has no valid uri authority.
* </p>
*
* @param overriddenLocalName the overridden local name, use null to set behavior back to default
*/
public void setLocalNameOverride(String overriddenLocalName)
{
this.overriddenLocalName = overriddenLocalName;
}

/**
* <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 #setLocalNameOverride(String)} 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
* the local host name.
*/
public String getLocalName()
{
if (overriddenLocalName != null)
return overriddenLocalName;

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

try
{
String name = InetAddress.getLocalHost().getHostName();
if (StringUtil.ALL_INTERFACES.equals(name))
return null;
return name;
}
catch (UnknownHostException e)
{
LOG.ignore(e);
}
return null;
}

public InetSocketAddress getLocalAddress()
{
return _endPoint.getLocalAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,9 @@ public String getLocalName()
{
if (_channel != null)
{
InetSocketAddress local = _channel.getLocalAddress();
if (local != null)
return formatAddrOrHost(local.getHostString());
String localName = _channel.getLocalName();
if (localName != null)
return formatAddrOrHost(localName);
}

try
Expand Down

0 comments on commit 603feae

Please sign in to comment.