Skip to content

Commit

Permalink
Prefer request hostName and hostPort in ReactorServerHttpRequest
Browse files Browse the repository at this point in the history
- Prefer request hostName and hostPort in ReactorServerHttpRequest#resolveBaseUrl
- The request hostName and hostPort are derived from the Host/X-Forwarded-*/Forwarded header associated with this request.
- Do not add the port when it is the default one

Closes spring-projectsgh-30033
  • Loading branch information
violetagg committed Mar 2, 2023
1 parent 244c979 commit af8de1f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 31 deletions.
2 changes: 1 addition & 1 deletion framework-platform/framework-platform.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,10 @@ private static URI initUri(HttpServerRequest request) throws URISyntaxException

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") && (port != 80)) || (scheme.equals("https") && (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) {
Expand Down

0 comments on commit af8de1f

Please sign in to comment.