Fixes #1054 - NPE in Jetty WebSocketListener on PING frame with empty payload

This commit is contained in:
Joakim Erdfelt 2016-11-04 10:24:06 -07:00
parent 846d560b44
commit bc67969135
2 changed files with 57 additions and 0 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.frames;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.ProtocolException;
import org.eclipse.jetty.websocket.common.WebSocketFrame;
@ -128,4 +129,14 @@ public abstract class ControlFrame extends WebSocketFrame
}
return super.setPayload(buf);
}
@Override
public ByteBuffer getPayload()
{
if (super.getPayload() == null)
{
return BufferUtil.EMPTY_BUFFER;
}
return super.getPayload();
}
}

View File

@ -28,6 +28,7 @@ import org.eclipse.jetty.websocket.api.extensions.Frame;
import org.eclipse.jetty.websocket.common.CloseInfo;
import org.eclipse.jetty.websocket.common.frames.BinaryFrame;
import org.eclipse.jetty.websocket.common.frames.PingFrame;
import org.eclipse.jetty.websocket.common.frames.PongFrame;
import org.eclipse.jetty.websocket.common.frames.TextFrame;
import org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession;
import org.eclipse.jetty.websocket.common.io.LocalWebSocketSession;
@ -45,6 +46,7 @@ import examples.AnnotatedBinaryStreamSocket;
import examples.AnnotatedFramesSocket;
import examples.AnnotatedTextSocket;
import examples.ListenerBasicSocket;
import examples.ListenerPingPongSocket;
public class EventDriverTest
{
@ -185,6 +187,50 @@ public class EventDriverTest
}
}
@Test
public void testListenerPingPong() throws Exception
{
ListenerPingPongSocket socket = new ListenerPingPongSocket();
EventDriver driver = wrap(socket);
try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container,testname,driver))
{
conn.start();
conn.open();
driver.incomingFrame(new PingFrame().setPayload("PING"));
driver.incomingFrame(new PongFrame().setPayload("PONG"));
driver.incomingFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
socket.capture.assertEventCount(4);
socket.capture.pop().assertEventStartsWith("onWebSocketConnect");
socket.capture.pop().assertEventStartsWith("onWebSocketPing(");
socket.capture.pop().assertEventStartsWith("onWebSocketPong(");
socket.capture.pop().assertEventStartsWith("onWebSocketClose(1000,");
}
}
@Test
public void testListenerEmptyPingPong() throws Exception
{
ListenerPingPongSocket socket = new ListenerPingPongSocket();
EventDriver driver = wrap(socket);
try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container,testname,driver))
{
conn.start();
conn.open();
driver.incomingFrame(new PingFrame());
driver.incomingFrame(new PongFrame());
driver.incomingFrame(new CloseInfo(StatusCode.NORMAL).asFrame());
socket.capture.assertEventCount(4);
socket.capture.pop().assertEventStartsWith("onWebSocketConnect");
socket.capture.pop().assertEventStartsWith("onWebSocketPing(");
socket.capture.pop().assertEventStartsWith("onWebSocketPong(");
socket.capture.pop().assertEventStartsWith("onWebSocketClose(1000,");
}
}
private EventDriver wrap(Object websocket)
{
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();