From dc7ed57c67a6b9d28a63e87e309355a9dae80845 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 10 Dec 2015 12:24:04 -0500 Subject: [PATCH] Support resource URL encoding at context path Issue: SPR-13757 --- patch.txt | 26 ++++++++++++++++++ .../resource/ResourceUrlEncodingFilter.java | 7 +++++ .../ResourceUrlEncodingFilterTests.java | 27 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 patch.txt diff --git a/patch.txt b/patch.txt new file mode 100644 index 0000000000..ba1b83c05f --- /dev/null +++ b/patch.txt @@ -0,0 +1,26 @@ +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 +index e94a2a6..5b67c58 100644 +--- 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 +@@ -89,6 +89,21 @@ public class ResourceUrlEncodingFilterTests { + }); + } + ++ // SPR-13757 ++ @Test ++ public void encodeURLAtContextPath() throws Exception { ++ MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context"); ++ request.setContextPath("/context"); ++ request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider); ++ MockHttpServletResponse response = new MockHttpServletResponse(); ++ ++ this.filter.doFilterInternal(request, response, (request1, response1) -> { ++ String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css"); ++ assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result); ++ }); ++ } ++ ++ + // SPR-13018 + @Test + public void encodeEmptyURLWithContext() throws Exception { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java index 22251e5d3e..bc94707a4e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlEncodingFilter.java @@ -95,6 +95,13 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter { String requestUri = urlProvider.getPathHelper().getRequestUri(this.request); String lookupPath = urlProvider.getPathHelper().getLookupPathForRequest(this.request); this.indexLookupPath = requestUri.lastIndexOf(lookupPath); + + if ("/".equals(lookupPath) && !"/".equals(requestUri)) { + String contextPath = urlProvider.getPathHelper().getContextPath(this.request); + if (requestUri.equals(contextPath)) { + this.indexLookupPath = requestUri.length(); + } + } } } 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 index bcd6bfc33d..964919484a 100644 --- 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 @@ -89,6 +89,33 @@ public class ResourceUrlEncodingFilterTests { }); } + // SPR-13757 + @Test + public void encodeContextPathUrlWithoutSuffix() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context"); + request.setContextPath("/context"); + request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider); + MockHttpServletResponse response = new MockHttpServletResponse(); + + this.filter.doFilterInternal(request, response, (request1, response1) -> { + String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css"); + assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result); + }); + } + + @Test + public void encodeContextPathUrlWithSuffix() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/context/"); + request.setContextPath("/context"); + request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider); + MockHttpServletResponse response = new MockHttpServletResponse(); + + this.filter.doFilterInternal(request, response, (request1, response1) -> { + String result = ((HttpServletResponse) response1).encodeURL("/context/resources/bar.css"); + assertEquals("/context/resources/bar-11e16cf79faee7ac698c805cf28248d2.css", result); + }); + } + // SPR-13018 @Test public void encodeEmptyURLWithContext() throws Exception { -- GitLab