Skip to content

Commit 364c7c4

Browse files
committed
Merge branch '5.1.x'
2 parents b3aebf9 + 6d8bf34 commit 364c7c4

File tree

10 files changed

+115
-15
lines changed

10 files changed

+115
-15
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.core.io.buffer.DataBuffer;
2626
import org.springframework.core.io.buffer.DataBufferUtils;
27+
import org.springframework.http.HttpHeaders;
2728

2829
/**
2930
* {@link ServerHttpResponse} decorator for HTTP HEAD requests.
@@ -52,7 +53,11 @@ public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
5253
DataBufferUtils.release(buffer);
5354
return next;
5455
})
55-
.doOnNext(count -> getHeaders().setContentLength(count))
56+
.doOnNext(length -> {
57+
if (length > 0 || getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH) == null) {
58+
getHeaders().setContentLength(length);
59+
}
60+
})
5661
.then();
5762
}
5863

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2019 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.http.server.reactive;
17+
18+
import java.nio.charset.StandardCharsets;
19+
20+
import io.netty.buffer.PooledByteBufAllocator;
21+
import org.junit.After;
22+
import org.junit.Test;
23+
import reactor.core.publisher.Flux;
24+
25+
import org.springframework.core.io.buffer.DataBuffer;
26+
import org.springframework.core.io.buffer.LeakAwareDataBufferFactory;
27+
import org.springframework.core.io.buffer.NettyDataBufferFactory;
28+
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* Unit tests for {@link HttpHeadResponseDecorator}.
34+
* @author Rossen Stoyanchev
35+
*/
36+
public class HttpHeadResponseDecoratorTests {
37+
38+
private final LeakAwareDataBufferFactory bufferFactory =
39+
new LeakAwareDataBufferFactory(new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT));
40+
41+
private final ServerHttpResponse response =
42+
new HttpHeadResponseDecorator(new MockServerHttpResponse(this.bufferFactory));
43+
44+
45+
@After
46+
public void tearDown() {
47+
this.bufferFactory.checkForLeaks();
48+
}
49+
50+
51+
@Test
52+
public void write() {
53+
Flux<DataBuffer> body = Flux.just(toDataBuffer("data1"), toDataBuffer("data2"));
54+
response.writeWith(body).block();
55+
assertEquals(10, response.getHeaders().getContentLength());
56+
}
57+
58+
@Test // gh-23484
59+
public void writeWithGivenContentLength() {
60+
int length = 15;
61+
this.response.getHeaders().setContentLength(length);
62+
this.response.writeWith(Flux.empty()).block();
63+
assertEquals(length, this.response.getHeaders().getContentLength());
64+
}
65+
66+
67+
private DataBuffer toDataBuffer(String s) {
68+
DataBuffer buffer = this.bufferFactory.allocateBuffer();
69+
buffer.write(s.getBytes(StandardCharsets.UTF_8));
70+
return buffer;
71+
}
72+
73+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ private boolean isInvalidEncodedPath(String resourcePath) {
203203
return true;
204204
}
205205
}
206+
catch (IllegalArgumentException ex) {
207+
// May not be possible to decode...
208+
}
206209
catch (UnsupportedEncodingException ex) {
207210
// Should never happen...
208211
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -468,7 +468,10 @@ private boolean isInvalidEncodedPath(String path) {
468468
return true;
469469
}
470470
}
471-
catch (IllegalArgumentException | UnsupportedEncodingException ex) {
471+
catch (IllegalArgumentException ex) {
472+
// May not be possible to decode...
473+
}
474+
catch (UnsupportedEncodingException ex) {
472475
// Should never happen...
473476
}
474477
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ private void testCheckResource(Resource location, String requestPath) throws IOE
106106
assertThat(actual).isNull();
107107
}
108108

109+
@Test // gh-23463
110+
public void ignoreInvalidEscapeSequence() throws IOException {
111+
UrlResource location = new UrlResource(getClass().getResource("./test/"));
112+
Resource resource = location.createRelative("test%file.txt");
113+
assertTrue(this.resolver.checkResource(resource, location));
114+
}
115+
109116
@Test
110117
public void checkResourceWithAllowedLocations() {
111118
this.resolver.setAllowedLocations(

spring-webflux/src/test/resources/org/springframework/web/reactive/resource/test/test%file.txt

Whitespace-only changes.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -288,6 +288,9 @@ private boolean isInvalidEncodedPath(String resourcePath) {
288288
return true;
289289
}
290290
}
291+
catch (IllegalArgumentException ex) {
292+
// May not be possible to decode...
293+
}
291294
catch (UnsupportedEncodingException ex) {
292295
// Should never happen...
293296
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -605,7 +605,10 @@ private boolean isInvalidEncodedPath(String path) {
605605
return true;
606606
}
607607
}
608-
catch (IllegalArgumentException | UnsupportedEncodingException ex) {
608+
catch (IllegalArgumentException ex) {
609+
// May not be possible to decode...
610+
}
611+
catch (UnsupportedEncodingException ex) {
609612
// Should never happen...
610613
}
611614
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ private void testCheckResource(Resource location, String requestPath) throws IOE
9191
assertThat(actual).isNull();
9292
}
9393

94+
@Test // gh-23463
95+
public void ignoreInvalidEscapeSequence() throws IOException {
96+
UrlResource location = new UrlResource(getClass().getResource("./test/"));
97+
Resource resource = location.createRelative("test%file.txt");
98+
assertTrue(this.resolver.checkResource(resource, location));
99+
}
100+
94101
@Test
95102
public void checkResourceWithAllowedLocations() {
96103
this.resolver.setAllowedLocations(
@@ -104,8 +111,7 @@ public void checkResourceWithAllowedLocations() {
104111
assertThat(actual).isEqualTo("../testalternatepath/bar.css");
105112
}
106113

107-
// SPR-12432
108-
@Test
114+
@Test // SPR-12432
109115
public void checkServletContextResource() throws Exception {
110116
Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class);
111117
MockServletContext context = new MockServletContext();
@@ -117,24 +123,21 @@ public void checkServletContextResource() throws Exception {
117123
assertThat(this.resolver.checkResource(resource, servletContextLocation)).isTrue();
118124
}
119125

120-
// SPR-12624
121-
@Test
126+
@Test // SPR-12624
122127
public void checkRelativeLocation() throws Exception {
123128
String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm();
124129
Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework"));
125130

126131
assertThat(this.resolver.resolveResource(null, "main.css", Collections.singletonList(location), null)).isNotNull();
127132
}
128133

129-
// SPR-12747
130-
@Test
134+
@Test // SPR-12747
131135
public void checkFileLocation() throws Exception {
132136
Resource resource = getResource("main.css");
133137
assertThat(this.resolver.checkResource(resource, resource)).isTrue();
134138
}
135139

136-
// SPR-13241
137-
@Test
140+
@Test // SPR-13241
138141
public void resolvePathRootResource() {
139142
Resource webjarsLocation = new ClassPathResource("/META-INF/resources/webjars/", PathResourceResolver.class);
140143
String path = this.resolver.resolveUrlPathInternal("", Collections.singletonList(webjarsLocation), null);

spring-webmvc/src/test/resources/org/springframework/web/servlet/resource/test/test%file.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)