use of io.netty.handler.codec.http.websocketx.WebSocketHandshakeException in project reactor-netty by reactor.
the class HttpClientWSOperations method onInboundNext.
@Override
@SuppressWarnings("unchecked")
public void onInboundNext(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof FullHttpResponse) {
started = true;
channel().pipeline().remove(HttpObjectAggregator.class);
FullHttpResponse response = (FullHttpResponse) msg;
setNettyResponse(response);
if (checkResponseCode(response)) {
try {
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(channel(), response);
}
} catch (WebSocketHandshakeException wshe) {
if (serverError) {
onInboundError(wshe);
return;
}
} finally {
// Release unused content (101 status)
response.content().release();
}
parentContext().fireContextActive(this);
handshakerResult.trySuccess();
}
return;
}
if (msg instanceof PingWebSocketFrame) {
channel().writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content()));
ctx.read();
return;
}
if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) msg).isFinalFragment()) {
if (log.isDebugEnabled()) {
log.debug("CloseWebSocketFrame detected. Closing Websocket");
}
CloseWebSocketFrame close = (CloseWebSocketFrame) msg;
sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content()));
} else {
super.onInboundNext(ctx, msg);
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketHandshakeException in project vert.x by eclipse.
the class WebsocketTest method doTestClientWebsocketConnectionCloseOnBadResponse.
private void doTestClientWebsocketConnectionCloseOnBadResponse(boolean keepAliveInOptions) throws Throwable {
final Exception serverGotCloseException = new Exception();
netServer = vertx.createNetServer().connectHandler(sock -> {
final Buffer fullReq = Buffer.buffer(230);
sock.handler(b -> {
fullReq.appendBuffer(b);
String reqPart = b.toString();
if (fullReq.toString().contains("\r\n\r\n")) {
try {
String content = "0123456789";
content = content + content;
content = content + content + content + content + content;
String resp = "HTTP/1.1 200 OK\r\n";
if (keepAliveInOptions) {
resp += "Connection: close\r\n";
}
resp += "Content-Length: 100\r\n\r\n" + content;
sock.write(Buffer.buffer(resp.getBytes("ASCII")));
} catch (UnsupportedEncodingException e) {
addResult(e);
}
}
});
sock.closeHandler(v -> {
addResult(serverGotCloseException);
});
}).listen(ar -> {
if (ar.failed()) {
addResult(ar.cause());
return;
}
NetServer server = ar.result();
int port = server.actualPort();
HttpClientOptions opts = new HttpClientOptions().setKeepAlive(keepAliveInOptions);
client.close();
client = vertx.createHttpClient(opts).websocket(port, "localhost", "/", ws -> {
addResult(new AssertionError("Websocket unexpectedly connected"));
ws.close();
}, t -> {
addResult(t);
});
});
boolean serverGotClose = false;
boolean clientGotCorrectException = false;
while (!serverGotClose || !clientGotCorrectException) {
Throwable result = resultQueue.poll(20, TimeUnit.SECONDS);
if (result == null) {
throw new AssertionError("Timed out waiting for expected state, current: serverGotClose = " + serverGotClose + ", clientGotCorrectException = " + clientGotCorrectException);
} else if (result == serverGotCloseException) {
serverGotClose = true;
} else if (result instanceof WebSocketHandshakeException && result.getMessage().equals("Websocket connection attempt returned HTTP status code 200")) {
clientGotCorrectException = true;
} else {
throw result;
}
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketHandshakeException in project netty by netty.
the class WebSocketClientHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel ch = ctx.channel();
if (!handshaker.isHandshakeComplete()) {
try {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
System.out.println("WebSocket Client connected!");
handshakeFuture.setSuccess();
} catch (WebSocketHandshakeException e) {
System.out.println("WebSocket Client failed to connect");
handshakeFuture.setFailure(e);
}
return;
}
if (msg instanceof FullHttpResponse) {
FullHttpResponse response = (FullHttpResponse) msg;
throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
}
WebSocketFrame frame = (WebSocketFrame) msg;
if (frame instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
System.out.println("WebSocket Client received message: " + textFrame.text());
} else if (frame instanceof PongWebSocketFrame) {
System.out.println("WebSocket Client received pong");
} else if (frame instanceof CloseWebSocketFrame) {
System.out.println("WebSocket Client received closing");
ch.close();
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketHandshakeException in project vert.x by eclipse.
the class WebSocketHandshakeInboundHandler method handshakeComplete.
private Future<HeadersAdaptor> handshakeComplete(FullHttpResponse response) {
int sc = response.status().code();
if (sc != 101) {
String msg = "WebSocket upgrade failure: " + sc;
ByteBuf content = response.content();
if (content != null && content.readableBytes() > 0) {
msg += " (" + content.toString(StandardCharsets.UTF_8) + ")";
}
UpgradeRejectedException failure = new UpgradeRejectedException(msg, sc);
return Future.failedFuture(failure);
} else {
try {
handshaker.finishHandshake(chctx.channel(), response);
return Future.succeededFuture(new HeadersAdaptor(response.headers()));
} catch (WebSocketHandshakeException e) {
return Future.failedFuture(e);
}
}
}
Aggregations