From 27b05ccd6e6c39a8069a3a7cfd3c72cc414b09f0 Mon Sep 17 00:00:00 2001 From: Jesse McConnell Date: Fri, 6 Jul 2012 17:09:05 -0500 Subject: [PATCH] fix protocol exception regarding control frames and FIN=false --- .../jetty/websocket/generator/FrameGenerator.java | 12 ++++++++---- .../eclipse/jetty/websocket/ab/TestABCase3.java | 14 ++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java index cc07c7c6270..f0c95160e49 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/FrameGenerator.java @@ -4,7 +4,6 @@ import java.nio.ByteBuffer; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.websocket.api.PolicyViolationException; import org.eclipse.jetty.websocket.api.ProtocolException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.protocol.CloseInfo; @@ -57,6 +56,11 @@ public class FrameGenerator byte b; + if ( frame.getOpCode().isControlFrame() && !frame.isFin() ) + { + throw new ProtocolException("Control Frames must be FIN=true"); + } + // Setup fin thru opcode b = 0x00; if (frame.isFin()) @@ -67,19 +71,19 @@ public class FrameGenerator { b |= 0x40; // 0100_0000 // TODO: extensions can negotiate this (somehow) - throw new PolicyViolationException("RSV1 not allowed to be set"); + throw new ProtocolException("RSV1 not allowed to be set"); } if (frame.isRsv2()) { b |= 0x20; // 0010_0000 // TODO: extensions can negotiate this (somehow) - throw new PolicyViolationException("RSV2 not allowed to be set"); + throw new ProtocolException("RSV2 not allowed to be set"); } if (frame.isRsv3()) { b |= 0x10; // TODO: extensions can negotiate this (somehow) - throw new PolicyViolationException("RSV3 not allowed to be set"); + throw new ProtocolException("RSV3 not allowed to be set"); } byte opcode = frame.getOpCode().getCode(); diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java index c546204efc4..4a62c10a8a9 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/ab/TestABCase3.java @@ -5,7 +5,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.eclipse.jetty.websocket.api.PolicyViolationException; +import org.eclipse.jetty.websocket.api.ProtocolException; import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.generator.Generator; import org.eclipse.jetty.websocket.protocol.FrameBuilder; @@ -24,6 +24,8 @@ public class TestABCase3 { List data = new ArrayList<>(); // @formatter:off + data.add(new WebSocketFrame[] + { FrameBuilder.ping().fin(false).asFrame() }); data.add(new WebSocketFrame[] { FrameBuilder.ping().rsv1(true).asFrame() }); data.add(new WebSocketFrame[] @@ -31,11 +33,15 @@ public class TestABCase3 data.add(new WebSocketFrame[] { FrameBuilder.ping().rsv3(true).asFrame() }); data.add(new WebSocketFrame[] - { FrameBuilder.pong().rsv1(true).asFrame() }); + { FrameBuilder.pong().fin(false).asFrame() }); + data.add(new WebSocketFrame[] + { FrameBuilder.ping().rsv1(true).asFrame() }); data.add(new WebSocketFrame[] { FrameBuilder.pong().rsv2(true).asFrame() }); data.add(new WebSocketFrame[] { FrameBuilder.pong().rsv3(true).asFrame() }); + data.add(new WebSocketFrame[] + { FrameBuilder.close().fin(false).asFrame() }); data.add(new WebSocketFrame[] { FrameBuilder.close().rsv1(true).asFrame() }); data.add(new WebSocketFrame[] @@ -53,8 +59,8 @@ public class TestABCase3 this.invalidFrame = invalidFrame; } - @Test(expected = PolicyViolationException.class) - public void testGenerateRSV1CloseFrame() + @Test(expected = ProtocolException.class) + public void testGenerateInvalidControlFrame() { Generator generator = new Generator(WebSocketPolicy.newServerPolicy());