Fixing testcase 7.5.1 (bad utf8 close reason) handling

This commit is contained in:
Joakim Erdfelt 2012-08-07 09:53:33 -07:00
parent 5a77c0801a
commit d76a5ce80e
3 changed files with 28 additions and 6 deletions

View File

@ -181,7 +181,8 @@ public class WebSocketEventDriver implements IncomingFrames
{
case OpCode.CLOSE:
{
CloseInfo close = new CloseInfo(frame);
boolean validate = true;
CloseInfo close = new CloseInfo(frame,validate);
if (events.onClose != null)
{
events.onClose.call(websocket,session,close.getStatusCode(),close.getReason());

View File

@ -19,8 +19,11 @@ import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.BadPayloadException;
import org.eclipse.jetty.websocket.api.ProtocolException;
import org.eclipse.jetty.websocket.api.StatusCode;
@ -71,13 +74,26 @@ public class CloseInfo
// Reason
try
{
reason = BufferUtil.toUTF8String(data);
Utf8StringBuilder utf = new Utf8StringBuilder();
utf.append(data);
reason = utf.toString();
}
catch (NotUtf8Exception e)
{
if (validate)
{
throw new BadPayloadException("Invalid Close Reason",e);
}
else
{
LOG.warn(e);
}
}
catch (RuntimeException e)
{
if (validate)
{
throw new ProtocolException("Invalid Close Reason:",e);
throw new ProtocolException("Invalid Close Reason",e);
}
else
{
@ -104,6 +120,11 @@ public class CloseInfo
this(frame.getPayload(),false);
}
public CloseInfo(WebSocketFrame frame, boolean validate)
{
this(frame.getPayload(),validate);
}
public ByteBuffer asByteBuffer()
{
if (statusCode != (-1))

View File

@ -384,8 +384,8 @@ public class TestABCase7 extends AbstractABCase
{
ByteBuffer payload = ByteBuffer.allocate(256);
BufferUtil.clearToFill(payload);
payload.put((byte)0xE8); // normal close
payload.put((byte)0x03);
payload.put((byte)0x03); // normal close
payload.put((byte)0xE8);
byte invalidUtf[] = Hex.asByteArray("CEBAE1BDB9CF83CEBCCEB5EDA080656469746564");
payload.put(invalidUtf);
BufferUtil.flipToFlush(payload,0);
@ -397,7 +397,7 @@ public class TestABCase7 extends AbstractABCase
send.add(close);
List<WebSocketFrame> expect = new ArrayList<>();
expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
expect.add(new CloseInfo(StatusCode.BAD_PAYLOAD).asFrame());
Fuzzer fuzzer = new Fuzzer(this);
try