Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSockets Next: make it possible to configure max frame size #46233

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.websockets.next.test.maxframesize;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.netty.handler.codec.http.websocketx.CorruptedWebSocketFrameException;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.websockets.next.OnError;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.test.utils.WSClient;
import io.vertx.core.Vertx;
import io.vertx.core.http.WebSocketFrame;

public class MaxFrameSizeTest {

@RegisterExtension
public static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(root -> {
root.addClasses(Echo.class, WSClient.class);
})
.overrideConfigKey("quarkus.websockets-next.server.max-frame-size", "10");

@Inject
Vertx vertx;

@TestHTTPResource("/echo")
URI echoUri;

@Test
void testMaxFrameSize() throws InterruptedException, ExecutionException, TimeoutException {
WSClient client = WSClient.create(vertx).connect(echoUri);
client.socket().writeFrame(WebSocketFrame.textFrame("foo".repeat(10), false));
assertTrue(Echo.CORRUPTED_LATCH.await(5, TimeUnit.SECONDS));
}

@WebSocket(path = "/echo")
public static class Echo {

static final CountDownLatch CORRUPTED_LATCH = new CountDownLatch(1);

@OnTextMessage
String process(String message) {
return message;
}

@OnError
void onError(CorruptedWebSocketFrameException e) {
// Note that connection is automatically closed when CorruptedWebSocketFrameException is thrown
CORRUPTED_LATCH.countDown();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public void close() {
disconnect();
}

public WebSocket socket() {
return socket.get();
}

public enum ReceiverMode {
BINARY,
TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ protected WebSocketClientOptions populateClientOptions() {
if (config.maxMessageSize().isPresent()) {
clientOptions.setMaxMessageSize(config.maxMessageSize().getAsInt());
}
if (config.maxFrameSize().isPresent()) {
clientOptions.setMaxFrameSize(config.maxFrameSize().getAsInt());
}

Optional<TlsConfiguration> maybeTlsConfiguration = TlsConfiguration.from(tlsConfigurationRegistry,
config.tlsConfigurationName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ private void customize(HttpServerOptions options) {
if (config.maxMessageSize().isPresent()) {
options.setMaxWebSocketMessageSize(config.maxMessageSize().getAsInt());
}
if (config.maxFrameSize().isPresent()) {
options.setMaxWebSocketFrameSize(config.maxFrameSize().getAsInt());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public interface WebSocketsClientRuntimeConfig {
*/
OptionalInt maxMessageSize();

/**
* The maximum size of a frame in bytes. The default values is
* {@value io.vertx.core.http.HttpClientOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZEX}.
*/
OptionalInt maxFrameSize();

/**
* The interval after which, when set, the client sends a ping message to a connected server automatically.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public interface WebSocketsServerRuntimeConfig {
*/
OptionalInt maxMessageSize();

/**
* The maximum size of a frame in bytes. The default values is
* {@value io.vertx.core.http.HttpServerOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZE}.
*/
OptionalInt maxFrameSize();

/**
* The interval after which, when set, the server sends a ping message to a connected client automatically.
* <p>
Expand Down