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
This commit is contained in:
parent
6d6d772f31
commit
5cac619e23
|
@ -20,6 +20,7 @@ import java.net.InetSocketAddress;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
||||||
import io.netty.handler.codec.http.cookie.Cookie;
|
import io.netty.handler.codec.http.cookie.Cookie;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.ipc.netty.http.server.HttpServerRequest;
|
import reactor.ipc.netty.http.server.HttpServerRequest;
|
||||||
|
@ -55,16 +56,39 @@ public class ReactorServerHttpRequest extends AbstractServerHttpRequest {
|
||||||
this.bufferFactory = bufferFactory;
|
this.bufferFactory = bufferFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static URI initUri(HttpServerRequest channel) throws URISyntaxException {
|
private static URI initUri(HttpServerRequest request) throws URISyntaxException {
|
||||||
Assert.notNull(channel, "'channel' must not be null");
|
Assert.notNull(request, "'request' must not be null");
|
||||||
InetSocketAddress address = channel.remoteAddress();
|
URI baseUri = resolveBaseUrl(request);
|
||||||
String requestUri = channel.uri();
|
String requestUri = request.uri();
|
||||||
return (address != null ? createUrl(address, requestUri) : new URI(requestUri));
|
return (baseUri != null ? new URI(baseUri.toString() + requestUri) : new URI(requestUri));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static URI createUrl(InetSocketAddress address, String requestUri) throws URISyntaxException {
|
private static URI resolveBaseUrl(HttpServerRequest request) throws URISyntaxException {
|
||||||
URI baseUrl = new URI(null, null, address.getHostString(), address.getPort(), null, null, null);
|
String header = request.requestHeaders().get(HttpHeaderNames.HOST);
|
||||||
return new URI(baseUrl.toString() + requestUri);
|
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(null, null, header.substring(0, portIndex),
|
||||||
|
Integer.parseInt(header.substring(portIndex + 1)), null, null, null);
|
||||||
|
} catch (NumberFormatException ignore) {
|
||||||
|
throw new URISyntaxException(header, "Unable to parse port", portIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new URI(null, header, null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
InetSocketAddress localAddress = (InetSocketAddress) request.context().channel().localAddress();
|
||||||
|
return new URI(null, null, localAddress.getHostString(),
|
||||||
|
localAddress.getPort(), null, null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpHeaders initHeaders(HttpServerRequest channel) {
|
private static HttpHeaders initHeaders(HttpServerRequest channel) {
|
||||||
|
|
Loading…
Reference in New Issue