WebSocket - Calling setPayload() outside of TextFrame and BinaryFrame

This commit is contained in:
Joakim Erdfelt 2013-08-20 16:45:26 -07:00
parent a089780701
commit e56fddfc4a
16 changed files with 68 additions and 74 deletions

View File

@ -89,7 +89,7 @@ public class OnPartialTest
public void testOnTextPartial() throws Throwable
{
List<WebSocketFrame> frames = new ArrayList<>();
frames.add(new TextFrame("Saved").setFin(false));
frames.add(new TextFrame().setPayload("Saved").setFin(false));
frames.add(new ContinuationFrame(" by ").setFin(false));
frames.add(new ContinuationFrame("zero").setFin(true));

View File

@ -122,7 +122,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
{
LOG.debug("sendBytes with {}",BufferUtil.toDetailString(data));
}
blockingWrite(new BinaryFrame(data));
blockingWrite(new BinaryFrame().setPayload(data));
}
finally
{
@ -144,7 +144,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
{
LOG.debug("sendBytesByFuture with {}",BufferUtil.toDetailString(data));
}
return sendAsyncFrame(new BinaryFrame(data));
return sendAsyncFrame(new BinaryFrame().setPayload(data));
}
@Override
@ -156,7 +156,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
{
LOG.debug("sendBytes({}, {})",BufferUtil.toDetailString(data),callback);
}
sendFrame(new BinaryFrame(data),callback);
sendFrame(new BinaryFrame().setPayload(data),callback);
}
public void sendFrame(WebSocketFrame frame, WriteCallback callback)
@ -196,7 +196,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
}
else
{
frame = new BinaryFrame(fragment);
frame = new BinaryFrame().setPayload(fragment);
}
frame.setFin(isLast);
blockingWrite(frame);
@ -241,7 +241,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
}
else
{
frame = new TextFrame(fragment);
frame = new TextFrame().setPayload(fragment);
}
frame.setFin(isLast);
blockingWrite(frame);
@ -322,7 +322,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
try
{
msgType.set(TEXT);
WebSocketFrame frame = new TextFrame(text);
WebSocketFrame frame = new TextFrame().setPayload(text);
if (LOG.isDebugEnabled())
{
LOG.debug("sendString with {}",BufferUtil.toDetailString(frame.getPayload()));
@ -345,7 +345,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
public Future<Void> sendStringByFuture(String text)
{
msgType.set(TEXT);
TextFrame frame = new TextFrame(text);
TextFrame frame = new TextFrame().setPayload(text);
if (LOG.isDebugEnabled())
{
LOG.debug("sendStringByFuture with {}",BufferUtil.toDetailString(frame.getPayload()));
@ -358,7 +358,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
{
Objects.requireNonNull(callback,"WriteCallback cannot be null");
msgType.set(TEXT);
TextFrame frame = new TextFrame(text);
TextFrame frame = new TextFrame().setPayload(text);
if (LOG.isDebugEnabled())
{
LOG.debug("sendString({},{})",BufferUtil.toDetailString(frame.getPayload()),callback);

View File

@ -30,21 +30,22 @@ public class BinaryFrame extends DataFrame
super(OpCode.BINARY);
}
public BinaryFrame(byte[] buf)
public BinaryFrame setPayload(ByteBuffer buf)
{
super.setPayload(buf);
return this;
}
public BinaryFrame setPayload(byte[] buf)
{
this();
setPayload(ByteBuffer.wrap(buf));
return this;
}
public BinaryFrame(ByteBuffer buf)
public BinaryFrame setPayload(String payload)
{
this();
setPayload(buf);
}
public BinaryFrame(String payload)
{
this(StringUtil.getBytes(payload));
setPayload(StringUtil.getUtf8Bytes(payload));
return this;
}
@Override

View File

@ -23,7 +23,6 @@ import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.common.OpCode;
import org.eclipse.jetty.websocket.common.WebSocketFrame;
public class TextFrame extends DataFrame
{
@ -32,19 +31,13 @@ public class TextFrame extends DataFrame
super(OpCode.TEXT);
}
public TextFrame(String msg)
{
this();
setPayload(msg);
}
@Override
public Type getType()
{
return Type.TEXT;
}
public WebSocketFrame setPayload(String str)
public TextFrame setPayload(String str)
{
setPayload(ByteBuffer.wrap(StringUtil.getUtf8Bytes(str)));
return this;

View File

@ -251,7 +251,7 @@ public class GeneratorTest
byte payload[] = new byte[10240];
Arrays.fill(payload,(byte)0x44);
WebSocketFrame frame = new BinaryFrame(payload);
WebSocketFrame frame = new BinaryFrame().setPayload(payload);
// Generate
int windowSize = 1024;
@ -279,7 +279,7 @@ public class GeneratorTest
byte mask[] = new byte[]
{ 0x2A, (byte)0xF0, 0x0F, 0x00 };
WebSocketFrame frame = new BinaryFrame(payload);
WebSocketFrame frame = new BinaryFrame().setPayload(payload);
frame.setMask(mask); // masking!
// Generate

View File

@ -46,10 +46,10 @@ public class ParserTest
public void testParseCase5_15()
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("fragment1").setFin(false));
send.add(new TextFrame().setPayload("fragment1").setFin(false));
send.add(new ContinuationFrame("fragment2").setFin(true));
send.add(new ContinuationFrame("fragment3").setFin(false)); // bad frame
send.add(new TextFrame("fragment4").setFin(true));
send.add(new TextFrame().setPayload("fragment4").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
ByteBuffer completeBuf = UnitGenerator.generate(send);
@ -71,8 +71,8 @@ public class ParserTest
public void testParseCase5_18()
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("fragment1").setFin(false));
send.add(new TextFrame("fragment2").setFin(true)); // bad frame, must be continuation
send.add(new TextFrame().setPayload("fragment1").setFin(false));
send.add(new TextFrame().setPayload("fragment2").setFin(true)); // bad frame, must be continuation
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
ByteBuffer completeBuf = UnitGenerator.generate(send);
@ -92,7 +92,7 @@ public class ParserTest
public void testParseCase5_19()
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("f1").setFin(false));
send.add(new TextFrame().setPayload("f1").setFin(false));
send.add(new ContinuationFrame(",f2").setFin(false));
send.add(new PingFrame().setPayload("pong-1"));
send.add(new ContinuationFrame(",f3").setFin(false));
@ -122,7 +122,7 @@ public class ParserTest
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PongFrame().setPayload("ping"));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
ByteBuffer completeBuf = UnitGenerator.generate(send);

View File

@ -35,7 +35,7 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testFragmentedUnmaskedTextMessage()
{
WebSocketFrame text1 = new TextFrame("Hel").setFin(false);
WebSocketFrame text1 = new TextFrame().setPayload("Hel").setFin(false);
WebSocketFrame text2 = new ContinuationFrame("lo");
ByteBuffer actual1 = UnitGenerator.generate(text1);

View File

@ -290,7 +290,7 @@ public class TestABCase1_2
@Test
public void testGenerateEmptyBinaryCase1_2_1()
{
WebSocketFrame binaryFrame = new BinaryFrame(new byte[] {});
WebSocketFrame binaryFrame = new BinaryFrame().setPayload(new byte[] {});
ByteBuffer actual = UnitGenerator.generate(binaryFrame);

View File

@ -48,7 +48,7 @@ public class EventDriverTest
private Frame makeBinaryFrame(String content, boolean fin)
{
return new BinaryFrame(content).setFin(fin);
return new BinaryFrame().setPayload(content).setFin(fin);
}
@Test
@ -116,8 +116,8 @@ public class EventDriverTest
{
conn.open();
driver.incomingFrame(new PingFrame().setPayload("PING"));
driver.incomingFrame(new TextFrame("Text Me"));
driver.incomingFrame(new BinaryFrame("Hello Bin"));
driver.incomingFrame(new TextFrame().setPayload("Text Me"));
driver.incomingFrame(new BinaryFrame().setPayload("Hello Bin"));
driver.incomingFrame(new CloseInfo(StatusCode.SHUTDOWN,"testcase").asFrame());
socket.capture.assertEventCount(6);

View File

@ -164,14 +164,14 @@ public class FragmentExtensionTest
// Expected Frames
List<WebSocketFrame> expectedFrames = new ArrayList<>();
expectedFrames.add(new TextFrame("No amount of experim").setFin(false));
expectedFrames.add(new TextFrame().setPayload("No amount of experim").setFin(false));
expectedFrames.add(new ContinuationFrame("entation can ever pr").setFin(false));
expectedFrames.add(new ContinuationFrame("ove me right;").setFin(true));
expectedFrames.add(new TextFrame("a single experiment ").setFin(false));
expectedFrames.add(new TextFrame().setPayload("a single experiment ").setFin(false));
expectedFrames.add(new ContinuationFrame("can prove me wrong.").setFin(true));
expectedFrames.add(new TextFrame("-- Albert Einstein").setFin(true));
expectedFrames.add(new TextFrame().setPayload("-- Albert Einstein").setFin(true));
capture.dump();
@ -236,9 +236,9 @@ public class FragmentExtensionTest
// Expected Frames
List<WebSocketFrame> expectedFrames = new ArrayList<>();
expectedFrames.add(new TextFrame("No amount of experimentation can ever prove me right;"));
expectedFrames.add(new TextFrame("a single experiment can prove me wrong."));
expectedFrames.add(new TextFrame("-- Albert Einstein"));
expectedFrames.add(new TextFrame().setPayload("No amount of experimentation can ever prove me right;"));
expectedFrames.add(new TextFrame().setPayload("a single experiment can prove me wrong."));
expectedFrames.add(new TextFrame().setPayload("-- Albert Einstein"));
// capture.dump();

View File

@ -222,7 +222,7 @@ public class MessageCompressionExtensionTest
// leave frames as-is, no compression, and pass into extension
for (String q : quote)
{
TextFrame frame = new TextFrame(q);
TextFrame frame = new TextFrame().setPayload(q);
frame.setRsv1(false); // indication to extension that frame is not compressed (ie: a normal frame)
ext.incomingFrame(frame);
}

View File

@ -123,7 +123,7 @@ public class WriteBytesProviderTest
int binPayloadSize = 50;
byte bin[] = new byte[binPayloadSize];
Arrays.fill(bin,(byte)0x00);
BinaryFrame binFrame = new BinaryFrame(bin);
BinaryFrame binFrame = new BinaryFrame().setPayload(bin);
byte maskingKey[] = Hex.asByteArray("11223344");
binFrame.setMask(maskingKey);
bytesProvider.enqueue(binFrame,binCallback);

View File

@ -103,7 +103,7 @@ public class WebSocketServletRFCTest
WebSocketFrame bin;
bin = new BinaryFrame(buf1).setFin(false);
bin = new BinaryFrame().setPayload(buf1).setFin(false);
client.write(bin); // write buf1 (fin=false)

View File

@ -160,7 +160,7 @@ public class TestABCase3 extends AbstractABCase
Arrays.fill(payload,(byte)0xFF);
List<WebSocketFrame> send = new ArrayList<>();
send.add(new BinaryFrame(payload).setRsv3(true).setRsv1(true)); // intentionally bad
send.add(new BinaryFrame().setPayload(payload).setRsv3(true).setRsv1(true)); // intentionally bad
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());

View File

@ -78,7 +78,7 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("sorry").setFin(true));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -106,7 +106,7 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("sorry").setFin(true));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -135,7 +135,7 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("sorry").setFin(false));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -163,7 +163,7 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("sorry").setFin(false));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -191,7 +191,7 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("sorry").setFin(false));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -219,10 +219,10 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_15() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("fragment1").setFin(false));
send.add(new TextFrame().setPayload("fragment1").setFin(false));
send.add(new ContinuationFrame("fragment2").setFin(true));
send.add(new ContinuationFrame("fragment3").setFin(false)); // bad frame
send.add(new TextFrame("fragment4").setFin(true));
send.add(new TextFrame().setPayload("fragment4").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -251,10 +251,10 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("fragment1").setFin(false)); // bad frame
send.add(new TextFrame("fragment2").setFin(false));
send.add(new TextFrame().setPayload("fragment2").setFin(false));
send.add(new ContinuationFrame("fragment3").setFin(true));
send.add(new ContinuationFrame("fragment4").setFin(false)); // bad frame
send.add(new TextFrame("fragment5").setFin(false));
send.add(new TextFrame().setPayload("fragment5").setFin(false));
send.add(new ContinuationFrame("fragment6").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -283,10 +283,10 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("fragment1").setFin(true)); // nothing to continue
send.add(new TextFrame("fragment2").setFin(false));
send.add(new TextFrame().setPayload("fragment2").setFin(false));
send.add(new ContinuationFrame("fragment3").setFin(true));
send.add(new ContinuationFrame("fragment4").setFin(true)); // nothing to continue
send.add(new TextFrame("fragment5").setFin(false));
send.add(new TextFrame().setPayload("fragment5").setFin(false));
send.add(new ContinuationFrame("fragment6").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -314,8 +314,8 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_18() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("fragment1").setFin(false));
send.add(new TextFrame("fragment2").setFin(true)); // bad frame, must be continuation
send.add(new TextFrame().setPayload("fragment1").setFin(false));
send.add(new TextFrame().setPayload("fragment2").setFin(true)); // bad frame, must be continuation
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -344,7 +344,7 @@ public class TestABCase5 extends AbstractABCase
{
// phase 1
List<WebSocketFrame> send1 = new ArrayList<>();
send1.add(new TextFrame("f1").setFin(false));
send1.add(new TextFrame().setPayload("f1").setFin(false));
send1.add(new ContinuationFrame(",f2").setFin(false));
send1.add(new PingFrame().setPayload("pong-1"));
@ -422,7 +422,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_20() throws Exception
{
List<WebSocketFrame> send1 = new ArrayList<>();
send1.add(new TextFrame("f1").setFin(false));
send1.add(new TextFrame().setPayload("f1").setFin(false));
send1.add(new ContinuationFrame(",f2").setFin(false));
send1.add(new PingFrame().setPayload("pong-1"));
@ -468,7 +468,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_20_slow() throws Exception
{
List<WebSocketFrame> send1 = new ArrayList<>();
send1.add(new TextFrame("f1").setFin(false));
send1.add(new TextFrame().setPayload("f1").setFin(false));
send1.add(new ContinuationFrame(",f2").setFin(false));
send1.add(new PingFrame().setPayload("pong-1"));
@ -515,7 +515,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_3() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new TextFrame().setPayload("hello, ").setFin(false));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -544,7 +544,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_4() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new TextFrame().setPayload("hello, ").setFin(false));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -573,7 +573,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_5() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new TextFrame().setPayload("hello, ").setFin(false));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -603,7 +603,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_6() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new TextFrame().setPayload("hello, ").setFin(false));
send.add(new PingFrame().setPayload("ping"));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -634,7 +634,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_7() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new TextFrame().setPayload("hello, ").setFin(false));
send.add(new PingFrame().setPayload("ping"));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -665,7 +665,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_8() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new TextFrame().setPayload("hello, ").setFin(false));
send.add(new PingFrame().setPayload("ping"));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -699,7 +699,7 @@ public class TestABCase5 extends AbstractABCase
List<WebSocketFrame> send = new ArrayList<>();
send.add(new ContinuationFrame("sorry").setFin(true));
send.add(new TextFrame("hello, world"));
send.add(new TextFrame().setPayload("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();

View File

@ -344,7 +344,7 @@ public class TestABCase9 extends AbstractABCase
Arrays.fill(data,(byte)0x21);
List<WebSocketFrame> send = new ArrayList<>();
send.add(new BinaryFrame(data));
send.add(new BinaryFrame().setPayload(data));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();