Skip to content

HttpHeaders should reject negative ContentLength values #32660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,14 @@ public Locale getContentLanguage() {
/**
* Set the length of the body in bytes, as specified by the
* {@code Content-Length} header.
* @param contentLength content length (greater than or equal to zero)
* @throws IllegalArgumentException if the content length is negative
* @see <a href="https://www.rfc-editor.org/rfc/rfc2616#section-14.13">RFC 2616, section 14.13</a>
*/
public void setContentLength(long contentLength) {
if (contentLength < 0){
throw new IllegalArgumentException("Content-Length must be a non-negative number");
}
set(CONTENT_LENGTH, Long.toString(contentLength));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ void contentLength() {
assertThat(headers.getFirst("Content-Length")).as("Invalid Content-Length header").isEqualTo("42");
}

@Test
void setContentLengthWithNegativeValue() {
assertThatIllegalArgumentException().isThrownBy(() ->
headers.setContentLength(-1));
}

@Test
void getContentLengthReturnsMinusOneForAbsentHeader() {
headers.remove(HttpHeaders.CONTENT_LENGTH);
assertThat(headers.getContentLength()).isEqualTo(-1);
}

@Test
void contentType() {
MediaType contentType = new MediaType("text", "html", StandardCharsets.UTF_8);
Expand Down