Revise parsed path handling in UrlHandlerFilter

Closes gh-35538
This commit is contained in:
rstoyanchev
2025-09-24 19:08:55 +01:00
parent d85a020e4e
commit 5a858915ea
3 changed files with 97 additions and 37 deletions
@@ -74,41 +74,23 @@ public final class UrlHandlerFilter extends OncePerRequestFilter {
}
@Override
protected boolean shouldNotFilterAsyncDispatch() {
return false;
}
@Override
protected boolean shouldNotFilterErrorDispatch() {
return false;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
RequestPath previousPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
RequestPath path = previousPath;
try {
if (path == null) {
path = ServletRequestPathUtils.parseAndCache(request);
RequestPath path = (ServletRequestPathUtils.hasParsedRequestPath(request) ?
ServletRequestPathUtils.getParsedRequestPath(request) :
ServletRequestPathUtils.parse(request));
for (Map.Entry<Handler, List<PathPattern>> entry : this.handlers.entrySet()) {
if (!entry.getKey().supports(request, path)) {
continue;
}
for (Map.Entry<Handler, List<PathPattern>> entry : this.handlers.entrySet()) {
if (!entry.getKey().supports(request, path)) {
continue;
for (PathPattern pattern : entry.getValue()) {
if (pattern.matches(path)) {
entry.getKey().handle(request, response, chain);
return;
}
for (PathPattern pattern : entry.getValue()) {
if (pattern.matches(path)) {
entry.getKey().handle(request, response, chain);
return;
}
}
}
}
finally {
if (previousPath != null) {
ServletRequestPathUtils.setParsedRequestPath(previousPath, request);
}
}
@@ -349,7 +331,18 @@ public final class UrlHandlerFilter extends OncePerRequestFilter {
hasPathInfo ? servletPath : trimTrailingSlash(servletPath),
hasPathInfo ? trimTrailingSlash(pathInfo) : pathInfo);
chain.doFilter(request, response);
RequestPath previousPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
if (previousPath != null) {
ServletRequestPathUtils.parseAndCache(request);
}
try {
chain.doFilter(request, response);
}
finally {
if (previousPath != null) {
ServletRequestPathUtils.setParsedRequestPath(previousPath, request);
}
}
}
}
@@ -52,16 +52,19 @@ public abstract class ServletRequestPathUtils {
/**
* Parse the {@link HttpServletRequest#getRequestURI() requestURI} to a
* {@link RequestPath} and save it in the request attribute
* {@link #PATH_ATTRIBUTE} for subsequent use with
* {@link org.springframework.web.util.pattern.PathPattern parsed patterns}.
* {@link RequestPath}.
* <p>The returned {@code RequestPath} will have both the contextPath and any
* servletPath prefix omitted from the {@link RequestPath#pathWithinApplication()
* pathWithinApplication} it exposes.
* <p>This method is typically called by the {@code DispatcherServlet} to determine
* if any {@code HandlerMapping} indicates that it uses parsed patterns.
* After that the pre-parsed and cached {@code RequestPath} can be accessed
* through {@link #getParsedRequestPath(ServletRequest)}.
* @since 6.2.12
*/
public static RequestPath parse(HttpServletRequest request) {
return ServletRequestPath.parse(request);
}
/**
* Variant of {@link #parse(HttpServletRequest)} that also saves the parsed
* path in the request attribute {@link #PATH_ATTRIBUTE}.
*/
public static RequestPath parseAndCache(HttpServletRequest request) {
RequestPath requestPath = ServletRequestPath.parse(request);