diff --git a/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs b/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs index 68358fc4..3e9703f1 100644 --- a/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs +++ b/src/clients/ioxide.httpclient/Http1/HttpClientConnection.cs @@ -472,10 +472,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) { @@ -488,11 +489,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) { @@ -506,10 +506,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 e277417f..a44780b0 100644 --- a/tests/Ioxide.Tests.Http/Program.cs +++ b/tests/Ioxide.Tests.Http/Program.cs @@ -22,6 +22,7 @@ private static int Main() RingHttpClientTests.Register(runner); HttpClientChunkedTests.Register(runner); HttpClientResponseLimitTests.Register(runner); + HttpClientHttp10Tests.Register(runner); HttpClientThreadTests.Register(runner); TlsClientTests.Register(runner);