Skip to content

Commit

Permalink
Full header support HTTP HEAD Resource requests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed Oct 27, 2020
1 parent d91b66a commit bb4e802
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,6 @@ public Mono<Void> handle(ServerWebExchange exchange) {
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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ public void getResourceHttpHeader() throws Exception {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,6 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public void getResourceHttpHeader() throws Exception {
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
Expand Down Expand Up @@ -686,6 +685,20 @@ public void partialContentByteRangeWithEncodedResource(GzipSupport.GzippedFiles
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");
Expand Down

0 comments on commit bb4e802

Please sign in to comment.