From f28fe2d5017a8f06f28877f7450320a17bb72bb6 Mon Sep 17 00:00:00 2001 From: Daniel Wegener Date: Thu, 15 Aug 2019 22:31:10 +0200 Subject: [PATCH] Add OnCommittedResponseWrapper.setContentLengthLong Add setContentLengthLong tracking to OnCommittedResponseWrapper in order to detect commits on servlets that use setContentLengthLong to announce the entity size they are about to write (as used in the Apache Tomcat's DefaultServlet). Fixes gh-7261 --- .../security/web/util/OnCommittedResponseWrapper.java | 6 ++++++ .../web/util/OnCommittedResponseWrapperTests.java | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/web/src/main/java/org/springframework/security/web/util/OnCommittedResponseWrapper.java b/web/src/main/java/org/springframework/security/web/util/OnCommittedResponseWrapper.java index 2002f78657..405206c85f 100644 --- a/web/src/main/java/org/springframework/security/web/util/OnCommittedResponseWrapper.java +++ b/web/src/main/java/org/springframework/security/web/util/OnCommittedResponseWrapper.java @@ -69,6 +69,12 @@ public abstract class OnCommittedResponseWrapper extends HttpServletResponseWrap super.setContentLength(len); } + @Override + public void setContentLengthLong(long len) { + setContentLength(len); + super.setContentLengthLong(len); + } + private void setContentLength(long len) { this.contentLength = len; checkContentLength(0); diff --git a/web/src/test/java/org/springframework/security/web/util/OnCommittedResponseWrapperTests.java b/web/src/test/java/org/springframework/security/web/util/OnCommittedResponseWrapperTests.java index f679f413ec..3f4f94d149 100644 --- a/web/src/test/java/org/springframework/security/web/util/OnCommittedResponseWrapperTests.java +++ b/web/src/test/java/org/springframework/security/web/util/OnCommittedResponseWrapperTests.java @@ -1101,6 +1101,17 @@ public class OnCommittedResponseWrapperTests { assertThat(committed).isTrue(); } + // gh-7261 + @Test + public void contentLengthLongOutputStreamWriteStringCommits() throws IOException { + String body = "something"; + response.setContentLengthLong(body.length()); + + response.getOutputStream().print(body); + + assertThat(committed).isTrue(); + } + @Test public void addHeaderContentLengthPrintWriterWriteStringCommits() throws Exception { int expected = 1234;