Skip to content

Commit 523f3aa

Browse files
authored
Override TryComputeLength(...) in web host HttpContent implementations (#212)
- #197 - other implementations in this repo are already fine
1 parent 37fce36 commit 523f3aa

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/System.Web.Http.WebHost/HttpControllerHandler.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -717,14 +717,17 @@ public Task WriteToStreamAsync(Stream stream, TransportContext context)
717717
return SerializeToStreamAsync(stream, context);
718718
}
719719

720-
public bool TryCalculateLength(out long length)
720+
public Task<Stream> GetContentReadStreamAsync()
721721
{
722-
return TryComputeLength(out length);
722+
return CreateContentReadStreamAsync();
723723
}
724724

725-
public Task<Stream> GetContentReadStreamAsync()
725+
protected override bool TryComputeLength(out long length)
726726
{
727-
return CreateContentReadStreamAsync();
727+
// Do not attempt to calculate length because SeekableBufferedRequestStream (for example)
728+
// may report 0 until the underlying Stream has been read to end.
729+
length = 0L;
730+
return false;
728731
}
729732
}
730733

@@ -762,7 +765,10 @@ protected override Task<Stream> CreateContentReadStreamAsync()
762765

763766
protected override bool TryComputeLength(out long length)
764767
{
765-
return StreamContent.TryCalculateLength(out length);
768+
// Do not attempt to calculate length because SeekableBufferedRequestStream (for example)
769+
// may report 0 until the underlying Stream has been read to end.
770+
length = 0L;
771+
return false;
766772
}
767773
}
768774
}

test/System.Web.Http.WebHost.Test/HttpControllerHandlerTest.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ public void ConvertRequest_Creates_HttpRequestMessage_For_All_HttpMethods(HttpMe
7373
Assert.Equal(httpMethod, request.Method);
7474
}
7575

76+
[Fact]
77+
public void ConvertRequest_DoesNotAddContentLength()
78+
{
79+
// Arrange
80+
HttpContextBase contextBase = CreateStubContextBase("Get", new MemoryStream());
81+
82+
// Act
83+
HttpRequestMessage request = HttpControllerHandler.ConvertRequest(contextBase);
84+
85+
// Assert
86+
var headers = request.Content.Headers;
87+
Assert.NotNull(headers);
88+
Assert.Null(headers.ContentLength);
89+
90+
IEnumerable<string> unused;
91+
Assert.False(headers.TryGetValues("Content-Length", out unused));
92+
}
93+
7694
[Fact]
7795
public void ConvertRequest_Copies_Headers_And_Content_Headers()
7896
{
@@ -130,8 +148,10 @@ public void ConvertRequest_AddsOwinEnvironment_WhenPresentInHttpContext()
130148
{
131149
HttpRequestBase stubRequest = CreateStubRequestBase("IgnoreMethod", ignoreStream);
132150
IDictionary<string, object> expectedEnvironment = new Dictionary<string, object>();
133-
IDictionary items = new Hashtable();
134-
items.Add(HttpControllerHandler.OwinEnvironmentHttpContextKey, expectedEnvironment);
151+
IDictionary items = new Hashtable
152+
{
153+
{ HttpControllerHandler.OwinEnvironmentHttpContextKey, expectedEnvironment }
154+
};
135155
HttpContextBase context = CreateStubContextBase(stubRequest, items);
136156

137157
// Act
@@ -1946,13 +1966,6 @@ private static HttpResponseBase CreateStubResponseBase()
19461966
return new Mock<HttpResponseBase>().Object;
19471967
}
19481968

1949-
private static HttpResponseBase CreateStubResponseBase(CancellationToken clientDisconnectedToken)
1950-
{
1951-
Mock<HttpResponseBase> mock = new Mock<HttpResponseBase>();
1952-
mock.Setup(r => r.ClientDisconnectedToken).Returns(clientDisconnectedToken);
1953-
return mock.Object;
1954-
}
1955-
19561969
private static HttpResponseBase CreateStubResponseBase(Stream outputStream)
19571970
{
19581971
Mock<HttpResponseBase> responseBaseMock = new Mock<HttpResponseBase>();

0 commit comments

Comments
 (0)