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          this(requestStreamHandler, inboundControlStreamHandler, unknownInboundStreamHandlerFactory,
64                  localSettings, disableQpackDynamicTable, null);
65      }
66  
67      /**
68       * Create a new instance.
69       * @param requestStreamHandler                  the {@link ChannelHandler} that is used for each new request stream.
70       *                                              This handler will receive {@link Http3HeadersFrame} and
71       *                                              {@link Http3DataFrame}s.
72       * @param inboundControlStreamHandler           the {@link ChannelHandler} which will be notified about
73       *                                              {@link Http3RequestStreamFrame}s or {@code null} if the user is not
74       *                                              interested in these.
75       * @param unknownInboundStreamHandlerFactory    the {@link LongFunction} that will provide a custom
76       *                                              {@link ChannelHandler} for unknown inbound stream types or
77       *                                              {@code null} if no special handling should be done.
78       * @param localSettings                         the local {@link Http3SettingsFrame} that should be sent to the
79       *                                              remote peer or {@code null} if the default settings should be used.
80       * @param disableQpackDynamicTable              If QPACK dynamic table should be disabled.
81       * @param nonStandardSettingsValidator          the {@link Http3Settings.NonStandardHttp3SettingsValidator} to
82       *                                              use when validating settings that are non-standard.
83       */
84      public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler,
85                                          @Nullable ChannelHandler inboundControlStreamHandler,
86                                          @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
87                                          @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable,
88                                          @Nullable Http3Settings.NonStandardHttp3SettingsValidator
89                                                  nonStandardSettingsValidator) {
90          this(requestStreamHandler, inboundControlStreamHandler, unknownInboundStreamHandlerFactory,
91                  localSettings, disableQpackDynamicTable, nonStandardSettingsValidator, null,
92                  Http3CodecUtils.DEFAULT_MAX_UNKNOWN_FRAME_PAYLOAD_LENGTH);
93      }
94  
95      /**
96       * Create a new instance.
97       * @param requestStreamHandler                  the {@link ChannelHandler} that is used for each new request stream.
98       *                                              This handler will receive {@link Http3HeadersFrame} and
99       *                                              {@link Http3DataFrame}s.
100      * @param inboundControlStreamHandler           the {@link ChannelHandler} which will be notified about
101      *                                              {@link Http3RequestStreamFrame}s or {@code null} if the user is not
102      *                                              interested in these.
103      * @param unknownInboundStreamHandlerFactory    the {@link LongFunction} that will provide a custom
104      *                                              {@link ChannelHandler} for unknown inbound stream types or
105      *                                              {@code null} if no special handling should be done.
106      * @param localSettings                         the local {@link Http3SettingsFrame} that should be sent to the
107      *                                              remote peer or {@code null} if the default settings should be used.
108      * @param disableQpackDynamicTable              If QPACK dynamic table should be disabled.
109      * @param nonStandardSettingsValidator          the {@link Http3Settings.NonStandardHttp3SettingsValidator} to
110      *                                              use when validating settings that are non-standard.
111      * @param sensitivityDetector                   detector that marks sensitive headers for QPACK Never Indexed
112      *                                              encoding, or {@code null} to use the historical default.
113      * @param maxUnknownFramePayloadLength          the maximum payload size of an unknown frame.
114      */
115     public Http3ServerConnectionHandler(ChannelHandler requestStreamHandler,
116                                         @Nullable ChannelHandler inboundControlStreamHandler,
117                                         @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
118                                         @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable,
119                                         @Nullable Http3Settings.NonStandardHttp3SettingsValidator
120                                                 nonStandardSettingsValidator,
121                                         @Nullable QpackSensitivityDetector sensitivityDetector,
122                                         int maxUnknownFramePayloadLength) {
123         super(true, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings,
124                 disableQpackDynamicTable, nonStandardSettingsValidator, sensitivityDetector,
125                 maxUnknownFramePayloadLength);
126         this.requestStreamHandler = ObjectUtil.checkNotNull(requestStreamHandler, "requestStreamHandler");
127     }
128 
129     @Override
130     void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
131         ChannelPipeline pipeline = streamChannel.pipeline();
132         Http3RequestStreamEncodeStateValidator encodeStateValidator = new Http3RequestStreamEncodeStateValidator();
133         Http3RequestStreamDecodeStateValidator decodeStateValidator = new Http3RequestStreamDecodeStateValidator();
134         // Add the encoder and decoder in the pipeline so we can handle Http3Frames
135         pipeline.addLast(newCodec(encodeStateValidator, decodeStateValidator));
136         pipeline.addLast(encodeStateValidator);
137         pipeline.addLast(decodeStateValidator);
138         pipeline.addLast(newRequestStreamValidationHandler(streamChannel, encodeStateValidator, decodeStateValidator));
139         pipeline.addLast(requestStreamHandler);
140     }
141 
142     @Override
143     void initUnidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
144         final long maxTableCapacity = maxTableCapacity();
145         streamChannel.pipeline().addLast(
146                 new Http3UnidirectionalStreamInboundServerHandler(codecFactory, nonStandardSettingsValidator,
147                         localControlStreamHandler, remoteControlStreamHandler,
148                         unknownInboundStreamHandlerFactory,
149                         () -> new QpackEncoderHandler(maxTableCapacity, qpackDecoder),
150                         () -> new QpackDecoderHandler(qpackEncoder)));
151     }
152 }