Browse Source
This commit introduces a new URL parser based on algorithm provided in the Living URL standard. This new UrlParser is used by UriComponentsBuilder::fromUriString, replacing the regular expressions. Closes gh-32513pull/32659/head
8 changed files with 2530 additions and 110 deletions
@ -0,0 +1,47 @@
@@ -0,0 +1,47 @@
|
||||
/* |
||||
* 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.util; |
||||
|
||||
/** |
||||
* Thrown when a URL string cannot be parsed. |
||||
* |
||||
* @author Arjen Poutsma |
||||
* @since 6.2 |
||||
*/ |
||||
public class InvalidUrlException extends IllegalArgumentException { |
||||
|
||||
private static final long serialVersionUID = 7409308391039105562L; |
||||
|
||||
|
||||
/** |
||||
* Construct a {@code InvalidUrlException} with the specified detail message. |
||||
* @param msg the detail message |
||||
*/ |
||||
public InvalidUrlException(String msg) { |
||||
super(msg); |
||||
} |
||||
|
||||
/** |
||||
* Construct a {@code InvalidUrlException} with the specified detail message |
||||
* and nested exception. |
||||
* @param msg the detail message |
||||
* @param cause the nested exception |
||||
*/ |
||||
public InvalidUrlException(String msg, Throwable cause) { |
||||
super(msg, cause); |
||||
} |
||||
} |
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* 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. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.util; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.lang.Nullable; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* @author Arjen Poutsma |
||||
*/ |
||||
class UrlParserTests { |
||||
|
||||
private static final UrlParser.UrlRecord EMPTY_URL_RECORD = new UrlParser.UrlRecord(); |
||||
|
||||
@Test |
||||
void parse() { |
||||
testParse("https://example.com", "https", "example.com", null, "", null, null); |
||||
testParse("https://example.com/", "https", "example.com", null, "/", null, null); |
||||
testParse("https://example.com/foo", "https", "example.com", null, "/foo", null, null); |
||||
testParse("https://example.com/foo/", "https", "example.com", null, "/foo/", null, null); |
||||
testParse("https://example.com:81/foo", "https", "example.com", "81", "/foo", null, null); |
||||
testParse("/foo", "", null, null, "/foo", null, null); |
||||
testParse("/foo/", "", null, null, "/foo/", null, null); |
||||
testParse("/foo/../bar", "", null, null, "/bar", null, null); |
||||
testParse("/foo/../bar/", "", null, null, "/bar/", null, null); |
||||
testParse("//other.info/foo/bar", "", "other.info", null, "/foo/bar", null, null); |
||||
testParse("//other.info/parent/../foo/bar", "", "other.info", null, "/foo/bar", null, null); |
||||
} |
||||
|
||||
private void testParse(String input, String scheme, @Nullable String host, @Nullable String port, String path, @Nullable String query, @Nullable String fragment) { |
||||
UrlParser.UrlRecord result = UrlParser.parse(input, EMPTY_URL_RECORD, null, null); |
||||
assertThat(result.scheme()).as("Invalid scheme").isEqualTo(scheme); |
||||
if (host != null) { |
||||
assertThat(result.host()).as("Host is null").isNotNull(); |
||||
assertThat(result.host().toString()).as("Invalid host").isEqualTo(host); |
||||
} |
||||
else { |
||||
assertThat(result.host()).as("Host is not null").isNull(); |
||||
} |
||||
assertThat(result.port()).as("Invalid port").isEqualTo(port); |
||||
assertThat(result.path().toString()).as("Invalid path").isEqualTo(path); |
||||
assertThat(result.query()).as("Invalid query").isEqualTo(query); |
||||
assertThat(result.fragment()).as("Invalid fragment").isEqualTo(fragment); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue