public Queue<FullHttpResponse> getResponses() { return initializer.getCommunicationHandler().getResponses(); }
public void sendRequest(FullHttpRequest request) { initializer.getCommunicationHandler().sendRequest(request, sslCtx, channel); }
public static NettyHttpClient newHttp2ClientWithHttp11Upgrade() { return new NettyHttpClient(null, new Http2ClientInitializer(null, Integer.MAX_VALUE)); }
@Test public void testRestPlainTextUpgrade() throws Exception { //when NettyHttpClient client = NettyHttpClient.newHttp2ClientWithHttp11Upgrade(); client.start("localhost", 8080); FullHttpRequest putValueInCacheRequest = new DefaultFullHttpRequest(HTTP_1_1, POST, "/rest/default/test", wrappedBuffer("test".getBytes(CharsetUtil.UTF_8))); client.sendRequest(putValueInCacheRequest); Queue<FullHttpResponse> responses = client.getResponses(); //then assertEquals(1, responses.size()); assertEquals(HttpResponseStatus.OK, responses.poll().status()); }
public static NettyHttpClient newHttp2ClientWithALPN(String keystorePath, String keystorePassword) throws Exception { SslContext sslContext = NettyTruststoreUtil.createTruststoreContext(keystorePath, keystorePassword.toCharArray(), ApplicationProtocolNames.HTTP_2); return new NettyHttpClient(sslContext, new Http2ClientInitializer(sslContext, Integer.MAX_VALUE)); }
@Override public void initChannel(SocketChannel ch) throws Exception { final Http2Connection connection = new DefaultHttp2Connection(false); connectionHandler = new HttpToHttp2ConnectionHandlerBuilder() .frameListener(new DelegatingDecompressorFrameListener( connection, new InboundHttp2ToHttpAdapterBuilder(connection) .maxContentLength(maxContentLength) .propagateSettings(true) .build())) .frameLogger(logger) .connection(connection) .build(); responseHandler = new Http2ResponseHandler(); settingsHandler = new Http2SettingsHandler(ch.newPromise()); if (sslCtx != null) { configureSsl(ch); } else { configureClearText(ch); } }
public BenchmarkHttpClient() { nettyHttpClient = NettyHttpClient.newHttp2ClientWithHttp11Upgrade(); http1Client = new HttpClient(); }
public void start(String address, int port, int threads, boolean http2) throws Exception { this.address = address; this.port = port; this.http2 = http2; if (http2) nettyHttpClient.start(address, port); else http1Client.start(); executor = Executors.newFixedThreadPool(threads); executorCompletionService = new ExecutorCompletionService(executor); }
public static NettyHttpClient newHttp11Client() { return new NettyHttpClient(null, new Http11ClientInitializer(null, Integer.MAX_VALUE)); }
/** * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2. */ private void configureClearText(SocketChannel ch) { HttpClientCodec sourceCodec = new HttpClientCodec(); Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec(connectionHandler); HttpClientUpgradeHandler upgradeHandler = new HttpClientUpgradeHandler(sourceCodec, upgradeCodec, maxContentLength); ch.pipeline().addLast(sourceCodec, upgradeHandler, new UpgradeRequestHandler(), new UserEventLogger()); }
public BenchmarkHttpClient(String keystorePath, String keystorePassword, String trustStorePath, String trustStorePassword) throws Exception { if (!OpenSsl.isAlpnSupported()) { throw new IllegalStateException("OpenSSL is not present, can not test TLS/ALPN support"); } nettyHttpClient = NettyHttpClient.newHttp2ClientWithALPN(keystorePath, keystorePassword); SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setTrustStorePassword(trustStorePath); sslContextFactory.setTrustStorePassword(trustStorePassword); sslContextFactory.setKeyStorePath(keystorePath); sslContextFactory.setKeyStorePassword(keystorePassword); http1Client = new HttpClient(sslContextFactory); usesTLS = true; }
@BeforeMethod public void afterMethod() { if (restServer != null) { restServer.stop(); } if (client != null) { client.stop(); } }
@Override protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { ChannelPipeline p = ctx.pipeline(); p.addLast(connectionHandler); configureEndOfPipeline(p); return; } ctx.close(); throw new IllegalStateException("unknown protocol: " + protocol); } });
@Override public Queue<FullHttpResponse> getResponses() { awaitResponses(TIMEOUT, TIMEOUT_UNITS); return responses; }
@Override public void upgradeToHttp2IfNeeded() throws Exception { settingsHandler.awaitSettings(15, TimeUnit.SECONDS); }
public void start(String host, int port) throws Exception { Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(host, port); b.handler(initializer); // Start the client. channel = b.connect().syncUninterruptibly().channel(); System.out.println("Connected to [" + host + ':' + port + ']'); initializer.upgradeToHttp2IfNeeded(); }
/** * Configure the pipeline for TLS NPN negotiation to HTTP/2. */ private void configureSsl(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(sslCtx.newHandler(ch.alloc())); // We must wait for the handshake to finish and the protocol to be negotiated before configuring // the HTTP/2 components of the pipeline. pipeline.addLast(new ApplicationProtocolNegotiationHandler("") { @Override protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { ChannelPipeline p = ctx.pipeline(); p.addLast(connectionHandler); configureEndOfPipeline(p); return; } ctx.close(); throw new IllegalStateException("unknown protocol: " + protocol); } }); pipeline.addLast(new UserEventLogger()); }
@Override public void sendRequest(FullHttpRequest request, SslContext sslContext, Channel channel) { int streamId = streamCounter.getAndAdd(2); HttpScheme scheme = sslContext != null ? HttpScheme.HTTPS : HttpScheme.HTTP; request.headers().add(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), scheme.name()); request.headers().add(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId); put(streamId, channel.write(request), channel.newPromise()); channel.flush(); } }
public void stop() throws Exception { if (http2) nettyHttpClient.stop(); else http1Client.stop(); executor.shutdownNow(); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { DefaultFullHttpRequest upgradeRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/"); ctx.writeAndFlush(upgradeRequest); ctx.fireChannelActive(); // Done with this handler, remove it from the pipeline. ctx.pipeline().remove(this); configureEndOfPipeline(ctx.pipeline()); } }