WebSocket - Calling setPayload() outside of PingFrame and PongFrame

This commit is contained in:
Joakim Erdfelt 2013-08-20 16:39:26 -07:00
parent 975d9870d2
commit a089780701
15 changed files with 68 additions and 80 deletions

View File

@ -274,7 +274,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
{
LOG.debug("sendPing with {}",BufferUtil.toDetailString(applicationData));
}
blockingWrite(new PingFrame(applicationData));
blockingWrite(new PingFrame().setPayload(applicationData));
}
finally
{
@ -300,7 +300,7 @@ public class WebSocketRemoteEndpoint implements RemoteEndpoint
{
LOG.debug("sendPong with {}",BufferUtil.toDetailString(applicationData));
}
blockingWrite(new PongFrame(applicationData));
blockingWrite(new PongFrame().setPayload(applicationData));
}
finally
{

View File

@ -30,21 +30,16 @@ public class PingFrame extends ControlFrame
super(OpCode.PING);
}
public PingFrame(ByteBuffer buf)
public PingFrame setPayload(byte[] bytes)
{
this();
setPayload(buf);
}
public PingFrame(byte[] bytes)
{
this();
setPayload(ByteBuffer.wrap(bytes));
return this;
}
public PingFrame(String payload)
public PingFrame setPayload(String payload)
{
this(StringUtil.getUtf8Bytes(payload));
setPayload(StringUtil.getUtf8Bytes(payload));
return this;
}
@Override

View File

@ -30,21 +30,16 @@ public class PongFrame extends ControlFrame
super(OpCode.PONG);
}
public PongFrame(ByteBuffer buf)
public PongFrame setPayload(byte[] bytes)
{
this();
setPayload(buf);
}
public PongFrame(byte[] bytes)
{
this();
setPayload(ByteBuffer.wrap(bytes));
return this;
}
public PongFrame(String payload)
public PongFrame setPayload(String payload)
{
this(StringUtil.getUtf8Bytes(payload));
setPayload(StringUtil.getUtf8Bytes(payload));
return this;
}
@Override

View File

@ -216,7 +216,7 @@ public class GeneratorTest
WebSocketFrame[] frames = new WebSocketFrame[pingCount + 1];
for (int i = 0; i < pingCount; i++)
{
frames[i] = new PingFrame(String.format("ping-%d",i));
frames[i] = new PingFrame().setPayload(String.format("ping-%d",i));
}
frames[pingCount] = new CloseInfo(StatusCode.NORMAL).asFrame();

View File

