View Javadoc
1   /*
2    * Copyright 2017 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.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.channel.ChannelHandler;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.handler.codec.ByteToMessageDecoder;
23  import io.netty.handler.codec.http.HttpServerCodec;
24  import io.netty.handler.codec.http.HttpServerUpgradeHandler;
25  
26  import java.util.List;
27  
28  import static io.netty.buffer.Unpooled.unreleasableBuffer;
29  import static io.netty.handler.codec.http2.Http2CodecUtil.connectionPrefaceBuf;
30  
31  import static io.netty.util.internal.ObjectUtil.checkNotNull;
32  
33  /**
34   * Performing cleartext upgrade, by h2c HTTP upgrade or Prior Knowledge.
35   * This handler config pipeline for h2c upgrade when handler added.
36   * And will update pipeline once it detect the connection is starting HTTP/2 by
37   * prior knowledge or not.
38   */
39  public final class CleartextHttp2ServerUpgradeHandler extends ByteToMessageDecoder {
40      private static final ByteBuf CONNECTION_PREFACE = unreleasableBuffer(connectionPrefaceBuf()).asReadOnly();
41  
42      private final HttpServerCodec httpServerCodec;
43      private final HttpServerUpgradeHandler httpServerUpgradeHandler;
44      private final ChannelHandler http2ServerHandler;
45  
46      /**
47       * Creates the channel handler provide cleartext HTTP/2 upgrade from HTTP
48       * upgrade or prior knowledge
49       *
50       * @param httpServerCodec the http server codec
51       * @param httpServerUpgradeHandler the http server upgrade handler for HTTP/2
52       * @param http2ServerHandler the http2 server handler, will be added into pipeline
53       *                           when starting HTTP/2 by prior knowledge
54       */
55      public CleartextHttp2ServerUpgradeHandler(HttpServerCodec httpServerCodec,
56                                                HttpServerUpgradeHandler httpServerUpgradeHandler,
57                                                ChannelHandler http2ServerHandler) {
58          this.httpServerCodec = checkNotNull(httpServerCodec, "httpServerCodec");
59          this.httpServerUpgradeHandler = checkNotNull(httpServerUpgradeHandler, "httpServerUpgradeHandler");
60          this.http2ServerHandler = checkNotNull(http2ServerHandler, "http2ServerHandler");
61      }
62  
63      @Override
64      public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
65          ctx.pipeline()
66                  .addAfter(ctx.name(), null, httpServerUpgradeHandler)
67                  .addAfter(ctx.name(), null, httpServerCodec);
68      }
69  
70      /**
71       * Peek inbound message to determine current connection wants to start HTTP/2
72       * by HTTP upgrade or prior knowledge
73       */
74      @Override
75      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
76          int prefaceLength = CONNECTION_PREFACE.readableBytes();
77          int bytesRead = Math.min(in.readableBytes(), prefaceLength);
78  
79          if (!ByteBufUtil.equals(CONNECTION_PREFACE, CONNECTION_PREFACE.readerIndex(),
80                  in, in.readerIndex(), bytesRead)) {
81              ctx.pipeline().remove(this);
82          } else if (bytesRead == prefaceLength) {
83              // Full h2 preface match, removed source codec, using http2 codec to handle
84              // following network traffic
85              ctx.pipeline()
86                      .remove(httpServerCodec)
87                      .remove(httpServerUpgradeHandler);
88  
89              ctx.pipeline().addAfter(ctx.name(), null, http2ServerHandler);
90              ctx.pipeline().remove(this);
91  
92              ctx.fireUserEventTriggered(PriorKnowledgeUpgradeEvent.INSTANCE);
93          }
94      }
95  
96      /**
97       * User event that is fired to notify about HTTP/2 protocol is started.
98       */
99      public static final class PriorKnowledgeUpgradeEvent {
100         private static final PriorKnowledgeUpgradeEvent INSTANCE = new PriorKnowledgeUpgradeEvent();
101 
102         private PriorKnowledgeUpgradeEvent() {
103         }
104     }
105 }