Skip to content

Commit

Permalink
Prefer request hostName and hostPort in ReactorServerHttpRequest
Browse files Browse the repository at this point in the history
Backport of 682a4d5 and 9624ea3

Closes gh-29974
  • Loading branch information
rstoyanchev committed Mar 14, 2023
1 parent d00fd4c commit f0da099
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -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"
Expand Down
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down Expand Up @@ -80,42 +79,15 @@ private static URI initUri(HttpServerRequest request) throws URISyntaxException
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) {
Expand Down

0 comments on commit f0da099

Please sign in to comment.