protected override void InitChannel(IChannel channel)
        {
            IHttp2Connection connection = new DefaultHttp2Connection(false);

            _connectionHandler = new HttpToHttp2ConnectionHandlerBuilder()
            {
                FrameListener = new DelegatingDecompressorFrameListener(
                    connection,
                    new InboundHttp2ToHttpAdapterBuilder(connection)
                {
                    MaxContentLength    = _maxContentLength,
                    IsPropagateSettings = true
                }.Build()),
                FrameLogger = Logger,
                Connection  = connection
            }.Build();
            _responseHandler = new HttpResponseHandler();
            _settingsHandler = new Http2SettingsHandler(channel.NewPromise());
            if (_cert != null)
            {
                ConfigureSsl(channel);
            }
            else
            {
                ConfigureClearText(channel);
            }
        }
Пример #2
0
        public void ServerCreatePushShouldSucceedOnLocalEndpointWhenMaxAllowedStreamsExceeded()
        {
            server = new DefaultHttp2Connection(true, 0);
            server.Local.SetMaxActiveStreams(1);
            IHttp2Stream requestStream = server.Remote.CreateStream(3, false);

            Assert.NotNull(server.Local.ReservePushStream(2, requestStream));
        }
 private void InitController(bool autoRefillConnectionWindow)
 {
     _connection = new DefaultHttp2Connection(false);
     controller  = new DefaultHttp2LocalFlowController(_connection, DefaultHttp2LocalFlowController.DefaultWindowUpdateRatio, autoRefillConnectionWindow);
     controller.FrameWriter(_frameWriter.Object);
     _connection.Local.FlowController = controller;
     _connection.Local.CreateStream(STREAM_ID, false);
     controller.SetChannelHandlerContext(_ctx.Object);
 }
Пример #4
0
 public void ClientCreatePushShouldFailOnRemoteEndpointWhenMaxAllowedStreamsExceeded()
 {
     Assert.Throws <StreamException>(() =>
     {
         client = new DefaultHttp2Connection(false, 0);
         client.Remote.SetMaxActiveStreams(1);
         IHttp2Stream requestStream = client.Remote.CreateStream(2, false);
         client.Remote.ReservePushStream(4, requestStream);
     });
 }
Пример #5
0
 public void ServerCreatePushShouldFailOnRemoteEndpointWhenMaxAllowedStreamsExceeded()
 {
     Assert.Throws <Http2Exception>(() =>
     {
         server = new DefaultHttp2Connection(true, 0);
         server.Remote.SetMaxActiveStreams(1);
         IHttp2Stream requestStream = server.Remote.CreateStream(3, false);
         server.Remote.ReservePushStream(2, requestStream);
     });
 }
Пример #6
0
        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);
        }
Пример #7
0
        public DefaultHttp2ConnectionTest(EventLoopGroupFixture fixture)
        {
            this.fixture = fixture;

            this.clientListener = new Mock <IHttp2ConnectionListener>();
            this.clientListener
            .Setup(x => x.OnStreamClosed(It.IsAny <IHttp2Stream>()))
            .Callback <IHttp2Stream>(stream => Assert.True(stream.Id > 0));
            this.clientListener
            .Setup(x => x.OnStreamRemoved(It.IsAny <IHttp2Stream>()))
            .Callback <IHttp2Stream>(stream => Assert.True(stream.Id > 0));
            this.clientListener2 = new Mock <IHttp2ConnectionListener>();

            this.server = new DefaultHttp2Connection(true);
            this.client = new DefaultHttp2Connection(false);
            this.client.AddListener(this.clientListener.Object);
        }
Пример #8
0
        public DefaultHttp2ConnectionTest()
        {
            _group = new DefaultEventLoopGroup(2);

            _clientListener = new Mock <IHttp2ConnectionListener>();
            _clientListener
            .Setup(x => x.OnStreamClosed(It.IsAny <IHttp2Stream>()))
            .Callback <IHttp2Stream>(stream => Assert.True(stream.Id > 0));
            _clientListener
            .Setup(x => x.OnStreamRemoved(It.IsAny <IHttp2Stream>()))
            .Callback <IHttp2Stream>(stream => Assert.True(stream.Id > 0));
            _clientListener2 = new Mock <IHttp2ConnectionListener>();

            _server = new DefaultHttp2Connection(true);
            _client = new DefaultHttp2Connection(false);
            _client.AddListener(_clientListener.Object);
        }
