Exemple #1
0
        public void TestEncodeDataEndWithTrailersAsClient()
        {
            EmbeddedChannel  ch       = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
            IByteBuffer      hello    = Unpooled.CopiedBuffer("hello world", Encoding.UTF8);
            ILastHttpContent trailers = new DefaultLastHttpContent(hello, true);
            HttpHeaders      headers  = trailers.TrailingHeaders;

            headers.Set((AsciiString)"key", "value");
            Assert.True(ch.WriteOutbound(trailers));

            var dataFrame = ch.ReadOutbound <IHttp2DataFrame>();

            try
            {
                Assert.Equal("hello world", dataFrame.Content.ToString(Encoding.UTF8));
                Assert.False(dataFrame.IsEndStream);
            }
            finally
            {
                dataFrame.Release();
            }

            IHttp2HeadersFrame headerFrame = ch.ReadOutbound <IHttp2HeadersFrame>();

            Assert.Equal("value", headerFrame.Headers.Get((AsciiString)"key", null));
            Assert.True(headerFrame.IsEndStream);

            Assert.Null(ch.ReadOutbound <object>());
            Assert.False(ch.Finish());
        }
Exemple #2
0
        public async Task SendEofToAllSubscriberAndClose()
        {
            try
            {
                if (RtmpConfig.Instance.IsSaveFlvFile && _flvOutput != null)
                {
                    try
                    {
                        _flvOutput.Close();
                    }
                    catch (IOException e)
                    {
                        _flvOutput.Close();
                        logger.Error("close file  failed", _flvOutput);
                    }
                }
                foreach (var subscriber in _subscribers)
                {
                    await subscriber.Value.WriteAndFlushAsync(UserControlMessageEvent.StreamEOF(Constants.DEFAULT_STREAM_ID));
                }

                foreach (var subscriber in _httpFLvSubscribers)
                {
                    var content = new DefaultLastHttpContent();
                    await subscriber.WriteAndFlushAsync(EmptyLastHttpContent.Default);
                }
            }
            catch
            {
            }
        }
Exemple #3
0
        public void AggregateWithTrailer()
        {
            var aggregator = new HttpObjectAggregator(1024 * 1024);
            var ch         = new EmbeddedChannel(aggregator);
            var message    = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Get, "http://localhost");

            message.Headers.Set((AsciiString)"X-Test", true);
            HttpUtil.SetTransferEncodingChunked(message, true);
            var chunk1  = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test")));
            var chunk2  = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test2")));
            var trailer = new DefaultLastHttpContent();

            trailer.TrailingHeaders.Set((AsciiString)"X-Trailer", true);

            Assert.False(ch.WriteInbound(message));
            Assert.False(ch.WriteInbound(chunk1));
            Assert.False(ch.WriteInbound(chunk2));

            // this should trigger a channelRead event so return true
            Assert.True(ch.WriteInbound(trailer));
            Assert.True(ch.Finish());
            var aggregatedMessage = ch.ReadInbound <IFullHttpRequest>();

            Assert.NotNull(aggregatedMessage);

            Assert.Equal(chunk1.Content.ReadableBytes + chunk2.Content.ReadableBytes, HttpUtil.GetContentLength(aggregatedMessage));
            Assert.Equal(bool.TrueString, aggregatedMessage.Headers.Get((AsciiString)"X-Test", null)?.ToString());
            Assert.Equal(bool.TrueString, aggregatedMessage.TrailingHeaders.Get((AsciiString)"X-Trailer", null)?.ToString());
            CheckContentBuffer(aggregatedMessage);
            var last = ch.ReadInbound <object>();

            Assert.Null(last);
        }
Exemple #4
0
        public void FormEncodeIncorrect()
        {
            var content = new DefaultLastHttpContent(Unpooled.CopiedBuffer(
                                                         Encoding.ASCII.GetBytes("project=netty&&project=netty")));
            var req     = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/");
            var decoder = new HttpPostRequestDecoder(req);

            Assert.Throws <ErrorDataDecoderException>(() => decoder.Offer(content));
            decoder.Destroy();
            content.Release();
        }
