diff --git a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.Egress.cs b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.Egress.cs index 3109639f..81f6f79c 100644 --- a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.Egress.cs +++ b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.Egress.cs @@ -1,5 +1,6 @@ using System.Buffers; using System.IO.Pipelines; +using System.Runtime.CompilerServices; namespace ioxide.nghttp2; @@ -96,7 +97,11 @@ private static int WriteStatus(int status, Span destination) private bool _draining; private bool _drainAgain; - private async ValueTask FlushEgressAsync() + // The read loop's wait, at its end, for the drain in flight to be done. + private TaskCompletionSource? _drained; + + // A detached drain, as the read loop's is, has nobody to throw to: its fault is reported through IsBroken. + private async ValueTask FlushEgressAsync(bool detached = false) { if (_handle == 0) { @@ -146,12 +151,60 @@ private async ValueTask FlushEgressAsync() } while (_drainAgain && _handle != 0); } + catch + { + _failed = true; // the transport refused a write: nothing queued can leave now + if (!detached) + { + throw; + } + } finally { - _draining = false; + if (OffReactor) + { + _ = EndDrainOnReactorAsync(); // a flush that faulted ends the drain on the thread that completed it + } + else + { + EndDrain(); + } + } + } + + private async Task EndDrainOnReactorAsync() + { + try + { + await new ReactorSwitch(_reactor); + } + catch (ObjectDisposedException) + { + // The reactor has stopped, so nothing races the end of the drain: it goes on here. } + EndDrain(); + } + + // Out of line: what it runs loops, and a loop in an async method's finally costs it its optimized code. + [MethodImpl(MethodImplOptions.NoInlining)] + private void EndDrain() + { + _draining = false; + // The read loop reads on behind a drain, so one that found the connection gone ends that read, or the next. + if (_failed && !_disposed) + { + _pipe.Input.CancelPendingRead(); + } + + FireBodyWakes(); // a stream this drain closed, sending its RST_STREAM, ends its reader now WakeParkedWriters(); + + if (_drained is { } drained) + { + _drained = null; + drained.SetResult(); + } } private unsafe int DrainOnce() diff --git a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs index 403abf10..9adb3e0c 100644 --- a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs +++ b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.RunBuffered.cs @@ -46,7 +46,8 @@ private async Task RunCoreAsync(Action dispatch) Setup(); // SETTINGS go out before anything is read: a peer that opens with the preface and a - // request immediately expects ours to be on the way. + // request immediately expects ours to be on the way. Waited for, unlike a pass's drain: the + // peer may send only 65,535 bytes until it has them, and the first pass finds no drain in flight. await FlushEgressAsync(); while (!IsBroken) @@ -55,6 +56,13 @@ private async Task RunCoreAsync(Action dispatch) _reactor ??= Reactor.Current; // a RunAsync begun off the reactor learns it here await new ReactorSwitch(_reactor); // a pipe may complete its read elsewhere: a default Pipe on the thread pool + // A drain the loop read on behind found the connection gone, and ended this read to say so. + if (IsBroken) + { + _pipe.Input.AdvanceTo(read.Buffer.End); + break; + } + _inPass = true; Feed(read.Buffer); _pipe.Input.AdvanceTo(read.Buffer.End); @@ -66,16 +74,25 @@ private async Task RunCoreAsync(Action dispatch) // this pass's drain rather than a write of its own. FireBodyWakes(); _inPass = false; - await FlushEgressAsync(); + + // Not waited for: an upload behind a stalled download is read on, not left to overflow the receive queue. + _ = FlushEgressAsync(detached: true); // A stream nghttp2 reset itself closes as the drain sends the RST_STREAM; its reader ends now. FireBodyWakes(); if (read.IsCompleted || read.IsCanceled || Nghttp2.ih2_is_dead(_handle) != 0) { - return; + break; } } + + // What the loop left to a drain still in flight, a GOAWAY among it, goes out before the teardown, if anything still can. + if (_draining && !IsBroken) + { + _drained = new TaskCompletionSource(); + await _drained.Task; + } } catch (Exception) { diff --git a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.cs b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.cs index dfdf6478..7fdba63c 100644 --- a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.cs +++ b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Connection.cs @@ -104,6 +104,7 @@ private void Retire(PendingRequest pending) /// public Nghttp2Connection(IDuplexPipe pipe, Nghttp2Options? options = null) { + Nghttp2.RequireAbi(); _pipe = pipe; _options = options ?? new Nghttp2Options(); } @@ -134,7 +135,7 @@ private unsafe void Setup() }; _handle = Nghttp2.ih2_server_new(callbacks, (void*)GCHandle.ToIntPtr(_self), - _streamBodies ? 1 : 0); + _streamBodies ? 1 : 0, (uint)_options.MaxConcurrentStreams, (uint)_options.InitialWindowSize); if (_handle == 0) { _failed = true; diff --git a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Options.cs b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Options.cs index 1ea44f0b..f4dc4b30 100644 --- a/src/protocols/ioxide.nghttp2/Connection/Nghttp2Options.cs +++ b/src/protocols/ioxide.nghttp2/Connection/Nghttp2Options.cs @@ -17,4 +17,25 @@ public sealed record Nghttp2Options /// ENHANCE_YOUR_CALM, never dispatched. /// public int MaxHeaderListSize { get; init; } = 64 * 1024; + + /// + /// Flow-control window advertised per stream, and enforced by nghttp2: how much of one request's + /// body the peer may send ahead of the credit the server gives back. nghttp2 answers a DATA frame + /// past it by ending the connection with FLOW_CONTROL_ERROR, which RFC 9113 6.9 allows as well as + /// a stream error. A buffered body is credited as it arrives, a streamed one as its handler reads + /// it, so a handler that stops reading holds one window and stalls only its own upload. + /// + /// The connection's own window is opened to 2^31-1 right behind the server's SETTINGS, and nghttp2 + /// tops it up once half of it is consumed, so what bounds a connection's request bodies in flight + /// is times this. nghttp2 holds a stream to it once the peer + /// has acknowledged the SETTINGS, and to the RFC's 65,535 until then. + /// + public int InitialWindowSize { get; init; } = 256 * 1024; + + /// + /// Streams the peer may have open at once, advertised in SETTINGS and enforced by nghttp2. It also + /// bounds a connection's request bodies in flight: this many streams times + /// , since the connection's window is not the limit. + /// + public int MaxConcurrentStreams { get; init; } = 100; } diff --git a/src/protocols/ioxide.nghttp2/Http/Nghttp2BodyReader.cs b/src/protocols/ioxide.nghttp2/Http/Nghttp2BodyReader.cs index a0e923d9..0c235771 100644 --- a/src/protocols/ioxide.nghttp2/Http/Nghttp2BodyReader.cs +++ b/src/protocols/ioxide.nghttp2/Http/Nghttp2BodyReader.cs @@ -17,9 +17,9 @@ namespace ioxide.nghttp2; /// one window instead of by the whole body, which is the difference that makes a hostile upload /// harmless. /// -/// As on ioxide.http2, credit is shared. Every stream rides one TCP connection, so a chunk -/// opens both the stream's window and the CONNECTION's - and a handler that never reads holds down -/// the connection window for every other stream on it, until it returns. +/// The window that holds an upload back is its stream's own. A chunk is credited to the +/// connection's window too, but that one is opened to its maximum and topped up as it is consumed, +/// so a handler that never reads holds back only its own upload, never the other streams'. /// /// /// Single consumer, on any thread: a read made off the reactor is served on it. Each diff --git a/src/protocols/ioxide.nghttp2/Interop/Nghttp2.cs b/src/protocols/ioxide.nghttp2/Interop/Nghttp2.cs index 8869eea5..898a9833 100644 --- a/src/protocols/ioxide.nghttp2/Interop/Nghttp2.cs +++ b/src/protocols/ioxide.nghttp2/Interop/Nghttp2.cs @@ -30,14 +30,16 @@ internal struct Callbacks public delegate* unmanaged OnStreamError; } - /// Create a server session and queue SETTINGS. The peer's connection preface is - /// validated out of . With non-zero nghttp2 - /// sends no WINDOW_UPDATE for DATA by itself - every byte handed to OnData is credited once, - /// through - and OnStreamError reports every close, NO_ERROR included. - /// Without it, OnStreamError reports every close but the normal end of a stream: NO_ERROR once - /// the response's END_STREAM has gone out. + /// Create a server session and queue SETTINGS, advertising + /// and , then the WINDOW_UPDATE that opens the connection's window to + /// 2^31-1. The peer's connection preface is validated out of . With + /// non-zero nghttp2 sends no WINDOW_UPDATE for DATA by itself - every byte + /// handed to OnData is credited once, through - and OnStreamError reports every + /// close, NO_ERROR included. Without it, OnStreamError reports every close but the normal end of a stream: + /// NO_ERROR once the response's END_STREAM has gone out. /// Returns the handle, or 0 on failure. - [DllImport(Lib)] internal static extern nint ih2_server_new(Callbacks callbacks, void* user, int manualFlow); + [DllImport(Lib)] internal static extern nint ih2_server_new(Callbacks callbacks, void* user, int manualFlow, + uint maxConcurrentStreams, uint initialWindowSize); /// Credit DATA the application is done with: the connection window, and the stream's /// while it is open. Manual flow control only. Returns 0, or a negative nghttp2 error. @@ -89,4 +91,36 @@ internal struct Callbacks /// Free the session and any retained request bodies. [DllImport(Lib)] internal static extern void ih2_free(nint connection); + + /// The shim's exported-surface revision; checked against as a connection is + /// built, so a stale native library fails loudly instead of taking arguments in the wrong places. + [DllImport(Lib)] private static extern uint ih2_abi(); + + /// What this managed binding was written against. Bump both together. + internal const uint Abi = 1; + + /// + /// Refuse to run against a native library this binding was not built for. The shim and this + /// assembly ship together, so a mismatch means a stale libioxide_nghttp2.so picked up out of a + /// build output. + /// + internal static void RequireAbi() + { + uint native; + try + { + native = ih2_abi(); + } + catch (EntryPointNotFoundException) + { + native = 0; // predates the counter, so certainly too old + } + + if (native != Abi) + { + throw new InvalidOperationException( + $"ioxide.nghttp2: the native library reports ABI {native}, this binding needs {Abi}. " + + "A stale libioxide_nghttp2.so is being loaded - rebuild it with scripts/build-nghttp2-native.sh."); + } + } } diff --git a/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c b/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c index 33392234..957d21d5 100644 --- a/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c +++ b/src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c @@ -9,7 +9,8 @@ * conn = ih2_client_new(cbs, user) one per TCP connection (client preface queued) * ih2_submit_request(hdrs, body) headers packed [u16 nlen][name][u16 vlen][value]* * - * conn = ih2_server_new(cbs, user, manual) one per accepted connection (server SETTINGS queued) + * conn = ih2_server_new(cbs, user, manual, one per accepted connection (server SETTINGS queued) + * streams, window) * ih2_submit_response(sid, hdrs, body) * ih2_consume(sid, len) manual flow control: credit DATA once it is read * @@ -29,6 +30,11 @@ #include +/* Exported-surface revision. Bump on any change to an exported signature or a struct crossing the + * boundary; ih2_abi() hands it to the managed side, which refuses to start on a mismatch. + * 1 - ih2_server_new gained max_concurrent_streams and initial_window_size */ +#define IH2_ABI 1 + /* ---- callback table into C# ------------------------------------------------------------- */ typedef struct ih2_callbacks { @@ -250,7 +256,8 @@ static ssize_t read_body(nghttp2_session *session, int32_t stream_id, uint8_t *b /* Both directions register the same callbacks and differ only in which nghttp2 constructor runs * and which SETTINGS go out first, so the wiring lives here once. */ -static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_flow) +static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_flow, + uint32_t max_concurrent_streams, uint32_t initial_window_size) { ih2_conn *c = calloc(1, sizeof(*c)); if (c == NULL) { @@ -298,10 +305,12 @@ static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_f * ENABLE_PUSH is off in both directions: nothing routes a pushed stream. */ nghttp2_settings_entry settings[] = { {NGHTTP2_SETTINGS_ENABLE_PUSH, 0}, - {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 1000}, - {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 1 << 20}, + {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, max_concurrent_streams}, + {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, initial_window_size}, }; - if (nghttp2_submit_settings(c->session, NGHTTP2_FLAG_NONE, settings, 3) != 0) { + /* SETTINGS size streams only, so this opens the connection's own window behind them (RFC 9113 6.9.2). */ + if (nghttp2_submit_settings(c->session, NGHTTP2_FLAG_NONE, settings, 3) != 0 || + nghttp2_session_set_local_window_size(c->session, NGHTTP2_FLAG_NONE, 0, NGHTTP2_MAX_WINDOW_SIZE) != 0) { nghttp2_session_del(c->session); free(c); return NULL; @@ -312,15 +321,18 @@ static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_f /* One per ACCEPTED connection. The peer is expected to have sent (or to be about to send) the * client connection preface; nghttp2 validates it out of ih2_read. manual_flow non-zero: every - * DATA byte handed to on_data is credited through ih2_consume, once. */ -ih2_conn *ih2_server_new(ih2_callbacks cbs, void *user, int manual_flow) + * DATA byte handed to on_data is credited through ih2_consume, once. The connection's receive + * window is opened to 2^31-1, so what the peer may send ahead of the server's credit is + * max_concurrent_streams times initial_window_size. */ +ih2_conn *ih2_server_new(ih2_callbacks cbs, void *user, int manual_flow, + uint32_t max_concurrent_streams, uint32_t initial_window_size) { - return ih2_new(cbs, user, 1, manual_flow); + return ih2_new(cbs, user, 1, manual_flow, max_concurrent_streams, initial_window_size); } ih2_conn *ih2_client_new(ih2_callbacks cbs, void *user) { - return ih2_new(cbs, user, 0, 0); + return ih2_new(cbs, user, 0, 0, 1000, 1 << 20); } /* Headers arrive packed as [u16 namelen][name][u16 valuelen][value]... - the same format the @@ -700,3 +712,8 @@ const char *ih2_strerror(int error_code) { return nghttp2_strerror(error_code); } + +uint32_t ih2_abi(void) +{ + return IH2_ABI; +} diff --git a/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so b/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so index 92924f3c..04bf6b94 100755 Binary files a/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so and b/src/protocols/ioxide.nghttp2/runtimes/linux-arm64/native/libioxide_nghttp2.so differ diff --git a/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so b/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so index 62318fe9..c0027521 100755 Binary files a/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so and b/src/protocols/ioxide.nghttp2/runtimes/linux-x64/native/libioxide_nghttp2.so differ diff --git a/tests/Ioxide.Tests.Chaos/Nghttp2ChaosTests.cs b/tests/Ioxide.Tests.Chaos/Nghttp2ChaosTests.cs index 4737853a..65a5620e 100644 --- a/tests/Ioxide.Tests.Chaos/Nghttp2ChaosTests.cs +++ b/tests/Ioxide.Tests.Chaos/Nghttp2ChaosTests.cs @@ -359,6 +359,9 @@ public static void Register(Runner runner) using var client = new H2cClient(port); client.Open(); + + // A chunk flushed while another drain is in flight leaves with that one, so the SETTINGS exchange settles first. + Assert.True(client.AwaitAnyOf([0x4]) == 0x4, "the server never acknowledged the client's SETTINGS"); client.Request(streamId: 1); (int frames, int bytes, bool ended) = client.DrainBody(streamId: 1); @@ -416,5 +419,48 @@ public static void Register(Runner runner) Assert.True(seen.Contains((1, Cap)), $"the handler saw {Describe(seen)}, not stream 1 with {Cap} body bytes"); }); + + runner.Test("nghttp2: a stream window of upload sent while the server's flush is stalled behind a download the client is not reading arrives whole", () => + { + // A download loopback's socket buffers cannot hold, and an upload in more 4 KiB receives than 64 queue entries hold. + const int Download = 32 * 1024 * 1024, Upload = 256 * 1024; + byte[] big = new byte[Download]; + int port = TestServer.Start(async (_, conn) => + { + try + { + await new Nghttp2Connection(conn).RunAsync(request => request.Path.Span.SequenceEqual("/download"u8) + ? new Nghttp2Response { Status = 200, Body = big } + : Nghttp2Response.Text(request.Body.Length.ToString(System.Globalization.CultureInfo.InvariantCulture))); + } + finally + { + conn.DecRef(); + } + }); + + using var client = new H2cClient(port, timeoutMs: 30_000); + client.WriteRaw("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"u8); + client.WriteFrame(0x4, flags: 0, streamId: 0, [0, 0x4, 0x7F, 0xFF, 0xFF, 0xFF]); // INITIAL_WINDOW_SIZE 2^31-1 + client.WriteWindowUpdate(streamId: 0, int.MaxValue - 65535); // so all of the download goes out at once + Assert.True(client.AwaitConnectionWindow(int.MaxValue), "the server did not open its connection window"); + + client.Request(streamId: 1, path: "/download"); + Thread.Sleep(200); // its response staged and its flush stuck, before the upload lands + + client.OpenPost(streamId: 3, "/upload"); + byte[] frame = new byte[16384]; + for (int sent = 0; sent < Upload; sent += frame.Length) + { + client.WriteFrame(0x0, flags: (byte)(sent + frame.Length == Upload ? 0x1 : 0x0), streamId: 3, frame); + } + + var bytes = new Dictionary(); + var ended = new HashSet(); + bool done = client.ReadData(bytes, ended, () => ended.Contains(1) && ended.Contains(3), credit: false, timeoutMs: 30_000); + Assert.True(done && bytes.GetValueOrDefault(1) == Download, + $"the download stopped at {bytes.GetValueOrDefault(1)} of {Download} bytes, and the upload's answer {(ended.Contains(3) ? "came" : "never came")}"); + Assert.Equal((long)Upload.ToString(System.Globalization.CultureInfo.InvariantCulture).Length, bytes.GetValueOrDefault(3)); + }); } } diff --git a/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs b/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs index 07526361..279e8641 100644 --- a/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs +++ b/tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs @@ -14,15 +14,21 @@ namespace Ioxide.Tests; /// /// The session runs with nghttp2's automatic WINDOW_UPDATE off, so the only credit a peer gets back /// is what the server consumes, and every DATA byte must be consumed exactly once. A byte that never -/// is shrinks the connection window for good. These tests make each way of leaking it stall an -/// upload: the window is 65535 bytes, so an upload past it completes only if credit keeps coming -/// back, and one the server should be holding stalls at the window instead of running ahead. +/// is shrinks its stream's window for good, and the connection's. The connection's is opened to +/// 2^31-1, so these tests make each way of leaking credit stall an upload at its stream's window: an +/// upload past it completes only if credit keeps coming back, and one the server should be holding +/// stalls at the window instead of running ahead. Nghttp2ReceiveWindowTests counts the connection's. /// internal static class Nghttp2StreamedRequestTests { - // RFC 9113's initial connection window, which nghttp2 leaves as it is: the most a peer may send - // ahead of what the server has consumed, on every stream together. - private const int ConnectionWindow = 65535; + // InitialWindowSize's default: the most a peer may send on one stream ahead of what the server has consumed. + private const int StreamWindow = 256 * 1024; + + // RFC 9113's initial window: a stream's until the peer acknowledges the server's SETTINGS, and the one + // a test sets where a stall at it, or nghttp2's credit once 32 KiB of it is read, is the signal. + private const int RfcWindow = 65535; + + private const byte WindowUpdateFrame = 0x8; private const int Large = 4 * 1024 * 1024; private const int Frame = 16384; @@ -55,11 +61,11 @@ public static void Register(Runner runner) Assert.True(read.Chunks > 1, $"the body reached the handler in {read.Chunks} chunk, not streamed"); }); - runner.Test($"nghttp2 streamed request ({flavor}): uploads a handler never reads give their connection credit back", + runner.Test($"nghttp2 streamed request ({flavor}): uploads a handler never reads are credited all the same, each past its stream's window", () => UnreadUploadsGiveCreditBack(sr)); } - runner.Test("nghttp2 streamed request: a handler that has not read holds its peer to one window, and the upload completes once it reads", () => + runner.Test("nghttp2 streamed request: a handler that has not read holds its own upload to one stream window, while another on the connection runs past it", () => { // "/hold" reads nothing until "/go" arrives on the same connection. HEADERS are not flow // controlled, so the release still gets through once the window is spent. @@ -68,21 +74,27 @@ public static void Register(Runner runner) client.Open(); long stalledAt = -1; + (H2cClient.UploadEnd End, uint Error, long Sent) other = default; (H2cClient.UploadEnd end, uint error, long sent) = client.Upload(streamId: 1, Large, path: "/hold", onStall: at => { stalledAt = at; - client.Request(streamId: 3, path: "/go"); + other = client.Upload(streamId: 3, Large, path: "/read", timeoutMs: 10_000); + client.Request(streamId: 5, path: "/go"); }); Assert.True(stalledAt >= 0, $"the client never stalled: the server took {sent} bytes while its handler read none"); - Assert.True(stalledAt == ConnectionWindow, - $"the server took {stalledAt} bytes before its handler read any, not one {ConnectionWindow}-byte window"); + Assert.True(stalledAt == StreamWindow, + $"the server took {stalledAt} bytes before its handler read any, not one {StreamWindow}-byte stream window"); + Assert.True(other.End == H2cClient.UploadEnd.Answered && other.Sent == Large, + $"while stream 1's handler read nothing, the upload on stream 3 ended {other.End} (error 0x{other.Error:x}) after {other.Sent} of {Large} bytes"); Assert.True(end == H2cClient.UploadEnd.Answered && sent == Large, $"once the handler read, the upload ended {end} (error 0x{error:x}) after {sent} of {Large} bytes"); Seen held = Single(seen, 1); Assert.True(held.Bytes == Large && held.Broken < 0, $"the handler read {Describe(seen)}, not {Large} bytes in order"); + Seen read = Single(seen, 3); + Assert.True(read.Bytes == Large && read.Broken < 0, $"the other handler read {Describe(seen)}, not {Large} bytes in order"); }); foreach ((uint code, string name) in new[] { (Cancel, "CANCEL"), (NoError, "NO_ERROR") }) @@ -91,16 +103,16 @@ public static void Register(Runner runner) runner.Test($"nghttp2 streamed request: a peer RST_STREAM ({name}) mid-body cuts the handler's read off, and the connection serves on", () => { var seen = new ConcurrentQueue(); - using var client = new H2cClient(Start(seen, streamedResponse: false), timeoutMs: 30_000); + using var client = new H2cClient(Start(seen, streamedResponse: false, initialWindowSize: RfcWindow), timeoutMs: 30_000); client.Open(); client.OpenPost(streamId: 1, "/read"); client.SendData(streamId: 1, offset: 0, Frame); client.SendData(streamId: 1, offset: Frame, Frame); - // nghttp2 returns credit once half its window is consumed, so this is the handler + // nghttp2 returns a stream's credit once half its window is consumed, so this is the handler // having read all of it, and parked on its next read when the reset lands. - Assert.True(client.AwaitConnectionWindow(ConnectionWindow), "the handler never read the first 32768 bytes"); + Assert.True(client.AwaitAnyOf([WindowUpdateFrame], streamId: 1) == WindowUpdateFrame, "the handler never read the first 32768 bytes"); client.ResetStream(streamId: 1, resetCode); const int After = 256 * 1024; @@ -133,18 +145,18 @@ public static void Register(Runner runner) Assert.True(seen.Count(s => s.Stream == 1) == 1, $"the trailers reached a handler as a request: {Describe(seen)}"); }); - runner.Test("nghttp2 streamed request: a body left unread when its stream is reset gives its connection credit back", () => + runner.Test("nghttp2 streamed request: a body left unread when its stream is reset is cut off for its handler, and the connection serves on", () => { - // A whole window queued behind "/hold" when the peer abandons the stream: if those bytes - // were not credited, no DATA could follow on any stream of the connection. + // A whole window queued behind "/hold" when the peer abandons the stream. The credit those bytes still + // owe the connection is counted in Nghttp2ReceiveWindowTests, at the top-up that shows it. var seen = new ConcurrentQueue(); using var client = new H2cClient(Start(seen, streamedResponse: false), timeoutMs: 30_000); client.Open(); client.OpenPost(streamId: 1, "/hold"); - for (int at = 0; at < ConnectionWindow; at += Frame) + for (int at = 0; at < RfcWindow; at += Frame) { - client.SendData(streamId: 1, offset: at, Math.Min(Frame, ConnectionWindow - at)); + client.SendData(streamId: 1, offset: at, Math.Min(Frame, RfcWindow - at)); } client.ResetStream(streamId: 1, Cancel); client.Request(streamId: 3, path: "/go"); @@ -152,15 +164,15 @@ public static void Register(Runner runner) const int After = 256 * 1024; (H2cClient.UploadEnd end, uint error, long sent) = client.Upload(streamId: 5, After, path: "/read"); Assert.True(end == H2cClient.UploadEnd.Answered && sent == After, - $"after a reset stream left {ConnectionWindow} bytes unread, the next upload ended {end} " - + $"(error 0x{error:x}) after {sent} of {After} bytes: their credit never came back"); + $"after a reset stream left {RfcWindow} bytes unread, the next upload ended {end} " + + $"(error 0x{error:x}) after {sent} of {After} bytes"); Seen held = Single(seen, 1); Assert.True(held.Bytes == 0 && held.Cut, $"the reset stream's handler should read none of a body its peer abandoned, and that it was cut: {Describe(seen)}"); }); - runner.Test("nghttp2 streamed request: a handler that fails mid-upload has its stream reset, and the connection keeps its credit", () => + runner.Test("nghttp2 streamed request: a handler that fails mid-upload has its stream reset, and the connection serves on", () => { // "/fail" sends its headers, then fails once "/go" releases it, with a window of its body // still unread - so its stream is reset by the server rather than by the peer. @@ -253,7 +265,7 @@ public static void Register(Runner runner) { try { - await new Nghttp2Connection(conn) + await new Nghttp2Connection(conn, new Nghttp2Options { InitialWindowSize = RfcWindow }) .RunAsync(async (_, body) => { long bytes = 0; @@ -286,9 +298,9 @@ public static void Register(Runner runner) client.SendData(streamId: 1, offset: 0, Frame); client.SendData(streamId: 1, offset: Frame, Frame); - // nghttp2 returns credit once half its window is consumed, so this is the handler + // nghttp2 returns a stream's credit once half its window is consumed, so this is the handler // having read all of it, and parked on its next read when the connection goes away. - Assert.True(client.AwaitConnectionWindow(ConnectionWindow), "the handler never read the first 32768 bytes"); + Assert.True(client.AwaitAnyOf([WindowUpdateFrame], streamId: 1) == WindowUpdateFrame, "the handler never read the first 32768 bytes"); } Assert.True(verdict.Task.Wait(10_000), "the handler parked on its body never woke when its connection went away"); @@ -297,10 +309,10 @@ public static void Register(Runner runner) runner.Test("nghttp2 streamed request: a padded upload reaches the handler without its padding, and the padding's credit comes back", () => { - // 255 bytes of padding per frame comes to more than a window over 4 MiB, so padding that - // was never credited would stall the upload on its own. + // 255 bytes of padding per frame comes to more than a 65,535-byte stream window over 4 MiB, so + // padding that was never credited would stall the upload on its own. var seen = new ConcurrentQueue(); - using var client = new H2cClient(Start(seen, streamedResponse: false), timeoutMs: 30_000); + using var client = new H2cClient(Start(seen, streamedResponse: false, initialWindowSize: RfcWindow), timeoutMs: 30_000); client.Open(); (H2cClient.UploadEnd end, uint error, long sent) = client.Upload(streamId: 1, Large, path: "/read", padding: 255); @@ -347,9 +359,8 @@ public static void Register(Runner runner) }); } - // Each upload fits the connection window and five cross it, so only credit that never comes - // back can wedge them. - private const int UploadBytes = 40 * 1024; + // Each upload crosses its stream's window, so only credit that never comes back can wedge one. + private const int UploadBytes = 100 * 1024; private const int Uploads = 5; /// @@ -358,11 +369,10 @@ public static void Register(Runner runner) /// private static void UnreadUploadsGiveCreditBack(bool streamedResponse) { - Assert.True(Uploads * (long)UploadBytes > ConnectionWindow && UploadBytes < ConnectionWindow, - "the uploads must cross the connection window while each fits it"); + Assert.True(UploadBytes > RfcWindow, "each upload must cross its stream's window"); var seen = new ConcurrentQueue(); - int port = Start(seen, streamedResponse); + int port = Start(seen, streamedResponse, initialWindowSize: RfcWindow); foreach (string path in new[] { "/read", "/unread", "/unread-after-a-wait" }) { @@ -389,11 +399,12 @@ private static void UnreadUploadsGiveCreditBack(bool streamedResponse) /// streamed response, "/fail" sends its headers and fails once released. /// private static int Start(ConcurrentQueue seen, bool streamedResponse, bool streamBodies = true, - int maxRequestBytes = 8 * 1024 * 1024) => TestServer.Start(async (reactor, conn) => + int maxRequestBytes = 8 * 1024 * 1024, int initialWindowSize = StreamWindow) => TestServer.Start(async (reactor, conn) => { try { - var connection = new Nghttp2Connection(conn, new Nghttp2Options { MaxRequestBytes = maxRequestBytes }); + var connection = new Nghttp2Connection(conn, + new Nghttp2Options { MaxRequestBytes = maxRequestBytes, InitialWindowSize = initialWindowSize }); var timer = new RingTimer(reactor); var gate = new TaskCompletionSource(); diff --git a/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs b/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs index 102bc82a..4b09f902 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/OffReactorH2FixTests.cs @@ -19,8 +19,9 @@ internal static class OffReactorH2FixTests // Past the initial windows on both stacks, so the body arrives only if credit flows mid-flight. private static readonly byte[] Upload = Pattern(1024 * 1024); - // The nghttp2 twins' 65,535: these test where calls run, and a 256 KiB window outruns the receive queue behind a pool hop. + // 65,535 on both stacks: these test where calls run, and a 256 KiB window outruns the receive queue behind a pool hop. private static readonly Http2Options RfcWindow = new() { InitialWindowSize = 65535 }; + private static readonly Nghttp2Options NgRfcWindow = new() { InitialWindowSize = 65535 }; public static void Register(Runner runner) { @@ -52,7 +53,7 @@ public static void Register(Runner runner) }))); runner.Test("h2 over a pipe that completes on the thread pool (nghttp2): an upload is echoed whole, read, answered and written on the reactor", () => - AssertPoolPipe((pipe, calls) => new Nghttp2Connection(pipe).RunAsync(request => + AssertPoolPipe((pipe, calls) => new Nghttp2Connection(pipe, NgRfcWindow).RunAsync(request => { calls.Note(); return new Nghttp2Response { Status = 200, Body = request.Body.ToArray() }; @@ -73,7 +74,7 @@ public static void Register(Runner runner) Echo(calls, body.ReadAsync, () => writer.WriteHeaders(new Http2Response { Status = 200 }), writer, writer.FlushAsync)))); runner.Test("h2 RunAsync begun on the thread pool (nghttp2): an upload read and echoed from the pool arrives whole, served on the reactor", () => - AssertLateStart((pipe, calls) => new Nghttp2Connection(pipe).RunAsync((_, body, writer) => + AssertLateStart((pipe, calls) => new Nghttp2Connection(pipe, NgRfcWindow).RunAsync((_, body, writer) => Echo(calls, body.ReadAsync, () => writer.WriteHeaders(new Nghttp2Response { Status = 200 }), writer, writer.FlushAsync)))); } diff --git a/tests/Ioxide.Tests.Unit/Http2StrictClient.cs b/tests/Ioxide.Tests.Unit/Http2StrictClient.cs index 50b331f6..6adfd639 100644 --- a/tests/Ioxide.Tests.Unit/Http2StrictClient.cs +++ b/tests/Ioxide.Tests.Unit/Http2StrictClient.cs @@ -122,6 +122,9 @@ public void Close(Task run) Assert.True(run.Wait(5_000), "connection wound down"); } + /// End the input, as a peer's FIN does, without waiting for the connection. + public void EndInput() => _input.Writer.Complete(); + public void Dispose() => Connection.Dispose(); private static byte[] FrameHeaderBytes(int length, byte type, byte flags, int streamId) => diff --git a/tests/Ioxide.Tests.Unit/Nghttp2ReceiveWindowTests.cs b/tests/Ioxide.Tests.Unit/Nghttp2ReceiveWindowTests.cs new file mode 100644 index 00000000..4b284eed --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Nghttp2ReceiveWindowTests.cs @@ -0,0 +1,366 @@ +using System.Buffers.Binary; +using ioxide.nghttp2; +using static Ioxide.Tests.Http2FlowControlTests; + +namespace Ioxide.Tests; + +/// +/// The nghttp2 server's receive windows (RFC 9113 6.9): the connection's opened behind the SETTINGS, +/// each stream's held to its own, the credit nghttp2 returns as bodies are consumed, and the frame +/// loop that reads on behind a stalled flush. +/// +internal static class Nghttp2ReceiveWindowTests +{ + private const int MaxWindow = int.MaxValue; + private const byte PingFrame = 0x6, GoAwayFrame = 0x7; + private const uint FlowControlError = 0x3; + + private const string Whole = "ended whole", CutOff = "was cut off"; + + public static void Register(Runner runner) + { + // ---------------------------------------------------------------- what the server advertises + + runner.Test("nghttp2 flow: the server's preface is its SETTINGS, then a WINDOW_UPDATE that opens the connection's window to 2^31-1, in the same write", () => + { + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + Task run = connection.RunAsync(_ => new Nghttp2Response { Status = 200 }); + + List preface = Frame.Walk(client.ReleaseFlush()); + Assert.True(preface.Count == 2 + && preface[0] is { Type: Frame.Settings, Flags: 0, StreamId: 0 } + && preface[1] is { Type: Frame.WindowUpdate, StreamId: 0 }, + $"the preface was [{string.Join(", ", preface.Select(f => $"type {f.Type} on {f.StreamId}"))}], not SETTINGS and then a WINDOW_UPDATE on stream 0"); + Assert.Equal(MaxWindow - ConnectionWindow, Increment(preface[1])); + + client.Close(run); + connection.Dispose(); + }); + + runner.Test("nghttp2: by default the server advertises 100 concurrent streams, each with a 256 KiB window, and Nghttp2Options sets both", () => + { + foreach ((Nghttp2Options? options, uint streams, uint window) in new (Nghttp2Options?, uint, uint)[] + { + (null, 100, StreamWindow), + (new Nghttp2Options { MaxConcurrentStreams = 7, InitialWindowSize = 100_000 }, 7, 100_000), + }) + { + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client, options); + Task run = connection.RunAsync(_ => new Nghttp2Response { Status = 200 }); + + Frame settings = Frame.Walk(client.ReleaseFlush())[0]; + Assert.True(Setting(settings, 0x3) == streams && Setting(settings, InitialWindowSize) == window, + $"the SETTINGS advertised {Setting(settings, 0x3)} streams of {Setting(settings, InitialWindowSize)} bytes, not {streams} of {window}"); + + client.Close(run); + connection.Dispose(); + } + }); + + // ---------------------------------------------------------------- what holds an upload back + + runner.Test("nghttp2 flow: concurrent uploads are each held to their own stream window, and a byte past one ends the connection with FLOW_CONTROL_ERROR", () => + { + const int Size = 2 * StreamWindow; + int[] streams = [1, 3, 5]; + var gates = streams.ToDictionary(id => id, _ => new TaskCompletionSource()); // one each: a second awaiter resumes on the pool + var read = streams.ToDictionary(id => id, _ => new Tally()); + var ends = new Dictionary(); + + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + Task run = connection.RunAsync(async (request, body) => + { + int id = request.StreamId; + await gates[id].Task; // none reads until let go + ends[id] = await EndOf(body, read[id]); + return new Nghttp2Response { Status = 200 }; + }); + + var peer = new PeerWindow(); + var frames = new List(); + Take(client, peer, frames); // the SETTINGS, and the connection's window behind them + client.Feed([.. Open(), .. SettingsAck(), .. Post(1), .. Post(3), .. Post(5)]); + + Dictionary sent = streams.ToDictionary(id => id, id => Send(client, peer, frames, id, from: 0, Size)); + Assert.True(sent.Values.All(n => n == StreamWindow), + $"unread, the uploads had [{string.Join(", ", sent.Values)}] bytes in flight, not a whole {StreamWindow}-byte window each"); + Assert.True(!frames.Any(f => f.Type is RstStreamFrame or GoAwayFrame), "an upload inside its window was refused"); + + foreach (int id in new[] { 1, 5 }) + { + gates[id].SetResult(); + sent[id] = Send(client, peer, frames, id, from: sent[id], Size); + Assert.True(sent[id] == Size && read[id].Bytes == Size && ends.GetValueOrDefault(id) == Whole, + $"once read, the upload on stream {id} stopped at {sent[id]} of {Size} bytes, {read[id].Bytes} of them read"); + } + + // nghttp2 makes it a connection error, which RFC 9113 6.9 allows as well as a stream one. + client.Feed(Data(3, 1, endStream: false)); // past what stream 3 was granted + Take(client, peer, frames); + Assert.True(frames.Any(f => f.Type == GoAwayFrame && GoAwayError(f) == FlowControlError), + "a byte past stream 3's window was taken without a FLOW_CONTROL_ERROR"); + Assert.True(run.Wait(5_000), "the connection did not end after its GOAWAY"); + + gates[3].SetResult(); + Assert.Equal(CutOff, ends[3]); + + client.Close(run); + connection.Dispose(); + }); + + runner.Test("nghttp2 flow: every byte comes back once, on nghttp2's top-up half way down the connection's window, whether it was read, dropped, reset or sent after its answer", () => + { + // nghttp2 credits a window once half of it is consumed: 1,073,741,823 bytes of a 2^31-1 one. + const int TopUp = MaxWindow / 2; + var held = new Dictionary { [3] = new(), [5] = new() }; + var read = new Tally(); + + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client, new Nghttp2Options { InitialWindowSize = MaxWindow }); // only the connection's window binds + Task run = connection.RunAsync(async (request, body) => + { + switch (request.StreamId) + { + case 1: + await ReadAll(body, read); // read as it comes + break; + case 3: + await held[3].Task; // answers with its body unread + break; + case 5: + await held[5].Task; + await EndOf(body, new Tally()); // its body dropped by the peer's reset + break; + } + return new Nghttp2Response { Status = 200 }; // 7 answers at once, before its body + }); + + var frames = new List(Frame.Walk(client.ReleaseFlush())); + long sent = 0, toStream1 = 0; + void Send(byte[] frame, int streamId) + { + int payload = (frame[0] << 16) | (frame[1] << 8) | frame[2]; + sent += payload; + toStream1 += streamId == 1 ? payload : 0; + client.Feed(frame); + frames.AddRange(client.Drain()); + } + + client.Feed([.. Open(), .. SettingsAck(), .. Post(1), .. Post(3), .. Post(5), .. Post(7)]); + frames.AddRange(client.Drain()); + + Send(Data(1, 10_000, endStream: false, padding: 200), 1); // read, and its padding taken by nghttp2 + Send(Data(3, 10_000, endStream: false), 3); // queued, then dropped unread + Send(Data(5, 10_000, endStream: false), 5); // queued, then dropped by the peer's reset + Send(Data(7, 10_000, endStream: false, padding: 50), 7); // after its answer + client.Feed(RstStream(5, Cancel)); + held[3].SetResult(); + held[5].SetResult(); + frames.AddRange(client.Drain()); + Send(Data(3, 10_000, endStream: false, padding: 10), 3); // after its answer too + + // Stream 1 up to the top-up, in full frames 64 to a write, and one more frame that reaches it. + const int Frame16K = 16384, PerWrite = 64; + long rest = TopUp - sent; + long full = (rest - 1) / Frame16K; + byte[] batch = [.. Enumerable.Range(0, PerWrite).SelectMany(_ => Data(1, Frame16K, endStream: false))]; + for (long done = 0; done < full; done += PerWrite) + { + int count = (int)Math.Min(PerWrite, full - done); + client.Feed(count == PerWrite ? batch : batch[..(count * (9 + Frame16K))]); + sent += (long)count * Frame16K; + toStream1 += (long)count * Frame16K; + List drained = client.Drain(); + Assert.True(!drained.Any(f => f.Type == GoAwayFrame), $"the connection ended with a GOAWAY {sent} bytes in"); + frames.AddRange(drained); + } + Assert.True(frames.Count(f => f is { Type: Frame.WindowUpdate, StreamId: 0 }) == 1, + $"the connection was credited before half its window was consumed, {sent} bytes in"); + + Send(Data(1, (int)(TopUp - sent), endStream: false), 1); // the frame whose last byte reaches it + + List topUps = frames.Where(f => f is { Type: Frame.WindowUpdate, StreamId: 0 }).Skip(1).Select(Increment).ToList(); + Assert.True(topUps.Count == 1 && topUps[0] == sent, + $"{sent} bytes were charged to the connection, and its top-ups came to [{string.Join(", ", topUps)}]"); + Assert.True(read.Bytes == toStream1 - 201, $"stream 1 was sent {toStream1} bytes and read {read.Bytes} of them"); + + client.Feed(Data(1, 0, endStream: true)); + client.Drain(); + client.Close(run); + connection.Dispose(); + }); + + // ---------------------------------------------------------------- the frame loop behind a stalled flush + + runner.Test("nghttp2 flow: the frame loop goes on reading while a flush is stalled", () => + { + var read = new Tally(); + + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + Task run = connection.RunAsync(async (request, body) => + { + if (request.StreamId == 3) + { + await ReadAll(body, read); + } + return new Nghttp2Response { Status = 200 }; + }); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Open(), .. Get(1)]); // stream 1's answer leaves on this pass's flush, which the peer does not take + Assert.Equal(1, client.PendingFlushes); + + client.Feed([.. Post(3), .. Data(3, 16384, endStream: false), .. Data(3, 16384, endStream: false), .. Ping()]); + Assert.True(read.Bytes == 2 * 16384, + $"behind the stalled flush the handler read {read.Bytes} of the {2 * 16384} bytes sent after it"); + Assert.Equal(1, client.PendingFlushes); // what the frames produced waits behind it + + List frames = client.Drain(); + Assert.True(frames.Any(f => f is { Type: PingFrame, Flags: 1 }), "the PING read behind the stalled flush was never answered"); + + client.Feed(Data(3, 0, endStream: true)); + client.Drain(); + client.Close(run); + connection.Dispose(); + }); + + runner.Test("nghttp2 flow: a stream nghttp2 resets behind a stalled flush ends its handler's read once that flush's drain sends the reset", () => + { + string end = "still reading"; + + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + Task run = connection.RunAsync(async (request, body) => + { + if (request.StreamId == 3) + { + end = await EndOf(body, new Tally()); + } + return new Nghttp2Response { Status = 200 }; + }); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Open(), .. Get(1)]); // this pass's flush the peer does not take yet + Assert.Equal(1, client.PendingFlushes); + + // A body past its content-length: nghttp2 resets the stream itself, and closes it as the reset goes out. + client.Feed([.. PostWithLength(3, 5), .. Data(3, 20, endStream: false)]); + Assert.Equal("still reading", end); + + List frames = client.Drain(); // the peer sends nothing more, so no later pass can end the read + Assert.True(frames.Any(f => f is { Type: RstStreamFrame, StreamId: 3 }), "nghttp2 did not reset a body longer than its content-length"); + Assert.Equal(CutOff, end); + + client.Close(run); + connection.Dispose(); + }); + + runner.Test("nghttp2 flow: a request read behind a stalled flush as the input ends is answered before the connection is torn down", () => + { + using var client = new StrictClient(); + var connection = new Nghttp2Connection(client); + Task run = connection.RunAsync(_ => Nghttp2Response.Text("ok")); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Open(), .. Get(1)]); // this pass's flush the peer does not take yet + Assert.Equal(1, client.PendingFlushes); + + client.Feed(Get(3)); // read behind it, answered behind it + client.EndInput(); + Assert.True(!run.IsCompleted, "the connection was torn down with an answer still queued behind its flush"); + + List frames = client.Drain(); + Assert.True(run.Wait(5_000), "the connection did not wind down once its flushes were out"); + Assert.True(frames.Any(f => f is { Type: Frame.Data, StreamId: 3, EndStream: true }), + $"stream 3's answer never went out: [{string.Join(", ", frames.Select(f => $"type {f.Type} on {f.StreamId}"))}]"); + + connection.Dispose(); + }); + } + + // A stream's body from `from` on, within what the server granted, taking in its flushes before each frame. Returns how far it got. + private static long Send(StrictClient client, PeerWindow peer, List frames, int streamId, long from, long size) + { + long sent = from; + while (true) + { + Take(client, peer, frames); + + int room = (int)Math.Min(Math.Min(16384, peer.Room(streamId)), size - sent); + if (room <= 0) + { + return sent; + } + + client.Feed(Data(streamId, room, endStream: sent + room == size)); + peer.Spend(streamId, room); + sent += room; + } + } + + // Every flush the server has in flight, taken in by the peer and kept for the test to look at. + private static void Take(StrictClient client, PeerWindow peer, List frames) + { + List flushed = client.Drain(); + peer.Absorb(flushed); + frames.AddRange(flushed); + } + + private static async ValueTask ReadAll(Nghttp2BodyReader body, Tally read) + { + while (true) + { + ReadOnlyMemory chunk = await body.ReadAsync(); + if (chunk.IsEmpty) + { + return; + } + read.Bytes += chunk.Length; + } + } + + // How a body read ends: whole at END_STREAM, or with the IOException of a reset stream. + private static async ValueTask EndOf(Nghttp2BodyReader body, Tally read) + { + try + { + await ReadAll(body, read); + return Whole; + } + catch (IOException) + { + return CutOff; + } + } + + // :method, :scheme http, :path /, and an :authority, which nghttp2 requires. + private static byte[] Post(int streamId) => FrameBytes(0x1, 0x4, streamId, [0x83, 0x86, 0x84, 0x01, 9, .. "a.example"u8.ToArray()]); + + private static byte[] Get(int streamId) => FrameBytes(0x1, 0x5, streamId, [0x82, 0x86, 0x84, 0x01, 9, .. "a.example"u8.ToArray()]); + + // A POST with a one-digit content-length: static entry 28, its value a literal. + private static byte[] PostWithLength(int streamId, int length) + => FrameBytes(0x1, 0x4, streamId, [0x83, 0x86, 0x84, 0x01, 9, .. "a.example"u8.ToArray(), 0x0F, 0x0D, 0x01, (byte)('0' + length)]); + + private static byte[] SettingsAck() => FrameBytes(0x4, 0x1, 0, []); + + private static int Increment(Frame windowUpdate) => (int)(BinaryPrimitives.ReadUInt32BigEndian(windowUpdate.Payload) & 0x7FFFFFFF); + + private static uint GoAwayError(Frame goAway) => BinaryPrimitives.ReadUInt32BigEndian(goAway.Payload.AsSpan(4)); + + private static uint Setting(Frame settings, ushort id) + { + for (int at = 0; at + 6 <= settings.Payload.Length; at += 6) + { + if (BinaryPrimitives.ReadUInt16BigEndian(settings.Payload.AsSpan(at)) == id) + { + return BinaryPrimitives.ReadUInt32BigEndian(settings.Payload.AsSpan(at + 2)); + } + } + throw new Exception($"the SETTINGS carried no setting 0x{id:x}"); + } +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..060cb646 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -40,6 +40,7 @@ private static int Main() Http2StreamedBodyTests.Register(runner); Http2FlowControlTests.Register(runner); Http2ReceiveWindowTests.Register(runner); + Nghttp2ReceiveWindowTests.Register(runner); Http2BufferedCreditTests.Register(runner); Http2EarlyAnswerTests.Register(runner); Http2EarlyAnswerGuardTests.Register(runner);