Skip to content

Commit 5cac619

Browse files
committed
Fix host+port resolution in ReactorServerHttpRequest
Prior to this commit, the host+port information of the incoming request where taken from the remoteAddress, which is actually the socket address of the HTTP client (i.e. not the server). This commit resolves this information from the "Host" HTTP request header, if available, or the actual local address of the channel if no header is available. Issue: SPR-15084
1 parent 6d6d772 commit 5cac619

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URI;
2121
import java.net.URISyntaxException;
2222

23+
import io.netty.handler.codec.http.HttpHeaderNames;
2324
import io.netty.handler.codec.http.cookie.Cookie;
2425
import reactor.core.publisher.Flux;
2526
import reactor.ipc.netty.http.server.HttpServerRequest;
@@ -55,16 +56,39 @@ public ReactorServerHttpRequest(HttpServerRequest request, NettyDataBufferFactor
5556
this.bufferFactory = bufferFactory;
5657
}
5758

58-
private static URI initUri(HttpServerRequest channel) throws URISyntaxException {
59-
Assert.notNull(channel, "'channel' must not be null");
60-
InetSocketAddress address = channel.remoteAddress();
61-
String requestUri = channel.uri();
62-
return (address != null ? createUrl(address, requestUri) : new URI(requestUri));
59+
private static URI initUri(HttpServerRequest request) throws URISyntaxException {
60+
Assert.notNull(request, "'request' must not be null");
61+
URI baseUri = resolveBaseUrl(request);
62+
String requestUri = request.uri();
63+
return (baseUri != null ? new URI(baseUri.toString() + requestUri) : new URI(requestUri));
6364
}
6465

65-
private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException {
66-
URI baseUrl = new URI(null, null, address.getHostString(), address.getPort(), null, null, null);
67-
return new URI(baseUrl.toString() + requestUri);
66+
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
67+
String header = request.requestHeaders().get(HttpHeaderNames.HOST);
68+
if (header != null) {
69+
final int portIndex;
70+
if (header.startsWith("[")) {
71+
portIndex = header.indexOf(':', header.indexOf(']'));
72+
} else {
73+
portIndex = header.indexOf(':');
74+
}
75+
if (portIndex != -1) {
76+
try {
77+
return new URI(null, null, header.substring(0, portIndex),
78+
Integer.parseInt(header.substring(portIndex + 1)), null, null, null);
79+
} catch (NumberFormatException ignore) {
80+
throw new URISyntaxException(header, "Unable to parse port", portIndex);
81+
}
82+
}
83+
else {
84+
return new URI(null, header, null, null);
85+
}
86+
}
87+
else {
88+
InetSocketAddress localAddress = (InetSocketAddress) request.context().channel().localAddress();
89+
return new URI(null, null, localAddress.getHostString(),
90+
localAddress.getPort(), null, null, null);
91+
}
6892
}
6993

7094
private static HttpHeaders initHeaders(HttpServerRequest channel) {

0 commit comments

Comments
 (0)