Skip to content

Commit 0468ef4

Browse files
committed
Follow-up fix for recent change to PORT_PATTERN
The pattern was changed in 65797d0 to check for characters up until "/", instead of for digits, but now also checks up until "?" and "#". Closes gh-26905
1 parent eb03144 commit 0468ef4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,20 @@ void verifyDoubleSlashReplacedWithSingleOne() {
12731273
assertThat(path).isEqualTo("/home/path");
12741274
}
12751275

1276+
@Test
1277+
void validPort() {
1278+
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567/path").build();
1279+
assertThat(uriComponents.getPort()).isEqualTo(52567);
1280+
assertThat(uriComponents.getPath()).isEqualTo("/path");
1281+
1282+
uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567?trace=false").build();
1283+
assertThat(uriComponents.getPort()).isEqualTo(52567);
1284+
assertThat(uriComponents.getQuery()).isEqualTo("trace=false");
1285+
1286+
uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567#fragment").build();
1287+
assertThat(uriComponents.getPort()).isEqualTo(52567);
1288+
assertThat(uriComponents.getFragment()).isEqualTo("fragment");
1289+
}
12761290

12771291
@Test
12781292
void verifyInvalidPort() {

0 commit comments

Comments
 (0)