Пример #1
0
        public void SendRstStream()
        {
            _frameInboundWriter.WriteInboundHeaders(3, _request, 31, true);

            IHttp2Stream stream = _frameCodec.Connection.Stream(3);

            Assert.NotNull(stream);
            Assert.Equal(Http2StreamState.HalfClosedRemote, stream.State);

            IHttp2HeadersFrame inboundHeaders = _inboundHandler.ReadInbound <IHttp2HeadersFrame>();

            Assert.NotNull(inboundHeaders);
            Assert.True(inboundHeaders.IsEndStream);

            IHttp2FrameStream stream2 = inboundHeaders.Stream;

            Assert.NotNull(stream2);
            Assert.Equal(3, stream2.Id);

            _channel.WriteOutbound(new DefaultHttp2ResetFrame((Http2Error)314 /* non-standard error */)
            {
                Stream = stream2
            });
            _frameWriter.Verify(
                x => x.WriteRstStreamAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.Is <int>(v => v == 3),
                    It.Is <Http2Error>(v => v == (Http2Error)314L),
                    It.IsAny <IPromise>()));
            Assert.Equal(Http2StreamState.Closed, stream.State);
            Assert.True(_channel.IsActive);
        }
Пример #2
0
        public void StreamIdentifiersExhausted()
        {
            int maxServerStreamId = int.MaxValue - 1;

            Assert.NotNull(_frameCodec.Connection.Local.CreateStream(maxServerStreamId, false));

            IHttp2FrameStream stream = _frameCodec.NewStream();

            Assert.NotNull(stream);

            var writePromise = _channel.NewPromise();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream
            }, writePromise);

            var goAwayFrame = _inboundHandler.ReadInbound <IHttp2GoAwayFrame>();

            Assert.NotNull(goAwayFrame);
            Assert.Equal(Http2Error.NoError, goAwayFrame.ErrorCode);
            Assert.Equal(int.MaxValue, goAwayFrame.LastStreamId);
            goAwayFrame.Release();

            Assert.IsType <Http2NoMoreStreamIdsException>(writePromise.Task.Exception.InnerException);
        }
Пример #3
0
        public void WindowUpdateFrameDecrementsConsumedBytes()
        {
            _frameInboundWriter.WriteInboundHeaders(3, _request, 31, false);

            var connection = _frameCodec.Connection;
            var stream     = connection.Stream(3);

            Assert.NotNull(stream);

            var data = Unpooled.Buffer(100).WriteZero(100);

            _frameInboundWriter.WriteInboundData(3, data, 0, false);

            IHttp2HeadersFrame inboundHeaders = _inboundHandler.ReadInbound <IHttp2HeadersFrame>();

            Assert.NotNull(inboundHeaders);
            Assert.NotNull(inboundHeaders.Stream);

            IHttp2FrameStream stream2 = inboundHeaders.Stream;

            int before = connection.Local.FlowController.UnconsumedBytes(stream);
            var f      = _channel.WriteAsync(new DefaultHttp2WindowUpdateFrame(100)
            {
                Stream = stream2
            });
            int after = connection.Local.FlowController.UnconsumedBytes(stream);

            Assert.Equal(100, before - after);
            Assert.True(f.IsSuccess());
        }
Пример #4
0
            public bool Visit(IHttp2FrameStream stream)
            {
                var childChannel = (AbstractHttp2StreamChannel)
                                   ((DefaultHttp2FrameStream)stream).Attachment;

                childChannel.TrySetWritable();
                return(true);
            }
Пример #5
0
        public async Task StreamShouldBeOpenInListener()
        {
            IHttp2FrameStream stream2 = _frameCodec.NewStream();

            Assert.Equal(Http2StreamState.Idle, stream2.State);

            await _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()) { Stream = stream2 });

            Assert.Equal(Http2StreamState.Open, stream2.State);
        }
Пример #6
0
        public Http2FrameStreamException(IHttp2FrameStream stream, Http2Error error, Exception cause)
            : base(cause.Message, cause)
        {
            if (stream is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.stream);
            }

            Stream = stream;
            Error  = error;
        }
