From c68a1b6066c7ef63b236c13679ac46941d57d0cf Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 14 May 2019 18:51:14 +1000 Subject: [PATCH] fix the websocket configuration problems shown by ClientConfigTest correctly detect when a message has exceeded the max size in the sink forward changes to the input buffer size to the connection in WSChannel Signed-off-by: Lachlan Roberts --- .../common/message/ByteArrayMessageSink.java | 17 ++++++++--------- .../common/message/ByteBufferMessageSink.java | 17 ++++++++--------- .../common/message/StringMessageSink.java | 15 +++++++-------- .../jetty/websocket/tests/EchoSocket.java | 9 +++++++++ .../jetty/websocket/tests/EventSocket.java | 11 +++++++++++ .../tests/client/ClientConfigTest.java | 2 +- .../core/internal/WebSocketChannel.java | 2 ++ 7 files changed, 46 insertions(+), 27 deletions(-) diff --git a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteArrayMessageSink.java b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteArrayMessageSink.java index 3f7e5831354..e7b9691b16e 100644 --- a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteArrayMessageSink.java +++ b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteArrayMessageSink.java @@ -18,13 +18,6 @@ package org.eclipse.jetty.websocket.common.message; -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.websocket.common.AbstractMessageSink; -import org.eclipse.jetty.websocket.common.invoke.InvalidSignatureException; -import org.eclipse.jetty.websocket.core.Frame; -import org.eclipse.jetty.websocket.core.MessageTooLargeException; - import java.io.ByteArrayOutputStream; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodType; @@ -32,6 +25,13 @@ import java.nio.ByteBuffer; import java.util.Objects; import java.util.concurrent.Executor; +import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.websocket.common.AbstractMessageSink; +import org.eclipse.jetty.websocket.common.invoke.InvalidSignatureException; +import org.eclipse.jetty.websocket.core.Frame; +import org.eclipse.jetty.websocket.core.MessageTooLargeException; + public class ByteArrayMessageSink extends AbstractMessageSink { private static final byte EMPTY_BUFFER[] = new byte[0]; @@ -63,10 +63,9 @@ public class ByteArrayMessageSink extends AbstractMessageSink if (frame.hasPayload()) { ByteBuffer payload = frame.getPayload(); - int nextSize = size + payload.remaining(); + size = size + payload.remaining(); if (maxMessageSize > 0 && size > maxMessageSize) throw new MessageTooLargeException("Message size [" + size + "] exceeds maximum size [" + maxMessageSize + "]"); - size = nextSize; if (out == null) out = new ByteArrayOutputStream(BUFFER_SIZE); diff --git a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteBufferMessageSink.java b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteBufferMessageSink.java index 5d45adbb378..ae076bd536b 100644 --- a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteBufferMessageSink.java +++ b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/ByteBufferMessageSink.java @@ -18,13 +18,6 @@ package org.eclipse.jetty.websocket.common.message; -import org.eclipse.jetty.util.BufferUtil; -import org.eclipse.jetty.util.Callback; -import org.eclipse.jetty.websocket.common.AbstractMessageSink; -import org.eclipse.jetty.websocket.common.invoke.InvalidSignatureException; -import org.eclipse.jetty.websocket.core.Frame; -import org.eclipse.jetty.websocket.core.MessageTooLargeException; - import java.io.ByteArrayOutputStream; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodType; @@ -32,6 +25,13 @@ import java.nio.ByteBuffer; import java.util.Objects; import java.util.concurrent.Executor; +import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.websocket.common.AbstractMessageSink; +import org.eclipse.jetty.websocket.common.invoke.InvalidSignatureException; +import org.eclipse.jetty.websocket.core.Frame; +import org.eclipse.jetty.websocket.core.MessageTooLargeException; + public class ByteBufferMessageSink extends AbstractMessageSink { private static final int BUFFER_SIZE = 65535; @@ -62,10 +62,9 @@ public class ByteBufferMessageSink extends AbstractMessageSink if (frame.hasPayload()) { ByteBuffer payload = frame.getPayload(); - int nextSize = size + payload.remaining(); + size = size + payload.remaining(); if (maxMessageSize > 0 && size > maxMessageSize) throw new MessageTooLargeException("Message size [" + size + "] exceeds maximum size [" + maxMessageSize + "]"); - size = nextSize; if (out == null) out = new ByteArrayOutputStream(BUFFER_SIZE); diff --git a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/StringMessageSink.java b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/StringMessageSink.java index 9a205ade846..58504ac37fb 100644 --- a/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/StringMessageSink.java +++ b/jetty-websocket/jetty-websocket-common/src/main/java/org/eclipse/jetty/websocket/common/message/StringMessageSink.java @@ -18,6 +18,12 @@ package org.eclipse.jetty.websocket.common.message; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodType; +import java.nio.ByteBuffer; +import java.util.Objects; +import java.util.concurrent.Executor; + import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Utf8StringBuilder; @@ -28,12 +34,6 @@ import org.eclipse.jetty.websocket.common.invoke.InvalidSignatureException; import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.core.MessageTooLargeException; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodType; -import java.nio.ByteBuffer; -import java.util.Objects; -import java.util.concurrent.Executor; - public class StringMessageSink extends AbstractMessageSink { private static final Logger LOG = Log.getLogger(StringMessageSink.class); @@ -66,10 +66,9 @@ public class StringMessageSink extends AbstractMessageSink if (frame.hasPayload()) { ByteBuffer payload = frame.getPayload(); - int nextSize = size + payload.remaining(); + size = size + payload.remaining(); if (maxMessageSize > 0 && size > maxMessageSize) throw new MessageTooLargeException("Message size [" + size + "] exceeds maximum size [" + maxMessageSize + "]"); - size = nextSize; if (utf == null) utf = new Utf8StringBuilder(1024); diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java index a9b8d404f3f..602951959be 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java @@ -19,7 +19,9 @@ package org.eclipse.jetty.websocket.tests; import java.io.IOException; +import java.nio.ByteBuffer; +import org.eclipse.jetty.websocket.api.WriteCallback; import org.eclipse.jetty.websocket.api.annotations.WebSocket; @WebSocket @@ -31,4 +33,11 @@ public class EchoSocket extends EventSocket super.onMessage(message); session.getRemote().sendString(message); } + + @Override + public void onMessage(byte[] buf, int offset, int len) + { + super.onMessage(buf, offset, len); + session.getRemote().sendBytes(ByteBuffer.wrap(buf, offset, len), WriteCallback.NOOP); + } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java index f9921c2cae7..22c52d82ac7 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.websocket.tests; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; @@ -42,6 +43,7 @@ public class EventSocket private String behavior; public BlockingQueue messageQueue = new BlockingArrayQueue<>(); + public BlockingQueue binaryMessageQueue = new BlockingArrayQueue<>(); public volatile int statusCode = StatusCode.UNDEFINED; public volatile String reason; public volatile Throwable error = null; @@ -66,6 +68,15 @@ public class EventSocket messageQueue.offer(message); } + + @OnWebSocketMessage + public void onMessage(byte buf[], int offset, int len) + { + ByteBuffer message = ByteBuffer.wrap(buf, offset, len); + LOG.info("{} onMessage(): {}", toString(), message); + binaryMessageQueue.offer(message); + } + @OnWebSocketClose public void onClose(int statusCode, String reason) { diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientConfigTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientConfigTest.java index 61cd8cdcf0a..7a6dafc7e93 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientConfigTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientConfigTest.java @@ -117,7 +117,7 @@ public class ClientConfigTest @Test public void testMaxBinaryMessageSize() throws Exception { - client.setMaxTextMessageSize(maxMessageSize); + client.setMaxBinaryMessageSize(maxMessageSize); URI uri = URI.create("ws://localhost:8080/"); EventSocket clientEndpoint = new EventSocket(); diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketChannel.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketChannel.java index d44b5e1404f..73d1ffb23bd 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketChannel.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/core/internal/WebSocketChannel.java @@ -637,6 +637,8 @@ public class WebSocketChannel implements IncomingFrames, FrameHandler.CoreSessio public void setInputBufferSize(int inputBufferSize) { this.inputBufferSize = inputBufferSize; + if (connection != null) + connection.setInputBufferSize(inputBufferSize); } @Override