diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java index 336cd43b47c..81c60a7b5b8 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpRequest.java @@ -25,10 +25,26 @@ import org.springframework.util.MultiValueMap; * Represents a reactive server-side HTTP request * * @author Arjen Poutsma + * @author Rossen Stoyanchev * @since 5.0 */ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage { + + // TODO: https://jira.spring.io/browse/SPR-14726 + + /** + * Returns the portion of the URL path that represents the context path for + * the current {@link HttpHandler}. The context path is always at the + * beginning of the request path. It starts with "/" but but does not end + * with "/". This method may return an empty string if no context path is + * configured. + * @return the context path (not decoded) or an empty string + */ + default String getContextPath() { + return ""; + } + /** * Return a read-only map with parsed and decoded query parameter values. */ diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java index 6ad1848ce47..120769829ad 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpRequest.java @@ -81,6 +81,11 @@ public class ServletServerHttpRequest extends AbstractServerHttpRequest { return HttpMethod.valueOf(getServletRequest().getMethod()); } + @Override + public String getContextPath() { + return getServletRequest().getContextPath(); + } + @Override protected URI initUri() throws URISyntaxException { StringBuffer url = this.request.getRequestURL(); diff --git a/spring-web/src/main/java/org/springframework/web/util/HttpRequestPathHelper.java b/spring-web/src/main/java/org/springframework/web/util/HttpRequestPathHelper.java index e34f29656d9..3f21c69aa66 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HttpRequestPathHelper.java +++ b/spring-web/src/main/java/org/springframework/web/util/HttpRequestPathHelper.java @@ -19,8 +19,10 @@ import java.io.UnsupportedEncodingException; import java.util.LinkedHashMap; import java.util.Map; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; /** @@ -54,10 +56,19 @@ public class HttpRequestPathHelper { public String getLookupPathForRequest(ServerWebExchange exchange) { - String path = exchange.getRequest().getURI().getRawPath(); + String path = getPathWithinApplication(exchange.getRequest()); return (this.shouldUrlDecode() ? decode(exchange, path) : path); } + private String getPathWithinApplication(ServerHttpRequest request) { + String contextPath = request.getContextPath(); + String path = request.getURI().getRawPath(); + if (!StringUtils.hasText(contextPath)) { + return path; + } + return (path.length() > contextPath.length() ? path.substring(contextPath.length()) : ""); + } + private String decode(ServerWebExchange exchange, String path) { // TODO: look up request encoding? try {