Issue #4450 - websocket Extensions no longer rely upon WSCoreSession

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2019-12-30 13:25:30 +11:00
parent c98897315c
commit 0d571ef2c2
7 changed files with 34 additions and 22 deletions

View File

@ -26,7 +26,7 @@ import org.eclipse.jetty.util.compression.DeflaterPool;
import org.eclipse.jetty.util.compression.InflaterPool; import org.eclipse.jetty.util.compression.InflaterPool;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; 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") @ManagedObject("Abstract Extension")
public class AbstractExtension implements Extension public class AbstractExtension implements Extension
@ -36,7 +36,7 @@ public class AbstractExtension implements Extension
private ExtensionConfig config; private ExtensionConfig config;
private OutgoingFrames nextOutgoing; private OutgoingFrames nextOutgoing;
private IncomingFrames nextIncoming; private IncomingFrames nextIncoming;
private WebSocketCoreSession coreSession; private Configuration configuration;
private DeflaterPool deflaterPool; private DeflaterPool deflaterPool;
private InflaterPool inflaterPool; private InflaterPool inflaterPool;
@ -169,14 +169,14 @@ public class AbstractExtension implements Extension
} }
@Override @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 @Override

View File

@ -18,7 +18,7 @@
package org.eclipse.jetty.websocket.core; 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. * Interface for WebSocket Extensions.
@ -88,7 +88,7 @@ public interface Extension extends IncomingFrames, OutgoingFrames
void setNextOutgoingFrames(OutgoingFrames nextOutgoing); 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);
} }

View File

@ -254,7 +254,7 @@ public class ExtensionStack implements IncomingFrames, OutgoingFrames, Dumpable
for (Extension extension : extensions) for (Extension extension : extensions)
{ {
extension.setWebSocketCoreSession(coreSession); extension.setConfiguration(coreSession);
} }
} }

View File

