Skip to content

Commit

Permalink
Fix null value support in ContentCachingResponseWrapper
Browse files Browse the repository at this point in the history
Prior to this commit, calling `setHeader` on the response wrapper would
have a separate code path for the "Content-Length" header. This did not
support calls with `null` values and would result in an exception.

This commit ensures that the cached content length value is reset in
this case and that the call is forwarded properly to the superclass.

Fixes gh-34460
  • Loading branch information
bclozel committed Feb 21, 2025
1 parent 6dd73ae commit 5a0bd9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ public boolean containsHeader(String name) {
@Override
public void setHeader(String name, String value) {
if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
this.contentLength = toContentLengthInt(Long.parseLong(value));
if (value != null) {
this.contentLength = toContentLengthInt(Long.parseLong(value));
}
else {
this.contentLength = null;
super.setHeader(name, null);
}
}
else {
super.setHeader(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ void setContentLengthAbove2GbViaSetHeader() {
.withMessageContaining(overflow);
}

@Test
void setContentLengthNull() {
MockHttpServletResponse response = new MockHttpServletResponse();
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
responseWrapper.setContentLength(1024);
responseWrapper.setHeader(CONTENT_LENGTH, null);

assertThat(response.getHeaderNames()).doesNotContain(CONTENT_LENGTH);
assertThat(responseWrapper.getHeader(CONTENT_LENGTH)).isNull();
}


private void assertHeader(HttpServletResponse response, String header, int value) {
assertHeader(response, header, Integer.toString(value));
Expand Down

0 comments on commit 5a0bd9e

Please sign in to comment.