Search in sources :

Example 1 with WebSocketFrameInternal

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);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) WebSocketFrameInternal(io.vertx.core.http.impl.ws.WebSocketFrameInternal)

Example 2 with WebSocketFrameInternal

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);
    }
}
Also used : WebSocketFrameInternal(io.vertx.core.http.impl.ws.WebSocketFrameInternal)

Example 3 with WebSocketFrameInternal

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();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) WebSocketFrameInternal(io.vertx.core.http.impl.ws.WebSocketFrameInternal) DecoderResult(io.netty.handler.codec.DecoderResult) HttpVersion(io.netty.handler.codec.http.HttpVersion)

Example 4 with WebSocketFrameInternal

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);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) HttpObject(io.netty.handler.codec.http.HttpObject) DecoderResult(io.netty.handler.codec.DecoderResult) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) WebSocketFrameInternal(io.vertx.core.http.impl.ws.WebSocketFrameInternal)

Aggregations

WebSocketFrameInternal (io.vertx.core.http.impl.ws.WebSocketFrameInternal)4 DecoderResult (io.netty.handler.codec.DecoderResult)2 Buffer (io.vertx.core.buffer.Buffer)2 ByteBuf (io.netty.buffer.ByteBuf)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 HttpContent (io.netty.handler.codec.http.HttpContent)1 HttpObject (io.netty.handler.codec.http.HttpObject)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 WebSocketFrameImpl (io.vertx.core.http.impl.ws.WebSocketFrameImpl)1