Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/protocols/ioxide.http3/Http3Connection.Streamed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ private async Task ServeAsync(Func<Http3Request, Http3ResponseWriter, ValueTask>
await new ReactorSwitch(Reactor); // back on the reactor, wherever the handler finished
RetireBody(rs);
await writer.CompleteAsync();
StopReadingIfAnsweredEarly(rs);
}
catch (Exception exception)
{
await new ReactorSwitch(Reactor);
Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {exception.GetBaseException().Message}");
RetireBody(rs);
await writer.FailAsync();
StopReadingIfAnsweredEarly(rs);
}
finally
{
Expand Down
29 changes: 28 additions & 1 deletion src/protocols/ioxide.http3/Http3Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public sealed partial class Http3Connection
private readonly Http3Options _options;
private bool _fatal;

// RFC 9114 H3_NO_ERROR: what a STOP_SENDING carries when the response did not need the rest of the request.
private const ulong H3NoError = 0x0100;

// RFC 9114 section 8.1. The code a protocol error closes the connection with.
private const ulong H3GeneralProtocolError = 0x0101;
private const ulong H3ClosedCriticalStream = 0x0104;
Expand Down Expand Up @@ -90,7 +93,7 @@ private sealed class ReqStream
public int HeadersLen;

public bool HeadersDone; // first HEADERS decoded (later ones = trailers)
public bool Finished; // fin seen and processed
public bool Finished; // fin seen and processed, or the stream reset by this side

// The stream was paced, so the engine credited none of what arrived since: the parser
// credits whatever it does not hand to the reader. That includes what it drops after
Expand Down Expand Up @@ -714,6 +717,7 @@ private void DispatchReadyAsync(Func<Http3Request, ValueTask<Http3Response>> han
Console.Error.WriteLine($"[ioxide.http3] request handler faulted: {e.GetBaseException().Message}");
RetireBody(rs);
Submit(sid, new Http3Response { Status = 500 });
StopReadingIfAnsweredEarly(rs);
Recycle(rs);
continue;
}
Expand All @@ -723,6 +727,7 @@ private void DispatchReadyAsync(Func<Http3Request, ValueTask<Http3Response>> han
RetireBody(rs);
Http3Response response = pending.Result; // once: a ValueTask is consumed by reading it
Submit(sid, response);
StopReadingIfAnsweredEarly(rs);
Recycle(rs);
}
else
Expand Down Expand Up @@ -753,6 +758,7 @@ private async Task CompleteBufferedAsync(ValueTask<Http3Response> pending, ReqSt
return;
}
Submit(rs.Request.StreamId, resp);
StopReadingIfAnsweredEarly(rs);
Recycle(rs);
}

Expand Down Expand Up @@ -786,6 +792,27 @@ private void RetireBody(ReqStream rs)
}
}

// Once the response is handed over: STOP_SENDING with H3_NO_ERROR stops an upload still arriving and leaves the client the response (RFC 9114 4.1).
private void StopReadingIfAnsweredEarly(ReqStream rs)
{
// Its fin or this side's reset ended the read side, and the peer's reset or the stream's close took it out of _requests.
if (rs.Finished || IsBroken || !_requests.ContainsKey(rs.Request.StreamId))
{
return;
}
_quicConnection.StopReading(rs.Request.StreamId, H3NoError);
}

// A streamed response's reset, which stops its request's read side as well.
internal void ResetResponse(long streamId, ulong code)
{
if (_requests.TryGetValue(streamId, out ReqStream? rs))
{
rs.Finished = true;
}
_quicConnection.ResetStream(streamId, code);
}

// --- egress --------------------------------------------------------------------------------

// Encode and send one response: HEADERS frame (+ DATA frame header) in one SendStream call,
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/ioxide.http3/Http3ResponseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ internal ValueTask FailAsync()
_staged = 0;
if (!_gone)
{
_quic.ResetStream(_streamId, H3InternalError); // never on a stream the peer already stopped
_connection.ResetResponse(_streamId, H3InternalError); // never on a stream the peer already stopped
}

if (_staging.Length > 0)
Expand Down
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.E2E/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private static int Main()
Nghttp2ManyHeadersTests.Register(runner);
StreamedPeerGoneTests.Register(runner);
EarlyAnswerTests.Register(runner);
Http3EarlyAnswerTests.Register(runner);
QuicStreamingCreditTests.Register(runner);
Http3BodyTests.Register(runner);
Http3RequestReuseTests.Register(runner);
Expand Down
147 changes: 123 additions & 24 deletions tests/Ioxide.Tests.E2E/Protocols/EarlyAnswerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using ioxide;
using ioxide.http2;
using ioxide.http3;
using ioxide.nghttp3;
using ioxide.ngtcp2;

Expand Down Expand Up @@ -114,6 +115,39 @@ private static void RegisterHttpClient(Runner runner)
using HttpClient client = H3Client();
AssertReadWhole(client, $"https://127.0.0.1:{port}/");
}, skip: !h3);

runner.Test("h3 early answer (ioxide.http3): a streamed 413 to an upload arrives whole, and the client stops uploading", () =>
{
int port = StartHttp3(connection => connection.RunAsync(async (_, _, writer) =>
{
writer.WriteHeaders(new Http3Response { Status = 413 });
writer.Write(h3Answer);
await writer.CompleteAsync();
}));
using HttpClient client = H3Client();
AssertAnsweredAndStopped(client, $"https://127.0.0.1:{port}/", h3Answer, H3Stopped);
}, skip: !h3);

runner.Test("h3 early answer (ioxide.http3): a buffered 413 to an upload arrives whole, and the client stops uploading", () =>
{
int port = StartHttp3(connection => connection.RunAsync((_, _) =>
new ValueTask<Http3Response>(new Http3Response { Status = 413, Body = bufferedAnswer })));
using HttpClient client = H3Client();
AssertAnsweredAndStopped(client, $"https://127.0.0.1:{port}/", bufferedAnswer, H3Stopped);
}, skip: !h3);

runner.Test("control: h3 (ioxide.http3) an upload its handler reads to the end is sent whole and answered", () =>
{
int port = StartHttp3(connection => connection.RunAsync(async (_, body, writer) =>
{
long read = await ReadAllAsync(body.ReadAsync);
writer.WriteHeaders(new Http3Response { Status = 200 });
writer.Write(System.Text.Encoding.ASCII.GetBytes($"read {read}"));
await writer.CompleteAsync();
}));
using HttpClient client = H3Client();
AssertReadWhole(client, $"https://127.0.0.1:{port}/");
}, skip: !h3);
}

// The shim's own client, so these run where msquic is missing.
Expand All @@ -134,19 +168,41 @@ private static void RegisterRawClient(Runner runner)
await writer.CompleteAsync();
})
: connection.RunAsync((_, _) => new ValueTask<Nghttp3Response>(new Nghttp3Response { Status = 413, Body = answer })));
AssertRawAnsweredAndStopped(port, answer);
});

using RawH3Client client = ConnectRaw(port);
client.OpenRequestStream();
Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload)], fin: false),
"the request's headers were refused");

long sent = UploadUntilStopped(client);
Assert.True(sent < H3Stopped, $"the client uploaded {sent} bytes to a handler that read none of them: nothing asked it to stop");
Assert.True(client.WaitForFin(timeoutMs: 5000), "the answer never ended");
AssertBody(answer, DataPayload(client.Received));
runner.Test($"h3 early answer (ioxide.http3, raw client): a {(stream ? "streamed" : "buffered")} 413 to an upload still arriving arrives whole, and the upload stops", () =>
{
int port = StartHttp3(connection => stream
? connection.RunAsync(async (_, _, writer) =>
{
writer.WriteHeaders(new Http3Response { Status = 413 });
writer.Write(answer);
await writer.CompleteAsync();
})
: connection.RunAsync((_, _) => new ValueTask<Http3Response>(new Http3Response { Status = 413, Body = answer })));
AssertRawAnsweredAndStopped(port, answer);
});
}

runner.Test("h3 early answer (ioxide.http3, raw client): a buffered 413 its handler finished off the reactor arrives whole, and the upload stops", () =>
{
// Released from the test's thread, so the stack submits the answer from a reactor post, outside the pass that read the request.
var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var answered = new TaskCompletionSource();
int port = StartHttp3(connection => connection.RunAsync(async (_, _) =>
{
parked.TrySetResult();
await answered.Task;
return new Http3Response { Status = 413, Body = answer };
}));
AssertRawAnsweredAndStopped(port, answer, beforeUpload: client =>
{
Assert.True(client.WaitFor(parked.Task, timeoutMs: 5000), "the handler never ran");
answered.SetResult();
});
});

runner.Test("h3 early answer (nghttp3, raw client): a client that refused the answer is still asked to stop uploading once its handler returns", () =>
{
// More answer than the client's window holds, so the transport is still sending it when the refusal lands.
Expand All @@ -164,20 +220,26 @@ private static void RegisterRawClient(Runner runner)
await refused.Task;
returned.TrySetResult((await writer.FlushAsync()).IsCompleted);
}));
AssertRefusedStillStopped(port, refused, returned.Task);
});

using RawH3Client client = ConnectRaw(port);
client.OpenRequestStream();
Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload), .. new byte[16 * 1024]], fin: false),
"the request's headers were refused");
client.StopSending(H3RequestCancelled);
Assert.True(client.WaitForReset(timeoutMs: 5000) == H3RequestCancelled, "the server never answered the refusal with RESET_STREAM");

refused.SetResult();
Assert.True(client.WaitFor(returned.Task, timeoutMs: 10_000), "the handler never returned");
Assert.True(returned.Task.Result, "the handler was never told the client refused its answer");

long sent = UploadUntilStopped(client);
Assert.True(sent < H3Stopped, $"the client uploaded {sent} more bytes after the handler returned: nothing asked it to stop");
runner.Test("h3 early answer (ioxide.http3, raw client): a client that refused the answer is still asked to stop uploading once its handler returns", () =>
{
var refused = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var returned = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
int port = StartHttp3(connection => connection.RunAsync(async (_, _, writer) =>
{
writer.WriteHeaders(new Http3Response { Status = 413 });
for (int i = 0; i < 128; i++)
{
answer.CopyTo(writer.GetSpan(answer.Length));
writer.Advance(answer.Length);
await writer.FlushAsync();
}
await refused.Task;
returned.TrySetResult((await writer.FlushAsync()).IsCompleted);
}));
AssertRefusedStillStopped(port, refused, returned.Task);
});

runner.Test("h3 early answer (nghttp3): a transport that ends the connection inside StopReading leaves nghttp3 nothing to call", () =>
Expand All @@ -202,6 +264,39 @@ private static void RegisterRawClient(Runner runner)
});
}

// The raw client uploads to a handler that answers without reading: the answer arrives whole, and the upload stops.
private static void AssertRawAnsweredAndStopped(int port, byte[] answer, Action<RawH3Client>? beforeUpload = null)
{
using RawH3Client client = ConnectRaw(port);
client.OpenRequestStream();
Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload)], fin: false),
"the request's headers were refused");
beforeUpload?.Invoke(client);

long sent = UploadUntilStopped(client);
Assert.True(sent < H3Stopped, $"the client uploaded {sent} bytes to a handler that read none of them: nothing asked it to stop");
Assert.True(client.WaitForFin(timeoutMs: 5000), "the answer never ended");
AssertBody(answer, DataPayload(client.Received));
}

// The client refuses the answer with STOP_SENDING, then the handler returns: its upload is still stopped.
private static void AssertRefusedStillStopped(int port, TaskCompletionSource refused, Task<bool> returned)
{
using RawH3Client client = ConnectRaw(port);
client.OpenRequestStream();
Assert.True(client.TryWrite([.. RawH3Client.RequestHeaders("POST"), .. RawH3Client.DataFrameHeader(Upload), .. new byte[16 * 1024]], fin: false),
"the request's headers were refused");
client.StopSending(H3RequestCancelled);
Assert.True(client.WaitForReset(timeoutMs: 5000) == H3RequestCancelled, "the server never answered the refusal with RESET_STREAM");

refused.SetResult();
Assert.True(client.WaitFor(returned, timeoutMs: 10_000), "the handler never returned");
Assert.True(returned.Result, "the handler was never told the client refused its answer");

long sent = UploadUntilStopped(client);
Assert.True(sent < H3Stopped, $"the client uploaded {sent} more bytes after the handler returned: nothing asked it to stop");
}

private static void AssertAnsweredAndStopped(HttpClient client, string url, byte[] answer, long stopped, bool duplex = false)
{
ICountedUpload upload = duplex ? new DuplexUpload(Upload) : new CountedUpload(Upload);
Expand Down Expand Up @@ -327,12 +422,16 @@ private static int StartH2(Func<Http2Connection, Task> serve)
}
});

private static int StartH3(Func<Nghttp3Connection, Task> serve)
private static int StartH3(Func<Nghttp3Connection, Task> serve) => StartQuic(conn => serve(new Nghttp3Connection(conn)));

private static int StartHttp3(Func<Http3Connection, Task> serve) => StartQuic(conn => serve(new Http3Connection(conn)));

private static int StartQuic(Func<QuicConnection, Task> serve)
{
(string certPath, string keyPath) = TestCert.Ensure();
var engine = TestServer.Own(new QuicEngine(certPath, keyPath, cidLength: 8, alpn: ["h3"]));
(_, int udpPort) = TestServer.StartDatagram(onDatagram: null, quicFactory: engine.CreateFactory(),
quicHandle: (_, conn) => serve(new Nghttp3Connection(conn)));
quicHandle: (_, conn) => serve(conn));
return udpPort;
}

Expand Down
Loading
Loading