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