View Javadoc
1   /*
2    * Copyright 2015 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  
17  package io.netty.example.http2.tiles;
18  
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.http.HttpObjectAggregator;
21  import io.netty.handler.codec.http.HttpServerCodec;
22  import io.netty.handler.codec.http2.DefaultHttp2Connection;
23  import io.netty.handler.codec.http2.HttpToHttp2ConnectionHandlerBuilder;
24  import io.netty.handler.codec.http2.InboundHttp2ToHttpAdapter;
25  import io.netty.handler.codec.http2.InboundHttp2ToHttpAdapterBuilder;
26  import io.netty.handler.ssl.ApplicationProtocolNames;
27  import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler;
28  
29  /**
30   * Used during protocol negotiation, the main function of this handler is to
31   * return the HTTP/1.1 or HTTP/2 handler once the protocol has been negotiated.
32   */
33  public class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler {
34  
35      private static final int MAX_CONTENT_LENGTH = 1024 * 100;
36  
37      protected Http2OrHttpHandler() {
38          super(ApplicationProtocolNames.HTTP_1_1);
39      }
40  
41      @Override
42      protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception {
43          if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
44              configureHttp2(ctx);
45              return;
46          }
47  
48          if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
49              configureHttp1(ctx);
50              return;
51          }
52  
53          throw new IllegalStateException("unknown protocol: " + protocol);
54      }
55  
56      private static void configureHttp2(ChannelHandlerContext ctx) {
57          DefaultHttp2Connection connection = new DefaultHttp2Connection(true);
58          InboundHttp2ToHttpAdapter listener = new InboundHttp2ToHttpAdapterBuilder(connection)
59                  .propagateSettings(true).validateHttpHeaders(false)
60                  .maxContentLength(MAX_CONTENT_LENGTH).build();
61  
62          ctx.pipeline().addLast(new HttpToHttp2ConnectionHandlerBuilder()
63                  .frameListener(listener)
64                  // .frameLogger(TilesHttp2ToHttpHandler.logger)
65                  .connection(connection).build());
66  
67          ctx.pipeline().addLast(new Http2RequestHandler());
68      }
69  
70      private static void configureHttp1(ChannelHandlerContext ctx) throws Exception {
71          ctx.pipeline().addLast(new HttpServerCodec(),
72                                 new HttpObjectAggregator(MAX_CONTENT_LENGTH),
73                                 new FallbackRequestHandler());
74      }
75  }