From 0d571ef2c24a7388a0031e5c972abd6be5667534 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Mon, 30 Dec 2019 13:25:30 +1100 Subject: [PATCH] Issue #4450 - websocket Extensions no longer rely upon WSCoreSession Signed-off-by: Lachlan Roberts --- .../jetty/websocket/core/AbstractExtension.java | 12 ++++++------ .../eclipse/jetty/websocket/core/Extension.java | 6 +++--- .../websocket/core/internal/ExtensionStack.java | 2 +- .../internal/PerMessageDeflateExtension.java | 8 ++++---- .../core/internal/ValidationExtension.java | 16 ++++++++++++++-- .../websocket/core/extensions/ExtensionTool.java | 2 +- .../PerMessageDeflateExtensionTest.java | 10 +++++----- 7 files changed, 34 insertions(+), 22 deletions(-) diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java index e3c629d5e67..2af69bef8e5 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/AbstractExtension.java @@ -26,7 +26,7 @@ import org.eclipse.jetty.util.compression.DeflaterPool; import org.eclipse.jetty.util.compression.InflaterPool; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; +import org.eclipse.jetty.websocket.core.FrameHandler.Configuration; @ManagedObject("Abstract Extension") public class AbstractExtension implements Extension @@ -36,7 +36,7 @@ public class AbstractExtension implements Extension private ExtensionConfig config; private OutgoingFrames nextOutgoing; private IncomingFrames nextIncoming; - private WebSocketCoreSession coreSession; + private Configuration configuration; private DeflaterPool deflaterPool; private InflaterPool inflaterPool; @@ -169,14 +169,14 @@ public class AbstractExtension implements Extension } @Override - public void setWebSocketCoreSession(WebSocketCoreSession coreSession) + public void setConfiguration(Configuration configuration) { - this.coreSession = coreSession; + this.configuration = configuration; } - protected WebSocketCoreSession getWebSocketCoreSession() + protected Configuration getConfiguration() { - return coreSession; + return configuration; } @Override diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/Extension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/Extension.java index 0fd66060372..0d97a2be44c 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/Extension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/Extension.java @@ -18,7 +18,7 @@ package org.eclipse.jetty.websocket.core; -import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; +import org.eclipse.jetty.websocket.core.FrameHandler.Configuration; /** * Interface for WebSocket Extensions. @@ -88,7 +88,7 @@ public interface Extension extends IncomingFrames, OutgoingFrames void setNextOutgoingFrames(OutgoingFrames nextOutgoing); /** - * Set the {@link WebSocketCoreSession} for this Extension + * Set the {@link Configuration} for this Extension. */ - void setWebSocketCoreSession(WebSocketCoreSession coreSession); + void setConfiguration(Configuration configuration); } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ExtensionStack.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ExtensionStack.java index 25f9d799b2e..e9784c01a60 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ExtensionStack.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ExtensionStack.java @@ -254,7 +254,7 @@ public class ExtensionStack implements IncomingFrames, OutgoingFrames, Dumpable for (Extension extension : extensions) { - extension.setWebSocketCoreSession(coreSession); + extension.setConfiguration(coreSession); } } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java index 3e79d933285..90e333c9c10 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/PerMessageDeflateExtension.java @@ -269,7 +269,7 @@ public class PerMessageDeflateExtension extends AbstractExtension private boolean deflate(Callback callback) { // Get a buffer for the inflated payload. - long maxFrameSize = getWebSocketCoreSession().getMaxFrameSize(); + long maxFrameSize = getConfiguration().getMaxFrameSize(); int bufferSize = (maxFrameSize <= 0) ? deflateBufferSize : (int)Math.min(maxFrameSize, deflateBufferSize); final ByteBuffer buffer = getBufferPool().acquire(bufferSize, false); callback = Callback.from(callback, () -> getBufferPool().release(buffer)); @@ -289,7 +289,7 @@ public class PerMessageDeflateExtension extends AbstractExtension if (buffer.limit() == bufferSize) { // We need to fragment. TODO: what if there was only bufferSize of content? - if (!getWebSocketCoreSession().isAutoFragment()) + if (!getConfiguration().isAutoFragment()) throw new MessageTooLargeException("Deflated payload exceeded the compress buffer size"); break; } @@ -402,7 +402,7 @@ public class PerMessageDeflateExtension extends AbstractExtension private boolean inflate(Callback callback) throws DataFormatException { // Get a buffer for the inflated payload. - long maxFrameSize = getWebSocketCoreSession().getMaxFrameSize(); + long maxFrameSize = getConfiguration().getMaxFrameSize(); int bufferSize = (maxFrameSize <= 0) ? inflateBufferSize : (int)Math.min(maxFrameSize, inflateBufferSize); final ByteBuffer payload = getBufferPool().acquire(bufferSize, false); callback = Callback.from(callback, () -> getBufferPool().release(payload)); @@ -421,7 +421,7 @@ public class PerMessageDeflateExtension extends AbstractExtension if (payload.limit() == bufferSize) { // We need to fragment. TODO: what if there was only bufferSize of content? - if (!getWebSocketCoreSession().isAutoFragment()) + if (!getConfiguration().isAutoFragment()) throw new MessageTooLargeException("Inflated payload exceeded the decompress buffer size"); break; } diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ValidationExtension.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ValidationExtension.java index 496ea84eb16..bbce3306a04 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ValidationExtension.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/ValidationExtension.java @@ -26,6 +26,7 @@ import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.core.AbstractExtension; import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.Frame; +import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.NullAppendable; import org.eclipse.jetty.websocket.core.ProtocolException; import org.eclipse.jetty.websocket.core.WebSocketComponents; @@ -38,6 +39,7 @@ public class ValidationExtension extends AbstractExtension { private static final Logger LOG = Log.getLogger(ValidationExtension.class); + private WebSocketCoreSession coreSession; private FrameSequence incomingSequence = null; private FrameSequence outgoingSequence = null; private boolean incomingFrameValidation = false; @@ -53,6 +55,16 @@ public class ValidationExtension extends AbstractExtension return "@validation"; } + @Override + public void setConfiguration(FrameHandler.Configuration configuration) + { + super.setConfiguration(configuration); + + if (!(configuration instanceof WebSocketCoreSession)) + throw new IllegalArgumentException("ValidationExtension needs a CoreSession Configuration"); + coreSession = (WebSocketCoreSession)configuration; + } + @Override public void onFrame(Frame frame, Callback callback) { @@ -62,7 +74,7 @@ public class ValidationExtension extends AbstractExtension incomingSequence.check(frame.getOpCode(), frame.isFin()); if (incomingFrameValidation) - getWebSocketCoreSession().assertValidIncoming(frame); + coreSession.assertValidIncoming(frame); if (incomingUtf8Validation != null) validateUTF8(frame, incomingUtf8Validation, continuedInOpCode); @@ -85,7 +97,7 @@ public class ValidationExtension extends AbstractExtension outgoingSequence.check(frame.getOpCode(), frame.isFin()); if (outgoingFrameValidation) - getWebSocketCoreSession().assertValidOutgoing(frame); + coreSession.assertValidOutgoing(frame); if (outgoingUtf8Validation != null) validateUTF8(frame, outgoingUtf8Validation, continuedOutOpCode); diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java index 890038716e1..b398cb3725b 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/ExtensionTool.java @@ -77,7 +77,7 @@ public class ExtensionTool { this.ext = components.getExtensionRegistry().newInstance(extConfig, components); this.ext.setNextIncomingFrames(capture); - this.ext.setWebSocketCoreSession(newWebSocketCoreSession()); + this.ext.setConfiguration(newWebSocketCoreSession()); } public void parseIncomingHex(String... rawhex) diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflateExtensionTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflateExtensionTest.java index 4366bff8d3f..01f8ca30b5e 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflateExtensionTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflateExtensionTest.java @@ -135,7 +135,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest tester.assertNegotiated("permessage-deflate"); - tester.parseIncomingHex( // context takeover (2 messages) + tester.parseIncomingHex(// context takeover (2 messages) // message 1 "0xc1 0x07", // (HEADER added for this test) "0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00", @@ -376,7 +376,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.init(config, components); - ext.setWebSocketCoreSession(newSession()); + ext.setConfiguration(newSession()); // Setup capture of incoming frames IncomingFramesCapture capture = new IncomingFramesCapture(); @@ -450,7 +450,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest { PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); ext.init(ExtensionConfig.parse("permessage-deflate"), components); - ext.setWebSocketCoreSession(newSession()); + ext.setConfiguration(newSession()); // Setup capture of outgoing frames OutgoingFramesCapture capture = new OutgoingFramesCapture(); @@ -497,7 +497,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ext.init(config, components); - ext.setWebSocketCoreSession(newSession()); + ext.setConfiguration(newSession()); // Setup capture of incoming frames OutgoingFramesCapture capture = new OutgoingFramesCapture(); @@ -548,7 +548,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest // Captured from Pywebsocket (r790) - "tora" sent 3 times. - tester.parseIncomingHex( // context takeover (3 messages) + tester.parseIncomingHex(// context takeover (3 messages) "c1 06 2a c9 2f 4a 04 00", // tora 1 "c1 05 2a 01 62 00 00", // tora 2 "c1 04 02 61 00 00" // tora 3