Respect context path in WebMvc.fn & WebFlux.fn

This commit makes several changes in both WebMvc.fn as well as
WebFlux.fn.

 - ServerRequest now exposes a RequestPath through requestPath(), and
   pathContainer() has been deprecated.

 - The PathPredicate and PathResourceLookupFunction now respects this
   RequestPath's pathInApplication() in their path-related
   functionality.

 - When nesting, the PathPredicate now appends the matched part of the
   path to the current context path, instead of removing the matched
   part (which was done previously). This has the same result: the
   matched part is gone, but now the full path stays the same.

Closes gh-25270
This commit is contained in:
Arjen Poutsma
2020-09-03 15:10:36 +02:00
parent 88249b2d9a
commit d550d344d5
14 changed files with 122 additions and 137 deletions

View File

@@ -43,7 +43,7 @@ import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
@@ -123,7 +123,7 @@ class DefaultServerRequest implements ServerRequest {
}
@Override
public PathContainer pathContainer() {
public RequestPath requestPath() {
return request().getPath();
}

View File

@@ -56,7 +56,7 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
@Override
public Mono<Resource> apply(ServerRequest request) {
PathContainer pathContainer = request.pathContainer();
PathContainer pathContainer = request.requestPath().pathWithinApplication();
if (!this.pattern.matches(pathContainer)) {
return Mono.empty();
}

View File

@@ -19,7 +19,6 @@ package org.springframework.web.reactive.function.server;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
@@ -47,6 +46,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
@@ -492,7 +492,7 @@ public abstract class RequestPredicates {
@Override
public boolean test(ServerRequest request) {
PathContainer pathContainer = request.pathContainer();
PathContainer pathContainer = request.requestPath().pathWithinApplication();
PathPattern.PathMatchInfo info = this.pattern.matchAndExtract(pathContainer);
traceMatch("Pattern", this.pattern.getPatternString(), request.path(), info != null);
if (info != null) {
@@ -518,7 +518,7 @@ public abstract class RequestPredicates {
@Override
public Optional<ServerRequest> nest(ServerRequest request) {
return Optional.ofNullable(this.pattern.matchStartOfPath(request.pathContainer()))
return Optional.ofNullable(this.pattern.matchStartOfPath(request.requestPath().pathWithinApplication()))
.map(info -> new SubPathServerRequestWrapper(request, info, this.pattern));
}
@@ -926,17 +926,27 @@ public abstract class RequestPredicates {
private final ServerRequest request;
private final PathContainer pathContainer;
private final RequestPath requestPath;
private final Map<String, Object> attributes;
public SubPathServerRequestWrapper(ServerRequest request,
PathPattern.PathRemainingMatchInfo info, PathPattern pattern) {
this.request = request;
this.pathContainer = new SubPathContainer(info.getPathRemaining());
this.requestPath = requestPath(request.requestPath(), info);
this.attributes = mergeAttributes(request, info.getUriVariables(), pattern);
}
private static RequestPath requestPath(RequestPath original, PathPattern.PathRemainingMatchInfo info) {
StringBuilder contextPath = new StringBuilder(original.contextPath().value());
contextPath.append(info.getPathMatched().value());
int length = contextPath.length();
if (length > 0 && contextPath.charAt(length - 1) == '/') {
contextPath.setLength(length - 1);
}
return original.modifyContextPath(contextPath.toString());
}
private static Map<String, Object> mergeAttributes(ServerRequest request,
Map<String, String> pathVariables, PathPattern pattern) {
Map<String, Object> result = new ConcurrentHashMap<>(request.attributes());
@@ -972,13 +982,8 @@ public abstract class RequestPredicates {
}
@Override
public String path() {
return this.pathContainer.value();
}
@Override
public PathContainer pathContainer() {
return this.pathContainer;
public RequestPath requestPath() {
return this.requestPath;
}
@Override
@@ -1089,46 +1094,6 @@ public abstract class RequestPredicates {
return method() + " " + path();
}
private static class SubPathContainer implements PathContainer {
private static final PathContainer.Separator SEPARATOR = () -> "/";
private final String value;
private final List<Element> elements;
public SubPathContainer(PathContainer original) {
this.value = prefixWithSlash(original.value());
this.elements = prependWithSeparator(original.elements());
}
private static String prefixWithSlash(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
return path;
}
private static List<Element> prependWithSeparator(List<Element> elements) {
List<Element> result = new ArrayList<>(elements);
if (result.isEmpty() || !(result.get(0) instanceof Separator)) {
result.add(0, SEPARATOR);
}
return Collections.unmodifiableList(result);
}
@Override
public String value() {
return this.value;
}
@Override
public List<Element> elements() {
return this.elements;
}
}
}
}

View File

@@ -42,6 +42,7 @@ import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.json.Jackson2CodecSupport;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -100,14 +101,24 @@ public interface ServerRequest {
* Get the request path.
*/
default String path() {
return uri().getRawPath();
return requestPath().pathWithinApplication().value();
}
/**
* Get the request path as a {@code PathContainer}.
* @deprecated as of 5.3, in favor on {@link #requestPath()}
*/
@Deprecated
default PathContainer pathContainer() {
return PathContainer.parsePath(path());
return requestPath();
}
/**
* Get the request path as a {@code PathContainer}.
* @since 5.3
*/
default RequestPath requestPath() {
return exchange().getRequest().getPath();
}
/**

View File

@@ -38,6 +38,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.multipart.Part;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
@@ -104,10 +105,16 @@ public class ServerRequestWrapper implements ServerRequest {
}
@Override
@Deprecated
public PathContainer pathContainer() {
return this.delegate.pathContainer();
}
@Override
public RequestPath requestPath() {
return this.delegate.requestPath();
}
@Override
public Headers headers() {
return this.delegate.headers();

View File

@@ -29,7 +29,6 @@ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRe
import org.springframework.web.testfixture.server.MockServerWebExchange;
import org.springframework.web.util.pattern.PathPatternParser;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -141,7 +140,7 @@ public class RequestPredicatesTests {
URI uri = URI.create("https://localhost/path");
RequestPredicate predicate = RequestPredicates.path("/p*");
MockServerHttpRequest mockRequest = MockServerHttpRequest.get(uri.toString()).build();
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList());
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
assertThat(predicate.test(request)).isTrue();
mockRequest = MockServerHttpRequest.head("https://example.com").build();
@@ -178,6 +177,15 @@ public class RequestPredicatesTests {
assertThat(predicate.test(request)).isTrue();
}
@Test
public void pathWithContext() {
RequestPredicate predicate = RequestPredicates.path("/p*");
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/context/path")
.contextPath("/context").build();
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
assertThat(predicate.test(request)).isTrue();
}
@Test
public void headers() {
String name = "MyHeader";