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 c9b14a277e5..1f8605ef7f3 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 @@ -1234,11 +1234,13 @@ public final class URIUtil { 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); @@ -1268,11 +1270,13 @@ public final class URIUtil { 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 c882ce4e3ee..a6c41fc3964 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 @@ -788,4 +788,43 @@ public class URIUtilTest { assertFalse(URIUtil.isValidHostRegisteredName(token), "Token [" + token + "] should be an invalid reg-name"); } + + 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()); + } }