From c30ace4766f57d992aeeafcd5b1fd81b34f6fd02 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Fri, 13 Nov 2026 12:02:56 +0000 Subject: [PATCH] httpclient: an HTTP/1.0 response with Transfer-Encoding is refused as faulty framing, and its connection closed, rather than read as chunked RFC 9112 6.1: a client that receives an HTTP/1.0 message with a Transfer-Encoding MUST treat its framing as faulty and close the connection after it. The client takes its framing from Glyph11, which had no such rule. Glyph11 0.4.1 refuses the message in its hardened parsers (dotnet-web-stack/Glyph11#91), and the client turns that into a failed request on a discarded connection, as it does any malformed head. The client moves from Glyph11 0.3.7 to 0.4.1, onto TryParseResponseHeader: TryExtractFullResponseHeaderROM is obsolete since 0.4.0, and the new entry point reports the header block's true length, so the + 1 goes. Glyph11's other response fixes since 0.3.7 come with it, among them a bare-LF head refused at once rather than waited on (Glyph11#85), status codes above 599 refused (Glyph11#80), and Content-Length lists and oversized values framed by their length (Glyph11#74, Glyph11#75, Glyph11#77). HttpClientHttp10Tests: the issue's test, a 304, and a keep-alive response whose connection must not be reused, beside the Content-Length control. On Glyph11 0.4.0 the three refusals fail; on 0.4.1 they pass. --- .../Http1/HttpClientConnection.cs | 16 +-- .../ioxide.httpclient.csproj | 2 +- .../HttpClientHttp10Tests.cs | 103 ++++++++++++++++++ tests/Ioxide.Tests.Http/Program.cs | 1 + 4 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 tests/Ioxide.Tests.Http/HttpClientHttp10Tests.cs diff --git a/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs b/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs index e53b54c9..4f82b6a6 100644 --- a/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs +++ b/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs @@ -468,10 +468,11 @@ private unsafe int AppendAvailable(HttpClientResponse response, long limit) /// /// /// Structure and hardening are Glyph11's: bare LF, obs-fold, whitespace before the colon, - /// token and field-value charsets, duplicate and malformed Content-Length, and the + /// token and field-value charsets, duplicate and malformed Content-Length, the /// Transfer-Encoding + Content-Length pair that lets a proxy and a client disagree about where - /// this response ends. What stays here is what Glyph11 has no opinion on - copying the fields - /// the caller keeps, and connection reuse. + /// this response ends, and Transfer-Encoding from an HTTP/1.0 sender, which RFC 9112 6.1 calls + /// faulty framing. What stays here is what Glyph11 has no opinion on - copying the fields the + /// caller keeps, and connection reuse. /// private unsafe int TryParseHead(HttpClientResponse response, HttpClientRequest request) { @@ -484,11 +485,10 @@ private unsafe int TryParseHead(HttpClientResponse response, HttpClientRequest r ReadOnlyMemory buffered = ReceiveBufferAsMemory(); bool complete; - int bytesRead; + int headSize; try { - complete = UltraHardenedParser.TryExtractFullResponseHeaderROM( - ref buffered, _head, in _limits, out bytesRead); + complete = UltraHardenedParser.TryParseResponseHeader(ref buffered, _head, in _limits, out headSize); } catch (HttpParseException e) { @@ -502,10 +502,6 @@ private unsafe int TryParseHead(HttpClientResponse response, HttpClientRequest r return 0; } - // Glyph11 reports one less than the block's real size - see the note in its diff harness, - // "C# returns total-1; the C ABI returns the clean total". - int headSize = bytesRead + 1; - response.Status = _head.Status; for (int i = 0; i < _head.Headers.Count; i++) diff --git a/src/clients/ioxide.httpclient/ioxide.httpclient.csproj b/src/clients/ioxide.httpclient/ioxide.httpclient.csproj index ddce4694..aee36fbd 100644 --- a/src/clients/ioxide.httpclient/ioxide.httpclient.csproj +++ b/src/clients/ioxide.httpclient/ioxide.httpclient.csproj @@ -26,7 +26,7 @@ - + diff --git a/tests/Ioxide.Tests.Http/HttpClientHttp10Tests.cs b/tests/Ioxide.Tests.Http/HttpClientHttp10Tests.cs new file mode 100644 index 00000000..e5e89c47 --- /dev/null +++ b/tests/Ioxide.Tests.Http/HttpClientHttp10Tests.cs @@ -0,0 +1,103 @@ +using System.Text; +using ioxide; +using ioxide.httpclient; + +namespace Ioxide.Tests; + +/// +/// RFC 9112 6.1: a recipient of an HTTP/1.0 message with a Transfer-Encoding "MUST treat the message as if the framing +/// is faulty, even if a Content-Length is present, and close the connection after processing the message". +/// +internal static class HttpClientHttp10Tests +{ + private static int _connections; + + public static void Register(Runner runner) + { + (string Path, string What)[] refused = + [ + ("/chunked", "an HTTP/1.0 response with Transfer-Encoding"), + ("/notmodified", "an HTTP/1.0 304 with Transfer-Encoding, though it has no body to frame"), + ]; + + foreach ((string path, string what) in refused) + { + runner.Test($"httpclient h1: refuses {what}", () => + { + int origin = TestServer.Start(OriginHandler); + int proxy = HttpClientTests.StartProxy(origin, poolSize: 1); + + (_, string body) = Client.Get(proxy, path); + Assert.True(body.StartsWith("599|") && body.Contains("HTTP/1.0"), + $"{what} should have been refused, got: {body}"); + }); + } + + runner.Test("httpclient h1: control: an HTTP/1.0 response with Content-Length is read", () => + { + int origin = TestServer.Start(OriginHandler); + int proxy = HttpClientTests.StartProxy(origin, poolSize: 1); + + (_, string body) = Client.Get(proxy, "/length"); + Assert.Equal("200|hello", body); + }); + + runner.Test("httpclient h1: an HTTP/1.0 response with Transfer-Encoding closes its connection, though it asked for keep-alive", () => + { + int origin = TestServer.Start(OriginHandler); + // Its handler skips TestServer's request-less probe, whose stray request could close the connection mid-test. + int proxy = HttpClientDnsTests.StartProxy(new HttpClientOptions { Host = "127.0.0.1", Port = (ushort)origin, PoolSize = 1 }); + + // Control: keep-alive holds an HTTP/1.0 connection, so a reuse would show. + (_, string first) = Client.Get(proxy, "/conn"); + Assert.True(first.StartsWith("200|connection "), $"expected a numbered connection, got: {first}"); + Assert.Equal(first, Client.Get(proxy, "/conn").Body); + + (_, string body) = Client.Get(proxy, "/keepalive"); + Assert.True(body.StartsWith("599|"), $"its framing is faulty, and it was read as chunked: {body}"); + + (_, string next) = Client.Get(proxy, "/conn"); + Assert.True(next.StartsWith("200|connection ") && next != first, $"its connection was reused: {next}"); + }); + } + + private static string KeptAlive(string body) + => $"HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nContent-Length: {body.Length}\r\n\r\n{body}"; + + private static async Task OriginHandler(Reactor reactor, TcpConnection connection) + { + int number = 0; + try + { + while (true) + { + RecvSnapshot snapshot = await connection.ReadAsync(); + string path = Wire.ReadPath(connection, snapshot); + if (path == "/conn" && number == 0) + { + number = Interlocked.Increment(ref _connections); + } + + connection.Write(Encoding.ASCII.GetBytes(path switch + { + "/chunked" => "HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n", + "/notmodified" => "HTTP/1.0 304 Not Modified\r\nTransfer-Encoding: chunked\r\n\r\n", + "/keepalive" => "HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n" + + "5\r\nhello\r\n0\r\n\r\n", + "/conn" => KeptAlive($"connection {number}"), + _ => "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nhello", + })); + + await connection.FlushAsync(); + if (snapshot.IsClosed) + { + return; + } + } + } + finally + { + connection.DecRef(); + } + } +} diff --git a/tests/Ioxide.Tests.Http/Program.cs b/tests/Ioxide.Tests.Http/Program.cs index bd024c2d..63e4de1d 100644 --- a/tests/Ioxide.Tests.Http/Program.cs +++ b/tests/Ioxide.Tests.Http/Program.cs @@ -21,6 +21,7 @@ private static int Main() HttpClientConnectTests.Register(runner); RingHttpClientTests.Register(runner); HttpClientChunkedTests.Register(runner); + HttpClientHttp10Tests.Register(runner); HttpClientThreadTests.Register(runner); TlsClientTests.Register(runner);