diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index 50dc1ddf824..06efee292c2 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -156,7 +156,7 @@ public class BufferUtil /* ------------------------------------------------------------ */ /** Flip the buffer to Flush mode. - * The limit is set to the first unused byte(the old position) amd + * The limit is set to the first unused byte(the old position) and * the position is set to the passed position. *

* This method is used as a replacement of {@link Buffer#flip()}. diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java index fa49a5e7055..46a2b91602d 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/protocol/Parser.java @@ -392,8 +392,8 @@ public class Parser case MASK_BYTES: { byte b = buffer.get(); + frame.getMask()[4 - cursor] = b; --cursor; - frame.getMask()[cursor] = b; if (cursor == 0) { // special case for empty payloads (no more bytes left in buffer) @@ -449,6 +449,7 @@ public class Parser getPolicy().assertValidPayloadLength(payloadLength); frame.assertValid(); payload = ByteBuffer.allocate(payloadLength); + BufferUtil.clearToFill(payload); } BufferUtil.put(buffer,payload); @@ -457,14 +458,18 @@ public class Parser { BufferUtil.flipToFlush(payload,0); + LOG.debug("PreMask: {}",BufferUtil.toDetailString(payload)); // demask (if needed) if (frame.isMasked()) { byte mask[] = frame.getMask(); + int offset; + int start = payload.position(); int end = payload.limit(); - for (int i = payload.position(); i < end; i++) + for (int i = start; i < end; i++) { - payload.put(i,(byte)(payload.get(i) ^ mask[i % 4])); + offset = (i - start); + payload.put(i,(byte)(payload.get(i) ^ mask[offset % 4])); } } diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/protocol/GeneratorTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/protocol/GeneratorTest.java index 3b1b0e60c85..38d68a74aa5 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/protocol/GeneratorTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/protocol/GeneratorTest.java @@ -17,6 +17,14 @@ import org.junit.Test; public class GeneratorTest { + private void parsePartial(Parser parser, ByteBuffer buf, int numBytes) + { + int len = Math.min(numBytes,buf.remaining()); + byte arr[] = new byte[len]; + buf.get(arr,0,len); + parser.parse(ByteBuffer.wrap(arr)); + } + /** * Prevent regression of masking of many packets. */ @@ -25,7 +33,7 @@ public class GeneratorTest { byte[] MASK = { 0x11, 0x22, 0x33, 0x44 }; - int pingCount = 1000; + int pingCount = 10; // the generator Generator generator = new UnitGenerator(); @@ -67,13 +75,11 @@ public class GeneratorTest int segmentSize = 5; while (completeBuf.remaining() > 0) { - ByteBuffer part = completeBuf.slice(); - int len = Math.min(segmentSize,part.remaining()); - part.limit(part.position() + len); - parser.parse(part); - completeBuf.position(completeBuf.position() + len); + parsePartial(parser,completeBuf,segmentSize); } + capture.dump(); + // Assert validity of frame int frameCount = send.size(); capture.assertFrameCount(frameCount); diff --git a/jetty-websocket/websocket-core/src/test/resources/jetty-logging.properties b/jetty-websocket/websocket-core/src/test/resources/jetty-logging.properties index fbf69445511..c100df43afe 100644 --- a/jetty-websocket/websocket-core/src/test/resources/jetty-logging.properties +++ b/jetty-websocket/websocket-core/src/test/resources/jetty-logging.properties @@ -1,2 +1,3 @@ org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog -org.eclipse.jetty.websocket.LEVEL=WARN \ No newline at end of file +org.eclipse.jetty.websocket.LEVEL=WARN +# org.eclipse.jetty.websocket.protocol.Parser.LEVEL=DEBUG \ No newline at end of file