Fixing EMPTY payload parsing issue

This commit is contained in:
Joakim Erdfelt 2012-07-06 14:01:35 -07:00
parent fc8198e854
commit 2f3937687d
3 changed files with 45 additions and 8 deletions

View File

@ -30,9 +30,9 @@ public class WebSocketPolicy
/** /**
* The maximum allowed payload size (validated in both directions) * The maximum allowed payload size (validated in both directions)
* <p> * <p>
* Default: 65535 (64K) * Default: 65536 (64K)
*/ */
private int maxPayloadSize = 65535; private int maxPayloadSize = 65536;
/** /**
* The maximum size of a text message during parsing/generating. * The maximum size of a text message during parsing/generating.
@ -51,9 +51,9 @@ public class WebSocketPolicy
/** /**
* Maximum Message Buffer size, which is also the max frame byte size. * Maximum Message Buffer size, which is also the max frame byte size.
* <p> * <p>
* Default: 65535 (64 K) * Default: 65536 (64 K)
*/ */
private int bufferSize = 65535; private int bufferSize = 65536;
/** /**
* The time in ms (milliseconds) that a websocket may be idle before closing. * The time in ms (milliseconds) that a websocket may be idle before closing.
@ -92,9 +92,9 @@ public class WebSocketPolicy
public void assertValidPayloadLength(int payloadLength) public void assertValidPayloadLength(int payloadLength)
{ {
// validate to buffer sizes // validate to buffer sizes
if (payloadLength > bufferSize) if (payloadLength > payloadLength)
{ {
throw new MessageTooLargeException("Requested payload length [" + payloadLength + "] exceeds maximum size [" + bufferSize + "]"); throw new MessageTooLargeException("Requested payload length [" + payloadLength + "] exceeds maximum size [" + payloadLength + "]");
} }
} }
@ -188,6 +188,10 @@ public class WebSocketPolicy
public void setMaxPayloadSize(int maxPayloadSize) public void setMaxPayloadSize(int maxPayloadSize)
{ {
if (maxPayloadSize < bufferSize)
{
throw new IllegalStateException("Cannot have payload size be smaller than buffer size");
}
this.maxPayloadSize = maxPayloadSize; this.maxPayloadSize = maxPayloadSize;
} }

View File

@ -229,6 +229,13 @@ public class FrameParser
} }
else else
{ {
// special case for empty payloads (no more bytes left in buffer)
if (payloadLength == 0)
{
state = State.START;
return true;
}
state = State.PAYLOAD; state = State.PAYLOAD;
} }
@ -248,6 +255,13 @@ public class FrameParser
} }
else else
{ {
// special case for empty payloads (no more bytes left in buffer)
if (payloadLength == 0)
{
state = State.START;
return true;
}
state = State.PAYLOAD; state = State.PAYLOAD;
} }
} }
@ -260,6 +274,13 @@ public class FrameParser
if (buffer.remaining() >= 4) if (buffer.remaining() >= 4)
{ {
buffer.get(m,0,4); buffer.get(m,0,4);
// special case for empty payloads (no more bytes left in buffer)
if (payloadLength == 0)
{
state = State.START;
return true;
}
state = State.PAYLOAD; state = State.PAYLOAD;
} }
else else
@ -276,6 +297,13 @@ public class FrameParser
getFrame().getMask()[cursor] = b; getFrame().getMask()[cursor] = b;
if (cursor == 0) if (cursor == 0)
{ {
// special case for empty payloads (no more bytes left in buffer)
if (payloadLength == 0)
{
state = State.START;
return true;
}
state = State.PAYLOAD; state = State.PAYLOAD;
} }
break; break;
@ -310,6 +338,11 @@ public class FrameParser
*/ */
public boolean parsePayload(ByteBuffer buffer) public boolean parsePayload(ByteBuffer buffer)
{ {
if (payloadLength == 0)
{
return true;
}
while (buffer.hasRemaining()) while (buffer.hasRemaining())
{ {
if (payload == null) if (payload == null)

View File

@ -1,6 +1,6 @@
package org.eclipse.jetty.websocket.ab; package org.eclipse.jetty.websocket.ab;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -542,6 +542,6 @@ public class TestABCase1_2
WebSocketFrame pActual = capture.getFrames().get(0); WebSocketFrame pActual = capture.getFrames().get(0);
Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(0)); Assert.assertThat("BinaryFrame.payloadLength",pActual.getPayloadLength(),is(0));
Assert.assertEquals("BinaryFrame.payload",0,pActual.getPayloadData().length); Assert.assertNull("BinaryFrame.payload",pActual.getPayloadData());
} }
} }