@@ -76,9 +76,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
76
76
77
77
private static final String HTTP_PATTERN = "(?i)(http|https):" ;
78
78
79
- private static final String USERINFO_PATTERN = "([^@ /?#]*)" ;
79
+ private static final String USERINFO_PATTERN = "([^/?#]*)" ;
80
80
81
- private static final String HOST_IPV4_PATTERN = "[^\\ [ /?#:]*" ;
81
+ private static final String HOST_IPV4_PATTERN = "[^/?#:]*" ;
82
82
83
83
private static final String HOST_IPV6_PATTERN = "\\ [[\\ p{XDigit}:.]*[%\\ p{Alnum}]*]" ;
84
84
@@ -243,9 +243,7 @@ public static UriComponentsBuilder fromUriString(String uri) {
243
243
builder .schemeSpecificPart (ssp );
244
244
}
245
245
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 );
249
247
builder .userInfo (userInfo );
250
248
builder .host (host );
251
249
if (StringUtils .hasLength (port )) {
@@ -287,9 +285,7 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
287
285
builder .scheme (scheme != null ? scheme .toLowerCase () : null );
288
286
builder .userInfo (matcher .group (4 ));
289
287
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 );
293
289
builder .host (host );
294
290
String port = matcher .group (7 );
295
291
if (StringUtils .hasLength (port )) {
@@ -308,6 +304,15 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
308
304
}
309
305
}
310
306
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
+
311
316
/**
312
317
* Create a new {@code UriComponents} object from the URI associated with
313
318
* the given HttpRequest while also overlaying with values from the headers
@@ -363,6 +368,7 @@ public static UriComponentsBuilder fromOriginHeader(String origin) {
363
368
if (StringUtils .hasLength (port )) {
364
369
builder .port (port );
365
370
}
371
+ checkSchemeAndHost (origin , scheme , host );
366
372
return builder ;
367
373
}
368
374
else {
0 commit comments