diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/CloseException.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/CloseException.java new file mode 100644 index 00000000000..ce35114f7eb --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/CloseException.java @@ -0,0 +1,31 @@ +package org.eclipse.jetty.websocket.api; + +@SuppressWarnings("serial") +public class CloseException extends WebSocketException +{ + private short closeCode; + + public CloseException(short closeCode, String message) + { + super(message); + this.closeCode = closeCode; + } + + public CloseException(short closeCode, String message, Throwable cause) + { + super(message,cause); + this.closeCode = closeCode; + } + + public CloseException(short closeCode, Throwable cause) + { + super(cause); + this.closeCode = closeCode; + } + + public short getCloseCode() + { + return closeCode; + } + +} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/PolicyViolationException.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/PolicyViolationException.java new file mode 100644 index 00000000000..37540a0d2a1 --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/PolicyViolationException.java @@ -0,0 +1,20 @@ +package org.eclipse.jetty.websocket.api; + +@SuppressWarnings("serial") +public class PolicyViolationException extends CloseException +{ + public PolicyViolationException(String message) + { + super(WebSocket.CLOSE_POLICY_VIOLATION,message); + } + + public PolicyViolationException(String message, Throwable t) + { + super(WebSocket.CLOSE_POLICY_VIOLATION,message,t); + } + + public PolicyViolationException(Throwable t) + { + super(WebSocket.CLOSE_POLICY_VIOLATION,t); + } +} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBehavior.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBehavior.java new file mode 100644 index 00000000000..a1ed134bccc --- /dev/null +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketBehavior.java @@ -0,0 +1,13 @@ +package org.eclipse.jetty.websocket.api; + +/** + * Behavior for how the WebSocket should operate. + *

+ * This dictated by the RFC 6455 spec in various places, where certain behavior must be performed depending on + * operation as a CLIENT vs a SERVER + */ +public enum WebSocketBehavior +{ + CLIENT, + SERVER; +} diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketSettings.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketPolicy.java similarity index 70% rename from jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketSettings.java rename to jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketPolicy.java index 715c9407e0e..1dfc2f6126e 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketSettings.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketPolicy.java @@ -5,7 +5,7 @@ import org.eclipse.jetty.websocket.masks.Masker; /** * Settings for WebSocket operations. */ -public class WebSocketSettings +public class WebSocketPolicy { /** * The maximum size of a text message during parsing/generating @@ -20,7 +20,22 @@ public class WebSocketSettings * The implementation for masking */ private Masker masker = null; - + + /** + * Behavior of the websockets + */ + private WebSocketBehavior behavior = WebSocketBehavior.SERVER; // TODO: Review default + + public WebSocketBehavior getBehavior() + { + return behavior; + } + + public Masker getMasker() + { + return masker; + } + public int getMaxBinaryMessageSize() { return maxBinaryMessageSize; @@ -31,6 +46,16 @@ public class WebSocketSettings return maxTextMessageSize; } + public void setBehavior(WebSocketBehavior behavior) + { + this.behavior = behavior; + } + + public void setMasker(Masker masker) + { + this.masker = masker; + } + public void setMaxBinaryMessageSize(int maxBinaryMessageSize) { this.maxBinaryMessageSize = maxBinaryMessageSize; @@ -41,14 +66,4 @@ public class WebSocketSettings this.maxTextMessageSize = maxTextMessageSize; } - public Masker getMaskGen() - { - return masker; - } - - public void setMaskGen(Masker masker) - { - this.masker = masker; - } - } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java index 7c3873d473e..76caab6ba20 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/CloseFrameGenerator.java @@ -3,13 +3,13 @@ package org.eclipse.jetty.websocket.generator; import java.nio.ByteBuffer; import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.CloseFrame; import org.eclipse.jetty.websocket.frames.PingFrame; public class CloseFrameGenerator extends FrameGenerator { - public CloseFrameGenerator(ByteBufferPool bufferPool, WebSocketSettings settings) + public CloseFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy settings) { super(bufferPool, settings); } 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 bd3b0a861c1..63d29248177 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 @@ -3,15 +3,15 @@ package org.eclipse.jetty.websocket.generator; import java.nio.ByteBuffer; import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.BaseFrame; public abstract class FrameGenerator { private final ByteBufferPool bufferPool; - private final WebSocketSettings settings; + private final WebSocketPolicy settings; - protected FrameGenerator(ByteBufferPool bufferPool, WebSocketSettings settings) + protected FrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy settings) { this.bufferPool = bufferPool; this.settings = settings; diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java index 43371dc545e..bc96ee24e3b 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/Generator.java @@ -5,7 +5,7 @@ import java.util.EnumMap; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.websocket.api.OpCode; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.BaseFrame; import org.eclipse.jetty.websocket.masks.Masker; @@ -38,7 +38,7 @@ public class Generator { private final EnumMap> generators = new EnumMap<>(OpCode.class); private Masker maskgen = null; - public Generator(ByteBufferPool bufferPool, WebSocketSettings settings) + public Generator(ByteBufferPool bufferPool, WebSocketPolicy settings) { generators.put(OpCode.PING,new PingFrameGenerator(bufferPool, settings)); generators.put(OpCode.PONG,new PongFrameGenerator(bufferPool, settings)); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java index a4a9cc164d9..e0399267624 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PingFrameGenerator.java @@ -3,12 +3,12 @@ package org.eclipse.jetty.websocket.generator; import java.nio.ByteBuffer; import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.PingFrame; public class PingFrameGenerator extends FrameGenerator { - public PingFrameGenerator(ByteBufferPool bufferPool, WebSocketSettings settings) + public PingFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy settings) { super(bufferPool, settings); } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java index 6f683e21012..4a68f6a9da1 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/generator/PongFrameGenerator.java @@ -3,13 +3,13 @@ package org.eclipse.jetty.websocket.generator; import java.nio.ByteBuffer; import org.eclipse.jetty.io.ByteBufferPool; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.PingFrame; import org.eclipse.jetty.websocket.frames.PongFrame; public class PongFrameGenerator extends FrameGenerator { - public PongFrameGenerator(ByteBufferPool bufferPool, WebSocketSettings settings) + public PongFrameGenerator(ByteBufferPool bufferPool, WebSocketPolicy settings) { super(bufferPool, settings); } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/BinaryPayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/BinaryPayloadParser.java index bfa44e5f9e6..20b04c39a40 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/BinaryPayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/BinaryPayloadParser.java @@ -2,7 +2,7 @@ package org.eclipse.jetty.websocket.parser; import java.nio.ByteBuffer; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.BinaryFrame; /** @@ -14,7 +14,7 @@ public class BinaryPayloadParser extends FrameParser private ByteBuffer payload; private int payloadLength; - public BinaryPayloadParser(WebSocketSettings settings) + public BinaryPayloadParser(WebSocketPolicy settings) { super(settings); frame = new BinaryFrame(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/ClosePayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/ClosePayloadParser.java index 42ce80a4a5b..392204b1b20 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/ClosePayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/ClosePayloadParser.java @@ -2,7 +2,7 @@ package org.eclipse.jetty.websocket.parser; import java.nio.ByteBuffer; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.CloseFrame; /** @@ -12,7 +12,7 @@ public class ClosePayloadParser extends FrameParser { private CloseFrame frame; - public ClosePayloadParser(WebSocketSettings settings) + public ClosePayloadParser(WebSocketPolicy settings) { super(settings); frame = new CloseFrame(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java index 0f05d4365f1..01cb054756d 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/FrameParser.java @@ -5,7 +5,7 @@ import java.nio.ByteBuffer; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.OpCode; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.BaseFrame; /** @@ -44,12 +44,12 @@ public abstract class FrameParser } private static final Logger LOG = Log.getLogger(FrameParser.class); - private WebSocketSettings settings; + private WebSocketPolicy settings; private State state = State.PAYLOAD_LEN; private int length = 0; private int cursor = 0; - public FrameParser(WebSocketSettings settings) + public FrameParser(WebSocketPolicy settings) { this.settings = settings; } @@ -94,7 +94,7 @@ public abstract class FrameParser */ public abstract T getFrame(); - protected WebSocketSettings getSettings() + protected WebSocketPolicy getSettings() { return settings; } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/Parser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/Parser.java index b036f1d6f1a..c8133d16e11 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/Parser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/Parser.java @@ -10,7 +10,7 @@ import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.OpCode; import org.eclipse.jetty.websocket.api.WebSocketException; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.BaseFrame; /** @@ -36,16 +36,16 @@ public class Parser private final List listeners = new CopyOnWriteArrayList<>(); private final EnumMap> parsers = new EnumMap<>(OpCode.class); private FrameParser parser; - private WebSocketSettings settings; + private WebSocketPolicy settings; private State state = State.FINOP; private int currentContinuationIndex = 0; public Parser() { - this(new WebSocketSettings()); + this(new WebSocketPolicy()); } - public Parser(WebSocketSettings settings) + public Parser(WebSocketPolicy settings) { /* * TODO: Investigate addition of decompression factory similar to SPDY work in situation of negotiated deflate extension? @@ -67,7 +67,7 @@ public class Parser listeners.add(listener); } - public WebSocketSettings getSettings() + public WebSocketPolicy getSettings() { return settings; } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java index 283577311a0..3f586bb7fd3 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PingPayloadParser.java @@ -2,7 +2,7 @@ package org.eclipse.jetty.websocket.parser; import java.nio.ByteBuffer; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.PingFrame; /** @@ -14,7 +14,7 @@ public class PingPayloadParser extends FrameParser private ByteBuffer payload; private int payloadLength; - public PingPayloadParser(WebSocketSettings settings) + public PingPayloadParser(WebSocketPolicy settings) { super(settings); frame = new PingFrame(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PongPayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PongPayloadParser.java index 256ab9a6844..863f422f94c 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PongPayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/PongPayloadParser.java @@ -2,7 +2,7 @@ package org.eclipse.jetty.websocket.parser; import java.nio.ByteBuffer; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.PongFrame; public class PongPayloadParser extends FrameParser @@ -11,7 +11,7 @@ public class PongPayloadParser extends FrameParser private ByteBuffer payload; private int payloadLength; - public PongPayloadParser(WebSocketSettings settings) + public PongPayloadParser(WebSocketPolicy settings) { super(settings); frame = new PongFrame(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/TextPayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/TextPayloadParser.java index fe773c30dcb..a5ab340d863 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/TextPayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/TextPayloadParser.java @@ -4,7 +4,7 @@ import java.nio.ByteBuffer; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.TextFrame; public class TextPayloadParser extends FrameParser @@ -13,7 +13,7 @@ public class TextPayloadParser extends FrameParser private ByteBuffer payload; private int payloadLength; - public TextPayloadParser(WebSocketSettings settings) + public TextPayloadParser(WebSocketPolicy settings) { super(settings); frame = new TextFrame(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/UnknownPayloadParser.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/UnknownPayloadParser.java index e4a17715842..0ab141523cd 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/UnknownPayloadParser.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/parser/UnknownPayloadParser.java @@ -2,14 +2,14 @@ package org.eclipse.jetty.websocket.parser; import java.nio.ByteBuffer; -import org.eclipse.jetty.websocket.api.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.BaseFrame; public class UnknownPayloadParser extends FrameParser { private BaseFrame frame; - public UnknownPayloadParser(WebSocketSettings settings) + public UnknownPayloadParser(WebSocketPolicy settings) { super(settings); frame = new BaseFrame(); diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java index a7cf9c2bad1..a5cc27b19ee 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/generator/RFC6455ExamplesGeneratorTest.java @@ -6,7 +6,7 @@ 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.WebSocketSettings; +import org.eclipse.jetty.websocket.api.WebSocketPolicy; import org.eclipse.jetty.websocket.frames.PingFrame; import org.eclipse.jetty.websocket.frames.PongFrame; import org.eclipse.jetty.websocket.masks.FixedMasker; @@ -34,7 +34,7 @@ public class RFC6455ExamplesGeneratorTest payload.put("Hello".getBytes(), 0, 5); ping.setPayload(payload); - PingFrameGenerator generator = new PingFrameGenerator(bufferPool, new WebSocketSettings()); + PingFrameGenerator generator = new PingFrameGenerator(bufferPool, new WebSocketPolicy()); ByteBuffer generatedPing = generator.generate(ping); @@ -61,8 +61,8 @@ public class RFC6455ExamplesGeneratorTest payload.put("Hello".getBytes(), 0, 5); pong.setPayload(payload); - WebSocketSettings settings = new WebSocketSettings(); - settings.setMaskGen(new FixedMasker()); + WebSocketPolicy settings = new WebSocketPolicy(); + settings.setMasker(new FixedMasker()); PongFrameGenerator generator = new PongFrameGenerator(bufferPool, settings);