protected override void InitChannel(IChannel channel) { var pipeline = channel.Pipeline; if (_cert is object) { var tlsSettings = new ClientTlsSettings(_targetHost) { ApplicationProtocols = new List <SslApplicationProtocol>(new[] { SslApplicationProtocol.Http2, SslApplicationProtocol.Http11 }) }.AllowAnyServerCertificate(); pipeline.AddLast("tls", new TlsHandler(tlsSettings)); } var build = Http2FrameCodecBuilder.ForClient(); build.InitialSettings = Http2Settings.DefaultSettings(); // this is the default, but shows it can be changed. Http2FrameCodec http2FrameCodec = build.Build(); pipeline.AddLast(http2FrameCodec); pipeline.AddLast(new Http2MultiplexHandler(new SimpleChannelInboundHandler0())); }
protected override void InitChannel(IChannel ch) { var pipeline = ch.Pipeline; if (_cert is object) { pipeline.AddLast("tls", new TlsHandler( stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true), new ClientTlsSettings(_targetHost) { ApplicationProtocols = new List <SslApplicationProtocol>(new[] { SslApplicationProtocol.Http2, //SslApplicationProtocol.Http11 }) } )); } var build = Http2FrameCodecBuilder.ForClient(); build.InitialSettings = Http2Settings.DefaultSettings(); // this is the default, but shows it can be changed. Http2FrameCodec http2FrameCodec = build.Build(); pipeline.AddLast(http2FrameCodec); pipeline.AddLast(new Http2MultiplexHandler(new SimpleChannelInboundHandler0())); }
private void AsyncSettingsAck0(Http2FrameCodec codec, IChannelHandler multiplexer) { // The client expects 2 settings frames. One from the connection setup and one from this test. CountdownEvent serverAckOneLatch = new CountdownEvent(1); CountdownEvent serverAckAllLatch = new CountdownEvent(2); CountdownEvent clientSettingsLatch = new CountdownEvent(2); CountdownEvent serverConnectedChannelLatch = new CountdownEvent(1); AtomicReference <IChannel> serverConnectedChannelRef = new AtomicReference <IChannel>(); _sb = new ServerBootstrap(); _sb.Group(new MultithreadEventLoopGroup(1), new MultithreadEventLoopGroup()); _sb.Channel <TcpServerSocketChannel>(); _sb.ChildHandler(new ActionChannelInitializer <IChannel>(ch => { ch.Pipeline.AddLast(codec); if (multiplexer != null) { ch.Pipeline.AddLast(multiplexer); } ch.Pipeline.AddLast(new TestServerInboundHandler(serverAckOneLatch, serverAckAllLatch, serverConnectedChannelLatch, serverConnectedChannelRef)); })); var loopback = IPAddress.IPv6Loopback; _serverChannel = _sb.BindAsync(loopback, 0).GetAwaiter().GetResult(); _bs = new Bootstrap(); _bs.Group(new MultithreadEventLoopGroup()); _bs.Channel <TcpSocketChannel>(); _bs.Handler(new ActionChannelInitializer <IChannel>(ch => { var builder = Http2MultiplexCodecBuilder.ForClient(DISCARD_HANDLER.Instance); builder.AutoAckSettingsFrame = false; ch.Pipeline.AddLast(builder.Build()); ch.Pipeline.AddLast(new TestClientInboundHandler(clientSettingsLatch)); })); var port = ((IPEndPoint)_serverChannel.LocalAddress).Port; var ccf = _bs.ConnectAsync(loopback, port); _clientChannel = ccf.GetAwaiter().GetResult(); serverConnectedChannelLatch.Wait(); _serverConnectedChannel = serverConnectedChannelRef.Value; _serverConnectedChannel.WriteAndFlushAsync(new DefaultHttp2SettingsFrame(new Http2Settings() .MaxConcurrentStreams(10))).GetAwaiter().GetResult(); clientSettingsLatch.Wait(); // We expect a timeout here because we want to asynchronously generate the SETTINGS ACK below. Assert.False(serverAckOneLatch.Wait(TimeSpan.FromMilliseconds(300))); // We expect 2 settings frames, the initial settings frame during connection establishment and the setting frame // written in this test. We should ack both of these settings frames. _clientChannel.WriteAndFlushAsync(DefaultHttp2SettingsAckFrame.Instance).GetAwaiter().GetResult(); _clientChannel.WriteAndFlushAsync(DefaultHttp2SettingsAckFrame.Instance).GetAwaiter().GetResult(); serverAckAllLatch.Wait(); }
public void FlowControlShouldBeResilientToMissingStreams() { IHttp2Connection conn = new DefaultHttp2Connection(true); IHttp2ConnectionEncoder enc = new DefaultHttp2ConnectionEncoder(conn, new DefaultHttp2FrameWriter()); IHttp2ConnectionDecoder dec = new DefaultHttp2ConnectionDecoder(conn, enc, new DefaultHttp2FrameReader()); Http2FrameCodec codec = new Http2FrameCodec(enc, dec, new Http2Settings(), false); EmbeddedChannel em = new EmbeddedChannel(codec); // We call #consumeBytes on a stream id which has not been seen yet to emulate the case // where a stream is deregistered which in reality can happen in response to a RST. Assert.False(codec.ConsumeBytes(1, 1)); Assert.True(em.FinishAndReleaseAll()); Assert.True(true); }
private void SetUp(Http2FrameCodecBuilder frameCodecBuilder, Http2Settings initialRemoteSettings) { // Some tests call this method twice. Once with JUnit's @Before and once directly to pass special settings. // This call ensures that in case of two consecutive calls to setUp(), the previous channel is shutdown and // ByteBufs are released correctly. Dispose0(); _frameWriter = Http2TestUtil.MockedFrameWriter(); var builder = frameCodecBuilder.FrameWriter(_frameWriter.Object); builder.FrameLogger = new Http2FrameLogger(Common.Internal.Logging.InternalLogLevel.TRACE); builder.InitialSettings = initialRemoteSettings; _frameCodec = frameCodecBuilder.Build(); _inboundHandler = new LastInboundHandler(); _channel = new EmbeddedChannel(); _frameInboundWriter = new Http2FrameInboundWriter(_channel); //channel.Connect(new InetSocketAddress(0)); _channel.Pipeline.AddLast(_frameCodec); _channel.Pipeline.AddLast(_inboundHandler); _channel.Pipeline.FireChannelActive(); // Handshake _frameWriter.Verify( x => x.WriteSettingsAsync( It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx), It.IsAny <Http2Settings>(), It.IsAny <IPromise>())); _frameWriter.VerifyNoOtherCalls(); _channel.WriteInbound(Http2CodecUtil.ConnectionPrefaceBuf()); _frameInboundWriter.WriteInboundSettings(initialRemoteSettings); _frameWriter.Verify( x => x.WriteSettingsAckAsync( It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx), It.IsAny <IPromise>())); _frameInboundWriter.WriteInboundSettingsAck(); var settingsFrame = _inboundHandler.ReadInbound <IHttp2SettingsFrame>(); Assert.NotNull(settingsFrame); var settingsAckFrame = _inboundHandler.ReadInbound <IHttp2SettingsAckFrame>(); Assert.NotNull(settingsAckFrame); }