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,7 +137,11 @@ 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)));
}
}
}
}

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
@ -33,7 +52,9 @@ public class Utf8StringBuilderTest
assertEquals(source, buffer.toString());
assertTrue(buffer.toString().endsWith("jetty"));
}
@Test
public void testShort()
throws Exception

View File

@ -42,7 +42,8 @@ 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;
final static byte OP_BINARY = 0x02;
@ -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)
{
@ -648,11 +674,17 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
if (_onControl.onControl(opcode,array,buffer.getIndex(),buffer.length()))
return;
}
switch(opcode)
{
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");