Skip to content

Commit dc7ed57

Browse files
committed
Support resource URL encoding at context path
Issue: SPR-13757
1 parent beef5ff commit dc7ed57

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

patch.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java
2+
index e94a2a6..5b67c58 100644
3+
--- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java
4+
+++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilterTests.java
5+
@@ -89,6 +89,21 @@ public class ResourceUrlEncodingFilterTests {
6+
});
7+
}
8+
9+
+ // SPR-13757
10+
+ @Test
11+
+ public void encodeURLAtContextPath() throws Exception {
12+
+ MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context");
13+
+ request.setContextPath("/context");
14+
+ request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
15+
+ MockHttpServletResponse response = new MockHttpServletResponse();
16+
+
17+
+ this.filter.doFilterInternal(request, response, (request1, response1) -> {
18+
+ String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css");
19+
+ assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result);
20+
+ });
21+
+ }
22+
+
23+
+
24+
// SPR-13018
25+
@Test
26+
public void encodeEmptyURLWithContext() throws Exception {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ private void initIndexLookupPath(ResourceUrlProvider urlProvider) {
9595
String requestUri = urlProvider.getPathHelper().getRequestUri(this.request);
9696
String lookupPath = urlProvider.getPathHelper().getLookupPathForRequest(this.request);
9797
this.indexLookupPath = requestUri.lastIndexOf(lookupPath);
98+
99+
if ("/".equals(lookupPath) && !"/".equals(requestUri)) {
100+
String contextPath = urlProvider.getPathHelper().getContextPath(this.request);
101+
if (requestUri.equals(contextPath)) {
102+
this.indexLookupPath = requestUri.length();
103+
}
104+
}
98105
}
99106
}
100107

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ public void doFilter(ServletRequest request, ServletResponse response) throws IO
8989
});
9090
}
9191

92+
// SPR-13757
93+
@Test
94+
public void encodeContextPathUrlWithoutSuffix() throws Exception {
95+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context");
96+
request.setContextPath("/context");
97+
request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
98+
MockHttpServletResponse response = new MockHttpServletResponse();
99+
100+
this.filter.doFilterInternal(request, response, (request1, response1) -> {
101+
String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css");
102+
assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result);
103+
});
104+
}
105+
106+
@Test
107+
public void encodeContextPathUrlWithSuffix() throws Exception {
108+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/");
109+
request.setContextPath("/context");
110+
request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
111+
MockHttpServletResponse response = new MockHttpServletResponse();
112+
113+
this.filter.doFilterInternal(request, response, (request1, response1) -> {
114+
String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css");
115+
assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result);
116+
});
117+
}
118+
92119
// SPR-13018
93120
@Test
94121
public void encodeEmptyURLWithContext() throws Exception {

0 commit comments

Comments
 (0)