1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http
{
internal partial class HttpConnection : IDisposable
{
private sealed class ConnectionCloseReadStream : HttpContentReadStream
{
public ConnectionCloseReadStream(HttpConnection connection) : base(connection)
{
}
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
if (_connection == null || buffer.Length == 0)
{
// Response body fully consumed or the caller didn't ask for any data
return 0;
}
ValueTask<int> readTask = _connection.ReadAsync(buffer);
int bytesRead;
if (readTask.IsCompletedSuccessfully)
{
bytesRead = readTask.Result;
}
else
{
CancellationTokenRegistration ctr = _connection.RegisterCancellation(cancellationToken);
try
{
bytesRead = await readTask.ConfigureAwait(false);
}
catch (Exception exc) when (CancellationHelper.ShouldWrapInOperationCanceledException(exc, cancellationToken))
{
throw CancellationHelper.CreateOperationCanceledException(exc, cancellationToken);
}
finally
{
ctr.Dispose();
}
}
if (bytesRead == 0)
{
// If cancellation is requested and tears down the connection, it could cause the read
// to return 0, which would otherwise signal the end of the data, but that would lead
// the caller to think that it actually received all of the data, rather than it ending
// early due to cancellation. So we prioritize cancellation in this race condition, and
// if we read 0 bytes and then find that cancellation has requested, we assume cancellation
// was the cause and throw.
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
// We cannot reuse this connection, so close it.
_connection.Dispose();
_connection = null;
return 0;
}
return bytesRead;
}
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
ValidateCopyToArgs(this, destination, bufferSize);
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}
if (_connection == null)
{
// null if response body fully consumed
return Task.CompletedTask;
}
Task copyTask = _connection.CopyToUntilEofAsync(destination, bufferSize, cancellationToken);
if (copyTask.IsCompletedSuccessfully)
{
Finish();
return Task.CompletedTask;
}
return CompleteCopyToAsync(copyTask, cancellationToken);
}
private async Task CompleteCopyToAsync(Task copyTask, CancellationToken cancellationToken)
{
CancellationTokenRegistration ctr = _connection.RegisterCancellation(cancellationToken);
try
{
await copyTask.ConfigureAwait(false);
}
catch (Exception exc) when (CancellationHelper.ShouldWrapInOperationCanceledException(exc, cancellationToken))
{
throw CancellationHelper.CreateOperationCanceledException(exc, cancellationToken);
}
finally
{
ctr.Dispose();
}
// If cancellation is requested and tears down the connection, it could cause the copy
// to end early but think it ended successfully. So we prioritize cancellation in this
// race condition, and if we find after the copy has completed that cancellation has
// been requested, we assume the copy completed due to cancellation and throw.
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
Finish();
}
private void Finish()
{
// We cannot reuse this connection, so close it.
_connection.Dispose();
_connection = null;
}
}
}
}
|