Skip to content

Commit 94cc871

Browse files
committed
DecompressionHandler: Set cancellation token for SerializeToStreamAsync override
The virtual method available in .NET Standard 2.1 does not include the cancellation token parameter, so we use an alternative method to set it in advance.
1 parent 00cb5e6 commit 94cc871

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

StandardSocketsHttpHandler/Net/Http/DecompressionHandler.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5959
if (GZipEnabled && last == s_gzip)
6060
{
6161
response.Content = new GZipDecompressedContent(response.Content);
62+
((GZipDecompressedContent)response.Content).SetDefaultCancellationToken(cancellationToken);
6263
}
6364
else if (DeflateEnabled && last == s_deflate)
6465
{
6566
response.Content = new DeflateDecompressedContent(response.Content);
67+
((DeflateDecompressedContent)response.Content).SetDefaultCancellationToken(cancellationToken);
6668
}
6769
}
6870

@@ -82,6 +84,7 @@ protected override void Dispose(bool disposing)
8284
private abstract class DecompressedContent : HttpContent
8385
{
8486
HttpContent _originalContent;
87+
private CancellationToken _cancellationToken;
8588
bool _contentConsumed;
8689

8790
public DecompressedContent(HttpContent originalContent)
@@ -106,13 +109,24 @@ public DecompressedContent(HttpContent originalContent)
106109
}
107110
}
108111

112+
public void SetDefaultCancellationToken(CancellationToken cancellationToken)
113+
{
114+
Debug.Assert(cancellationToken != null);
115+
116+
_cancellationToken = cancellationToken;
117+
}
118+
109119
protected abstract Stream GetDecompressedStream(Stream originalStream);
110120

111-
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
121+
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) =>
122+
SerializeToStreamAsyncInternal(stream, context, _cancellationToken);
123+
124+
internal async Task SerializeToStreamAsyncInternal(Stream stream, TransportContext context, CancellationToken cancellationToken)
112125
{
113126
using (Stream decompressedStream = await CreateContentReadStreamAsync().ConfigureAwait(false))
114127
{
115-
await decompressedStream.CopyToAsync(stream).ConfigureAwait(false);
128+
const int BufferSize = 81920; // Stream.DefaultCopyBufferSize
129+
await decompressedStream.CopyToAsync(stream, BufferSize, cancellationToken).ConfigureAwait(false);
116130
}
117131
}
118132

0 commit comments

Comments
 (0)