From ff76201050e3a9b8672366d878bab4361a8f44f4 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 16 Aug 2012 14:00:14 +1000 Subject: [PATCH] jetty-9 setContentLength smaller than content written is an IllegalArgumentException --- .../java/org/eclipse/jetty/server/Response.java | 7 ++++++- .../jetty/server/HttpManyWaysToCommitTest.java | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index 765f2a077e8..6a954671ec9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -790,11 +790,16 @@ public class Response implements HttpServletResponse // if the getHandling committed the response! if (isCommitted() || _channel.isIncluding()) return; + + long written=_channel.getOutputStream().getWritten(); + if (written>len) + throw new IllegalArgumentException("setContent("+len+") when already written "+written); + _contentLength=len; _fields.putLongField(HttpHeader.CONTENT_LENGTH.toString(), len); if (_contentLength>0) - checkAllContentWritten(_channel.getOutputStream().getWritten()); + checkAllContentWritten(written); } /* ------------------------------------------------------------ */ diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java index b27e0b516ce..e6cd68c0a8d 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpManyWaysToCommitTest.java @@ -34,6 +34,8 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpScheme; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.StdErrLog; import org.junit.After; import org.junit.Before; import org.junit.Ignore; @@ -72,12 +74,14 @@ public class HttpManyWaysToCommitTest server = new Server(); connector = new SelectChannelConnector(server); server.setConnectors(new Connector[]{connector}); + ((StdErrLog)Log.getLogger(HttpChannel.class)).setHideStacks(true); } /* ------------------------------------------------------------ */ @After public void tearDown() throws Exception { + ((StdErrLog)Log.getLogger(HttpChannel.class)).setHideStacks(false); server.stop(); } @@ -519,7 +523,7 @@ public class HttpManyWaysToCommitTest Response response = executeRequest(); - assertThat("response code is 200", response.getCode(), is("200")); + assertThat(response.getCode(), is("500")); } private class WriteAndSetContentLengthTooSmallHandler extends ThrowExceptionOnDemandHandler @@ -552,7 +556,6 @@ public class HttpManyWaysToCommitTest writer.flush(); Response response = readResponse(reader); - System.out.println(response);//TODO: remove return response; } @@ -737,8 +740,15 @@ public class HttpManyWaysToCommitTest public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (throwException) - throw new IllegalStateException("explicit thrown on demand"); + throw new TestCommitException(); } } + private static class TestCommitException extends IllegalStateException + { + public TestCommitException() + { + super("Thrown by test"); + } + } }