Issue #3302 Supporting host:ip in X-Forwarded-For (fixing review remarks)
Signed-off-by: alexey.barsov <alexey.barsov@jetbrains.com>
This commit is contained in:
parent
edf74a6554
commit
803a45b43b
|
@ -31,6 +31,8 @@ import org.eclipse.jetty.http.QuotedCSV;
|
||||||
import org.eclipse.jetty.server.HttpConfiguration.Customizer;
|
import org.eclipse.jetty.server.HttpConfiguration.Customizer;
|
||||||
import org.eclipse.jetty.util.HostPort;
|
import org.eclipse.jetty.util.HostPort;
|
||||||
import org.eclipse.jetty.util.StringUtil;
|
import org.eclipse.jetty.util.StringUtil;
|
||||||
|
import org.eclipse.jetty.util.log.Log;
|
||||||
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
|
@ -55,6 +57,8 @@ import org.eclipse.jetty.util.StringUtil;
|
||||||
*/
|
*/
|
||||||
public class ForwardedRequestCustomizer implements Customizer
|
public class ForwardedRequestCustomizer implements Customizer
|
||||||
{
|
{
|
||||||
|
private static final Logger LOG = Log.getLogger(ForwardedRequestCustomizer.class);
|
||||||
|
|
||||||
private HostPortHttpField _forcedHost;
|
private HostPortHttpField _forcedHost;
|
||||||
private String _forwardedHeader = HttpHeader.FORWARDED.toString();
|
private String _forwardedHeader = HttpHeader.FORWARDED.toString();
|
||||||
private String _forwardedHostHeader = HttpHeader.X_FORWARDED_HOST.toString();
|
private String _forwardedHostHeader = HttpHeader.X_FORWARDED_HOST.toString();
|
||||||
|
@ -298,7 +302,7 @@ public class ForwardedRequestCustomizer implements Customizer
|
||||||
RFC7239 rfc7239 = null;
|
RFC7239 rfc7239 = null;
|
||||||
String forwardedHost = null;
|
String forwardedHost = null;
|
||||||
String forwardedServer = null;
|
String forwardedServer = null;
|
||||||
String forwardedFor = null;
|
HostPort forwardedFor = null;
|
||||||
String forwardedProto = null;
|
String forwardedProto = null;
|
||||||
String forwardedHttps = null;
|
String forwardedHttps = null;
|
||||||
|
|
||||||
|
@ -390,7 +394,7 @@ public class ForwardedRequestCustomizer implements Customizer
|
||||||
}
|
}
|
||||||
else if (forwardedFor != null)
|
else if (forwardedFor != null)
|
||||||
{
|
{
|
||||||
request.setRemoteAddr(InetSocketAddress.createUnresolved(forwardedFor,request.getRemotePort()));
|
request.setRemoteAddr(InetSocketAddress.createUnresolved(forwardedFor.getHost(), (forwardedFor.getPort() > 0) ? forwardedFor.getPort() : request.getRemotePort()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle protocol identifier
|
// handle protocol identifier
|
||||||
|
@ -432,23 +436,24 @@ public class ForwardedRequestCustomizer implements Customizer
|
||||||
return headerValue.substring(0,commaIndex).trim();
|
return headerValue.substring(0,commaIndex).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getRemoteAddr(String headerValue)
|
protected HostPort getRemoteAddr(String headerValue)
|
||||||
{
|
{
|
||||||
String leftMost = getLeftMost(headerValue);
|
String leftMost = getLeftMost(headerValue);
|
||||||
|
if (leftMost == null)
|
||||||
if (leftMost != null && leftMost.contains(":")) {
|
{
|
||||||
try {
|
return null;
|
||||||
HostPort hostPort = new HostPort(leftMost);
|
|
||||||
if (hostPort.getPort() > 0) {
|
|
||||||
// address in a format host:port, return host part only.
|
|
||||||
return hostPort.getHost();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// failed to parse in host[:port] format, fallback to the value resolved from header
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return leftMost;
|
try
|
||||||
|
{
|
||||||
|
return new HostPort(leftMost);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// failed to parse in host[:port] format
|
||||||
|
LOG.ignore(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue