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
This commit is contained in:
Brian Clozel
2025-06-15 18:42:45 +02:00
parent 9f1aef16f9
commit faada70d59
2 changed files with 3 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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);