Skip to content

Commit 046dc99

Browse files
authored
Merge pull request #46233 from mkouba/ws-next-max-frame-config
WebSockets Next: make it possible to configure max frame size
2 parents 660ae7a + 1f82857 commit 046dc99

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.quarkus.websockets.next.test.maxframesize;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import java.net.URI;
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.ExecutionException;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.TimeoutException;
10+
11+
import jakarta.inject.Inject;
12+
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
16+
import io.netty.handler.codec.http.websocketx.CorruptedWebSocketFrameException;
17+
import io.quarkus.test.QuarkusUnitTest;
18+
import io.quarkus.test.common.http.TestHTTPResource;
19+
import io.quarkus.websockets.next.OnError;
20+
import io.quarkus.websockets.next.OnTextMessage;
21+
import io.quarkus.websockets.next.WebSocket;
22+
import io.quarkus.websockets.next.test.utils.WSClient;
23+
import io.vertx.core.Vertx;
24+
import io.vertx.core.http.WebSocketFrame;
25+
26+
public class MaxFrameSizeTest {
27+
28+
@RegisterExtension
29+
public static final QuarkusUnitTest test = new QuarkusUnitTest()
30+
.withApplicationRoot(root -> {
31+
root.addClasses(Echo.class, WSClient.class);
32+
})
33+
.overrideConfigKey("quarkus.websockets-next.server.max-frame-size", "10");
34+
35+
@Inject
36+
Vertx vertx;
37+
38+
@TestHTTPResource("/echo")
39+
URI echoUri;
40+
41+
@Test
42+
void testMaxFrameSize() throws InterruptedException, ExecutionException, TimeoutException {
43+
WSClient client = WSClient.create(vertx).connect(echoUri);
44+
client.socket().writeFrame(WebSocketFrame.textFrame("foo".repeat(10), false));
45+
assertTrue(Echo.CORRUPTED_LATCH.await(5, TimeUnit.SECONDS));
46+
}
47+
48+
@WebSocket(path = "/echo")
49+
public static class Echo {
50+
51+
static final CountDownLatch CORRUPTED_LATCH = new CountDownLatch(1);
52+
53+
@OnTextMessage
54+
String process(String message) {
55+
return message;
56+
}
57+
58+
@OnError
59+
void onError(CorruptedWebSocketFrameException e) {
60+
// Note that connection is automatically closed when CorruptedWebSocketFrameException is thrown
61+
CORRUPTED_LATCH.countDown();
62+
}
63+
64+
}
65+
66+
}

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/utils/WSClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ public void close() {
164164
disconnect();
165165
}
166166

167+
public WebSocket socket() {
168+
return socket.get();
169+
}
170+
167171
public enum ReceiverMode {
168172
BINARY,
169173
TEXT,

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ protected WebSocketClientOptions populateClientOptions() {
153153
if (config.maxMessageSize().isPresent()) {
154154
clientOptions.setMaxMessageSize(config.maxMessageSize().getAsInt());
155155
}
156+
if (config.maxFrameSize().isPresent()) {
157+
clientOptions.setMaxFrameSize(config.maxFrameSize().getAsInt());
158+
}
156159

157160
Optional<TlsConfiguration> maybeTlsConfiguration = TlsConfiguration.from(tlsConfigurationRegistry,
158161
config.tlsConfigurationName());

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketHttpServerOptionsCustomizer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ private void customize(HttpServerOptions options) {
3434
if (config.maxMessageSize().isPresent()) {
3535
options.setMaxWebSocketMessageSize(config.maxMessageSize().getAsInt());
3636
}
37+
if (config.maxFrameSize().isPresent()) {
38+
options.setMaxWebSocketFrameSize(config.maxFrameSize().getAsInt());
39+
}
3740
}
3841

3942
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/config/WebSocketsClientRuntimeConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public interface WebSocketsClientRuntimeConfig {
3434
*/
3535
OptionalInt maxMessageSize();
3636

37+
/**
38+
* The maximum size of a frame in bytes. The default values is
39+
* {@value io.vertx.core.http.HttpClientOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZEX}.
40+
*/
41+
OptionalInt maxFrameSize();
42+
3743
/**
3844
* The interval after which, when set, the client sends a ping message to a connected server automatically.
3945
* <p>

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/config/WebSocketsServerRuntimeConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public interface WebSocketsServerRuntimeConfig {
4040
*/
4141
OptionalInt maxMessageSize();
4242

43+
/**
44+
* The maximum size of a frame in bytes. The default values is
45+
* {@value io.vertx.core.http.HttpServerOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZE}.
46+
*/
47+
OptionalInt maxFrameSize();
48+
4349
/**
4450
* The interval after which, when set, the server sends a ping message to a connected client automatically.
4551
* <p>

0 commit comments

Comments
 (0)