@ -94,10 +94,10 @@ public class ParserTest
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("f1").setFin(false));
send.add(new ContinuationFrame(",f2").setFin(false));
send.add(new PingFrame("pong-1"));
send.add(new PingFrame().setPayload("pong-1"));
send.add(new ContinuationFrame(",f3").setFin(false));
send.add(new ContinuationFrame(",f4").setFin(false));
send.add(new PingFrame("pong-2"));
send.add(new PingFrame().setPayload("pong-2"));
send.add(new ContinuationFrame(",f5").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -121,7 +121,7 @@ public class ParserTest
public void testParseCase5_6()
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PongFrame("ping"));
send.add(new PongFrame().setPayload("ping"));
send.add(new TextFrame("hello, world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());

View File

@ -61,7 +61,7 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleMaskedPongRequest()
{
PongFrame pong = new PongFrame("Hello");
PongFrame pong = new PongFrame().setPayload("Hello");
pong.setMask(new byte[]
{ 0x37, (byte)0xfa, 0x21, 0x3d });
@ -158,7 +158,7 @@ public class RFC6455ExamplesGeneratorTest
@Test
public void testSingleUnmaskedPingRequest() throws Exception
{
PingFrame ping = new PingFrame("Hello");
PingFrame ping = new PingFrame().setPayload("Hello");
ByteBuffer actual = UnitGenerator.generate(ping);

View File

@ -55,7 +55,7 @@ public class TestABCase2
bytes[i] = Integer.valueOf(Integer.toOctalString(i)).byteValue();
}
PingFrame pingFrame = new PingFrame(bytes);
WebSocketFrame pingFrame = new PingFrame().setPayload(bytes);
ByteBuffer actual = UnitGenerator.generate(pingFrame);
@ -79,7 +79,7 @@ public class TestABCase2
{
byte[] bytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
PingFrame pingFrame = new PingFrame(bytes);
PingFrame pingFrame = new PingFrame().setPayload(bytes);
ByteBuffer actual = UnitGenerator.generate(pingFrame);
@ -122,7 +122,7 @@ public class TestABCase2
String message = "Hello, world!";
byte[] messageBytes = StringUtil.getUtf8Bytes(message);
PingFrame pingFrame = new PingFrame(messageBytes);
PingFrame pingFrame = new PingFrame().setPayload(messageBytes);
ByteBuffer actual = UnitGenerator.generate(pingFrame);

View File

@ -26,7 +26,6 @@ import org.eclipse.jetty.websocket.api.WebSocketException;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.extensions.Frame;
import org.eclipse.jetty.websocket.common.CloseInfo;
import org.eclipse.jetty.websocket.common.WebSocketFrame;
import org.eclipse.jetty.websocket.common.frames.BinaryFrame;
import org.eclipse.jetty.websocket.common.frames.PingFrame;
import org.eclipse.jetty.websocket.common.frames.TextFrame;
@ -116,7 +115,7 @@ public class EventDriverTest
try (LocalWebSocketSession conn = new LocalWebSocketSession(testname,driver))
{
conn.open();
driver.incomingFrame(new PingFrame("PING"));
driver.incomingFrame(new PingFrame().setPayload("PING"));
driver.incomingFrame(new TextFrame("Text Me"));
driver.incomingFrame(new BinaryFrame("Hello Bin"));
driver.incomingFrame(new CloseInfo(StatusCode.SHUTDOWN,"testcase").asFrame());

View File

@ -115,7 +115,7 @@ public class FragmentExtensionTest
ext.setNextIncomingFrames(capture);
String payload = "Are you there?";
Frame ping = new PingFrame(payload);
Frame ping = new PingFrame().setPayload(payload);
ext.incomingFrame(ping);
capture.assertFrameCount(1);
@ -286,7 +286,7 @@ public class FragmentExtensionTest
ext.setNextOutgoingFrames(capture);
String payload = "Are you there?";
Frame ping = new PingFrame(payload);
Frame ping = new PingFrame().setPayload(payload);
ext.outgoingFrame(ping,null);

View File

@ -177,7 +177,7 @@ public class MessageCompressionExtensionTest
ext.setNextIncomingFrames(capture);
String payload = "Are you there?";
Frame ping = new PingFrame(payload);
Frame ping = new PingFrame().setPayload(payload);
ext.incomingFrame(ping);
capture.assertFrameCount(1);
@ -339,7 +339,7 @@ public class MessageCompressionExtensionTest
ext.setNextOutgoingFrames(capture);
String payload = "Are you there?";
Frame ping = new PingFrame(payload);
Frame ping = new PingFrame().setPayload(payload);
ext.outgoingFrame(ping,null);

View File

@ -31,7 +31,6 @@ import org.eclipse.jetty.websocket.api.extensions.Frame;
import org.eclipse.jetty.websocket.common.CloseInfo;
import org.eclipse.jetty.websocket.common.Hex;
import org.eclipse.jetty.websocket.common.UnitGenerator;
import org.eclipse.jetty.websocket.common.WebSocketFrame;
import org.eclipse.jetty.websocket.common.frames.BinaryFrame;
import org.eclipse.jetty.websocket.common.frames.TextFrame;
import org.junit.Assert;

View File

@ -82,8 +82,8 @@ public class TestABCase2 extends AbstractABCase
for (int i = 0; i < pingCount; i++)
{
String payload = String.format("ping-%d[%X]",i,i);
send.add(new PingFrame(payload));
expect.add(new PongFrame(payload));
send.add(new PingFrame().setPayload(payload));
expect.add(new PongFrame().setPayload(payload));
}
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -121,8 +121,8 @@ public class TestABCase2 extends AbstractABCase
for (int i = 0; i < pingCount; i++)
{
String payload = String.format("ping-%d[%X]",i,i);
send.add(new PingFrame(payload));
expect.add(new PongFrame(payload));
send.add(new PingFrame().setPayload(payload));
expect.add(new PongFrame().setPayload(payload));
}
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -151,11 +151,11 @@ public class TestABCase2 extends AbstractABCase
byte payload[] = StringUtil.getUtf8Bytes("Hello world");
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PingFrame(payload));
send.add(new PingFrame().setPayload(payload));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame(copyOf(payload)));
expect.add(new PongFrame().setPayload(copyOf(payload)));
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
@ -182,11 +182,11 @@ public class TestABCase2 extends AbstractABCase
{ 0x00, (byte)0xFF, (byte)0xFE, (byte)0xFD, (byte)0xFC, (byte)0xFB, 0x00, (byte)0xFF };
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PingFrame(payload));
send.add(new PingFrame().setPayload(payload));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame(copyOf(payload)));
expect.add(new PongFrame().setPayload(copyOf(payload)));
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
@ -213,11 +213,11 @@ public class TestABCase2 extends AbstractABCase
Arrays.fill(payload,(byte)0xFE);
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PingFrame(payload));
send.add(new PingFrame().setPayload(payload));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame(copyOf(payload)));
expect.add(new PongFrame().setPayload(copyOf(payload)));
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
@ -280,11 +280,11 @@ public class TestABCase2 extends AbstractABCase
Arrays.fill(payload,(byte)'6');
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PingFrame(payload));
send.add(new PingFrame().setPayload(payload));
send.add(new CloseInfo(StatusCode.NORMAL,"Test 2.6").asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame(copyOf(payload)));
expect.add(new PongFrame().setPayload(copyOf(payload)));
expect.add(new CloseInfo(StatusCode.NORMAL,"Test 2.6").asFrame());
Fuzzer fuzzer = new Fuzzer(this);
@ -336,7 +336,7 @@ public class TestABCase2 extends AbstractABCase
public void testCase2_8() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PongFrame("unsolicited")); // unsolicited pong
send.add(new PongFrame().setPayload("unsolicited")); // unsolicited pong
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
@ -363,12 +363,12 @@ public class TestABCase2 extends AbstractABCase
public void testCase2_9() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PongFrame("unsolicited")); // unsolicited pong
send.add(new PingFrame("our ping")); // our ping
send.add(new PongFrame().setPayload("unsolicited")); // unsolicited pong
send.add(new PingFrame().setPayload("our ping")); // our ping
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame("our ping")); // our pong
expect.add(new PongFrame().setPayload("our ping")); // our pong
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
Fuzzer fuzzer = new Fuzzer(this);

