Skip to content

Commit a08593b

Browse files
committed
Correct matching of static resources with parsed patterns
Closes gh-26775
1 parent 7954dc7 commit a08593b

File tree

6 files changed

+226
-8
lines changed

6 files changed

+226
-8
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ private Mono<Resource> getResource(String resourcePath, List<? extends Resource>
110110
*/
111111
protected Mono<Resource> getResource(String resourcePath, Resource location) {
112112
try {
113-
if (location instanceof ClassPathResource) {
113+
if (!(location instanceof UrlResource)) {
114114
resourcePath = UriUtils.decode(resourcePath, StandardCharsets.UTF_8);
115115
}
116116
Resource resource = location.createRelative(resourcePath);

spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import reactor.test.StepVerifier;
3333

3434
import org.springframework.core.io.ClassPathResource;
35+
import org.springframework.core.io.FileSystemResource;
3536
import org.springframework.core.io.Resource;
3637
import org.springframework.core.io.UrlResource;
3738
import org.springframework.core.io.buffer.DataBuffer;
@@ -51,6 +52,7 @@
5152
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
5253
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse;
5354
import org.springframework.web.testfixture.server.MockServerWebExchange;
55+
import org.springframework.web.util.UriUtils;
5456

5557
import static java.nio.charset.StandardCharsets.UTF_8;
5658
import static org.assertj.core.api.Assertions.assertThat;
@@ -232,6 +234,25 @@ public void getResourceWithRegisteredMediaType() throws Exception {
232234
assertResponseBody(exchange, "foo bar foo bar foo bar");
233235
}
234236

237+
@Test
238+
public void getResourceFromFileSystem() throws Exception {
239+
String path = new ClassPathResource("", getClass()).getFile().getCanonicalPath()
240+
.replace("classes/java", "resources") + "/";
241+
242+
ResourceWebHandler handler = new ResourceWebHandler();
243+
handler.setLocations(Collections.singletonList(new FileSystemResource(path)));
244+
handler.afterPropertiesSet();
245+
246+
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
247+
setPathWithinHandlerMapping(exchange, UriUtils.encodePath("test/фоо.css", UTF_8));
248+
handler.handle(exchange).block(TIMEOUT);
249+
250+
HttpHeaders headers = exchange.getResponse().getHeaders();
251+
assertThat(headers.getContentType()).isEqualTo(MediaType.parseMediaType("text/css"));
252+
assertThat(headers.getContentLength()).isEqualTo(17);
253+
assertResponseBody(exchange, "h1 { color:red; }");
254+
}
255+
235256
@Test // SPR-14577
236257
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
237258
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h1 { color:red; }

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,9 +33,11 @@
3333
import org.springframework.core.io.ClassPathResource;
3434
import org.springframework.core.io.Resource;
3535
import org.springframework.core.io.UrlResource;
36+
import org.springframework.http.server.PathContainer;
3637
import org.springframework.lang.Nullable;
3738
import org.springframework.util.StringUtils;
3839
import org.springframework.web.context.support.ServletContextResource;
40+
import org.springframework.web.util.ServletRequestPathUtils;
3941
import org.springframework.web.util.UriUtils;
4042
import org.springframework.web.util.UrlPathHelper;
4143

@@ -151,7 +153,7 @@ private Resource getResource(String resourcePath, @Nullable HttpServletRequest r
151153

152154
for (Resource location : locations) {
153155
try {
154-
String pathToUse = encodeIfNecessary(resourcePath, request, location);
156+
String pathToUse = encodeOrDecodeIfNecessary(resourcePath, request, location);
155157
Resource resource = getResource(pathToUse, location);
156158
if (resource != null) {
157159
return resource;
@@ -255,8 +257,11 @@ else if (resource instanceof ServletContextResource) {
255257
return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath));
256258
}
257259

258-
private String encodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) {
259-
if (shouldEncodeRelativePath(location) && request != null) {
260+
private String encodeOrDecodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) {
261+
if (shouldDecodeRelativePath(location, request)) {
262+
return UriUtils.decode(path, StandardCharsets.UTF_8);
263+
}
264+
else if (shouldEncodeRelativePath(location) && request != null) {
260265
Charset charset = this.locationCharsets.getOrDefault(location, StandardCharsets.UTF_8);
261266
StringBuilder sb = new StringBuilder();
262267
StringTokenizer tokenizer = new StringTokenizer(path, "/");
@@ -275,8 +280,15 @@ private String encodeIfNecessary(String path, @Nullable HttpServletRequest reque
275280
}
276281
}
277282

283+
private boolean shouldDecodeRelativePath(Resource location, @Nullable HttpServletRequest request) {
284+
return (!(location instanceof UrlResource) && request != null &&
285+
ServletRequestPathUtils.hasCachedPath(request) &&
286+
ServletRequestPathUtils.getCachedPath(request) instanceof PathContainer);
287+
}
288+
278289
private boolean shouldEncodeRelativePath(Resource location) {
279-
return (location instanceof UrlResource && this.urlPathHelper != null && this.urlPathHelper.isUrlDecode());
290+
return (location instanceof UrlResource &&
291+
this.urlPathHelper != null && this.urlPathHelper.isUrlDecode());
280292
}
281293

282294
private boolean isInvalidEncodedPath(String resourcePath) {
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.servlet.resource;
17+
18+
import java.io.IOException;
19+
import java.net.MalformedURLException;
20+
import java.net.URI;
21+
import java.net.URL;
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.stream.Stream;
24+
25+
import javax.servlet.ServletException;
26+
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.Arguments;
29+
import org.junit.jupiter.params.provider.MethodSource;
30+
31+
import org.springframework.core.io.ClassPathResource;
32+
import org.springframework.core.io.FileSystemResource;
33+
import org.springframework.core.io.UrlResource;
34+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
35+
import org.springframework.web.servlet.DispatcherServlet;
36+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
37+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
38+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
39+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
40+
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
41+
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
42+
import org.springframework.web.testfixture.servlet.MockServletConfig;
43+
import org.springframework.web.testfixture.servlet.MockServletContext;
44+
import org.springframework.web.util.UriUtils;
45+
import org.springframework.web.util.pattern.PathPatternParser;
46+
47+
import static org.assertj.core.api.Assertions.assertThat;
48+
import static org.junit.jupiter.params.provider.Arguments.arguments;
49+
50+
/**
51+
* Integration tests for static resource handling.
52+
* @author Rossen Stoyanchev
53+
*/
54+
public class ResourceHttpRequestHandlerIntegrationTests {
55+
56+
private final MockServletContext servletContext = new MockServletContext();
57+
58+
private final MockServletConfig servletConfig = new MockServletConfig(this.servletContext);
59+
60+
61+
public static Stream<Arguments> argumentSource() {
62+
return Stream.of(
63+
arguments(true, "/cp"),
64+
arguments(true, "/fs"),
65+
arguments(true, "/url"),
66+
arguments(false, "/cp"),
67+
arguments(false, "/fs"),
68+
arguments(false, "/url")
69+
);
70+
}
71+
72+
73+
@ParameterizedTest
74+
@MethodSource("argumentSource")
75+
void cssFile(boolean usePathPatterns, String pathPrefix) throws Exception {
76+
MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo.css");
77+
MockHttpServletResponse response = new MockHttpServletResponse();
78+
79+
DispatcherServlet servlet = initDispatcherServlet(usePathPatterns, WebConfig.class);
80+
servlet.service(request, response);
81+
82+
String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix;
83+
assertThat(response.getStatus()).as(description).isEqualTo(200);
84+
assertThat(response.getContentType()).as(description).isEqualTo("text/css");
85+
assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }");
86+
}
87+
88+
@ParameterizedTest
89+
@MethodSource("argumentSource")
90+
void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception {
91+
MockHttpServletRequest request = initRequest(pathPrefix + "/test/фоо.css");
92+
MockHttpServletResponse response = new MockHttpServletResponse();
93+
94+
DispatcherServlet servlet = initDispatcherServlet(usePathPatterns, WebConfig.class);
95+
servlet.service(request, response);
96+
97+
String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix;
98+
assertThat(response.getStatus()).as(description).isEqualTo(200);
99+
assertThat(response.getContentType()).as(description).isEqualTo("text/css");
100+
assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }");
101+
}
102+
103+
private DispatcherServlet initDispatcherServlet(boolean usePathPatterns, Class<?>... configClasses)
104+
throws ServletException {
105+
106+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
107+
context.register(configClasses);
108+
if (usePathPatterns) {
109+
context.register(PathPatternParserConfig.class);
110+
}
111+
context.setServletConfig(this.servletConfig);
112+
context.refresh();
113+
114+
DispatcherServlet servlet = new DispatcherServlet();
115+
servlet.setApplicationContext(context);
116+
servlet.init(this.servletConfig);
117+
return servlet;
118+
}
119+
120+
private MockHttpServletRequest initRequest(String path) {
121+
path = UriUtils.encodePath(path, StandardCharsets.UTF_8);
122+
MockHttpServletRequest request = new MockHttpServletRequest("GET", path);
123+
request.setCharacterEncoding(StandardCharsets.UTF_8.name());
124+
return request;
125+
}
126+
127+
128+
@EnableWebMvc
129+
static class WebConfig implements WebMvcConfigurer {
130+
131+
@Override
132+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
133+
ClassPathResource classPathLocation = new ClassPathResource("", getClass());
134+
String path = getPath(classPathLocation);
135+
136+
registerClasspathLocation("/cp/**", classPathLocation, registry);
137+
registerFileSystemLocation("/fs/**", path, registry);
138+
registerUrlLocation("/url/**", "file://" + path, registry);
139+
}
140+
141+
protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) {
142+
registry.addResourceHandler(pattern).addResourceLocations(resource);
143+
}
144+
145+
protected void registerFileSystemLocation(String pattern, String path, ResourceHandlerRegistry registry) {
146+
FileSystemResource fileSystemLocation = new FileSystemResource(path);
147+
registry.addResourceHandler(pattern).addResourceLocations(fileSystemLocation);
148+
}
149+
150+
protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) {
151+
UrlResource urlLocation = new UrlResource(toURL(path));
152+
registry.addResourceHandler(pattern).addResourceLocations(urlLocation);
153+
}
154+
155+
private String getPath(ClassPathResource resource) {
156+
try {
157+
return resource.getFile().getCanonicalPath().replace("classes/java", "resources") + "/";
158+
}
159+
catch (IOException ex) {
160+
throw new IllegalStateException(ex);
161+
}
162+
}
163+
164+
private URL toURL(String path) {
165+
try {
166+
return URI.create(path).toURL();
167+
}
168+
catch (MalformedURLException ex) {
169+
throw new IllegalStateException(ex);
170+
}
171+
}
172+
}
173+
174+
175+
static class PathPatternParserConfig implements WebMvcConfigurer {
176+
177+
@Override
178+
public void configurePathMatch(PathMatchConfigurer configurer) {
179+
configurer.setPatternParser(new PathPatternParser());
180+
}
181+
}
182+
183+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h1 { color:red; }

0 commit comments

Comments
 (0)