Append trailing slash to static location

Closes gh-33815
This commit is contained in:
rstoyanchev
2024-11-06 11:10:45 +00:00
parent 37243f44e8
commit 59ec871e76
4 changed files with 28 additions and 14 deletions

View File

@@ -65,7 +65,8 @@ public abstract class ResourceHandlerUtils {
else {
path = location.getURL().getPath();
}
assertLocationPath(path);
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
}
catch (IOException ex) {
// ignore
@@ -73,12 +74,18 @@ public abstract class ResourceHandlerUtils {
}
/**
* Assert the given location path is a directory and ends on slash.
* Check if the given static resource location path ends with a trailing
* slash, and append it if necessary.
* @param path the location path
* @return the resulting path to use
*/
public static void assertLocationPath(@Nullable String path) {
Assert.notNull(path, "Resource location path must not be null");
Assert.isTrue(path.endsWith(FOLDER_SEPARATOR) || path.endsWith(WINDOWS_FOLDER_SEPARATOR),
"Resource location does not end with slash: " + path);
public static String initLocationPath(String path) {
String separator = (path.contains(FOLDER_SEPARATOR) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR);
if (!path.endsWith(separator)) {
path = path.concat(separator);
logger.warn("Appended trailing slash to static resource location: " + path);
}
return path;
}
/**

View File

@@ -496,7 +496,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
charset = Charset.forName(value);
location = location.substring(endIndex + 1);
}
ResourceHandlerUtils.assertLocationPath(location);
location = ResourceHandlerUtils.initLocationPath(location);
Resource resource = applicationContext.getResource(location);
if (location.equals("/") && !(resource instanceof ServletContextResource)) {
throw new IllegalStateException(