View Javadoc
1   /*
2    * Copyright 2014 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  
17  package io.netty.example.http2.server;
18  
19  import io.netty.bootstrap.ServerBootstrap;
20  import io.netty.channel.Channel;
21  import io.netty.channel.ChannelOption;
22  import io.netty.channel.EventLoopGroup;
23  import io.netty.channel.nio.NioEventLoopGroup;
24  import io.netty.channel.socket.nio.NioServerSocketChannel;
25  import io.netty.handler.codec.http2.Http2OrHttpChooser.SelectedProtocol;
26  import io.netty.handler.codec.http2.Http2SecurityUtil;
27  import io.netty.handler.logging.LogLevel;
28  import io.netty.handler.logging.LoggingHandler;
29  import io.netty.handler.ssl.ApplicationProtocolConfig;
30  import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
31  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
32  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
33  import io.netty.handler.ssl.SslContext;
34  import io.netty.handler.ssl.SslProvider;
35  import io.netty.handler.ssl.SupportedCipherSuiteFilter;
36  import io.netty.handler.ssl.util.SelfSignedCertificate;
37  
38  /**
39   * A HTTP/2 Server that responds to requests with a Hello World. Once started, you can test the
40   * server with the example client.
41   */
42  public final class Http2Server {
43  
44      static final boolean SSL = System.getProperty("ssl") != null;
45      static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
46  
47      public static void main(String[] args) throws Exception {
48          // Configure SSL.
49          final SslContext sslCtx;
50          if (SSL) {
51              SelfSignedCertificate ssc = new SelfSignedCertificate();
52              sslCtx = SslContext.newServerContext(SslProvider.JDK,
53                      ssc.certificate(), ssc.privateKey(), null,
54                      Http2SecurityUtil.CIPHERS,
55                      /* NOTE: the following filter may not include all ciphers required by the HTTP/2 specification
56                       * Please refer to the HTTP/2 specification for cipher requirements. */
57                      SupportedCipherSuiteFilter.INSTANCE,
58                      new ApplicationProtocolConfig(
59                              Protocol.ALPN,
60                              SelectorFailureBehavior.FATAL_ALERT,
61                              SelectedListenerFailureBehavior.FATAL_ALERT,
62                              SelectedProtocol.HTTP_2.protocolName(),
63                              SelectedProtocol.HTTP_1_1.protocolName()),
64                      0, 0);
65          } else {
66              sslCtx = null;
67          }
68          // Configure the server.
69          EventLoopGroup bossGroup = new NioEventLoopGroup(1);
70          EventLoopGroup workerGroup = new NioEventLoopGroup();
71          try {
72              ServerBootstrap b = new ServerBootstrap();
73              b.option(ChannelOption.SO_BACKLOG, 1024);
74              b.group(bossGroup, workerGroup)
75               .channel(NioServerSocketChannel.class)
76               .handler(new LoggingHandler(LogLevel.INFO))
77               .childHandler(new Http2ServerInitializer(sslCtx));
78  
79              Channel ch = b.bind(PORT).sync().channel();
80  
81              System.err.println("Open your HTTP/2-enabled web browser and navigate to " +
82                      (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
83  
84              ch.closeFuture().sync();
85          } finally {
86              bossGroup.shutdownGracefully();
87              workerGroup.shutdownGracefully();
88          }
89      }
90  }