Пример #7
0
        public void IterateActiveStreams()
        {
            var builder = Http2FrameCodecBuilder.ForServer();

            builder.EncoderEnforceMaxConcurrentStreams = true;
            SetUp(builder, new Http2Settings().MaxConcurrentStreams(1));

            _frameInboundWriter.WriteInboundHeaders(3, _request, 0, false);

            IHttp2HeadersFrame headersFrame = _inboundHandler.ReadInbound <IHttp2HeadersFrame>();

            Assert.NotNull(headersFrame);

            IHttp2FrameStream activeInbond = headersFrame.Stream;

            IHttp2FrameStream activeOutbound = _frameCodec.NewStream();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = activeOutbound
            });

            IHttp2FrameStream bufferedOutbound = _frameCodec.NewStream();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = bufferedOutbound
            });

            //@SuppressWarnings("unused")
            IHttp2FrameStream idleStream = _frameCodec.NewStream();

            ISet <IHttp2FrameStream> activeStreams = new HashSet <IHttp2FrameStream>();

            bool localVisit(IHttp2FrameStream stream)
            {
                activeStreams.Add(stream);
                return(true);
            }

            _frameCodec.ForEachActiveStream(localVisit);

            Assert.Equal(2, activeStreams.Count);

            ISet <IHttp2FrameStream> expectedStreams = new HashSet <IHttp2FrameStream>();

            expectedStreams.Add(activeInbond);
            expectedStreams.Add(activeOutbound);
            Assert.Equal(expectedStreams, activeStreams);
        }
Пример #8
0
        public void StateChanges()
        {
            _frameInboundWriter.WriteInboundHeaders(1, _request, 31, true);

            IHttp2Stream stream = _frameCodec.Connection.Stream(1);

            Assert.NotNull(stream);
            Assert.Equal(Http2StreamState.HalfClosedRemote, stream.State);

            var evt = _inboundHandler.ReadInboundMessageOrUserEvent <Http2FrameStreamEvent>();

            Assert.Equal(Http2StreamState.HalfClosedRemote, evt.Stream.State);

            var inboundFrame          = _inboundHandler.ReadInbound <IHttp2StreamFrame>();
            IHttp2FrameStream stream2 = inboundFrame.Stream;

            Assert.NotNull(stream2);
            Assert.Equal(1, stream2.Id);
            Assert.Equal(inboundFrame, new DefaultHttp2HeadersFrame(_request, true, 31)
            {
                Stream = stream2
            });
            Assert.Null(_inboundHandler.ReadInbound());

            _channel.WriteOutbound(new DefaultHttp2HeadersFrame(_response, true, 27)
            {
                Stream = stream2
            });
            _frameWriter.Verify(
                x => x.WriteHeadersAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.Is <int>(v => v == 1),
                    It.Is <IHttp2Headers>(v => v.Equals(_response)),
                    It.Is <int>(v => v == 27),
                    It.Is <bool>(v => v == true),
                    It.IsAny <IPromise>()));
            _frameWriter.Verify(
                x => x.WriteRstStreamAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.IsAny <int>(),
                    It.IsAny <Http2Error>(),
                    It.IsAny <IPromise>()),
                Times.Never());

            Assert.Equal(Http2StreamState.Closed, stream.State);
            evt = _inboundHandler.ReadInboundMessageOrUserEvent <Http2FrameStreamEvent>();
            Assert.Equal(Http2StreamState.Closed, evt.Stream.State);

            Assert.True(_channel.IsActive);
        }
Пример #9
0
        [Fact]//(timeout = 5000)
        public async Task NewOutboundStream()
        {
            IHttp2FrameStream stream = _frameCodec.NewStream();

            Assert.NotNull(stream);
            Assert.False(Http2CodecUtil.IsStreamIdValid(stream.Id));

            var listenerExecuted = new TaskCompletionSource();

            await _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers(), false) { Stream = stream });

            Assert.True(Http2CodecUtil.IsStreamIdValid(stream.Id));

            var data = Unpooled.Buffer().WriteZero(100);
            await _channel.WriteAndFlushAsync(new DefaultHttp2DataFrame(data) { Stream = stream });
        }
Пример #10
0
        /**
         * Sends a "Hello World" DATA frame to the client.
         */
        private static void SendResponse(IChannelHandlerContext ctx, IHttp2FrameStream stream, IByteBuffer payload)
        {
            // Send a frame for the response status
            IHttp2Headers headers = new DefaultHttp2Headers()
            {
                Status = HttpResponseStatus.OK.CodeAsText
            };

            ctx.WriteAsync(new DefaultHttp2HeadersFrame(headers)
            {
                Stream = stream
            });
            ctx.WriteAsync(new DefaultHttp2DataFrame(payload, true)
            {
                Stream = stream
            });
        }
