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
  e70e95b3_6153_ac8d_192f_c528a381f039["HelloWorldHttp2Handler"]
  091d8d10_5512_0640_b4b0_931c46ab24ad["HelloWorldHttp2Handler.java"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|defined in| 091d8d10_5512_0640_b4b0_931c46ab24ad
  0b084d55_898c_b728_4e52_2eb2d7bf38b8["HelloWorldHttp2Handler()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 0b084d55_898c_b728_4e52_2eb2d7bf38b8
  6df84173_ca8a_e6c4_bf6b_f98b9bb55cce["Http2Headers()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 6df84173_ca8a_e6c4_bf6b_f98b9bb55cce
  73a2420c_3556_3e02_6313_afa480eeee6a["userEventTriggered()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 73a2420c_3556_3e02_6313_afa480eeee6a
  d24d23a0_6523_bfc4_4a31_005df77fa0e8["exceptionCaught()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| d24d23a0_6523_bfc4_4a31_005df77fa0e8
  6a02f636_5a1c_6de7_de0e_243781f44505["sendResponse()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 6a02f636_5a1c_6de7_de0e_243781f44505
  2aff3e0d_b9da_dfd8_52d7_204fc2934c59["onDataRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 2aff3e0d_b9da_dfd8_52d7_204fc2934c59
  35a20a9d_00ea_ecf9_7cf7_973f32dc00d6["onHeadersRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 35a20a9d_00ea_ecf9_7cf7_973f32dc00d6
  cb99f0aa_1cb7_4d40_f391_f39db7875690["onPriorityRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| cb99f0aa_1cb7_4d40_f391_f39db7875690
  fdca8d6f_0641_fd62_6fbc_391ebcf8ca0d["onRstStreamRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| fdca8d6f_0641_fd62_6fbc_391ebcf8ca0d
  d70a79b0_a361_d4e6_6f61_459528aae853["onSettingsAckRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| d70a79b0_a361_d4e6_6f61_459528aae853
  dd2b3ad1_3be6_9822_9a50_9496e47244f6["onSettingsRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| dd2b3ad1_3be6_9822_9a50_9496e47244f6
  737c5c4c_8679_5d55_7efc_6acbda626339["onPingRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 737c5c4c_8679_5d55_7efc_6acbda626339
  16dc354c_fbba_305f_b328_5746fbc7f894["onPingAckRead()"]
  e70e95b3_6153_ac8d_192f_c528a381f039 -->|method| 16dc354c_fbba_305f_b328_5746fbc7f894

Relationship Graph

Source Code

testsuite-http2/src/main/java/io/netty/testsuite/http2/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 testsuite-http2/src/main/java/io/netty/testsuite/http2/HelloWorldHttp2Handler.java.
Where is HelloWorldHttp2Handler defined?
HelloWorldHttp2Handler is defined in testsuite-http2/src/main/java/io/netty/testsuite/http2/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