Skip to content

Commit 24cef14

Browse files
committed
Merge branch '5.2.x' into master
2 parents b01adad + bb4e802 commit 24cef14

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

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

Lines changed: 3 additions & 8 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-2020 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.
@@ -375,15 +375,9 @@ public Mono<Void> handle(ServerWebExchange exchange) {
375375

376376
// Check the media type for the resource
377377
MediaType mediaType = MediaTypeFactory.getMediaType(resource).orElse(null);
378+
setHeaders(exchange, resource, mediaType);
378379

379380
// Content phase
380-
if (HttpMethod.HEAD.matches(exchange.getRequest().getMethodValue())) {
381-
setHeaders(exchange, resource, mediaType);
382-
exchange.getResponse().getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
383-
return Mono.empty();
384-
}
385-
386-
setHeaders(exchange, resource, mediaType);
387381
ResourceHttpMessageWriter writer = getResourceHttpMessageWriter();
388382
Assert.state(writer != null, "No ResourceHttpMessageWriter");
389383
return writer.write(Mono.just(resource),
@@ -558,6 +552,7 @@ protected void setHeaders(ServerWebExchange exchange, Resource resource, @Nullab
558552
if (mediaType != null) {
559553
headers.setContentType(mediaType);
560554
}
555+
561556
if (resource instanceof HttpResource) {
562557
HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
563558
exchange.getResponse().getHeaders().putAll(resourceHeaders);

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ public void getResourceHttpHeader() throws Exception {
118118
assertThat(resourceLastModifiedDate("test/foo.css") / 1000).isEqualTo(headers.getLastModified() / 1000);
119119
assertThat(headers.getFirst("Accept-Ranges")).isEqualTo("bytes");
120120
assertThat(headers.get("Accept-Ranges").size()).isEqualTo(1);
121-
122-
StepVerifier.create(exchange.getResponse().getBody())
123-
.expectErrorMatches(ex -> ex.getMessage().startsWith("No content was written"))
124-
.verify();
125121
}
126122

127123
@Test

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,22 +531,16 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
531531

532532
// Check the media type for the resource
533533
MediaType mediaType = getMediaType(request, resource);
534+
setHeaders(response, resource, mediaType);
534535

535536
// Content phase
536-
if (METHOD_HEAD.equals(request.getMethod())) {
537-
setHeaders(response, resource, mediaType);
538-
return;
539-
}
540-
541537
ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
542538
if (request.getHeader(HttpHeaders.RANGE) == null) {
543539
Assert.state(this.resourceHttpMessageConverter != null, "Not initialized");
544-
setHeaders(response, resource, mediaType);
545540
this.resourceHttpMessageConverter.write(resource, mediaType, outputMessage);
546541
}
547542
else {
548543
Assert.state(this.resourceRegionHttpMessageConverter != null, "Not initialized");
549-
response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
550544
ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(request);
551545
try {
552546
List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
@@ -555,7 +549,7 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
555549
HttpRange.toResourceRegions(httpRanges, resource), mediaType, outputMessage);
556550
}
557551
catch (IllegalArgumentException ex) {
558-
response.setHeader("Content-Range", "bytes */" + resource.contentLength());
552+
response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
559553
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
560554
}
561555
}
@@ -773,6 +767,7 @@ protected void setHeaders(HttpServletResponse response, Resource resource, @Null
773767
if (mediaType != null) {
774768
response.setContentType(mediaType.toString());
775769
}
770+
776771
if (resource instanceof HttpResource) {
777772
HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
778773
resourceHeaders.forEach((headerName, headerValues) -> {
@@ -788,6 +783,7 @@ protected void setHeaders(HttpServletResponse response, Resource resource, @Null
788783
}
789784
});
790785
}
786+
791787
response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
792788
}
793789

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.ExtendWith;
2930

3031
import org.springframework.core.io.ClassPathResource;
3132
import org.springframework.core.io.Resource;
@@ -58,6 +59,7 @@
5859
* @author Rossen Stoyanchev
5960
* @author Brian Clozel
6061
*/
62+
@ExtendWith(GzipSupport.class)
6163
public class ResourceHttpRequestHandlerTests {
6264

6365
private ResourceHttpRequestHandler handler;
@@ -116,7 +118,6 @@ public void getResourceHttpHeader() throws Exception {
116118
assertThat(this.response.getDateHeader("Last-Modified") / 1000).isEqualTo(resourceLastModified("test/foo.css") / 1000);
117119
assertThat(this.response.getHeader("Accept-Ranges")).isEqualTo("bytes");
118120
assertThat(this.response.getHeaders("Accept-Ranges").size()).isEqualTo(1);
119-
assertThat(this.response.getContentAsByteArray().length).isEqualTo(0);
120121
}
121122

122123
@Test
@@ -657,6 +658,49 @@ public void partialContentMultipleByteRanges() throws Exception {
657658
assertThat(ranges[11]).isEqualTo("t.");
658659
}
659660

661+
@Test // gh-25976
662+
public void partialContentByteRangeWithEncodedResource(GzipSupport.GzippedFiles gzippedFiles) throws Exception {
663+
String path = "js/foo.js";
664+
gzippedFiles.create(path);
665+
666+
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
667+
handler.setResourceResolvers(Arrays.asList(new EncodedResourceResolver(), new PathResourceResolver()));
668+
handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass())));
669+
handler.setServletContext(new MockServletContext());
670+
handler.afterPropertiesSet();
671+
672+
this.request.addHeader("Accept-Encoding", "gzip");
673+
this.request.addHeader("Range", "bytes=0-1");
674+
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, path);
675+
handler.handleRequest(this.request, this.response);
676+
677+
assertThat(this.response.getStatus()).isEqualTo(206);
678+
assertThat(this.response.getHeaderNames()).containsExactlyInAnyOrder(
679+
"Content-Type", "Content-Length", "Content-Range", "Accept-Ranges",
680+
"Last-Modified", "Content-Encoding", "Vary");
681+
682+
assertThat(this.response.getContentType()).isEqualTo("text/javascript");
683+
assertThat(this.response.getContentLength()).isEqualTo(2);
684+
assertThat(this.response.getHeader("Content-Range")).isEqualTo("bytes 0-1/66");
685+
assertThat(this.response.getHeaderValues("Accept-Ranges")).containsExactly("bytes");
686+
assertThat(this.response.getHeaderValues("Content-Encoding")).containsExactly("gzip");
687+
assertThat(this.response.getHeaderValues("Vary")).containsExactly("Accept-Encoding");
688+
}
689+
690+
@Test // gh-25976
691+
public void partialContentWithHttpHead() throws Exception {
692+
this.request.setMethod("HEAD");
693+
this.request.addHeader("Range", "bytes=0-1");
694+
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt");
695+
this.handler.handleRequest(this.request, this.response);
696+
697+
assertThat(this.response.getStatus()).isEqualTo(206);
698+
assertThat(this.response.getContentType()).isEqualTo("text/plain");
699+
assertThat(this.response.getContentLength()).isEqualTo(2);
700+
assertThat(this.response.getHeader("Content-Range")).isEqualTo("bytes 0-1/10");
701+
assertThat(this.response.getHeaderValues("Accept-Ranges")).containsExactly("bytes");
702+
}
703+
660704
@Test // SPR-14005
661705
public void doOverwriteExistingCacheControlHeaders() throws Exception {
662706
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");

0 commit comments

Comments
 (0)