Skip to content

Commit 5385a15

Browse files
committed
[grid] Modifying ws upgrade to handle VNC client.
1 parent 38c2b3f commit 5385a15

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

java/server/src/org/openqa/selenium/grid/router/httpd/RouterServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.auto.service.AutoService;
2121
import com.google.common.collect.ImmutableMap;
2222
import com.google.common.collect.ImmutableSet;
23+
2324
import org.openqa.selenium.BuildInfo;
2425
import org.openqa.selenium.UsernameAndPassword;
2526
import org.openqa.selenium.cli.CliCommand;
@@ -48,7 +49,6 @@
4849
import org.openqa.selenium.grid.web.GridUiRoute;
4950
import org.openqa.selenium.internal.Require;
5051
import org.openqa.selenium.remote.http.HttpClient;
51-
import org.openqa.selenium.remote.http.HttpHandler;
5252
import org.openqa.selenium.remote.http.HttpResponse;
5353
import org.openqa.selenium.remote.http.Routable;
5454
import org.openqa.selenium.remote.http.Route;

java/server/src/org/openqa/selenium/netty/server/WebSocketUpgradeHandler.java

+30-31
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.openqa.selenium.netty.server;
1919

20+
import org.openqa.selenium.internal.Require;
21+
import org.openqa.selenium.remote.http.Message;
22+
2023
import io.netty.buffer.ByteBuf;
2124
import io.netty.buffer.Unpooled;
2225
import io.netty.channel.ChannelFuture;
@@ -38,9 +41,6 @@
3841
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
3942
import io.netty.util.AttributeKey;
4043

41-
import org.openqa.selenium.internal.Require;
42-
import org.openqa.selenium.remote.http.Message;
43-
4444
import java.util.Optional;
4545
import java.util.function.BiFunction;
4646
import java.util.function.Consumer;
@@ -69,6 +69,27 @@ public WebSocketUpgradeHandler(
6969
this.factory = Require.nonNull("Factory", factory);
7070
}
7171

72+
private static void sendHttpResponse(
73+
ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) {
74+
// Generate an error page if response status code is not OK (200).
75+
if (res.status().code() != 200) {
76+
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), UTF_8);
77+
res.content().writeBytes(buf);
78+
buf.release();
79+
setContentLength(res, res.content().readableBytes());
80+
}
81+
82+
// Send the response and close the connection if necessary.
83+
ChannelFuture f = ctx.channel().writeAndFlush(res);
84+
if (!isKeepAlive(req) || res.status().code() != 200) {
85+
f.addListener(ChannelFutureListener.CLOSE);
86+
}
87+
}
88+
89+
private static String getWebSocketLocation(HttpRequest req) {
90+
return "ws://" + req.headers().get(HttpHeaderNames.HOST);
91+
}
92+
7293
@Override
7394
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
7495
if (msg instanceof HttpRequest) {
@@ -100,26 +121,25 @@ private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) {
100121
}
101122

102123
// Only handle the initial HTTP upgrade request
103-
if (!(req.headers().contains("Connection", "upgrade", true) &&
104-
req.headers().contains("Sec-WebSocket-Version"))) {
124+
if (!(req.headers().containsValue("Connection", "upgrade", true) &&
125+
req.headers().contains("Sec-WebSocket-Version"))) {
105126
ctx.fireChannelRead(req);
106127
return;
107128
}
108129

109130
// Is this something we should try and handle?
110131
Optional<Consumer<Message>> maybeHandler = factory.apply(
111132
req.uri(),
112-
msg -> {
113-
ctx.channel().writeAndFlush(Require.nonNull("Message to send", msg));
114-
});
133+
msg -> ctx.channel().writeAndFlush(Require.nonNull("Message to send", msg)));
115134
if (!maybeHandler.isPresent()) {
116-
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, ctx.alloc().buffer(0)));
135+
sendHttpResponse(ctx, req,
136+
new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, ctx.alloc().buffer(0)));
117137
return;
118138
}
119139

120140
// Handshake
121141
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
122-
getWebSocketLocation(req), null, false, Integer.MAX_VALUE);
142+
getWebSocketLocation(req), null, true, Integer.MAX_VALUE);
123143
handshaker = wsFactory.newHandshaker(req);
124144
if (handshaker == null) {
125145
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
@@ -155,29 +175,8 @@ private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame fram
155175
}
156176
}
157177

158-
private static void sendHttpResponse(
159-
ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) {
160-
// Generate an error page if response status code is not OK (200).
161-
if (res.status().code() != 200) {
162-
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), UTF_8);
163-
res.content().writeBytes(buf);
164-
buf.release();
165-
setContentLength(res, res.content().readableBytes());
166-
}
167-
168-
// Send the response and close the connection if necessary.
169-
ChannelFuture f = ctx.channel().writeAndFlush(res);
170-
if (!isKeepAlive(req) || res.status().code() != 200) {
171-
f.addListener(ChannelFutureListener.CLOSE);
172-
}
173-
}
174-
175178
@Override
176179
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
177180
ctx.close();
178181
}
179-
180-
private static String getWebSocketLocation(HttpRequest req) {
181-
return "ws://" + req.headers().get(HttpHeaderNames.HOST);
182-
}
183182
}

0 commit comments

Comments
 (0)