Skip to content

Commit 297cbae

Browse files
committed
Extract reusable checkSchemeAndPort method
Closes gh-32440
1 parent 274fba4 commit 297cbae

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
7777

7878
private static final String HTTP_PATTERN = "(?i)(http|https):";
7979

80-
private static final String USERINFO_PATTERN = "([^@/?#]*)";
80+
private static final String USERINFO_PATTERN = "([^/?#]*)";
8181

82-
private static final String HOST_IPV4_PATTERN = "[^\\[/?#:]*";
82+
private static final String HOST_IPV4_PATTERN = "[^/?#:]*";
8383

8484
private static final String HOST_IPV6_PATTERN = "\\[[\\p{XDigit}:.]*[%\\p{Alnum}]*]";
8585

@@ -252,9 +252,7 @@ public static UriComponentsBuilder fromUriString(String uri) {
252252
builder.schemeSpecificPart(ssp);
253253
}
254254
else {
255-
if (StringUtils.hasLength(scheme) && scheme.startsWith("http") && !StringUtils.hasLength(host)) {
256-
throw new IllegalArgumentException("[" + uri + "] is not a valid HTTP URL");
257-
}
255+
checkSchemeAndHost(uri, scheme, host);
258256
builder.userInfo(userInfo);
259257
builder.host(host);
260258
if (StringUtils.hasLength(port)) {
@@ -296,9 +294,7 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
296294
builder.scheme(scheme != null ? scheme.toLowerCase() : null);
297295
builder.userInfo(matcher.group(4));
298296
String host = matcher.group(5);
299-
if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
300-
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
301-
}
297+
checkSchemeAndHost(httpUrl, scheme, host);
302298
builder.host(host);
303299
String port = matcher.group(7);
304300
if (StringUtils.hasLength(port)) {
@@ -317,6 +313,15 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
317313
}
318314
}
319315

316+
private static void checkSchemeAndHost(String uri, @Nullable String scheme, @Nullable String host) {
317+
if (StringUtils.hasLength(scheme) && scheme.startsWith("http") && !StringUtils.hasLength(host)) {
318+
throw new IllegalArgumentException("[" + uri + "] is not a valid HTTP URL");
319+
}
320+
if (StringUtils.hasLength(host) && host.startsWith("[") && !host.endsWith("]")) {
321+
throw new IllegalArgumentException("Invalid IPV6 host in [" + uri + "]");
322+
}
323+
}
324+
320325
/**
321326
* Create a new {@code UriComponents} object from the URI associated with
322327
* the given HttpRequest while also overlaying with values from the headers
@@ -402,6 +407,7 @@ public static UriComponentsBuilder fromOriginHeader(String origin) {
402407
if (StringUtils.hasLength(port)) {
403408
builder.port(port);
404409
}
410+
checkSchemeAndHost(origin, scheme, host);
405411
return builder;
406412
}
407413
else {

0 commit comments

Comments
 (0)