improved websocket handling of error cases for autobahn test suite

This commit is contained in:
Greg Wilkins 2011-09-09 01:52:27 +10:00
parent a75b9bfb0c
commit e914e0f97f
3 changed files with 69 additions and 4 deletions

View File

@ -137,10 +137,14 @@ public abstract class Utf8Appendable
// 10xxxxxx
_bits=(_bits<<6)|(b&0x3f);
if (--_more==0)
{
if (_bits>=0xD800 && _bits<=0xDFFF)
throw new NotUtf8Exception();
_appendable.append(new String(Character.toChars(_bits)));
}
}
}
}
public static class NotUtf8Exception extends IllegalStateException

View File

@ -21,6 +21,25 @@ import org.junit.Test;
public class Utf8StringBuilderTest
{
@Test
public void testInvalid()
throws Exception
{
Utf8StringBuilder buffer = new Utf8StringBuilder();
buffer.append((byte)0xED);
buffer.append((byte)0xA0);
try
{
buffer.append((byte)0x80);
assertTrue(false);
}
catch(Utf8Appendable.NotUtf8Exception e)
{
assertTrue(true);
}
}
@Test
public void testUtfStringBuilder()
throws Exception
@ -34,6 +53,8 @@ public class Utf8StringBuilderTest
assertTrue(buffer.toString().endsWith("jetty"));
}
@Test
public void testShort()
throws Exception

View File

@ -42,6 +42,7 @@ import org.eclipse.jetty.websocket.WebSocket.OnTextMessage;
public class WebSocketConnectionD13 extends AbstractConnection implements WebSocketConnection
{
private static final Logger LOG = Log.getLogger(WebSocketConnectionD13.class);
private static final boolean STRICT=true;
final static byte OP_CONTINUATION = 0x00;
final static byte OP_TEXT = 0x01;
@ -636,6 +637,31 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
{
byte[] array=buffer.array();
if (STRICT)
{
if (isControlFrame(opcode) && buffer.length()>125)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Control frame too large");
return;
}
if ((flags&0x7)!=0)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"RSV bits set 0x"+Integer.toHexString(flags));
return;
}
if (_opcode!=-1 && opcode!=WebSocketConnectionD13.OP_CONTINUATION)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad continuation"+Integer.toHexString(opcode));
return;
}
// Ignore all frames after error close
if (_closeCode!=0 && _closeCode!=CLOSE_NORMAL && opcode!=OP_CLOSE)
return;
}
// Deliver frame if websocket is a FrameWebSocket
if (_onFrame!=null)
{
@ -653,6 +679,12 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
{
case WebSocketConnectionD13.OP_CONTINUATION:
{
if (_opcode==-1)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad Continuation");
return;
}
// If text, append to the message buffer
if (_onTextMessage!=null && _opcode==WebSocketConnectionD13.OP_TEXT)
{
@ -757,7 +789,7 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
break;
}
default:
case WebSocketConnectionD13.OP_BINARY:
{
if (_onBinaryMessage!=null && checkBinaryMessageSize(0,buffer.length()))
{
@ -779,11 +811,19 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
_connection.close(WebSocketConnectionD13.CLOSE_POLICY_VIOLATION,"Binary frame aggregation disabled");
}
}
break;
}
default:
if (STRICT)
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad opcode 0x"+Integer.toHexString(opcode));
return;
}
}
catch(Utf8Appendable.NotUtf8Exception notUtf8)
{
LOG.warn(notUtf8);
LOG.warn("{} for {}",notUtf8,_endp);
LOG.debug(notUtf8);
_connection.close(WebSocketConnectionD13.CLOSE_NOT_UTF8,"Invalid UTF-8");