Author: kkolinko Date: Thu Nov 10 22:33:16 2011 New Revision: 1200603 URL: http://svn.apache.org/viewvc?rev=1200603&view=rev Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=52121 Fix possible output corruption when compression is enabled for a connector and the response is flushed.
Modified: tomcat/tc6.0.x/trunk/STATUS.txt tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.java tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Modified: tomcat/tc6.0.x/trunk/STATUS.txt URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/STATUS.txt?rev=1200603&r1=1200602&r2=1200603&view=diff ============================================================================== --- tomcat/tc6.0.x/trunk/STATUS.txt (original) +++ tomcat/tc6.0.x/trunk/STATUS.txt Thu Nov 10 22:33:16 2011 @@ -64,13 +64,6 @@ PATCHES PROPOSED TO BACKPORT: - getStuckThreadIds() returns a list of ids. It might be useful to have a similar method that returns Thread.getName() names. -* Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=52121 - Fix possible output corruption when compression is - enabled for a connector and the response is flushed. - https://issues.apache.org/bugzilla/attachment.cgi?id=27905 - +1: kkolinko, markt, rjung - -1: - * Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=50570 Apply FIPS mode patch from TC7: http://svn.apache.org/viewvc?rev=1199985&view=rev Modified: tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.java URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.java?rev=1200603&r1=1200602&r2=1200603&view=diff ============================================================================== --- tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.java (original) +++ tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/filters/FlushableGZIPOutputStream.java Thu Nov 10 22:33:16 2011 @@ -35,58 +35,74 @@ public class FlushableGZIPOutputStream e super(os); } - private static final byte[] EMPTYBYTEARRAY = new byte[0]; - private boolean hasData = false; - /** - * Here we make sure we have received data, so that the header has been for - * sure written to the output stream already. + * It is used to reserve one byte of real data so that it can be used when + * flushing the stream. */ + private byte[] lastByte = new byte[1]; + private boolean hasLastByte = false; + @Override - public synchronized void write(byte[] bytes, int i, int i1) - throws IOException { - super.write(bytes, i, i1); - hasData = true; + public void write(byte[] bytes) throws IOException { + write(bytes, 0, bytes.length); } @Override - public synchronized void write(int i) throws IOException { - super.write(i); - hasData = true; + public synchronized void write(byte[] bytes, int offset, int length) + throws IOException { + if (length > 0) { + flushLastByte(); + if (length > 1) { + super.write(bytes, offset, length - 1); + } + rememberLastByte(bytes[offset + length - 1]); + } } @Override - public synchronized void write(byte[] bytes) throws IOException { - super.write(bytes); - hasData = true; + public synchronized void write(int i) throws IOException { + flushLastByte(); + rememberLastByte((byte) i); } @Override - public synchronized void flush() throws IOException { - if (!hasData) { - return; // do not allow the gzip header to be flushed on its own - } - - // trick the deflater to flush - /** - * Now this is tricky: We force the Deflater to flush its data by - * switching compression level. As yet, a perplexingly simple workaround - * for - * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html - */ - if (!def.finished()) { - def.setInput(EMPTYBYTEARRAY, 0, 0); - - def.setLevel(Deflater.NO_COMPRESSION); - deflate(); + public synchronized void close() throws IOException { + flushLastByte(); + super.close(); + } - def.setLevel(Deflater.DEFAULT_COMPRESSION); - deflate(); + private void rememberLastByte(byte b) { + lastByte[0] = b; + hasLastByte = true; + } - out.flush(); + private void flushLastByte() throws IOException { + if (hasLastByte) { + super.write(lastByte, 0, 1); + hasLastByte = false; } + } - hasData = false; // no more data to flush + @Override + public synchronized void flush() throws IOException { + if (hasLastByte) { + // - do not allow the gzip header to be flushed on its own + // - do not do anything if there is no data to send + + // trick the deflater to flush + /** + * Now this is tricky: We force the Deflater to flush its data by + * switching compression level. As yet, a perplexingly simple workaround + * for + * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html + */ + if (!def.finished()) { + def.setLevel(Deflater.NO_COMPRESSION); + flushLastByte(); + def.setLevel(Deflater.DEFAULT_COMPRESSION); + } + } + out.flush(); } /* Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?rev=1200603&r1=1200602&r2=1200603&view=diff ============================================================================== --- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Thu Nov 10 22:33:16 2011 @@ -160,6 +160,11 @@ the socket from 30s to 10s. (kkolinko) </fix> <fix> + <bug>52121</bug>: Fix possible output corruption when compression is + enabled for a connector and the response is flushed. Test + case provided by David Marcks. (kkolinko) + </fix> + <fix> Replace unneeded call that iterated events queue in NioEndpoint.Poller. (kkolinko) </fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org