Exemple #5
0
        public void ChunkedContentWithTrailingHeader()
        {
            var ch = new EmbeddedChannel(new HttpContentCompressor());

            ch.WriteInbound(NewRequest());

            var res = new DefaultHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK);

            res.Headers.Set(HttpHeaderNames.TransferEncoding, HttpHeaderValues.Chunked);
            ch.WriteOutbound(res);

            AssertEncodedResponse(ch);

            ch.WriteOutbound(new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("Hell"))));
            ch.WriteOutbound(new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("o, w"))));
            var content = new DefaultLastHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("orld")));

            content.TrailingHeaders.Set((AsciiString)"X-Test", (AsciiString)"Netty");
            ch.WriteOutbound(content);

            var chunk = ch.ReadOutbound <IHttpContent>();

            Assert.Equal($"1f8b08000000000000{Platform}f248cdc901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
            chunk.Release();

            chunk = ch.ReadOutbound <IHttpContent>();
            Assert.Equal("cad7512807000000ffff", ByteBufferUtil.HexDump(chunk.Content));
            chunk.Release();

            chunk = ch.ReadOutbound <IHttpContent>();
            Assert.Equal("ca2fca4901000000ffff", ByteBufferUtil.HexDump(chunk.Content));
            chunk.Release();

            chunk = ch.ReadOutbound <IHttpContent>();
            Assert.Equal("0300c2a99ae70c000000", ByteBufferUtil.HexDump(chunk.Content));
            chunk.Release();

            var lastChunk = ch.ReadOutbound <ILastHttpContent>();

            Assert.NotNull(lastChunk);
            Assert.Equal("Netty", lastChunk.TrailingHeaders.Get((AsciiString)"X-Test", null).ToString());
            Assert.Equal(DecoderResult.Success, chunk.Result);
            lastChunk.Release();

            var last = ch.ReadOutbound();

            Assert.Null(last);
        }
