Prefer request hostName and hostPort in ReactorServerHttpRequest
Backport of682a4d53and9624ea39Closes gh-29974
This commit is contained in:
@@ -29,7 +29,7 @@ configure(allprojects) { project ->
|
||||
imports {
|
||||
mavenBom "com.fasterxml.jackson:jackson-bom:2.12.7"
|
||||
mavenBom "io.netty:netty-bom:4.1.89.Final"
|
||||
mavenBom "io.projectreactor:reactor-bom:2020.0.27"
|
||||
mavenBom "io.projectreactor:reactor-bom:2020.0.29"
|
||||
mavenBom "io.r2dbc:r2dbc-bom:Arabba-SR13"
|
||||
mavenBom "io.rsocket:rsocket-bom:1.1.3"
|
||||
mavenBom "org.eclipse.jetty:jetty-bom:9.4.51.v20230217"
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
import javax.net.ssl.SSLSession;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||
import io.netty.handler.codec.http.cookie.Cookie;
|
||||
import io.netty.handler.ssl.SslHandler;
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -80,42 +79,15 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
|
||||
return new URI(resolveBaseUrl(request) + resolveRequestUri(request));
|
||||
}
|
||||
|
||||
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
|
||||
String scheme = getScheme(request);
|
||||
|
||||
InetSocketAddress hostAddress = request.hostAddress();
|
||||
if (hostAddress != null) {
|
||||
return new URI(scheme, null, hostAddress.getHostString(), hostAddress.getPort(), null, null, null);
|
||||
}
|
||||
|
||||
String header = request.requestHeaders().get(HttpHeaderNames.HOST);
|
||||
if (header != null) {
|
||||
final int portIndex;
|
||||
if (header.startsWith("[")) {
|
||||
portIndex = header.indexOf(':', header.indexOf(']'));
|
||||
}
|
||||
else {
|
||||
portIndex = header.indexOf(':');
|
||||
}
|
||||
if (portIndex != -1) {
|
||||
try {
|
||||
return new URI(scheme, null, header.substring(0, portIndex),
|
||||
Integer.parseInt(header.substring(portIndex + 1)), null, null, null);
|
||||
}
|
||||
catch (NumberFormatException ex) {
|
||||
throw new URISyntaxException(header, "Unable to parse port", portIndex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return new URI(scheme, header, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Neither local hostAddress nor HOST header available");
|
||||
private static String resolveBaseUrl(HttpServerRequest request) {
|
||||
String scheme = request.scheme();
|
||||
int port = request.hostPort();
|
||||
return scheme + "://" + request.hostName() + (usePort(scheme, port) ? ":" + port : "");
|
||||
}
|
||||
|
||||
private static String getScheme(HttpServerRequest request) {
|
||||
return request.scheme();
|
||||
private static boolean usePort(String scheme, int port) {
|
||||
return ((scheme.equals("http") || scheme.equals("ws")) && (port != 80)) ||
|
||||
((scheme.equals("https") || scheme.equals("wss")) && (port != 443));
|
||||
}
|
||||
|
||||
private static String resolveRequestUri(HttpServerRequest request) {
|
||||
|
||||
Reference in New Issue
Block a user