Skip to content

Commit 381f790

Browse files
committed
Extract reusable checkSchemeAndPort method
Closes gh-32440
1 parent f5a3658 commit 381f790

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
@@ -76,9 +76,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
7676

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

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

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

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

@@ -243,9 +243,7 @@ public static UriComponentsBuilder fromUriString(String uri) {
243243
builder.schemeSpecificPart(ssp);
244244
}
245245
else {
246-
if (StringUtils.hasLength(scheme) && scheme.startsWith("http") && !StringUtils.hasLength(host)) {
247-
throw new IllegalArgumentException("[" + uri + "] is not a valid HTTP URL");
248-
}
246+
checkSchemeAndHost(uri, scheme, host);
249247
builder.userInfo(userInfo);
250248
builder.host(host);
251249
if (StringUtils.hasLength(port)) {
@@ -287,9 +285,7 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
287285
builder.scheme(scheme != null ? scheme.toLowerCase() : null);
288286
builder.userInfo(matcher.group(4));
289287
String host = matcher.group(5);
290-
if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
291-
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
292-
}
288+
checkSchemeAndHost(httpUrl, scheme, host);
293289
builder.host(host);
294290
String port = matcher.group(7);
295291
if (StringUtils.hasLength(port)) {
@@ -308,6 +304,15 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
308304
}
309305
}
310306

307+
private static void checkSchemeAndHost(String uri, @Nullable String scheme, @Nullable String host) {
308+
if (StringUtils.hasLength(scheme) && scheme.startsWith("http") && !StringUtils.hasLength(host)) {
309+
throw new IllegalArgumentException("[" + uri + "] is not a valid HTTP URL");
310+
}
311+
if (StringUtils.hasLength(host) && host.startsWith("[") && !host.endsWith("]")) {
312+
throw new IllegalArgumentException("Invalid IPV6 host in [" + uri + "]");
313+
}
314+
}
315+
311316
/**
312317
* Create a new {@code UriComponents} object from the URI associated with
313318
* the given HttpRequest while also overlaying with values from the headers
@@ -363,6 +368,7 @@ public static UriComponentsBuilder fromOriginHeader(String origin) {
363368
if (StringUtils.hasLength(port)) {
364369
builder.port(port);
365370
}
371+
checkSchemeAndHost(origin, scheme, host);
366372
return builder;
367373
}
368374
else {

0 commit comments

Comments
 (0)