Browse Source

Reset respone content-type for invalid range responses

Prior to this commit, the `ResourceHttpRequestHandler` would detect
invalid range requests and reply with a 416 response status and the
relevant range header. Because this was triggering an error dispatch,
the error handling would collect error metadata and produce an error
response with the original content-type.
This would most likely fail because the content-type is most likely a
file-related media type which cannot be used for error responses.

This commit resets the response content type in these cases and let the
error handling pick the most sensible media type for the error response.

Fixes gh- 34490
pull/35405/head
Brian Clozel 6 months ago
parent
commit
faada70d59
  1. 1
      spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java
  2. 2
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

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

@ -631,6 +631,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator @@ -631,6 +631,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
HttpRange.toResourceRegions(httpRanges, resource), mediaType, outputMessage);
}
catch (IllegalArgumentException ex) {
response.setContentType(null);
response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
}

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

@ -333,6 +333,8 @@ class ResourceHttpRequestHandlerTests { @@ -333,6 +333,8 @@ class ResourceHttpRequestHandlerTests {
this.handler.handleRequest(this.request, this.response);
assertThat(this.response.getStatus()).isEqualTo(416);
// MockHttpServletResponse does not reset content type in 6.2.x
//assertThat(this.response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_TYPE);
assertThat(this.response.getHeader("Content-Range")).isEqualTo("bytes */10");
assertThat(this.response.getHeader("Accept-Ranges")).isEqualTo("bytes");
assertThat(this.response.getHeaders("Accept-Ranges")).hasSize(1);

Loading…
Cancel
Save