diff --git a/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c b/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c
index 3a3ed59a..bf0ac7cc 100644
--- a/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c
+++ b/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c
@@ -180,6 +180,18 @@ static int on_stream_close(nghttp2_session *session, int32_t stream_id, uint32_t
return 0;
}
+/* Asks a client to stop uploading once its answer is out (RFC 9113 8.1); nghttp2 would send a reset queued sooner ahead of the body. */
+static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
+{
+ (void)user_data;
+ if ((frame->hd.type == NGHTTP2_HEADERS || frame->hd.type == NGHTTP2_DATA) &&
+ (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) != 0 &&
+ nghttp2_session_get_stream_remote_close(session, frame->hd.stream_id) == 0) {
+ return nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, frame->hd.stream_id, NGHTTP2_NO_ERROR);
+ }
+ return 0;
+}
+
/* nghttp2 drops a response whose header block is past its limit (64 KiB) with no frame on the wire, which would leave
* the stream open for good. Only that error: a stream already gone is not to be reset again (RFC 9113 5.4.2). */
static int on_frame_not_send(nghttp2_session *session, const nghttp2_frame *frame, int lib_error_code, void *user_data)
@@ -273,6 +285,10 @@ static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_f
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, on_data_chunk_recv);
nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, on_stream_close);
nghttp2_session_callbacks_set_on_frame_not_send_callback(callbacks, on_frame_not_send);
+ if (server && manual_flow) {
+ /* Only a body-reader handler answers before its request ends, and only manual flow reports that reset's close. */
+ nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, on_frame_send);
+ }
int rv;
if (manual_flow) {
diff --git a/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so b/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so
index 01d77003..4dea68fa 100755
Binary files a/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so and b/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so differ
diff --git a/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so b/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so
index 99e30679..8f0aa55f 100755
Binary files a/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so and b/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so differ
diff --git a/tests/Ioxide.Tests.Chaos/H2cClient.cs b/tests/Ioxide.Tests.Chaos/H2cClient.cs
index 37d46c31..82f46f51 100644
--- a/tests/Ioxide.Tests.Chaos/H2cClient.cs
+++ b/tests/Ioxide.Tests.Chaos/H2cClient.cs
@@ -42,6 +42,9 @@ public enum UploadEnd
/// RST_STREAM on the stream. The error code says why.
Reset,
+ /// HEADERS, then RST_STREAM(NO_ERROR): the request was answered, and the client asked to send no more of it (RFC 9113 8.1).
+ Stopped,
+
/// GOAWAY: the whole connection ended, not just the stream.
GoAway,
@@ -53,7 +56,7 @@ public enum UploadEnd
/// POST body bytes of within the server's
/// flow-control windows, reading what arrives before sending more, until the server has answered
/// and the whole body is sent, or it resets the stream, or goes away. An answer that comes early
- /// does not stop the body: a real client still sends it, and the server still has to credit it.
+ /// does not stop the body, but a RST_STREAM(NO_ERROR) after it does.
///
///
/// Pad every DATA frame with this many bytes. Padding counts against both windows and never
@@ -142,7 +145,8 @@ public enum UploadEnd
}
else if (type == RstStream && sid == streamId && payload.Length == 4)
{
- return (UploadEnd.Reset, BinaryPrimitives.ReadUInt32BigEndian(payload), sent);
+ uint code = BinaryPrimitives.ReadUInt32BigEndian(payload);
+ return (answered && code == 0 ? UploadEnd.Stopped : UploadEnd.Reset, code, sent);
}
else if (type == Headers && sid == streamId)
{
diff --git a/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs b/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs
index 07526361..f8ecdb1b 100644
--- a/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs
+++ b/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs
@@ -371,8 +371,10 @@ private static void UnreadUploadsGiveCreditBack(bool streamedResponse)
for (int i = 1; i <= Uploads; i++)
{
+ // An unread upload that has not ended by its answer is stopped after it (RFC 9113 8.1).
(H2cClient.UploadEnd end, uint error, long sent) = client.Upload(streamId: (2 * i) - 1, UploadBytes, path: path);
- Assert.True(end == H2cClient.UploadEnd.Answered && sent == UploadBytes,
+ bool stopped = end == H2cClient.UploadEnd.Stopped && path != "/read";
+ Assert.True(stopped || (end == H2cClient.UploadEnd.Answered && sent == UploadBytes),
$"{path}: upload {i} of {Uploads}, {(i - 1) * UploadBytes / 1024} KiB already sent, ended {end} "
+ $"(error 0x{error:x}) after {sent} of {UploadBytes} bytes");
}
diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs
index fae2ded9..28137e1b 100644
--- a/tests/Ioxide.Tests.E2E/Program.cs
+++ b/tests/Ioxide.Tests.E2E/Program.cs
@@ -74,6 +74,7 @@ private static int Main()
Http2StreamedFaultTests.Register(runner);
Http2BufferedFaultTests.Register(runner);
Nghttp2ManyHeadersTests.Register(runner);
+ Nghttp2EarlyAnswerTests.Register(runner);
StreamedPeerGoneTests.Register(runner);
EarlyAnswerTests.Register(runner);
Http3EarlyAnswerTests.Register(runner);
diff --git a/tests/Ioxide.Tests.E2E/Protocols/Nghttp2EarlyAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/Nghttp2EarlyAnswerTests.cs
new file mode 100644
index 00000000..97cd201b
--- /dev/null
+++ b/tests/Ioxide.Tests.E2E/Protocols/Nghttp2EarlyAnswerTests.cs
@@ -0,0 +1,215 @@
+using System.Buffers;
+using System.Net;
+using ioxide;
+using ioxide.nghttp2;
+
+namespace Ioxide.Tests;
+
+/// An ioxide.nghttp2 answer that ends before its request does, with .NET's HttpClient: the answer arrives whole, and the upload stops (RFC 9113 8.1).
+internal static class Nghttp2EarlyAnswerTests
+{
+ // Far more than any window lets through before the client hears it should stop.
+ private const long Upload = 64L << 20;
+
+ // A control's upload, read whole by its handler.
+ private const long ReadUpload = 8L << 20;
+
+ // Measured at 64 KiB, the connection window; a reset stream takes no more credit, so its window bounds what follows.
+ private const long Stopped = 4L << 20;
+
+ public static void Register(Runner runner)
+ {
+ // Inside the client's 64 KiB stream window: this client credits an answer only once its upload ends, and the reset follows the END_STREAM.
+ byte[] answer = Pattern(32 * 1024);
+
+ runner.Test("h2 early answer (nghttp2): a streamed 413 to an upload arrives whole, and the client stops uploading", () =>
+ {
+ int port = Start(connection => connection.RunAsync((_, _, writer) =>
+ {
+ writer.WriteHeaders(new Nghttp2Response { Status = 413 });
+ writer.Write(answer);
+ return ValueTask.CompletedTask;
+ }));
+ AssertAnsweredAndStopped(port, answer);
+ });
+
+ runner.Test("h2 early answer (nghttp2): a buffered 413 to an upload arrives whole, and the client stops uploading", () =>
+ {
+ int port = Start(connection => connection.RunAsync((_, _) =>
+ new ValueTask(new Nghttp2Response { Status = 413, Body = answer })));
+ AssertAnsweredAndStopped(port, answer);
+ });
+
+ runner.Test("h2 early answer (nghttp2): a bodyless 413 to an upload arrives, and the client stops uploading", () =>
+ {
+ int port = Start(connection => connection.RunAsync((_, _) =>
+ new ValueTask(new Nghttp2Response { Status = 413 })));
+ AssertAnsweredAndStopped(port, []);
+ });
+
+ runner.Test("h2 early answer (nghttp2): control: an upload its handler reads to the end is sent whole and answered", () =>
+ {
+ int port = Start(connection => connection.RunAsync(async (_, body, writer) =>
+ {
+ long read = 0;
+ ReadOnlyMemory chunk;
+ while (!(chunk = await body.ReadAsync()).IsEmpty)
+ {
+ read += chunk.Length;
+ }
+ writer.WriteHeaders(new Nghttp2Response { Status = 200 });
+ writer.Write(System.Text.Encoding.ASCII.GetBytes($"read {read}"));
+ }));
+
+ using HttpClient client = H2Client();
+ var upload = new CountedUpload(ReadUpload);
+ Task<(HttpStatusCode Status, byte[] Body)> answered = PostAsync(client, $"http://127.0.0.1:{port}/", upload);
+
+ Assert.True(answered.Wait(100_000), "the answer never arrived");
+ Assert.Equal(HttpStatusCode.OK, answered.Result.Status);
+ Assert.Equal($"read {ReadUpload}", System.Text.Encoding.ASCII.GetString(answered.Result.Body));
+ Assert.Equal(ReadUpload, upload.Ended.Result);
+ });
+ }
+
+ private static void AssertAnsweredAndStopped(int port, byte[] answer)
+ {
+ using HttpClient client = H2Client();
+ var upload = new CountedUpload(Upload);
+ Task<(HttpStatusCode Status, byte[] Body)> answered = PostAsync(client, $"http://127.0.0.1:{port}/", upload);
+
+ Assert.True(answered.Wait(100_000), "the answer never arrived");
+ Assert.Equal(HttpStatusCode.RequestEntityTooLarge, answered.Result.Status);
+ Assert.True(answered.Result.Body.AsSpan().SequenceEqual(answer),
+ $"the answer arrived {answered.Result.Body.Length} bytes long, of {answer.Length}, or with different content");
+
+ long sent = upload.Ended.Result;
+ Assert.True(sent < Stopped,
+ $"the client uploaded {sent} of {Upload} bytes to a handler that read none of them: nothing asked it to stop");
+ }
+
+ // The answer, once the upload has ended as well: at its last byte, or where the client gave up on the rest.
+ private static async Task<(HttpStatusCode Status, byte[] Body)> PostAsync(HttpClient client, string url, CountedUpload upload)
+ {
+ try
+ {
+ using HttpResponseMessage response = await client.PostAsync(url, upload);
+ byte[] body = await response.Content.ReadAsByteArrayAsync();
+
+ // Held open until then: disposing it would have the client cancel the upload itself.
+ if (await Task.WhenAny(upload.Ended, Task.Delay(30_000)) != upload.Ended)
+ {
+ throw new Exception($"the upload neither finished nor stopped, at {upload.Sent} bytes");
+ }
+ return (response.StatusCode, body);
+ }
+ catch (HttpRequestException exception)
+ {
+ // The default message names neither the protocol error nor the side that raised it.
+ Exception cause = exception.GetBaseException();
+ throw new Exception($"{cause.GetType().Name}: {cause.Message}", exception);
+ }
+ }
+
+ private static int Start(Func serve)
+ => TestServer.Start(async (_, conn) =>
+ {
+ try
+ {
+ await serve(new Nghttp2Connection(conn));
+ }
+ finally
+ {
+ conn.DecRef();
+ }
+ });
+
+ // Cleartext HTTP/2 with prior knowledge.
+ private static HttpClient H2Client() => new(new SocketsHttpHandler())
+ {
+ DefaultRequestVersion = HttpVersion.Version20,
+ DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact,
+ Timeout = TimeSpan.FromSeconds(60),
+ };
+
+ // Position-dependent, so an answer reassembled out of order fails on content and not only length.
+ private static byte[] Pattern(int length)
+ {
+ byte[] bytes = new byte[length];
+ for (int i = 0; i < length; i++)
+ {
+ bytes[i] = (byte)((i * 31) ^ (i >> 8));
+ }
+ return bytes;
+ }
+
+ // A StreamContent, so not duplex: the client reads its answer once the upload has ended or been stopped.
+ private sealed class CountedUpload : StreamContent
+ {
+ private readonly Source _source;
+ private readonly TaskCompletionSource _ended = new(TaskCreationOptions.RunContinuationsAsynchronously);
+
+ public CountedUpload(long length) : this(new Source(length))
+ {
+ }
+
+ private CountedUpload(Source source) : base(source, 64 * 1024)
+ {
+ _source = source;
+ Headers.ContentLength = source.Size;
+ }
+
+ public long Sent => _source.Taken;
+
+ public Task Ended => _ended.Task;
+
+ // The overload a client sends through; StreamContent routes a subclass on to the one without a token.
+ protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
+ {
+ try
+ {
+ await base.SerializeToStreamAsync(stream, context, cancellationToken);
+ }
+ finally
+ {
+ _ended.TrySetResult(Sent);
+ }
+ }
+ }
+
+ // Zeros, as many as the upload is long, counted as they are taken.
+ private sealed class Source(long size) : Stream
+ {
+ private long _taken;
+
+ public long Size => size;
+
+ public long Taken => Interlocked.Read(ref _taken);
+
+ public override int Read(Span buffer)
+ {
+ int count = (int)Math.Min(buffer.Length, size - Taken);
+ buffer[..count].Clear();
+ Interlocked.Add(ref _taken, count);
+ return count;
+ }
+
+ public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));
+
+ public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default)
+ => new(Read(buffer.Span));
+
+ public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
+ => Task.FromResult(Read(buffer.AsSpan(offset, count)));
+
+ public override bool CanRead => true;
+ public override bool CanSeek => false;
+ public override bool CanWrite => false;
+ public override long Length => throw new NotSupportedException();
+ public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
+ public override void Flush() { }
+ public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
+ public override void SetLength(long value) => throw new NotSupportedException();
+ public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
+ }
+}
diff --git a/tests/Ioxide.Tests.Unit/Nghttp2EarlyAnswerTests.cs b/tests/Ioxide.Tests.Unit/Nghttp2EarlyAnswerTests.cs
new file mode 100644
index 00000000..c4706b64
--- /dev/null
+++ b/tests/Ioxide.Tests.Unit/Nghttp2EarlyAnswerTests.cs
@@ -0,0 +1,390 @@
+using System.Buffers;
+using System.Buffers.Binary;
+using System.Text;
+using ioxide.http2;
+using ioxide.nghttp2;
+using static Ioxide.Tests.Http2FlowControlTests;
+
+namespace Ioxide.Tests;
+
+/// An nghttp2 answer that ends before its request does is followed by RST_STREAM(NO_ERROR) after its END_STREAM (RFC 9113 8.1), watched on the wire.
+internal static class Nghttp2EarlyAnswerTests
+{
+ private const uint NoError = 0x0, InternalError = 0x2;
+ private const byte GoAwayFrame = 0x7;
+
+ public static void Register(Runner runner)
+ {
+ RegisterResets(runner);
+ RegisterControls(runner);
+ RegisterGuards(runner);
+ }
+
+ private static void RegisterResets(Runner runner)
+ {
+ runner.Test("nghttp2 early answer: a buffered answer to an upload still arriving is followed by RST_STREAM(NO_ERROR)", () =>
+ {
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync((_, _) => new ValueTask(Nghttp2Response.Text("too large", 413)));
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ AssertAnsweredThenStopped(client.Drain(), 413, "too large");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: a bodyless answer, END_STREAM on its HEADERS, to an upload still arriving is followed by RST_STREAM(NO_ERROR)", () =>
+ {
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync((_, _) => new ValueTask(new Nghttp2Response { Status = 413 }));
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ List frames = client.Drain();
+ Assert.True(frames.Any(f => f.StreamId == 1 && f.Type == Frame.Headers && f.EndStream), "the answer's END_STREAM did not ride its HEADERS");
+ AssertAnsweredThenStopped(frames, 413, "");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: a streamed answer to an upload still arriving is followed by RST_STREAM(NO_ERROR)", () =>
+ {
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync((_, _, writer) => Answer(writer, "too large"));
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ AssertAnsweredThenStopped(client.Drain(), 413, "too large");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: the 500 of a handler that faulted with its upload still arriving is followed by RST_STREAM(NO_ERROR)", () =>
+ {
+ // Parked, so the 500 is written by its own drain rather than a read pass's.
+ var later = new TaskCompletionSource();
+
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync((_, _) => FaultLater(later.Task));
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ List frames = client.Drain();
+ Assert.True(!frames.Any(f => f.StreamId == 1), "the stream was answered or reset while its handler ran");
+
+ later.SetResult();
+ frames.AddRange(client.Drain());
+ AssertAnsweredThenStopped(frames, 500, "");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: with a flush in flight, the reset still follows END_STREAM, once", () =>
+ {
+ var later = new TaskCompletionSource();
+
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync(async (request, _, writer) =>
+ {
+ if (request.StreamId == 1)
+ {
+ await later.Task;
+ }
+ await Answer(writer, request.StreamId == 1 ? "too large" : "other");
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false), .. Get(3)]);
+ Assert.True(client.PendingFlushes == 1, "the pass flush is not in flight");
+
+ later.SetResult(); // answered outside the pass, behind that flush
+ AssertAnsweredThenStopped(client.Drain(), 413, "too large");
+
+ Close(client, connection, run);
+ });
+ }
+
+ private static void RegisterControls(Runner runner)
+ {
+ runner.Test("nghttp2 early answer: control: an answer to a request its last DATA ended is not followed by a reset", () =>
+ {
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync(async (_, body, writer) =>
+ {
+ while (!(await body.ReadAsync()).IsEmpty)
+ {
+ }
+ await Answer(writer, "read it all");
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: true)]);
+ AssertAnsweredNotReset(client.Drain(), "read it all");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: control: an answer to a request its trailers ended is not followed by a reset", () =>
+ {
+ var later = new TaskCompletionSource();
+
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync(async (_, _) =>
+ {
+ await later.Task;
+ return Nghttp2Response.Text("late", 413);
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 100, endStream: false), .. Trailers(1)]);
+ List frames = client.Drain();
+
+ later.SetResult();
+ frames.AddRange(client.Drain());
+ AssertAnsweredNotReset(frames, "late");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: control: an answer to a request an empty DATA ended is not followed by a reset", () =>
+ {
+ var later = new TaskCompletionSource();
+
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync(async (_, _, writer) =>
+ {
+ await later.Task;
+ await Answer(writer, "late");
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 100, endStream: false), .. Data(1, 0, endStream: true)]);
+ List frames = client.Drain();
+
+ later.SetResult();
+ frames.AddRange(client.Drain());
+ AssertAnsweredNotReset(frames, "late");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: control: an answer to a GET, END_STREAM on its HEADERS, is not followed by a reset", () =>
+ {
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync((_, _) => new ValueTask(Nghttp2Response.Text("got it")));
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Get(1)]);
+ AssertAnsweredNotReset(client.Drain(), "got it");
+
+ Close(client, connection, run);
+ });
+ }
+
+ private static void RegisterGuards(Runner runner)
+ {
+ runner.Test("nghttp2 early answer: a buffered answer parked on credit is reset only after its END_STREAM, and arrives whole", () =>
+ {
+ // Past the peer's stream window the rest waits for a WINDOW_UPDATE, while the upload goes on and is credited.
+ byte[] answer = new byte[64 * 1024];
+
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync((_, _) => new ValueTask(new Nghttp2Response { Status = 413, Body = answer }));
+
+ client.ReleaseFlush();
+ client.Feed([.. Open((InitialWindowSize, 16384)), .. WindowUpdate(0, 1 << 20), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ List frames = client.Drain();
+ Assert.Equal(16384, Frame.Body(frames, 1).Length); // one stream window, then parked
+ Assert.True(!frames.Any(f => f.Type == RstStreamFrame), "the stream was reset with its answer parked on credit");
+
+ client.Feed(Data(1, 16384, endStream: false));
+ frames.AddRange(client.Drain());
+ client.Feed(WindowUpdate(1, 1 << 20));
+ frames.AddRange(client.Drain());
+
+ Assert.Equal(answer.Length, Frame.Body(frames, 1).Length);
+ AssertAnsweredThenStopped(frames, 413, Encoding.UTF8.GetString(answer));
+
+ Close(client, connection, run);
+ });
+
+ foreach (bool streamed in new[] { false, true })
+ {
+ bool stream = streamed;
+ runner.Test($"nghttp2 early answer: a {(stream ? "streamed" : "buffered")} answer to an upload the peer reset gets nothing back", () =>
+ {
+ var later = new TaskCompletionSource();
+
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = stream
+ ? connection.RunAsync(async (_, _, writer) =>
+ {
+ await later.Task;
+ await Answer(writer, "too late");
+ })
+ : connection.RunAsync(async (_, _) =>
+ {
+ await later.Task;
+ return Nghttp2Response.Text("too late", 413);
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false), .. RstStream(1, Cancel)]);
+ List frames = client.Drain();
+
+ later.SetResult();
+ frames.AddRange(client.Drain());
+ Assert.True(!frames.Any(f => f.StreamId == 1),
+ $"after the peer reset stream 1, it was sent [{string.Join(", ", frames.Where(f => f.StreamId == 1).Select(f => $"type {f.Type}"))}] (RFC 9113 5.4.2)");
+
+ Close(client, connection, run);
+ });
+ }
+
+ runner.Test("nghttp2 early answer: a handler that fails after its headers, its upload still arriving, is reset once, with INTERNAL_ERROR", () =>
+ {
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync(async (_, _, writer) =>
+ {
+ writer.WriteHeaders(new Nghttp2Response { Status = 200 });
+ await writer.FlushAsync();
+ throw new InvalidOperationException("the handler gave up mid-body");
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ uint[] resets = Resets(client.Drain(), 1);
+ Assert.True(resets.SequenceEqual([InternalError]),
+ $"stream 1 was reset with [{string.Join(", ", resets.Select(c => $"0x{c:x}"))}], where the stack's own reset is one 0x{InternalError:x}");
+
+ Close(client, connection, run);
+ });
+
+ runner.Test("nghttp2 early answer: DATA in flight after the reset comes back on the connection, never on its closed stream", () =>
+ {
+ // What a client had in flight when the reset reached it fills the connection window: only its credit lets stream 3's body in.
+ using var client = new StrictClient();
+ var connection = new Nghttp2Connection(client);
+ Task run = connection.RunAsync(async (request, body) =>
+ {
+ if (request.StreamId == 1)
+ {
+ return Nghttp2Response.Text("too large", 413);
+ }
+ long read = 0;
+ ReadOnlyMemory chunk;
+ while (!(chunk = await body.ReadAsync()).IsEmpty)
+ {
+ read += chunk.Length;
+ }
+ return Nghttp2Response.Text($"read {read}");
+ });
+
+ client.ReleaseFlush();
+ client.Feed([.. Open(), .. Post(1), .. Data(1, 1000, endStream: false)]);
+ AssertAnsweredThenStopped(client.Drain(), 413, "too large");
+
+ client.Feed(Fill(1, 65535 - 1000));
+ List after = client.Drain();
+ Assert.True(!after.Any(f => f.StreamId == 1),
+ $"after its RST_STREAM, stream 1 was sent [{string.Join(", ", after.Where(f => f.StreamId == 1).Select(f => $"type {f.Type}"))}] (RFC 9113 5.1)");
+
+ client.Feed([.. Post(3), .. Data(3, 16384, endStream: true)]);
+ after.AddRange(client.Drain());
+ Assert.True(!after.Any(f => f.Type == GoAwayFrame), "the connection went away: the DATA after the reset was never credited on it");
+ Assert.Equal("read 16384", Encoding.UTF8.GetString(Frame.Body(after, 3)));
+
+ Close(client, connection, run);
+ });
+ }
+
+ // The answer whole and RST_STREAM(NO_ERROR) after its END_STREAM, as the stream's last frame.
+ private static void AssertAnsweredThenStopped(List frames, int status, string body)
+ {
+ List stream = frames.Where(f => f.StreamId == 1).ToList();
+ int end = stream.FindIndex(f => f.EndStream && f.Type is Frame.Data or Frame.Headers);
+ int reset = stream.FindIndex(f => f.Type == RstStreamFrame);
+
+ Assert.True(end >= 0, "the answer never ended");
+ Assert.Equal(status, Status(frames, 1));
+ Assert.Equal(body, Encoding.UTF8.GetString(Frame.Body(stream, 1)));
+ Assert.True(reset >= 0,
+ "the answer ended with its request still arriving, and no RST_STREAM asked the client to stop uploading");
+ Assert.True(reset > end && reset == stream.Count - 1,
+ $"RST_STREAM was frame {reset} of the stream's {stream.Count} and its END_STREAM frame {end}: a reset ahead of the end cuts the answer off");
+ Assert.True(Resets(frames, 1).Length == 1, $"stream 1 was reset {Resets(frames, 1).Length} times");
+
+ uint code = BinaryPrimitives.ReadUInt32BigEndian(stream[reset].Payload);
+ Assert.True(code == NoError, $"the stream was reset with 0x{code:x}, where NO_ERROR leaves the client its answer");
+ }
+
+ private static void AssertAnsweredNotReset(List frames, string body)
+ {
+ Assert.Equal(body, Encoding.UTF8.GetString(Frame.Body(frames, 1)));
+ Assert.True(frames.Any(f => f.StreamId == 1 && f.EndStream), "the answer never ended");
+ Assert.True(!frames.Any(f => f.Type == RstStreamFrame), "a request that had ended was reset");
+ }
+
+ // A stream's :status, decoded in wire order through one table, as nghttp2's encoder indexes across responses.
+ private static int Status(List frames, int streamId)
+ {
+ var decoder = new HpackDecoder();
+ byte[] scratch = new byte[4096];
+ var statuses = new Dictionary();
+ foreach (Frame frame in frames.Where(f => f.Type == Frame.Headers))
+ {
+ decoder.Decode(frame.Payload, scratch, (statuses, frame.StreamId), static (state, name, value) =>
+ {
+ if (name.SequenceEqual(":status"u8))
+ {
+ state.statuses[state.StreamId] = int.Parse(Encoding.ASCII.GetString(value));
+ }
+ });
+ }
+ return statuses.GetValueOrDefault(streamId, -1);
+ }
+
+ private static uint[] Resets(List frames, int streamId)
+ => frames.Where(f => f.StreamId == streamId && f.Type == RstStreamFrame)
+ .Select(f => BinaryPrimitives.ReadUInt32BigEndian(f.Payload)).ToArray();
+
+ private static async ValueTask FaultLater(Task later)
+ {
+ await later;
+ throw new InvalidOperationException("the handler gave up");
+ }
+
+ private static ValueTask Answer(Nghttp2ResponseWriter writer, string body)
+ {
+ writer.WriteHeaders(new Nghttp2Response { Status = 413 });
+ writer.Write(Encoding.UTF8.GetBytes(body));
+ return ValueTask.CompletedTask; // the connection completes the writer once its handler returns
+ }
+
+ private static void Close(StrictClient client, Nghttp2Connection connection, Task run)
+ {
+ client.Close(run);
+ connection.Dispose();
+ }
+
+ // nghttp2 wants an :authority. Static-table HPACK otherwise: 0x83 POST / 0x82 GET, 0x86 :scheme http, 0x84 :path /.
+ private static byte[] Post(int streamId) => FrameBytes(0x1, 0x4, streamId, [0x83, 0x86, 0x84, 0x01, 9, .. "a.example"u8]);
+
+ private static byte[] Get(int streamId) => FrameBytes(0x1, 0x5, streamId, [0x82, 0x86, 0x84, 0x01, 9, .. "a.example"u8]);
+
+ // 0x90 is static 16, accept-encoding: gzip, deflate - a regular field, as trailers carry.
+ private static byte[] Trailers(int streamId) => FrameBytes(0x1, 0x5, streamId, [0x90]);
+}
diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs
index d1d2e9c6..95146068 100644
--- a/tests/Ioxide.Tests.Unit/Program.cs
+++ b/tests/Ioxide.Tests.Unit/Program.cs
@@ -47,6 +47,7 @@ private static int Main()
Http2EarlyAnswerTests.Register(runner);
Http2EarlyAnswerGuardTests.Register(runner);
Nghttp2ResetResponseTests.Register(runner);
+ Nghttp2EarlyAnswerTests.Register(runner);
Http2PeerClosedFlushTests.Register(runner);
Http2StreamResetTests.Register(runner);
HpackEncodeTests.Register(runner);