Skip to content

Commit e35fe33

Browse files
committed
Revisit fix for gh-26905 in UriComponentsBuilder
This commit revisits the recently updated `PORT_PATTERN` in `UriComponentsBuilder`. The fix introduced in gh-26905 fails with ambiguous URL patterns, especially when the port and path parts of the pattern are hard to differentiate, for example "https://localhost:{port}{path}". This commit reinstates the previous behavior without undoing the actual fix. The only limitation introduced here is the fact that only a single pattern variable is allowed for the port pattern part. Fixes gh-27039
1 parent 23f396a commit e35fe33

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
8585

8686
private static final String HOST_PATTERN = "(" + HOST_IPV6_PATTERN + "|" + HOST_IPV4_PATTERN + ")";
8787

88-
private static final String PORT_PATTERN = "([^/?#]*)";
88+
private static final String PORT_PATTERN = "(\\{[^}]+}?|[^/?#]*)";
8989

9090
private static final String PATH_PATTERN = "([^?#]*)";
9191

spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,4 +1297,14 @@ void verifyInvalidPort() {
12971297
.isInstanceOf(NumberFormatException.class);
12981298
}
12991299

1300+
@Test // gh-27039
1301+
void expandPortAndPathWithoutSeparator() {
1302+
URI uri = UriComponentsBuilder
1303+
.fromUriString("ws://localhost:{port}{path}")
1304+
.buildAndExpand(7777, "/test")
1305+
.toUri();
1306+
assertThat(uri.toString()).isEqualTo("ws://localhost:7777/test");
1307+
}
1308+
1309+
13001310
}

0 commit comments

Comments
 (0)