1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty5.example.http2.helloworld.frame.client;
16
17 import io.netty5.bootstrap.Bootstrap;
18 import io.netty5.channel.Channel;
19 import io.netty5.channel.ChannelOption;
20 import io.netty5.channel.EventLoopGroup;
21 import io.netty5.channel.MultithreadEventLoopGroup;
22 import io.netty5.channel.nio.NioHandler;
23 import io.netty5.channel.socket.nio.NioSocketChannel;
24 import io.netty5.handler.codec.http2.DefaultHttp2Headers;
25 import io.netty5.handler.codec.http2.DefaultHttp2HeadersFrame;
26 import io.netty5.handler.codec.http2.Http2HeadersFrame;
27 import io.netty5.handler.codec.http2.Http2SecurityUtil;
28 import io.netty5.handler.codec.http2.Http2StreamChannel;
29 import io.netty5.handler.codec.http2.Http2StreamChannelBootstrap;
30 import io.netty5.handler.ssl.ApplicationProtocolConfig;
31 import io.netty5.handler.ssl.ApplicationProtocolConfig.Protocol;
32 import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
33 import io.netty5.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
34 import io.netty5.handler.ssl.ApplicationProtocolNames;
35 import io.netty5.handler.ssl.SslContext;
36 import io.netty5.handler.ssl.SslContextBuilder;
37 import io.netty5.handler.ssl.SslProvider;
38 import io.netty5.handler.ssl.SupportedCipherSuiteFilter;
39 import io.netty5.handler.ssl.util.InsecureTrustManagerFactory;
40
41
42
43
44
45
46
47
48
49 public final class Http2FrameClient {
50
51 static final boolean SSL = System.getProperty("ssl") != null;
52 static final String HOST = System.getProperty("host", "127.0.0.1");
53 static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
54 static final String PATH = System.getProperty("path", "/");
55
56 private Http2FrameClient() {
57 }
58
59 public static void main(String[] args) throws Exception {
60 final EventLoopGroup clientWorkerGroup = new MultithreadEventLoopGroup(NioHandler.newFactory());
61
62
63 final SslContext sslCtx;
64 if (SSL) {
65 final SslProvider provider =
66 SslProvider.isAlpnSupported(SslProvider.OPENSSL)? SslProvider.OPENSSL : SslProvider.JDK;
67 sslCtx = SslContextBuilder.forClient()
68 .sslProvider(provider)
69 .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
70
71 .trustManager(InsecureTrustManagerFactory.INSTANCE)
72 .applicationProtocolConfig(new ApplicationProtocolConfig(
73 Protocol.ALPN,
74 SelectorFailureBehavior.NO_ADVERTISE,
75 SelectedListenerFailureBehavior.ACCEPT,
76 ApplicationProtocolNames.HTTP_2,
77 ApplicationProtocolNames.HTTP_1_1))
78 .build();
79 } else {
80 sslCtx = null;
81 }
82
83 try {
84 final Bootstrap b = new Bootstrap();
85 b.group(clientWorkerGroup);
86 b.channel(NioSocketChannel.class);
87 b.option(ChannelOption.SO_KEEPALIVE, true);
88 b.remoteAddress(HOST, PORT);
89 b.handler(new Http2ClientFrameInitializer(sslCtx));
90
91
92 final Channel channel = b.connect().asStage().get();
93 System.out.println("Connected to [" + HOST + ':' + PORT + ']');
94
95 final Http2ClientStreamFrameResponseHandler streamFrameResponseHandler =
96 new Http2ClientStreamFrameResponseHandler();
97
98 final Http2StreamChannelBootstrap streamChannelBootstrap = new Http2StreamChannelBootstrap(channel);
99 final Http2StreamChannel streamChannel = streamChannelBootstrap.open().asStage().get();
100 streamChannel.pipeline().addLast(streamFrameResponseHandler);
101
102
103 final DefaultHttp2Headers headers = new DefaultHttp2Headers();
104 headers.method("GET");
105 headers.path(PATH);
106 headers.scheme(SSL? "https" : "http");
107 final Http2HeadersFrame headersFrame = new DefaultHttp2HeadersFrame(headers, true);
108 streamChannel.writeAndFlush(headersFrame);
109 System.out.println("Sent HTTP/2 GET request to " + PATH);
110
111
112 if (!streamFrameResponseHandler.responseSuccessfullyCompleted()) {
113 System.err.println("Did not get HTTP/2 response in expected time.");
114 }
115
116 System.out.println("Finished HTTP/2 request, will close the connection.");
117
118
119 channel.close().asStage().sync();
120 } finally {
121 clientWorkerGroup.shutdownGracefully();
122 }
123 }
124
125 }