Skip to content

Commit b8aefa4

Browse files
committed
Made handler project compatible with .NET Standard 2.0
1 parent 26b04c7 commit b8aefa4

14 files changed

+145
-99
lines changed

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ChunkedEncodingReadStream.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,46 @@ private sealed class ChunkedEncodingReadStream : HttpContentReadStream
3030

3131
public ChunkedEncodingReadStream(HttpConnection connection) : base(connection) { }
3232

33-
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
33+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
34+
{
35+
ValidateBufferArgs(buffer, offset, count);
36+
return ReadAsyncInternal(new Memory<byte>(buffer, offset, count), cancellationToken);
37+
}
38+
39+
private Task<int> ReadAsyncInternal(Memory<byte> buffer, CancellationToken cancellationToken)
3440
{
3541
if (cancellationToken.IsCancellationRequested)
3642
{
3743
// Cancellation requested.
38-
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
44+
return Task.FromCanceled<int>(cancellationToken);
3945
}
4046

4147
if (_connection == null || buffer.Length == 0)
4248
{
4349
// Response body fully consumed or the caller didn't ask for any data.
44-
return new ValueTask<int>(0);
50+
return Task.FromResult(0);
4551
}
4652

4753
// Try to consume from data we already have in the buffer.
48-
int bytesRead = ReadChunksFromConnectionBuffer(buffer.Span, cancellationRegistration: default);
54+
int bytesRead = ReadChunksFromConnectionBuffer(buffer.Span, cancellationRegistration: default, cancellationToken);
4955
if (bytesRead > 0)
5056
{
51-
return new ValueTask<int>(bytesRead);
57+
return Task.FromResult(bytesRead);
5258
}
5359

5460
// We may have just consumed the remainder of the response (with no actual data
5561
// available), so check again.
5662
if (_connection == null)
5763
{
5864
Debug.Assert(_state == ParsingState.Done);
59-
return new ValueTask<int>(0);
65+
return Task.FromResult(0);
6066
}
6167

6268
// Nothing available to consume. Fall back to I/O.
6369
return ReadAsyncCore(buffer, cancellationToken);
6470
}
6571

