Author: markt
Date: Mon Mar  2 15:02:30 2015
New Revision: 1663324

URL: http://svn.apache.org/r1663324
Log:
When an uncompressed part results in multiple compressed parts, ensure that the 
OpCodes are correctly set.

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/PerMessageDeflate.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/PerMessageDeflate.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PerMessageDeflate.java?rev=1663324&r1=1663323&r2=1663324&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PerMessageDeflate.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PerMessageDeflate.java Mon 
Mar  2 15:02:30 2015
@@ -316,16 +316,13 @@ public class PerMessageDeflate implement
         List<MessagePart> allCompressedParts = new ArrayList<>();
 
         for (MessagePart uncompressedPart : uncompressedParts) {
-            byte opCode = uncompressedPart.getOpCode();
-            if (Util.isControl(opCode)) {
+            if (Util.isControl(uncompressedPart.getOpCode())) {
                 // Control messages can appear in the middle of other messages
                 // and must not be compressed. Pass it straight through
                 allCompressedParts.add(uncompressedPart);
             } else {
                 List<MessagePart> compressedParts = new ArrayList<>();
                 ByteBuffer uncompressedPayload = uncompressedPart.getPayload();
-                SendHandler uncompressedIntermediateHandler =
-                        uncompressedPart.getIntermediateHandler();
 
                 deflater.setInput(uncompressedPayload.array(),
                         uncompressedPayload.arrayOffset() + 
uncompressedPayload.position(),
@@ -342,7 +339,8 @@ public class PerMessageDeflate implement
                             compressedPayload.remaining(), flush);
                     compressedPayload.position(compressedPayload.position() + 
written);
 
-                    if (!uncompressedPart.isFin() && 
compressedPayload.hasRemaining() && deflater.needsInput()) {
+                    if (!uncompressedPart.isFin() && 
compressedPayload.hasRemaining() &&
+                            deflater.needsInput()) {
                         // This message part has been fully processed by the
                         // deflater. Fire the send handler for this message 
part
                         // and move on to the next message part.
@@ -362,28 +360,24 @@ public class PerMessageDeflate implement
                     boolean fin = uncompressedPart.isFin();
                     boolean full = compressedPayload.limit() == 
compressedPayload.capacity();
                     boolean needsInput = deflater.needsInput();
-                    long blockingWriteTimeoutExpiry = 
uncompressedPart.getBlockingWriteTimeoutExpiry();
 
                     if (fin && !full && needsInput) {
                         // End of compressed message. Drop EOM bytes and 
output.
                         compressedPayload.limit(compressedPayload.limit() - 
EOM_BYTES.length);
-                        compressedPart = new MessagePart(true, 
getRsv(uncompressedPart),
-                                opCode, compressedPayload, 
uncompressedIntermediateHandler,
-                                uncompressedIntermediateHandler, 
blockingWriteTimeoutExpiry);
+                        compressedPart = createNewCompressedMessagePart(
+                                uncompressedPart, true, compressedPayload);
                         deflateRequired = false;
                         startNewMessage();
                     } else if (full && !needsInput) {
                         // Write buffer full and input message not fully read.
                         // Output and start new compressed part.
-                        compressedPart = new MessagePart(false, 
getRsv(uncompressedPart),
-                                opCode, compressedPayload, 
uncompressedIntermediateHandler,
-                                uncompressedIntermediateHandler, 
blockingWriteTimeoutExpiry);
+                        compressedPart = createNewCompressedMessagePart(
+                                uncompressedPart, false, compressedPayload);
                     } else if (!fin && full && needsInput) {
                         // Write buffer full and input message not fully read.
                         // Output and get more data.
-                        compressedPart = new MessagePart(false, 
getRsv(uncompressedPart),
-                                opCode, compressedPayload, 
uncompressedIntermediateHandler,
-                                uncompressedIntermediateHandler, 
blockingWriteTimeoutExpiry);
+                        compressedPart = createNewCompressedMessagePart(
+                                uncompressedPart, false, compressedPayload);
                         deflateRequired = false;
                     } else if (fin && full && needsInput) {
                         // Write buffer full. Input fully read. Deflater may be
@@ -393,24 +387,22 @@ public class PerMessageDeflate implement
                         // - in middle of EOM bytes
                         // - about to write EOM bytes
                         // - more data to write
-                        int eomBufferWritten = deflater.deflate(EOM_BUFFER, 0, 
EOM_BUFFER.length, Deflater.SYNC_FLUSH);
+                        int eomBufferWritten = deflater.deflate(
+                                EOM_BUFFER, 0, EOM_BUFFER.length, 
Deflater.SYNC_FLUSH);
                         if (eomBufferWritten < EOM_BUFFER.length) {
                             // EOM has just been completed
-                            compressedPayload.limit(compressedPayload.limit() 
- EOM_BYTES.length + eomBufferWritten);
-                            compressedPart = new MessagePart(true,
-                                    getRsv(uncompressedPart), opCode, 
compressedPayload,
-                                    uncompressedIntermediateHandler, 
uncompressedIntermediateHandler,
-                                    blockingWriteTimeoutExpiry);
+                            compressedPayload.limit(compressedPayload.limit() -
+                                    EOM_BYTES.length + eomBufferWritten);
+                            compressedPart = createNewCompressedMessagePart(
+                                    uncompressedPart, true, compressedPayload);
                             deflateRequired = false;
                             startNewMessage();
                         } else {
                             // More data to write
                             // Copy bytes to new write buffer
                             writeBuffer.put(EOM_BUFFER, 0, eomBufferWritten);
-                            compressedPart = new MessagePart(false,
-                                    getRsv(uncompressedPart), opCode, 
compressedPayload,
-                                    uncompressedIntermediateHandler, 
uncompressedIntermediateHandler,
-                                    blockingWriteTimeoutExpiry);
+                            compressedPart = createNewCompressedMessagePart(
+                                    uncompressedPart, false, 
compressedPayload);
                         }
                     } else {
                         throw new IllegalStateException("Should never happen");
@@ -447,12 +439,20 @@ public class PerMessageDeflate implement
     }
 
 
-    private int getRsv(MessagePart uncompressedMessagePart) {
-        int result = uncompressedMessagePart.getRsv();
+    private MessagePart createNewCompressedMessagePart(MessagePart 
uncompressedMessagePart,
+            boolean fin, ByteBuffer compressedPayload) {
+        int rsv = uncompressedMessagePart.getRsv();
+        byte opCode = uncompressedMessagePart.getOpCode();
         if (!firstCompressedFrameWritten) {
-            result += RSV_BITMASK;
+            rsv += RSV_BITMASK;
             firstCompressedFrameWritten = true;
+        } else {
+            // This must be a continuation frame
+            opCode = 0;
         }
-        return result;
+        return new MessagePart(fin, rsv, opCode, compressedPayload,
+                uncompressedMessagePart.getIntermediateHandler(),
+                uncompressedMessagePart.getIntermediateHandler(),
+                uncompressedMessagePart.getBlockingWriteTimeoutExpiry());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to