Exemple #6
0
        public void TestEncodeTrailersAsClient()
        {
            EmbeddedChannel  ch       = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
            ILastHttpContent trailers = new DefaultLastHttpContent(Unpooled.Empty, true);
            HttpHeaders      headers  = trailers.TrailingHeaders;

            headers.Set((AsciiString)"key", "value");
            Assert.True(ch.WriteOutbound(trailers));

            IHttp2HeadersFrame headerFrame = ch.ReadOutbound <IHttp2HeadersFrame>();

            Assert.Equal("value", headerFrame.Headers.Get((AsciiString)"key", null));
            Assert.True(headerFrame.IsEndStream);

            Assert.Null(ch.ReadOutbound <object>());
            Assert.False(ch.Finish());
        }
        public void ChunkedContentWithTrailingHeader()
        {
            var ch = new EmbeddedChannel(new TestEncoder());

            ch.WriteInbound(new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, "/"));

            var res = new DefaultHttpResponse(HttpVersion.Http11, HttpResponseStatus.OK);

            res.Headers.Set(HttpHeaderNames.TransferEncoding, HttpHeaderValues.Chunked);
            ch.WriteOutbound(res);

            AssertEncodedResponse(ch);

            ch.WriteOutbound(new DefaultHttpContent(Unpooled.WrappedBuffer(new byte[3])));
            ch.WriteOutbound(new DefaultHttpContent(Unpooled.WrappedBuffer(new byte[2])));
            var content = new DefaultLastHttpContent(Unpooled.WrappedBuffer(new byte[1]));

            content.TrailingHeaders.Set((AsciiString)"X-Test", (AsciiString)"Netty");
            ch.WriteOutbound(content);

            var chunk = ch.ReadOutbound <IHttpContent>();

            Assert.Equal("3", chunk.Content.ToString(Encoding.ASCII));
            chunk.Release();

            chunk = ch.ReadOutbound <IHttpContent>();
            Assert.Equal("2", chunk.Content.ToString(Encoding.ASCII));
            chunk.Release();

            chunk = ch.ReadOutbound <IHttpContent>();
            Assert.Equal("1", chunk.Content.ToString(Encoding.ASCII));
            chunk.Release();

            var last = ch.ReadOutbound <ILastHttpContent>();

            Assert.NotNull(last);
            Assert.False(last.Content.IsReadable());
            Assert.Equal("Netty", last.TrailingHeaders.Get((AsciiString)"X-Test", null).ToString());
            Assert.Equal(DecoderResult.Success, res.Result);
            last.Release();

            var next = ch.ReadOutbound <object>();

            Assert.Null(next);
        }
        private void EmptyContent(bool chunked, bool trailers)
        {
            HttpRequestEncoder encoder = new HttpRequestEncoder();
            EmbeddedChannel    channel = new EmbeddedChannel(encoder);
            var request = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/");

            if (chunked)
            {
                HttpUtil.SetTransferEncodingChunked(request, true);
            }
            Assert.True(channel.WriteOutbound(request));

            var contentBuffer = Unpooled.Buffer();

            Assert.True(channel.WriteOutbound(new DefaultHttpContent(contentBuffer)));

            var lastContentBuffer = Unpooled.Buffer();
            var last = new DefaultLastHttpContent(lastContentBuffer);

            if (trailers)
            {
                last.TrailingHeaders.Set((AsciiString)"X-Netty-Test", "true");
            }
            Assert.True(channel.WriteOutbound(last));

            // Ensure we only produce ByteBuf instances.
            var head = channel.ReadOutbound <IByteBuffer>();

            Assert.True(head.Release());

            var content = channel.ReadOutbound <IByteBuffer>();

            content.Release();

            var lastContent = channel.ReadOutbound <IByteBuffer>();

            lastContent.Release();
            Assert.False(channel.Finish());
        }
Exemple #9
0
        public void TestEncodeDataEndAsClient()
        {
            EmbeddedChannel  ch    = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
            IByteBuffer      hello = Unpooled.CopiedBuffer("hello world", Encoding.UTF8);
            ILastHttpContent end   = new DefaultLastHttpContent(hello, true);

            Assert.True(ch.WriteOutbound(end));

            var dataFrame = ch.ReadOutbound <IHttp2DataFrame>();

            try
            {
                Assert.Equal("hello world", dataFrame.Content.ToString(Encoding.UTF8));
                Assert.True(dataFrame.IsEndStream);
            }
            finally
            {
                dataFrame.Release();
            }

            Assert.Null(ch.ReadOutbound <object>());
            Assert.False(ch.Finish());
        }
