Skip to content

Commit faada70

Browse files
committed
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
1 parent 9f1aef1 commit faada70

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ public void handleRequest(HttpServletRequest request, HttpServletResponse respon
631631
HttpRange.toResourceRegions(httpRanges, resource), mediaType, outputMessage);
632632
}
633633
catch (IllegalArgumentException ex) {
634+
response.setContentType(null);
634635
response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
635636
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
636637
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ void partialContentInvalidRangeHeader() throws Exception {
333333
this.handler.handleRequest(this.request, this.response);
334334

335335
assertThat(this.response.getStatus()).isEqualTo(416);
336+
// MockHttpServletResponse does not reset content type in 6.2.x
337+
//assertThat(this.response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_TYPE);
336338
assertThat(this.response.getHeader("Content-Range")).isEqualTo("bytes */10");
337339
assertThat(this.response.getHeader("Accept-Ranges")).isEqualTo("bytes");
338340
assertThat(this.response.getHeaders("Accept-Ranges")).hasSize(1);

0 commit comments

Comments
 (0)