View File

@ -71,7 +71,7 @@ public class TestABCase3 extends AbstractABCase
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload("small"));
send.add(new TextFrame().setPayload("small").setRsv2(true)); // intentionally bad
send.add(new PingFrame("ping"));
send.add(new PingFrame().setPayload("ping"));
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new TextFrame().setPayload("small")); // echo on good frame
@ -100,7 +100,7 @@ public class TestABCase3 extends AbstractABCase
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload("small"));
send.add(new TextFrame().setPayload("small").setRsv1(true).setRsv2(true)); // intentionally bad
send.add(new PingFrame("ping"));
send.add(new PingFrame().setPayload("ping"));
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new TextFrame().setPayload("small")); // echo on good frame
@ -129,7 +129,7 @@ public class TestABCase3 extends AbstractABCase
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload("small"));
send.add(new TextFrame().setPayload("small").setRsv3(true)); // intentionally bad
send.add(new PingFrame("ping"));
send.add(new PingFrame().setPayload("ping"));
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new TextFrame().setPayload("small")); // echo on good frame
@ -189,7 +189,7 @@ public class TestABCase3 extends AbstractABCase
Arrays.fill(payload,(byte)0xFF);
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PingFrame(payload).setRsv3(true).setRsv2(true)); // intentionally bad
send.add(new PingFrame().setPayload(payload).setRsv3(true).setRsv2(true)); // intentionally bad
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());

View File

