Remove regular expressions from UriComponentsBuilder

See gh-33639
This commit is contained in:
rstoyanchev 2024-10-04 15:21:07 +01:00
parent bbb53d03c4
commit 6c62965cbb
5 changed files with 14 additions and 52 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,7 +43,7 @@ public abstract class CorsUtils {
if (origin == null) {
return false;
}
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build();
String scheme = request.getScheme();
String host = request.getServerName();
int port = request.getServerPort();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -82,7 +82,7 @@ public abstract class CorsUtils {
Assert.notNull(actualHost, "Actual request host must not be null");
Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build();
return (actualScheme.equals(originUrl.getScheme()) &&
actualHost.equals(originUrl.getHost()) &&
actualPort == getPort(originUrl.getScheme(), originUrl.getPort()));

View File

@ -71,34 +71,8 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?");
private static final String SCHEME_PATTERN = "([^:/?#\\\\]+):";
private static final String USERINFO_PATTERN = "([^/?#\\\\]*)";
private static final String HOST_IPV4_PATTERN = "[^/?#:\\\\]*";
private static final String HOST_IPV6_PATTERN = "\\[[\\p{XDigit}:.]*[%\\p{Alnum}]*]";
private static final String HOST_PATTERN = "(" + HOST_IPV6_PATTERN + "|" + HOST_IPV4_PATTERN + ")";
private static final String PORT_PATTERN = "(\\{[^}]+\\}?|[^/?#\\\\]*)";
private static final String PATH_PATTERN = "([^?#]*)";
private static final String QUERY_PATTERN = "([^#]*)";
private static final String LAST_PATTERN = "(.*)";
// Regex patterns that matches URIs. See RFC 3986, appendix B
private static final Pattern URI_PATTERN = Pattern.compile(
"^(" + SCHEME_PATTERN + ")?" + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN +
")?" + ")?" + PATH_PATTERN + "(\\?" + QUERY_PATTERN + ")?" + "(#" + LAST_PATTERN + ")?");
private static final Object[] EMPTY_VALUES = new Object[0];
private static final WhatWgUrlParser.UrlRecord EMPTY_URL_RECORD = new WhatWgUrlParser.UrlRecord();
@Nullable
private String scheme;
@ -237,7 +211,8 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
yield builder.rfcUriRecord(record);
}
case WHAT_WG -> {
WhatWgUrlParser.UrlRecord record = WhatWgUrlParser.parse(uri, EMPTY_URL_RECORD, null, null);
WhatWgUrlParser.UrlRecord record =
WhatWgUrlParser.parse(uri, WhatWgUrlParser.EMPTY_RECORD, null, null);
yield builder.whatWgUrlRecord(record);
}
};
@ -293,27 +268,12 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
/**
* Create an instance by parsing the "Origin" header of an HTTP request.
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454</a>
* @deprecated in favor of {@link UriComponentsBuilder#fromUriString(String)};
* to be removed in 7.0
*/
@Deprecated(since = "6.2", forRemoval = true)
public static UriComponentsBuilder fromOriginHeader(String origin) {
Matcher matcher = URI_PATTERN.matcher(origin);
if (matcher.matches()) {
UriComponentsBuilder builder = new UriComponentsBuilder();
String scheme = matcher.group(2);
String host = matcher.group(6);
String port = matcher.group(8);
if (StringUtils.hasLength(scheme)) {
builder.scheme(scheme);
}
builder.host(host);
if (StringUtils.hasLength(port)) {
builder.port(port);
}
checkSchemeAndHost(origin, scheme, host);
return builder;
}
else {
throw new IllegalArgumentException("[" + origin + "] is not a valid \"Origin\" header value");
}
return fromUriString(origin);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -815,7 +815,7 @@ public abstract class WebUtils {
port = uri.getPort();
}
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
UriComponents originUrl = UriComponentsBuilder.fromUriString(origin).build();
return (ObjectUtils.nullSafeEquals(scheme, originUrl.getScheme()) &&
ObjectUtils.nullSafeEquals(host, originUrl.getHost()) &&
getPort(scheme, port) == getPort(originUrl.getScheme(), originUrl.getPort()));

View File

@ -62,6 +62,8 @@ import org.springframework.util.Assert;
*/
final class WhatWgUrlParser {
public static final UrlRecord EMPTY_RECORD = new UrlRecord();
private static final Log logger = LogFactory.getLog(WhatWgUrlParser.class);
private static final int EOF = -1;