Skip to content

Commit 1d7cb4f

Browse files
committed
Defer initialization of HandlerMethod validation flags
Re-create the HandlerMethod only after the original is used as a key in the CORS lookup map. Closes gh-34379
1 parent 8499253 commit 1d7cb4f

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

spring-web/src/main/java/org/springframework/web/util/ServletRequestPathUtils.java

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -179,6 +179,27 @@ public static boolean hasCachedPath(ServletRequest request) {
179179
request.getAttribute(UrlPathHelper.PATH_ATTRIBUTE) != null);
180180
}
181181

182+
/**
183+
* Check if the Servlet is mapped by a path prefix, and if so return that
184+
* path prefix.
185+
* @param request the current request
186+
* @return the prefix, or {@code null} if the Servlet is not mapped by prefix
187+
* @since 6.2.3
188+
*/
189+
@Nullable
190+
public static String getServletPathPrefix(HttpServletRequest request) {
191+
HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING);
192+
mapping = (mapping != null ? mapping : request.getHttpServletMapping());
193+
if (ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) {
194+
String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
195+
servletPath = (servletPath != null ? servletPath : request.getServletPath());
196+
servletPath = (servletPath.endsWith("/") ? servletPath.substring(0, servletPath.length() - 1) : servletPath);
197+
return servletPath;
198+
}
199+
return null;
200+
}
201+
202+
182203

183204
/**
184205
* Simple wrapper around the default {@link RequestPath} implementation that
@@ -251,22 +272,11 @@ public static RequestPath parse(HttpServletRequest request) {
251272
String requestUri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
252273
requestUri = (requestUri != null ? requestUri : request.getRequestURI());
253274
String servletPathPrefix = getServletPathPrefix(request);
254-
return (StringUtils.hasText(servletPathPrefix) ?
255-
new ServletRequestPath(new PathElements(requestUri, request.getContextPath(), servletPathPrefix)) :
256-
RequestPath.parse(requestUri, request.getContextPath()));
257-
}
258-
259-
@Nullable
260-
private static String getServletPathPrefix(HttpServletRequest request) {
261-
HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING);
262-
mapping = (mapping != null ? mapping : request.getHttpServletMapping());
263-
if (ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) {
264-
String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE);
265-
servletPath = (servletPath != null ? servletPath : request.getServletPath());
266-
servletPath = (servletPath.endsWith("/") ? servletPath.substring(0, servletPath.length() - 1) : servletPath);
267-
return UriUtils.encodePath(servletPath, StandardCharsets.UTF_8);
275+
if (!StringUtils.hasLength(servletPathPrefix)) {
276+
return RequestPath.parse(requestUri, request.getContextPath());
268277
}
269-
return null;
278+
servletPathPrefix = UriUtils.encodePath(servletPathPrefix, StandardCharsets.UTF_8);
279+
return new ServletRequestPath(new PathElements(requestUri, request.getContextPath(), servletPathPrefix));
270280
}
271281

272282
record PathElements(String rawPath, @Nullable String contextPath, String servletPathPrefix) {

0 commit comments

Comments
 (0)