-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
#7283
Changes from 6 commits
603feae
d802ea0
e2ceb36
52f144f
9724765
18eadf2
8b41067
e18861a
855dbf2
8880a07
f0c188f
387a24f
ec0e70a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor | |
private MetaData.Response _committedMetaData; | ||
private RequestLog _requestLog; | ||
private long _oldIdleTimeout; | ||
private String overriddenLocalName; | ||
joakime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Bytes written after interception (eg after compression) | ||
|
@@ -298,6 +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 AbstractConnector#setLocalAuthority(HostPort)} 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not keep the |
||
{ | ||
Connector connector = getConnector(); | ||
|
||
if (connector instanceof AbstractConnector) | ||
{ | ||
HostPort localAuthority = ((AbstractConnector)connector).getLocalAuthority(); | ||
if (localAuthority != null) | ||
return localAuthority.getHost(); | ||
} | ||
|
||
InetSocketAddress local = getLocalAddress(); | ||
if (local != null) | ||
return local.getHostString(); | ||
|
||
Comment on lines
+325
to
+328
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should never fall through to here. If there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above about this feature not being NetworkConnector specific. |
||
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 AbstractConnector#setLocalAuthority(HostPort)} 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() | ||
{ | ||
Connector connector = getConnector(); | ||
|
||
if (connector instanceof AbstractConnector) | ||
joakime marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
HostPort localAuthority = ((AbstractConnector)connector).getLocalAuthority(); | ||
if (localAuthority != null) | ||
return localAuthority.getPort(); | ||
} | ||
|
||
InetSocketAddress local = getLocalAddress(); | ||
return local == null ? 0 : local.getPort(); | ||
} | ||
|
||
public InetSocketAddress getLocalAddress() | ||
{ | ||
return _endPoint.getLocalAddress(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1025,22 +1025,11 @@ 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 | ||
{ | ||
String name = InetAddress.getLocalHost().getHostName(); | ||
if (StringUtil.ALL_INTERFACES.equals(name)) | ||
return null; | ||
return formatAddrOrHost(name); | ||
} | ||
catch (UnknownHostException e) | ||
{ | ||
LOG.ignore(e); | ||
} | ||
return null; | ||
} | ||
|
||
|
@@ -1050,10 +1039,16 @@ public String getLocalName() | |
@Override | ||
public int getLocalPort() | ||
{ | ||
if (_channel == null) | ||
return 0; | ||
InetSocketAddress local = _channel.getLocalAddress(); | ||
return local == null ? 0 : local.getPort(); | ||
if (_channel != null) | ||
{ | ||
int localPort = _channel.getLocalPort(); | ||
if (localPort > 0) | ||
return localPort; | ||
InetSocketAddress local = _channel.getLocalAddress(); | ||
if (local != null) | ||
return local.getPort(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not have to do this fall back yet again here. Either is a local authority (so we have a port) or there is not (and we don't) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried this. |
||
} | ||
return 0; | ||
} | ||
|
||
/* | ||
|
@@ -1474,19 +1469,18 @@ private int findServerPort() | |
HttpField host = metadata == null ? null : metadata.getFields().getField(HttpHeader.HOST); | ||
if (host != null) | ||
{ | ||
// TODO is this needed now? | ||
HostPortHttpField authority = (host instanceof HostPortHttpField) | ||
? ((HostPortHttpField)host) | ||
: new HostPortHttpField(host.getValue()); | ||
metadata.getURI().setAuthority(authority.getHost(), authority.getPort()); | ||
return authority.getPort(); | ||
if (authority.getHostPort().hasHost() && authority.getHostPort().hasPort()) | ||
{ | ||
metadata.getURI().setAuthority(authority.getHost(), authority.getPort()); | ||
return authority.getPort(); | ||
} | ||
} | ||
|
||
// Return host from connection | ||
if (_channel != null) | ||
return getLocalPort(); | ||
|
||
return -1; | ||
return getLocalPort(); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should always return a local authority, either one set explicitly or one that is determined dynamically in
doStart
(oropen
).Then the layers about can just ask for the authority and not have to check if null and then look at the address etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also make this a method on
NetworkConnector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a NetworkConnector specific concept.
We have UnixDomainConnector and LocalConnector that can benefit from setLocalAuthority() and are not network connectors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot "always return a local authority".
I tried it.
Going this route breaks expectations in ForwardRequestCustomizer and DetectorConnection and ProxyEndPoint behaviors, so I reverted it.