Skip to content

Commit 1b21f5a

Browse files
rzikmCopilotCopilot
authored
Read GET request bodies in the HTTP/1.1 loopback helper (#134419)
## Fix The Content rows in `SendAsync_RequestWithLatin1HeaderValue_Succeeds` send a GET with `StringContent("test content")`, but the HTTP/1.1 loopback helper skipped GET bodies. It could therefore send its response and close while the client still had request bytes to send. Local packet captures using the archived build 1606484 binaries reproduced the matching Windows 10053 failure: headers plus `test cont`, then the server's complete 200 response and FIN, then the client's final `ent`, followed by a server reset. Nine of the twelve body bytes were already in the helper buffer in a failure snapshot; unread kernel bytes alone are not the explanation. The unsafe interleaving is teardown before the client's remaining upload arrives. Following review, remove the test-local server branch and fix `LoopbackServer.Connection.ReadRequestDataAsync` instead: honor Content-Length and chunked framing for GET just as for other request methods. The Latin1 test returns to its existing server path, which now reads the body before responding and closing. Its request method, raw-header assertions, response headers, non-ASCII early returns, and special WinHTTP HTTP/2 path are unchanged. Header-only callers can still use `readBody: false` and read the body separately. Add a four-row HTTP/1.1 regression theory covering Content-Length/chunked framing and immediate/deferred body reads. No product changes, retries, sleeps, timeout increases, or additional Latin1 server cases. ## Validation of current shared-helper fix Windows x64, Debug libraries with Release CoreCLR, plus .NET Framework 4.8.1 where listed. All final builds succeeded with zero warnings and zero errors. - New GET-body regression: all 4 rows failed before the helper change (expected `test content`, actual empty body); all 4 pass afterward. - Focused Sockets run: 76 passed (72 existing Latin1 rows plus 4 new regression rows), zero failures/skips. Nine of each class's eighteen Latin1 rows still return early for values above 0x7f. - Full System.Net.Http functional project: 5432 passed, 48 skips, zero failures. - Full System.Net.Http unit project: 2677 passed, 4 skips, zero failures. - WinHTTP Latin1 theory: 36 passed on current .NET and 18 passed on .NET Framework 4.8.1, zero failures/skips. - Full WinHTTP functional project: current .NET 1708 passed / 41 skips; .NET Framework 4.8.1 639 passed / 28 skips; zero failures. - Full WinHTTP unit project: 173 passed, zero failures/skips. - Full System.Net.Http.Json functional project: current .NET 90 passed and .NET Framework 4.8.1 77 passed, zero failures/skips. - Full System.Net.Requests project: 1207 passed, 8 skips, zero failures. These runs exercise existing skip conditions; this revision adds no skips to existing tests. Other operating systems and browser/WASI execution were not run locally. ## Earlier test-local candidate evidence The following measurements were taken for the superseded test-local fix at `2451ea85565a98e374e7444f5054d6df75e458dc`, not repeated for the current shared-helper implementation: | Check | Original | Earlier candidate | |---|---:|---:| | Full Latin1 theory, all four Sockets classes | 2 failures / 72 | 0 failures / 72 | | SOH/BEL/DEL Content, fresh interleaved processes | 1 x 10053 / 3600 | 0 failures / 3600 | | Ordinary ASCII Content | 400 passed | 400 passed | The original theory failures were DEL/Content in both Sockets and the inherited Sync class. This method calls `SendAsync` directly in both classes. The repeated Content/ASCII calls do not return early. All fresh repetition completed, with no 10054 errors or timeouts. The earlier candidate also passed the full System.Net.Http functional project (5428 passed, 48 skips), unit project (2677 passed, 4 skips), WinHTTP Latin1 controls (36 current .NET and 18 .NET Framework), and 40 extracted-source branch probes covering split-body completion, truncated-body EOF, incorrect Content-Length, and incorrect body, including connection closure on failures. These are local reproductions, not packet captures from the historical CI machine. Sixteen unlocalized timeouts from an earlier diagnostic run remain retained and unexplained; they are not counted as passes or claimed fixed by either implementation. Resolves #132004 > [!NOTE] > This change and description were prepared with GitHub Copilot. --------- Co-authored-by: Copilot App <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent 8e3a74e commit 1b21f5a

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -815,16 +815,13 @@ public override async Task<HttpRequestData> ReadRequestDataAsync(bool readBody =
815815
requestData.Headers.Add(new HttpHeaderData(name, value, raw: lineBytes, rawValueStart: offset + 1));
816816
}
817817

818-
if (requestData.Method != "GET")
818+
if (requestData.GetHeaderValueCount("Content-Length") != 0)
819819
{
820-
if (requestData.GetHeaderValueCount("Content-Length") != 0)
821-
{
822-
_contentLength = int.Parse(requestData.GetSingleHeaderValue("Content-Length"));
823-
}
824-
else if (requestData.GetHeaderValueCount("Transfer-Encoding") != 0 && requestData.GetSingleHeaderValue("Transfer-Encoding") == "chunked")
825-
{
826-
_contentLength = -1;
827-
}
820+
_contentLength = int.Parse(requestData.GetSingleHeaderValue("Content-Length"));
821+
}
822+
else if (requestData.GetHeaderValueCount("Transfer-Encoding") != 0 && requestData.GetSingleHeaderValue("Transfer-Encoding") == "chunked")
823+
{
824+
_contentLength = -1;
828825
}
829826

830827
if (readBody)

‎src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http1.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Net.Test.Common;
7+
using System.Text;
78
using System.Threading.Tasks;
89

910
using Xunit;
@@ -16,6 +17,36 @@ public class HttpClientHandlerTest_Http1 : HttpClientHandlerTestBase
1617
{
1718
public HttpClientHandlerTest_Http1(ITestOutputHelper output) : base(output) { }
1819

20+
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
21+
[ActiveIssue("https://github.com/dotnet/runtime/issues/129223", typeof(PlatformDetection), nameof(PlatformDetection.IsWasi))]
22+
[InlineData(false, false)]
23+
[InlineData(false, true)]
24+
[InlineData(true, false)]
25+
[InlineData(true, true)]
26+
public async Task SendAsync_GetWithContent_ServerReadsBody(bool chunked, bool readBody)
27+
{
28+
const string Content = "test content";
29+
await Http11LoopbackServerFactory.Singleton.CreateClientAndServerAsync(async uri =>
30+
{
31+
using HttpClient client = CreateHttpClient();
32+
using var request = new HttpRequestMessage(HttpMethod.Get, uri)
33+
{
34+
Content = new StringContent(Content),
35+
Version = HttpVersion.Version11
36+
};
37+
request.Headers.TransferEncodingChunked = chunked;
38+
using HttpResponseMessage response = await client.SendAsync(request);
39+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
40+
}, async server =>
41+
{
42+
await using GenericLoopbackConnection connection = await server.EstablishGenericConnectionAsync();
43+
HttpRequestData request = await connection.ReadRequestDataAsync(readBody);
44+
byte[] body = readBody ? request.Body : await connection.ReadRequestBodyAsync();
45+
Assert.Equal(Content, Encoding.UTF8.GetString(body));
46+
await connection.SendResponseAsync();
47+
});
48+
}
49+
1950
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNodeJS))]
2051
[ActiveIssue("https://github.com/dotnet/runtime/issues/101115", typeof(PlatformDetection), nameof(PlatformDetection.IsFirefox))]
2152
[ActiveIssue("https://github.com/dotnet/runtime/issues/129223", typeof(PlatformDetection), nameof(PlatformDetection.IsWasi))]

0 commit comments

Comments
 (0)