Exemple #10
0
        public void ChunkedRequestWithBodyAndTrailingHeaders()
        {
            string        text            = "foooooo";
            string        text2           = "goooo";
            List <string> receivedBuffers = new List <string>();

            this.serverListener
            .Setup(x => x.OnDataRead(
                       It.IsAny <IChannelHandlerContext>(),
                       It.Is <int>(v => v == 3),
                       It.IsAny <IByteBuffer>(),
                       It.Is <int>(v => v == 0),
                       It.Is <bool>(v => v == false)))
            .Returns <IChannelHandlerContext, int, IByteBuffer, int, bool>((ctx, id, buf, p, e) =>
            {
                lock (receivedBuffers)
                {
                    receivedBuffers.Add(buf.ToString(Encoding.UTF8));
                }
                return(0);
            });
            this.BootstrapEnv(4, 1, 1);
            IHttpRequest request = new DefaultHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post,
                                                          "http://[email protected]:5555/example");
            HttpHeaders httpHeaders = request.Headers;

            httpHeaders.Set(HttpHeaderNames.Host, "www.example.org:5555");
            httpHeaders.Add(HttpHeaderNames.TransferEncoding, "chunked");
            httpHeaders.Add(AsciiString.Of("foo"), AsciiString.Of("goo"));
            httpHeaders.Add(AsciiString.Of("foo"), AsciiString.Of("goo2"));
            httpHeaders.Add(AsciiString.Of("foo2"), AsciiString.Of("goo2"));
            var http2Headers = new DefaultHttp2Headers()
            {
                Method    = new AsciiString("POST"),
                Path      = new AsciiString("/example"),
                Authority = new AsciiString("www.example.org:5555"),
                Scheme    = new AsciiString("http")
            };

            http2Headers.Add(new AsciiString("foo"), new AsciiString("goo"));
            http2Headers.Add(new AsciiString("foo"), new AsciiString("goo2"));
            http2Headers.Add(new AsciiString("foo2"), new AsciiString("goo2"));

            DefaultHttpContent httpContent     = new DefaultHttpContent(Unpooled.CopiedBuffer(text, Encoding.UTF8));
            ILastHttpContent   lastHttpContent = new DefaultLastHttpContent(Unpooled.CopiedBuffer(text2, Encoding.UTF8));

            lastHttpContent.TrailingHeaders.Add(AsciiString.Of("trailing"), AsciiString.Of("bar"));

            IHttp2Headers http2TrailingHeaders = new DefaultHttp2Headers
            {
                { new AsciiString("trailing"), new AsciiString("bar") }
            };

            var writePromise       = this.NewPromise();
            var writeFuture        = this.clientChannel.WriteAsync(request, writePromise);
            var contentPromise     = this.NewPromise();
            var contentFuture      = this.clientChannel.WriteAsync(httpContent, contentPromise);
            var lastContentPromise = this.NewPromise();
            var lastContentFuture  = this.clientChannel.WriteAsync(lastHttpContent, lastContentPromise);

            this.clientChannel.Flush();

            Assert.True(writePromise.Task.Wait(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));
            Assert.True(writePromise.IsSuccess);
            Assert.True(writeFuture.Wait(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));
            Assert.True(writeFuture.IsSuccess());

            Assert.True(contentPromise.Task.Wait(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));
            Assert.True(contentPromise.IsSuccess);
            Assert.True(contentFuture.Wait(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));
            Assert.True(contentFuture.IsSuccess());

            Assert.True(lastContentPromise.Task.Wait(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));
            Assert.True(lastContentPromise.IsSuccess);
            Assert.True(lastContentFuture.Wait(TimeSpan.FromSeconds(WAIT_TIME_SECONDS)));
            Assert.True(lastContentFuture.IsSuccess());

            this.AwaitRequests();
            this.serverListener.Verify(
                x => x.OnHeadersRead(
                    It.IsAny <IChannelHandlerContext>(),
                    It.Is <int>(v => v == 3),
                    It.Is <IHttp2Headers>(v => v.Equals(http2Headers)),
                    It.Is <int>(v => v == 0),
                    It.IsAny <short>(),
                    It.IsAny <bool>(),
                    It.Is <int>(v => v == 0),
                    It.Is <bool>(v => v == false)));
            this.serverListener.Verify(
                x => x.OnDataRead(
                    It.IsAny <IChannelHandlerContext>(),
                    It.Is <int>(v => v == 3),
                    It.IsAny <IByteBuffer>(),
                    It.Is <int>(v => v == 0),
                    It.Is <bool>(v => v == false)));
            this.serverListener.Verify(
                x => x.OnHeadersRead(
                    It.IsAny <IChannelHandlerContext>(),
                    It.Is <int>(v => v == 3),
                    It.Is <IHttp2Headers>(v => v.Equals(http2TrailingHeaders)),
                    It.Is <int>(v => v == 0),
                    It.IsAny <short>(),
                    It.IsAny <bool>(),
                    It.Is <int>(v => v == 0),
                    It.Is <bool>(v => v == true)));
            lock (receivedBuffers)
            {
                Assert.Single(receivedBuffers);
                Assert.Equal(text + text2, receivedBuffers[0]);
            }
        }