View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec.http2;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.channel.ChannelHandlerContext;
20  import io.netty5.util.internal.UnstableApi;
21  
22  import static io.netty5.handler.codec.http2.Http2FrameLogger.Direction.INBOUND;
23  import static java.util.Objects.requireNonNull;
24  
25  /**
26   * Decorator around a {@link Http2FrameReader} that logs all inbound frames before calling
27   * back the listener.
28   */
29  @UnstableApi
30  public class Http2InboundFrameLogger implements Http2FrameReader {
31      private final Http2FrameReader reader;
32      private final Http2FrameLogger logger;
33  
34      public Http2InboundFrameLogger(Http2FrameReader reader, Http2FrameLogger logger) {
35          this.reader = requireNonNull(reader, "reader");
36          this.logger = requireNonNull(logger, "logger");
37      }
38  
39      @Override
40      public void readFrame(ChannelHandlerContext ctx, Buffer input, final Http2FrameListener listener)
41              throws Http2Exception {
42          reader.readFrame(ctx, input, new Http2FrameListener() {
43  
44              @Override
45              public int onDataRead(ChannelHandlerContext ctx, int streamId, Buffer data,
46                      int padding, boolean endOfStream)
47                      throws Http2Exception {
48                  logger.logData(INBOUND, ctx, streamId, data, padding, endOfStream);
49                  return listener.onDataRead(ctx, streamId, data, padding, endOfStream);
50              }
51  
52              @Override
53              public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
54                      Http2Headers headers, int padding, boolean endStream)
55                      throws Http2Exception {
56                  logger.logHeaders(INBOUND, ctx, streamId, headers, padding, endStream);
57                  listener.onHeadersRead(ctx, streamId, headers, padding, endStream);
58              }
59  
60              @Override
61              public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
62                      Http2Headers headers, int streamDependency, short weight, boolean exclusive,
63                      int padding, boolean endStream) throws Http2Exception {
64                  logger.logHeaders(INBOUND, ctx, streamId, headers, streamDependency, weight, exclusive,
65                          padding, endStream);
66                  listener.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive,
67                          padding, endStream);
68              }
69  
70              @Override
71              public void onPriorityRead(ChannelHandlerContext ctx, int streamId,
72                      int streamDependency, short weight, boolean exclusive) throws Http2Exception {
73                  logger.logPriority(INBOUND, ctx, streamId, streamDependency, weight, exclusive);
74                  listener.onPriorityRead(ctx, streamId, streamDependency, weight, exclusive);
75              }
76  
77              @Override
78              public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode)
79                      throws Http2Exception {
80                  logger.logRstStream(INBOUND, ctx, streamId, errorCode);
81                  listener.onRstStreamRead(ctx, streamId, errorCode);
82              }
83  
84              @Override
85              public void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Exception {
86                  logger.logSettingsAck(INBOUND, ctx);
87                  listener.onSettingsAckRead(ctx);
88              }
89  
90              @Override
91              public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings)
92                      throws Http2Exception {
93                  logger.logSettings(INBOUND, ctx, settings);
94                  listener.onSettingsRead(ctx, settings);
95              }
96  
97              @Override
98              public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception {
99                  logger.logPing(INBOUND, ctx, data);
100                 listener.onPingRead(ctx, data);
101             }
102 
103             @Override
104             public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception {
105                 logger.logPingAck(INBOUND, ctx, data);
106                 listener.onPingAckRead(ctx, data);
107             }
108 
109             @Override
110             public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId,
111                     int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
112                 logger.logPushPromise(INBOUND, ctx, streamId, promisedStreamId, headers, padding);
113                 listener.onPushPromiseRead(ctx, streamId, promisedStreamId, headers, padding);
114             }
115 
116             @Override
117             public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
118                                      Buffer debugData) throws Http2Exception {
119                 logger.logGoAway(INBOUND, ctx, lastStreamId, errorCode, debugData);
120                 listener.onGoAwayRead(ctx, lastStreamId, errorCode, debugData);
121             }
122 
123             @Override
124             public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement)
125                     throws Http2Exception {
126                 logger.logWindowsUpdate(INBOUND, ctx, streamId, windowSizeIncrement);
127                 listener.onWindowUpdateRead(ctx, streamId, windowSizeIncrement);
128             }
129 
130             @Override
131             public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
132                                        Http2Flags flags, Buffer payload) throws Http2Exception {
133                 logger.logUnknownFrame(INBOUND, ctx, frameType, streamId, flags, payload);
134                 listener.onUnknownFrame(ctx, frameType, streamId, flags, payload);
135             }
136         });
137     }
138 
139     @Override
140     public void close() {
141         reader.close();
142     }
143 
144     @Override
145     public Configuration configuration() {
146         return reader.configuration();
147     }
148 }