Home / Function/ finishHandshake() — netty Function Reference

finishHandshake() — netty Function Reference

Architecture documentation for the finishHandshake() function in WebSocketClientHandshaker.java from the netty codebase.

Function java ProtocolCodecs HTTP calls 3 called by 1

Entity Profile

Dependency Diagram

graph TD
  9794b10c_d406_d7de_aceb_ab5d06acccfc["finishHandshake()"]
  c066c80e_6ce3_75b8_2f7f_b6e3991df91b["WebSocketClientHandshaker"]
  9794b10c_d406_d7de_aceb_ab5d06acccfc -->|defined in| c066c80e_6ce3_75b8_2f7f_b6e3991df91b
  8d6f0453_9599_2392_6c36_119ec8ca7972["ChannelFuture()"]
  8d6f0453_9599_2392_6c36_119ec8ca7972 -->|calls| 9794b10c_d406_d7de_aceb_ab5d06acccfc
  6dade8c9_35fa_3e71_c61b_c7a75b3abe76["verify()"]
  9794b10c_d406_d7de_aceb_ab5d06acccfc -->|calls| 6dade8c9_35fa_3e71_c61b_c7a75b3abe76
  96862294_8f4d_b0d6_9cc2_3c46ee273d11["setActualSubprotocol()"]
  9794b10c_d406_d7de_aceb_ab5d06acccfc -->|calls| 96862294_8f4d_b0d6_9cc2_3c46ee273d11
  6965c28d_3a63_f0f2_f423_fae621978e5a["setHandshakeComplete()"]
  9794b10c_d406_d7de_aceb_ab5d06acccfc -->|calls| 6965c28d_3a63_f0f2_f423_fae621978e5a
  style 9794b10c_d406_d7de_aceb_ab5d06acccfc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java lines 358–444

    public final void finishHandshake(Channel channel, FullHttpResponse response) {
        verify(response);

        // Verify the subprotocol that we received from the server.
        // This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
        String receivedProtocol = response.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
        receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
        String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
        boolean protocolValid = false;

        if (expectedProtocol.isEmpty() && receivedProtocol == null) {
            // No subprotocol required and none received
            protocolValid = true;
            setActualSubprotocol(expectedSubprotocol); // null or "" - we echo what the user requested
        } else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
            // We require a subprotocol and received one -> verify it
            for (String protocol : expectedProtocol.split(",")) {
                if (protocol.trim().equals(receivedProtocol)) {
                    protocolValid = true;
                    setActualSubprotocol(receivedProtocol);
                    break;
                }
            }
        } // else mixed cases - which are all errors

        if (!protocolValid) {
            throw new WebSocketClientHandshakeException(String.format(
                    "Invalid subprotocol. Actual: %s. Expected one of: %s",
                    receivedProtocol, expectedSubprotocol), response);
        }

        setHandshakeComplete();

        final ChannelPipeline p = channel.pipeline();
        // Remove decompressor from pipeline if its in use
        HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
        if (decompressor != null) {
            p.remove(decompressor);
        }

        // Remove aggregator if present before
        HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
        if (aggregator != null) {
            p.remove(aggregator);
        }

        ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
        if (ctx == null) {
            ctx = p.context(HttpClientCodec.class);
            if (ctx == null) {
                throw new IllegalStateException("ChannelPipeline does not contain " +
                        "an HttpRequestEncoder or HttpClientCodec");
            }
            final HttpClientCodec codec =  (HttpClientCodec) ctx.handler();
            // Remove the encoder part of the codec as the user may start writing frames after this method returns.
            codec.removeOutboundHandler();

            p.addAfter(ctx.name(), "ws-decoder", newWebsocketDecoder());

            // Delay the removal of the decoder so the user can setup the pipeline if needed to handle
            // WebSocketFrame messages.
            // See https://github.com/netty/netty/issues/4533
            channel.eventLoop().execute(new Runnable() {
                @Override
                public void run() {
                    p.remove(codec);
                }
            });
        } else {
            if (p.get(HttpRequestEncoder.class) != null) {
                // Remove the encoder part of the codec as the user may start writing frames after this method returns.
                p.remove(HttpRequestEncoder.class);
            }
            final ChannelHandlerContext context = ctx;
            p.addAfter(context.name(), "ws-decoder", newWebsocketDecoder());

            // Delay the removal of the decoder so the user can setup the pipeline if needed to handle
            // WebSocketFrame messages.
            // See https://github.com/netty/netty/issues/4533
            channel.eventLoop().execute(new Runnable() {
                @Override

Subdomains

Called By

Frequently Asked Questions

What does finishHandshake() do?
finishHandshake() is a function in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java.
Where is finishHandshake() defined?
finishHandshake() is defined in codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java at line 358.
What does finishHandshake() call?
finishHandshake() calls 3 function(s): setActualSubprotocol, setHandshakeComplete, verify.
What calls finishHandshake()?
finishHandshake() is called by 1 function(s): ChannelFuture.

Analyze Your Own Codebase

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

Try Supermodel Free