Пример #9
0
        private static void ConfigureHttp2(IChannelHandlerContext ctx)
        {
            var connection = new DefaultHttp2Connection(true);
            InboundHttp2ToHttpAdapter listener = new InboundHttp2ToHttpAdapterBuilder(connection)
            {
                IsPropagateSettings   = true,
                IsValidateHttpHeaders = false,
                MaxContentLength      = MAX_CONTENT_LENGTH
            }.Build();

            ctx.Pipeline.AddLast(new HttpToHttp2ConnectionHandlerBuilder()
            {
                FrameListener = listener,
                // FrameLogger = TilesHttp2ToHttpHandler.logger,
                Connection = connection
            }.Build());

            ctx.Pipeline.AddLast(new Http2RequestHandler());
        }
        protected virtual void SetInitialChannelPipeline(IChannel ch)
        {
            var p          = ch.Pipeline;
            var connection = new DefaultHttp2Connection(false);

            this.clientHandler = new Http2ConnectionHandlerBuilder()
            {
                FrameListener = new InboundHttp2ToHttpAdapterBuilder(connection)
                {
                    MaxContentLength = this.maxContentLength,
                }.Build(),
                Connection = connection,
                GracefulShutdownTimeout = TimeSpan.Zero
            }.Build();
            p.AddLast(this.clientHandler);

            this.clientDelegator = new HttpResponseDelegator(this.clientListener.Object, this.clientLatch, this.clientLatch2);
            p.AddLast(this.clientDelegator);
        }
        protected virtual void SetInitialServerChannelPipeline(IChannel ch)
        {
            this.serverConnectedChannel = ch;
            var p          = ch.Pipeline;
            var connection = new DefaultHttp2Connection(true);

            this.serverHandler = new Http2ConnectionHandlerBuilder()
            {
                FrameListener = new InboundHttp2ToHttpAdapterBuilder(connection)
                {
                    MaxContentLength      = this.maxContentLength,
                    IsValidateHttpHeaders = true,
                    IsPropagateSettings   = true,
                }.Build(),
                Connection = connection,
                GracefulShutdownTimeout = TimeSpan.Zero
            }.Build();
            p.AddLast(this.serverHandler);

            this.serverDelegator = new HttpResponseDelegator(this.serverListener.Object, this.serverLatch, this.serverLatch2);
            p.AddLast(this.serverDelegator);
            this.settingsDelegator = new HttpSettingsDelegator(this.settingsListener.Object, this.settingsLatch);
            p.AddLast(this.settingsDelegator);
        }
        public Http2ControlFrameLimitEncoderTest()
        {
            _writer   = new Mock <IHttp2FrameWriter>();
            _ctx      = new Mock <IChannelHandlerContext>();
            _channel  = new Mock <IChannel>();
            _unsafe   = new Mock <IChannelUnsafe>();
            _config   = new Mock <IChannelConfiguration>();
            _executor = new Mock <IEventExecutor>();

            _numWrites = 0;

            var configuration   = new Mock <IHttp2FrameWriterConfiguration>();
            var frameSizePolicy = new Mock <IHttp2FrameSizePolicy>();

            _writer.SetupGet(x => x.Configuration).Returns(configuration.Object);
            configuration.SetupGet(x => x.FrameSizePolicy).Returns(frameSizePolicy.Object);
            frameSizePolicy.SetupGet(x => x.MaxFrameSize).Returns(Http2CodecUtil.DefaultMaxFrameSize);

            _writer
            .Setup(x => x.WriteRstStreamAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <int>(),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IPromise>((ctx, streamId, errorCode, p) =>
            {
                return(HandlePromise(p, 3).Task);
            });
            _writer
            .Setup(x => x.WriteSettingsAckAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, IPromise>((ctx, p) =>
            {
                return(HandlePromise(p, 1).Task);
            });
            _writer
            .Setup(x => x.WritePingAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <bool>(),
                       It.IsAny <long>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, bool, long, IPromise>((ctx, ack, data, p) =>
            {
                var promise = HandlePromise(p, 3);
                if (ack == false)
                {
                    promise.TryComplete();
                }
                return(promise.Task);
            });
            _writer
            .Setup(x => x.WriteGoAwayAsync(
                       It.IsAny <IChannelHandlerContext>(),
                       It.IsAny <int>(),
                       It.IsAny <Http2Error>(),
                       It.IsAny <IByteBuffer>(),
                       It.IsAny <IPromise>()))
            .Returns <IChannelHandlerContext, int, Http2Error, IByteBuffer, IPromise>((ctx, streamId, errCode, debugData, p) =>
            {
                ReferenceCountUtil.Release(debugData);
                _goAwayPromises.AddLast​(p);
                return(p.Task);
            });
            IHttp2Connection connection = new DefaultHttp2Connection(false);

            connection.Remote.FlowController = new DefaultHttp2RemoteFlowController(connection);
            connection.Local.FlowController  = new DefaultHttp2LocalFlowController(connection).FrameWriter(_writer.Object);

            DefaultHttp2ConnectionEncoder defaultEncoder =
                new DefaultHttp2ConnectionEncoder(connection, _writer.Object);

            _encoder = new Http2ControlFrameLimitEncoder(defaultEncoder, 2);
            DefaultHttp2ConnectionDecoder decoder =
                new DefaultHttp2ConnectionDecoder(connection, _encoder, (new Mock <IHttp2FrameReader>()).Object);
            var builder = new Http2ConnectionHandlerBuilder();

            builder.FrameListener = (new Mock <IHttp2FrameListener>()).Object;
            Http2ConnectionHandler handler = builder.Codec(decoder, _encoder).Build();

            // Set LifeCycleManager on _encoder and decoder
            _ctx.SetupGet(x => x.Channel).Returns(_channel.Object);
            _ctx.SetupGet(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);
            _channel.SetupGet(x => x.Allocator).Returns(UnpooledByteBufferAllocator.Default);
            _executor.SetupGet(x => x.InEventLoop).Returns(true);
            _ctx.Setup(x => x.NewPromise()).Returns(() => NewPromise());
            _ctx.SetupGet(x => x.Executor).Returns(_executor.Object);
            _channel.SetupGet(x => x.IsActive).Returns(false);
            _channel.SetupGet(x => x.Configuration).Returns(_config.Object);
            _channel.SetupGet(x => x.IsWritable).Returns(true);
            _channel.SetupGet(x => x.BytesBeforeUnwritable).Returns(long.MaxValue);
            _config.SetupGet(x => x.WriteBufferHighWaterMark).Returns(int.MaxValue);
            _config.SetupGet(x => x.MessageSizeEstimator).Returns(DefaultMessageSizeEstimator.Default);
            ChannelMetadata metadata = new ChannelMetadata(false, 16);

            _channel.SetupGet(x => x.Metadata).Returns(metadata);
            _channel.SetupGet(x => x.Unsafe).Returns(_unsafe.Object);
            handler.HandlerAdded(_ctx.Object);
        }