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