查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.example.http2.helloworld.client;
16  
17  import io.netty.bootstrap.Bootstrap;
18  import io.netty.buffer.Unpooled;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.EventLoopGroup;
22  import io.netty.channel.nio.NioEventLoopGroup;
23  import io.netty.channel.socket.nio.NioSocketChannel;
24  import io.netty.handler.codec.http.DefaultFullHttpRequest;
25  import io.netty.handler.codec.http.FullHttpRequest;
26  import io.netty.handler.codec.http.HttpHeaderNames;
27  import io.netty.handler.codec.http.HttpHeaderValues;
28  import io.netty.handler.codec.http.HttpScheme;
29  import io.netty.handler.codec.http2.Http2SecurityUtil;
30  import io.netty.handler.codec.http2.HttpConversionUtil;
31  import io.netty.handler.ssl.ApplicationProtocolConfig;
32  import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
33  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
34  import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
35  import io.netty.handler.ssl.ApplicationProtocolNames;
36  import io.netty.handler.ssl.OpenSsl;
37  import io.netty.handler.ssl.SslContext;
38  import io.netty.handler.ssl.SslContextBuilder;
39  import io.netty.handler.ssl.SslProvider;
40  import io.netty.handler.ssl.SupportedCipherSuiteFilter;
41  import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
42  import io.netty.util.AsciiString;
43  import io.netty.util.CharsetUtil;
44  
45  import java.util.concurrent.TimeUnit;
46  
47  import static io.netty.buffer.Unpooled.wrappedBuffer;
48  import static io.netty.handler.codec.http.HttpMethod.GET;
49  import static io.netty.handler.codec.http.HttpMethod.POST;
50  import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
51  
52  /**
53   * An HTTP2 client that allows you to send HTTP2 frames to a server using HTTP1-style approaches
54   * (via {@link io.netty.handler.codec.http2.InboundHttp2ToHttpAdapter}). Inbound and outbound
55   * frames are logged.
56   * When run from the command-line, sends a single HEADERS frame to the server and gets back
57   * a "Hello World" response.
58   * See the ./http2/helloworld/frame/client/ example for a HTTP2 client example which does not use
59   * HTTP1-style objects and patterns.
60   */
61  public final class Http2Client {
62  
63      static final boolean SSL = System.getProperty("ssl") != null;
64      static final String HOST = System.getProperty("host", "127.0.0.1");
65      static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
66      static final String URL = System.getProperty("url", "/whatever");
67      static final String URL2 = System.getProperty("url2");
68      static final String URL2DATA = System.getProperty("url2data", "test data!");
69  
70      public static void main(String[] args) throws Exception {
71          // Configure SSL.
72          final SslContext sslCtx;
73          if (SSL) {
74              SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
75              sslCtx = SslContextBuilder.forClient()
76                  .sslProvider(provider)
77                  /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification.
78                   * Please refer to the HTTP/2 specification for cipher requirements. */
79                  .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
80                  .trustManager(InsecureTrustManagerFactory.INSTANCE)
81                  .applicationProtocolConfig(new ApplicationProtocolConfig(
82                      Protocol.ALPN,
83                      // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
84                      SelectorFailureBehavior.NO_ADVERTISE,
85                      // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
86                      SelectedListenerFailureBehavior.ACCEPT,
87                      ApplicationProtocolNames.HTTP_2,
88                      ApplicationProtocolNames.HTTP_1_1))
89                  .build();
90          } else {
91              sslCtx = null;
92          }
93  
94          EventLoopGroup workerGroup = new NioEventLoopGroup();
95          Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE);
96  
97          try {
98              // Configure the client.
99              Bootstrap b = new Bootstrap();
100             b.group(workerGroup);
101             b.channel(NioSocketChannel.class);
102             b.option(ChannelOption.SO_KEEPALIVE, true);
103             b.remoteAddress(HOST, PORT);
104             b.handler(initializer);
105 
106             // Start the client.
107             Channel channel = b.connect().syncUninterruptibly().channel();
108             System.out.println("Connected to [" + HOST + ':' + PORT + ']');
109 
110             // Wait for the HTTP/2 upgrade to occur.
111             Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
112             http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);
113 
114             HttpResponseHandler responseHandler = initializer.responseHandler();
115             int streamId = 3;
116             HttpScheme scheme = SSL ? HttpScheme.HTTPS : HttpScheme.HTTP;
117             AsciiString hostName = new AsciiString(HOST + ':' + PORT);
118             System.err.println("Sending request(s)...");
119             if (URL != null) {
120                 // Create a simple GET request.
121                 FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, URL, Unpooled.EMPTY_BUFFER);
122                 request.headers().add(HttpHeaderNames.HOST, hostName);
123                 request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
124                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
125                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
126                 responseHandler.put(streamId, channel.write(request), channel.newPromise());
127                 streamId += 2;
128             }
129             if (URL2 != null) {
130                 // Create a simple POST request with a body.
131                 FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, URL2,
132                         wrappedBuffer(URL2DATA.getBytes(CharsetUtil.UTF_8)));
133                 request.headers().add(HttpHeaderNames.HOST, hostName);
134                 request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name());
135                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
136                 request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.DEFLATE);
137                 responseHandler.put(streamId, channel.write(request), channel.newPromise());
138             }
139             channel.flush();
140             responseHandler.awaitResponses(5, TimeUnit.SECONDS);
141             System.out.println("Finished HTTP/2 request(s)");
142 
143             // Wait until the connection is closed.
144             channel.close().syncUninterruptibly();
145         } finally {
146             workerGroup.shutdownGracefully();
147         }
148     }
149 }