From bb4e802af5add60823672482ffa61d4c7aa4890e Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 27 Oct 2020 11:14:37 +0000 Subject: [PATCH] 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 --- .../web/reactive/resource/ResourceWebHandler.java | 5 ----- .../resource/ResourceWebHandlerTests.java | 4 ---- .../resource/ResourceHttpRequestHandler.java | 4 ---- .../resource/ResourceHttpRequestHandlerTests.java | 15 ++++++++++++++- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java index 5d33a3d449ed..9da249f7c0ed 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java @@ -355,11 +355,6 @@ public Mono 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), diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java index 7bdbeff94529..3b7d8204ab88 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java @@ -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 diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index cbf5659fb43c..dbd56fffa6cf 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java @@ -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"); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index d6a9f2b7ff29..9aa44543105b 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -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 @@ -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");