@ -269,7 +269,7 @@ public class PerMessageDeflateExtension extends AbstractExtension
private boolean deflate(Callback callback) private boolean deflate(Callback callback)
{ {
// Get a buffer for the inflated payload. // 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); int bufferSize = (maxFrameSize <= 0) ? deflateBufferSize : (int)Math.min(maxFrameSize, deflateBufferSize);
final ByteBuffer buffer = getBufferPool().acquire(bufferSize, false); final ByteBuffer buffer = getBufferPool().acquire(bufferSize, false);
callback = Callback.from(callback, () -> getBufferPool().release(buffer)); callback = Callback.from(callback, () -> getBufferPool().release(buffer));
@ -289,7 +289,7 @@ public class PerMessageDeflateExtension extends AbstractExtension
if (buffer.limit() == bufferSize) if (buffer.limit() == bufferSize)
{ {
// We need to fragment. TODO: what if there was only bufferSize of content? // 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"); throw new MessageTooLargeException("Deflated payload exceeded the compress buffer size");
break; break;
} }
@ -402,7 +402,7 @@ public class PerMessageDeflateExtension extends AbstractExtension
private boolean inflate(Callback callback) throws DataFormatException private boolean inflate(Callback callback) throws DataFormatException
{ {
// Get a buffer for the inflated payload. // 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); int bufferSize = (maxFrameSize <= 0) ? inflateBufferSize : (int)Math.min(maxFrameSize, inflateBufferSize);
final ByteBuffer payload = getBufferPool().acquire(bufferSize, false); final ByteBuffer payload = getBufferPool().acquire(bufferSize, false);
callback = Callback.from(callback, () -> getBufferPool().release(payload)); callback = Callback.from(callback, () -> getBufferPool().release(payload));
@ -421,7 +421,7 @@ public class PerMessageDeflateExtension extends AbstractExtension
if (payload.limit() == bufferSize) if (payload.limit() == bufferSize)
{ {
// We need to fragment. TODO: what if there was only bufferSize of content? // 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"); throw new MessageTooLargeException("Inflated payload exceeded the decompress buffer size");
break; break;
} }

View File

@ -26,6 +26,7 @@ import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.core.AbstractExtension; import org.eclipse.jetty.websocket.core.AbstractExtension;
import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.ExtensionConfig;
import org.eclipse.jetty.websocket.core.Frame; 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.NullAppendable;
import org.eclipse.jetty.websocket.core.ProtocolException; import org.eclipse.jetty.websocket.core.ProtocolException;
import org.eclipse.jetty.websocket.core.WebSocketComponents; 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 static final Logger LOG = Log.getLogger(ValidationExtension.class);
private WebSocketCoreSession coreSession;
private FrameSequence incomingSequence = null; private FrameSequence incomingSequence = null;
private FrameSequence outgoingSequence = null; private FrameSequence outgoingSequence = null;
private boolean incomingFrameValidation = false; private boolean incomingFrameValidation = false;
@ -53,6 +55,16 @@ public class ValidationExtension extends AbstractExtension
return "@validation"; 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 @Override
public void onFrame(Frame frame, Callback callback) public void onFrame(Frame frame, Callback callback)
{ {
@ -62,7 +74,7 @@ public class ValidationExtension extends AbstractExtension
incomingSequence.check(frame.getOpCode(), frame.isFin()); incomingSequence.check(frame.getOpCode(), frame.isFin());
if (incomingFrameValidation) if (incomingFrameValidation)
getWebSocketCoreSession().assertValidIncoming(frame); coreSession.assertValidIncoming(frame);
if (incomingUtf8Validation != null) if (incomingUtf8Validation != null)
validateUTF8(frame, incomingUtf8Validation, continuedInOpCode); validateUTF8(frame, incomingUtf8Validation, continuedInOpCode);
@ -85,7 +97,7 @@ public class ValidationExtension extends AbstractExtension
outgoingSequence.check(frame.getOpCode(), frame.isFin()); outgoingSequence.check(frame.getOpCode(), frame.isFin());
if (outgoingFrameValidation) if (outgoingFrameValidation)
getWebSocketCoreSession().assertValidOutgoing(frame); coreSession.assertValidOutgoing(frame);
if (outgoingUtf8Validation != null) if (outgoingUtf8Validation != null)
validateUTF8(frame, outgoingUtf8Validation, continuedOutOpCode); validateUTF8(frame, outgoingUtf8Validation, continuedOutOpCode);

View File

@ -77,7 +77,7 @@ public class ExtensionTool
{ {
this.ext = components.getExtensionRegistry().newInstance(extConfig, components); this.ext = components.getExtensionRegistry().newInstance(extConfig, components);
this.ext.setNextIncomingFrames(capture); this.ext.setNextIncomingFrames(capture);
this.ext.setWebSocketCoreSession(newWebSocketCoreSession()); this.ext.setConfiguration(newWebSocketCoreSession());
} }
public void parseIncomingHex(String... rawhex) public void parseIncomingHex(String... rawhex)

View File

@ -135,7 +135,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
tester.assertNegotiated("permessage-deflate"); tester.assertNegotiated("permessage-deflate");
tester.parseIncomingHex( // context takeover (2 messages) tester.parseIncomingHex(// context takeover (2 messages)
// message 1 // message 1
"0xc1 0x07", // (HEADER added for this test) "0xc1 0x07", // (HEADER added for this test)
"0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00", "0xf2 0x48 0xcd 0xc9 0xc9 0x07 0x00",
@ -376,7 +376,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
ext.init(config, components); ext.init(config, components);
ext.setWebSocketCoreSession(newSession()); ext.setConfiguration(newSession());
// Setup capture of incoming frames // Setup capture of incoming frames
IncomingFramesCapture capture = new IncomingFramesCapture(); IncomingFramesCapture capture = new IncomingFramesCapture();
@ -450,7 +450,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
{ {
PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
ext.init(ExtensionConfig.parse("permessage-deflate"), components); ext.init(ExtensionConfig.parse("permessage-deflate"), components);
ext.setWebSocketCoreSession(newSession()); ext.setConfiguration(newSession());
// Setup capture of outgoing frames // Setup capture of outgoing frames
OutgoingFramesCapture capture = new OutgoingFramesCapture(); OutgoingFramesCapture capture = new OutgoingFramesCapture();
@ -497,7 +497,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
PerMessageDeflateExtension ext = new PerMessageDeflateExtension(); PerMessageDeflateExtension ext = new PerMessageDeflateExtension();
ExtensionConfig config = ExtensionConfig.parse("permessage-deflate"); ExtensionConfig config = ExtensionConfig.parse("permessage-deflate");
ext.init(config, components); ext.init(config, components);
ext.setWebSocketCoreSession(newSession()); ext.setConfiguration(newSession());
// Setup capture of incoming frames // Setup capture of incoming frames
OutgoingFramesCapture capture = new OutgoingFramesCapture(); OutgoingFramesCapture capture = new OutgoingFramesCapture();
@ -548,7 +548,7 @@ public class PerMessageDeflateExtensionTest extends AbstractExtensionTest
// Captured from Pywebsocket (r790) - "tora" sent 3 times. // 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 06 2a c9 2f 4a 04 00", // tora 1
"c1 05 2a 01 62 00 00", // tora 2 "c1 05 2a 01 62 00 00", // tora 2
"c1 04 02 61 00 00" // tora 3 "c1 04 02 61 00 00" // tora 3