From 124b1e2f2eea098f934d77c06befd5f14f09bb30 Mon Sep 17 00:00:00 2001 From: JT Date: Tue, 27 Oct 2026 11:38:33 +0800 Subject: [PATCH 1/3] Align chunk reading between legacy and modern transports --- .../ChunkedReadStream.cs | 9 +- .../HttpConnection.cs | 21 +-- .../LogsResponseFramingTests.cs | 164 ++++++++++++++++++ 3 files changed, 180 insertions(+), 14 deletions(-) create mode 100644 test/Docker.DotNet.Tests/LogsResponseFramingTests.cs diff --git a/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs b/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs index e4e26346..e2f6ca2f 100644 --- a/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs +++ b/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs @@ -64,6 +64,11 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, var headerLine = await _inner.ReadLineAsync(cancellationToken) .ConfigureAwait(false); + if (headerLine == null) + { + throw new EndOfStreamException("The connection closed before the next chunk header."); + } + if (!int.TryParse(headerLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out _chunkBytesRemaining)) { throw new IOException($"Invalid chunk header encountered: '{headerLine}'."); @@ -81,7 +86,7 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, if (readBytesCount == 0) { - throw new EndOfStreamException(); + throw new EndOfStreamException("The connection closed in the middle of a chunk."); } _chunkBytesRemaining -= readBytesCount; @@ -94,7 +99,7 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, if (emptyLine == null) { - throw new EndOfStreamException(); + throw new EndOfStreamException("The connection closed before the line ending a chunk."); } if (emptyLine.Length > 0) diff --git a/src/Microsoft.Net.Http.Client/HttpConnection.cs b/src/Microsoft.Net.Http.Client/HttpConnection.cs index 91933bd2..8dbf1b74 100644 --- a/src/Microsoft.Net.Http.Client/HttpConnection.cs +++ b/src/Microsoft.Net.Http.Client/HttpConnection.cs @@ -2,8 +2,6 @@ namespace Microsoft.Net.Http.Client; internal sealed class HttpConnection : IDisposable { - private static readonly ISet DockerStreamHeaders = new HashSet { "application/vnd.docker.raw-stream", "application/vnd.docker.multiplexed-stream" }; - public HttpConnection(BufferedReadStream transport) { Transport = transport; @@ -181,7 +179,8 @@ private HttpResponseMessage CreateResponseMessage(List responseLines) throw new HttpRequestException("The given header line format is invalid: " + rawHeader); } string headerName = rawHeader.Substring(0, colonOffset); - string headerValue = rawHeader.Substring(colonOffset + 2); + // The space after the colon is optional whitespace (RFC 9110, section 5.6.3), as is any after the value. + string headerValue = rawHeader.Substring(colonOffset + 1).Trim(' ', '\t'); if (!response.Headers.TryAddWithoutValidation(headerName, headerValue)) { bool success = response.Content.Headers.TryAddWithoutValidation(headerName, headerValue); @@ -197,24 +196,22 @@ private HttpResponseMessage CreateResponseMessage(List responseLines) // - For 101 Switching Protocols, `Content-Length` is ignored per RFC 9110 // 3. Protocol upgrades (HTTP 101 + `Upgrade: tcp`) // - e.g., `/containers/{id}/attach` or `/exec/{id}/start` - // 4. Streaming responses without connection upgrade headers - // - e.g., `/containers/{id}/logs` + // 4. Responses with neither, read until the connection closes // // This separation ensures chunked framing, streaming, and upgraded connections are handled // correctly, while tolerating proxies that incorrectly send `Content-Length: 0` on upgrades. + // + // Docker's stream content types (`application/vnd.docker.raw-stream` and + // `application/vnd.docker.multiplexed-stream`) describe the body, not how it is framed on the + // connection. A `/containers/{id}/logs` response is chunked when the daemon flushes it before + // finishing, but an empty one can arrive with `Content-Length: 0` instead. var isSwitchingProtocols = response.StatusCode == HttpStatusCode.SwitchingProtocols; var isConnectionUpgrade = response.Headers.TryGetValues("Upgrade", out var responseHeaderValues) && responseHeaderValues.Any(header => "tcp".Equals(header, StringComparison.OrdinalIgnoreCase)); - var isStream = content.Headers.TryGetValues("Content-Type", out var contentHeaderValues) - && contentHeaderValues.Any(DockerStreamHeaders.Contains); - - // Treat the response as chunked for standard HTTP chunked or Docker raw-streams, - // but not for upgraded connections. - var isChunkedTransferEncoding = (response.Headers.TransferEncodingChunked.GetValueOrDefault() && !isConnectionUpgrade) - || (!isConnectionUpgrade && isStream); + var isChunkedTransferEncoding = response.Headers.TransferEncodingChunked.GetValueOrDefault() && !isConnectionUpgrade; if (isSwitchingProtocols && isConnectionUpgrade) { diff --git a/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs b/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs new file mode 100644 index 00000000..da376f44 --- /dev/null +++ b/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs @@ -0,0 +1,164 @@ +using System.Net; +using System.Net.Sockets; + +namespace Docker.DotNet.Tests; + +/// +/// Serves /containers/{id}/logs responses in each framing HTTP allows from a fake daemon, and reads them through +/// the legacy and native transports. Both must read the same output: the framing comes from Transfer-Encoding +/// or Content-Length, not from Docker's stream content type. +/// +public sealed class LogsResponseFramingTests +{ + private const string Head = "HTTP/1.1 200 OK\r\nApi-Version: 1.47\r\nContent-Type: application/vnd.docker.multiplexed-stream\r\n"; + + // One stdout frame: stream type 1, three padding bytes, a big-endian length of 6, then the payload. + private const string Frame = "\u0001\0\0\0\0\0\0\u0006hello\n"; + + private static readonly Dictionary Framings = new() + { + ["chunked, empty"] = (Head + "Transfer-Encoding: chunked\r\n\r\n0\r\n\r\n", ""), + ["chunked, one frame"] = (Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame + "\r\n0\r\n\r\n", "hello\n"), + ["Content-Length 0"] = (Head + "Content-Length: 0\r\n\r\n", ""), + ["Content-Length, one frame"] = (Head + "Content-Length: 14\r\n\r\n" + Frame, "hello\n"), + ["close-delimited, empty"] = (Head + "Connection: close\r\n\r\n", ""), + ["close-delimited, one frame"] = (Head + "Connection: close\r\n\r\n" + Frame, "hello\n"), + ["chunked, no space after the header colons"] = ("HTTP/1.1 200 OK\r\nContent-Type:application/vnd.docker.multiplexed-stream\r\nTransfer-Encoding:chunked\r\n\r\ne\r\n" + Frame + "\r\n0\r\n\r\n", "hello\n"), + ["Content-Length, tabs and spaces around the header value"] = (Head + "Content-Length:\t 14 \t\r\n\r\n" + Frame, "hello\n"), + }; + + [Theory] + [InlineData("chunked, empty")] + [InlineData("chunked, one frame")] + [InlineData("Content-Length 0")] + [InlineData("Content-Length, one frame")] + [InlineData("close-delimited, empty")] + [InlineData("close-delimited, one frame")] + [InlineData("chunked, no space after the header colons")] + [InlineData("Content-Length, tabs and spaces around the header value")] + public async Task GetContainerLogsAsync_ReadsEachFramingLikeTheNativeTransport(string framing) + { + var (response, expectedStdout) = Framings[framing]; + + var legacy = await ReadLogsAsync(new LegacyHttpTransportOptions(), response); + var native = await ReadLogsAsync(new NativeHttpTransportOptions(), response); + + Assert.Equal(expectedStdout, native); + Assert.Equal(native, legacy); + } + + [Theory] + [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame + "\r\n", "The connection closed before the next chunk header.")] + [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n\u0001\0\0\0", "The connection closed in the middle of a chunk.")] + [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame, "The connection closed before the line ending a chunk.")] + public async Task GetContainerLogsAsync_FailsOnAChunkedResponseCutOffBeforeItsLastChunk(string response, string legacyMessage) + { + var legacy = await Assert.ThrowsAsync(() => ReadLogsAsync(new LegacyHttpTransportOptions(), response)); + await Assert.ThrowsAnyAsync(() => ReadLogsAsync(new NativeHttpTransportOptions(), response)); + + Assert.Equal(legacyMessage, legacy.Message); + } + + private static Task ReadLogsAsync(LegacyHttpTransportOptions transportOptions, string response) + { + return ReadLogsAsync(endpoint => new DockerClientBuilder().WithEndpoint(endpoint).WithTransportOptions(transportOptions).Build(), response); + } + + private static Task ReadLogsAsync(NativeHttpTransportOptions transportOptions, string response) + { + return ReadLogsAsync(endpoint => new DockerClientBuilder().WithEndpoint(endpoint).WithTransportOptions(transportOptions).Build(), response); + } + + private static async Task ReadLogsAsync(Func createClient, string response) + { + await using var daemon = new FakeDaemon(response); + using var client = createClient(daemon.Endpoint); + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + + using var logs = await client.Containers.GetContainerLogsAsync("x", new ContainerLogsParameters { ShowStdout = true, ShowStderr = true }, cts.Token); + var (stdout, _) = await logs.ReadOutputToEndAsync(cts.Token); + return stdout; + } + + /// + /// Answers /logs with a fixed raw response, and every other request (the client inspects the container to + /// learn whether it has a TTY) with a minimal container JSON. It closes each connection after one response. + /// + private sealed class FakeDaemon : IAsyncDisposable + { + private const string Inspect = "{\"Id\":\"x\",\"Config\":{\"Tty\":false}}"; + + private readonly TcpListener _listener = new(IPAddress.Loopback, 0); + + private readonly CancellationTokenSource _stopping = new(); + + private readonly Task _serving; + + public FakeDaemon(string logsResponse) + { + _listener.Start(); + Endpoint = new Uri($"tcp://127.0.0.1:{((IPEndPoint)_listener.LocalEndpoint).Port}"); + _serving = ServeAsync(logsResponse); + } + + public Uri Endpoint { get; } + + public async ValueTask DisposeAsync() + { + _stopping.Cancel(); + + try + { + await _serving.ConfigureAwait(false); + } + finally + { + _listener.Stop(); + _stopping.Dispose(); + } + } + + private async Task ServeAsync(string logsResponse) + { + while (true) + { + TcpClient client; + try + { + client = await _listener.AcceptTcpClientAsync(_stopping.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + return; + } + + using (client) + { + var stream = client.GetStream(); + var request = new StringBuilder(); + var buffer = new byte[4096]; + while (!request.ToString().Contains("\r\n\r\n")) + { + var read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); + if (read == 0) + { + break; + } + + request.Append(Encoding.ASCII.GetString(buffer, 0, read)); + } + + var requestLine = request.ToString().Split(["\r\n"], StringSplitOptions.None)[0]; + var reply = requestLine.Contains("/logs") + ? logsResponse + : $"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {Inspect.Length}\r\nConnection: close\r\n\r\n{Inspect}"; + + // Latin-1 maps each char to one byte, so the frame header's control bytes go out unchanged. + var bytes = Encoding.Latin1.GetBytes(reply); + await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); + client.Client.Shutdown(SocketShutdown.Both); + } + } + } + } +} From f3b9aa7869af513b15f7e970f101c0fe2b5e33cd Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 27 Oct 2026 16:49:38 +0100 Subject: [PATCH 2/3] chore: Align comments, exception message, and test names with repo conventions --- src/Microsoft.Net.Http.Client/ChunkedReadStream.cs | 2 +- src/Microsoft.Net.Http.Client/HttpConnection.cs | 9 +++++---- test/Docker.DotNet.Tests/LogsResponseFramingTests.cs | 9 +++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs b/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs index e2f6ca2f..ad220b4a 100644 --- a/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs +++ b/src/Microsoft.Net.Http.Client/ChunkedReadStream.cs @@ -99,7 +99,7 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, if (emptyLine == null) { - throw new EndOfStreamException("The connection closed before the line ending a chunk."); + throw new EndOfStreamException("The connection closed before the empty line ending a chunk."); } if (emptyLine.Length > 0) diff --git a/src/Microsoft.Net.Http.Client/HttpConnection.cs b/src/Microsoft.Net.Http.Client/HttpConnection.cs index 8dbf1b74..056b1bae 100644 --- a/src/Microsoft.Net.Http.Client/HttpConnection.cs +++ b/src/Microsoft.Net.Http.Client/HttpConnection.cs @@ -179,7 +179,7 @@ private HttpResponseMessage CreateResponseMessage(List responseLines) throw new HttpRequestException("The given header line format is invalid: " + rawHeader); } string headerName = rawHeader.Substring(0, colonOffset); - // The space after the colon is optional whitespace (RFC 9110, section 5.6.3), as is any after the value. + // The space after the colon is optional whitespace, as is any after the value (RFC 9112 ยง5). string headerValue = rawHeader.Substring(colonOffset + 1).Trim(' ', '\t'); if (!response.Headers.TryAddWithoutValidation(headerName, headerValue)) { @@ -188,15 +188,16 @@ private HttpResponseMessage CreateResponseMessage(List responseLines) } } - // Response handling is based on headers and type. The implementation currently covers - // four main cases: + // Response handling is based on the status code and headers. The implementation currently + // covers four main cases: // // 1. Chunked transfer encoding (HTTP/1.1 `Transfer-Encoding: chunked`) // 2. HTTP responses with a `Content-Length` header // - For 101 Switching Protocols, `Content-Length` is ignored per RFC 9110 // 3. Protocol upgrades (HTTP 101 + `Upgrade: tcp`) // - e.g., `/containers/{id}/attach` or `/exec/{id}/start` - // 4. Responses with neither, read until the connection closes + // 4. Responses without chunked transfer encoding or a `Content-Length` header + // - The body is read until the connection closes // // This separation ensures chunked framing, streaming, and upgraded connections are handled // correctly, while tolerating proxies that incorrectly send `Content-Length: 0` on upgrades. diff --git a/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs b/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs index da376f44..6f448241 100644 --- a/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs +++ b/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs @@ -36,7 +36,7 @@ public sealed class LogsResponseFramingTests [InlineData("close-delimited, one frame")] [InlineData("chunked, no space after the header colons")] [InlineData("Content-Length, tabs and spaces around the header value")] - public async Task GetContainerLogsAsync_ReadsEachFramingLikeTheNativeTransport(string framing) + public async Task GetContainerLogsAsync_ResponseFraming_ReadsLikeNativeTransport(string framing) { var (response, expectedStdout) = Framings[framing]; @@ -50,8 +50,8 @@ public async Task GetContainerLogsAsync_ReadsEachFramingLikeTheNativeTransport(s [Theory] [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame + "\r\n", "The connection closed before the next chunk header.")] [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n\u0001\0\0\0", "The connection closed in the middle of a chunk.")] - [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame, "The connection closed before the line ending a chunk.")] - public async Task GetContainerLogsAsync_FailsOnAChunkedResponseCutOffBeforeItsLastChunk(string response, string legacyMessage) + [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame, "The connection closed before the empty line ending a chunk.")] + public async Task GetContainerLogsAsync_TruncatedChunkedResponse_ThrowsIOException(string response, string legacyMessage) { var legacy = await Assert.ThrowsAsync(() => ReadLogsAsync(new LegacyHttpTransportOptions(), response)); await Assert.ThrowsAnyAsync(() => ReadLogsAsync(new NativeHttpTransportOptions(), response)); @@ -73,7 +73,8 @@ private static async Task ReadLogsAsync(Func createCl { await using var daemon = new FakeDaemon(response); using var client = createClient(daemon.Endpoint); - using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken); + cts.CancelAfter(TimeSpan.FromSeconds(10)); using var logs = await client.Containers.GetContainerLogsAsync("x", new ContainerLogsParameters { ShowStdout = true, ShowStderr = true }, cts.Token); var (stdout, _) = await logs.ReadOutputToEndAsync(cts.Token); From 4ead25f5a9516d2981faf01794d154c76bc478b4 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 27 Oct 2026 19:15:18 +0100 Subject: [PATCH 3/3] chore: Simplify logs framing test fake daemon and align with repo style --- .../Docker.DotNet.Tests.csproj | 2 + .../LogsResponseFramingTests.cs | 177 ++++++++---------- 2 files changed, 83 insertions(+), 96 deletions(-) diff --git a/test/Docker.DotNet.Tests/Docker.DotNet.Tests.csproj b/test/Docker.DotNet.Tests/Docker.DotNet.Tests.csproj index 064c3b68..3ff7f766 100644 --- a/test/Docker.DotNet.Tests/Docker.DotNet.Tests.csproj +++ b/test/Docker.DotNet.Tests/Docker.DotNet.Tests.csproj @@ -31,7 +31,9 @@ + + diff --git a/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs b/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs index 6f448241..7892cff2 100644 --- a/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs +++ b/test/Docker.DotNet.Tests/LogsResponseFramingTests.cs @@ -1,93 +1,79 @@ -using System.Net; -using System.Net.Sockets; - namespace Docker.DotNet.Tests; -/// -/// Serves /containers/{id}/logs responses in each framing HTTP allows from a fake daemon, and reads them through -/// the legacy and native transports. Both must read the same output: the framing comes from Transfer-Encoding -/// or Content-Length, not from Docker's stream content type. -/// public sealed class LogsResponseFramingTests { - private const string Head = "HTTP/1.1 200 OK\r\nApi-Version: 1.47\r\nContent-Type: application/vnd.docker.multiplexed-stream\r\n"; + private const string LogsResponseHeaders = "HTTP/1.1 200 OK\r\nApi-Version: 1.47\r\nContent-Type: application/vnd.docker.multiplexed-stream\r\n"; - // One stdout frame: stream type 1, three padding bytes, a big-endian length of 6, then the payload. - private const string Frame = "\u0001\0\0\0\0\0\0\u0006hello\n"; + private const string LogsResponseHeadersWithoutSpaces = "HTTP/1.1 200 OK\r\nApi-Version:1.47\r\nContent-Type:application/vnd.docker.multiplexed-stream\r\n"; - private static readonly Dictionary Framings = new() - { - ["chunked, empty"] = (Head + "Transfer-Encoding: chunked\r\n\r\n0\r\n\r\n", ""), - ["chunked, one frame"] = (Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame + "\r\n0\r\n\r\n", "hello\n"), - ["Content-Length 0"] = (Head + "Content-Length: 0\r\n\r\n", ""), - ["Content-Length, one frame"] = (Head + "Content-Length: 14\r\n\r\n" + Frame, "hello\n"), - ["close-delimited, empty"] = (Head + "Connection: close\r\n\r\n", ""), - ["close-delimited, one frame"] = (Head + "Connection: close\r\n\r\n" + Frame, "hello\n"), - ["chunked, no space after the header colons"] = ("HTTP/1.1 200 OK\r\nContent-Type:application/vnd.docker.multiplexed-stream\r\nTransfer-Encoding:chunked\r\n\r\ne\r\n" + Frame + "\r\n0\r\n\r\n", "hello\n"), - ["Content-Length, tabs and spaces around the header value"] = (Head + "Content-Length:\t 14 \t\r\n\r\n" + Frame, "hello\n"), - }; + // One 14-byte stdout frame: stream type 1, three padding bytes, + // a big-endian length of 6, then the payload. + private const string StdoutFrame = "\u0001\0\0\0\0\0\0\u0006hello\n"; [Theory] - [InlineData("chunked, empty")] - [InlineData("chunked, one frame")] - [InlineData("Content-Length 0")] - [InlineData("Content-Length, one frame")] - [InlineData("close-delimited, empty")] - [InlineData("close-delimited, one frame")] - [InlineData("chunked, no space after the header colons")] - [InlineData("Content-Length, tabs and spaces around the header value")] - public async Task GetContainerLogsAsync_ResponseFraming_ReadsLikeNativeTransport(string framing) + [InlineData(LogsResponseHeaders + "Transfer-Encoding: chunked\r\n\r\n0\r\n\r\n", "", Label = "chunked, empty")] + [InlineData(LogsResponseHeaders + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + StdoutFrame + "\r\n0\r\n\r\n", "hello\n", Label = "chunked, one frame")] + [InlineData(LogsResponseHeaders + "Content-Length: 0\r\n\r\n", "", Label = "Content-Length, empty")] + [InlineData(LogsResponseHeaders + "Content-Length: 14\r\n\r\n" + StdoutFrame, "hello\n", Label = "Content-Length, one frame")] + [InlineData(LogsResponseHeaders + "Connection: close\r\n\r\n", "", Label = "close-delimited, empty")] + [InlineData(LogsResponseHeaders + "Connection: close\r\n\r\n" + StdoutFrame, "hello\n", Label = "close-delimited, one frame")] + [InlineData(LogsResponseHeadersWithoutSpaces + "Transfer-Encoding:chunked\r\n\r\ne\r\n" + StdoutFrame + "\r\n0\r\n\r\n", "hello\n", Label = "chunked, no spaces after header colons")] + [InlineData(LogsResponseHeaders + "Content-Length:\t 14 \t\r\n\r\n" + StdoutFrame, "hello\n", Label = "Content-Length, tabs and spaces around header value")] + public async Task GetContainerLogsAsync_ResponseFraming_ReadsLikeNativeTransport(string response, string expectedStdout) { - var (response, expectedStdout) = Framings[framing]; + var legacyStdout = await ReadLogsAsync(LegacyHttp.DockerHandlerFactory.Instance, new LegacyHttpTransportOptions(), response); + var nativeStdout = await ReadLogsAsync(NativeHttp.DockerHandlerFactory.Instance, new NativeHttpTransportOptions(), response); - var legacy = await ReadLogsAsync(new LegacyHttpTransportOptions(), response); - var native = await ReadLogsAsync(new NativeHttpTransportOptions(), response); - - Assert.Equal(expectedStdout, native); - Assert.Equal(native, legacy); + Assert.Equal(expectedStdout, nativeStdout); + Assert.Equal(nativeStdout, legacyStdout); } [Theory] - [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame + "\r\n", "The connection closed before the next chunk header.")] - [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n\u0001\0\0\0", "The connection closed in the middle of a chunk.")] - [InlineData(Head + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + Frame, "The connection closed before the empty line ending a chunk.")] - public async Task GetContainerLogsAsync_TruncatedChunkedResponse_ThrowsIOException(string response, string legacyMessage) + [InlineData(LogsResponseHeaders + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + StdoutFrame + "\r\n", "The connection closed before the next chunk header.", Label = "before next chunk header")] + [InlineData(LogsResponseHeaders + "Transfer-Encoding: chunked\r\n\r\ne\r\n\u0001\0\0\0", "The connection closed in the middle of a chunk.", Label = "in middle of chunk")] + [InlineData(LogsResponseHeaders + "Transfer-Encoding: chunked\r\n\r\ne\r\n" + StdoutFrame, "The connection closed before the empty line ending a chunk.", Label = "before empty line ending chunk")] + public async Task GetContainerLogsAsync_TruncatedChunkedResponse_ThrowsIOException(string response, string expectedMessage) { - var legacy = await Assert.ThrowsAsync(() => ReadLogsAsync(new LegacyHttpTransportOptions(), response)); - await Assert.ThrowsAnyAsync(() => ReadLogsAsync(new NativeHttpTransportOptions(), response)); + var legacyException = await Assert.ThrowsAsync(() => ReadLogsAsync(LegacyHttp.DockerHandlerFactory.Instance, new LegacyHttpTransportOptions(), response)); + await Assert.ThrowsAnyAsync(() => ReadLogsAsync(NativeHttp.DockerHandlerFactory.Instance, new NativeHttpTransportOptions(), response)); - Assert.Equal(legacyMessage, legacy.Message); + Assert.Equal(expectedMessage, legacyException.Message); } - private static Task ReadLogsAsync(LegacyHttpTransportOptions transportOptions, string response) + private static async Task ReadLogsAsync(IDockerHandlerFactory transportFactory, TTransportOptions transportOptions, string response) { - return ReadLogsAsync(endpoint => new DockerClientBuilder().WithEndpoint(endpoint).WithTransportOptions(transportOptions).Build(), response); - } - - private static Task ReadLogsAsync(NativeHttpTransportOptions transportOptions, string response) - { - return ReadLogsAsync(endpoint => new DockerClientBuilder().WithEndpoint(endpoint).WithTransportOptions(transportOptions).Build(), response); - } + var parameters = new ContainerLogsParameters + { + ShowStderr = true, + ShowStdout = true + }; - private static async Task ReadLogsAsync(Func createClient, string response) - { await using var daemon = new FakeDaemon(response); - using var client = createClient(daemon.Endpoint); + + using var client = new DockerClientBuilder() + .WithEndpoint(daemon.Endpoint) + .WithTransportOptions(transportFactory, transportOptions) + .Build(); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken); cts.CancelAfter(TimeSpan.FromSeconds(10)); - using var logs = await client.Containers.GetContainerLogsAsync("x", new ContainerLogsParameters { ShowStdout = true, ShowStderr = true }, cts.Token); - var (stdout, _) = await logs.ReadOutputToEndAsync(cts.Token); + using var multiplexedStream = await client.Containers.GetContainerLogsAsync("x", parameters, cts.Token) + .ConfigureAwait(false); + + var (stdout, _) = await multiplexedStream.ReadOutputToEndAsync(cts.Token) + .ConfigureAwait(false); + return stdout; } - /// - /// Answers /logs with a fixed raw response, and every other request (the client inspects the container to - /// learn whether it has a TTY) with a minimal container JSON. It closes each connection after one response. - /// + // Answers /logs with the given response, + // and the container inspect with a non-TTY container. private sealed class FakeDaemon : IAsyncDisposable { - private const string Inspect = "{\"Id\":\"x\",\"Config\":{\"Tty\":false}}"; + private const string InspectJson = "{\"Id\":\"x\",\"Config\":{\"Tty\":false}}"; + + private static readonly byte[] InspectResponse = Encoding.Latin1.GetBytes($"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {InspectJson.Length}\r\nConnection: close\r\n\r\n{InspectJson}"); private readonly TcpListener _listener = new(IPAddress.Loopback, 0); @@ -98,8 +84,11 @@ private sealed class FakeDaemon : IAsyncDisposable public FakeDaemon(string logsResponse) { _listener.Start(); + + // Latin-1 maps each char to the byte with the same value, so frame + // bytes above 0x7F are sent unchanged. + _serving = ServeAsync(Encoding.Latin1.GetBytes(logsResponse)); Endpoint = new Uri($"tcp://127.0.0.1:{((IPEndPoint)_listener.LocalEndpoint).Port}"); - _serving = ServeAsync(logsResponse); } public Uri Endpoint { get; } @@ -110,7 +99,12 @@ public async ValueTask DisposeAsync() try { - await _serving.ConfigureAwait(false); + await _serving + .ConfigureAwait(false); + } + catch (OperationCanceledException) + { + // This is expected when the daemon is disposed. } finally { @@ -119,47 +113,38 @@ public async ValueTask DisposeAsync() } } - private async Task ServeAsync(string logsResponse) + private async Task ServeAsync(byte[] logsResponse) { while (true) { - TcpClient client; - try - { - client = await _listener.AcceptTcpClientAsync(_stopping.Token).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - return; - } + using var client = await _listener.AcceptTcpClientAsync(_stopping.Token) + .ConfigureAwait(false); - using (client) + using var stream = client.GetStream(); + using var reader = new StreamReader(stream, Encoding.Latin1); + + var requestLine = await reader.ReadLineAsync(_stopping.Token) + .ConfigureAwait(false); + + // Reads the remaining request headers, + // so closing the connection doesn't reset it. + do { - var stream = client.GetStream(); - var request = new StringBuilder(); - var buffer = new byte[4096]; - while (!request.ToString().Contains("\r\n\r\n")) - { - var read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); - if (read == 0) - { - break; - } + var header = await reader.ReadLineAsync(_stopping.Token) + .ConfigureAwait(false); - request.Append(Encoding.ASCII.GetString(buffer, 0, read)); + if (string.IsNullOrEmpty(header)) + { + break; } + } + while (true); - var requestLine = request.ToString().Split(["\r\n"], StringSplitOptions.None)[0]; - var reply = requestLine.Contains("/logs") - ? logsResponse - : $"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {Inspect.Length}\r\nConnection: close\r\n\r\n{Inspect}"; + var response = requestLine?.Contains("/logs") == true ? logsResponse : InspectResponse; - // Latin-1 maps each char to one byte, so the frame header's control bytes go out unchanged. - var bytes = Encoding.Latin1.GetBytes(reply); - await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); - client.Client.Shutdown(SocketShutdown.Both); - } + await stream.WriteAsync(response, _stopping.Token) + .ConfigureAwait(false); } } } -} +} \ No newline at end of file