提交 6aef1a1d 编写于 作者: B Brian Clozel

Fix ResourceUrlProvider path check in getForRequestUrl

Prior to this change, getForRequestUrl implementation would only work
for applications with a non-empty servlet path. So web applications
mapped to "/" would trigger a IllegalStateException while checking the
current request against the request path within the current mapping.

This change relaxes this and only check that the path within mapping is
within the request URL.

Issue: SPR-12158
上级 1c2857d1
......@@ -180,7 +180,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
}
int index = getPathHelper().getRequestUri(request).indexOf(pathWithinMapping);
Assert.state(index > 0 && index < requestUrl.length(), "Failed to determine lookup path: " + requestUrl);
Assert.state(index != -1, "Failed to determine lookup path: " + requestUrl);
String prefix = requestUrl.substring(0, index);
String lookupPath = requestUrl.substring(index);
......
......@@ -73,7 +73,7 @@ public class ResourceTransformerSupportTests {
}
@Test
public void rewriteAbsolutePath() throws Exception {
public void rewriteAbsolutePathWithContext() throws Exception {
this.request.setRequestURI("/servlet/context/resources/main.css");
this.request.setMethod("GET");
this.request.setServletPath("/servlet");
......@@ -84,6 +84,18 @@ public class ResourceTransformerSupportTests {
Resource mainCss = new ClassPathResource("test/main.css", getClass());
String actual = this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain);
assertEquals("/servlet/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
@Test
public void rewriteAbsolutePath() throws Exception {
this.request.setRequestURI("/resources/main.css");
this.request.setMethod("GET");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/resources/main.css");
String resourcePath = "/resources/bar.css";
Resource mainCss = new ClassPathResource("test/main.css", getClass());
String actual = this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain);
assertEquals("/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
actual = this.transformer.resolveUrlPath("bar.css", this.request, mainCss, this.transformerChain);
assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
......@@ -102,6 +114,19 @@ public class ResourceTransformerSupportTests {
assertEquals("bar-11e16cf79faee7ac698c805cf28248d2.css", actual);
}
@Test(expected = IllegalStateException.class)
public void rewriteAbsolutePathWrongPath() throws Exception {
this.request.setRequestURI("/servlet/context/resources/main.css");
this.request.setMethod("GET");
this.request.setServletPath("/servlet");
this.request.setContextPath("/context");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/wrong/main.css");
String resourcePath = "/servlet/context/resources/bar.css";
Resource mainCss = new ClassPathResource("test/main.css", getClass());
this.transformer.resolveUrlPath(resourcePath, this.request, mainCss, this.transformerChain);
}
@Test
public void rewriteRelativePathUpperLevel() throws Exception {
this.request.setRequestURI("/servlet/context/resources/images/image.png");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册