Full header support HTTP HEAD Resource requests

Allow the body to be written in order for all headers to be set
as they would be on HTTP GET. The body content is ignored as a
lower level.

See gh-25976
This commit is contained in:
Rossen Stoyanchev 2020-10-27 11:14:37 +00:00
parent d91b66a04c
commit bb4e802af5
4 changed files with 14 additions and 14 deletions

View File

@ -355,11 +355,6 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
setHeaders(exchange, resource, mediaType);
// Content phase
if (HttpMethod.HEAD.matches(exchange.getRequest().getMethodValue())) {
exchange.getResponse().getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
return Mono.empty();
}
ResourceHttpMessageWriter writer = getResourceHttpMessageWriter();
Assert.state(writer != null, "No ResourceHttpMessageWriter");
return writer.write(Mono.just(resource),

View File

@ -123,10 +123,6 @@ public class ResourceWebHandlerTests {
assertThat(resourceLastModifiedDate("test/foo.css") / 1000).isEqualTo(headers.getLastModified() / 1000);
assertThat(headers.getFirst("Accept-Ranges")).isEqualTo("bytes");
assertThat(headers.get("Accept-Ranges").size()).isEqualTo(1);
StepVerifier.create(exchange.getResponse().getBody())
.expectErrorMatches(ex -> ex.getMessage().startsWith("No content was written"))
.verify();
}
@Test

View File

@ -511,10 +511,6 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
setHeaders(response, resource, mediaType);
// Content phase
if (METHOD_HEAD.equals(request.getMethod())) {
return;
}
ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
if (request.getHeader(HttpHeaders.RANGE) == null) {
Assert.state(this.resourceHttpMessageConverter != null, "Not initialized");

View File

@ -118,7 +118,6 @@ public class ResourceHttpRequestHandlerTests {
assertThat(this.response.getDateHeader("Last-Modified") / 1000).isEqualTo(resourceLastModified("test/foo.css") / 1000);
assertThat(this.response.getHeader("Accept-Ranges")).isEqualTo("bytes");
assertThat(this.response.getHeaders("Accept-Ranges").size()).isEqualTo(1);
assertThat(this.response.getContentAsByteArray().length).isEqualTo(0);
}
@Test
@ -686,6 +685,20 @@ public class ResourceHttpRequestHandlerTests {
assertThat(this.response.getHeaderValues("Vary")).containsExactly("Accept-Encoding");
}
@Test // gh-25976
public void partialContentWithHttpHead() throws Exception {
this.request.setMethod("HEAD");
this.request.addHeader("Range", "bytes=0-1");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt");
this.handler.handleRequest(this.request, this.response);
assertThat(this.response.getStatus()).isEqualTo(206);
assertThat(this.response.getContentType()).isEqualTo("text/plain");
assertThat(this.response.getContentLength()).isEqualTo(2);
assertThat(this.response.getHeader("Content-Range")).isEqualTo("bytes 0-1/10");
assertThat(this.response.getHeaderValues("Accept-Ranges")).containsExactly("bytes");
}
@Test // SPR-14005
public void doOverwriteExistingCacheControlHeaders() throws Exception {
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");