Skip to content

Commit 5847454

Browse files
committed
Prefer local hostAddress in ReactorServerHttpRequest
Closes gh-28601
1 parent 569df6e commit 5847454

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorNetty2ServerHttpRequest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,6 +80,12 @@ private static URI initUri(HttpServerRequest request) throws URISyntaxException
8080

8181
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
8282
String scheme = getScheme(request);
83+
84+
InetSocketAddress hostAddress = request.hostAddress();
85+
if (hostAddress != null) {
86+
return new URI(scheme, null, hostAddress.getHostString(), hostAddress.getPort(), null, null, null);
87+
}
88+
8389
CharSequence charSequence = request.requestHeaders().get(HttpHeaderNames.HOST);
8490
if (charSequence != null) {
8591
String header = charSequence.toString();
@@ -103,12 +109,8 @@ private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxExc
103109
return new URI(scheme, header, null, null);
104110
}
105111
}
106-
else {
107-
InetSocketAddress localAddress = request.hostAddress();
108-
Assert.state(localAddress != null, "No host address available");
109-
return new URI(scheme, null, localAddress.getHostString(),
110-
localAddress.getPort(), null, null, null);
111-
}
112+
113+
throw new IllegalStateException("Neither local hostAddress nor HOST header available");
112114
}
113115

114116
private static String getScheme(HttpServerRequest request) {

spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpRequest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,6 +79,12 @@ private static URI initUri(HttpServerRequest request) throws URISyntaxException
7979

8080
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
8181
String scheme = getScheme(request);
82+
83+
InetSocketAddress hostAddress = request.hostAddress();
84+
if (hostAddress != null) {
85+
return new URI(scheme, null, hostAddress.getHostString(), hostAddress.getPort(), null, null, null);
86+
}
87+
8288
String header = request.requestHeaders().get(HttpHeaderNames.HOST);
8389
if (header != null) {
8490
final int portIndex;
@@ -101,12 +107,8 @@ private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxExc
101107
return new URI(scheme, header, null, null);
102108
}
103109
}
104-
else {
105-
InetSocketAddress localAddress = request.hostAddress();
106-
Assert.state(localAddress != null, "No host address available");
107-
return new URI(scheme, null, localAddress.getHostString(),
108-
localAddress.getPort(), null, null, null);
109-
}
110+
111+
throw new IllegalStateException("Neither local hostAddress nor HOST header available");
110112
}
111113

112114
private static String getScheme(HttpServerRequest request) {

0 commit comments

Comments
 (0)