From e978a268bb8a05f0958c589a870eb3747765fa4e Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 20:08:43 +0000 Subject: [PATCH] nghttp2, nghttp3, http3: a buffered handler that answers null gets a 500 on its own stream instead of ending the connection or going unanswered (#616) A buffered handler that answers null made the stack throw a NullReferenceException at its first read of the response, before anything was submitted. Only nghttp2's parked path caught it. nghttp2: answered synchronously, as nearly every handler is, it left DispatchBuffered's try/finally, and the read loop took it for a broken peer. It freed the session without a GOAWAY, losing every other stream on the connection and what the pass had submitted, and told nothing to stderr. nghttp3 and http3: answered synchronously or with a completed task, it left the dispatch and the run loop's try/finally, so the task RunAsync returned faulted. The teardown sent no CONNECTION_CLOSE, since no error code had been recorded, so the client heard nothing and every request on the connection waited out its idle timeout. Answered after an await, it faulted the task that finishes the request, which nothing awaits, outside its catch: that stream got neither a response nor a reset. Each stack now hands every buffered answer, synchronous, completed or awaited, to one helper that tests it for null first. A null answer gets what a handler that threw gets: the fault is reported with a NullReferenceException (OnHandlerFault on nghttp3, else stderr; stderr on nghttp2 and http3) and the stream gets a 500. nghttp2's parked path keeps its catch, and its body is the helper both paths share. The test comes before the submit, rather than a catch around it, because a catch stops the JIT inlining the submit's P/Invoke. Nothing else thrown while a response is handed over is handled differently. --- src/protocols/ioxide.http3/Http3Connection.cs | 25 +- .../Nghttp2Connection.RunBuffered.cs | 36 ++- .../Nghttp3Connection.RunBuffered.cs | 29 ++- tests/Ioxide.Tests.E2E/Program.cs | 1 + .../Protocols/H3NullAnswerTests.cs | 236 ++++++++++++++++++ .../Protocols/Http2BufferedFaultTests.cs | 47 +++- .../Nghttp2NullAnswerTests.cs | 51 ++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + 8 files changed, 402 insertions(+), 24 deletions(-) create mode 100644 tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs create mode 100644 tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs diff --git a/src/protocols/ioxide.http3/Http3Connection.cs b/src/protocols/ioxide.http3/Http3Connection.cs index 34dcfa99..66bd7bdc 100644 --- a/src/protocols/ioxide.http3/Http3Connection.cs +++ b/src/protocols/ioxide.http3/Http3Connection.cs @@ -682,7 +682,7 @@ private void DispatchReady(Func handler) resp = new Http3Response { Status = 500 }; } - Submit(rs.Request.StreamId, resp); + SubmitAnswer(rs.Request.StreamId, resp); Recycle(rs); } _ready.Clear(); @@ -722,7 +722,7 @@ private void DispatchReadyAsync(Func> han { RetireBody(rs); Http3Response response = pending.Result; // once: a ValueTask is consumed by reading it - Submit(sid, response); + SubmitAnswer(sid, response); Recycle(rs); } else @@ -752,10 +752,29 @@ private async Task CompleteBufferedAsync(ValueTask pending, ReqSt { return; } - Submit(rs.Request.StreamId, resp); + SubmitAnswer(rs.Request.StreamId, resp); Recycle(rs); } + // A null answer has sent nothing, so it gets what a handler that threw gets. + private void SubmitAnswer(long streamId, Http3Response resp) + { + if (resp is null) + { + AnswerNull(streamId); + return; + } + Submit(streamId, resp); + } + + // Out of line, as only a handler that answered null comes here. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AnswerNull(long streamId) + { + Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {new NullReferenceException().Message}"); + Submit(streamId, new Http3Response { Status = 500 }); + } + /// Back for the next stream once the handler is done and the response submitted. private void Recycle(ReqStream rs) { diff --git a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs index 403abf10..94dfd146 100644 --- a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs +++ b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.IO.Pipelines; +using System.Runtime.CompilerServices; namespace ioxide.nghttp2; @@ -174,7 +175,7 @@ private void DispatchBuffered(Func> h { try { - SubmitResponse(pending.StreamId, inFlight.Result); + SubmitAnswer(pending.StreamId, inFlight.Result); } finally { @@ -210,13 +211,7 @@ private async Task CompleteAsync(ValueTask inFlight, PendingReq await new ReactorSwitch(_reactor); // Nobody can observe this task, so an escaping exception would vanish silently and the // peer would wait on a stream that is never coming. - Console.Error.WriteLine( - $"[ioxide.nghttp2] request handler faulted: {exception.GetBaseException().Message}"); - - if (!IsBroken) - { - SubmitResponse(pending.StreamId, new Nghttp2Response { Status = 500 }); - } + AnswerFault(pending.StreamId, exception); } finally { @@ -224,4 +219,29 @@ private async Task CompleteAsync(ValueTask inFlight, PendingReq await FlushEgressAsync(); } } + + // A null answer has submitted nothing, so it gets what a handler that threw gets. Tested, not caught: a catch + // around the submit stops the JIT inlining its P/Invoke. + private void SubmitAnswer(int streamId, Nghttp2Response response) + { + if (response is null) + { + AnswerFault(streamId, new NullReferenceException()); + return; + } + SubmitResponse(streamId, response); + } + + // A handler that threw, or answered null, gets a 500 on its own stream. Out of line, as only a fault comes here. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AnswerFault(int streamId, Exception exception) + { + Console.Error.WriteLine( + $"[ioxide.nghttp2] request handler faulted: {exception.GetBaseException().Message}"); + + if (!IsBroken) + { + SubmitResponse(streamId, new Nghttp2Response { Status = 500 }); + } + } } diff --git a/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs b/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs index 2da49b46..c5d94e2e 100644 --- a/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs +++ b/src/protocols/ioxide.nghttp3/Connection/Nghttp3Connection.RunBuffered.cs @@ -1,3 +1,5 @@ +using System.Runtime.CompilerServices; + namespace ioxide.nghttp3; /// @@ -78,7 +80,7 @@ private void DispatchBufferedSync(Func handler) response = new Nghttp3Response { Status = 500 }; } - QueueResponse(request.StreamId, response); + QueueAnswer(request, response); ReleaseRequest(request); // sync handler returned - the contract just ended } _readyStreamIds.Clear(); @@ -113,7 +115,7 @@ private void DispatchBufferedAsync(Func pending, Ngh } await new ReactorSwitch(Reactor); // back on the reactor, wherever the handler finished - long streamId = request.StreamId; request.HandlerDone = true; RetireBody(request); if (_nghttp3Handle != 0 && !_protocolFailed) { - QueueResponse(streamId, response); + QueueAnswer(request, response); PumpEgress(); } StopReadingIfAnsweredEarly(request); @@ -158,4 +159,24 @@ private async Task CompleteBufferedAsync(ValueTask pending, Ngh ReleaseRequest(request); } } + + // A null answer has submitted nothing, so it gets what a handler that threw gets. Tested, not caught: a catch + // around the submit stops the JIT inlining its P/Invoke. + private void QueueAnswer(Nghttp3Request request, Nghttp3Response response) + { + if (response is null) + { + AnswerNull(request); + return; + } + QueueResponse(request.StreamId, response); + } + + // Out of line, as only a handler that answered null comes here. + [MethodImpl(MethodImplOptions.NoInlining)] + private void AnswerNull(Nghttp3Request request) + { + ReportHandlerFault(request, new NullReferenceException()); + QueueResponse(request.StreamId, new Nghttp3Response { Status = 500 }); + } } diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs index a471a316..4fa9cea4 100644 --- a/tests/Ioxide.Tests.E2E/Program.cs +++ b/tests/Ioxide.Tests.E2E/Program.cs @@ -79,6 +79,7 @@ private static int Main() Http3RequestReuseTests.Register(runner); H3BodyModeTests.Register(runner); H3BufferedFaultTests.Register(runner); + H3NullAnswerTests.Register(runner); StreamResetTests.Register(runner); HandlerFaultHookTests.Register(runner); H3BodyLimitTests.Register(runner); diff --git a/tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs b/tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs new file mode 100644 index 00000000..3677531d --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Protocols/H3NullAnswerTests.cs @@ -0,0 +1,236 @@ +using ioxide; +using ioxide.http3; +using ioxide.nghttp3; +using ioxide.ngtcp2; +using static Ioxide.Tests.H3ErrorCodeTests; + +namespace Ioxide.Tests; + +/// +/// A whole-response HTTP/3 handler that answers null, on both stacks and from every buffered overload. A null answer +/// cannot be sent, so its request is answered as one whose handler threw: a bare 500 on its own stream, the fault +/// reported, and the connection's other requests served. Checked on a transport that records what the stack sends, +/// where two requests share one pass, and over QUIC. +/// +internal static class H3NullAnswerTests +{ + // A GET framed by hand: static :method GET, :scheme https and :path /, and a literal :authority, which nghttp3 requires. + private static readonly byte[] Get = [0x01, 0x10, 0x00, 0x00, 0xD1, 0xD7, 0xC1, 0x50, 0x09, .. "localhost"u8]; + + private enum Shape { Synchronous, Completed, Awaited, ReaderCompleted, ReaderAwaited } + + public static void Register(Runner runner) + { + foreach (Shape shape in Enum.GetValues()) + { + Shape s = shape; + + runner.Test($"h3 null answer (nghttp3, {Name(s)}): the request is answered 500, the hook hears the fault, and the next request in the pass is served", () => + { + var faults = new List(); + var options = new Nghttp3Options { OnHandlerFault = (request, fault) => faults.Add($"stream {request.StreamId}: {fault.GetType().Name}") }; + AssertAnsweredInPass((quic, gate) => ServeNghttp3(new Nghttp3Connection(quic, options), s, gate)); + Assert.Equal("stream 0: NullReferenceException", string.Join(", ", faults)); + }); + + runner.Test($"h3 null answer (ioxide.http3, {Name(s)}): the request is answered 500, stderr hears the fault, and the next request in the pass is served", () => + { + var stderr = new StringWriter(); + TextWriter original = Console.Error; + Console.SetError(stderr); + try + { + AssertAnsweredInPass((quic, gate) => ServeHttp3(new Http3Connection(quic), s, gate)); + } + finally + { + Console.SetError(original); + } + + int reported = stderr.ToString().Split('\n').Count(line => line.StartsWith("[ioxide.http3] request handler faulted")); + Assert.True(reported == 1, $"stderr had: [{stderr}]"); + }); + } + + foreach (string stack in new[] { "nghttp3", "ioxide.http3" }) + { + foreach (Shape shape in new[] { Shape.Synchronous, Shape.Awaited }) + { + string at = stack; + Shape s = shape; + runner.Test($"h3 null answer ({at}, {Name(s)}, over QUIC): the request is answered 500, and the connection serves on", () => + { + using H3TestClient client = Connect(at, s); + + (int status, string body) = client.Get("/null", timeoutMs: 5000); + Assert.True(status == 500 && body == "", $"the request whose handler answered null got {Describe(status, body)}, not a bare 500"); + + (status, body) = client.Get("/ok", timeoutMs: 5000); + Assert.True(status == 200 && body == "ok", + $"the next request on the connection got {Describe(status, body)}" + (client.PeerClosed ? ": the server closed the connection" : "")); + }); + } + } + } + + private static string Name(Shape shape) => shape switch + { + Shape.Synchronous => "synchronous", + Shape.Completed => "completed task", + Shape.Awaited => "awaited", + Shape.ReaderCompleted => "body reader, completed task", + _ => "body reader, awaited", + }; + + // Stream 0 answers null, once the gate opens when its shape awaits; stream 4 answers "ok" at once. Both arrive in one pass. + private static void AssertAnsweredInPass(Func serve) + { + var quic = new RecordingQuic(); + Assert.True(quic.EnqueueStreamData(0, Get, fin: true) && quic.EnqueueStreamData(4, Get, fin: true), "the recv ring refused a request"); + + var gate = new TaskCompletionSource(); + Task run = serve(quic, gate.Task); + Assert.True(!run.IsCompleted, $"the run loop ended on a null answer: {Why(run)}"); + + gate.SetResult(); + quic.MarkClosed(); + Assert.True(Task.WaitAny([run], 5000) == 0 && run.IsCompletedSuccessfully, $"the run loop did not end cleanly once the transport closed: {Why(run)}"); + + Assert.Equal("500 []", Answer(quic, 0)); + Assert.Equal("200 [ok]", Answer(quic, 4)); + Assert.True(quic.ClosedWith is null, $"the connection was closed with 0x{quic.ClosedWith:x}"); + } + + private static Task ServeNghttp3(Nghttp3Connection connection, Shape shape, Task gate) + { + static Nghttp3Response? Respond(Nghttp3Request request) => request.StreamId == 0 ? null : Nghttp3Response.Text("ok"); + + async ValueTask Later(Nghttp3Request request) + { + if (request.StreamId == 0) + { + await gate; + } + return Respond(request)!; + } + + return shape switch + { + Shape.Synchronous => connection.RunAsync(request => Respond(request)!), + Shape.Completed => connection.RunAsync(request => new ValueTask(Respond(request)!)), + Shape.Awaited => connection.RunAsync(Later), + Shape.ReaderCompleted => connection.RunAsync((Nghttp3Request request, Nghttp3BodyReader _) => new ValueTask(Respond(request)!)), + _ => connection.RunAsync((Nghttp3Request request, Nghttp3BodyReader _) => Later(request)), + }; + } + + private static Task ServeHttp3(Http3Connection connection, Shape shape, Task gate) + { + static Http3Response? Respond(Http3Request request) => request.StreamId == 0 ? null : Http3Response.Text("ok"); + + async ValueTask Later(Http3Request request) + { + if (request.StreamId == 0) + { + await gate; + } + return Respond(request)!; + } + + return shape switch + { + Shape.Synchronous => connection.RunAsync(request => Respond(request)!), + Shape.Completed => connection.RunAsync(request => new ValueTask(Respond(request)!)), + Shape.Awaited => connection.RunAsync(Later), + Shape.ReaderCompleted => connection.RunAsync((Http3Request request, Http3BodyReader _) => new ValueTask(Respond(request)!)), + _ => connection.RunAsync((Http3Request request, Http3BodyReader _) => Later(request)), + }; + } + + // "/null" answers null, after a later reactor pass when the shape awaits; anything else answers "ok". + private static H3TestClient Connect(string stack, Shape shape) + { + (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) => (stack, shape) switch + { + ("nghttp3", Shape.Synchronous) => new Nghttp3Connection(conn).RunAsync(request => IsNull(request.Path) ? null! : Nghttp3Response.Text("ok")), + ("nghttp3", _) => new Nghttp3Connection(conn).RunAsync(async request => + { + await TestServer.LaterPass(); + return IsNull(request.Path) ? null! : Nghttp3Response.Text("ok"); + }), + (_, Shape.Synchronous) => new Http3Connection(conn).RunAsync(request => IsNull(request.Path) ? null! : Http3Response.Text("ok")), + _ => new Http3Connection(conn).RunAsync(async request => + { + await TestServer.LaterPass(); + return IsNull(request.Path) ? null! : Http3Response.Text("ok"); + }), + }); + + var client = new H3TestClient("127.0.0.1", udpPort); + client.Connect(); + Assert.True(client.CompleteHandshake(timeoutMs: 5000), "handshake did not complete"); + return client; + + static bool IsNull(ReadOnlyMemory path) => path.Span.SequenceEqual("/null"u8); + } + + // "status [body]" from what the stack sent on one stream, read off its HEADERS and DATA frames; "nothing" when it sent nothing. + private static string Answer(RecordingQuic quic, long streamId) + { + List<(long StreamId, byte[] Bytes, bool Fin)> sent = [.. quic.Sent.Where(s => s.StreamId == streamId)]; + if (sent.Count == 0) + { + return "nothing"; + } + + byte[] stream = [.. sent.SelectMany(s => s.Bytes)]; + byte[]? fields = null; + var body = new List(); + for (int at = 0; at < stream.Length;) + { + long type = ReadVarint(stream, ref at); + int length = (int)ReadVarint(stream, ref at); + if (type == 0x01) + { + fields ??= stream[at..(at + length)]; + } + else if (type == 0x00) + { + body.AddRange(stream[at..(at + length)]); + } + at += length; + } + + // :status follows the field section's two-byte prefix: static index 25 is 200, 71 is 500. + string status = fields switch + { + null => "no HEADERS", + [_, _, 0xD9, ..] => "200", + [_, _, 0xFF, 0x08, ..] => "500", + _ => Convert.ToHexString(fields), + }; + return $"{status} [{System.Text.Encoding.ASCII.GetString(body.ToArray())}]" + (sent[^1].Fin ? "" : ", not ended"); + } + + private static long ReadVarint(byte[] bytes, ref int at) + { + int length = 1 << (bytes[at] >> 6); + long value = bytes[at] & 0x3F; + for (int i = 1; i < length; i++) + { + value = (value << 8) | bytes[at + i]; + } + at += length; + return value; + } + + private static string Why(Task run) => run.Exception?.GetBaseException().Message ?? run.Status.ToString(); + + private static string Describe(int status, string body) => status < 0 ? "no answer" : $"{status} [{body}]"; +} diff --git a/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs b/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs index 4c30cf56..54b8cd3d 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/Http2BufferedFaultTests.cs @@ -8,7 +8,8 @@ namespace Ioxide.Tests; /// /// A whole-response HTTP/2 handler that throws, on both stacks. Throwing from its task or before it /// has one - as every synchronous handler does - answers 500 on its own stream, and every other -/// stream on the connection carries on. +/// stream on the connection carries on. On nghttp2 so does a handler that answers null, which +/// cannot be submitted. /// internal static class Http2BufferedFaultTests { @@ -44,10 +45,35 @@ public static void Register(Runner runner) Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); }); } + + runner.Test("h2 fault (nghttp2): a synchronous handler that answers null answers 500, and the connection serves on", () => + { + var server = new FaultServer(); + int port = Start(native: true, server, synchronous: true); + using HttpClient client = H2Client(); + + Assert.Equal("500: ", Fetch(client, port, "/null")); + Assert.Equal("200: ok", Fetch(client, port, "/ok")); + Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); + }); + + runner.Test("h2 fault (nghttp2): a request in flight beside a handler that answers null without parking is answered", () => + { + var server = new FaultServer(); + int port = Start(native: true, server, synchronous: false); + using HttpClient client = H2Client(); + + Task slow = client.GetAsync($"http://127.0.0.1:{port}/slow"); + Assert.True(server.SlowArrived.Task.Wait(5_000), "/slow never reached its handler"); + + Assert.Equal("500: ", Fetch(client, port, "/null")); + Assert.Equal("200: slow", Describe(slow)); + Assert.True(Volatile.Read(ref server.Connections) == 1, WholeConnection(server)); + }); } private static string WholeConnection(FaultServer server) - => $"{server.Connections} connections served requests: the handler that threw took its whole connection down"; + => $"{server.Connections} connections served requests: the handler's fault took its whole connection down"; private sealed class FaultServer { @@ -56,9 +82,10 @@ private sealed class FaultServer public bool BoomSeen; } - // "/boom" throws before the handler returns, "/slow" parks until "/boom" has been dispatched, - // anything else answers "ok". Synchronous serves through the synchronous overload; otherwise - // through the asynchronous one, with a lambda that is not async and so throws before its task. + // "/boom" throws before the handler returns, on nghttp2 "/null" answers null without parking, + // "/slow" parks until either has been dispatched, anything else answers "ok". Synchronous serves + // through the synchronous overload; otherwise through the asynchronous one, with a lambda that + // is not async and so throws, or answers, before its task. private static int Start(bool native, FaultServer server, bool synchronous) => TestServer.Start(async (reactor, conn) => { @@ -72,10 +99,10 @@ private static int Start(bool native, FaultServer server, bool synchronous) await (synchronous ? connection.RunAsync(request => Answer(request.Path) is { } body ? new Nghttp2Response { Status = 200, Body = body } - : throw new InvalidOperationException("the handler failed")) + : IsNull(request.Path) ? null! : throw new InvalidOperationException("the handler failed")) : connection.RunAsync(request => Answer(request.Path) is { } body ? Park(timer, server, body, b => new Nghttp2Response { Status = 200, Body = b }) - : throw new InvalidOperationException("the handler failed"))); + : IsNull(request.Path) ? new ValueTask((Nghttp2Response)null!) : throw new InvalidOperationException("the handler failed"))); } else { @@ -94,7 +121,7 @@ private static int Start(bool native, FaultServer server, bool synchronous) conn.DecRef(); } - // null for "/boom", noted first so a parked "/slow" knows it went through. + // null for "/boom" and "/null", noted first so a parked "/slow" knows it went through. byte[]? Answer(ReadOnlyMemory path) { if (!counted) @@ -103,13 +130,15 @@ private static int Start(bool native, FaultServer server, bool synchronous) Interlocked.Increment(ref server.Connections); } string at = System.Text.Encoding.ASCII.GetString(path.Span); - if (at == "/boom") + if (at is "/boom" or "/null") { server.BoomSeen = true; return null; } return at == "/slow" ? "slow"u8.ToArray() : "ok"u8.ToArray(); } + + static bool IsNull(ReadOnlyMemory path) => path.Span.SequenceEqual("/null"u8); }); private static async ValueTask Park(RingTimer timer, FaultServer server, byte[] body, Func respond) diff --git a/tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs b/tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs new file mode 100644 index 00000000..377ecef9 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Nghttp2NullAnswerTests.cs @@ -0,0 +1,51 @@ +using System.Text; +using ioxide.nghttp2; + +namespace Ioxide.Tests; + +/// A buffered handler that answers null is answered as one that throws: a bare 500 on its own stream, and the connection serves on. +internal static class Nghttp2NullAnswerTests +{ + public static void Register(Runner runner) + { + runner.Test("nghttp2: a synchronous handler that answers null is answered 500 on its own stream, the fault reaches stderr, and the next stream in the same read is served", () => + { + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + var stderr = new StringWriter(); + TextWriter original = Console.Error; + Console.SetError(stderr); + try + { + Task run = connection.RunAsync(request => request.StreamId == 1 ? null! : Nghttp2Response.Text("ok")); + + client.SendRequests(); // preface and SETTINGS + client.Drain(); + client.Feed([.. Get(1), .. Get(3)]); + List frames = client.Drain(); + + Assert.True(!run.IsCompleted, "the connection ended on a null answer"); + List stream = frames.Where(f => f.StreamId == 1).ToList(); + Assert.True(stream is [{ Type: Frame.Headers, EndStream: true }], + $"stream 1 was sent [{string.Join(", ", stream.Select(f => $"type {f.Type} flags 0x{f.Flags:x}"))}], not one HEADERS that ends it"); + Assert.True(stream[0].Payload is [0x8E], $"stream 1's header block was [{Convert.ToHexString(stream[0].Payload)}], not :status 500 alone"); + Assert.Equal("ok", Encoding.UTF8.GetString(Frame.Body(frames, 3))); + Assert.True(stderr.ToString().Contains("[ioxide.nghttp2] request handler faulted"), $"stderr had: [{stderr}]"); + + client.Close(run); + } + finally + { + Console.SetError(original); + connection.Dispose(); + } + }); + } + + // A GET with END_STREAM: :scheme http, :path /, and an :authority, which nghttp2 requires. + private static byte[] Get(int streamId) + { + byte[] block = [0x82, 0x86, 0x84, 0x01, 9, .. "a.example"u8.ToArray()]; + return Http2FlowControlTests.FrameBytes(0x1, 0x5, streamId, block); + } +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..bf1030be 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -34,6 +34,7 @@ private static int Main() Http2HeaderListTests.Register(runner); Nghttp2HeaderListTests.Register(runner); Nghttp2OversizedResponseTests.Register(runner); + Nghttp2NullAnswerTests.Register(runner); Http2RequestHeaderTests.Register(runner); Http2RequestReuseTests.Register(runner); Nghttp2RequestReuseTests.Register(runner);