Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/main/java/org/java_websocket/drafts/Draft_6455.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ public class Draft_6455 extends Draft {
*/
private final SecureRandom reuseableRandom = new SecureRandom();

/**
* Default maximum payload size for a single frame when not explicitly configured (16 MiB).
*
* @since 1.6.1
*/
public static final int DEFAULT_MAX_FRAME_SIZE = 16 * 1024 * 1024;

/**
* Attribute for the maximum allowed size of a frame
*
Expand Down Expand Up @@ -207,7 +214,7 @@ public Draft_6455(List<IExtension> inputExtensions) {
* @since 1.3.7
*/
public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtocols) {
this(inputExtensions, inputProtocols, Integer.MAX_VALUE);
this(inputExtensions, inputProtocols, DEFAULT_MAX_FRAME_SIZE);
}

/**
Expand Down Expand Up @@ -381,6 +388,15 @@ public int getMaxFrameSize() {
return maxFrameSize;
}

@Override
public int checkAlloc(int bytecount) throws InvalidDataException {
int allowed = super.checkAlloc(bytecount);
if (allowed > maxFrameSize) {
throw new LimitExceededException("Payload limit reached.", maxFrameSize);
}
return allowed;
}

/**
* Getter for all available protocols for this draft
*
Expand Down
43 changes: 37 additions & 6 deletions src/test/java/org/java_websocket/drafts/Draft_6455Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.java_websocket.enums.CloseHandshakeType;
import org.java_websocket.enums.HandshakeState;
import org.java_websocket.exceptions.InvalidHandshakeException;
import org.java_websocket.exceptions.LimitExceededException;
import org.java_websocket.extensions.DefaultExtension;
import org.java_websocket.extensions.IExtension;
import org.java_websocket.framing.BinaryFrame;
Expand Down Expand Up @@ -123,6 +124,24 @@ public void testGetExtension() throws Exception {
assert (draft_6455.getExtension() instanceof DefaultExtension);
}

@Test
public void testDefaultMaxFrameSize() {
Draft_6455 draft = new Draft_6455();
assertEquals(Draft_6455.DEFAULT_MAX_FRAME_SIZE, draft.getMaxFrameSize());
}

@Test
public void testDeclaredPayloadLengthAboveDefaultMaxFrameSizeRejected() {
Draft_6455 draft = new Draft_6455();
long declaredLength = (long) Draft_6455.DEFAULT_MAX_FRAME_SIZE + 1;
ByteBuffer frame = ByteBuffer.allocate(10);
frame.put((byte) 0x81);
frame.put((byte) 127);
frame.putLong(declaredLength);
frame.flip();
assertThrows(LimitExceededException.class, () -> draft.translateFrame(frame));
}

@Test
public void testGetKnownExtensions() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
Expand Down Expand Up @@ -202,24 +221,36 @@ public void testGetCloseHandshakeType() throws Exception {
@Test
public void testToString() throws Exception {
Draft_6455 draft_6455 = new Draft_6455();
assertEquals("Draft_6455 extension: DefaultExtension max frame size: 2147483647",
assertEquals(
"Draft_6455 extension: DefaultExtension max frame size: "
+ Draft_6455.DEFAULT_MAX_FRAME_SIZE,
draft_6455.toString());
draft_6455.acceptHandshakeAsServer(handshakedataProtocolExtension);
assertEquals("Draft_6455 extension: DefaultExtension protocol: max frame size: 2147483647",
assertEquals(
"Draft_6455 extension: DefaultExtension protocol: max frame size: "
+ Draft_6455.DEFAULT_MAX_FRAME_SIZE,
draft_6455.toString());
draft_6455 = new Draft_6455(Collections.emptyList(),
Collections.singletonList(new Protocol("chat")));
assertEquals("Draft_6455 extension: DefaultExtension max frame size: 2147483647",
assertEquals(
"Draft_6455 extension: DefaultExtension max frame size: "
+ Draft_6455.DEFAULT_MAX_FRAME_SIZE,
draft_6455.toString());
draft_6455.acceptHandshakeAsServer(handshakedataProtocolExtension);
assertEquals("Draft_6455 extension: DefaultExtension protocol: chat max frame size: 2147483647",
assertEquals(
"Draft_6455 extension: DefaultExtension protocol: chat max frame size: "
+ Draft_6455.DEFAULT_MAX_FRAME_SIZE,
draft_6455.toString());
draft_6455 = new Draft_6455(Collections.singletonList(new TestExtension()),
Collections.singletonList(new Protocol("chat")));
assertEquals("Draft_6455 extension: DefaultExtension max frame size: 2147483647",
assertEquals(
"Draft_6455 extension: DefaultExtension max frame size: "
+ Draft_6455.DEFAULT_MAX_FRAME_SIZE,
draft_6455.toString());
draft_6455.acceptHandshakeAsServer(handshakedataProtocolExtension);
assertEquals("Draft_6455 extension: TestExtension protocol: chat max frame size: 2147483647",
assertEquals(
"Draft_6455 extension: TestExtension protocol: chat max frame size: "
+ Draft_6455.DEFAULT_MAX_FRAME_SIZE,
draft_6455.toString());
draft_6455 = new Draft_6455(Collections.emptyList(),
Collections.singletonList(new Protocol("chat")), 10);
Expand Down
Loading