diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/ConnectionInfo.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/ConnectionInfo.java index 66b6db10d9..ca716b833c 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/ConnectionInfo.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/ConnectionInfo.java @@ -65,10 +65,12 @@ static ConnectionInfo from(Channel channel, HttpRequest request, boolean secured String header = request.headers().get(HttpHeaderNames.HOST); if (header != null) { hostName = header; - int portIndex = header.charAt(0) == '[' ? header.indexOf(':', header.indexOf(']')) : header.indexOf(':'); - if (portIndex != -1) { - hostName = header.substring(0, portIndex); - hostPort = Integer.parseInt(header.substring(portIndex + 1)); + if (!header.isEmpty()) { + int portIndex = header.charAt(0) == '[' ? header.indexOf(':', header.indexOf(']')) : header.indexOf(':'); + if (portIndex != -1) { + hostName = header.substring(0, portIndex); + hostPort = Integer.parseInt(header.substring(portIndex + 1)); + } } } diff --git a/reactor-netty-http/src/test/java/reactor/netty/http/server/ConnectionInfoTests.java b/reactor-netty-http/src/test/java/reactor/netty/http/server/ConnectionInfoTests.java index ee1da4abc9..6669d7fa07 100644 --- a/reactor-netty-http/src/test/java/reactor/netty/http/server/ConnectionInfoTests.java +++ b/reactor-netty-http/src/test/java/reactor/netty/http/server/ConnectionInfoTests.java @@ -90,6 +90,20 @@ void noHeaders() { }); } + @Test + void noHeadersEmptyHostHeader() { + testClientRequest( + clientRequestHeaders -> clientRequestHeaders.set(HttpHeaderNames.HOST, ""), + serverRequest -> { + Assertions.assertThat(serverRequest.hostAddress().getHostString()) + .containsPattern("^0:0:0:0:0:0:0:1(%\\w*)?|127.0.0.1$"); + Assertions.assertThat(serverRequest.hostAddress().getPort()).isEqualTo(this.disposableServer.port()); + Assertions.assertThat(serverRequest.hostName()).isEmpty(); + int port = serverRequest.scheme().equals("https") ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT; + Assertions.assertThat(serverRequest.hostPort()).isEqualTo(port); + }); + } + @Test void hostHeaderNoForwardedHeaders() { testClientRequest( @@ -130,6 +144,20 @@ void forwardedHost() { }); } + @Test + void forwardedHostEmptyHostHeader() { + testClientRequest( + clientRequestHeaders -> clientRequestHeaders.add("Forwarded", "host=192.168.0.1") + .set(HttpHeaderNames.HOST, ""), + serverRequest -> { + Assertions.assertThat(serverRequest.hostAddress().getHostString()).isEqualTo("192.168.0.1"); + int port = serverRequest.scheme().equals("https") ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT; + Assertions.assertThat(serverRequest.hostAddress().getPort()).isEqualTo(port); + Assertions.assertThat(serverRequest.hostName()).isEqualTo("192.168.0.1"); + Assertions.assertThat(serverRequest.hostPort()).isEqualTo(port); + }); + } + @Test void forwardedHostIpV6() { testClientRequest( @@ -168,6 +196,20 @@ void xForwardedHost() { }); } + @Test + void xForwardedHostEmptyHostHeader() { + testClientRequest( + clientRequestHeaders -> clientRequestHeaders.add("X-Forwarded-Host", + "[1abc:2abc:3abc::5ABC:6abc], 192.168.0.1").set(HttpHeaderNames.HOST, ""), + serverRequest -> { + Assertions.assertThat(serverRequest.hostAddress().getHostString()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); + int port = serverRequest.scheme().equals("https") ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT; + Assertions.assertThat(serverRequest.hostAddress().getPort()).isEqualTo(port); + Assertions.assertThat(serverRequest.hostName()).isEqualTo("1abc:2abc:3abc:0:0:0:5abc:6abc"); + Assertions.assertThat(serverRequest.hostPort()).isEqualTo(port); + }); + } + @Test void xForwardedHostPortIncluded() { testClientRequest(