Skip to content

Commit

Permalink
Prefer local hostAddress in ReactorServerHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Feb 15, 2023
1 parent 569df6e commit 5847454
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,6 +80,12 @@ 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);
}

CharSequence charSequence = request.requestHeaders().get(HttpHeaderNames.HOST);
if (charSequence != null) {
String header = charSequence.toString();
Expand All @@ -103,12 +109,8 @@ private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxExc
return new URI(scheme, header, null, null);
}
}
else {
InetSocketAddress localAddress = request.hostAddress();
Assert.state(localAddress != null, "No host address available");
return new URI(scheme, null, localAddress.getHostString(),
localAddress.getPort(), null, null, null);
}

throw new IllegalStateException("Neither local hostAddress nor HOST header available");
}

private static String getScheme(HttpServerRequest request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,6 +79,12 @@ 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;
Expand All @@ -101,12 +107,8 @@ private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxExc
return new URI(scheme, header, null, null);
}
}
else {
InetSocketAddress localAddress = request.hostAddress();
Assert.state(localAddress != null, "No host address available");
return new URI(scheme, null, localAddress.getHostString(),
localAddress.getPort(), null, null, null);
}

throw new IllegalStateException("Neither local hostAddress nor HOST header available");
}

private static String getScheme(HttpServerRequest request) {
Expand Down

0 comments on commit 5847454

Please sign in to comment.