jetty-9 setContentLength smaller than content written is an IllegalArgumentException

This commit is contained in:
Greg Wilkins 2012-08-16 14:00:14 +10:00
parent 4907498886
commit ff76201050
2 changed files with 19 additions and 4 deletions

View File

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

View File

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