Skip to content

Commit 65797d0

Browse files
glqdltrstoyanchev
authored andcommitted
UriComponentsBuilder handles invalid port correctly
See gh-26905
1 parent 5811212 commit 65797d0

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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 = "(\\d*(?:\\{[^/]+?})?)";
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
4040
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
41+
import static org.assertj.core.api.Assertions.fail;
4142

4243
/**
4344
* Unit tests for {@link UriComponentsBuilder}.
@@ -1272,4 +1273,42 @@ void verifyDoubleSlashReplacedWithSingleOne() {
12721273
assertThat(path).isEqualTo("/home/path");
12731274
}
12741275

1276+
1277+
@Test
1278+
void verifyNonNumberPort() {
1279+
UriComponents numberPort = UriComponentsBuilder.fromUriString("http://localhost:8080/path").build();
1280+
assertThat(numberPort.getScheme()).isEqualTo("http");
1281+
assertThat(numberPort.getHost()).isEqualTo("localhost");
1282+
assertThat(numberPort.getPort()).isEqualTo(8080);
1283+
assertThat(numberPort.getPath()).isEqualTo("/path");
1284+
1285+
UriComponents stringPort = UriComponentsBuilder.fromUriString("http://localhost:port/path").build();
1286+
try{
1287+
stringPort.getPort();
1288+
fail("port must be a number");
1289+
}catch (NumberFormatException e){
1290+
1291+
}
1292+
assertThat(stringPort.getScheme()).isEqualTo("http");
1293+
assertThat(stringPort.getHost()).isEqualTo("localhost");
1294+
assertThat(stringPort.getPath()).isEqualTo("/path");
1295+
1296+
UriComponents httpNumberPort = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/path").build();
1297+
assertThat(httpNumberPort.getScheme()).isEqualTo("http");
1298+
assertThat(httpNumberPort.getPort()).isEqualTo(8080);
1299+
assertThat(httpNumberPort.getHost()).isEqualTo("localhost");
1300+
assertThat(httpNumberPort.getPath()).isEqualTo("/path");
1301+
1302+
UriComponents httpStringPort= UriComponentsBuilder.fromHttpUrl("http://localhost:port/path").build();
1303+
try{
1304+
httpStringPort.getPort();
1305+
fail("port must be a number");
1306+
}catch (NumberFormatException e){
1307+
1308+
}
1309+
assertThat(httpStringPort.getScheme()).isEqualTo("http");
1310+
assertThat(httpStringPort.getHost()).isEqualTo("localhost");
1311+
assertThat(httpStringPort.getPath()).isEqualTo("/path");
1312+
}
1313+
12751314
}

0 commit comments

Comments
 (0)