From 603907cecddd78d0736b9d9cf7d96d79cf521912 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 7 Feb 2024 14:57:00 -0600 Subject: [PATCH] Allow spec port stripping of 80/443 on websocket too. Signed-off-by: Joakim Erdfelt --- .../java/org/eclipse/jetty/util/URIUtil.java | 4 ++ .../org/eclipse/jetty/util/URIUtilTest.java | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java index 92698fac8095..cf42987f6bff 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java @@ -1175,11 +1175,13 @@ public static void appendSchemeHostPort(StringBuilder url, String scheme, String { switch (scheme) { + case "ws": case "http": if (port != 80) url.append(':').append(port); break; + case "wss": case "https": if (port != 443) url.append(':').append(port); @@ -1209,11 +1211,13 @@ public static void appendSchemeHostPort(StringBuffer url, String scheme, String { switch (scheme) { + case "ws": case "http": if (port != 80) url.append(':').append(port); break; + case "wss": case "https": if (port != 443) url.append(':').append(port); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java index 5036ad35ad32..3e7eb8b84111 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java @@ -756,4 +756,43 @@ public void testEncodeDecodeVisibleOnly() String decoded = URIUtil.decodePath(encoded); assertEquals(path, decoded); } + + public static Stream appendSchemeHostPortCases() + { + return Stream.of( + // Default behaviors of stripping a port number based on scheme + Arguments.of("http", "example.org", 80, "http://example.org"), + Arguments.of("https", "example.org", 443, "https://example.org"), + Arguments.of("ws", "example.org", 80, "ws://example.org"), + Arguments.of("wss", "example.org", 443, "wss://example.org"), + // Mismatches between scheme and port + Arguments.of("http", "example.org", 443, "http://example.org:443"), + Arguments.of("https", "example.org", 80, "https://example.org:80"), + Arguments.of("ws", "example.org", 443, "ws://example.org:443"), + Arguments.of("wss", "example.org", 80, "wss://example.org:80"), + // Odd ports + Arguments.of("http", "example.org", 12345, "http://example.org:12345"), + Arguments.of("https", "example.org", 54321, "https://example.org:54321"), + Arguments.of("ws", "example.org", 6666, "ws://example.org:6666"), + Arguments.of("wss", "example.org", 7777, "wss://example.org:7777") + ); + } + + @ParameterizedTest + @MethodSource("appendSchemeHostPortCases") + public void testAppendSchemeHostPortBuilder(String scheme, String server, int port, String expectedStr) + { + StringBuilder actual = new StringBuilder(); + URIUtil.appendSchemeHostPort(actual, scheme, server, port); + assertEquals(expectedStr, actual.toString()); + } + + @ParameterizedTest + @MethodSource("appendSchemeHostPortCases") + public void testAppendSchemeHostPortBuffer(String scheme, String server, int port, String expectedStr) + { + StringBuffer actual = new StringBuffer(); + URIUtil.appendSchemeHostPort(actual, scheme, server, port); + assertEquals(expectedStr, actual.toString()); + } }