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
9 changes: 7 additions & 2 deletions src/Microsoft.Net.Http.Client/ChunkedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public override async Task<int> 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}'.");
Expand All @@ -81,7 +86,7 @@ public override async Task<int> 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;
Expand All @@ -94,7 +99,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,

if (emptyLine == null)
{
throw new EndOfStreamException();
throw new EndOfStreamException("The connection closed before the empty line ending a chunk.");
}

if (emptyLine.Length > 0)
Expand Down
26 changes: 12 additions & 14 deletions src/Microsoft.Net.Http.Client/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ namespace Microsoft.Net.Http.Client;

internal sealed class HttpConnection : IDisposable
{
private static readonly ISet<string> DockerStreamHeaders = new HashSet<string> { "application/vnd.docker.raw-stream", "application/vnd.docker.multiplexed-stream" };

public HttpConnection(BufferedReadStream transport)
{
Transport = transport;
Expand Down Expand Up @@ -181,40 +179,40 @@ private HttpResponseMessage CreateResponseMessage(List<string> 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, as is any after the value (RFC 9112 §5).
string headerValue = rawHeader.Substring(colonOffset + 1).Trim(' ', '\t');
if (!response.Headers.TryAddWithoutValidation(headerName, headerValue))
{
bool success = response.Content.Headers.TryAddWithoutValidation(headerName, headerValue);
System.Diagnostics.Debug.Assert(success, "Failed to add response header: " + rawHeader);
}
}

// 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. Streaming responses without connection upgrade headers
// - e.g., `/containers/{id}/logs`
// 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.
//
// 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)
{
Expand Down
2 changes: 2 additions & 0 deletions test/Docker.DotNet.Tests/Docker.DotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<Using Include="System.Diagnostics" />
<Using Include="System.IO" />
<Using Include="System.Linq" />
<Using Include="System.Net" />
<Using Include="System.Net.Security" />
<Using Include="System.Net.Sockets" />
<Using Include="System.Security.Cryptography.X509Certificates" />
<Using Include="System.Text" />
<Using Include="System.Text.Json" />
Expand Down
150 changes: 150 additions & 0 deletions test/Docker.DotNet.Tests/LogsResponseFramingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
namespace Docker.DotNet.Tests;

public sealed class LogsResponseFramingTests
{
private const string LogsResponseHeaders = "HTTP/1.1 200 OK\r\nApi-Version: 1.47\r\nContent-Type: application/vnd.docker.multiplexed-stream\r\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";

// 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(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 legacyStdout = await ReadLogsAsync(LegacyHttp.DockerHandlerFactory.Instance, new LegacyHttpTransportOptions(), response);
var nativeStdout = await ReadLogsAsync(NativeHttp.DockerHandlerFactory.Instance, new NativeHttpTransportOptions(), response);

Assert.Equal(expectedStdout, nativeStdout);
Assert.Equal(nativeStdout, legacyStdout);
}

[Theory]
[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 legacyException = await Assert.ThrowsAsync<EndOfStreamException>(() => ReadLogsAsync(LegacyHttp.DockerHandlerFactory.Instance, new LegacyHttpTransportOptions(), response));
await Assert.ThrowsAnyAsync<IOException>(() => ReadLogsAsync(NativeHttp.DockerHandlerFactory.Instance, new NativeHttpTransportOptions(), response));

Assert.Equal(expectedMessage, legacyException.Message);
}

private static async Task<string> ReadLogsAsync<TTransportOptions>(IDockerHandlerFactory<TTransportOptions> transportFactory, TTransportOptions transportOptions, string response)
{
var parameters = new ContainerLogsParameters
{
ShowStderr = true,
ShowStdout = true
};

await using var daemon = new FakeDaemon(response);

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 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 the given response,
// and the container inspect with a non-TTY container.
private sealed class FakeDaemon : IAsyncDisposable
{
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);

private readonly CancellationTokenSource _stopping = new();

private readonly Task _serving;

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}");
}

public Uri Endpoint { get; }

public async ValueTask DisposeAsync()
{
_stopping.Cancel();

try
{
await _serving
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// This is expected when the daemon is disposed.
}
finally
{
_listener.Stop();
_stopping.Dispose();
}
}

private async Task ServeAsync(byte[] logsResponse)
{
while (true)
{
using var client = await _listener.AcceptTcpClientAsync(_stopping.Token)
.ConfigureAwait(false);

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 header = await reader.ReadLineAsync(_stopping.Token)
.ConfigureAwait(false);

if (string.IsNullOrEmpty(header))
{
break;
}
}
while (true);

var response = requestLine?.Contains("/logs") == true ? logsResponse : InspectResponse;

await stream.WriteAsync(response, _stopping.Token)
.ConfigureAwait(false);
}
}
}
}