@@ -77,9 +77,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
77
77
78
78
private static final String HTTP_PATTERN = "(?i)(http|https):" ;
79
79
80
- private static final String USERINFO_PATTERN = "([^@ /?#]*)" ;
80
+ private static final String USERINFO_PATTERN = "([^/?#]*)" ;
81
81
82
- private static final String HOST_IPV4_PATTERN = "[^\\ [ /?#:]*" ;
82
+ private static final String HOST_IPV4_PATTERN = "[^/?#:]*" ;
83
83
84
84
private static final String HOST_IPV6_PATTERN = "\\ [[\\ p{XDigit}:.]*[%\\ p{Alnum}]*]" ;
85
85
@@ -252,9 +252,7 @@ public static UriComponentsBuilder fromUriString(String uri) {
252
252
builder .schemeSpecificPart (ssp );
253
253
}
254
254
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 );
258
256
builder .userInfo (userInfo );
259
257
builder .host (host );
260
258
if (StringUtils .hasLength (port )) {
@@ -296,9 +294,7 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
296
294
builder .scheme (scheme != null ? scheme .toLowerCase () : null );
297
295
builder .userInfo (matcher .group (4 ));
298
296
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 );
302
298
builder .host (host );
303
299
String port = matcher .group (7 );
304
300
if (StringUtils .hasLength (port )) {
@@ -317,6 +313,15 @@ public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
317
313
}
318
314
}
319
315
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
+
320
325
/**
321
326
* Create a new {@code UriComponents} object from the URI associated with
322
327
* the given HttpRequest while also overlaying with values from the headers
@@ -402,6 +407,7 @@ public static UriComponentsBuilder fromOriginHeader(String origin) {
402
407
if (StringUtils .hasLength (port )) {
403
408
builder .port (port );
404
409
}
410
+ checkSchemeAndHost (origin , scheme , host );
405
411
return builder ;
406
412
}
407
413
else {
0 commit comments