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 static io.netty.handler.codec.http2.Http2SecurityUtil.CIPHERS;
20  import io.netty.bootstrap.ServerBootstrap;
21  import io.netty.channel.Channel;
22  import io.netty.channel.ChannelFuture;
23  import io.netty.channel.ChannelInitializer;
24  import io.netty.channel.ChannelOption;
25  import io.netty.channel.EventLoopGroup;
26  import io.netty.channel.socket.SocketChannel;
27  import io.netty.channel.socket.nio.NioServerSocketChannel;
28  import io.netty.handler.ssl.ApplicationProtocolConfig;
29  import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
30  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
31  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
32  import io.netty.handler.ssl.ApplicationProtocolNames;
33  import io.netty.handler.ssl.SslContext;
34  import io.netty.handler.ssl.SslContextBuilder;
35  import io.netty.handler.ssl.SupportedCipherSuiteFilter;
36  import io.netty.pkitesting.CertificateBuilder;
37  import io.netty.pkitesting.X509Bundle;
38  
39  /**
40   * Demonstrates an Http2 server using Netty to display a bunch of images and
41   * simulate latency. It is a Netty version of the <a href="https://http2.golang.org/gophertiles?latency=0">
42   * Go lang HTTP2 tiles demo</a>.
43   */
44  public class Http2Server {
45  
46      public static final int PORT = Integer.parseInt(System.getProperty("http2-port", "8443"));
47  
48      private final EventLoopGroup group;
49  
50      public Http2Server(EventLoopGroup eventLoopGroup) {
51          group = eventLoopGroup;
52      }
53  
54      public ChannelFuture start() throws Exception {
55          final SslContext sslCtx = configureTLS();
56          ServerBootstrap b = new ServerBootstrap();
57          b.option(ChannelOption.SO_BACKLOG, 1024);
58          b.group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
59              @Override
60              protected void initChannel(SocketChannel ch) throws Exception {
61                  ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new Http2OrHttpHandler());
62              }
63          });
64  
65          Channel ch = b.bind(PORT).sync().channel();
66          return ch.closeFuture();
67      }
68  
69      private static SslContext configureTLS() throws Exception {
70          X509Bundle ssc = new CertificateBuilder()
71                  .subject("cn=localhost")
72                  .setIsCertificateAuthority(true)
73                  .buildSelfSigned();
74          ApplicationProtocolConfig apn = new ApplicationProtocolConfig(
75                  Protocol.ALPN,
76                  // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
77                  SelectorFailureBehavior.NO_ADVERTISE,
78                  // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
79                  SelectedListenerFailureBehavior.ACCEPT,
80                  ApplicationProtocolNames.HTTP_2,
81                  ApplicationProtocolNames.HTTP_1_1);
82  
83          return SslContextBuilder.forServer(ssc.toKeyManagerFactory())
84                                  .ciphers(CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
85                                  .applicationProtocolConfig(apn).build();
86      }
87  }