Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix handling of large Content-Lengths in managed HttpListener
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed May 25, 2017
1 parent a649e98 commit 096ef77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ internal void AddHeader(string header)
string val = header.Substring(colon + 1).Trim();
if (name.Equals("content-length", StringComparison.OrdinalIgnoreCase))
{
long parsedContentLength = long.Parse(val);
// To match Windows behavior:
// Content lengths >= 0 and <= long.MaxValue are accepted as is.
// Content lengths > long.MaxValue and <= ulong.MaxValue are treated as 0.
// Content lengths < 0 cause the requests to fail.
// Other input is a failure, too.
long parsedContentLength =
ulong.TryParse(val, out ulong parsedUlongContentLength) ? (parsedUlongContentLength <= long.MaxValue ? (long)parsedUlongContentLength : 0) :
long.Parse(val);
if (parsedContentLength < 0 || (_clSet && parsedContentLength != _contentLength))
{
_context.ErrorMessage = "Invalid Content-Length.";
Expand Down
5 changes: 3 additions & 2 deletions src/System.Net.HttpListener/tests/HttpListenerRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ public async Task ContentEncoding_NoBody_ReturnsDefault()
}

[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
[InlineData("POST", "Content-Length: 9223372036854775807", 9223372036854775807)]
// [ActiveIssue(20232, TestPlatforms.AnyUnix)] [InlineData("POST", "Content-Length: 9223372036854775808", 0)]
[InlineData("POST", "Content-Length: 9223372036854775807", 9223372036854775807)] // long.MaxValue
[InlineData("POST", "Content-Length: 9223372036854775808", 0)] // long.MaxValue + 1
[InlineData("POST", "Content-Length: 18446744073709551615 ", 0)] // ulong.MaxValue
[InlineData("POST", "Content-Length: 0", 0)]
[InlineData("PUT", "Content-Length: 0", 0)]
[InlineData("PUT", "Content-Length: 1", 1)]
Expand Down

0 comments on commit 096ef77

Please sign in to comment.