mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Introduce new URL parser
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-32513
This commit is contained in:
+1
-1
@@ -639,7 +639,7 @@ class ForwardedHeaderFilterTests {
|
||||
|
||||
String location = "//other.info/parent/../foo/bar";
|
||||
String redirectedUrl = sendRedirect(location);
|
||||
assertThat(redirectedUrl).isEqualTo(("https:" + location));
|
||||
assertThat(redirectedUrl).isEqualTo(("https://other.info/foo/bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+25
-38
@@ -33,7 +33,6 @@ import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link UriComponentsBuilder}.
|
||||
@@ -228,15 +227,11 @@ class UriComponentsBuilderTests {
|
||||
void fromUriStringIPv6Host() {
|
||||
UriComponents result = UriComponentsBuilder
|
||||
.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc]:8080/resource").build().encode();
|
||||
assertThat(result.getHost()).isEqualTo("[1abc:2abc:3abc::5ABC:6abc]");
|
||||
|
||||
UriComponents resultWithScopeId = UriComponentsBuilder
|
||||
.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc%eth0]:8080/resource").build().encode();
|
||||
assertThat(resultWithScopeId.getHost()).isEqualTo("[1abc:2abc:3abc::5ABC:6abc%25eth0]");
|
||||
assertThat(result.getHost()).isEqualToIgnoringCase("[1abc:2abc:3abc::5ABC:6abc]");
|
||||
|
||||
UriComponents resultIPv4compatible = UriComponentsBuilder
|
||||
.fromUriString("http://[::192.168.1.1]:8080/resource").build().encode();
|
||||
assertThat(resultIPv4compatible.getHost()).isEqualTo("[::192.168.1.1]");
|
||||
assertThat(resultIPv4compatible.getHost()).isEqualTo("[::c0a8:101]");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -257,27 +252,27 @@ class UriComponentsBuilderTests {
|
||||
@Test // SPR-14828
|
||||
void fromHttpUrlQueryParamEncodedAndContainingPlus() {
|
||||
String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98";
|
||||
URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build(true).toUri();
|
||||
URI uri = UriComponentsBuilder.fromUriString(httpUrl).build(true).toUri();
|
||||
|
||||
assertThat(uri.toString()).isEqualTo(httpUrl);
|
||||
}
|
||||
|
||||
@Test // SPR-10779
|
||||
void fromHttpUrlCaseInsensitiveScheme() {
|
||||
assertThat(UriComponentsBuilder.fromHttpUrl("HTTP://www.google.com").build().getScheme()).isEqualTo("http");
|
||||
assertThat(UriComponentsBuilder.fromHttpUrl("HTTPS://www.google.com").build().getScheme()).isEqualTo("https");
|
||||
assertThat(UriComponentsBuilder.fromUriString("HTTP://www.google.com").build().getScheme()).isEqualTo("http");
|
||||
assertThat(UriComponentsBuilder.fromUriString("HTTPS://www.google.com").build().getScheme()).isEqualTo("https");
|
||||
}
|
||||
|
||||
@Test // SPR-10539
|
||||
void fromHttpUrlInvalidIPv6Host() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
UriComponentsBuilder.fromHttpUrl("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource"));
|
||||
UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromHttpUrlWithoutFragment() {
|
||||
String httpUrl = "http://localhost:8080/test/print";
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build();
|
||||
assertThat(uriComponents.getScheme()).isEqualTo("http");
|
||||
assertThat(uriComponents.getUserInfo()).isNull();
|
||||
assertThat(uriComponents.getHost()).isEqualTo("localhost");
|
||||
@@ -289,7 +284,7 @@ class UriComponentsBuilderTests {
|
||||
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
|
||||
|
||||
httpUrl = "http://user:test@localhost:8080/test/print?foo=bar";
|
||||
uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
|
||||
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build();
|
||||
assertThat(uriComponents.getScheme()).isEqualTo("http");
|
||||
assertThat(uriComponents.getUserInfo()).isEqualTo("user:test");
|
||||
assertThat(uriComponents.getHost()).isEqualTo("localhost");
|
||||
@@ -301,7 +296,7 @@ class UriComponentsBuilderTests {
|
||||
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
|
||||
|
||||
httpUrl = "http://localhost:8080/test/print?foo=bar";
|
||||
uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
|
||||
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build();
|
||||
assertThat(uriComponents.getScheme()).isEqualTo("http");
|
||||
assertThat(uriComponents.getUserInfo()).isNull();
|
||||
assertThat(uriComponents.getHost()).isEqualTo("localhost");
|
||||
@@ -315,20 +310,20 @@ class UriComponentsBuilderTests {
|
||||
|
||||
@Test // gh-25300
|
||||
void fromHttpUrlWithFragment() {
|
||||
String httpUrl = "https://example.com#baz";
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
|
||||
String httpUrl = "https://example.com/#baz";
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build();
|
||||
assertThat(uriComponents.getScheme()).isEqualTo("https");
|
||||
assertThat(uriComponents.getUserInfo()).isNull();
|
||||
assertThat(uriComponents.getHost()).isEqualTo("example.com");
|
||||
assertThat(uriComponents.getPort()).isEqualTo(-1);
|
||||
assertThat(uriComponents.getPath()).isNullOrEmpty();
|
||||
assertThat(uriComponents.getPath()).isEqualTo("/");
|
||||
assertThat(uriComponents.getPathSegments()).isEmpty();
|
||||
assertThat(uriComponents.getQuery()).isNull();
|
||||
assertThat(uriComponents.getFragment()).isEqualTo("baz");
|
||||
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
|
||||
|
||||
httpUrl = "http://localhost:8080/test/print#baz";
|
||||
uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
|
||||
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build();
|
||||
assertThat(uriComponents.getScheme()).isEqualTo("http");
|
||||
assertThat(uriComponents.getUserInfo()).isNull();
|
||||
assertThat(uriComponents.getHost()).isEqualTo("localhost");
|
||||
@@ -340,7 +335,7 @@ class UriComponentsBuilderTests {
|
||||
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
|
||||
|
||||
httpUrl = "http://localhost:8080/test/print?foo=bar#baz";
|
||||
uriComponents = UriComponentsBuilder.fromHttpUrl(httpUrl).build();
|
||||
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build();
|
||||
assertThat(uriComponents.getScheme()).isEqualTo("http");
|
||||
assertThat(uriComponents.getUserInfo()).isNull();
|
||||
assertThat(uriComponents.getHost()).isEqualTo("localhost");
|
||||
@@ -637,17 +632,11 @@ class UriComponentsBuilderTests {
|
||||
void relativeUrls() {
|
||||
String baseUrl = "https://example.com";
|
||||
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toString())
|
||||
.isEqualTo(baseUrl + "/foo/../bar");
|
||||
.isEqualTo(baseUrl + "/bar");
|
||||
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUriString())
|
||||
.isEqualTo(baseUrl + "/foo/../bar");
|
||||
.isEqualTo(baseUrl + "/bar");
|
||||
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUri().getPath())
|
||||
.isEqualTo("/foo/../bar");
|
||||
assertThat(UriComponentsBuilder.fromUriString("../../").build().toString())
|
||||
.isEqualTo("../../");
|
||||
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUriString())
|
||||
.isEqualTo("../../");
|
||||
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUri().getPath())
|
||||
.isEqualTo("../../");
|
||||
.isEqualTo("/bar");
|
||||
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toString())
|
||||
.isEqualTo(baseUrl + "/foo/../bar");
|
||||
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUriString())
|
||||
@@ -746,9 +735,9 @@ class UriComponentsBuilderTests {
|
||||
|
||||
// empty
|
||||
tester.accept("{}", "%7B%7D");
|
||||
tester.accept("{ \t}", "%7B%20%09%7D");
|
||||
tester.accept("{ \t}", "%7B%20%7D");
|
||||
tester.accept("/a{}b", "/a%7B%7Db");
|
||||
tester.accept("/a{ \t}b", "/a%7B%20%09%7Db");
|
||||
tester.accept("/a{ \t}b", "/a%7B%20%7Db");
|
||||
|
||||
// nested, matching
|
||||
tester.accept("{foo{}}", "%7Bfoo%7B%7D%7D");
|
||||
@@ -815,19 +804,17 @@ class UriComponentsBuilderTests {
|
||||
@Test
|
||||
void verifyInvalidPort() {
|
||||
String url = "http://localhost:XXX/path";
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> UriComponentsBuilder.fromUriString(url).build().toUri())
|
||||
.withMessage("The port must be an integer: XXX");
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> UriComponentsBuilder.fromHttpUrl(url).build().toUri())
|
||||
.withMessage("The port must be an integer: XXX");
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> UriComponentsBuilder.fromUriString(url).build().toUri());
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> UriComponentsBuilder.fromUriString(url).build().toUri());
|
||||
}
|
||||
|
||||
@Test // gh-27039
|
||||
void expandPortAndPathWithoutSeparator() {
|
||||
URI uri = UriComponentsBuilder
|
||||
.fromUriString("ws://localhost:{port}{path}")
|
||||
.buildAndExpand(7777, "/test")
|
||||
.fromUriString("ws://localhost:{port}/{path}")
|
||||
.buildAndExpand(7777, "test")
|
||||
.toUri();
|
||||
assertThat(uri.toString()).isEqualTo("ws://localhost:7777/test");
|
||||
}
|
||||
|
||||
@@ -27,9 +27,9 @@ import java.util.Collections;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.web.util.UriComponentsBuilder.fromHttpUrl;
|
||||
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
|
||||
|
||||
/**
|
||||
@@ -159,10 +159,9 @@ class UriComponentsTests {
|
||||
|
||||
@Test // gh-28521
|
||||
void invalidPort() {
|
||||
assertExceptionsForInvalidPort(fromUriString("https://example.com:XXX/bar").build());
|
||||
assertThatExceptionOfType(InvalidUrlException.class)
|
||||
.isThrownBy(() -> fromUriString("https://example.com:XXX/bar"));
|
||||
assertExceptionsForInvalidPort(fromUriString("https://example.com/bar").port("XXX").build());
|
||||
assertExceptionsForInvalidPort(fromHttpUrl("https://example.com:XXX/bar").build());
|
||||
assertExceptionsForInvalidPort(fromHttpUrl("https://example.com/bar").port("XXX").build());
|
||||
}
|
||||
|
||||
private void assertExceptionsForInvalidPort(UriComponents uriComponents) {
|
||||
@@ -243,7 +242,6 @@ class UriComponentsTests {
|
||||
UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();
|
||||
UriComponents uric3 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bin").build();
|
||||
|
||||
assertThat(uric1).isInstanceOf(OpaqueUriComponents.class);
|
||||
assertThat(uric1).isEqualTo(uric1);
|
||||
assertThat(uric1).isEqualTo(uric2);
|
||||
assertThat(uric1).isNotEqualTo(uric3);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user