From d639f557641b8c13601c23e8f494950a9f7aa348 Mon Sep 17 00:00:00 2001 From: Stephane Landelle Date: Tue, 10 Mar 2020 15:17:22 +0100 Subject: [PATCH] Fix WebSocketClientHandshaker not generating correct handshake request when path is empty (#10095) Motivation: WebSocketClientHandshaker#upgradeUrl doesn't comperly compute relative url when path is empty and produces url such as `?access_token=foo` instead of `/?access_token=foo`. Modifications: * fix WebSocketClientHandshaker#upgradeUrl * add tests for urls without path, with and without query Result: WebSocketClientHandshaker properly connects to url without path. --- .../websocketx/WebSocketClientHandshaker.java | 7 ++---- .../WebSocketClientHandshakerTest.java | 24 +++++++++++++++++++ 2 files changed, 26 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 d9bdd93f11..570689dc79 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 @@ -546,12 +546,9 @@ public abstract class WebSocketClientHandshaker { } String path = wsURL.getRawPath(); + path = path == null || path.isEmpty() ? "/" : path; String query = wsURL.getRawQuery(); - if (query != null && !query.isEmpty()) { - path = path + '?' + query; - } - - return path == null || path.isEmpty() ? "/" : path; + return query != null && !query.isEmpty() ? path + '?' + query : path; } static CharSequence websocketHostValue(URI wsURL) { 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 3cf81435a7..327439d678 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 @@ -217,6 +217,30 @@ public abstract class WebSocketClientHandshakerTest { } } + @Test + public void testUpgradeUrlWithoutPath() { + URI uri = URI.create("ws://localhost:9999"); + WebSocketClientHandshaker handshaker = newHandshaker(uri); + FullHttpRequest request = handshaker.newHandshakeRequest(); + try { + assertEquals("/", request.uri()); + } finally { + request.release(); + } + } + + @Test + public void testUpgradeUrlWithoutPathWithQuery() { + URI uri = URI.create("ws://localhost:9999?a=b%20c"); + WebSocketClientHandshaker handshaker = newHandshaker(uri); + FullHttpRequest request = handshaker.newHandshakeRequest(); + try { + assertEquals("/?a=b%20c", request.uri()); + } finally { + request.release(); + } + } + @Test public void testAbsoluteUpgradeUrlWithQuery() { URI uri = URI.create("ws://localhost:9999/path%20with%20ws?a=b%20c");