Merge branch 'jetty-9' of ssh://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project into jetty-9

This commit is contained in:
Joakim Erdfelt 2012-06-26 09:01:36 -07:00
commit d2056f3eb2
5 changed files with 588 additions and 15 deletions

View File

@ -68,23 +68,40 @@ public abstract class FrameGenerator<T extends BaseFrame>
// payload lengths
int payloadLength = frame.getPayloadLength();
if ((payloadLength >= 0x7F) && (payloadLength <= 0xFF_FF))
{
// we have a 16 bit length
b |= 0x7E;
framing.put(b); // indicate 2 byte length
framing.putShort((short)(payloadLength & 0xFF_FF)); // write 2 byte length
}
else if (payloadLength >= 0xFFFF)
/*
* if length is over 65535 then its a 7 + 64 bit length
*/
if (payloadLength > 0xFF_FF)
{
// we have a 64 bit length
b |= 0x7F;
framing.put(b); // indicate 4 byte length
framing.putInt(payloadLength); // write 4 byte length
framing.put((byte)0);
framing.put((byte)0);
framing.put((byte)0);
framing.put((byte)0);
framing.put((byte)((payloadLength>>24) & 0xFF));
framing.put((byte)((payloadLength>>16) & 0xFF));
framing.put((byte)((payloadLength>>8) & 0xFF));
framing.put((byte)(payloadLength & 0xFF));
}
/*
* if payload is ge 126 we have a 7 + 16 bit length
*/
else if (payloadLength >= 0x7E)
{
b |= 0x7E;
framing.put(b); // indicate 2 byte length
framing.put((byte)(payloadLength>>8));
framing.put((byte)(payloadLength & 0xFF));
}
/*
* we have a 7 bit length
*/
else
{
// we have a 7 bit length
b |= (payloadLength & 0x7F);
framing.put(b);
}

View File

