Skip to content

Commit

Permalink
Have HttpServerRequest expose canUpgradeToWebSocket as this sounds le…
Browse files Browse the repository at this point in the history
…gitimate for most users to check this before perfoming a WebSocket upgrade on the server
  • Loading branch information
vietj committed Jul 2, 2024
1 parent ef0aaec commit 77a0d21
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.impl.HttpUtils;
import io.vertx.core.net.HostAndPort;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.SocketAddress;
Expand Down Expand Up @@ -377,6 +378,14 @@ default int streamId() {
return -1;
}

/**
* @return whether this request can be upgraded to a WebSocket, implying it uses HTTP/1.x and presents
* the correct characteristics for a proper upgrade.
*/
default boolean canUpgradeToWebSocket() {
return HttpUtils.canUpgradeToWebSocket(this);
}

/**
* Upgrade the connection of the current request to a WebSocket.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5560,7 +5560,7 @@ public void testCannotUpgradeToWebSocketIfUpgradeDoesNotContainWebsocket() throw
private void doTestCanUpgradeToWebSocket(UnaryOperator<RequestOptions> config, boolean shouldSucceed) throws Exception {
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
if (HttpUtils.canUpgradeToWebSocket(req)) {
if (req.canUpgradeToWebSocket()) {
resp.headers()
.set(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE)
.set(HttpHeaders.UPGRADE, HttpHeaders.WEBSOCKET);
Expand Down

0 comments on commit 77a0d21

Please sign in to comment.