Prefer request hostName and hostPort in ReactorServerHttpRequest

See gh-30062
This commit is contained in:
Violeta Georgieva 2023-03-02 11:03:39 +02:00 committed by rstoyanchev
parent 4419d56178
commit 682a4d5353
2 changed files with 6 additions and 32 deletions

View File

@ -11,7 +11,7 @@ dependencies {
api(platform("io.micrometer:micrometer-bom:1.10.4"))
api(platform("io.netty:netty-bom:4.1.89.Final"))
api(platform("io.netty:netty5-bom:5.0.0.Alpha5"))
api(platform("io.projectreactor:reactor-bom:2022.0.3"))
api(platform("io.projectreactor:reactor-bom:2022.0.4-SNAPSHOT"))
api(platform("io.rsocket:rsocket-bom:1.1.3"))
api(platform("org.apache.groovy:groovy-bom:4.0.8"))
api(platform("org.apache.logging.log4j:log4j-bom:2.19.0"))

View File

@ -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;
@ -79,36 +78,11 @@ class ReactorServerHttpRequest extends AbstractServerHttpRequest {
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, portIndex + 1, header.length(), 10), 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");
int port = request.hostPort();
return ((scheme.equals("http") || scheme.equals("ws")) && (port != 80)) ||
((scheme.equals("https") || scheme.equals("wss")) && (port != 443)) ?
new URI(scheme, null, request.hostName(), port, null, null, null) :
new URI(scheme, request.hostName(), null, null);
}
private static String getScheme(HttpServerRequest request) {