diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java index beafe65d0d23..dd86b8e9a264 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -110,7 +110,7 @@ private Mono getResource(String resourcePath, List */ protected Mono getResource(String resourcePath, Resource location) { try { - if (location instanceof ClassPathResource) { + if (!(location instanceof UrlResource)) { resourcePath = UriUtils.decode(resourcePath, StandardCharsets.UTF_8); } Resource resource = location.createRelative(resourcePath); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java index 1232a9a60dfd..3c25602b2f52 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ import reactor.test.StepVerifier; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.core.io.buffer.DataBuffer; @@ -51,6 +52,7 @@ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse; import org.springframework.web.testfixture.server.MockServerWebExchange; +import org.springframework.web.util.UriUtils; import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; @@ -232,6 +234,25 @@ public void getResourceWithRegisteredMediaType() throws Exception { assertResponseBody(exchange, "foo bar foo bar foo bar"); } + @Test + public void getResourceFromFileSystem() throws Exception { + String path = new ClassPathResource("", getClass()).getFile().getCanonicalPath() + .replace("classes/java", "resources") + "/"; + + ResourceWebHandler handler = new ResourceWebHandler(); + handler.setLocations(Collections.singletonList(new FileSystemResource(path))); + handler.afterPropertiesSet(); + + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); + setPathWithinHandlerMapping(exchange, UriUtils.encodePath("test/фоо.css", UTF_8)); + handler.handle(exchange).block(TIMEOUT); + + HttpHeaders headers = exchange.getResponse().getHeaders(); + assertThat(headers.getContentType()).isEqualTo(MediaType.parseMediaType("text/css")); + assertThat(headers.getContentLength()).isEqualTo(17); + assertResponseBody(exchange, "h1 { color:red; }"); + } + @Test // SPR-14577 public void getMediaTypeWithFavorPathExtensionOff() throws Exception { List paths = Collections.singletonList(new ClassPathResource("test/", getClass())); diff --git "a/spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/\321\204\320\276\320\276.css" "b/spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/\321\204\320\276\320\276.css" new file mode 100644 index 000000000000..e2f0b1c742ae --- /dev/null +++ "b/spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/\321\204\320\276\320\276.css" @@ -0,0 +1 @@ +h1 { color:red; } \ No newline at end of file diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 82dea1581d6e..c6b364992741 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,9 +33,11 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; +import org.springframework.http.server.PathContainer; import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; import org.springframework.web.context.support.ServletContextResource; +import org.springframework.web.util.ServletRequestPathUtils; import org.springframework.web.util.UriUtils; import org.springframework.web.util.UrlPathHelper; @@ -151,7 +153,7 @@ private Resource getResource(String resourcePath, @Nullable HttpServletRequest r for (Resource location : locations) { try { - String pathToUse = encodeIfNecessary(resourcePath, request, location); + String pathToUse = encodeOrDecodeIfNecessary(resourcePath, request, location); Resource resource = getResource(pathToUse, location); if (resource != null) { return resource; @@ -255,8 +257,11 @@ else if (resource instanceof ServletContextResource) { return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); } - private String encodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) { - if (shouldEncodeRelativePath(location) && request != null) { + private String encodeOrDecodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) { + if (shouldDecodeRelativePath(location, request)) { + return UriUtils.decode(path, StandardCharsets.UTF_8); + } + else if (shouldEncodeRelativePath(location) && request != null) { Charset charset = this.locationCharsets.getOrDefault(location, StandardCharsets.UTF_8); StringBuilder sb = new StringBuilder(); StringTokenizer tokenizer = new StringTokenizer(path, "/"); @@ -275,8 +280,15 @@ private String encodeIfNecessary(String path, @Nullable HttpServletRequest reque } } + private boolean shouldDecodeRelativePath(Resource location, @Nullable HttpServletRequest request) { + return (!(location instanceof UrlResource) && request != null && + ServletRequestPathUtils.hasCachedPath(request) && + ServletRequestPathUtils.getCachedPath(request) instanceof PathContainer); + } + private boolean shouldEncodeRelativePath(Resource location) { - return (location instanceof UrlResource && this.urlPathHelper != null && this.urlPathHelper.isUrlDecode()); + return (location instanceof UrlResource && + this.urlPathHelper != null && this.urlPathHelper.isUrlDecode()); } private boolean isInvalidEncodedPath(String resourcePath) { diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java new file mode 100644 index 000000000000..faf8f1dc0b82 --- /dev/null +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java @@ -0,0 +1,183 @@ +/* + * Copyright 2002-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.web.servlet.resource; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.stream.Stream; + +import javax.servlet.ServletException; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.UrlResource; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.testfixture.servlet.MockHttpServletRequest; +import org.springframework.web.testfixture.servlet.MockHttpServletResponse; +import org.springframework.web.testfixture.servlet.MockServletConfig; +import org.springframework.web.testfixture.servlet.MockServletContext; +import org.springframework.web.util.UriUtils; +import org.springframework.web.util.pattern.PathPatternParser; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +/** + * Integration tests for static resource handling. + * @author Rossen Stoyanchev + */ +public class ResourceHttpRequestHandlerIntegrationTests { + + private final MockServletContext servletContext = new MockServletContext(); + + private final MockServletConfig servletConfig = new MockServletConfig(this.servletContext); + + + public static Stream argumentSource() { + return Stream.of( + arguments(true, "/cp"), + arguments(true, "/fs"), + arguments(true, "/url"), + arguments(false, "/cp"), + arguments(false, "/fs"), + arguments(false, "/url") + ); + } + + + @ParameterizedTest + @MethodSource("argumentSource") + void cssFile(boolean usePathPatterns, String pathPrefix) throws Exception { + MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo.css"); + MockHttpServletResponse response = new MockHttpServletResponse(); + + DispatcherServlet servlet = initDispatcherServlet(usePathPatterns, WebConfig.class); + servlet.service(request, response); + + String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix; + assertThat(response.getStatus()).as(description).isEqualTo(200); + assertThat(response.getContentType()).as(description).isEqualTo("text/css"); + assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }"); + } + + @ParameterizedTest + @MethodSource("argumentSource") + void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception { + MockHttpServletRequest request = initRequest(pathPrefix + "/test/фоо.css"); + MockHttpServletResponse response = new MockHttpServletResponse(); + + DispatcherServlet servlet = initDispatcherServlet(usePathPatterns, WebConfig.class); + servlet.service(request, response); + + String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix; + assertThat(response.getStatus()).as(description).isEqualTo(200); + assertThat(response.getContentType()).as(description).isEqualTo("text/css"); + assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }"); + } + + private DispatcherServlet initDispatcherServlet(boolean usePathPatterns, Class... configClasses) + throws ServletException { + + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(configClasses); + if (usePathPatterns) { + context.register(PathPatternParserConfig.class); + } + context.setServletConfig(this.servletConfig); + context.refresh(); + + DispatcherServlet servlet = new DispatcherServlet(); + servlet.setApplicationContext(context); + servlet.init(this.servletConfig); + return servlet; + } + + private MockHttpServletRequest initRequest(String path) { + path = UriUtils.encodePath(path, StandardCharsets.UTF_8); + MockHttpServletRequest request = new MockHttpServletRequest("GET", path); + request.setCharacterEncoding(StandardCharsets.UTF_8.name()); + return request; + } + + + @EnableWebMvc + static class WebConfig implements WebMvcConfigurer { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + ClassPathResource classPathLocation = new ClassPathResource("", getClass()); + String path = getPath(classPathLocation); + + registerClasspathLocation("/cp/**", classPathLocation, registry); + registerFileSystemLocation("/fs/**", path, registry); + registerUrlLocation("/url/**", "file://" + path, registry); + } + + protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) { + registry.addResourceHandler(pattern).addResourceLocations(resource); + } + + protected void registerFileSystemLocation(String pattern, String path, ResourceHandlerRegistry registry) { + FileSystemResource fileSystemLocation = new FileSystemResource(path); + registry.addResourceHandler(pattern).addResourceLocations(fileSystemLocation); + } + + protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) { + UrlResource urlLocation = new UrlResource(toURL(path)); + registry.addResourceHandler(pattern).addResourceLocations(urlLocation); + } + + private String getPath(ClassPathResource resource) { + try { + return resource.getFile().getCanonicalPath().replace("classes/java", "resources") + "/"; + } + catch (IOException ex) { + throw new IllegalStateException(ex); + } + } + + private URL toURL(String path) { + try { + return URI.create(path).toURL(); + } + catch (MalformedURLException ex) { + throw new IllegalStateException(ex); + } + } + } + + + static class PathPatternParserConfig implements WebMvcConfigurer { + + @Override + public void configurePathMatch(PathMatchConfigurer configurer) { + configurer.setPatternParser(new PathPatternParser()); + } + } + +} diff --git "a/spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/\321\204\320\276\320\276.css" "b/spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/\321\204\320\276\320\276.css" new file mode 100644 index 000000000000..e2f0b1c742ae --- /dev/null +++ "b/spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/\321\204\320\276\320\276.css" @@ -0,0 +1 @@ +h1 { color:red; } \ No newline at end of file