Skip to content

Commit 2d3fe96

Browse files
committed
Allow file locations for resource handling
Prior to this change, location checks for serving resources would append `/` to the location path it didn't already have one. This commit makes sure not to append a `/` if the provided location is actually a file. Issue: SPR-12747
1 parent 9b26e4f commit 2d3fe96

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ else if (resource instanceof ServletContextResource) {
315315
resourcePath = resource.getURL().getPath();
316316
locationPath = location.getURL().getPath();
317317
}
318+
if(locationPath.equals(resourcePath)) {
319+
return true;
320+
}
318321
locationPath = (locationPath.endsWith("/") ||
319322
!StringUtils.hasLength(locationPath) ? locationPath : locationPath + "/");
320323
if (!resourcePath.startsWith(locationPath)) {

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,22 @@ public void resourceNotFound() throws Exception {
280280
}
281281

282282

283+
// SPR-12747
284+
@Test
285+
public void getResourceWithResourceLocation() throws Exception {
286+
List<Resource> resourcePaths = new ArrayList<Resource>();
287+
resourcePaths.add(new ClassPathResource("test/foo.css", getClass()));
288+
this.handler.setLocations(resourcePaths);
289+
MockHttpServletRequest request = new MockHttpServletRequest();
290+
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/foo.css");
291+
request.setMethod("GET");
292+
MockHttpServletResponse response = new MockHttpServletResponse();
293+
handler.handleRequest(request, response);
294+
assertEquals("text/css", response.getContentType());
295+
assertEquals(17, response.getContentLength());
296+
}
297+
298+
283299
private static class TestServletContext extends MockServletContext {
284300

285301
@Override

0 commit comments

Comments
 (0)