use of io.vertx.core.http.impl.ws.WebSocketFrameInternal in project vert.x by eclipse.
the class VertxHttpHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof WebSocketFrameInternal) {
WebSocketFrameInternal frame = (WebSocketFrameInternal) msg;
ByteBuf buf = frame.getBinaryData();
if (buf != Unpooled.EMPTY_BUFFER) {
buf = safeBuffer(buf, ctx.alloc());
}
switch(frame.type()) {
case BINARY:
msg = new BinaryWebSocketFrame(frame.isFinal(), 0, buf);
break;
case TEXT:
msg = new TextWebSocketFrame(frame.isFinal(), 0, buf);
break;
case CLOSE:
msg = new CloseWebSocketFrame(true, 0, buf);
break;
case CONTINUATION:
msg = new ContinuationWebSocketFrame(frame.isFinal(), 0, buf);
break;
case PONG:
msg = new PongWebSocketFrame(buf);
break;
case PING:
msg = new PingWebSocketFrame(buf);
break;
default:
throw new IllegalStateException("Unsupported websocket msg " + msg);
}
}
ctx.write(msg, promise);
}
use of io.vertx.core.http.impl.ws.WebSocketFrameInternal in project vert.x by eclipse.
the class Http1xConnectionBase method handleWsFrame.
void handleWsFrame(WebSocketFrame msg) {
WebSocketFrameInternal frame = decodeFrame(msg);
WebSocketImplBase<?> w;
synchronized (this) {
w = webSocket;
}
if (w != null) {
w.context.execute(frame, w::handleFrame);
}
}
use of io.vertx.core.http.impl.ws.WebSocketFrameInternal in project vert.x by eclipse.
the class ServerConnection method processMessage.
private void processMessage(Object msg) {
if (msg instanceof HttpObject) {
HttpObject obj = (HttpObject) msg;
DecoderResult result = obj.decoderResult();
if (result.isFailure()) {
Throwable cause = result.cause();
if (cause instanceof TooLongFrameException) {
String causeMsg = cause.getMessage();
HttpVersion version;
if (msg instanceof HttpRequest) {
version = ((HttpRequest) msg).protocolVersion();
} else if (currentRequest != null) {
version = currentRequest.version() == io.vertx.core.http.HttpVersion.HTTP_1_0 ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
} else {
version = HttpVersion.HTTP_1_1;
}
HttpResponseStatus status = causeMsg.startsWith("An HTTP line is larger than") ? HttpResponseStatus.REQUEST_URI_TOO_LONG : HttpResponseStatus.BAD_REQUEST;
DefaultFullHttpResponse resp = new DefaultFullHttpResponse(version, status);
writeToChannel(resp);
}
// That will close the connection as it is considered as unusable
channel.pipeline().fireExceptionCaught(result.cause());
return;
}
if (msg instanceof HttpRequest) {
HttpRequest request = (HttpRequest) msg;
if (server.options().isHandle100ContinueAutomatically()) {
if (HttpHeaders.is100ContinueExpected(request)) {
write100Continue();
}
}
HttpServerResponseImpl resp = new HttpServerResponseImpl(vertx, this, request);
HttpServerRequestImpl req = new HttpServerRequestImpl(this, request, resp);
handleRequest(req, resp);
}
if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) msg;
if (chunk.content().isReadable()) {
Buffer buff = Buffer.buffer(chunk.content());
handleChunk(buff);
}
//TODO chunk trailers
if (msg instanceof LastHttpContent) {
if (!paused) {
handleEnd();
} else {
// Requeue
pending.add(LastHttpContent.EMPTY_LAST_CONTENT);
}
}
}
} else if (msg instanceof WebSocketFrameInternal) {
WebSocketFrameInternal frame = (WebSocketFrameInternal) msg;
handleWsFrame(frame);
}
checkNextTick();
}
use of io.vertx.core.http.impl.ws.WebSocketFrameInternal 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);
}
Aggregations