Skip to content

Commit

Permalink
Merge pull request #11389 from jetty/fix/9.4.x/uriutil-append-scheme-…
Browse files Browse the repository at this point in the history
…host-port-with-websocket

Allow spec port stripping of 80/443 on websocket too.
  • Loading branch information
joakime committed Feb 8, 2024
2 parents eef4a91 + 603907c commit ede74bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions jetty-util/src/test/java/org/eclipse/jetty/util/URIUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,43 @@ public void testEncodeDecodeVisibleOnly()
String decoded = URIUtil.decodePath(encoded);
assertEquals(path, decoded);
}

public static Stream<Arguments> 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());
}
}

0 comments on commit ede74bf

Please sign in to comment.