diff --git a/src/protocols/ioxide.http3/Http3Connection.Streamed.cs b/src/protocols/ioxide.http3/Http3Connection.Streamed.cs index 90aee16c..ffdf77e6 100644 --- a/src/protocols/ioxide.http3/Http3Connection.Streamed.cs +++ b/src/protocols/ioxide.http3/Http3Connection.Streamed.cs @@ -91,6 +91,7 @@ private async Task ServeAsync(Func await new ReactorSwitch(Reactor); // back on the reactor, wherever the handler finished RetireBody(rs); await writer.CompleteAsync(); + StopReadingIfAnsweredEarly(rs); } catch (Exception exception) { @@ -98,6 +99,7 @@ private async Task ServeAsync(Func Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {exception.GetBaseException().Message}"); RetireBody(rs); await writer.FailAsync(); + StopReadingIfAnsweredEarly(rs); } finally { diff --git a/src/protocols/ioxide.http3/Http3Connection.cs b/src/protocols/ioxide.http3/Http3Connection.cs index 34dcfa99..09d029a3 100644 --- a/src/protocols/ioxide.http3/Http3Connection.cs +++ b/src/protocols/ioxide.http3/Http3Connection.cs @@ -25,6 +25,9 @@ public sealed partial class Http3Connection private readonly Http3Options _options; private bool _fatal; + // RFC 9114 H3_NO_ERROR: what a STOP_SENDING carries when the response did not need the rest of the request. + private const ulong H3NoError = 0x0100; + // RFC 9114 section 8.1. The code a protocol error closes the connection with. private const ulong H3GeneralProtocolError = 0x0101; private const ulong H3ClosedCriticalStream = 0x0104; @@ -90,7 +93,7 @@ private sealed class ReqStream public int HeadersLen; public bool HeadersDone; // first HEADERS decoded (later ones = trailers) - public bool Finished; // fin seen and processed + public bool Finished; // fin seen and processed, or the stream reset by this side // The stream was paced, so the engine credited none of what arrived since: the parser // credits whatever it does not hand to the reader. That includes what it drops after @@ -714,6 +717,7 @@ private void DispatchReadyAsync(Func> han Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {e.GetBaseException().Message}"); RetireBody(rs); Submit(sid, new Http3Response { Status = 500 }); + StopReadingIfAnsweredEarly(rs); Recycle(rs); continue; } @@ -723,6 +727,7 @@ private void DispatchReadyAsync(Func> han RetireBody(rs); Http3Response response = pending.Result; // once: a ValueTask is consumed by reading it Submit(sid, response); + StopReadingIfAnsweredEarly(rs); Recycle(rs); } else @@ -753,6 +758,7 @@ private async Task CompleteBufferedAsync(ValueTask pending, ReqSt return; } Submit(rs.Request.StreamId, resp); + StopReadingIfAnsweredEarly(rs); Recycle(rs); } @@ -786,6 +792,27 @@ private void RetireBody(ReqStream rs) } } + // Once the response is handed over: STOP_SENDING with H3_NO_ERROR stops an upload still arriving and leaves the client the response (RFC 9114 4.1). + private void StopReadingIfAnsweredEarly(ReqStream rs) + { + // Its fin or this side's reset ended the read side, and the peer's reset or the stream's close took it out of _requests. + if (rs.Finished || IsBroken || !_requests.ContainsKey(rs.Request.StreamId)) + { + return; + } + _quicConnection.StopReading(rs.Request.StreamId, H3NoError); + } + + // A streamed response's reset, which stops its request's read side as well. + internal void ResetResponse(long streamId, ulong code) + { + if (_requests.TryGetValue(streamId, out ReqStream? rs)) + { + rs.Finished = true; + } + _quicConnection.ResetStream(streamId, code); + } + // --- egress -------------------------------------------------------------------------------- // Encode and send one response: HEADERS frame (+ DATA frame header) in one SendStream call, diff --git a/src/protocols/ioxide.http3/Http3ResponseWriter.cs b/src/protocols/ioxide.http3/Http3ResponseWriter.cs index 31e9a1c7..8781df66 100644 --- a/src/protocols/ioxide.http3/Http3ResponseWriter.cs +++ b/src/protocols/ioxide.http3/Http3ResponseWriter.cs @@ -208,7 +208,7 @@ internal ValueTask FailAsync() _staged = 0; if (!_gone) { - _quic.ResetStream(_streamId, H3InternalError); // never on a stream the peer already stopped + _connection.ResetResponse(_streamId, H3InternalError); // never on a stream the peer already stopped } if (_staging.Length > 0) diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs index 2a072014..e14961d1 100644 --- a/tests/Ioxide.Tests.E2E/Program.cs +++ b/tests/Ioxide.Tests.E2E/Program.cs @@ -76,6 +76,7 @@ private static int Main() Nghttp2ManyHeadersTests.Register(runner); StreamedPeerGoneTests.Register(runner); EarlyAnswerTests.Register(runner); + Http3EarlyAnswerTests.Register(runner); QuicStreamingCreditTests.Register(runner); Http3BodyTests.Register(runner); Http3RequestReuseTests.Register(runner); diff --git a/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs index cacc22c6..17c84292 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs @@ -2,6 +2,7 @@ using System.Net; using ioxide; using ioxide.http2; +using ioxide.http3; using ioxide.nghttp3; using ioxide.ngtcp2; @@ -114,6 +115,39 @@ private static void RegisterHttpClient(Runner runner) using HttpClient client = H3Client(); AssertReadWhole(client, $"https://127.0.0.1:{port}/"); }, skip: !h3); + + runner.Test("h3 early answer (ioxide.http3): a streamed 413 to an upload arrives whole, and the client stops uploading", () => + { + int port = StartHttp3(connection => connection.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 413 }); + writer.Write(h3Answer); + await writer.CompleteAsync(); + })); + using HttpClient client = H3Client(); + AssertAnsweredAndStopped(client, $"https://127.0.0.1:{port}/", h3Answer, H3Stopped); + }, skip: !h3); + + runner.Test("h3 early answer (ioxide.http3): a buffered 413 to an upload arrives whole, and the client stops uploading", () => + { + int port = StartHttp3(connection => connection.RunAsync((_, _) => + new ValueTask(new Http3Response { Status = 413, Body = bufferedAnswer }))); + using HttpClient client = H3Client(); + AssertAnsweredAndStopped(client, $"https://127.0.0.1:{port}/", bufferedAnswer, H3Stopped); + }, skip: !h3); + + runner.Test("control: h3 (ioxide.http3) an upload its handler reads to the end is sent whole and answered", () => + { + int port = StartHttp3(connection => connection.RunAsync(async (_, body, writer) => + { + long read = await ReadAllAsync(body.ReadAsync); + writer.WriteHeaders(new Http3Response { Status = 200 }); + writer.Write(System.Text.Encoding.ASCII.GetBytes($"read {read}")); + await writer.CompleteAsync(); + })); + using HttpClient client = H3Client(); + AssertReadWhole(client, $"https://127.0.0.1:{port}/"); + }, skip: !h3); } // The shim's own client, so these run where msquic is missing. @@ -134,19 +168,41 @@ private static void RegisterRawClient(Runner runner) await writer.CompleteAsync(); }) : connection.RunAsync((_, _) => new ValueTask(new Nghttp3Response { Status = 413, Body = answer }))); + AssertRawAnsweredAndStopped(port, answer); + }); - using RawH3Client client = ConnectRaw(port); - client.OpenRequestStream(); - Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload)], fin: false), - "the request's headers were refused"); - - long sent = UploadUntilStopped(client); - Assert.True(sent < H3Stopped, $"the client uploaded {sent} bytes to a handler that read none of them: nothing asked it to stop"); - Assert.True(client.WaitForFin(timeoutMs: 5000), "the answer never ended"); - AssertBody(answer, DataPayload(client.Received)); + runner.Test($"h3 early answer (ioxide.http3, raw client): a {(stream ? "streamed" : "buffered")} 413 to an upload still arriving arrives whole, and the upload stops", () => + { + int port = StartHttp3(connection => stream + ? connection.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 413 }); + writer.Write(answer); + await writer.CompleteAsync(); + }) + : connection.RunAsync((_, _) => new ValueTask(new Http3Response { Status = 413, Body = answer }))); + AssertRawAnsweredAndStopped(port, answer); }); } + runner.Test("h3 early answer (ioxide.http3, raw client): a buffered 413 its handler finished off the reactor arrives whole, and the upload stops", () => + { + // Released from the test's thread, so the stack submits the answer from a reactor post, outside the pass that read the request. + var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var answered = new TaskCompletionSource(); + int port = StartHttp3(connection => connection.RunAsync(async (_, _) => + { + parked.TrySetResult(); + await answered.Task; + return new Http3Response { Status = 413, Body = answer }; + })); + AssertRawAnsweredAndStopped(port, answer, beforeUpload: client => + { + Assert.True(client.WaitFor(parked.Task, timeoutMs: 5000), "the handler never ran"); + answered.SetResult(); + }); + }); + runner.Test("h3 early answer (nghttp3, raw client): a client that refused the answer is still asked to stop uploading once its handler returns", () => { // More answer than the client's window holds, so the transport is still sending it when the refusal lands. @@ -164,20 +220,26 @@ private static void RegisterRawClient(Runner runner) await refused.Task; returned.TrySetResult((await writer.FlushAsync()).IsCompleted); })); + AssertRefusedStillStopped(port, refused, returned.Task); + }); - using RawH3Client client = ConnectRaw(port); - client.OpenRequestStream(); - Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload), .. new byte[16 * 1024]], fin: false), - "the request's headers were refused"); - client.StopSending(H3RequestCancelled); - Assert.True(client.WaitForReset(timeoutMs: 5000) == H3RequestCancelled, "the server never answered the refusal with RESET_STREAM"); - - refused.SetResult(); - Assert.True(client.WaitFor(returned.Task, timeoutMs: 10_000), "the handler never returned"); - Assert.True(returned.Task.Result, "the handler was never told the client refused its answer"); - - long sent = UploadUntilStopped(client); - Assert.True(sent < H3Stopped, $"the client uploaded {sent} more bytes after the handler returned: nothing asked it to stop"); + runner.Test("h3 early answer (ioxide.http3, raw client): a client that refused the answer is still asked to stop uploading once its handler returns", () => + { + var refused = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var returned = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + int port = StartHttp3(connection => connection.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 413 }); + for (int i = 0; i < 128; i++) + { + answer.CopyTo(writer.GetSpan(answer.Length)); + writer.Advance(answer.Length); + await writer.FlushAsync(); + } + await refused.Task; + returned.TrySetResult((await writer.FlushAsync()).IsCompleted); + })); + AssertRefusedStillStopped(port, refused, returned.Task); }); runner.Test("h3 early answer (nghttp3): a transport that ends the connection inside StopReading leaves nghttp3 nothing to call", () => @@ -202,6 +264,39 @@ private static void RegisterRawClient(Runner runner) }); } + // The raw client uploads to a handler that answers without reading: the answer arrives whole, and the upload stops. + private static void AssertRawAnsweredAndStopped(int port, byte[] answer, Action? beforeUpload = null) + { + using RawH3Client client = ConnectRaw(port); + client.OpenRequestStream(); + Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload)], fin: false), + "the request's headers were refused"); + beforeUpload?.Invoke(client); + + long sent = UploadUntilStopped(client); + Assert.True(sent < H3Stopped, $"the client uploaded {sent} bytes to a handler that read none of them: nothing asked it to stop"); + Assert.True(client.WaitForFin(timeoutMs: 5000), "the answer never ended"); + AssertBody(answer, DataPayload(client.Received)); + } + + // The client refuses the answer with STOP_SENDING, then the handler returns: its upload is still stopped. + private static void AssertRefusedStillStopped(int port, TaskCompletionSource refused, Task returned) + { + using RawH3Client client = ConnectRaw(port); + client.OpenRequestStream(); + Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload), .. new byte[16 * 1024]], fin: false), + "the request's headers were refused"); + client.StopSending(H3RequestCancelled); + Assert.True(client.WaitForReset(timeoutMs: 5000) == H3RequestCancelled, "the server never answered the refusal with RESET_STREAM"); + + refused.SetResult(); + Assert.True(client.WaitFor(returned, timeoutMs: 10_000), "the handler never returned"); + Assert.True(returned.Result, "the handler was never told the client refused its answer"); + + long sent = UploadUntilStopped(client); + Assert.True(sent < H3Stopped, $"the client uploaded {sent} more bytes after the handler returned: nothing asked it to stop"); + } + private static void AssertAnsweredAndStopped(HttpClient client, string url, byte[] answer, long stopped, bool duplex = false) { ICountedUpload upload = duplex ? new DuplexUpload(Upload) : new CountedUpload(Upload); @@ -327,12 +422,16 @@ private static int StartH2(Func serve) } }); - private static int StartH3(Func serve) + private static int StartH3(Func serve) => StartQuic(conn => serve(new Nghttp3Connection(conn))); + + private static int StartHttp3(Func serve) => StartQuic(conn => serve(new Http3Connection(conn))); + + private static int StartQuic(Func serve) { (string certPath, string keyPath) = TestCert.Ensure(); var engine = TestServer.Own(new QuicEngine(certPath, keyPath, cidLength: 8, alpn: ["h3"])); (_, int udpPort) = TestServer.StartDatagram(onDatagram: null, quicFactory: engine.CreateFactory(), - quicHandle: (_, conn) => serve(new Nghttp3Connection(conn))); + quicHandle: (_, conn) => serve(conn)); return udpPort; } diff --git a/tests/Ioxide.Tests.E2E/Protocols/Http3EarlyAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/Http3EarlyAnswerTests.cs new file mode 100644 index 00000000..fbf98c79 --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Protocols/Http3EarlyAnswerTests.cs @@ -0,0 +1,179 @@ +using System.Buffers; +using ioxide; +using ioxide.http3; + +namespace Ioxide.Tests; + +/// +/// ioxide.http3 asks a client to stop uploading the rest of a body its handler answered without reading (RFC 9114 4.1): +/// STOP_SENDING with H3_NO_ERROR once the response is handed over, and nothing for a request that ended, was reset or lost +/// its connection. Checked on a transport that logs what the stack asks of it, in order. +/// +internal static class Http3EarlyAnswerTests +{ + // Past the inline limit, so a buffered answer is two sends: its head, then its body with the fin. + private static readonly byte[] Answer = new byte[8 * 1024]; + + // A DATA frame promising 1000 bytes, 100 of them here: the rest is still to come when the handler answers. + private static readonly byte[] StillArriving = [.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(1000), .. new byte[100]]; + private static readonly byte[] Whole = [.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(100), .. new byte[100]]; + + private const ulong H3RequestCancelled = 0x10c; + + public static void Register(Runner runner) + { + runner.Test("h3 early answer (ioxide.http3, recorded): a buffered answer to an upload still arriving is followed by STOP_SENDING(H3_NO_ERROR)", () => + Assert.Equal("send, send+fin, stop 0x100", End(Start(StillArriving, ended: false, Buffered)))); + + runner.Test("h3 early answer (ioxide.http3, recorded): the 500 of a buffered handler that threw is followed by STOP_SENDING(H3_NO_ERROR)", () => + Assert.Equal("send+fin, stop 0x100", End(Start(StillArriving, ended: false, c => c.RunAsync(ThrowsAtOnce))))); + + runner.Test("h3 early answer (ioxide.http3, recorded): a buffered answer its handler awaited is followed by STOP_SENDING(H3_NO_ERROR)", () => + { + var answered = new TaskCompletionSource(); + Served served = Start(StillArriving, ended: false, c => c.RunAsync(async (_, _) => + { + await answered.Task; + return new Http3Response { Status = 413, Body = Answer }; + })); + answered.SetResult(); + Assert.Equal("send, send+fin, stop 0x100", End(served)); + }); + + runner.Test("h3 early answer (ioxide.http3, recorded): a streamed answer to an upload still arriving is followed by STOP_SENDING(H3_NO_ERROR)", () => + Assert.Equal("send, send+fin, stop 0x100", End(Start(StillArriving, ended: false, Streamed)))); + + runner.Test("h3 early answer (ioxide.http3, recorded): the 500 of a streamed handler that failed before its headers is followed by STOP_SENDING(H3_NO_ERROR)", () => + Assert.Equal("send, send+fin, stop 0x100", End(Start(StillArriving, ended: false, + c => c.RunAsync((_, _, _) => throw new InvalidOperationException("the handler failed before its headers")))))); + + runner.Test("h3 early answer (ioxide.http3, recorded): a client that refused the answer is still asked to stop uploading once its handler returns", () => + { + var refused = new TaskCompletionSource(); + Served served = Start(StillArriving, ended: false, c => c.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 413 }); + writer.Write(Answer); + await writer.FlushAsync(); + await refused.Task; + })); + served.Quic.OnSendRefused!(0); // how the engine reports the client's STOP_SENDING + refused.SetResult(); + Assert.Equal("send, send, stop 0x100", End(served)); + }); + + foreach (bool streamed in new[] { false, true }) + { + Func serve = streamed ? Streamed : Buffered; + runner.Test($"control: h3 (ioxide.http3, recorded) a {(streamed ? "streamed" : "buffered")} answer to an upload that ended is not followed by STOP_SENDING", () => + Assert.Equal("send, send+fin", End(Start(Whole, ended: true, serve)))); + } + + runner.Test("h3 early answer (ioxide.http3, recorded): an upload the client reset gets no STOP_SENDING once its handler answers", () => + { + var answered = new TaskCompletionSource(); + Served served = Start(StillArriving, ended: false, c => c.RunAsync(async (_, _) => + { + await answered.Task; + return new Http3Response { Status = 413, Body = Answer }; + })); + Assert.True(served.Quic.EnqueueStreamEvent(0, QuicStreamEvent.Reset, H3RequestCancelled), "the recv ring refused the reset"); + served.Quic.SignalDataArrived(); + answered.SetResult(); + Assert.Equal("send, send+fin", End(served)); + }); + + runner.Test("h3 early answer (ioxide.http3, recorded): a streamed handler that fails after its headers is reset once, and gets no STOP_SENDING", () => + Assert.Equal("send, send, reset 0x102", End(Start(StillArriving, ended: false, c => c.RunAsync(async (_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 200 }); + writer.Write(Answer); + await writer.FlushAsync(); + throw new InvalidOperationException("the handler failed after its headers"); + }))))); + + runner.Test("h3 early answer (ioxide.http3, recorded): a request that arrives with its connection's close gets no STOP_SENDING", () => + { + var quic = new OrderQuic(); + Assert.True(quic.EnqueueStreamData(0, StillArriving, fin: false), "the recv ring refused the request"); + quic.MarkClosed(); + Task run = Buffered(new Http3Connection(quic)); + Assert.True(run.Wait(5000), "the run loop never ended"); + Assert.Equal("send, send+fin", string.Join(", ", quic.Calls)); + }); + } + + private static Task Buffered(Http3Connection connection) + => connection.RunAsync((_, _) => new ValueTask(new Http3Response { Status = 413, Body = Answer })); + + private static Task Streamed(Http3Connection connection) + => connection.RunAsync((_, _, writer) => + { + writer.WriteHeaders(new Http3Response { Status = 413 }); + writer.Write(Answer); + return ValueTask.CompletedTask; + }); + + private static ValueTask ThrowsAtOnce(Http3Request request, Http3BodyReader body) + => throw new InvalidOperationException("the handler failed before it returned"); + + private readonly record struct Served(OrderQuic Quic, Task Run); + + // Stream 0's request, served at once: with no reactor every await here completes inline, on this thread. + private static Served Start(byte[] request, bool ended, Func serve) + { + var quic = new OrderQuic(); + Assert.True(quic.EnqueueStreamData(0, request, ended), "the recv ring refused the request"); + quic.SignalDataArrived(); + return new Served(quic, serve(new Http3Connection(quic))); + } + + private static string End(Served served) + { + served.Quic.MarkClosed(); + Assert.True(served.Run.Wait(5000), "the run loop never ended"); + return string.Join(", ", served.Quic.Calls); + } + + // What the stack asks of its transport on stream 0, in order. The read surface is the real base one. + private sealed class OrderQuic : QuicConnection + { + public readonly List Calls = []; + private long _nextUni = 3; + + public OrderQuic() + { + NegotiatedProtocol = "h3"; + } + + public override void OnDatagram(ReadOnlySpan payload, byte tos) { } + public override long GetNextTimeout(long nowMs) => long.MaxValue; + public override void OnTimer(long nowMs) { } + public override void OnEvicted(QuicEvictReason reason) { } + + public override void SendStream(long streamId, ReadOnlySpan data, bool fin) + { + if (streamId == 0) + { + Calls.Add(fin ? "send+fin" : "send"); + } + } + + public override void ResetStream(long streamId, ulong applicationErrorCode) => Calls.Add($"reset 0x{applicationErrorCode:x}"); + + public override void StopReading(long streamId, ulong applicationErrorCode) => Calls.Add($"stop 0x{applicationErrorCode:x}"); + + public override long OpenUniStream() + { + long id = _nextUni; + _nextUni += 4; + return id; + } + + public override void Close(ulong applicationErrorCode) + { + Calls.Add($"close 0x{applicationErrorCode:x}"); + MarkClosed(); + } + } +}