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
This commit is contained in:
Brian Clozel
2015-02-27 19:10:41 +01:00
parent 9b26e4f1ad
commit 2d3fe96cd6
2 changed files with 19 additions and 0 deletions

View File

@@ -315,6 +315,9 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
resourcePath = resource.getURL().getPath();
locationPath = location.getURL().getPath();
}
if(locationPath.equals(resourcePath)) {
return true;
}
locationPath = (locationPath.endsWith("/") ||
!StringUtils.hasLength(locationPath) ? locationPath : locationPath + "/");
if (!resourcePath.startsWith(locationPath)) {

View File

@@ -280,6 +280,22 @@ public class ResourceHttpRequestHandlerTests {
}
// SPR-12747
@Test
public void getResourceWithResourceLocation() throws Exception {
List<Resource> resourcePaths = new ArrayList<Resource>();
resourcePaths.add(new ClassPathResource("test/foo.css", getClass()));
this.handler.setLocations(resourcePaths);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/foo.css");
request.setMethod("GET");
MockHttpServletResponse response = new MockHttpServletResponse();
handler.handleRequest(request, response);
assertEquals("text/css", response.getContentType());
assertEquals(17, response.getContentLength());
}
private static class TestServletContext extends MockServletContext {
@Override