Home / Class/ HelloWorldHttp2Handler Class — netty Architecture

HelloWorldHttp2Handler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  679cb2c0_10f0_ca95_5e26_6209ae084a9f["HelloWorldHttp2Handler"]
  87eef2f1_5ce1_7373_5ba9_48cad5789430["HelloWorldHttp2Handler.java"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|defined in| 87eef2f1_5ce1_7373_5ba9_48cad5789430
  6d953cad_92d6_a54c_265b_77b8f5d8f62a["HelloWorldHttp2Handler()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 6d953cad_92d6_a54c_265b_77b8f5d8f62a
  f75c6930_d0c5_71df_6c84_928ae87de9ce["Http2Headers()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| f75c6930_d0c5_71df_6c84_928ae87de9ce
  d33a3241_5ef6_9a5c_3d1e_663abb2f1a1e["userEventTriggered()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| d33a3241_5ef6_9a5c_3d1e_663abb2f1a1e
  e21acdb4_f78c_a21a_55d7_c33d0b8a907e["exceptionCaught()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| e21acdb4_f78c_a21a_55d7_c33d0b8a907e
  133bb4fc_5955_8367_566e_0a5a5e4eeeae["sendResponse()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 133bb4fc_5955_8367_566e_0a5a5e4eeeae
  20537d4b_6e78_aaec_bd7e_1852c0565d54["onDataRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 20537d4b_6e78_aaec_bd7e_1852c0565d54
  e042b45a_8425_005c_5b9c_9970568a3fe5["onHeadersRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| e042b45a_8425_005c_5b9c_9970568a3fe5
  43ca317d_7d53_8213_a8c1_d26914a1e916["onPriorityRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 43ca317d_7d53_8213_a8c1_d26914a1e916
  3aa13c3f_8275_f1cb_0611_6059a98b2923["onRstStreamRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 3aa13c3f_8275_f1cb_0611_6059a98b2923
  4e7d520e_cc7a_080f_744a_5ad6a5ee3252["onSettingsAckRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 4e7d520e_cc7a_080f_744a_5ad6a5ee3252
  4e3e3df6_66ec_0c54_7a76_e05db4efdc44["onSettingsRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 4e3e3df6_66ec_0c54_7a76_e05db4efdc44
  295150cb_209d_b298_9fe6_552985312f49["onPingRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 295150cb_209d_b298_9fe6_552985312f49
  886e7c12_af53_a2ca_8c3d_8c9e69c0aa09["onPingAckRead()"]
  679cb2c0_10f0_ca95_5e26_6209ae084a9f -->|method| 886e7c12_af53_a2ca_8c3d_8c9e69c0aa09

Relationship Graph

Source Code

example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java lines 43–166

public final class HelloWorldHttp2Handler extends Http2ConnectionHandler implements Http2FrameListener {

    static final ByteBuf RESPONSE_BYTES = unreleasableBuffer(
            copiedBuffer("Hello World", CharsetUtil.UTF_8)).asReadOnly();

    HelloWorldHttp2Handler(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder,
                           Http2Settings initialSettings) {
        super(decoder, encoder, initialSettings);
    }

    private static Http2Headers http1HeadersToHttp2Headers(FullHttpRequest request) {
        CharSequence host = request.headers().get(HttpHeaderNames.HOST);
        Http2Headers http2Headers = new DefaultHttp2Headers()
                .method(HttpMethod.GET.asciiName())
                .path(request.uri())
                .scheme(HttpScheme.HTTP.name());
        if (host != null) {
            http2Headers.authority(host);
        }
        return http2Headers;
    }

    /**
     * Handles the cleartext HTTP upgrade event. If an upgrade occurred, sends a simple response via HTTP/2
     * on stream 1 (the stream specifically reserved for cleartext HTTP upgrade).
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof HttpServerUpgradeHandler.UpgradeEvent) {
            HttpServerUpgradeHandler.UpgradeEvent upgradeEvent =
                    (HttpServerUpgradeHandler.UpgradeEvent) evt;
            onHeadersRead(ctx, 1, http1HeadersToHttp2Headers(upgradeEvent.upgradeRequest()), 0 , true);
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
        ctx.close();
    }

    /**
     * Sends a "Hello World" DATA frame to the client.
     */
    private void sendResponse(ChannelHandlerContext ctx, int streamId, ByteBuf payload) {
        // Send a frame for the response status
        Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
        encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
        encoder().writeData(ctx, streamId, payload, 0, true, ctx.newPromise());

        // no need to call flush as channelReadComplete(...) will take care of it.
    }

    @Override
    public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) {
        int processed = data.readableBytes() + padding;
        if (endOfStream) {
            sendResponse(ctx, streamId, data.retain());
        }
        return processed;
    }

    @Override
    public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
                              Http2Headers headers, int padding, boolean endOfStream) {
        if (endOfStream) {
            ByteBuf content = ctx.alloc().buffer();
            content.writeBytes(RESPONSE_BYTES.duplicate());
            ByteBufUtil.writeAscii(content, " - via HTTP/2");
            sendResponse(ctx, streamId, content);
        }
    }

    @Override
    public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency,
                              short weight, boolean exclusive, int padding, boolean endOfStream) {
        onHeadersRead(ctx, streamId, headers, padding, endOfStream);
    }

Frequently Asked Questions

What is the HelloWorldHttp2Handler class?
HelloWorldHttp2Handler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java.
Where is HelloWorldHttp2Handler defined?
HelloWorldHttp2Handler is defined in example/src/main/java/io/netty/example/http2/helloworld/server/HelloWorldHttp2Handler.java at line 43.

Analyze Your Own Codebase

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

Try Supermodel Free