Skip to content

Override TryComputeLength(...) in web host HttpContent implementations #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/System.Web.Http.WebHost/HttpControllerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,17 @@ public Task WriteToStreamAsync(Stream stream, TransportContext context)
return SerializeToStreamAsync(stream, context);
}

public bool TryCalculateLength(out long length)
public Task<Stream> GetContentReadStreamAsync()
{
return TryComputeLength(out length);
return CreateContentReadStreamAsync();
}

public Task<Stream> 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;
}
}

Expand Down Expand Up @@ -762,7 +765,10 @@ protected override Task<Stream> 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;
}
}
}
Expand Down
31 changes: 22 additions & 9 deletions test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> unused;
Assert.False(headers.TryGetValues("Content-Length", out unused));
}

[Fact]
public void ConvertRequest_Copies_Headers_And_Content_Headers()
{
Expand Down Expand Up @@ -130,8 +148,10 @@ public void ConvertRequest_AddsOwinEnvironment_WhenPresentInHttpContext()
{
HttpRequestBase stubRequest = CreateStubRequestBase("IgnoreMethod", ignoreStream);
IDictionary<string, object> expectedEnvironment = new Dictionary<string, object>();
IDictionary items = new Hashtable();
items.Add(HttpControllerHandler.OwinEnvironmentHttpContextKey, expectedEnvironment);
IDictionary items = new Hashtable
{
{ HttpControllerHandler.OwinEnvironmentHttpContextKey, expectedEnvironment }
};
HttpContextBase context = CreateStubContextBase(stubRequest, items);

// Act
Expand Down Expand Up @@ -1946,13 +1966,6 @@ private static HttpResponseBase CreateStubResponseBase()
return new Mock<HttpResponseBase>().Object;
}

private static HttpResponseBase CreateStubResponseBase(CancellationToken clientDisconnectedToken)
{
Mock<HttpResponseBase> mock = new Mock<HttpResponseBase>();
mock.Setup(r => r.ClientDisconnectedToken).Returns(clientDisconnectedToken);
return mock.Object;
}

private static HttpResponseBase CreateStubResponseBase(Stream outputStream)
{
Mock<HttpResponseBase> responseBaseMock = new Mock<HttpResponseBase>();
Expand Down