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.netty5.example.http2.tiles;
18  
19  import io.netty5.bootstrap.ServerBootstrap;
20  import io.netty5.channel.Channel;
21  import io.netty5.channel.ChannelInitializer;
22  import io.netty5.channel.ChannelOption;
23  import io.netty5.channel.EventLoopGroup;
24  import io.netty5.channel.socket.SocketChannel;
25  import io.netty5.channel.socket.nio.NioServerSocketChannel;
26  import io.netty5.handler.ssl.ApplicationProtocolConfig;
27  import io.netty5.handler.ssl.ApplicationProtocolConfig.Protocol;
28  import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
29  import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
30  import io.netty5.handler.ssl.ApplicationProtocolNames;
31  import io.netty5.handler.ssl.SslContext;
32  import io.netty5.handler.ssl.SslContextBuilder;
33  import io.netty5.handler.ssl.SupportedCipherSuiteFilter;
34  import io.netty5.handler.ssl.util.SelfSignedCertificate;
35  import io.netty5.util.concurrent.Future;
36  
37  import javax.net.ssl.SSLException;
38  import java.security.cert.CertificateException;
39  
40  import static io.netty5.handler.codec.http2.Http2SecurityUtil.CIPHERS;
41  
42  /**
43   * Demonstrates an Http2 server using Netty to display a bunch of images and
44   * simulate latency. It is a Netty version of the <a href="https://http2.golang.org/gophertiles?latency=0">
45   * Go lang HTTP2 tiles demo</a>.
46   */
47  public class Http2Server {
48  
49      public static final int PORT = Integer.parseInt(System.getProperty("http2-port", "8443"));
50  
51      private final EventLoopGroup group;
52  
53      public Http2Server(EventLoopGroup eventLoopGroup) {
54          group = eventLoopGroup;
55      }
56  
57      public Future<Void> start() throws Exception {
58          final SslContext sslCtx = configureTLS();
59          ServerBootstrap b = new ServerBootstrap();
60          b.option(ChannelOption.SO_BACKLOG, 1024);
61          b.group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
62              @Override
63              protected void initChannel(SocketChannel ch) throws Exception {
64                  ch.pipeline().addLast(sslCtx.newHandler(ch.bufferAllocator()), new Http2OrHttpHandler());
65              }
66          });
67  
68          Channel ch = b.bind(PORT).asStage().get();
69          return ch.closeFuture();
70      }
71  
72      private static SslContext configureTLS() throws CertificateException, SSLException {
73          SelfSignedCertificate ssc = new SelfSignedCertificate();
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.certificate(), ssc.privateKey(), null)
84                                  .ciphers(CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
85                                  .applicationProtocolConfig(apn).build();
86      }
87  }