View Javadoc
1   /*
2    * Copyright 2020 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.http3;
17  
18  import io.netty.channel.ChannelHandler;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.ChannelPipeline;
21  import io.netty.handler.codec.quic.QuicStreamChannel;
22  import io.netty.util.internal.ObjectUtil;
23  import org.jetbrains.annotations.Nullable;
24  
25  import java.util.function.LongFunction;
26  
27  
28  /**
29   * Handler that handles <a href="https://tools.ietf.org/html/draft-ietf-quic-http-32">HTTP3</a> for the server-side.
30   */
31  public final class Http3ServerConnectionHandler extends Http3ConnectionHandler {
32      private final ChannelHandler requestStreamHandler;
33  
34      /**
35       * Create a new instance.
36       *
37       * @param requestStreamHandler  the {@link ChannelHandler} that is used for each new request stream.
38       *                              This handler will receive {@link Http3HeadersFrame} and {@link Http3DataFrame}s.
39       */
40      public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler) {
41          this(requestStreamHandler, null, null, null, true);
42      }
43  
44      /**
45       * Create a new instance.
46       * @param requestStreamHandler                  the {@link ChannelHandler} that is used for each new request stream.
47       *                                              This handler will receive {@link Http3HeadersFrame} and
48       *                                              {@link Http3DataFrame}s.
49       * @param inboundControlStreamHandler           the {@link ChannelHandler} which will be notified about
50       *                                              {@link Http3RequestStreamFrame}s or {@code null} if the user is not
51       *                                              interested in these.
52       * @param unknownInboundStreamHandlerFactory    the {@link LongFunction} that will provide a custom
53       *                                              {@link ChannelHandler} for unknown inbound stream types or
54       *                                              {@code null} if no special handling should be done.
55       * @param localSettings                         the local {@link Http3SettingsFrame} that should be sent to the
56       *                                              remote peer or {@code null} if the default settings should be used.
57       * @param disableQpackDynamicTable              If QPACK dynamic table should be disabled.
58       */
59      public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler,
60                                          @Nullable ChannelHandler inboundControlStreamHandler,
61                                          @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
62                                          @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable) {
63          super(true, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings,
64                  disableQpackDynamicTable);
65          this.requestStreamHandler = ObjectUtil.checkNotNull(requestStreamHandler, "requestStreamHandler");
66      }
67  
68      @Override
69      void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
70          ChannelPipeline pipeline = streamChannel.pipeline();
71          Http3RequestStreamEncodeStateValidator encodeStateValidator = new Http3RequestStreamEncodeStateValidator();
72          Http3RequestStreamDecodeStateValidator decodeStateValidator = new Http3RequestStreamDecodeStateValidator();
73          // Add the encoder and decoder in the pipeline so we can handle Http3Frames
74          pipeline.addLast(newCodec(encodeStateValidator, decodeStateValidator));
75          pipeline.addLast(encodeStateValidator);
76          pipeline.addLast(decodeStateValidator);
77          pipeline.addLast(newRequestStreamValidationHandler(streamChannel, encodeStateValidator, decodeStateValidator));
78          pipeline.addLast(requestStreamHandler);
79      }
80  
81      @Override
82      void initUnidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
83          final long maxTableCapacity = maxTableCapacity();
84          streamChannel.pipeline().addLast(
85                  new Http3UnidirectionalStreamInboundServerHandler(codecFactory,
86                          localControlStreamHandler, remoteControlStreamHandler,
87                          unknownInboundStreamHandlerFactory,
88                          () -> new QpackEncoderHandler(maxTableCapacity, qpackDecoder),
89                          () -> new QpackDecoderHandler(qpackEncoder)));
90      }
91  }