@ -49,7 +49,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_1() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PingFrame("hello, ").setFin(false));
send.add(new PingFrame().setPayload("hello, ").setFin(false));
send.add(new ContinuationFrame("world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -346,21 +346,21 @@ public class TestABCase5 extends AbstractABCase
List<WebSocketFrame> send1 = new ArrayList<>();
send1.add(new TextFrame("f1").setFin(false));
send1.add(new ContinuationFrame(",f2").setFin(false));
send1.add(new PingFrame("pong-1"));
send1.add(new PingFrame().setPayload("pong-1"));
List<WebSocketFrame> expect1 = new ArrayList<>();
expect1.add(new PongFrame("pong-1"));
expect1.add(new PongFrame().setPayload("pong-1"));
// phase 2
List<WebSocketFrame> send2 = new ArrayList<>();
send2.add(new ContinuationFrame(",f3").setFin(false));
send2.add(new ContinuationFrame(",f4").setFin(false));
send2.add(new PingFrame("pong-2"));
send2.add(new PingFrame().setPayload("pong-2"));
send2.add(new ContinuationFrame(",f5").setFin(true));
send2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect2 = new ArrayList<>();
expect2.add(new PongFrame("pong-2"));
expect2.add(new PongFrame().setPayload("pong-2"));
expect2.add(new TextFrame().setPayload("f1,f2,f3,f4,f5"));
expect2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -394,7 +394,7 @@ public class TestABCase5 extends AbstractABCase
public void testCase5_2() throws Exception
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new PongFrame("hello, ").setFin(false));
send.add(new PongFrame().setPayload("hello, ").setFin(false));
send.add(new ContinuationFrame("world"));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -424,20 +424,20 @@ public class TestABCase5 extends AbstractABCase
List<WebSocketFrame> send1 = new ArrayList<>();
send1.add(new TextFrame("f1").setFin(false));
send1.add(new ContinuationFrame(",f2").setFin(false));
send1.add(new PingFrame("pong-1"));
send1.add(new PingFrame().setPayload("pong-1"));
List<WebSocketFrame> send2 = new ArrayList<>();
send2.add(new ContinuationFrame(",f3").setFin(false));
send2.add(new ContinuationFrame(",f4").setFin(false));
send2.add(new PingFrame("pong-2"));
send2.add(new PingFrame().setPayload("pong-2"));
send2.add(new ContinuationFrame(",f5").setFin(true));
send2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect1 = new ArrayList<>();
expect1.add(new PongFrame("pong-1"));
expect1.add(new PongFrame().setPayload("pong-1"));
List<WebSocketFrame> expect2 = new ArrayList<>();
expect2.add(new PongFrame("pong-2"));
expect2.add(new PongFrame().setPayload("pong-2"));
expect2.add(new TextFrame().setPayload("f1,f2,f3,f4,f5"));
expect2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -470,20 +470,20 @@ public class TestABCase5 extends AbstractABCase
List<WebSocketFrame> send1 = new ArrayList<>();
send1.add(new TextFrame("f1").setFin(false));
send1.add(new ContinuationFrame(",f2").setFin(false));
send1.add(new PingFrame("pong-1"));
send1.add(new PingFrame().setPayload("pong-1"));
List<WebSocketFrame> send2 = new ArrayList<>();
send2.add(new ContinuationFrame(",f3").setFin(false));
send2.add(new ContinuationFrame(",f4").setFin(false));
send2.add(new PingFrame("pong-2"));
send2.add(new PingFrame().setPayload("pong-2"));
send2.add(new ContinuationFrame(",f5").setFin(true));
send2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect1 = new ArrayList<>();
expect1.add(new PongFrame("pong-1"));
expect1.add(new PongFrame().setPayload("pong-1"));
List<WebSocketFrame> expect2 = new ArrayList<>();
expect2.add(new PongFrame("pong-2"));
expect2.add(new PongFrame().setPayload("pong-2"));
expect2.add(new TextFrame().setPayload("f1,f2,f3,f4,f5"));
expect2.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -604,12 +604,12 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new PingFrame("ping"));
send.add(new PingFrame().setPayload("ping"));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame("ping"));
expect.add(new PongFrame().setPayload("ping"));
expect.add(new TextFrame().setPayload("hello, world"));
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -635,12 +635,12 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new PingFrame("ping"));
send.add(new PingFrame().setPayload("ping"));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame("ping"));
expect.add(new PongFrame().setPayload("ping"));
expect.add(new TextFrame().setPayload("hello, world"));
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -666,12 +666,12 @@ public class TestABCase5 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame("hello, ").setFin(false));
send.add(new PingFrame("ping"));
send.add(new PingFrame().setPayload("ping"));
send.add(new ContinuationFrame("world").setFin(true));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new PongFrame("ping"));
expect.add(new PongFrame().setPayload("ping"));
expect.add(new TextFrame().setPayload("hello, world"));
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());

View File

@ -112,7 +112,7 @@ public class TestABCase7 extends AbstractABCase
{
List<WebSocketFrame> send = new ArrayList<>();
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
send.add(new PingFrame("out of band ping"));
send.add(new PingFrame().setPayload("out of band ping"));
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
@ -202,7 +202,7 @@ public class TestABCase7 extends AbstractABCase
List<WebSocketFrame> send = new ArrayList<>();
send.add(new TextFrame().setPayload(buf));
send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
send.add(new PingFrame("out of band"));
send.add(new PingFrame().setPayload("out of band"));
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new TextFrame().setPayload(clone(buf)));