From 8b2badf44f9b43a4ff247ada81f93e486ea9dfbf Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 27 Feb 2017 17:56:42 +0100 Subject: [PATCH] Only add port to HOST header value if needed Motivation: We only need to add the port to the HOST header value if its not a standard port. Modifications: - Only add port if needed. - Fix parsing of ipv6 address which is enclosed by []. Result: Fixes [#6426]. --- .../websocketx/WebSocketClientHandshaker.java | 26 +++++++++- .../WebSocketClientHandshaker00.java | 2 +- .../WebSocketClientHandshaker07.java | 2 +- .../WebSocketClientHandshaker08.java | 2 +- .../WebSocketClientHandshaker13.java | 2 +- .../WebSocketClientHandshakerTest.java | 31 ++++++++++++ .../src/main/java/io/netty/util/NetUtil.java | 47 +++++++++++++++++++ 7 files changed, 107 insertions(+), 5 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java index a168306674..7e68da4a74 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java @@ -31,9 +31,11 @@ import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpRequestEncoder; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponseDecoder; +import io.netty.util.NetUtil; import io.netty.util.ReferenceCountUtil; import io.netty.util.internal.ThrowableUtil; +import java.net.InetSocketAddress; import java.net.URI; import java.nio.channels.ClosedChannelException; @@ -451,13 +453,35 @@ public abstract class WebSocketClientHandshaker { return wsPort; } + static CharSequence websocketHostValue(URI wsURL) { + int port = wsURL.getPort(); + if (port == -1) { + return wsURL.getHost(); + } + String host = wsURL.getHost(); + if (port == 80) { + return "http".equals(wsURL.getScheme()) + || "ws".equals(wsURL.getScheme()) ? + host : NetUtil.toSocketAddressString(host, 80); + } + if (port == 443) { + return "https".equals(wsURL.getScheme()) + || "wss".equals(wsURL.getScheme()) ? + host : NetUtil.toSocketAddressString(host, 443); + } + + // if the port is not standard (80/443) its needed to add the port to the header. + // See http://tools.ietf.org/html/rfc6454#section-6.2 + return NetUtil.toSocketAddressString(InetSocketAddress.createUnresolved(host, port)); + } + static CharSequence websocketOriginValue(String host, int wsPort) { String originValue = (wsPort == 443 ? "https" : "http") + "://" + host; if (wsPort != 80 && wsPort != 443) { // if the port is not standard (80/443) its needed to add the port to the header. // See http://tools.ietf.org/html/rfc6454#section-6.2 - return originValue + ':' + wsPort; + return NetUtil.toSocketAddressString(originValue, wsPort); } return originValue; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java index 018f518b36..65621a7114 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java @@ -132,7 +132,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { HttpHeaders headers = request.headers(); headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET) .add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE) - .add(HttpHeaders.Names.HOST, host) + .add(HttpHeaders.Names.HOST, websocketHostValue(wsURL)) .add(HttpHeaders.Names.ORIGIN, websocketOriginValue(host, wsPort)) .add(HttpHeaders.Names.SEC_WEBSOCKET_KEY1, key1) .add(HttpHeaders.Names.SEC_WEBSOCKET_KEY2, key2); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java index e9d8dcbc62..09f05fbee9 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java @@ -117,7 +117,7 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET) .add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE) .add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key) - .add(HttpHeaders.Names.HOST, host) + .add(HttpHeaders.Names.HOST, websocketHostValue(wsURL)) .add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(host, wsPort)); String expectedSubprotocol = expectedSubprotocol(); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java index dba717619c..c728276cfd 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java @@ -117,7 +117,7 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET) .add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE) .add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key) - .add(HttpHeaders.Names.HOST, host) + .add(HttpHeaders.Names.HOST, websocketHostValue(wsURL)) .add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(host, wsPort)); String expectedSubprotocol = expectedSubprotocol(); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java index 264fdd49d2..17ba075fab 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java @@ -116,7 +116,7 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { headers.add(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET) .add(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE) .add(HttpHeaders.Names.SEC_WEBSOCKET_KEY, key) - .add(HttpHeaders.Names.HOST, host + ':' + wsPort) + .add(HttpHeaders.Names.HOST, websocketHostValue(wsURL)) .add(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(host, wsPort)); String expectedSubprotocol = expectedSubprotocol(); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshakerTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshakerTest.java index e7e1e24367..d8fd6fb87f 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshakerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshakerTest.java @@ -40,6 +40,37 @@ import static org.junit.Assert.assertTrue; public abstract class WebSocketClientHandshakerTest { protected abstract WebSocketClientHandshaker newHandshaker(URI uri); + @Test + public void testHostHeader() { + testHostHeaderDefaultHttp(URI.create("ws://localhost:80/"), "localhost"); + testHostHeaderDefaultHttp(URI.create("http://localhost:80/"), "localhost"); + testHostHeaderDefaultHttp(URI.create("ws://[::1]:80/"), "[::1]"); + testHostHeaderDefaultHttp(URI.create("http://[::1]:80/"), "[::1]"); + testHostHeaderDefaultHttp(URI.create("ws://localhost:9999/"), "localhost:9999"); + testHostHeaderDefaultHttp(URI.create("http://localhost:9999/"), "localhost:9999"); + testHostHeaderDefaultHttp(URI.create("ws://[::1]:9999/"), "[::1]:9999"); + testHostHeaderDefaultHttp(URI.create("http://[::1]:9999/"), "[::1]:9999"); + + testHostHeaderDefaultHttp(URI.create("wss://localhost:443/"), "localhost"); + testHostHeaderDefaultHttp(URI.create("https://localhost:443/"), "localhost"); + testHostHeaderDefaultHttp(URI.create("wss://[::1]:443/"), "[::1]"); + testHostHeaderDefaultHttp(URI.create("https://[::1]:443/"), "[::1]"); + testHostHeaderDefaultHttp(URI.create("wss://localhost:9999/"), "localhost:9999"); + testHostHeaderDefaultHttp(URI.create("https://localhost:9999/"), "localhost:9999"); + testHostHeaderDefaultHttp(URI.create("wss://[::1]:9999/"), "[::1]:9999"); + testHostHeaderDefaultHttp(URI.create("https://[::1]:9999/"), "[::1]:9999"); + } + + private void testHostHeaderDefaultHttp(URI uri, String expected) { + WebSocketClientHandshaker handshaker = newHandshaker(uri); + FullHttpRequest request = handshaker.newHandshakeRequest(); + try { + assertEquals(expected, request.headers().get(HttpHeaders.Names.HOST)); + } finally { + request.release(); + } + } + @Test public void testRawPath() { URI uri = URI.create("ws://localhost:9999/path%20with%20ws"); diff --git a/common/src/main/java/io/netty/util/NetUtil.java b/common/src/main/java/io/netty/util/NetUtil.java index ed48665f42..581d79b33d 100644 --- a/common/src/main/java/io/netty/util/NetUtil.java +++ b/common/src/main/java/io/netty/util/NetUtil.java @@ -26,6 +26,7 @@ import java.io.FileReader; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; @@ -870,6 +871,52 @@ public final class NetUtil { } /** + * Returns the {@link String} representation of an {@link InetSocketAddress}. + *

+ * The output does not include Scope ID. + * @param addr {@link InetSocketAddress} to be converted to an address string + * @return {@code String} containing the text-formatted IP address + */ + public static String toSocketAddressString(InetSocketAddress addr) { + String port = String.valueOf(addr.getPort()); + final StringBuilder sb; + + if (addr.isUnresolved()) { + String hostString = PlatformDependent.javaVersion() >= 7 ? addr.getHostString() : addr.getHostName(); + sb = newSocketAddressStringBuilder(hostString, port, !isValidIpV6Address(hostString)); + } else { + InetAddress address = addr.getAddress(); + String hostString = toAddressString(address); + sb = newSocketAddressStringBuilder(hostString, port, address instanceof Inet4Address); + } + return sb.append(':').append(port).toString(); + } + + /** + * Returns the {@link String} representation of a host port combo. + */ + public static String toSocketAddressString(String host, int port) { + String portStr = String.valueOf(port); + return newSocketAddressStringBuilder( + host, portStr, isValidIpV4Address(host)).append(portStr).toString(); + } + + private static StringBuilder newSocketAddressStringBuilder(String host, String port, boolean ipv4) { + int hostLen = host.length(); + if (ipv4) { + // Need to include enough space for hostString:port. + return new StringBuilder(hostLen + 1 + port.length()).append(host); + } + // Need to include enough space for [hostString]:port. + StringBuilder stringBuilder = new StringBuilder(hostLen + 3 + port.length()); + if (hostLen > 1 && host.charAt(0) == '[' && host.charAt(hostLen - 1) == ']') { + return stringBuilder.append(host); + } + return stringBuilder.append('[').append(host).append(']'); + } + + /** +>>>>>>> 0514b0c... Only add port to HOST header value if needed * Returns the {@link String} representation of an {@link InetAddress}. *