@ -145,12 +145,14 @@ public abstract class FrameParser<T extends BaseFrame>
getFrame().setMasked((b & 0x80) != 0);
length = (byte)(0x7F & b);
System.out.println(length);
if (length == 127)
{
// length 4 bytes (extended payload length)
// length 8 bytes (extended payload length)
if (buffer.remaining() >= 4)
{
length = buffer.getInt();
buffer.getInt(); //toss the first one, first 4 bytes
length = buffer.getInt(); // last 4 bytes for actual length
}
else
{
@ -166,6 +168,10 @@ public abstract class FrameParser<T extends BaseFrame>
if (buffer.remaining() >= 2)
{
length = buffer.getShort();
if (length == -1)
{
length = 65535;
}
}
else
{

View File

@ -164,6 +164,23 @@ public class Parser
}
}
}
/*
* if the payload was empty we could end up in this state
* because there was no remaining bits to process
*/
if ( state == State.PAYLOAD )
{
parser.getFrame().setPayload(ByteBuffer.allocate(0));
notifyFrame( parser.getFrame() );
parser.reset();
if (parser.getFrame().isFin())
{
reset();
}
state = State.FINOP;
}
}
catch (WebSocketException e)
{
@ -174,7 +191,7 @@ public class Parser
notifyWebSocketException(new WebSocketException(t));
}
finally
{
{
// Be sure to consume after exceptions
buffer.position(buffer.limit());
}

View File

@ -0,0 +1,532 @@
package org.eclipse.jetty.websocket.ab;
import static org.hamcrest.Matchers.is;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.StandardByteBufferPool;
import org.eclipse.jetty.websocket.ByteBufferAssert;
import org.eclipse.jetty.websocket.Debug;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.frames.BinaryFrame;
import org.eclipse.jetty.websocket.frames.TextFrame;
import org.eclipse.jetty.websocket.generator.Generator;
import org.eclipse.jetty.websocket.parser.FrameParseCapture;
import org.eclipse.jetty.websocket.parser.Parser;
import org.eclipse.jetty.websocket.parser.TextPayloadParser;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class ABCase1
{
StandardByteBufferPool bufferPool = new StandardByteBufferPool();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
@Test
public void testGenerateEmptyTextCase1_1_1()
{
TextFrame textFrame = new TextFrame("");
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x81, (byte)0x00 });
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParseEmptyTextCase1_1_1()
{
ByteBuffer expected = ByteBuffer.allocate(5);
expected.put(new byte[]
{ (byte)0x81, (byte)0x00 });
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(0));
ByteBufferAssert.assertSize("TextFrame.payload",0,pActual.getPayload());
}
@Test
public void testGenerate125ByteTextCase1_1_2()
{
int length = 125;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7F;
expected.put(b);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse125ByteTextCase1_1_2()
{
int length = 125;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7F;
expected.put(b);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate126ByteTextCase1_1_3()
{
int length = 126;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
//expected.put((byte)((length>>8) & 0xFF));
//expected.put((byte)(length & 0xFF));
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse126ByteTextCase1_1_3()
{
int length = 126;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate127ByteTextCase1_1_4()
{
int length = 127;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
//expected.put((byte)((length>>8) & 0xFF));
//expected.put((byte)(length & 0xFF));
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse127ByteTextCase1_1_4()
{
int length = 127;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= length & 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate128ByteTextCase1_1_5()
{
int length = 128;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put((byte)(length>>8));
expected.put((byte)(length & 0xFF));
//expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse128ByteTextCase1_1_5()
{
int length = 128;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.putShort((short)length);
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate65535ByteTextCase1_1_6()
{
int length = 65535;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]{ (byte)0xff, (byte)0xff});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse65535ByteTextCase1_1_6()
{
Debug.enableDebugLogging(Parser.class);
Debug.enableDebugLogging(TextPayloadParser.class);
int length = 65535;
ByteBuffer expected = ByteBuffer.allocate(length + 5);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7E;
expected.put(b);
expected.put(new byte[]{ (byte)0xff, (byte)0xff});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
@Test
public void testGenerate65536ByteTextCase1_1_7()
{
int length = 65536;
StringBuilder builder = new StringBuilder();
for ( int i = 0 ; i < length ; ++i)
{
builder.append("*");
}
TextFrame textFrame = new TextFrame(builder.toString());
textFrame.setFin(true);
Generator generator = new Generator(bufferPool,policy);
ByteBuffer actual = generator.generate(textFrame);
ByteBuffer expected = ByteBuffer.allocate(length + 11);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7F;
expected.put(b);
expected.put(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
actual.flip();
expected.flip();
ByteBufferAssert.assertEquals("buffers do not match",expected,actual);
}
@Test
public void testParse65536ByteTextCase1_1_7()
{
int length = 65536;
ByteBuffer expected = ByteBuffer.allocate(length + 11);
expected.put(new byte[]
{ (byte)0x81 });
byte b = 0x00; // no masking
b |= 0x7F;
expected.put(b);
expected.put(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00});
for ( int i = 0 ; i < length ; ++i )
{
expected.put("*".getBytes());
}
expected.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setMaxTextMessageSize(length);
Parser parser = new Parser(policy);
FrameParseCapture capture = new FrameParseCapture();
parser.addListener(capture);
parser.parse(expected);
capture.assertNoErrors();
capture.assertHasFrame(TextFrame.class,1);
TextFrame pActual = (TextFrame)capture.getFrames().get(0);
Assert.assertThat("TextFrame.payloadLength",pActual.getPayloadLength(),is(length));
ByteBufferAssert.assertSize("TextFrame.payload",length,pActual.getPayload());
}
}

View File

@ -232,10 +232,11 @@ public class RFC6455ExamplesGeneratorTest
ByteBuffer expected = ByteBuffer.allocate(dataSize + 10);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// 256 bytes binary message in a single unmasked frame
// 64k bytes binary message in a single unmasked frame
expected.put(new byte[]
{ (byte)0x82, (byte)0x7F });
expected.putInt(0x0000000000010000);
expected.putInt(0x00_00_00_00);
expected.putInt(0x00_01_00_00);
for (int i = 0; i < dataSize; i++)
{