diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index 18d9c3ea..dbe56bf5 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -101,19 +101,26 @@ private void Handle(in FrameHeader header, ReadOnlySpan payload) case FrameType.Settings: HandleSettings(header, payload); break; case FrameType.WindowUpdate: HandleWindowUpdate(header, payload); break; case FrameType.Ping: HandlePing(header, payload); break; - case FrameType.RstStream: HandleRstStream(header); break; + case FrameType.RstStream: HandleRstStream(header, payload); break; + case FrameType.Priority: HandlePriority(header, payload); break; case FrameType.GoAway: _failed = true; break; - // PRIORITY carries no obligation, and PUSH_PROMISE cannot arrive at a server. Both are - // ignored rather than treated as errors, which is what the RFC asks of a receiver. - case FrameType.Priority: - case FrameType.PushPromise: + // RFC 9113 8.4: a server never receives a push, so a client that sends one is broken. + case FrameType.PushPromise: GoAway(Http2Error.ProtocolError); break; + + // RFC 9113 4.1: a frame of a type nobody knows is ignored. default: break; } } - private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) + /// + /// PRIORITY carries no obligation (RFC 9113 5.3.2), so its signal is ignored, but not its framing: + /// it always names a stream, and it is five octets long (6.3). A stream that depends on itself is + /// refused too: RFC 9113 dropped the rule with the priority scheme, but RFC 7540 5.3.1 made it a + /// stream error, and nghttp2 still refuses one. + /// + private void HandlePriority(in FrameHeader header, ReadOnlySpan payload) { if (header.StreamId == 0) { @@ -121,6 +128,58 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) return; } + if (payload.Length != 5) + { + RefusePriority(header.StreamId, Http2Error.FrameSizeError); + return; + } + + if (DependsOn(payload) == header.StreamId) + { + RefusePriority(header.StreamId, Http2Error.ProtocolError); + } + } + + // A stream error, but PRIORITY may name a stream never opened - an idle one, an even one, or one + // skipped below the highest - which no RST_STREAM may name (RFC 9113 6.4), and which nothing here + // tells from one closed long ago. Only a stream still live is reset; for any other the connection + // answers. + private void RefusePriority(int streamId, uint errorCode) + { + if (_streams.ContainsKey(streamId) || _responseWindows.ContainsKey(streamId)) + { + ResetOpenStream(streamId, errorCode); + } + else + { + GoAway(errorCode); + } + } + + // The stream a priority's five octets name, its exclusive bit aside. + private static int DependsOn(ReadOnlySpan priority) + => (int)(BinaryPrimitives.ReadUInt32BigEndian(priority) & 0x7FFFFFFF); + + /// + /// A stream error on a stream that may be serving already: its body reader ends, and its response + /// finds the stream gone, as after the peer's own reset - nothing may follow a reset (RFC 9113 5.1). + /// + private void ResetOpenStream(int streamId, uint errorCode) + { + _responseWindows.Remove(streamId); + ResetStream(streamId, errorCode); + ReleaseCreditWaiters(streamId); // a writer parked on credit wakes to find the stream gone + } + + private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) + { + // RFC 9113 5.1.1: a client opens odd streams only, and a server that pushes nothing has no even ones. + if (header.StreamId == 0 || (header.StreamId & 1) == 0) + { + GoAway(Http2Error.ProtocolError); + return; + } + ReadOnlySpan block = payload; // Padding and priority sit in front of the header block and are not part of it. @@ -141,6 +200,7 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) block = block[..^padding]; } + bool selfDependent = false; if ((header.Flags & FrameFlags.Priority) != 0) { if (block.Length < 5) @@ -148,14 +208,34 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) GoAway(Http2Error.FrameSizeError); return; } + selfDependent = DependsOn(block) == header.StreamId; block = block[5..]; } - if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) + bool open = _streams.TryGetValue(header.StreamId, out PendingRequest? pending); + if (!open && header.StreamId <= _highestStream) + { + HandleHeadersOnClosed(header, block); + return; + } + + if (selfDependent) { - // An id at or below one already opened opens nothing (RFC 9113 5.1.1) - it is trailers - // for a request already answered - so it is decoded like a refused block, but not reset. - if (header.StreamId <= _highestStream || _streams.Count >= _options.MaxConcurrentStreams) + // Refused like a malformed request (see HandlePriority): decoded aside to keep HPACK in + // step, and whatever the stream was doing ends. A new one is reset once its block is in. + if (open) + { + ResetOpenStream(header.StreamId, Http2Error.ProtocolError); + } + _discardBlock.StreamId = header.StreamId; + _discardReset = Http2Error.ProtocolError; + DiscardHeaderBlock(header, block); + return; + } + + if (!open) + { + if (_streams.Count >= _options.MaxConcurrentStreams) { // Past the limit we advertised. The block still has to be DECODED - HPACK is one // stream across the whole connection, so skipping it would desynchronise every @@ -168,12 +248,22 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) pending = RentPending(header.StreamId); _streams[header.StreamId] = pending; - _highestStream = header.StreamId; + OpenStreamId(header.StreamId); // Only the block that opens a stream can say there is no body. An END_STREAM on a later // one ends trailers, which come after a body, empty or not. pending.HasBody = (header.Flags & FrameFlags.EndStream) == 0; } + else if (pending!.RequestEnded) + { + // Half-closed (remote): the peer has ended the request, and may send nothing more on it + // but WINDOW_UPDATE, PRIORITY and RST_STREAM (RFC 9113 5.1). Decoded aside, as HPACK + // must be, and nothing of it reaches a handler. + ResetOpenStream(header.StreamId, Http2Error.StreamClosed); + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + return; + } else if (pending.BodyReader is { } reader) { // Trailers for a request already being served: its arena backs what the handler holds, @@ -229,6 +319,51 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) } } + /// + /// HEADERS on a stream at or below the highest the client opened, and no longer open (RFC 9113 5.1). + /// One the peer itself closed is its error (). One the client + /// skipped, and so never opened, is an unexpected stream id (5.1.1). Any other - one this side + /// reset or stopped reading, or one closed too long ago to be remembered - is still on its way, + /// which the peer could not have known. A block not refused with the connection is decoded and + /// dropped, as HPACK must be. + /// + private void HandleHeadersOnClosed(in FrameHeader header, ReadOnlySpan block) + { + int how = ClosedHow(header.StreamId); + if (how is ClosedByPeer or ClosedByPeerReset) + { + RefuseOnPeerClosed(header.StreamId, how); + } + else if (how == 0 && NeverOpened(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); + } + + if (!_failed) + { + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + } + } + + /// + /// DATA or HEADERS on a stream the peer closed are its error (RFC 9113 5.1), of the stream after its + /// RST_STREAM or while the response is still going, and of the connection once its END_STREAM and + /// ours have closed the stream both ways - a MAY in RFC 9113, a MUST in RFC 7540. After the reset + /// the stream is this side's, so what follows is dropped. + /// + private void RefuseOnPeerClosed(int streamId, int how) + { + if (how == ClosedByPeerReset || _responseWindows.ContainsKey(streamId)) + { + ResetOpenStream(streamId, Http2Error.StreamClosed); + } + else + { + GoAway(Http2Error.StreamClosed); + } + } + /// /// A header block that opens no request - a refused stream's, or trailers: decoded to keep the /// HPACK table in step with the peer, and thrown away. Bounded like any other block, so refusing @@ -251,13 +386,15 @@ private void DiscardHeaderBlock(in FrameHeader header, ReadOnlySpan block) int streamId = _discardBlock.StreamId; _discardBlock.StreamId = 0; + uint reset = _discardReset; + _discardReset = Http2Error.RefusedStream; // Only a new id was refused; nothing of another stream can arrive mid-block to move the // highest. Trailers are not reset: REFUSED_STREAM says the request went unprocessed. if (DecodeHeaderBlock(_discardBlock, discard: true) && streamId > _highestStream) { - ResetStream(streamId, Http2Error.RefusedStream); - _highestStream = streamId; // refused, but used + ResetStream(streamId, reset); + OpenStreamId(streamId); // refused, but used } } @@ -361,7 +498,7 @@ private bool DecodeHeaderBlock(PendingRequest pending, bool endsStream = false, private void HandleData(in FrameHeader header, ReadOnlySpan payload) { - if (header.StreamId == 0 || header.StreamId > _highestStream) + if (header.StreamId == 0 || IsIdle(header.StreamId)) { GoAway(Http2Error.ProtocolError); // RFC 9113 6.1: DATA always belongs to a stream, and 5.1: never an idle one return; @@ -404,7 +541,22 @@ private void HandleData(in FrameHeader header, ReadOnlySpan payload) if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) { - return; // done with its request, so owed no credit, behind a buffered answer still parked on credit too + // Past the peer's END_STREAM or its RST_STREAM it is the peer's error. Otherwise this side is + // done with the request, so the frame is owed no credit, behind a buffered answer still + // parked on credit too. + int how = ClosedHow(header.StreamId); + if (how is ClosedByPeer or ClosedByPeerReset) + { + RefuseOnPeerClosed(header.StreamId, how); + } + return; + } + + // Half-closed (remote): after its END_STREAM, not a byte more reaches the body reader. + if (pending.RequestEnded) + { + ResetOpenStream(header.StreamId, Http2Error.StreamClosed); + return; } // Past its window is a stream error (6.9), and its response stops with it; an empty frame may still end it (6.9.1). @@ -470,9 +622,7 @@ private void RefuseBody(PendingRequest pending) ? "the request body ran past its content-length" : $"the request body ended {pending.ContentLength - pending.BodyReceived} bytes short of its content-length"); - _responseWindows.Remove(streamId); - ResetStream(streamId, Http2Error.ProtocolError); - ReleaseCreditWaiters(streamId); // a writer parked on credit wakes to find the stream gone + ResetOpenStream(streamId, Http2Error.ProtocolError); } private void TryComplete(PendingRequest pending) @@ -491,14 +641,28 @@ private void TryComplete(PendingRequest pending) // Its window moves with it: a WINDOW_UPDATE finds a stream by _streams only until here. _streams.Remove(pending.StreamId); + RememberClosed(pending.StreamId, ClosedByPeer); // half-closed (remote), and closed once answered _responseWindows[pending.StreamId] = pending.SendWindow; _ready.Enqueue(pending); } private void HandleSettings(in FrameHeader header, ReadOnlySpan payload) { + // RFC 9113 6.5: SETTINGS are the connection's, and an ACK says only that ours were applied. + if (header.StreamId != 0) + { + GoAway(Http2Error.ProtocolError); + return; + } + if ((header.Flags & FrameFlags.Ack) != 0) { + if (payload.Length != 0) + { + GoAway(Http2Error.FrameSizeError); + return; + } + // Our SETTINGS acknowledged: the peer has resized its open streams by them, and sizes new ones by them (RFC 9113 6.9.2). int delta = _options.InitialWindowSize - _initialStreamWindow; if (delta != 0) @@ -527,6 +691,15 @@ private void HandleSettings(in FrameHeader header, ReadOnlySpan payload) switch (id) { + case Http2Setting.EnablePush: + // Nothing here pushes, so the value says nothing to us, but it is a flag (6.5.2). + if (value > 1) + { + GoAway(Http2Error.ProtocolError); + return; + } + break; + case Http2Setting.InitialWindowSize: if (value > int.MaxValue) { @@ -580,6 +753,12 @@ private void HandleWindowUpdate(in FrameHeader header, ReadOnlySpan payloa return; } + if (header.StreamId != 0 && IsIdle(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); // RFC 9113 5.1: nothing but HEADERS and PRIORITY on an idle stream + return; + } + int increment = (int)(BinaryPrimitives.ReadUInt32BigEndian(payload) & 0x7FFFFFFF); if (increment == 0) { @@ -627,6 +806,11 @@ private void HandleWindowUpdate(in FrameHeader header, ReadOnlySpan payloa private void HandlePing(in FrameHeader header, ReadOnlySpan payload) { + if (header.StreamId != 0) + { + GoAway(Http2Error.ProtocolError); // RFC 9113 6.7: a PING is the connection's + return; + } if (payload.Length != 8) { GoAway(Http2Error.FrameSizeError); @@ -644,10 +828,28 @@ private void HandlePing(in FrameHeader header, ReadOnlySpan payload) Stage(frame); } - private void HandleRstStream(in FrameHeader header) + private void HandleRstStream(in FrameHeader header, ReadOnlySpan payload) { + // RFC 9113 6.4: it always names a stream, and carries one error code. + if (header.StreamId == 0) + { + GoAway(Http2Error.ProtocolError); + return; + } + if (payload.Length != 4) + { + GoAway(Http2Error.FrameSizeError); + return; + } + if (IsIdle(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); // RFC 9113 5.1: nothing but HEADERS and PRIORITY on an idle stream + return; + } + if (_streams.Remove(header.StreamId, out PendingRequest? pending)) { + RememberClosed(header.StreamId, ClosedByPeerReset); Discard(pending); // the peer gave up; there is nobody to answer } diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index 9ca2d941..5215eecb 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Write.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Write.cs @@ -327,6 +327,7 @@ private void ResetStream(int streamId, uint errorCode) { Discard(pending); } + RememberClosed(streamId, ClosedByUs); // what the peer sends before it hears of this is dropped (RFC 9113 5.1) Span frame = stackalloc byte[FrameHeader.Size + 4]; new FrameHeader(4, FrameType.RstStream, FrameFlags.None, streamId).Write(frame); @@ -338,7 +339,9 @@ private void GoAway(uint errorCode) { Span frame = stackalloc byte[FrameHeader.Size + 8]; new FrameHeader(8, FrameType.GoAway, FrameFlags.None, 0).Write(frame); - BinaryPrimitives.WriteUInt32BigEndian(frame[FrameHeader.Size..], 0); // last stream id + // RFC 9113 6.8: the highest stream that was or might be processed, so the client knows which of its + // requests it may retry elsewhere. Refused ones are among them, and REFUSED_STREAM already said so. + BinaryPrimitives.WriteUInt32BigEndian(frame[FrameHeader.Size..], (uint)_highestStream); BinaryPrimitives.WriteUInt32BigEndian(frame[(FrameHeader.Size + 4)..], errorCode); Stage(frame); _failed = true; diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index c269c524..48ac94b4 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -57,6 +57,10 @@ public sealed partial class Http2Connection : IDisposable // with another stream's frames, so one of these is enough. private readonly PendingRequest _discardBlock = new(); + // What a new stream whose block is discarded is reset with once that block is decoded: + // REFUSED_STREAM past the concurrency limit, PROTOCOL_ERROR for one refused as malformed. + private uint _discardReset = Http2Error.RefusedStream; + // The stream whose header block is still open, or 0. private int _headerBlockStream; @@ -64,6 +68,24 @@ public sealed partial class Http2Connection : IDisposable // opens nothing (RFC 9113 5.1.1). private int _highestStream; + // Streams closed lately, and how, for the frames that still arrive on them (RFC 9113 5.1): each an + // id with its ClosedBy kind above it. A ring, the newest over the oldest, so it is bounded whatever + // the peer does, and looked up only for a frame on a stream that is not open. RFC 9113 5.1 lets a + // receiver limit how long it remembers a stream it reset, and a stream past the ring is treated + // as one this side reset: its DATA and HEADERS are dropped, so a client whose frames were simply + // late is never refused for them. + private readonly long[] _closedStreams = new long[ClosedStreamMemory]; + private int _closedNext; + private const int ClosedStreamMemory = 128; + private const int ClosedByPeer = 1, ClosedByPeerReset = 2, ClosedByUs = 3; + + // Ranges of ids the client skipped as it opened higher ones, so never opened: HEADERS on one is an + // unexpected stream id (RFC 9113 5.1.1). A ring of the latest, packed low id over high id; a + // client that skips more often than it holds is let off rather than refused. + private readonly long[] _skippedStreams = new long[SkippedRanges]; + private int _skippedNext; + private const int SkippedRanges = 16; + // The handler takes a body reader: a request is dispatched at END_HEADERS, its body to follow. private bool _streamBodies; @@ -442,6 +464,11 @@ private void RetireStream(PendingRequest pending) { ResetStream(pending.StreamId, Http2Error.NoError); } + else if (live) + { + // Its END_STREAM is in, or, behind a buffered rest parked on credit, the upload is dropped until the rest's own reset. + RememberClosed(pending.StreamId, pending.RequestEnded ? ClosedByPeer : ClosedByUs); + } Recycle(pending); @@ -452,6 +479,54 @@ private void RetireStream(PendingRequest pending) } } + private void RememberClosed(int streamId, int how) + { + _closedStreams[_closedNext] = ((long)how << 32) | (uint)streamId; + _closedNext = (_closedNext + 1) % ClosedStreamMemory; + } + + // ClosedByUs wins: once this side has reset a stream, whatever follows is dropped, whoever ended it + // first. Then the peer's reset over its END_STREAM. Zero for a stream past the ring, or never opened. + private int ClosedHow(int streamId) + { + ReadOnlySpan closed = _closedStreams; + for (int how = ClosedByUs; how >= ClosedByPeer; how--) + { + if (closed.Contains(((long)how << 32) | (uint)streamId)) + { + return how; + } + } + return 0; + } + + // A new stream: every id the client skipped to reach it is one it can never open (RFC 9113 5.1.1). + private void OpenStreamId(int streamId) + { + int next = _highestStream == 0 ? 1 : _highestStream + 2; + if (streamId > next) + { + _skippedStreams[_skippedNext] = ((long)next << 32) | (uint)(streamId - 2); + _skippedNext = (_skippedNext + 1) % SkippedRanges; + } + _highestStream = streamId; + } + + private bool NeverOpened(int streamId) + { + foreach (long range in _skippedStreams) + { + if (range != 0 && streamId >= (int)(range >> 32) && streamId <= (int)range) + { + return true; + } + } + return false; + } + + // RFC 9113 5.1.1: a stream the client has not opened yet, or one only a server could, which this one never does. + private bool IsIdle(int streamId) => streamId > _highestStream || (streamId & 1) == 0; + // Kept unless the pool is full or a peer's header lines grew its lists; only the reactor rents. private void Recycle(PendingRequest pending) { diff --git a/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs b/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs index 2b0c5d8f..46b99bab 100644 --- a/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs +++ b/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs @@ -124,21 +124,29 @@ public static void Register(Runner runner) })); // Control: at or below the highest id opened a stream is closed, not idle - stream 1 by - // opening 3 (RFC 9113 5.1.1), stream 3 once answered - and its DATA is still dropped. + // opening 3 (RFC 9113 5.1.1) - and its DATA is still dropped. Assert.Equal("HEADERS 3", H2cClient.Verdict(port, c => { c.RequestHeadersOnly(streamId: 3, endHeaders: true, endStream: false); c.WriteFrame(0x0, flags: 0, streamId: 1, "x"u8); c.WriteFrame(0x0, flags: 0x1, streamId: 3, []); })); + + // A stream answered after the client's END_STREAM is closed too, but by the client, whose + // DATA after it is its error (RFC 9113 5.1, a MUST in RFC 7540): STREAM_CLOSED, not PROTOCOL_ERROR. using (var client = new H2cClient(port)) { client.Open(); client.Request(streamId: 3); Assert.True(client.AwaitResponse(streamId: 3), "server did not answer stream 3"); client.WriteFrame(0x0, flags: 0x1, streamId: 3, "x"u8); - client.Request(streamId: 5); - Assert.True(client.AwaitResponse(streamId: 5), "DATA on a stream already answered ended the connection"); + string verdict; + do + { + verdict = client.AwaitVerdict(); // past the rest of the answer + } + while (verdict == "DATA 3"); + Assert.Equal("GOAWAY STREAM_CLOSED", verdict); } AssertServes(port); diff --git a/tests/Ioxide.Tests.Chaos/H2cClient.cs b/tests/Ioxide.Tests.Chaos/H2cClient.cs index 82f46f51..8954035e 100644 --- a/tests/Ioxide.Tests.Chaos/H2cClient.cs +++ b/tests/Ioxide.Tests.Chaos/H2cClient.cs @@ -544,6 +544,7 @@ private static string ErrorName(byte[] payload, int at) { 0x1 => "PROTOCOL_ERROR", 0x3 => "FLOW_CONTROL_ERROR", + 0x5 => "STREAM_CLOSED", 0x7 => "REFUSED_STREAM", 0x9 => "COMPRESSION_ERROR", _ => $"0x{code:x}", diff --git a/tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs b/tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs new file mode 100644 index 00000000..3d9f9c4b --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs @@ -0,0 +1,140 @@ +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// What the connection remembers of closed streams, and what it does when it does not. A stream this +/// side reset or stopped reading takes the peer's late frames without complaint (RFC 9113 5.1), and so +/// does one closed too long ago to be remembered: a client whose frames were only late is never +/// refused for them. Each block is still decoded, so HPACK stays in step. +/// +internal static class Http2ClosedStreamTests +{ + private const byte Data = 0x0, Headers = 0x1, Priority = 0x2, Continuation = 0x9; + private const byte EndStream = 0x1, EndHeaders = 0x4; + + public static void Register(Runner runner) + { + runner.Test("h2 closed streams: trailers on a stream answered early, after more closes than the connection remembers, are dropped and the connection goes on", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync((request, _) => + { + served.Add(request.StreamId); + return ValueTask.FromResult(Http2Response.Text("ok")); // answered unread: stream 1's upload is reset with NO_ERROR + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Post(1)); + for (int stream = 3; stream <= 263; stream += 2) + { + client.Feed(Http2FlowControlTests.Get(stream)); // 131 closes, past what the connection remembers + } + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Insert)); // stream 1's trailers, sent before the client saw the reset + client.Feed(Frame(Headers, EndHeaders | EndStream, 265, [0x82, 0x86, 0x84, 0xBE])); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x0", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(133, served.Count); + Assert.Equal(265, served[^1]); + }); + + // A buffered request ready, so closed by the peer's END_STREAM, then reset by this side: the later wins. + runner.Test("h2 closed streams: DATA on a stream the peer ended and this side then reset is dropped, not refused with the connection", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + Task run = client.Connection.RunAsync(async request => + { + if (request.StreamId == 1) + { + await gate.Task; + } + return Http2Response.Text("ok"); + }); + client.SendRequests(1); + client.Drain(); + + client.Feed(Frame(Priority, 0, 1, [0, 0, 0, 3])); // a stream error: this side resets it + client.Feed(Frame(Data, EndStream, 1, [1])); + client.Feed(Http2FlowControlTests.Get(3)); + gate.SetResult(); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x6", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + + // RetireStream with the answer parked on the client's credit: no reset yet, but the upload is dropped. + runner.Test("h2 closed streams: DATA and trailers behind an answer parked on credit are dropped, and the answer still ends whole", () => + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((request, _) => ValueTask.FromResult(request.StreamId == 1 + ? new Http2Response { Status = 200, Body = new byte[70_000] } // past the client's 65535-byte window, unread + : Http2Response.Text("ok"))); + client.SendRequests(); + List frames = client.Drain(); + + client.Feed(Post(1)); + frames.AddRange(client.Drain()); + client.Feed(Frame(Data, 0, 1, new byte[10])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Insert)); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + frames.AddRange(client.Drain()); + client.SendWindowUpdate(1, 70_000); + client.SendWindowUpdate(0, 70_000); + frames.AddRange(client.Drain()); + client.Close(run); + + Assert.Equal(0, frames.Count(f => f.Type == 0x7)); + Assert.Equal(70_000, Ioxide.Tests.Frame.Body(frames, 1).Length); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + + foreach (bool halfClosed in new[] { false, true }) + { + bool answering = halfClosed; + runner.Test($"h2 closed streams: HEADERS continued in CONTINUATION on a stream {(answering ? "the peer ended, its answer still to come, reset the stream" : "this side reset are dropped")}, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + var served = new List(); + Task run = client.Connection.RunAsync(async (request, _) => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + if (request.StreamId == 1 && answering) + { + await gate.Task; + } + return Http2Response.Text("ok"); // unread: an upload still arriving is reset with NO_ERROR + }); + client.SendRequests(); + client.Drain(); + + client.Feed(answering ? Http2FlowControlTests.Get(1) : Post(1)); + client.Feed(Frame(Headers, EndStream, 1, Insert[..3])); + client.Feed(Frame(Continuation, EndHeaders, 1, Insert[3..])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + gate.SetResult(); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal(answering ? "RST_STREAM 1 0x5" : "RST_STREAM 1 0x0", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["1 ", "3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + } + + // Inserts x-b: w into the dynamic table, which a later GET names by index. + private static byte[] Insert => Http2TrailerTests.Field("x-b", "w", index: true); + + // A POST, its body still to come. + private static byte[] Post(int streamId) => Frame(Headers, EndHeaders, streamId, [0x83, 0x86, 0x84]); + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); +} diff --git a/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs new file mode 100644 index 00000000..7d6c7a87 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs @@ -0,0 +1,166 @@ +using System.Buffers.Binary; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// The framing of PRIORITY, PUSH_PROMISE, RST_STREAM, SETTINGS and PING (RFC 9113 6.3-6.7), and the +/// GOAWAY a connection error is answered with (6.8). A malformed control frame is the peer's error, of +/// the stream or of the connection as the RFC names it, and never something taken as it came. +/// +internal static class Http2ControlFrameTests +{ + private const byte Headers = 0x1, Priority = 0x2, RstStream = 0x3, Settings = 0x4, PushPromise = 0x5, Ping = 0x6, GoAway = 0x7; + private const byte EndStream = 0x1, Ack = 0x1, EndHeaders = 0x4, PriorityFlag = 0x20; + + public static void Register(Runner runner) + { + // Each one follows stream 1, served, so the GOAWAY has a stream to name; stream 3 comes after it. + foreach ((string name, byte[] frame, string expected) in new[] + { + ("a PRIORITY on stream 0", Frame(Priority, 0, 0, Dependency(1)), "GOAWAY 1 0x1"), + ("a PRIORITY of four octets on an idle stream", Frame(Priority, 0, 5, [0, 0, 0, 1]), "GOAWAY 1 0x6"), + ("a PRIORITY on an idle stream that depends on itself", Frame(Priority, 0, 5, Dependency(5)), "GOAWAY 1 0x1"), + ("a PUSH_PROMISE", Frame(PushPromise, EndHeaders, 1, [0, 0, 0, 2, 0x82, 0x86, 0x84]), "GOAWAY 1 0x1"), + ("an RST_STREAM on stream 0", Frame(RstStream, 0, 0, [0, 0, 0, 8]), "GOAWAY 1 0x1"), + ("an RST_STREAM of three octets", Frame(RstStream, 0, 1, [0, 0, 8]), "GOAWAY 1 0x6"), + ("a SETTINGS ACK with a payload", Frame(Settings, Ack, 0, [0, 4, 0, 0, 0xff, 0xff]), "GOAWAY 1 0x6"), + ("a SETTINGS on stream 1", Frame(Settings, 0, 1, []), "GOAWAY 1 0x1"), + ("SETTINGS_ENABLE_PUSH 2", Http2FlowControlTests.Settings((0x2, 2)), "GOAWAY 1 0x1"), + ("a PING on stream 1", Frame(Ping, 0, 1, new byte[8]), "GOAWAY 1 0x1"), + ("a DATA on an idle stream", Frame(0x0, 0, 9, [1]), "GOAWAY 1 0x1"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 control frames: {shape} ends the connection, and its GOAWAY names the last stream served", () => + { + (List frames, List served) = Exchange(bad); + + Assert.Equal(answer, Errors(frames)); + Assert.True(served.SequenceEqual([1]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + runner.Test("h2 control frames: control: a well-formed PRIORITY, SETTINGS_ENABLE_PUSH 0 and 1, an empty SETTINGS ACK and a PING take nothing down", () => + { + (List frames, List served) = Exchange( + [ + .. Frame(Priority, 0, 1, Dependency(0)), // a closed stream + .. Frame(Priority, 0, 7, Dependency(1)), // an idle one, which it leaves idle + .. Http2FlowControlTests.Settings((0x2, 0)), + .. Http2FlowControlTests.Settings((0x2, 1)), + .. Frame(Settings, Ack, 0, []), + .. Frame(Ping, 0, 0, [1, 2, 3, 4, 5, 6, 7, 8]), + ]); + + Assert.Equal("", Errors(frames)); + Assert.True(served.SequenceEqual([1, 3]), $"the handler was handed: {string.Join(", ", served)}"); + Assert.Equal(1, frames.Count(f => f.Type == Ping && f.Flags == Ack && f.Payload.SequenceEqual(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }))); + }); + + // A request still arriving, so the stream is open when the frame names it. + foreach ((string name, byte[] frame, string expected) in new[] + { + ("a PRIORITY of four octets", Frame(Priority, 0, 1, [0, 0, 0, 3]), "RST_STREAM 1 0x6"), + ("a PRIORITY that depends on its own stream", Frame(Priority, 0, 1, Dependency(1)), "RST_STREAM 1 0x1"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 control frames: {shape} resets a request still arriving, and the next is served", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Frame(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])); // a POST, its body still to come + client.Feed(bad); + client.Feed(Http2FlowControlTests.Get(3)); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal(answer, Errors(frames)); + Assert.True(served.SequenceEqual([3]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // RFC 7540 5.3.1, which h2spec and nghttp2 still hold a server to. + runner.Test("h2 control frames: HEADERS whose priority depends on their own stream are reset before any handler runs, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add($"{request.StreamId} {Fields(request)}"); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + byte[] insert = Http2TrailerTests.Field("x-b", "w", index: true); // only counts if the block was decoded + client.Feed(Frame(Headers, EndHeaders | EndStream | PriorityFlag, 1, [.. Dependency(1), 0x82, 0x86, 0x84, .. insert])); + client.Feed(Frame(Headers, EndHeaders | EndStream | PriorityFlag, 3, [.. Dependency(1), 0x82, 0x86, 0x84, 0xBE])); // control: on another stream + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x1", Errors(frames)); + Assert.True(served.SequenceEqual(["3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // Stream 1 served, the frame, then stream 3. + private static (List Frames, List Served) Exchange(byte[] frame) + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(1); + List frames = client.Drain(); + client.Feed(frame); + client.Feed(Http2FlowControlTests.Get(3)); + frames.AddRange(client.Drain()); + client.Close(run); + return (frames, served); + } + + private static byte[] Dependency(int streamId) + { + byte[] priority = new byte[5]; + BinaryPrimitives.WriteInt32BigEndian(priority, streamId); + priority[4] = 15; + return priority; + } + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); + + internal static string Fields(Http2Request request) + { + var fields = new List(); + foreach (KeyValuePair, ReadOnlyMemory> field in request.Headers.AsSpan()) + { + fields.Add($"{System.Text.Encoding.ASCII.GetString(field.Key.Span)}={System.Text.Encoding.ASCII.GetString(field.Value.Span)}"); + } + return string.Join(' ', fields); + } + + // Every RST_STREAM and GOAWAY, in wire order: a GOAWAY by its last stream id and code. + internal static string Errors(List frames) => string.Join(", ", frames + .Where(f => f.Type is RstStream or GoAway) + .Select(f => f.Type == RstStream + ? $"RST_STREAM {f.StreamId} 0x{BinaryPrimitives.ReadUInt32BigEndian(f.Payload):x}" + : $"GOAWAY {BinaryPrimitives.ReadUInt32BigEndian(f.Payload)} 0x{BinaryPrimitives.ReadUInt32BigEndian(f.Payload.AsSpan(4)):x}")); +} diff --git a/tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs b/tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs new file mode 100644 index 00000000..025e582f --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs @@ -0,0 +1,162 @@ +using System.Buffers.Binary; +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// The stream errors a PRIORITY, or the priority fields of a HEADERS, can raise (RFC 9113 6.3, RFC 7540 +/// 5.3.1), on a stream in each state it can be in: never opened, being decoded, or being served by a +/// handler that is reading its body and parked on credit for its response. +/// +internal static class Http2PriorityResetTests +{ + private const byte Headers = 0x1, Priority = 0x2, Continuation = 0x9; + private const byte EndStream = 0x1, EndHeaders = 0x4, PriorityFlag = 0x20; + + public static void Register(Runner runner) + { + // RFC 9113 6.4: no RST_STREAM may name a stream that was never opened. + foreach ((string name, int streamId) in new[] { ("an even stream", 2), ("an odd stream skipped below the highest", 3) }) + { + string shape = name; + int stream = streamId; + runner.Test($"h2 priority: a PRIORITY of four octets on {shape} ends the connection rather than reset a stream never opened", () => + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(_ => Http2Response.Text("ok")); + client.SendRequests(1, 5); + List frames = client.Drain(); + client.Feed(Frame(Priority, 0, stream, [0, 0, 0, 1])); + frames.AddRange(client.Drain()); + client.Close(run); + + Assert.Equal("GOAWAY 5 0x6", Http2ControlFrameTests.Errors(frames)); + }); + } + + // Each frame lands while the handler has a read parked on the body and a flush parked on credit. + byte[] insert = Http2TrailerTests.Field("x-b", "w", index: true); + foreach ((string name, byte[] frame, string expected) in new[] + { + ("a PRIORITY of four octets", Frame(Priority, 0, 1, [0, 0, 0, 3]), "RST_STREAM 1 0x6"), + ("trailers whose priority depends on their own stream", Frame(Headers, EndHeaders | EndStream | PriorityFlag, 1, [.. Dependency(1), .. insert]), "RST_STREAM 1 0x1"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 priority: {shape} on a stream being served ends its read, wakes its flush parked on credit to the peer gone, and nothing more goes out on it", () => + { + using var client = new StrictClient(); + var read = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var flushed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var served = new List(); + Task run = client.Connection.RunAsync(async (request, body, writer) => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + writer.WriteHeaders(new Http2Response { Status = 200 }); + if (request.StreamId != 1) + { + return; + } + _ = ReadAsync(body, read); + writer.GetSpan(70_000)[..70_000].Fill((byte)'x'); // past the peer's 65535-byte window + writer.Advance(70_000); + flushed.TrySetResult((await writer.FlushAsync()).IsCompleted ? "peer gone" : "flushed"); + }); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Http2FlowControlTests.Open(), .. Frame(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])]); + client.Drain(); + Assert.True(!read.Task.IsCompleted && !flushed.Task.IsCompleted, "precondition: the handler was meant to be parked on its read and its flush"); + + client.Feed(bad); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, .. (bad[3] == Headers ? (byte[])[0xBE] : [])])); + List frames = client.Drain(); + + Assert.True(read.Task.Wait(2_000), "the read parked on the body never woke"); + Assert.True(read.Task.Result.StartsWith("IOException"), $"the read ended with: {read.Task.Result}"); + Assert.True(flushed.Task.Wait(2_000), "the flush parked on credit never woke"); + Assert.Equal("peer gone", flushed.Task.Result); + Assert.Equal(answer, Http2ControlFrameTests.Errors(frames)); + Assert.Equal(0, frames.Count(f => f.StreamId == 1 && f.Type != 0x3)); + Assert.Equal(bad[3] == Headers ? "3 x-b=w" : "3 ", served[^1]); + client.Close(run); + }); + } + + runner.Test("h2 priority: HEADERS depending on their own stream, the block continued in CONTINUATION, are reset once it is in, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Frame(Headers, EndStream | PriorityFlag, 1, [.. Dependency(1), 0x82, 0x86])); + client.Feed(Frame(Continuation, EndHeaders, 1, [0x84, .. Http2TrailerTests.Field("x-b", "w", index: true)])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 priority: trailers depending on their own stream reset a buffered request still arriving, and it is never served", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Frame(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])); + client.Feed(Http2FlowControlTests.FrameBytes(0x0, 0, 1, new byte[4])); + client.Feed(Frame(Headers, EndHeaders | EndStream | PriorityFlag, 1, [.. Dependency(1), .. Http2TrailerTests.Field("x-checksum", "abc")])); + client.Feed(Http2FlowControlTests.Get(3)); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([3]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + private static async Task ReadAsync(Http2BodyReader body, TaskCompletionSource outcome) + { + try + { + var bytes = new List(); + for (ReadOnlyMemory chunk = await body.ReadAsync(); !chunk.IsEmpty; chunk = await body.ReadAsync()) + { + bytes.AddRange(chunk.ToArray()); + } + outcome.TrySetResult($"ended after {Encoding.ASCII.GetString([.. bytes])}"); + } + catch (Exception e) + { + outcome.TrySetResult(e.GetType().Name); + } + } + + private static byte[] Dependency(int streamId) + { + byte[] priority = new byte[5]; + BinaryPrimitives.WriteInt32BigEndian(priority, streamId); + priority[4] = 15; + return priority; + } + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); +} diff --git a/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs new file mode 100644 index 00000000..0c08e549 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs @@ -0,0 +1,195 @@ +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// Frames on streams that are not open (RFC 9113 5.1, 5.1.1). On an idle stream, or one a client +/// could never have opened, they are a connection error. After the peer's END_STREAM they are its +/// error, of the stream while the response is still going and of the connection once it is closed, +/// and nothing of them reaches a body reader. After this side's own reset they are dropped: the peer +/// sent them before it could know. +/// +internal static class Http2StreamStateTests +{ + private const byte Data = 0x0, Headers = 0x1, RstStream = 0x3, WindowUpdate = 0x8; + private const byte EndStream = 0x1, EndHeaders = 0x4; + + public static void Register(Runner runner) + { + // After stream 1, served and closed both ways. + foreach ((string name, byte[] frame, string expected) in new[] + { + ("an RST_STREAM on an idle stream", Frame(RstStream, 0, 3, [0, 0, 0, 8]), "GOAWAY 1 0x1"), + ("a WINDOW_UPDATE on an idle stream", Frame(WindowUpdate, 0, 3, [0, 0, 0, 1]), "GOAWAY 1 0x1"), + ("a WINDOW_UPDATE on an even stream, which only a server could open,", Frame(WindowUpdate, 0, 2, [0, 0, 0, 1]), "GOAWAY 1 0x1"), + ("HEADERS on an even stream", Frame(Headers, EndHeaders | EndStream, 2, [0x82, 0x86, 0x84]), "GOAWAY 1 0x1"), + ("a DATA after the stream closed both ways", Frame(Data, EndStream, 1, [1]), "GOAWAY 1 0x5"), + ("HEADERS after the stream closed both ways", Frame(Headers, EndHeaders | EndStream, 1, [0x82, 0x86, 0x84]), "GOAWAY 1 0x5"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 stream states: {shape} ends the connection", () => + { + (List frames, List served) = Exchange([.. Http2FlowControlTests.Get(1)], bad); + + Assert.Equal(answer, Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([1]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + runner.Test("h2 stream states: control: a WINDOW_UPDATE, an RST_STREAM and a PRIORITY on a stream closed both ways take nothing down", () => + { + (List frames, List served) = Exchange([.. Http2FlowControlTests.Get(1)], + [ + .. Frame(WindowUpdate, 0, 1, [0, 0, 0, 1]), // sent before the client saw the END_STREAM, as RFC 9113 5.1 allows + .. Frame(RstStream, 0, 1, [0, 0, 0, 8]), + .. Frame(0x2, 0, 1, [0, 0, 0, 0, 15]), + ]); + + Assert.Equal("", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([1, 7]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 stream states: HEADERS on a stream below one already opened, and never opened itself, end the connection", () => + { + (List frames, List served) = Exchange([.. Http2FlowControlTests.Get(5)], Http2FlowControlTests.Get(1)); + + Assert.Equal("GOAWAY 5 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([5]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + // RFC 9113 5.1: after the peer's RST_STREAM, a stream error; the stream is then this side's, so a second frame is dropped. + runner.Test("h2 stream states: a DATA after the peer's RST_STREAM resets the stream with STREAM_CLOSED, once, and the connection goes on", () => + { + (List frames, List served) = Exchange([], [.. Post(1), .. Frame(RstStream, 0, 1, [0, 0, 0, 8]), .. Frame(Data, 0, 1, [1]), .. Frame(Data, EndStream, 1, [1])]); + + Assert.Equal("RST_STREAM 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([7]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + // The issue's own case: no content-length, so nothing else would tell the reader the bytes are not the body's. + runner.Test("h2 stream states: a DATA after END_STREAM, with the handler still reading, resets the stream, and its body reader hands out none of it", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + var read = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Task run = client.Connection.RunAsync(async (request, body) => + { + if (request.StreamId != 1) + { + return Http2Response.Text("ok"); + } + await gate.Task; // reads only once every frame below is in + var bytes = new List(); + try + { + for (ReadOnlyMemory chunk = await body.ReadAsync(); !chunk.IsEmpty; chunk = await body.ReadAsync()) + { + bytes.AddRange(chunk.ToArray()); + } + } + catch (IOException) + { + bytes.AddRange("|reset"u8.ToArray()); // as after the peer's own reset: the stream is gone under it + } + read.TrySetResult(Encoding.ASCII.GetString([.. bytes])); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed([.. Post(1), .. Frame(Data, EndStream, 1, "body"u8), .. Frame(Data, 0, 1, "late"u8)]); + client.Feed(Http2FlowControlTests.Get(3)); // the connection goes on + gate.SetResult(); + List frames = client.Drain(); + + Assert.True(read.Task.Wait(2_000), "the handler never finished reading"); + Assert.Equal("|reset", read.Task.Result); + Assert.Equal("RST_STREAM 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + client.Close(run); + }); + + runner.Test("h2 stream states: HEADERS after END_STREAM, with the answer still to come, reset the stream, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + var served = new List(); + Task run = client.Connection.RunAsync(async request => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + if (request.StreamId == 1) + { + await gate.Task; + } + return Http2Response.Text("ok"); + }); + client.SendRequests(1); // its handler parks: half-closed (remote), the answer still to come + client.Drain(); + + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Http2TrailerTests.Field("x-b", "w", index: true))); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + gate.SetResult(); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(0, frames.Count(f => f.Type == Headers && f.StreamId == 1)); // the reset ends the answer too + Assert.True(served.SequenceEqual(["1 ", "3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + // RFC 9113 5.1: what follows this side's own reset was sent before the peer knew of it. + runner.Test("h2 stream states: control: DATA and trailers on a stream this side reset are dropped, and the connection goes on", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync((request, body) => + { + served.Add(request.StreamId); + return ValueTask.FromResult(Http2Response.Text("early")); // answered unread: the upload is reset with NO_ERROR + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Post(1)); + client.Feed(Frame(Data, 0, 1, new byte[10])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Http2TrailerTests.Field("x-b", "w", index: true))); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x0", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([1, 3]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // The opening frames, then the one under test, then stream 7's GET. + private static (List Frames, List Served) Exchange(byte[] before, byte[] frame) + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + List frames = client.Drain(); + client.Feed(before); + frames.AddRange(client.Drain()); + client.Feed(frame); + client.Feed(Http2FlowControlTests.Get(7)); + frames.AddRange(client.Drain()); + client.Close(run); + return (frames, served); + } + + // A POST, its body still to come. + private static byte[] Post(int streamId) => Frame(Headers, EndHeaders, streamId, [0x83, 0x86, 0x84]); + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index 95146068..7a3d144d 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -50,6 +50,10 @@ private static int Main() Nghttp2EarlyAnswerTests.Register(runner); Http2PeerClosedFlushTests.Register(runner); Http2StreamResetTests.Register(runner); + Http2ControlFrameTests.Register(runner); + Http2PriorityResetTests.Register(runner); + Http2StreamStateTests.Register(runner); + Http2ClosedStreamTests.Register(runner); HpackEncodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner);