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.handler.codec.quic.QuicStreamChannel;
21  import org.jetbrains.annotations.Nullable;
22  
23  import java.util.function.LongFunction;
24  
25  public final class Http3ClientConnectionHandler extends Http3ConnectionHandler {
26  
27      private final LongFunction<ChannelHandler> pushStreamHandlerFactory;
28  
29      /**
30       * Create a new instance.
31       */
32      public Http3ClientConnectionHandler() {
33          this(null, null, null, null, true);
34      }
35  
36      /**
37       * Create a new instance.
38       *
39       * @param inboundControlStreamHandler           the {@link ChannelHandler} which will be notified about
40       *                                              {@link Http3RequestStreamFrame}s or {@code null} if the user is not
41       *                                              interested in these.
42       * @param pushStreamHandlerFactory              the {@link LongFunction} that will provide a custom
43       *                                              {@link ChannelHandler} for push streams {@code null} if no special
44       *                                              handling should be done. When present, push ID will be passed as an
45       *                                              argument to the {@link LongFunction}.
46       * @param unknownInboundStreamHandlerFactory    the {@link LongFunction} that will provide a custom
47       *                                              {@link ChannelHandler} for unknown inbound stream types or
48       *                                              {@code null} if no special handling should be done.
49       * @param localSettings                         the local {@link Http3SettingsFrame} that should be sent to the
50       *                                              remote peer or {@code null} if the default settings should be used.
51       * @param disableQpackDynamicTable              If QPACK dynamic table should be disabled.
52       */
53      public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStreamHandler,
54                                          @Nullable LongFunction<ChannelHandler> pushStreamHandlerFactory,
55                                          @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
56                                          @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable) {
57          this(inboundControlStreamHandler, pushStreamHandlerFactory, unknownInboundStreamHandlerFactory, localSettings,
58                  disableQpackDynamicTable, null);
59      }
60  
61      /**
62       * Create a new instance.
63       *
64       * @param inboundControlStreamHandler           the {@link ChannelHandler} which will be notified about
65       *                                              {@link Http3RequestStreamFrame}s or {@code null} if the user is not
66       *                                              interested in these.
67       * @param pushStreamHandlerFactory              the {@link LongFunction} that will provide a custom
68       *                                              {@link ChannelHandler} for push streams {@code null} if no special
69       *                                              handling should be done. When present, push ID will be passed as an
70       *                                              argument to the {@link LongFunction}.
71       * @param unknownInboundStreamHandlerFactory    the {@link LongFunction} that will provide a custom
72       *                                              {@link ChannelHandler} for unknown inbound stream types or
73       *                                              {@code null} if no special handling should be done.
74       * @param localSettings                         the local {@link Http3SettingsFrame} that should be sent to the
75       *                                              remote peer or {@code null} if the default settings should be used.
76       * @param disableQpackDynamicTable              If QPACK dynamic table should be disabled.
77       * @param nonStandardSettingsValidator          the {@link Http3Settings.NonStandardHttp3SettingsValidator} to use
78       *                                              when validating settings that are non-standard.
79       */
80      public Http3ClientConnectionHandler(@Nullable ChannelHandler inboundControlStreamHandler,
81                                          @Nullable LongFunction<ChannelHandler> pushStreamHandlerFactory,
82                                          @Nullable LongFunction<ChannelHandler> unknownInboundStreamHandlerFactory,
83                                          @Nullable Http3SettingsFrame localSettings, boolean disableQpackDynamicTable,
84                                          @Nullable Http3Settings.NonStandardHttp3SettingsValidator
85                                                  nonStandardSettingsValidator) {
86          super(false, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings,
87                  disableQpackDynamicTable, nonStandardSettingsValidator);
88          this.pushStreamHandlerFactory = pushStreamHandlerFactory;
89      }
90  
91      @Override
92      void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel channel) {
93          // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.1
94          Http3CodecUtils.connectionError(ctx, Http3ErrorCode.H3_STREAM_CREATION_ERROR,
95                  "Server initiated bidirectional streams are not allowed", true);
96      }
97  
98      @Override
99      void initUnidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
100         final long maxTableCapacity = maxTableCapacity();
101         streamChannel.pipeline().addLast(
102                 new Http3UnidirectionalStreamInboundClientHandler(codecFactory, nonStandardSettingsValidator,
103                         localControlStreamHandler, remoteControlStreamHandler,
104                         unknownInboundStreamHandlerFactory, pushStreamHandlerFactory,
105                         () -> new QpackEncoderHandler(maxTableCapacity, qpackDecoder),
106                         () -> new QpackDecoderHandler(qpackEncoder)));
107     }
108 }