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          super(false, inboundControlStreamHandler, unknownInboundStreamHandlerFactory, localSettings,
58                  disableQpackDynamicTable);
59          this.pushStreamHandlerFactory = pushStreamHandlerFactory;
60      }
61  
62      @Override
63      void initBidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel channel) {
64          // See https://tools.ietf.org/html/draft-ietf-quic-http-32#section-6.1
65          Http3CodecUtils.connectionError(ctx, Http3ErrorCode.H3_STREAM_CREATION_ERROR,
66                  "Server initiated bidirectional streams are not allowed", true);
67      }
68  
69      @Override
70      void initUnidirectionalStream(ChannelHandlerContext ctx, QuicStreamChannel streamChannel) {
71          final long maxTableCapacity = maxTableCapacity();
72          streamChannel.pipeline().addLast(
73                  new Http3UnidirectionalStreamInboundClientHandler(codecFactory,
74                          localControlStreamHandler, remoteControlStreamHandler,
75                          unknownInboundStreamHandlerFactory, pushStreamHandlerFactory,
76                          () -> new QpackEncoderHandler(maxTableCapacity, qpackDecoder),
77                          () -> new QpackDecoderHandler(qpackEncoder)));
78      }
79  }