use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class VertxHttpHandler method safeObject.
@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf buf = content.content();
if (buf != Unpooled.EMPTY_BUFFER && buf.isDirect()) {
ByteBuf newBuf = safeBuffer(content, allocator);
if (msg instanceof LastHttpContent) {
LastHttpContent last = (LastHttpContent) msg;
return new AssembledLastHttpContent(newBuf, last.trailingHeaders(), last.getDecoderResult());
} else {
return new DefaultHttpContent(newBuf);
}
}
} else if (msg instanceof WebSocketFrame) {
ByteBuf payload = safeBuffer((WebSocketFrame) msg, allocator);
boolean isFinal = ((WebSocketFrame) msg).isFinalFragment();
FrameType frameType;
if (msg instanceof BinaryWebSocketFrame) {
frameType = FrameType.BINARY;
} else if (msg instanceof CloseWebSocketFrame) {
frameType = FrameType.CLOSE;
} else if (msg instanceof PingWebSocketFrame) {
frameType = FrameType.PING;
} else if (msg instanceof PongWebSocketFrame) {
frameType = FrameType.PONG;
} else if (msg instanceof TextWebSocketFrame) {
frameType = FrameType.TEXT;
} else if (msg instanceof ContinuationWebSocketFrame) {
frameType = FrameType.CONTINUATION;
} else {
throw new IllegalStateException("Unsupported websocket msg " + msg);
}
return new WebSocketFrameImpl(frameType, payload, isFinal);
}
return msg;
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class WebSocketImplBase method writeTextFrameInternal.
private void writeTextFrameInternal(String str) {
WebSocketFrame frame = new WebSocketFrameImpl(str);
writeFrame(frame);
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class ClientHandler method doMessageReceived.
@Override
protected void doMessageReceived(ClientConnection conn, ChannelHandlerContext ctx, Object msg) {
if (conn == null) {
return;
}
if (msg instanceof HttpObject) {
HttpObject obj = (HttpObject) msg;
DecoderResult result = obj.decoderResult();
if (result.isFailure()) {
// Close the connection as Netty's HttpResponseDecoder will not try further processing
// see https://github.com/netty/netty/issues/3362
conn.handleException(result.cause());
conn.close();
return;
}
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) obj;
conn.handleResponse(response);
return;
}
if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) obj;
if (chunk.content().isReadable()) {
Buffer buff = Buffer.buffer(chunk.content().slice());
conn.handleResponseChunk(buff);
}
if (chunk instanceof LastHttpContent) {
conn.handleResponseEnd((LastHttpContent) chunk);
}
return;
}
} else if (msg instanceof WebSocketFrameInternal) {
WebSocketFrameInternal frame = (WebSocketFrameInternal) msg;
switch(frame.type()) {
case BINARY:
case CONTINUATION:
case TEXT:
conn.handleWsFrame(frame);
break;
case PING:
// Echo back the content of the PING frame as PONG frame as specified in RFC 6455 Section 5.5.2
ctx.writeAndFlush(new WebSocketFrameImpl(FrameType.PONG, frame.getBinaryData()));
break;
case PONG:
// Just ignore it
break;
case CLOSE:
if (!closeFrameSent) {
// Echo back close frame and close the connection once it was written.
// This is specified in the WebSockets RFC 6455 Section 5.4.1
ctx.writeAndFlush(frame).addListener(ChannelFutureListener.CLOSE);
closeFrameSent = true;
}
break;
default:
throw new IllegalStateException("Invalid type: " + frame.type());
}
return;
}
throw new IllegalStateException("Invalid object " + msg);
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class Http1xConnectionBase method decodeFrame.
private WebSocketFrameInternal decodeFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame msg) {
ByteBuf payload = safeBuffer(msg.content());
boolean isFinal = msg.isFinalFragment();
WebSocketFrameType frameType;
if (msg instanceof BinaryWebSocketFrame) {
frameType = WebSocketFrameType.BINARY;
} else if (msg instanceof CloseWebSocketFrame) {
frameType = WebSocketFrameType.CLOSE;
} else if (msg instanceof PingWebSocketFrame) {
frameType = WebSocketFrameType.PING;
} else if (msg instanceof PongWebSocketFrame) {
frameType = WebSocketFrameType.PONG;
} else if (msg instanceof TextWebSocketFrame) {
frameType = WebSocketFrameType.TEXT;
} else if (msg instanceof ContinuationWebSocketFrame) {
frameType = WebSocketFrameType.CONTINUATION;
} else {
throw new IllegalStateException("Unsupported WebSocket msg " + msg);
}
return new WebSocketFrameImpl(frameType, payload, isFinal);
}
use of io.vertx.core.http.impl.ws.WebSocketFrameImpl in project vert.x by eclipse.
the class WebSocketTest method testClientWebSocketReceivePongExceedsMaxFrameSize.
@Test
public void testClientWebSocketReceivePongExceedsMaxFrameSize() {
String pingBody = randomAlphaString(113);
Integer maxFrameSize = 64;
Buffer ping1 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(0, maxFrameSize));
Buffer ping2 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(maxFrameSize, pingBody.length()));
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
try {
ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PONG, ping1.copy().getByteBuf(), false));
ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PONG, ping2.copy().getByteBuf(), true));
} catch (Throwable t) {
fail(t);
}
}).listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
List<Buffer> pongs = new ArrayList<>();
ws.pongHandler(pong -> {
pongs.add(pong);
if (pongs.size() == 2) {
assertEquals(pongs, Arrays.asList(ping1, ping2));
testComplete();
}
});
}));
}));
await();
}
Aggregations