Home / Function/ upgrade() — netty Function Reference

upgrade() — netty Function Reference

Architecture documentation for the upgrade() function in HttpServerUpgradeHandler.java from the netty codebase.

Function java ProtocolCodecs HTTP calls 3 called by 1

Entity Profile

Dependency Diagram

graph TD
  149aae54_88df_d06f_7ef6_3a140cffc9ad["upgrade()"]
  9c8c1840_4e39_0876_abe2_bb3ce8067466["HttpServerUpgradeHandler"]
  149aae54_88df_d06f_7ef6_3a140cffc9ad -->|defined in| 9c8c1840_4e39_0876_abe2_bb3ce8067466
  71a1fb51_f416_1d4b_7593_4149e6ec5c5d["decode()"]
  71a1fb51_f416_1d4b_7593_4149e6ec5c5d -->|calls| 149aae54_88df_d06f_7ef6_3a140cffc9ad
  c68ee1f2_acba_ff86_bef6_328b183214dd["splitHeader()"]
  149aae54_88df_d06f_7ef6_3a140cffc9ad -->|calls| c68ee1f2_acba_ff86_bef6_328b183214dd
  68ea0c7f_e92e_7abc_5a17_26f630b8b5f1["UpgradeEvent()"]
  149aae54_88df_d06f_7ef6_3a140cffc9ad -->|calls| 68ea0c7f_e92e_7abc_5a17_26f630b8b5f1
  59817d90_61ce_6926_f6dc_85381927acef["release()"]
  149aae54_88df_d06f_7ef6_3a140cffc9ad -->|calls| 59817d90_61ce_6926_f6dc_85381927acef
  style 149aae54_88df_d06f_7ef6_3a140cffc9ad fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java lines 370–455

    private boolean upgrade(final ChannelHandlerContext ctx, final FullHttpRequest request) {
        // Select the best protocol based on those requested in the UPGRADE header.
        final List<CharSequence> requestedProtocols = splitHeader(request.headers().get(HttpHeaderNames.UPGRADE));
        final int numRequestedProtocols = requestedProtocols.size();
        UpgradeCodec upgradeCodec = null;
        CharSequence upgradeProtocol = null;
        for (int i = 0; i < numRequestedProtocols; i ++) {
            final CharSequence p = requestedProtocols.get(i);
            final UpgradeCodec c = upgradeCodecFactory.newUpgradeCodec(p);
            if (c != null) {
                upgradeProtocol = p;
                upgradeCodec = c;
                break;
            }
        }

        if (upgradeCodec == null) {
            // None of the requested protocols are supported, don't upgrade.
            return false;
        }

        // Make sure the CONNECTION header is present.
        List<String> connectionHeaderValues = request.headers().getAll(HttpHeaderNames.CONNECTION);

        if (connectionHeaderValues == null || connectionHeaderValues.isEmpty()) {
            return false;
        }

        final StringBuilder concatenatedConnectionValue = new StringBuilder(connectionHeaderValues.size() * 10);
        for (CharSequence connectionHeaderValue : connectionHeaderValues) {
            concatenatedConnectionValue.append(connectionHeaderValue).append(COMMA);
        }
        concatenatedConnectionValue.setLength(concatenatedConnectionValue.length() - 1);

        // Make sure the CONNECTION header contains UPGRADE as well as all protocol-specific headers.
        Collection<CharSequence> requiredHeaders = upgradeCodec.requiredUpgradeHeaders();
        List<CharSequence> values = splitHeader(concatenatedConnectionValue);
        if (!containsContentEqualsIgnoreCase(values, HttpHeaderNames.UPGRADE) ||
                !containsAllContentEqualsIgnoreCase(values, requiredHeaders)) {
            return false;
        }

        // Ensure that all required protocol-specific headers are found in the request.
        for (CharSequence requiredHeader : requiredHeaders) {
            if (!request.headers().contains(requiredHeader)) {
                return false;
            }
        }

        // Prepare and send the upgrade response. Wait for this write to complete before upgrading,
        // since we need the old codec in-place to properly encode the response.
        final FullHttpResponse upgradeResponse = createUpgradeResponse(upgradeProtocol);
        if (!upgradeCodec.prepareUpgradeResponse(ctx, request, upgradeResponse.headers())) {
            return false;
        }

        // Create the user event to be fired once the upgrade completes.
        final UpgradeEvent event = new UpgradeEvent(upgradeProtocol, request);

        // After writing the upgrade response we immediately prepare the
        // pipeline for the next protocol to avoid a race between completion
        // of the write future and receiving data before the pipeline is
        // restructured.
        try {
            final ChannelFuture writeComplete = ctx.writeAndFlush(upgradeResponse);
            // Perform the upgrade to the new protocol.
            sourceCodec.upgradeFrom(ctx);
            upgradeCodec.upgradeTo(ctx, request);

            // Remove this handler from the pipeline.
            ctx.pipeline().remove(HttpServerUpgradeHandler.this);

            // Notify that the upgrade has occurred. Retain the event to offset
            // the release() in the finally block.
            ctx.fireUserEventTriggered(event.retain());

            // Add the listener last to avoid firing upgrade logic after
            // the channel is already closed since the listener may fire
            // immediately if the write failed eagerly.
            writeComplete.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
        } finally {

Subdomains

Called By

Frequently Asked Questions

What does upgrade() do?
upgrade() is a function in the netty codebase, defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java.
Where is upgrade() defined?
upgrade() is defined in codec-http/src/main/java/io/netty/handler/codec/http/HttpServerUpgradeHandler.java at line 370.
What does upgrade() call?
upgrade() calls 3 function(s): UpgradeEvent, release, splitHeader.
What calls upgrade()?
upgrade() is called by 1 function(s): decode.

Analyze Your Own Codebase

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

Try Supermodel Free