Home / Class/ WebSocketServerProtocolHandshakeHandler Class — netty Architecture

WebSocketServerProtocolHandshakeHandler Class — netty Architecture

Architecture documentation for the WebSocketServerProtocolHandshakeHandler class in WebSocketServerProtocolHandshakeHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668["WebSocketServerProtocolHandshakeHandler"]
  e017cbfd_d53a_0bf2_32a0_9894bff8bc0e["WebSocketServerProtocolHandshakeHandler.java"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|defined in| e017cbfd_d53a_0bf2_32a0_9894bff8bc0e
  fd2c5cb7_055f_3d37_e7cf_da8458669df1["WebSocketServerProtocolHandshakeHandler()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| fd2c5cb7_055f_3d37_e7cf_da8458669df1
  4677a692_7981_ab6b_c134_7f6a010bab6a["handlerAdded()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| 4677a692_7981_ab6b_c134_7f6a010bab6a
  da9f8d7e_bece_8a5d_bade_9cfafa7bf590["channelRead()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| da9f8d7e_bece_8a5d_bade_9cfafa7bf590
  cd048d62_9bdc_867a_c433_c9a6c433bed0["isWebSocketPath()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| cd048d62_9bdc_867a_c433_c9a6c433bed0
  fe09eb27_db78_5286_5fac_98f9598fa06b["checkNextUri()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| fe09eb27_db78_5286_5fac_98f9598fa06b
  fdfa0574_1ebf_0a28_2d09_a6a40759b030["String()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| fdfa0574_1ebf_0a28_2d09_a6a40759b030
  8fbbb8f9_4fbe_de99_c718_f954c3700023["applyHandshakeTimeout()"]
  7859e8c2_d3fe_97c8_edc5_e54ff6dd5668 -->|method| 8fbbb8f9_4fbe_de99_c718_f954c3700023

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java lines 40–162

class WebSocketServerProtocolHandshakeHandler extends ChannelInboundHandlerAdapter {

    private final WebSocketServerProtocolConfig serverConfig;
    private ChannelHandlerContext ctx;
    private ChannelPromise handshakePromise;
    private boolean isWebSocketPath;

    WebSocketServerProtocolHandshakeHandler(WebSocketServerProtocolConfig serverConfig) {
        this.serverConfig = checkNotNull(serverConfig, "serverConfig");
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) {
        this.ctx = ctx;
        handshakePromise = ctx.newPromise();
    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
        final HttpObject httpObject = (HttpObject) msg;

        if (httpObject instanceof HttpRequest) {
            final HttpRequest req = (HttpRequest) httpObject;
            isWebSocketPath = isWebSocketPath(req);
            if (!isWebSocketPath) {
                ctx.fireChannelRead(msg);
                return;
            }

            try {
                final WebSocketServerHandshaker handshaker = WebSocketServerHandshakerFactory.resolveHandshaker(
                        req,
                        getWebSocketLocation(ctx.pipeline(), req, serverConfig.websocketPath()),
                        serverConfig.subprotocols(), serverConfig.decoderConfig());
                final ChannelPromise localHandshakePromise = handshakePromise;
                if (handshaker == null) {
                    WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
                } else {
                    // Ensure we set the handshaker and replace this handler before we
                    // trigger the actual handshake. Otherwise we may receive websocket bytes in this handler
                    // before we had a chance to replace it.
                    //
                    // See https://github.com/netty/netty/issues/9471.
                    WebSocketServerProtocolHandler.setHandshaker(ctx.channel(), handshaker);
                    ctx.pipeline().remove(this);

                    final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req);
                    handshakeFuture.addListener(future -> {
                        if (!future.isSuccess()) {
                            localHandshakePromise.tryFailure(future.cause());
                            ctx.fireExceptionCaught(future.cause());
                        } else {
                            localHandshakePromise.trySuccess();
                            // Kept for compatibility
                            ctx.fireUserEventTriggered(
                                    ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
                            ctx.fireUserEventTriggered(
                                    new WebSocketServerProtocolHandler.HandshakeComplete(
                                            req.uri(), req.headers(), handshaker.selectedSubprotocol()));
                        }
                    });
                    applyHandshakeTimeout();
                }
            } finally {
                ReferenceCountUtil.release(req);
            }
        } else if (!isWebSocketPath) {
            ctx.fireChannelRead(msg);
        } else {
            ReferenceCountUtil.release(msg);
        }
    }

    private boolean isWebSocketPath(HttpRequest req) {
        String websocketPath = serverConfig.websocketPath();
        String uri = req.uri();
        return serverConfig.checkStartsWith()
                ? uri.startsWith(websocketPath) && ("/".equals(websocketPath) || checkNextUri(uri, websocketPath))
                : uri.equals(websocketPath);
    }

Frequently Asked Questions

What is the WebSocketServerProtocolHandshakeHandler class?
WebSocketServerProtocolHandshakeHandler is a class in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java.
Where is WebSocketServerProtocolHandshakeHandler defined?
WebSocketServerProtocolHandshakeHandler is defined in codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java at line 40.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free