diff --git a/src/System.Web.Http.WebHost/HttpControllerHandler.cs b/src/System.Web.Http.WebHost/HttpControllerHandler.cs index 9a78f57f3..213f0e982 100644 --- a/src/System.Web.Http.WebHost/HttpControllerHandler.cs +++ b/src/System.Web.Http.WebHost/HttpControllerHandler.cs @@ -717,14 +717,17 @@ public Task WriteToStreamAsync(Stream stream, TransportContext context) return SerializeToStreamAsync(stream, context); } - public bool TryCalculateLength(out long length) + public Task GetContentReadStreamAsync() { - return TryComputeLength(out length); + return CreateContentReadStreamAsync(); } - public Task GetContentReadStreamAsync() + protected override bool TryComputeLength(out long length) { - return CreateContentReadStreamAsync(); + // Do not attempt to calculate length because SeekableBufferedRequestStream (for example) + // may report 0 until the underlying Stream has been read to end. + length = 0L; + return false; } } @@ -762,7 +765,10 @@ protected override Task CreateContentReadStreamAsync() protected override bool TryComputeLength(out long length) { - return StreamContent.TryCalculateLength(out length); + // Do not attempt to calculate length because SeekableBufferedRequestStream (for example) + // may report 0 until the underlying Stream has been read to end. + length = 0L; + return false; } } } diff --git a/test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs b/test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs index c360f8e5a..7a991490c 100644 --- a/test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs +++ b/test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs @@ -73,6 +73,24 @@ public void ConvertRequest_Creates_HttpRequestMessage_For_All_HttpMethods(HttpMe Assert.Equal(httpMethod, request.Method); } + [Fact] + public void ConvertRequest_DoesNotAddContentLength() + { + // Arrange + HttpContextBase contextBase = CreateStubContextBase("Get", new MemoryStream()); + + // Act + HttpRequestMessage request = HttpControllerHandler.ConvertRequest(contextBase); + + // Assert + var headers = request.Content.Headers; + Assert.NotNull(headers); + Assert.Null(headers.ContentLength); + + IEnumerable unused; + Assert.False(headers.TryGetValues("Content-Length", out unused)); + } + [Fact] public void ConvertRequest_Copies_Headers_And_Content_Headers() { @@ -130,8 +148,10 @@ public void ConvertRequest_AddsOwinEnvironment_WhenPresentInHttpContext() { HttpRequestBase stubRequest = CreateStubRequestBase("IgnoreMethod", ignoreStream); IDictionary expectedEnvironment = new Dictionary(); - IDictionary items = new Hashtable(); - items.Add(HttpControllerHandler.OwinEnvironmentHttpContextKey, expectedEnvironment); + IDictionary items = new Hashtable + { + { HttpControllerHandler.OwinEnvironmentHttpContextKey, expectedEnvironment } + }; HttpContextBase context = CreateStubContextBase(stubRequest, items); // Act @@ -1946,13 +1966,6 @@ private static HttpResponseBase CreateStubResponseBase() return new Mock().Object; } - private static HttpResponseBase CreateStubResponseBase(CancellationToken clientDisconnectedToken) - { - Mock mock = new Mock(); - mock.Setup(r => r.ClientDisconnectedToken).Returns(clientDisconnectedToken); - return mock.Object; - } - private static HttpResponseBase CreateStubResponseBase(Stream outputStream) { Mock responseBaseMock = new Mock();