66-
private async ValueTask<int> ReadAsyncCore(Memory<byte> buffer, CancellationToken cancellationToken)
72+
private async Task<int> ReadAsyncCore(Memory<byte> buffer, CancellationToken cancellationToken)
6773
{
6874
// Should only be called if ReadChunksFromConnectionBuffer returned 0.
6975

@@ -108,7 +114,7 @@ private async ValueTask<int> ReadAsyncCore(Memory<byte> buffer, CancellationToke
108114

109115
// Now that we have more, see if we can get any response data, and if
110116
// we can we're done.
111-
int bytesCopied = ReadChunksFromConnectionBuffer(buffer.Span, ctr);
117+
int bytesCopied = ReadChunksFromConnectionBuffer(buffer.Span, ctr, cancellationToken);
112118
if (bytesCopied > 0)
113119
{
114120
return bytesCopied;
@@ -144,7 +150,7 @@ private async Task CopyToAsyncCore(Stream destination, CancellationToken cancell
144150
{
145151
while (true)
146152
{
147-
ReadOnlyMemory<byte> bytesRead = ReadChunkFromConnectionBuffer(int.MaxValue, ctr);
153+
ReadOnlyMemory<byte> bytesRead = ReadChunkFromConnectionBuffer(int.MaxValue, ctr, cancellationToken);
148154
if (bytesRead.Length == 0)
149155
{
150156
break;
@@ -171,12 +177,12 @@ private async Task CopyToAsyncCore(Stream destination, CancellationToken cancell
171177
}
172178
}
173179

174-
private int ReadChunksFromConnectionBuffer(Span<byte> buffer, CancellationTokenRegistration cancellationRegistration)
180+
private int ReadChunksFromConnectionBuffer(Span<byte> buffer, CancellationTokenRegistration cancellationRegistration, CancellationToken cancellationToken)
175181
{
176182
int totalBytesRead = 0;
177183
while (buffer.Length > 0)
178184
{
179-
ReadOnlyMemory<byte> bytesRead = ReadChunkFromConnectionBuffer(buffer.Length, cancellationRegistration);
185+
ReadOnlyMemory<byte> bytesRead = ReadChunkFromConnectionBuffer(buffer.Length, cancellationRegistration, cancellationToken);
180186
Debug.Assert(bytesRead.Length <= buffer.Length);
181187
if (bytesRead.Length == 0)
182188
{
@@ -190,7 +196,7 @@ private int ReadChunksFromConnectionBuffer(Span<byte> buffer, CancellationTokenR
190196
return totalBytesRead;
191197
}
192198

193-
private ReadOnlyMemory<byte> ReadChunkFromConnectionBuffer(int maxBytesToRead, CancellationTokenRegistration cancellationRegistration)
199+
private ReadOnlyMemory<byte> ReadChunkFromConnectionBuffer(int maxBytesToRead, CancellationTokenRegistration cancellationRegistration, CancellationToken cancellationToken = default)
194200
{
195201
Debug.Assert(maxBytesToRead > 0);
196202

@@ -295,7 +301,7 @@ private ReadOnlyMemory<byte> ReadChunkFromConnectionBuffer(int maxBytesToRead, C
295301
// (e.g. if a timer is used and has already queued its callback but the
296302
// callback hasn't yet run).
297303
cancellationRegistration.Dispose();
298-
CancellationHelper.ThrowIfCancellationRequested(cancellationRegistration.Token);
304+
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
299305

300306
_state = ParsingState.Done;
301307
_connection.CompleteResponse();

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ChunkedEncodingWriteStream.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ public ChunkedEncodingWriteStream(HttpConnection connection) : base(connection)
1818
{
1919
}
2020

21-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken ignored)
21+
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ignored)
22+
{
23+
return WriteAsyncInternal(new ReadOnlyMemory<byte>(buffer, offset, count), ignored);
24+
}
25+
26+
private Task WriteAsyncInternal(ReadOnlyMemory<byte> buffer, CancellationToken ignored)
2227
{
2328
Debug.Assert(_connection._currentRequest != null);
2429

2530
// The token is ignored because it's coming from SendAsync and the only operations
2631
// here are those that are already covered by the token having been registered with
2732
// to close the connection.
2833

29-
ValueTask task = buffer.Length == 0 ?
34+
Task task = buffer.Length == 0 ?
3035
// Don't write if nothing was given, especially since we don't want to accidentally send a 0 chunk,
3136
// which would indicate end of body. Instead, just ensure no content is stuck in the buffer.
3237
_connection.FlushAsync() :
33-
new ValueTask(WriteChunkAsync(buffer));
38+
WriteChunkAsync(buffer);
3439

3540
return task;
3641
}

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ConnectHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public CertificateCallbackMapper(Func<HttpRequestMessage, X509Certificate2, X509
3737
}
3838
}
3939

40-
public static async ValueTask<(Socket, Stream)> ConnectAsync(string host, int port, CancellationToken cancellationToken)
40+
public static async Task<(Socket, Stream)> ConnectAsync(string host, int port, CancellationToken cancellationToken)
4141
{
4242
// Rather than creating a new Socket and calling ConnectAsync on it, we use the static
4343
// Socket.ConnectAsync with a SocketAsyncEventArgs, as we can then use Socket.CancelConnectAsync
@@ -139,7 +139,7 @@ protected override void OnCompleted(SocketAsyncEventArgs _)
139139
}
140140
}
141141

142-
public static ValueTask<SslStream> EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Stream stream, CancellationToken cancellationToken)
142+
public static Task<SslStream> EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Stream stream, CancellationToken cancellationToken)
143143
{
144144
// If there's a cert validation callback, and if it came from HttpClientHandler,
145145
// wrap the original delegate in order to change the sender to be the request message (expected by HttpClientHandler's delegate).
@@ -157,7 +157,7 @@ public static ValueTask<SslStream> EstablishSslConnectionAsync(SslClientAuthenti
157157
return EstablishSslConnectionAsyncCore(stream, sslOptions, cancellationToken);
158158
}
159159

160-
private static async ValueTask<SslStream> EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
160+
private static async Task<SslStream> EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
161161
{
162162
SslStream sslStream = new SslStream(stream, false, sslOptions.RemoteCertificateValidationCallback, sslOptions.LocalCertificateSelectionCallback, sslOptions.EncryptionPolicy);
163163

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ConnectionCloseReadStream.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ public ConnectionCloseReadStream(HttpConnection connection) : base(connection)
1616
{
1717
}
1818

19-
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
19+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
20+
{
21+
ValidateBufferArgs(buffer, offset, count);
22+
return ReadAsyncInternal(new Memory<byte>(buffer, offset, count), cancellationToken);
23+
}
24+
25+
private async Task<int> ReadAsyncInternal(Memory<byte> buffer, CancellationToken cancellationToken)
2026
{
2127
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
2228

@@ -26,9 +32,9 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
2632
return 0;
2733
}
2834

29-
ValueTask<int> readTask = _connection.ReadAsync(buffer);
35+
Task<int> readTask = _connection.ReadAsync(buffer);
3036
int bytesRead;
31-
if (readTask.IsCompletedSuccessfully)
37+
if (readTask.IsCompletedSuccessfully())
3238
{
3339
bytesRead = readTask.Result;
3440
}

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ContentLengthReadStream.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public ContentLengthReadStream(HttpConnection connection, ulong contentLength) :
2121
_contentBytesRemaining = contentLength;
2222
}
2323

24-
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
24+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
25+
{
26+
ValidateBufferArgs(buffer, offset, count);
27+
return ReadAsyncInternal(new Memory<byte>(buffer, offset, count), cancellationToken);
28+
}
29+
30+
private async Task<int> ReadAsyncInternal(Memory<byte> buffer, CancellationToken cancellationToken)
2531
{
2632
CancellationHelper.ThrowIfCancellationRequested(cancellationToken);
2733

@@ -38,9 +44,9 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
3844
buffer = buffer.Slice(0, (int)_contentBytesRemaining);
3945
}
4046

41-
ValueTask<int> readTask = _connection.ReadAsync(buffer);
47+
Task<int> readTask = _connection.ReadAsync(buffer);
4248
int bytesRead;
43-
if (readTask.IsCompletedSuccessfully)
49+
if (readTask.IsCompletedSuccessfully())
4450
{
4551
bytesRead = readTask.Result;
4652
}
@@ -198,7 +204,10 @@ public override async Task<bool> DrainAsync(int maxDrainBytes)
198204
// (e.g. if a timer is used and has already queued its callback but the
199205
// callback hasn't yet run).
200206
ctr.Dispose();
201-
CancellationHelper.ThrowIfCancellationRequested(ctr.Token);
207+
if (cts != null)
208+
{
209+
CancellationHelper.ThrowIfCancellationRequested(cts.Token);
210+
}
202211

203212
Finish();
204213
return true;

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/ContentLengthWriteStream.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ public ContentLengthWriteStream(HttpConnection connection) : base(connection)
1616
{
1717
}
1818

19-
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken ignored) // token ignored as it comes from SendAsync
19+
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ignored) // token ignored as it comes from SendAsync
20+
{
21+
return WriteAsyncInternal(new ReadOnlyMemory<byte>(buffer, offset, count), ignored);
22+
}
23+
24+
private Task WriteAsyncInternal(ReadOnlyMemory<byte> buffer, CancellationToken ignored) // token ignored as it comes from SendAsync
2025
{
2126
Debug.Assert(_connection._currentRequest != null);
2227

2328
// Have the connection write the data, skipping the buffer. Importantly, this will
2429
// force a flush of anything already in the buffer, i.e. any remaining request headers
2530
// that are still buffered.
26-
return new ValueTask(_connection.WriteAsync(buffer));
31+
return _connection.WriteAsync(buffer);
2732
}
2833

2934
public override Task FinishAsync()

StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/EmptyReadStream.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public override void Close() { /* nop */ }
2222

2323
public override int ReadByte() => -1;
2424

25-
public override int Read(Span<byte> buffer) => 0;
26-
27-
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) =>
28-
cancellationToken.IsCancellationRequested ? new ValueTask<int>(Task.FromCanceled<int>(cancellationToken)) :
29-
new ValueTask<int>(0);
25+
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
26+
{
27+
ValidateBufferArgs(buffer, offset, count);
28+
return cancellationToken.IsCancellationRequested? Task.FromCanceled<int>(cancellationToken) : Task.FromResult<int>(0);
29+
}
3030
}
3131
}
3232
}

0 commit comments

Comments
 (0)