Skip to content

Commit

Permalink
Handle frames of payload greater than 125 bytes in ClientWsConnection…
Browse files Browse the repository at this point in the history
…. New test that sends long text frames. (#8134)
  • Loading branch information
spericas authored Dec 11, 2023
1 parent 0c4e5a0 commit ca2ee43
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ private ClientWsConnection send(ClientWsFrame frame) {
opCodeFull |= opCode.code();
sendBuffer.write(opCodeFull);

long payloadLength = frame.payloadLength();
if (frame.payloadLength() < 126) {
// this is a masked frame (all client frames MUST be masked)
payloadLength = payloadLength | 0b10000000;
sendBuffer.write((int) payloadLength);
// TODO finish other options (payload longer than 126 bytes)
long length = frame.payloadLength();
if (length < 126) {
sendBuffer.write((int) length | 0b10000000);
} else if (length < 1 << 16) {
sendBuffer.write(126 | 0b10000000);
sendBuffer.write((int) (length >>> 8));
sendBuffer.write((int) (length & 0xFF));
} else {
sendBuffer.write(127 | 0b10000000);
for (int i = 56; i >= 0; i -= 8){
sendBuffer.write((int) (length >>> i) & 0xFF);
}
}

// write masking key
Expand Down
5 changes: 5 additions & 0 deletions webserver/tests/websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>helidon-webserver-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5-websocket</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.webserver.tests.websocket;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import io.helidon.webclient.websocket.WsClient;
import io.helidon.webserver.Router;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;
import io.helidon.webserver.testing.junit5.SetUpServer;
import io.helidon.webserver.websocket.WsConfig;
import io.helidon.webserver.websocket.WsRouting;
import io.helidon.websocket.WsCloseCodes;
import io.helidon.websocket.WsListener;
import io.helidon.websocket.WsSession;
import org.junit.jupiter.api.Test;

import static io.helidon.webserver.tests.websocket.WebSocketTest.randomString;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@ServerTest
public class WebSocketClientTest {

private final WsClient wsClient;

private static final String[] text = {
randomString(100),
randomString(1000),
randomString(10000),
randomString(100000)
};

WebSocketClientTest(WsClient wsClient) {
this.wsClient = wsClient;
}

@SetUpServer
public static void setup(WebServerConfig.Builder builder) {
builder.addProtocol(
WsConfig.builder()
.maxFrameLength(100000) // overrides application.yaml
.build());
}

@SetUpRoute
static void router(Router.RouterBuilder<?> router) {
router.addRouting(WsRouting.builder().endpoint("/echo", new EchoService()));
}

/**
* Tests sending long text messages using Helidon's WS client.
*/
@Test
void testLongTextMessages() throws InterruptedException {
Set<String> messages = new HashSet<>();
CountDownLatch messageLatch = new CountDownLatch(text.length);

wsClient.connect("/echo", new WsListener() {
@Override
public void onMessage(WsSession session, String text, boolean last) {
messages.add(text);
messageLatch.countDown();
if (messageLatch.getCount() == 0) {
session.close(WsCloseCodes.NORMAL_CLOSE, "Bye!");
}
}

@Override
public void onOpen(WsSession session) {
for (String s : text) {
session.send(s, false);
}
}
});

boolean await = messageLatch.await(10, TimeUnit.SECONDS);
assertThat(await, is(true));
assertThat(messages, hasItems(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Results results() throws ExecutionException, InterruptedException, TimeoutExcept
}
}

private static String randomString(int length) {
static String randomString(int length) {
int leftLimit = 97; // letter 'a'
int rightLimit = 122; // letter 'z'
return new Random().ints(leftLimit, rightLimit + 1)
Expand Down

0 comments on commit ca2ee43

Please sign in to comment.