Talk about reactor-netty AccessLogHandlerH2

  netty

Order

This article mainly studies AccessLogHandlerH2 of reactor-netty.

AccessLogHandlerH2

reactor-netty-0.8.5.RELEASE-sources.jar! /reactor/netty/http/server/AccessLogHandlerH2.java

final class AccessLogHandlerH2 extends ChannelDuplexHandler {
    static final String H2_PROTOCOL_NAME = "HTTP/2.0";

    AccessLog accessLog = new AccessLog();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof Http2HeadersFrame){
            final Http2HeadersFrame requestHeaders = (Http2HeadersFrame) msg;
            final SocketChannel channel = (SocketChannel) ctx.channel()
                                                             .parent();
            final Http2Headers headers = requestHeaders.headers();

            accessLog = new AccessLog()
                    .address(channel.remoteAddress().getHostString())
                    .port(channel.localAddress().getPort())
                    .method(headers.method())
                    .uri(headers.path())
                    .protocol(H2_PROTOCOL_NAME);
        }
        super.channelRead(ctx, msg);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        boolean lastContent = false;
        if (msg instanceof Http2HeadersFrame) {
            final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg;
            final Http2Headers headers = responseHeaders.headers();
            lastContent = responseHeaders.isEndStream();

            accessLog.status(headers.status())
                     .chunked(true);
        }
        if (msg instanceof Http2DataFrame) {
            final Http2DataFrame data = (Http2DataFrame) msg;
            lastContent = data.isEndStream();

            accessLog.increaseContentLength(data.content().readableBytes());
        }
        if (lastContent) {
            ctx.write(msg, promise)
               .addListener(future -> {
                   if (future.isSuccess()) {
                       accessLog.log();
                   }
               });
            return;
        }
        ctx.write(msg, promise);
    }
}

Access logHandlerH2 is the implementation of AccessLog for HTTP2, specifically judging Http2HeadersFrame and Http2DataFrame.

HttpServerBind

reactor-netty-0.8.5.RELEASE-sources.jar! /reactor/netty/http/server/HttpServerBind.java

final class HttpServerBind extends HttpServer
        implements Function<ServerBootstrap, ServerBootstrap> {

    //......
        
    static void addStreamHandlers(Channel ch, ConnectionObserver listener, boolean readForwardHeaders,
            ServerCookieEncoder encoder, ServerCookieDecoder decoder) {
        if (ACCESS_LOG) {
            ch.pipeline()
              .addLast(NettyPipeline.AccessLogHandler, new AccessLogHandlerH2());
        }
        ch.pipeline()
          .addLast(new Http2StreamBridgeHandler(listener, readForwardHeaders, encoder, decoder))
          .addLast(new Http2StreamFrameToHttpObjectCodec(true));

        ChannelOperations.addReactiveBridge(ch, ChannelOperations.OnSetup.empty(), listener);

        if (log.isDebugEnabled()) {
            log.debug(format(ch, "Initialized HTTP/2 pipeline {}"), ch.pipeline());
        }
    }

    //......

The addStreamHandlers static method of HttpServerBind is used to determine whether to open access log. if it is opened, accessloghandlers 2 will be created and added to pipeline;. The initChannel method of the Http1OrH2CleartextCodec and the initChannel method of the Http2StreamInitializer both call this method.

Summary

  • Access logHandlerH2 is the implementation of AccessLog for HTTP2, specifically judging Http2HeadersFrame and Http2DataFrame.
  • The addStreamHandlers static method of HttpServerBind is used to determine whether to open access log. if it is opened, accessloghandlers 2 will be created and added to pipeline.
  • The initChannel method of the Http1OrH2CleartextCodec and the initChannel method of the Http2StreamInitializer both call this method.

doc