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
10 changes: 7 additions & 3 deletions src/Docker.DotNet/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ private async Task<HttpResponseMessage> PrivateMakeRequestAsync(

if (Timeout.InfiniteTimeSpan == timeout)
{
#if NET6_0_OR_GREATER
return await _client.SendAsync(request, completionOption, cancellationToken)
.ConfigureAwait(false);
#else
var tcs = new TaskCompletionSource<HttpResponseMessage>(
TaskCreationOptions.RunContinuationsAsynchronously);

Expand All @@ -372,12 +376,12 @@ private async Task<HttpResponseMessage> PrivateMakeRequestAsync(

return await await Task.WhenAny(tcs.Task, _client.SendAsync(request, completionOption, cancellationToken))
.ConfigureAwait(false);
#endif
}
else
{
using var timeoutCts = new CancellationTokenSource(timeout);

using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
linkedCts.CancelAfter(timeout);

return await _client.SendAsync(request, completionOption, linkedCts.Token)
.ConfigureAwait(false);
Expand Down
28 changes: 22 additions & 6 deletions src/Microsoft.Net.Http.Client/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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" };
private static readonly ISet<string> DockerStreamHeaders = new HashSet<string> { "application/vnd.docker.raw-stream", "application/vnd.docker.multiplexed-stream" };

public HttpConnection(BufferedReadStream transport)
{
Expand All @@ -18,27 +18,43 @@ public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, Can
// Serialize headers & send
string rawRequest = SerializeRequest(request);
byte[] requestBytes = Encoding.ASCII.GetBytes(rawRequest);
await Transport.WriteAsync(requestBytes, 0, requestBytes.Length, cancellationToken);

await Transport.WriteAsync(requestBytes, 0, requestBytes.Length, cancellationToken)
.ConfigureAwait(false);

if (request.Content != null)
{
if (request.Content.Headers.ContentLength.HasValue)
{
await request.Content.CopyToAsync(Transport);
#if NET6_0_OR_GREATER
await request.Content.CopyToAsync(Transport, cancellationToken)
.ConfigureAwait(false);
#else
await request.Content.CopyToAsync(Transport)
.ConfigureAwait(false);
#endif
}
else
{
// The length of the data is unknown. Send it in chunked mode.
using (var chunkedStream = new ChunkedWriteStream(Transport))
{
await request.Content.CopyToAsync(chunkedStream);
await chunkedStream.EndContentAsync(cancellationToken);
#if NET6_0_OR_GREATER
await request.Content.CopyToAsync(chunkedStream, cancellationToken)
.ConfigureAwait(false);
#else
await request.Content.CopyToAsync(chunkedStream)
.ConfigureAwait(false);
#endif
await chunkedStream.EndContentAsync(cancellationToken)
.ConfigureAwait(false);
}
}
}

// Receive headers
List<string> responseLines = await ReadResponseLinesAsync(cancellationToken);
List<string> responseLines = await ReadResponseLinesAsync(cancellationToken)
.ConfigureAwait(false);

// Receive body and determine the response type (Content-Length, Transfer-Encoding, Opaque)
return CreateResponseMessage(responseLines);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ public WriteClosableStream HijackStream()
return _connection.Transport;
}

protected override Task SerializeToStreamAsync(Stream stream, System.Net.TransportContext context)
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return _responseStream.CopyToAsync(stream);
}

#if NET6_0_OR_GREATER
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
{
return _responseStream.CopyToAsync(stream, cancellationToken);
}
#endif

protected override Task<Stream> CreateContentReadStreamAsync()
{
return Task.FromResult(_responseStream);
Expand Down
Loading