Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 221 additions & 19 deletions src/protocols/ioxide.http2/Http2Connection.Frames.cs

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/protocols/ioxide.http2/Http2Connection.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@
{
Discard(pending);
}
RememberClosed(streamId, ClosedByUs); // what the peer sends before it hears of this is dropped (RFC 9113 5.1)

Span<byte> frame = stackalloc byte[FrameHeader.Size + 4];
new FrameHeader(4, FrameType.RstStream, FrameFlags.None, streamId).Write(frame);
Expand All @@ -338,7 +339,9 @@
{
Span<byte> 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;
Expand Down Expand Up @@ -406,7 +409,7 @@
}
}

Span<byte> header = stackalloc byte[FrameHeader.Size];

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 412 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
new FrameHeader(chunk, first ? FrameType.Headers : FrameType.Continuation, flags, streamId)
.Write(header);

Expand Down Expand Up @@ -447,7 +450,7 @@

bool last = offset + chunk >= body.Length;

Span<byte> header = stackalloc byte[FrameHeader.Size];

Check warning on line 453 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 453 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 453 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 453 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 453 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 453 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
new FrameHeader(chunk, FrameType.Data, last ? FrameFlags.EndStream : FrameFlags.None, streamId)
.Write(header);

Expand Down
75 changes: 75 additions & 0 deletions src/protocols/ioxide.http2/Http2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,35 @@ 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;

// The highest stream id the client has opened, refused ones included; a HEADERS at or below it
// 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;

Expand Down Expand Up @@ -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);

Expand All @@ -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<long> 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)
{
Expand Down
14 changes: 11 additions & 3 deletions tests/Ioxide.Tests.Chaos/H2ChaosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.Chaos/H2cClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
140 changes: 140 additions & 0 deletions tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using ioxide.http2;

namespace Ioxide.Tests;

/// <summary>
/// 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.
/// </summary>
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<int>();
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<Frame> 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<Frame> 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<Frame> 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<string>();
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<Frame> 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<byte> payload)
=> Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload);
}
Loading
Loading