Пример #11
0
        public void WriteUnknownFrame()
        {
            IHttp2FrameStream stream = _frameCodec.NewStream();

            var buffer = Unpooled.Buffer().WriteByte(1);
            DefaultHttp2UnknownFrame unknownFrame = new DefaultHttp2UnknownFrame(
                (Http2FrameTypes)20, new Http2Flags().Ack(true), buffer);

            unknownFrame.Stream = stream;
            _channel.WriteAsync(unknownFrame);

            _frameWriter.Verify(
                x => x.WriteFrameAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.Is <Http2FrameTypes>(v => v == unknownFrame.FrameType),
                    It.Is <int>(v => v == unknownFrame.Stream.Id),
                    It.Is <Http2Flags>(v => v == unknownFrame.Flags),
                    It.Is <IByteBuffer>(v => v.Equals(buffer)),
                    It.IsAny <IPromise>()));
        }
Пример #12
0
        /**
         * If receive a frame with end-of-stream set, send a pre-canned response.
         */
        private static void OnDataRead(IChannelHandlerContext ctx, IHttp2DataFrame data)
        {
            IHttp2FrameStream stream = data.Stream;

            if (data.IsEndStream)
            {
                SendResponse(ctx, stream, data.Content);
            }
            else
            {
                // We do not send back the response to the remote-peer, so we need to release it.
                data.Release();
            }

            // Update the flowcontroller
            ctx.WriteAsync(new DefaultHttp2WindowUpdateFrame(data.InitialFlowControlledBytes)
            {
                Stream = stream
            });
        }
Пример #13
0
        public void DoNotLeakOnFailedInitializationForChannels()
        {
            // We use a limit of 1 and then increase it step by step.
            SetUp(Http2FrameCodecBuilder.ForServer(), new Http2Settings().MaxConcurrentStreams(2));

            IHttp2FrameStream stream1 = _frameCodec.NewStream();
            IHttp2FrameStream stream2 = _frameCodec.NewStream();

            var stream1HeaderPromise = _channel.NewPromise();
            var stream2HeaderPromise = _channel.NewPromise();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream1
            },
                                        stream1HeaderPromise);
            _channel.RunPendingTasks();

            _frameInboundWriter.WriteInboundGoAway(stream1.Id, 0L, Unpooled.Empty);

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream2
            },
                                        stream2HeaderPromise);
            _channel.RunPendingTasks();

            try
            {
                stream1HeaderPromise.Task.GetAwaiter().GetResult();
            }
            catch
            {
                Assert.False(true);
            }
            Assert.True(stream2HeaderPromise.IsCompleted);

            Assert.Equal(0, _frameCodec.NumInitializingStreams);
            Assert.False(_channel.FinishAndReleaseAll());
        }
Пример #14
0
        public void WindowUpdateMayFail()
        {
            _frameInboundWriter.WriteInboundHeaders(3, _request, 31, false);
            var          connection = _frameCodec.Connection;
            IHttp2Stream stream     = connection.Stream(3);

            Assert.NotNull(stream);

            IHttp2HeadersFrame inboundHeaders = _inboundHandler.ReadInbound <IHttp2HeadersFrame>();

            Assert.NotNull(inboundHeaders);

            IHttp2FrameStream stream2 = inboundHeaders.Stream;

            // Fails, cause trying to return too many bytes to the flow controller
            var f = _channel.WriteAsync(new DefaultHttp2WindowUpdateFrame(100)
            {
                Stream = stream2
            });

            Assert.True(f.IsCompleted);
            Assert.False(f.IsSuccess());
            Assert.IsType <Http2Exception>(f.Exception.InnerException);
        }
Пример #15
0
 public bool Equals(IHttp2FrameStream other)
 {
     return(ReferenceEquals(this, other));
 }
Пример #16
0
 private Http2FrameStreamEvent(IHttp2FrameStream stream, EventType type)
 {
     Stream = stream;
     Type   = type;
 }
Пример #17
0
        public void MultipleNewOutboundStreamsShouldBeBuffered()
        {
            var builder = Http2FrameCodecBuilder.ForServer();

            builder.EncoderEnforceMaxConcurrentStreams = true;
            // We use a limit of 1 and then increase it step by step.
            SetUp(builder, new Http2Settings().MaxConcurrentStreams(1));

            IHttp2FrameStream stream1 = _frameCodec.NewStream();
            IHttp2FrameStream stream2 = _frameCodec.NewStream();
            IHttp2FrameStream stream3 = _frameCodec.NewStream();

            IPromise promise1 = _channel.NewPromise();
            IPromise promise2 = _channel.NewPromise();
            IPromise promise3 = _channel.NewPromise();

            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream1
            }, promise1);
            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream2
            }, promise2);
            _channel.WriteAndFlushAsync(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers())
            {
                Stream = stream3
            }, promise3);

            Assert.True(Http2CodecUtil.IsStreamIdValid(stream1.Id));
            _channel.RunPendingTasks();
            Assert.True(Http2CodecUtil.IsStreamIdValid(stream2.Id));

            if (promise1.IsCompleted)
            {
                Assert.True(promise1.Task.IsSuccess());
            }
            else
            {
                try
                {
                    promise1.Task.GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    Assert.False(true);
                }
            }
            Assert.False(promise2.IsCompleted);
            Assert.False(promise3.IsCompleted);

            // Increase concurrent streams limit to 2
            _frameInboundWriter.WriteInboundSettings(new Http2Settings().MaxConcurrentStreams(2));
            _channel.Flush();

            // As we increased the limit to 2 we should have also succeed the second frame.
            if (promise2.IsCompleted)
            {
                Assert.True(promise2.Task.IsSuccess());
            }
            else
            {
                try
                {
                    promise2.Task.GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    Assert.False(true);
                }
            }
            Assert.False(promise3.IsCompleted);

            _frameInboundWriter.WriteInboundSettings(new Http2Settings().MaxConcurrentStreams(3));
            _channel.Flush();

            // With the max streams of 3 all streams should be succeed now.
            if (promise3.IsCompleted)
            {
                Assert.True(promise3.Task.IsSuccess());
            }
            else
            {
                try
                {
                    promise3.Task.GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    Assert.False(true);
                }
            }

            Assert.False(_channel.FinishAndReleaseAll());
        }
Пример #18
0
 public static Http2FrameStreamEvent StateChanged(IHttp2FrameStream stream)
 {
     return(new Http2FrameStreamEvent(stream, EventType.State));
 }
Пример #19
0
        public void EntityRequestEntityResponse()
        {
            _frameInboundWriter.WriteInboundHeaders(1, _request, 0, false);

            IHttp2Stream stream = _frameCodec.Connection.Stream(1);

            Assert.NotNull(stream);
            Assert.Equal(Http2StreamState.Open, stream.State);

            IHttp2HeadersFrame inboundHeaders = _inboundHandler.ReadInbound <IHttp2HeadersFrame>();
            IHttp2FrameStream  stream2        = inboundHeaders.Stream;

            Assert.NotNull(stream2);
            Assert.Equal(1, stream2.Id);
            Assert.Equal(new DefaultHttp2HeadersFrame(_request, false)
            {
                Stream = stream2
            }, inboundHeaders);
            Assert.Null(_inboundHandler.ReadInbound());

            IByteBuffer hello = Http2TestUtil.BB("hello");

            _frameInboundWriter.WriteInboundData(1, hello, 31, true);
            IHttp2DataFrame inboundData = _inboundHandler.ReadInbound <IHttp2DataFrame>();
            IHttp2DataFrame expected    = new DefaultHttp2DataFrame(Http2TestUtil.BB("hello"), true, 31)
            {
                Stream = stream2
            };

            Http2TestUtil.AssertEqualsAndRelease(expected, inboundData);

            Assert.Null(_inboundHandler.ReadInbound());

            _channel.WriteOutbound(new DefaultHttp2HeadersFrame(_response, false)
            {
                Stream = stream2
            });
            _frameWriter.Verify(
                x => x.WriteHeadersAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.Is <int>(v => v == 1),
                    It.Is <IHttp2Headers>(v => v.Equals(_response)),
                    It.Is <int>(v => v == 0),
                    It.Is <bool>(v => v == false),
                    It.IsAny <IPromise>()));

            _channel.WriteOutbound(new DefaultHttp2DataFrame(Http2TestUtil.BB("world"), true, 27)
            {
                Stream = stream2
            });
            var outboundData = new ArgumentCaptor <IByteBuffer>();

            _frameWriter.Verify(
                x => x.WriteDataAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.Is <int>(v => v == 1),
                    It.Is <IByteBuffer>(v => outboundData.Capture(v)),
                    It.Is <int>(v => v == 27),
                    It.Is <bool>(v => v == true),
                    It.IsAny <IPromise>()));

            IByteBuffer bb = Http2TestUtil.BB("world");

            Assert.Equal(bb, outboundData.GetValue());
            Assert.Equal(1, outboundData.GetValue().ReferenceCount);
            bb.Release();
            outboundData.GetValue().Release();

            _frameWriter.Verify(
                x => x.WriteRstStreamAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.IsAny <int>(),
                    It.IsAny <Http2Error>(),
                    It.IsAny <IPromise>()), Times.Never());
            Assert.True(_channel.IsActive);
        }
Пример #20
0
 public static Http2FrameStreamEvent WritabilityChanged(IHttp2FrameStream stream)
 {
     return(new Http2FrameStreamEvent(stream, EventType.Writability));
 }