RequestPath improvements
Static parse methods on PathSegmentContainer and PathSegment plus: isEmpty() on PathSegmentContainer and PathSegment isAbsolute() and hasTrailingSlash() on PathSegmentContainer char[] alternative for valueDecoded() on PathSegment
This commit is contained in:
parent
7b5f96c804
commit
97a97f9bba
|
|
@ -28,6 +28,7 @@ import org.springframework.util.MultiValueMap;
|
|||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link RequestPath}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
|
|
@ -58,13 +59,13 @@ class DefaultRequestPath implements RequestPath {
|
|||
this.pathWithinApplication = initPathWithinApplication(this.fullPath, this.contextPath);
|
||||
}
|
||||
|
||||
DefaultRequestPath(RequestPath requestPath, String contextPath, Charset charset) {
|
||||
DefaultRequestPath(RequestPath requestPath, String contextPath) {
|
||||
this.fullPath = new DefaultPathSegmentContainer(requestPath.value(), requestPath.pathSegments());
|
||||
this.contextPath = initContextPath(this.fullPath, contextPath);
|
||||
this.pathWithinApplication = initPathWithinApplication(this.fullPath, this.contextPath);
|
||||
}
|
||||
|
||||
private static PathSegmentContainer parsePath(String path, Charset charset) {
|
||||
static PathSegmentContainer parsePath(String path, Charset charset) {
|
||||
path = StringUtils.hasText(path) ? path : "";
|
||||
if ("".equals(path)) {
|
||||
return EMPTY_PATH;
|
||||
|
|
@ -73,8 +74,8 @@ class DefaultRequestPath implements RequestPath {
|
|||
return ROOT_PATH;
|
||||
}
|
||||
List<PathSegment> result = new ArrayList<>();
|
||||
int begin = 1;
|
||||
while (true) {
|
||||
int begin = (path.charAt(0) == '/' ? 1 : 0);
|
||||
while (begin < path.length()) {
|
||||
int end = path.indexOf('/', begin);
|
||||
String segment = (end != -1 ? path.substring(begin, end) : path.substring(begin));
|
||||
result.add(parsePathSegment(segment, charset));
|
||||
|
|
@ -82,22 +83,18 @@ class DefaultRequestPath implements RequestPath {
|
|||
break;
|
||||
}
|
||||
begin = end + 1;
|
||||
if (begin == path.length()) {
|
||||
// trailing slash
|
||||
result.add(EMPTY_PATH_SEGMENT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new DefaultPathSegmentContainer(path, result);
|
||||
}
|
||||
|
||||
private static PathSegment parsePathSegment(String input, Charset charset) {
|
||||
static PathSegment parsePathSegment(String input, Charset charset) {
|
||||
if ("".equals(input)) {
|
||||
return EMPTY_PATH_SEGMENT;
|
||||
}
|
||||
int index = input.indexOf(';');
|
||||
if (index == -1) {
|
||||
return new DefaultPathSegment(input, StringUtils.uriDecode(input, charset), "", EMPTY_MAP);
|
||||
String inputDecoded = StringUtils.uriDecode(input, charset);
|
||||
return new DefaultPathSegment(input, inputDecoded, "", EMPTY_MAP);
|
||||
}
|
||||
String value = input.substring(0, index);
|
||||
String valueDecoded = StringUtils.uriDecode(value, charset);
|
||||
|
|
@ -180,16 +177,37 @@ class DefaultRequestPath implements RequestPath {
|
|||
}
|
||||
|
||||
|
||||
// PathSegmentContainer methods..
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.contextPath.isEmpty() && this.pathWithinApplication.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String value() {
|
||||
return this.fullPath.value();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAbsolute() {
|
||||
return !this.contextPath.isEmpty() && this.contextPath.isAbsolute() || this.pathWithinApplication.isAbsolute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PathSegment> pathSegments() {
|
||||
return this.fullPath.pathSegments();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTrailingSlash() {
|
||||
return this.pathWithinApplication.hasTrailingSlash();
|
||||
}
|
||||
|
||||
|
||||
// RequestPath methods..
|
||||
|
||||
@Override
|
||||
public PathSegmentContainer contextPath() {
|
||||
return this.contextPath;
|
||||
|
|
@ -205,12 +223,22 @@ class DefaultRequestPath implements RequestPath {
|
|||
|
||||
private final String path;
|
||||
|
||||
private final boolean empty;
|
||||
|
||||
private final boolean absolute;
|
||||
|
||||
private final List<PathSegment> pathSegments;
|
||||
|
||||
private final boolean trailingSlash;
|
||||
|
||||
DefaultPathSegmentContainer(String path, List<PathSegment> pathSegments) {
|
||||
|
||||
|
||||
DefaultPathSegmentContainer(String path, List<PathSegment> segments) {
|
||||
this.path = path;
|
||||
this.pathSegments = Collections.unmodifiableList(pathSegments);
|
||||
this.absolute = path.startsWith("/");
|
||||
this.pathSegments = Collections.unmodifiableList(segments);
|
||||
this.trailingSlash = path.endsWith("/") && path.length() > 1;
|
||||
this.empty = !this.absolute && !this.trailingSlash && segments.stream().allMatch(PathSegment::isEmpty);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -219,11 +247,26 @@ class DefaultRequestPath implements RequestPath {
|
|||
return this.path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.empty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAbsolute() {
|
||||
return this.absolute;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PathSegment> pathSegments() {
|
||||
return this.pathSegments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTrailingSlash() {
|
||||
return this.trailingSlash;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
|
|
@ -254,6 +297,10 @@ class DefaultRequestPath implements RequestPath {
|
|||
|
||||
private final String valueDecoded;
|
||||
|
||||
private final char[] valueCharsDecoded;
|
||||
|
||||
private final boolean empty;
|
||||
|
||||
private final String semicolonContent;
|
||||
|
||||
private final MultiValueMap<String, String> parameters;
|
||||
|
|
@ -262,8 +309,12 @@ class DefaultRequestPath implements RequestPath {
|
|||
DefaultPathSegment(String value, String valueDecoded, String semicolonContent,
|
||||
MultiValueMap<String, String> params) {
|
||||
|
||||
Assert.isTrue(!value.contains("/"), "Invalid path segment value: " + value);
|
||||
|
||||
this.value = value;
|
||||
this.valueDecoded = valueDecoded;
|
||||
this.valueCharsDecoded = valueDecoded.toCharArray();
|
||||
this.empty = !StringUtils.hasText(this.valueDecoded);
|
||||
this.semicolonContent = semicolonContent;
|
||||
this.parameters = CollectionUtils.unmodifiableMultiValueMap(params);
|
||||
}
|
||||
|
|
@ -279,6 +330,16 @@ class DefaultRequestPath implements RequestPath {
|
|||
return this.valueDecoded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char[] valueCharsDecoded() {
|
||||
return this.valueCharsDecoded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.empty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String semicolonContent() {
|
||||
return this.semicolonContent;
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
|||
return null;
|
||||
}
|
||||
else if (uriToUse == null) {
|
||||
return new DefaultRequestPath(this.delegate.getPath(), this.contextPath, StandardCharsets.UTF_8);
|
||||
return new DefaultRequestPath(this.delegate.getPath(), this.contextPath);
|
||||
}
|
||||
else {
|
||||
return new DefaultRequestPath(uriToUse, this.contextPath, StandardCharsets.UTF_8);
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
|
|
@ -32,10 +34,21 @@ public interface PathSegment {
|
|||
String value();
|
||||
|
||||
/**
|
||||
* The path {@link #value()} decoded.
|
||||
* Return the path {@link #value()} decoded.
|
||||
*/
|
||||
String valueDecoded();
|
||||
|
||||
/**
|
||||
* Return the same as {@link #valueDecoded()} but as a {@code char[]}.
|
||||
*/
|
||||
char[] valueCharsDecoded();
|
||||
|
||||
/**
|
||||
* Whether the path value (encoded or decoded) is empty meaning that it has
|
||||
* {@link Character#isWhitespace whitespace} characters or none.
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Return the portion of the path segment after and including the first
|
||||
* ";" (semicolon) representing path parameters. The actual parsed
|
||||
|
|
@ -48,4 +61,15 @@ public interface PathSegment {
|
|||
*/
|
||||
MultiValueMap<String, String> parameters();
|
||||
|
||||
|
||||
/**
|
||||
* Parse the given path segment value.
|
||||
* @param path the value to parse
|
||||
* @param encoding the charset to use for the decoded value
|
||||
* @return the parsed path segment
|
||||
*/
|
||||
static PathSegment parse(String path, Charset encoding) {
|
||||
return DefaultRequestPath.parsePathSegment(path, encoding);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,13 +15,18 @@
|
|||
*/
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Container for 0..N path segments.
|
||||
*
|
||||
* <p>Typically consumed via {@link ServerHttpRequest#getPath()} but can also
|
||||
* be created by parsing a path value via {@link #parse(String, Charset)}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
* @see RequestPath
|
||||
*/
|
||||
public interface PathSegmentContainer {
|
||||
|
||||
|
|
@ -30,9 +35,36 @@ public interface PathSegmentContainer {
|
|||
*/
|
||||
String value();
|
||||
|
||||
/**
|
||||
* Whether the path (encoded or decoded) is empty meaning that it has
|
||||
* {@link Character#isWhitespace whitespace} characters or none.
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Whether the path {@link #value()} starts with "/".
|
||||
*/
|
||||
boolean isAbsolute();
|
||||
|
||||
/**
|
||||
* The list of path segments contained.
|
||||
*/
|
||||
List<PathSegment> pathSegments();
|
||||
|
||||
/**
|
||||
* Whether the path {@link #value()} ends with "/".
|
||||
*/
|
||||
boolean hasTrailingSlash();
|
||||
|
||||
|
||||
/**
|
||||
* Parse the given path value into a {@link PathSegmentContainer}.
|
||||
* @param path the value to parse
|
||||
* @param encoding the charset to use for decoded path segment values
|
||||
* @return the parsed path
|
||||
*/
|
||||
static PathSegmentContainer parse(String path, Charset encoding) {
|
||||
return DefaultRequestPath.parsePath(path, encoding);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
|
@ -27,6 +26,7 @@ import org.junit.Test;
|
|||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
|
|
@ -38,97 +38,118 @@ public class DefaultRequestPathTests {
|
|||
@Test
|
||||
public void pathSegment() throws Exception {
|
||||
// basic
|
||||
testPathSegment("cars", "", "cars", "cars", new LinkedMultiValueMap<>());
|
||||
testPathSegment("cars", "", "cars", "cars", false, new LinkedMultiValueMap<>());
|
||||
|
||||
// empty
|
||||
testPathSegment("", "", "", "", new LinkedMultiValueMap<>());
|
||||
testPathSegment("", "", "", "", true, new LinkedMultiValueMap<>());
|
||||
|
||||
// spaces
|
||||
testPathSegment("%20", "", "%20", " ", new LinkedMultiValueMap<>());
|
||||
testPathSegment("%20a%20", "", "%20a%20", " a ", new LinkedMultiValueMap<>());
|
||||
testPathSegment("%20%20", "", "%20%20", " ", true, new LinkedMultiValueMap<>());
|
||||
testPathSegment("%20a%20", "", "%20a%20", " a ", false, new LinkedMultiValueMap<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSegmentWithParams() throws Exception {
|
||||
public void pathSegmentParams() throws Exception {
|
||||
// basic
|
||||
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("colors", "red");
|
||||
params.add("colors", "blue");
|
||||
params.add("colors", "green");
|
||||
params.add("year", "2012");
|
||||
testPathSegment("cars", ";colors=red,blue,green;year=2012", "cars", "cars", params);
|
||||
testPathSegment("cars", ";colors=red,blue,green;year=2012", "cars", "cars", false, params);
|
||||
|
||||
// trailing semicolon
|
||||
params = new LinkedMultiValueMap<>();
|
||||
params.add("p", "1");
|
||||
testPathSegment("path", ";p=1;", "path", "path", params);
|
||||
testPathSegment("path", ";p=1;", "path", "path", false, params);
|
||||
|
||||
// params with spaces
|
||||
params = new LinkedMultiValueMap<>();
|
||||
params.add("param name", "param value");
|
||||
testPathSegment("path", ";param%20name=param%20value;%20", "path", "path", params);
|
||||
testPathSegment("path", ";param%20name=param%20value;%20", "path", "path", false, params);
|
||||
|
||||
// empty params
|
||||
params = new LinkedMultiValueMap<>();
|
||||
params.add("p", "1");
|
||||
testPathSegment("path", ";;;%20;%20;p=1;%20", "path", "path", params);
|
||||
testPathSegment("path", ";;;%20;%20;p=1;%20", "path", "path", false, params);
|
||||
}
|
||||
|
||||
private void testPathSegment(String pathSegment, String semicolonContent,
|
||||
String value, String valueDecoded, boolean empty, MultiValueMap<String, String> params) {
|
||||
|
||||
PathSegment segment = PathSegment.parse(pathSegment + semicolonContent, UTF_8);
|
||||
|
||||
assertEquals("value: '" + pathSegment + "'", value, segment.value());
|
||||
assertEquals("valueDecoded: '" + pathSegment + "'", valueDecoded, segment.valueDecoded());
|
||||
assertEquals("isEmpty: '" + pathSegment + "'", empty, segment.isEmpty());
|
||||
assertEquals("semicolonContent: '" + pathSegment + "'", semicolonContent, segment.semicolonContent());
|
||||
assertEquals("params: '" + pathSegment + "'", params, segment.parameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void path() throws Exception {
|
||||
// basic
|
||||
testPath("/a/b/c", "/a/b/c", Arrays.asList("a", "b", "c"));
|
||||
testPath("/a/b/c", "/a/b/c", false, true, Arrays.asList("a", "b", "c"), false);
|
||||
|
||||
// root path
|
||||
testPath("/%20", "/%20", Collections.singletonList("%20"));
|
||||
testPath("", "", Collections.emptyList());
|
||||
testPath("%20", "", Collections.emptyList());
|
||||
testPath("/", "/", false, true, Collections.singletonList(""), false);
|
||||
|
||||
// empty path
|
||||
testPath("", "", true, false, Collections.emptyList(), false);
|
||||
testPath("%20%20", "%20%20", true, false, Collections.singletonList("%20%20"), false);
|
||||
|
||||
// trailing slash
|
||||
testPath("/a/b/", "/a/b/", Arrays.asList("a", "b", ""));
|
||||
testPath("/a/b//", "/a/b//", Arrays.asList("a", "b", "", ""));
|
||||
testPath("/a/b/", "/a/b/", false, true, Arrays.asList("a", "b"), true);
|
||||
testPath("/a/b//", "/a/b//", false, true, Arrays.asList("a", "b", ""), true);
|
||||
|
||||
// extra slashes ande spaces
|
||||
testPath("//%20/%20", "//%20/%20", Arrays.asList("", "%20", "%20"));
|
||||
// extra slashes and spaces
|
||||
testPath("/%20", "/%20", false, true, Collections.singletonList("%20"), false);
|
||||
testPath("//%20/%20", "//%20/%20", false, true, Arrays.asList("", "%20", "%20"), false);
|
||||
}
|
||||
|
||||
private void testPath(String input, String value, boolean empty, boolean absolute,
|
||||
List<String> segments, boolean trailingSlash) {
|
||||
|
||||
PathSegmentContainer path = PathSegmentContainer.parse(input, UTF_8);
|
||||
|
||||
List<String> segmentValues = path.pathSegments().stream().map(PathSegment::value)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals("value: '" + input + "'", value, path.value());
|
||||
assertEquals("empty: '" + input + "'", empty, path.isEmpty());
|
||||
assertEquals("isAbsolute: '" + input + "'", absolute, path.isAbsolute());
|
||||
assertEquals("pathSegments: " + input, segments, segmentValues);
|
||||
assertEquals("hasTrailingSlash: '" + input + "'", trailingSlash, path.hasTrailingSlash());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextPath() throws Exception {
|
||||
URI uri = URI.create("http://localhost:8080/app/a/b/c");
|
||||
RequestPath path = new DefaultRequestPath(uri, "/app", StandardCharsets.UTF_8);
|
||||
public void requestPath() throws Exception {
|
||||
// basic
|
||||
testRequestPath("/app/a/b/c", "/app", "/a/b/c", false, true, false);
|
||||
|
||||
PathSegmentContainer contextPath = path.contextPath();
|
||||
assertEquals("/app", contextPath.value());
|
||||
assertEquals(Collections.singletonList("app"), pathSegmentValues(contextPath));
|
||||
// no context path
|
||||
testRequestPath("/a/b/c", "", "/a/b/c", false, true, false);
|
||||
|
||||
PathSegmentContainer pathWithinApplication = path.pathWithinApplication();
|
||||
assertEquals("/a/b/c", pathWithinApplication.value());
|
||||
assertEquals(Arrays.asList("a", "b", "c"), pathSegmentValues(pathWithinApplication));
|
||||
// empty path
|
||||
testRequestPath("", "", "", true, false, false);
|
||||
testRequestPath("", "/", "", true, false, false);
|
||||
|
||||
// trailing slash
|
||||
testRequestPath("/app/a/", "/app", "/a/", false, true, true);
|
||||
testRequestPath("/app/a//", "/app", "/a//", false, true, true);
|
||||
}
|
||||
|
||||
private void testRequestPath(String fullPath, String contextPath, String pathWithinApplication,
|
||||
boolean empty, boolean absolute, boolean trailingSlash) {
|
||||
|
||||
private void testPathSegment(String pathSegment, String semicolonContent,
|
||||
String value, String valueDecoded, MultiValueMap<String, String> parameters) {
|
||||
URI uri = URI.create("http://localhost:8080" + fullPath);
|
||||
RequestPath requestPath = new DefaultRequestPath(uri, contextPath, UTF_8);
|
||||
|
||||
URI uri = URI.create("http://localhost:8080/" + pathSegment + semicolonContent);
|
||||
PathSegment segment = new DefaultRequestPath(uri, "", StandardCharsets.UTF_8).pathSegments().get(0);
|
||||
|
||||
assertEquals(value, segment.value());
|
||||
assertEquals(valueDecoded, segment.valueDecoded());
|
||||
assertEquals(semicolonContent, segment.semicolonContent());
|
||||
assertEquals(parameters, segment.parameters());
|
||||
}
|
||||
|
||||
private void testPath(String input, String value, List<String> segments) {
|
||||
URI uri = URI.create("http://localhost:8080" + input);
|
||||
RequestPath path = new DefaultRequestPath(uri, "", StandardCharsets.UTF_8);
|
||||
|
||||
assertEquals(value, path.value());
|
||||
assertEquals(segments, pathSegmentValues(path));
|
||||
}
|
||||
|
||||
private static List<String> pathSegmentValues(PathSegmentContainer path) {
|
||||
return path.pathSegments().stream().map(PathSegment::value).collect(Collectors.toList());
|
||||
assertEquals(empty, requestPath.isEmpty());
|
||||
assertEquals(absolute, requestPath.isAbsolute());
|
||||
assertEquals(trailingSlash, requestPath.hasTrailingSlash());
|
||||
assertEquals(contextPath.equals("/") ? "" : contextPath, requestPath.contextPath().value());
|
||||
assertEquals(pathWithinApplication, requestPath.pathWithinApplication().value());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue