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
16 changes: 16 additions & 0 deletions src/protocols/ioxide.nghttp2/native/ioxide_nghttp2_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ static int on_stream_close(nghttp2_session *session, int32_t stream_id, uint32_t
return 0;
}

/* Asks a client to stop uploading once its answer is out (RFC 9113 8.1); nghttp2 would send a reset queued sooner ahead of the body. */
static int on_frame_send(nghttp2_session *session, const nghttp2_frame *frame, void *user_data)
{
(void)user_data;
if ((frame->hd.type == NGHTTP2_HEADERS || frame->hd.type == NGHTTP2_DATA) &&
(frame->hd.flags & NGHTTP2_FLAG_END_STREAM) != 0 &&
nghttp2_session_get_stream_remote_close(session, frame->hd.stream_id) == 0) {
return nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, frame->hd.stream_id, NGHTTP2_NO_ERROR);
}
return 0;
}

/* nghttp2 drops a response whose header block is past its limit (64 KiB) with no frame on the wire, which would leave
* the stream open for good. Only that error: a stream already gone is not to be reset again (RFC 9113 5.4.2). */
static int on_frame_not_send(nghttp2_session *session, const nghttp2_frame *frame, int lib_error_code, void *user_data)
Expand Down Expand Up @@ -273,6 +285,10 @@ static ih2_conn *ih2_new(ih2_callbacks cbs, void *user, int server, int manual_f
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, on_data_chunk_recv);
nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, on_stream_close);
nghttp2_session_callbacks_set_on_frame_not_send_callback(callbacks, on_frame_not_send);
if (server && manual_flow) {
/* Only a body-reader handler answers before its request ends, and only manual flow reports that reset's close. */
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, on_frame_send);
}

int rv;
if (manual_flow) {
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 6 additions & 2 deletions tests/Ioxide.Tests.Chaos/H2cClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public enum UploadEnd
/// <summary>RST_STREAM on the stream. The error code says why.</summary>
Reset,

/// <summary>HEADERS, then RST_STREAM(NO_ERROR): the request was answered, and the client asked to send no more of it (RFC 9113 8.1).</summary>
Stopped,

/// <summary>GOAWAY: the whole connection ended, not just the stream.</summary>
GoAway,

Expand All @@ -53,7 +56,7 @@ public enum UploadEnd
/// POST <paramref name="length"/> body bytes of <see cref="PatternAt"/> within the server's
/// flow-control windows, reading what arrives before sending more, until the server has answered
/// and the whole body is sent, or it resets the stream, or goes away. An answer that comes early
/// does not stop the body: a real client still sends it, and the server still has to credit it.
/// does not stop the body, but a RST_STREAM(NO_ERROR) after it does.
/// </summary>
/// <param name="padding">
/// Pad every DATA frame with this many bytes. Padding counts against both windows and never
Expand Down Expand Up @@ -142,7 +145,8 @@ public enum UploadEnd
}
else if (type == RstStream && sid == streamId && payload.Length == 4)
{
return (UploadEnd.Reset, BinaryPrimitives.ReadUInt32BigEndian(payload), sent);
uint code = BinaryPrimitives.ReadUInt32BigEndian(payload);
return (answered && code == 0 ? UploadEnd.Stopped : UploadEnd.Reset, code, sent);
}
else if (type == Headers && sid == streamId)
{
Expand Down
4 changes: 3 additions & 1 deletion tests/Ioxide.Tests.Chaos/Nghttp2StreamedRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,10 @@ private static void UnreadUploadsGiveCreditBack(bool streamedResponse)

for (int i = 1; i <= Uploads; i++)
{
// An unread upload that has not ended by its answer is stopped after it (RFC 9113 8.1).
(H2cClient.UploadEnd end, uint error, long sent) = client.Upload(streamId: (2 * i) - 1, UploadBytes, path: path);
Assert.True(end == H2cClient.UploadEnd.Answered && sent == UploadBytes,
bool stopped = end == H2cClient.UploadEnd.Stopped && path != "/read";
Assert.True(stopped || (end == H2cClient.UploadEnd.Answered && sent == UploadBytes),
$"{path}: upload {i} of {Uploads}, {(i - 1) * UploadBytes / 1024} KiB already sent, ended {end} "
+ $"(error 0x{error:x}) after {sent} of {UploadBytes} bytes");
}
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 @@ -74,6 +74,7 @@ private static int Main()
Http2StreamedFaultTests.Register(runner);
Http2BufferedFaultTests.Register(runner);
Nghttp2ManyHeadersTests.Register(runner);
Nghttp2EarlyAnswerTests.Register(runner);
StreamedPeerGoneTests.Register(runner);
EarlyAnswerTests.Register(runner);
Http3EarlyAnswerTests.Register(runner);
Expand Down
215 changes: 215 additions & 0 deletions tests/Ioxide.Tests.E2E/Protocols/Nghttp2EarlyAnswerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
using System.Buffers;
using System.Net;
using ioxide;
using ioxide.nghttp2;

namespace Ioxide.Tests;

/// <summary>An ioxide.nghttp2 answer that ends before its request does, with .NET's HttpClient: the answer arrives whole, and the upload stops (RFC 9113 8.1).</summary>
internal static class Nghttp2EarlyAnswerTests
{
// Far more than any window lets through before the client hears it should stop.
private const long Upload = 64L << 20;

// A control's upload, read whole by its handler.
private const long ReadUpload = 8L << 20;

// Measured at 64 KiB, the connection window; a reset stream takes no more credit, so its window bounds what follows.
private const long Stopped = 4L << 20;

public static void Register(Runner runner)
{
// Inside the client's 64 KiB stream window: this client credits an answer only once its upload ends, and the reset follows the END_STREAM.
byte[] answer = Pattern(32 * 1024);

runner.Test("h2 early answer (nghttp2): a streamed 413 to an upload arrives whole, and the client stops uploading", () =>
{
int port = Start(connection => connection.RunAsync((_, _, writer) =>
{
writer.WriteHeaders(new Nghttp2Response { Status = 413 });
writer.Write(answer);
return ValueTask.CompletedTask;
}));
AssertAnsweredAndStopped(port, answer);
});

runner.Test("h2 early answer (nghttp2): a buffered 413 to an upload arrives whole, and the client stops uploading", () =>
{
int port = Start(connection => connection.RunAsync((_, _) =>
new ValueTask<Nghttp2Response>(new Nghttp2Response { Status = 413, Body = answer })));
AssertAnsweredAndStopped(port, answer);
});

runner.Test("h2 early answer (nghttp2): a bodyless 413 to an upload arrives, and the client stops uploading", () =>
{
int port = Start(connection => connection.RunAsync((_, _) =>
new ValueTask<Nghttp2Response>(new Nghttp2Response { Status = 413 })));
AssertAnsweredAndStopped(port, []);
});

runner.Test("h2 early answer (nghttp2): control: an upload its handler reads to the end is sent whole and answered", () =>
{
int port = Start(connection => connection.RunAsync(async (_, body, writer) =>
{
long read = 0;
ReadOnlyMemory<byte> chunk;
while (!(chunk = await body.ReadAsync()).IsEmpty)
{
read += chunk.Length;
}
writer.WriteHeaders(new Nghttp2Response { Status = 200 });
writer.Write(System.Text.Encoding.ASCII.GetBytes($"read {read}"));
}));

using HttpClient client = H2Client();
var upload = new CountedUpload(ReadUpload);
Task<(HttpStatusCode Status, byte[] Body)> answered = PostAsync(client, $"http://127.0.0.1:{port}/", upload);

Assert.True(answered.Wait(100_000), "the answer never arrived");
Assert.Equal(HttpStatusCode.OK, answered.Result.Status);
Assert.Equal($"read {ReadUpload}", System.Text.Encoding.ASCII.GetString(answered.Result.Body));
Assert.Equal(ReadUpload, upload.Ended.Result);
});
}

private static void AssertAnsweredAndStopped(int port, byte[] answer)
{
using HttpClient client = H2Client();
var upload = new CountedUpload(Upload);
Task<(HttpStatusCode Status, byte[] Body)> answered = PostAsync(client, $"http://127.0.0.1:{port}/", upload);

Assert.True(answered.Wait(100_000), "the answer never arrived");
Assert.Equal(HttpStatusCode.RequestEntityTooLarge, answered.Result.Status);
Assert.True(answered.Result.Body.AsSpan().SequenceEqual(answer),
$"the answer arrived {answered.Result.Body.Length} bytes long, of {answer.Length}, or with different content");

long sent = upload.Ended.Result;
Assert.True(sent < Stopped,
$"the client uploaded {sent} of {Upload} bytes to a handler that read none of them: nothing asked it to stop");
}

// The answer, once the upload has ended as well: at its last byte, or where the client gave up on the rest.
private static async Task<(HttpStatusCode Status, byte[] Body)> PostAsync(HttpClient client, string url, CountedUpload upload)
{
try
{
using HttpResponseMessage response = await client.PostAsync(url, upload);
byte[] body = await response.Content.ReadAsByteArrayAsync();

// Held open until then: disposing it would have the client cancel the upload itself.
if (await Task.WhenAny(upload.Ended, Task.Delay(30_000)) != upload.Ended)
{
throw new Exception($"the upload neither finished nor stopped, at {upload.Sent} bytes");
}
return (response.StatusCode, body);
}
catch (HttpRequestException exception)
{
// The default message names neither the protocol error nor the side that raised it.
Exception cause = exception.GetBaseException();
throw new Exception($"{cause.GetType().Name}: {cause.Message}", exception);
}
}

private static int Start(Func<Nghttp2Connection, Task> serve)
=> TestServer.Start(async (_, conn) =>
{
try
{
await serve(new Nghttp2Connection(conn));
}
finally
{
conn.DecRef();
}
});

// Cleartext HTTP/2 with prior knowledge.
private static HttpClient H2Client() => new(new SocketsHttpHandler())
{
DefaultRequestVersion = HttpVersion.Version20,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact,
Timeout = TimeSpan.FromSeconds(60),
};

// Position-dependent, so an answer reassembled out of order fails on content and not only length.
private static byte[] Pattern(int length)
{
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
{
bytes[i] = (byte)((i * 31) ^ (i >> 8));
}
return bytes;
}

// A StreamContent, so not duplex: the client reads its answer once the upload has ended or been stopped.
private sealed class CountedUpload : StreamContent
{
private readonly Source _source;
private readonly TaskCompletionSource<long> _ended = new(TaskCreationOptions.RunContinuationsAsynchronously);

public CountedUpload(long length) : this(new Source(length))
{
}

private CountedUpload(Source source) : base(source, 64 * 1024)
{
_source = source;
Headers.ContentLength = source.Size;
}

public long Sent => _source.Taken;

public Task<long> Ended => _ended.Task;

// The overload a client sends through; StreamContent routes a subclass on to the one without a token.
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
{
try
{
await base.SerializeToStreamAsync(stream, context, cancellationToken);
}
finally
{
_ended.TrySetResult(Sent);
}
}
}

// Zeros, as many as the upload is long, counted as they are taken.
private sealed class Source(long size) : Stream
{
private long _taken;

public long Size => size;

public long Taken => Interlocked.Read(ref _taken);

public override int Read(Span<byte> buffer)
{
int count = (int)Math.Min(buffer.Length, size - Taken);
buffer[..count].Clear();
Interlocked.Add(ref _taken, count);
return count;
}

public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
=> new(Read(buffer.Span));

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> Task.FromResult(Read(buffer.AsSpan(offset, count)));

public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
public override void Flush() { }
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}
}
Loading
Loading