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    *   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.helloworld.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.Http2SecurityUtil;
26  import io.netty.handler.logging.LogLevel;
27  import io.netty.handler.logging.LoggingHandler;
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.OpenSsl;
34  import io.netty.handler.ssl.SslContext;
35  import io.netty.handler.ssl.SslContextBuilder;
36  import io.netty.handler.ssl.SslProvider;
37  import io.netty.handler.ssl.SupportedCipherSuiteFilter;
38  import io.netty.handler.ssl.util.SelfSignedCertificate;
39  
40  /**
41   * An HTTP/2 Server that responds to requests with a Hello World. Once started, you can test the
42   * server with the example client.
43   */
44  public final class Http2Server {
45  
46      static final boolean SSL = System.getProperty("ssl") != null;
47  
48      static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
49  
50      public static void main(String[] args) throws Exception {
51          // Configure SSL.
52          final SslContext sslCtx;
53          if (SSL) {
54              SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
55              SelfSignedCertificate ssc = new SelfSignedCertificate();
56              sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
57                  .sslProvider(provider)
58                  /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification.
59                   * Please refer to the HTTP/2 specification for cipher requirements. */
60                  .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
61                  .applicationProtocolConfig(new ApplicationProtocolConfig(
62                      Protocol.ALPN,
63                      // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
64                      SelectorFailureBehavior.NO_ADVERTISE,
65                      // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
66                      SelectedListenerFailureBehavior.ACCEPT,
67                      ApplicationProtocolNames.HTTP_2,
68                      ApplicationProtocolNames.HTTP_1_1))
69                  .build();
70          } else {
71              sslCtx = null;
72          }
73          // Configure the server.
74          EventLoopGroup group = new NioEventLoopGroup();
75          try {
76              ServerBootstrap b = new ServerBootstrap();
77              b.option(ChannelOption.SO_BACKLOG, 1024);
78              b.group(group)
79               .channel(NioServerSocketChannel.class)
80               .handler(new LoggingHandler(LogLevel.INFO))
81               .childHandler(new Http2ServerInitializer(sslCtx));
82  
83              Channel ch = b.bind(PORT).sync().channel();
84  
85              System.err.println("Open your HTTP/2-enabled web browser and navigate to " +
86                      (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
87  
88              ch.closeFuture().sync();
89          } finally {
90              group.shutdownGracefully();
91          }
92      }
93  }