Skip to content

Commit 6c62965

Browse files
committed
Remove regular expressions from UriComponentsBuilder
See gh-33639
1 parent bbb53d0 commit 6c62965

File tree

5 files changed

+14
-52
lines changed

5 files changed

+14
-52
lines changed

Diff for: spring-web/src/main/java/org/springframework/web/cors/CorsUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public static boolean isCorsRequest(HttpServletRequest request) {
4343
if (origin == null) {
4444
return false;
4545
}
46-
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
46+
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build();
4747
String scheme = request.getScheme();
4848
String host = request.getServerName();
4949
int port = request.getServerPort();

Diff for: spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,7 +82,7 @@ public static boolean isSameOrigin(ServerHttpRequest request) {
8282
Assert.notNull(actualHost, "Actual request host must not be null");
8383
Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");
8484

85-
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
85+
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build();
8686
return (actualScheme.equals(originUrl.getScheme()) &&
8787
actualHost.equals(originUrl.getHost()) &&
8888
actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));

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

+6-46
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,8 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
7171

7272
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
7373

74-
private static final String SCHEME_PATTERN = "([^:/?#\\\\]+):";
75-
76-
private static final String USERINFO_PATTERN = "([^/?#\\\\]*)";
77-
78-
private static final String HOST_IPV4_PATTERN = "[^/?#:\\\\]*";
79-
80-
private static final String HOST_IPV6_PATTERN = "\\[[\\p{XDigit}:.]*[%\\p{Alnum}]*]";
81-
82-
private static final String HOST_PATTERN = "(" + HOST_IPV6_PATTERN + "|" + HOST_IPV4_PATTERN + ")";
83-
84-
private static final String PORT_PATTERN = "(\\{[^}]+\\}?|[^/?#\\\\]*)";
85-
86-
private static final String PATH_PATTERN = "([^?#]*)";
87-
88-
private static final String QUERY_PATTERN = "([^#]*)";
89-
90-
private static final String LAST_PATTERN = "(.*)";
91-
92-
// Regex patterns that matches URIs. See RFC 3986, appendix B
93-
private static final Pattern URI_PATTERN = Pattern.compile(
94-
"^(" + SCHEME_PATTERN + ")?" + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN +
95-
")?" + ")?" + PATH_PATTERN + "(\\?" + QUERY_PATTERN + ")?" + "(#" + LAST_PATTERN + ")?");
96-
9774
private static final Object[] EMPTY_VALUES = new Object[0];
9875

99-
private static final WhatWgUrlParser.UrlRecord EMPTY_URL_RECORD = new WhatWgUrlParser.UrlRecord();
100-
101-
10276
@Nullable
10377
private String scheme;
10478

@@ -237,7 +211,8 @@ public static UriComponentsBuilder fromUriString(String uri, ParserType parserTy
237211
yield builder.rfcUriRecord(record);
238212
}
239213
case WHAT_WG -> {
240-
WhatWgUrlParser.UrlRecord record = WhatWgUrlParser.parse(uri, EMPTY_URL_RECORD, null, null);
214+
WhatWgUrlParser.UrlRecord record =
215+
WhatWgUrlParser.parse(uri, WhatWgUrlParser.EMPTY_RECORD, null, null);
241216
yield builder.whatWgUrlRecord(record);
242217
}
243218
};
@@ -293,27 +268,12 @@ public static UriComponentsBuilder fromHttpRequest(HttpRequest request) {
293268
/**
294269
* Create an instance by parsing the "Origin" header of an HTTP request.
295270
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454</a>
271+
* @deprecated in favor of {@link UriComponentsBuilder#fromUriString(String)};
272+
* to be removed in 7.0
296273
*/
274+
@Deprecated(since = "6.2", forRemoval = true)
297275
public static UriComponentsBuilder fromOriginHeader(String origin) {
298-
Matcher matcher = URI_PATTERN.matcher(origin);
299-
if (matcher.matches()) {
300-
UriComponentsBuilder builder = new UriComponentsBuilder();
301-
String scheme = matcher.group(2);
302-
String host = matcher.group(6);
303-
String port = matcher.group(8);
304-
if (StringUtils.hasLength(scheme)) {
305-
builder.scheme(scheme);
306-
}
307-
builder.host(host);
308-
if (StringUtils.hasLength(port)) {
309-
builder.port(port);
310-
}
311-
checkSchemeAndHost(origin, scheme, host);
312-
return builder;
313-
}
314-
else {
315-
throw new IllegalArgumentException("[" + origin + "] is not a valid \"Origin\" header value");
316-
}
276+
return fromUriString(origin);
317277
}
318278

319279

Diff for: spring-web/src/main/java/org/springframework/web/util/WebUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -815,7 +815,7 @@ public static boolean isSameOrigin(HttpRequest request) {
815815
port = uri.getPort();
816816
}
817817

818-
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
818+
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build();
819819
return (ObjectUtils.nullSafeEquals(scheme, originUrl.getScheme()) &&
820820
ObjectUtils.nullSafeEquals(host, originUrl.getHost()) &&
821821
getPort(scheme, port) == getPort(originUrl.getScheme(), originUrl.getPort()));

Diff for: spring-web/src/main/java/org/springframework/web/util/WhatWgUrlParser.java

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
*/
6363
final class WhatWgUrlParser {
6464

65+
public static final UrlRecord EMPTY_RECORD = new UrlRecord();
66+
6567
private static final Log logger = LogFactory.getLog(WhatWgUrlParser.class);
6668

6769
private static final int EOF = -1;

0 commit comments

Comments
 (0)