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.netty.handler.codec.http2;
17  
18  import static io.netty.handler.codec.http2.Http2FrameLogger.Direction.INBOUND;
19  import static io.netty.util.internal.ObjectUtil.checkNotNull;
20  import io.netty.buffer.ByteBuf;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.util.internal.UnstableApi;
23  
24  /**
25   * Decorator around a {@link Http2FrameReader} that logs all inbound frames before calling
26   * back the listener.
27   */
28  @UnstableApi
29  public class Http2InboundFrameLogger implements Http2FrameReader {
30      private final Http2FrameReader reader;
31      private final Http2FrameLogger logger;
32  
33      public Http2InboundFrameLogger(Http2FrameReader reader, Http2FrameLogger logger) {
34          this.reader = checkNotNull(reader, "reader");
35          this.logger = checkNotNull(logger, "logger");
36      }
37  
38      @Override
39      public void readFrame(ChannelHandlerContext ctx, ByteBuf input, final Http2FrameListener listener)
40              throws Http2Exception {
41          reader.readFrame(ctx, input, new Http2FrameListener() {
42  
43              @Override
44              public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data,
45                      int padding, boolean endOfStream)
46                      throws Http2Exception {
47                  logger.logData(INBOUND, ctx, streamId, data, padding, endOfStream);
48                  return listener.onDataRead(ctx, streamId, data, padding, endOfStream);
49              }
50  
51              @Override
52              public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
53                      Http2Headers headers, int padding, boolean endStream)
54                      throws Http2Exception {
55                  logger.logHeaders(INBOUND, ctx, streamId, headers, padding, endStream);
56                  listener.onHeadersRead(ctx, streamId, headers, padding, endStream);
57              }
58  
59              @Override
60              public void onHeadersRead(ChannelHandlerContext ctx, int streamId,
61                      Http2Headers headers, int streamDependency, short weight, boolean exclusive,
62                      int padding, boolean endStream) throws Http2Exception {
63                  logger.logHeaders(INBOUND, ctx, streamId, headers, streamDependency, weight, exclusive,
64                          padding, endStream);
65                  listener.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive,
66                          padding, endStream);
67              }
68  
69              @Override
70              public void onPriorityRead(ChannelHandlerContext ctx, int streamId,
71                      int streamDependency, short weight, boolean exclusive) throws Http2Exception {
72                  logger.logPriority(INBOUND, ctx, streamId, streamDependency, weight, exclusive);
73                  listener.onPriorityRead(ctx, streamId, streamDependency, weight, exclusive);
74              }
75  
76              @Override
77              public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode)
78                      throws Http2Exception {
79                  logger.logRstStream(INBOUND, ctx, streamId, errorCode);
80                  listener.onRstStreamRead(ctx, streamId, errorCode);
81              }
82  
83              @Override
84              public void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Exception {
85                  logger.logSettingsAck(INBOUND, ctx);
86                  listener.onSettingsAckRead(ctx);
87              }
88  
89              @Override
90              public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings)
91                      throws Http2Exception {
92                  logger.logSettings(INBOUND, ctx, settings);
93                  listener.onSettingsRead(ctx, settings);
94              }
95  
96              @Override
97              public void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception {
98                  logger.logPing(INBOUND, ctx, data);
99                  listener.onPingRead(ctx, data);
100             }
101 
102             @Override
103             public void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception {
104                 logger.logPingAck(INBOUND, ctx, data);
105                 listener.onPingAckRead(ctx, data);
106             }
107 
108             @Override
109             public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId,
110                     int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
111                 logger.logPushPromise(INBOUND, ctx, streamId, promisedStreamId, headers, padding);
112                 listener.onPushPromiseRead(ctx, streamId, promisedStreamId, headers, padding);
113             }
114 
115             @Override
116             public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
117                     ByteBuf debugData) throws Http2Exception {
118                 logger.logGoAway(INBOUND, ctx, lastStreamId, errorCode, debugData);
119                 listener.onGoAwayRead(ctx, lastStreamId, errorCode, debugData);
120             }
121 
122             @Override
123             public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement)
124                     throws Http2Exception {
125                 logger.logWindowsUpdate(INBOUND, ctx, streamId, windowSizeIncrement);
126                 listener.onWindowUpdateRead(ctx, streamId, windowSizeIncrement);
127             }
128 
129             @Override
130             public void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
131                     Http2Flags flags, ByteBuf payload) throws Http2Exception {
132                 logger.logUnknownFrame(INBOUND, ctx, frameType, streamId, flags, payload);
133                 listener.onUnknownFrame(ctx, frameType, streamId, flags, payload);
134             }
135         });
136     }
137 
138     @Override
139     public void close() {
140         reader.close();
141     }
142 
143     @Override
144     public Configuration configuration() {
145         return reader.configuration();
146     }
147 }