Beispiel #1
0
        public void OversizedRequest()
        {
            var aggregator = new HttpObjectAggregator(4);
            var ch         = new EmbeddedChannel(aggregator);
            var message    = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Put, "http://localhost");
            var chunk1     = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test")));
            var chunk2     = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test2")));
            EmptyLastHttpContent chunk3 = EmptyLastHttpContent.Default;

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

            var response = ch.ReadOutbound <IFullHttpResponse>();

            Assert.Equal(HttpResponseStatus.RequestEntityTooLarge, response.Status);
            Assert.Equal("0", response.Headers.Get(HttpHeaderNames.ContentLength, null));
            Assert.False(ch.Open);

            try
            {
                Assert.False(ch.WriteInbound(chunk3));
                Assert.True(false, "Shoud not get here, expecting exception thrown.");
            }
            catch (Exception e)
            {
                Assert.True(e is ClosedChannelException);
            }

            Assert.False(ch.Finish());
        }
Beispiel #2
0
        public void AggregateTransferEncodingChunked()
        {
            var aggregator = new HttpObjectAggregator(1024 * 1024);
            var ch         = new EmbeddedChannel(aggregator);

            var message = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Put, "http://localhost");

            message.Headers.Set((AsciiString)"X-Test", true);
            message.Headers.Set((AsciiString)"Transfer-Encoding", (AsciiString)"Chunked");
            var chunk1 = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test")));
            var chunk2 = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test2")));
            EmptyLastHttpContent chunk3 = EmptyLastHttpContent.Default;

            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(chunk3));
            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));
            CheckContentBuffer(aggregatedMessage);
            var last = ch.ReadInbound <object>();

            Assert.Null(last);
        }
Beispiel #3
0
        public void RequestAfterOversized100ContinueAndDecoder()
        {
            var ch = new EmbeddedChannel(new HttpRequestDecoder(), new HttpObjectAggregator(15));

            // Write first request with Expect: 100-continue.
            var message = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Put, "http://localhost");

            HttpUtil.Set100ContinueExpected(message, true);
            HttpUtil.SetContentLength(message, 16);

            var chunk1 = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("some")));
            var chunk2 = new DefaultHttpContent(Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("test")));
            EmptyLastHttpContent chunk3 = EmptyLastHttpContent.Default;

            // Send a request with 100-continue + large Content-Length header value.
            Assert.False(ch.WriteInbound(message));

            // The aggregator should respond with '413'.
            var response = ch.ReadOutbound <IFullHttpResponse>();

            Assert.Equal(HttpResponseStatus.RequestEntityTooLarge, response.Status);
            Assert.Equal((AsciiString)"0", response.Headers.Get(HttpHeaderNames.ContentLength, null));

            // An ill-behaving client could continue to send data without a respect, and such data should be discarded.
            Assert.False(ch.WriteInbound(chunk1));

            // The aggregator should not close the connection because keep-alive is on.
            Assert.True(ch.Open);

            // Now send a valid request.
            var message2 = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Put, "http://localhost");

            Assert.False(ch.WriteInbound(message2));
            Assert.False(ch.WriteInbound(chunk2));
            Assert.True(ch.WriteInbound(chunk3));

            var fullMsg = ch.ReadInbound <IFullHttpRequest>();

            Assert.NotNull(fullMsg);

            Assert.Equal(chunk2.Content.ReadableBytes + chunk3.Content.ReadableBytes, HttpUtil.GetContentLength(fullMsg));
            Assert.Equal(HttpUtil.GetContentLength(fullMsg), fullMsg.Content.ReadableBytes);

            fullMsg.Release();
            Assert.False(ch.Finish());
        }