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.concurrent.Future;
21  import io.netty5.util.internal.UnstableApi;
22  
23  import static io.netty5.handler.codec.http2.Http2FrameLogger.Direction.OUTBOUND;
24  import static java.util.Objects.requireNonNull;
25  
26  /**
27   * Decorator around a {@link Http2FrameWriter} that logs all outbound frames before calling the
28   * writer.
29   */
30  @UnstableApi
31  public class Http2OutboundFrameLogger implements Http2FrameWriter {
32      private final Http2FrameWriter writer;
33      private final Http2FrameLogger logger;
34  
35      public Http2OutboundFrameLogger(Http2FrameWriter writer, Http2FrameLogger logger) {
36          this.writer = requireNonNull(writer, "writer");
37          this.logger = requireNonNull(logger, "logger");
38      }
39  
40      @Override
41      public Future<Void> writeData(ChannelHandlerContext ctx, int streamId, Buffer data,
42                                    int padding, boolean endStream) {
43          logger.logData(OUTBOUND, ctx, streamId, data, padding, endStream);
44          return writer.writeData(ctx, streamId, data, padding, endStream);
45      }
46  
47      @Override
48      public Future<Void> writeHeaders(ChannelHandlerContext ctx, int streamId,
49                                       Http2Headers headers, int padding, boolean endStream) {
50          logger.logHeaders(OUTBOUND, ctx, streamId, headers, padding, endStream);
51          return writer.writeHeaders(ctx, streamId, headers, padding, endStream);
52      }
53  
54      @Override
55      public Future<Void> writeHeaders(ChannelHandlerContext ctx, int streamId,
56                                       Http2Headers headers, int streamDependency, short weight, boolean exclusive,
57                                       int padding, boolean endStream) {
58          logger.logHeaders(OUTBOUND, ctx, streamId, headers, streamDependency, weight, exclusive,
59                  padding, endStream);
60          return writer.writeHeaders(ctx, streamId, headers, streamDependency, weight,
61                  exclusive, padding, endStream);
62      }
63  
64      @Override
65      public Future<Void> writePriority(ChannelHandlerContext ctx, int streamId,
66                                        int streamDependency, short weight, boolean exclusive) {
67          logger.logPriority(OUTBOUND, ctx, streamId, streamDependency, weight, exclusive);
68          return writer.writePriority(ctx, streamId, streamDependency, weight, exclusive);
69      }
70  
71      @Override
72      public Future<Void> writeRstStream(ChannelHandlerContext ctx,
73                                         int streamId, long errorCode) {
74          logger.logRstStream(OUTBOUND, ctx, streamId, errorCode);
75          return writer.writeRstStream(ctx, streamId, errorCode);
76      }
77  
78      @Override
79      public Future<Void> writeSettings(ChannelHandlerContext ctx,
80                                        Http2Settings settings) {
81          logger.logSettings(OUTBOUND, ctx, settings);
82          return writer.writeSettings(ctx, settings);
83      }
84  
85      @Override
86      public Future<Void> writeSettingsAck(ChannelHandlerContext ctx) {
87          logger.logSettingsAck(OUTBOUND, ctx);
88          return writer.writeSettingsAck(ctx);
89      }
90  
91      @Override
92      public Future<Void> writePing(ChannelHandlerContext ctx, boolean ack,
93                                    long data) {
94          if (ack) {
95              logger.logPingAck(OUTBOUND, ctx, data);
96          } else {
97              logger.logPing(OUTBOUND, ctx, data);
98          }
99          return writer.writePing(ctx, ack, data);
100     }
101 
102     @Override
103     public Future<Void> writePushPromise(ChannelHandlerContext ctx, int streamId,
104                                          int promisedStreamId, Http2Headers headers, int padding) {
105         logger.logPushPromise(OUTBOUND, ctx, streamId, promisedStreamId, headers, padding);
106         return writer.writePushPromise(ctx, streamId, promisedStreamId, headers, padding);
107     }
108 
109     @Override
110     public Future<Void> writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
111                                     Buffer debugData) {
112         logger.logGoAway(OUTBOUND, ctx, lastStreamId, errorCode, debugData);
113         return writer.writeGoAway(ctx, lastStreamId, errorCode, debugData);
114     }
115 
116     @Override
117     public Future<Void> writeWindowUpdate(ChannelHandlerContext ctx,
118                                           int streamId, int windowSizeIncrement) {
119         logger.logWindowsUpdate(OUTBOUND, ctx, streamId, windowSizeIncrement);
120         return writer.writeWindowUpdate(ctx, streamId, windowSizeIncrement);
121     }
122 
123     @Override
124     public Future<Void> writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
125                                    Http2Flags flags, Buffer payload) {
126         logger.logUnknownFrame(OUTBOUND, ctx, frameType, streamId, flags, payload);
127         return writer.writeFrame(ctx, frameType, streamId, flags, payload);
128     }
129 
130     @Override
131     public void close() {
132         writer.close();
133     }
134 
135     @Override
136     public Configuration configuration() {
137         return writer.configuration();
138     }
139 }