diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java index eef1e640865..bfe11dc2bcb 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java @@ -68,21 +68,19 @@ public abstract class FrameGenerator // payload lengths int payloadLength = frame.getPayloadLength(); - if (payloadLength >= 0x7F) - { - // we have a 64 bit length - b |= 0x7F; - framing.put(b); - - framing.putInt(payloadLength); - } - else if (payloadLength >= 0x7E) + if ((payloadLength >= 0x7F) && (payloadLength <= 0xFF_FF)) { // we have a 16 bit length b |= 0x7E; - framing.put(b); - - framing.putShort((short)(payloadLength & 0xFFFF)); + framing.put(b); // indicate 2 byte length + framing.putShort((short)(payloadLength & 0xFF_FF)); // write 2 byte length + } + else if (payloadLength >= 0xFFFF) + { + // we have a 64 bit length + b |= 0x7F; + framing.put(b); // indicate 4 byte length + framing.putInt(payloadLength); // write 4 byte length } else { diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java index e735f86ca4b..5f9485bc556 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java @@ -4,7 +4,6 @@ package org.eclipse.jetty.websocket.generator; import java.nio.ByteBuffer; import org.eclipse.jetty.io.StandardByteBufferPool; -import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.websocket.ByteBufferAssert; import org.eclipse.jetty.websocket.api.WebSocketBehavior; @@ -19,63 +18,11 @@ public class RFC6455ExamplesGeneratorTest { StandardByteBufferPool bufferPool = new StandardByteBufferPool(); - - @Test - public void testSingleUnmaskedTextMessage() - { - - TextFrame text = new TextFrame("Hello"); - - text.setFin(true); - - WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); - - TextFrameGenerator generator = new TextFrameGenerator(bufferPool,policy); - - ByteBuffer actual = generator.generate(text); - - ByteBuffer expected = ByteBuffer.allocate(10); - - expected.put(new byte[] - { (byte)0x81, (byte)0x05, (byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c, (byte)0x6f }); - - expected.flip(); - actual.flip(); - - ByteBufferAssert.assertEquals("t1 buffers are not equal", expected, actual); - } - - @Test - public void testSingleMaskedTextMessage() - { - TextFrame text = new TextFrame(); - text.setPayload("Hello"); - text.setFin(true); - text.setMask(new byte[] - { 0x37, (byte)0xfa, 0x21, 0x3d }); - - WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); - - TextFrameGenerator gen = new TextFrameGenerator(bufferPool,policy); - - ByteBuffer actual = gen.generate(text); - actual.flip(); // make readable - - ByteBuffer expected = ByteBuffer.allocate(11); - // Raw bytes as found in RFC 6455, Section 5.7 - Examples - // A single-frame masked text message - expected.put(new byte[] - { (byte)0x81, (byte)0x85, 0x37, (byte)0xfa, 0x21, 0x3d, 0x7f, (byte)0x9f, 0x4d, 0x51, 0x58 }); - expected.flip(); // make readable - - ByteBufferAssert.assertEquals("masked text buffers are not equal",expected,actual); - } - @Test public void testFragmentedUnmaskedTextMessage() { - + TextFrame text1 = new TextFrame(); TextFrame text2 = new TextFrame(); @@ -110,32 +57,6 @@ public class RFC6455ExamplesGeneratorTest ByteBufferAssert.assertEquals("t1 buffers are not equal", expected1, actual1); ByteBufferAssert.assertEquals("t2 buffers are not equal", expected2, actual2); } - - - - @Test - public void testSingleUnmaskedPingRequest() throws Exception - { - PingFrame ping = new PingFrame(); - - byte msg[] = "Hello".getBytes(StringUtil.__UTF8_CHARSET); - ByteBuffer payload = ByteBuffer.allocate(msg.length); - payload.put(msg); - ping.setPayload(payload); - - WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); - - PingFrameGenerator gen = new PingFrameGenerator(bufferPool,policy); - ByteBuffer actual = gen.generate(ping); - actual.flip(); // make readable - - ByteBuffer expected = ByteBuffer.allocate(10); - expected.put(new byte[] - { (byte)0x89, (byte)0x05, (byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c, (byte)0x6f }); - expected.flip(); // make readable - - ByteBufferAssert.assertEquals("Ping buffers",expected,actual); - } @Test public void testSingleMaskedPongRequest() @@ -165,6 +86,34 @@ public class RFC6455ExamplesGeneratorTest ByteBufferAssert.assertEquals("pong buffers are not equal",expected,actual); } + @Test + public void testSingleMaskedTextMessage() + { + TextFrame text = new TextFrame(); + text.setPayload("Hello"); + text.setFin(true); + text.setMask(new byte[] + { 0x37, (byte)0xfa, 0x21, 0x3d }); + + WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); + + TextFrameGenerator gen = new TextFrameGenerator(bufferPool,policy); + + ByteBuffer actual = gen.generate(text); + actual.flip(); // make readable + + ByteBuffer expected = ByteBuffer.allocate(11); + // Raw bytes as found in RFC 6455, Section 5.7 - Examples + // A single-frame masked text message + expected.put(new byte[] + { (byte)0x81, (byte)0x85, 0x37, (byte)0xfa, 0x21, 0x3d, 0x7f, (byte)0x9f, 0x4d, 0x51, 0x58 }); + expected.flip(); // make readable + + ByteBufferAssert.assertEquals("masked text buffers are not equal",expected,actual); + } + + + @Test public void testSingleUnmasked256ByteBinaryMessage() { @@ -173,50 +122,92 @@ public class RFC6455ExamplesGeneratorTest BinaryFrame binary = new BinaryFrame(); binary.setFin(true); ByteBuffer payload = ByteBuffer.allocate(dataSize); - for (int i = 0; i < dataSize; i++) { payload.put((byte)0x44); } binary.setPayload(payload); - + WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); BinaryFrameGenerator gen = new BinaryFrameGenerator(bufferPool,policy); - + ByteBuffer actual = gen.generate(binary); - + ByteBuffer expected = ByteBuffer.allocate(dataSize + 10); // Raw bytes as found in RFC 6455, Section 5.7 - Examples // 256 bytes binary message in a single unmasked frame expected.put(new byte[] - { (byte)0x82, (byte)0x7E, (byte)0x01_00 }); - + { (byte)0x82, (byte)0x7E }); + expected.putShort((short)0x01_00); + for (int i = 0; i < dataSize; i++) { expected.put((byte)0x44); } - + actual.flip(); expected.flip(); - - System.out.println(binary); - - System.out.println(BufferUtil.toDetailString(expected)); - System.out.println(BufferUtil.toDetailString(actual)); - - for ( int i = 0 ; i < 6; ++i ) - { - System.out.println("a " + Integer.toHexString(actual.get())); - System.out.println("e " + Integer.toHexString(expected.get())); - } - - - //ByteBufferAssert.assertEquals("binary buffers are not equal", expected, actual); + // System.out.println(binary); + // System.out.println(BufferUtil.toDetailString(expected)); + // System.out.println(BufferUtil.toDetailString(actual)); + + // for (int i = 0; i < 20; ++i) + // { + // System.out.printf("a [%2d] 0x%02x%n",i,actual.get()); + // System.out.printf("e [%2d] 0x%02x%n",i,expected.get()); + // } + + ByteBufferAssert.assertEquals("binary buffers are not equal", expected, actual); } - + @Test + public void testSingleUnmaskedPingRequest() throws Exception + { + PingFrame ping = new PingFrame(); + + byte msg[] = "Hello".getBytes(StringUtil.__UTF8_CHARSET); + ByteBuffer payload = ByteBuffer.allocate(msg.length); + payload.put(msg); + ping.setPayload(payload); + + WebSocketPolicy policy = WebSocketPolicy.newServerPolicy(); + + PingFrameGenerator gen = new PingFrameGenerator(bufferPool,policy); + ByteBuffer actual = gen.generate(ping); + actual.flip(); // make readable + + ByteBuffer expected = ByteBuffer.allocate(10); + expected.put(new byte[] + { (byte)0x89, (byte)0x05, (byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c, (byte)0x6f }); + expected.flip(); // make readable + + ByteBufferAssert.assertEquals("Ping buffers",expected,actual); + } + + @Test + public void testSingleUnmaskedTextMessage() + { + TextFrame text = new TextFrame("Hello"); + text.setFin(true); + + WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER); + + TextFrameGenerator generator = new TextFrameGenerator(bufferPool,policy); + + ByteBuffer actual = generator.generate(text); + + ByteBuffer expected = ByteBuffer.allocate(10); + + expected.put(new byte[] + { (byte)0x81, (byte)0x05, (byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c, (byte)0x6f }); + + expected.flip(); + actual.flip(); + + ByteBufferAssert.assertEquals("t1 buffers are not equal", expected, actual); + } }