| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Net.Http.Headers; |
| | | 10 | | using System.Runtime.ExceptionServices; |
| | | 11 | | using System.Text; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | |
| | | 15 | | namespace System.Net.Http |
| | | 16 | | { |
| | | 17 | | public abstract class HttpContent : IDisposable |
| | | 18 | | { |
| | | 19 | | private HttpContentHeaders? _headers; |
| | | 20 | | private LimitArrayPoolWriteStream? _bufferedContent; |
| | | 21 | | private object? _contentReadStream; // Stream or Task<Stream> |
| | | 22 | | private bool _disposed; |
| | | 23 | | private bool _canCalculateLength; |
| | | 24 | | |
| | | 25 | | internal const int MaxBufferSize = int.MaxValue; |
| | 1 | 26 | | internal static readonly Encoding DefaultStringEncoding = Encoding.UTF8; |
| | | 27 | | |
| | | 28 | | private const int UTF8CodePage = 65001; |
| | | 29 | | private const int UTF32CodePage = 12000; |
| | | 30 | | private const int UnicodeCodePage = 1200; |
| | | 31 | | private const int BigEndianUnicodeCodePage = 1201; |
| | | 32 | | |
| | 1 | 33 | | private static ReadOnlySpan<byte> UTF8Preamble => [0xEF, 0xBB, 0xBF]; |
| | 1 | 34 | | private static ReadOnlySpan<byte> UTF32Preamble => [0xFF, 0xFE, 0x00, 0x00]; |
| | 1 | 35 | | private static ReadOnlySpan<byte> UnicodePreamble => [0xFF, 0xFE]; |
| | 1 | 36 | | private static ReadOnlySpan<byte> BigEndianUnicodePreamble => [0xFE, 0xFF]; |
| | | 37 | | |
| | | 38 | | #if DEBUG |
| | | 39 | | static HttpContent() |
| | 1 | 40 | | { |
| | | 41 | | // Ensure the encoding constants used in this class match the actual data from the Encoding class |
| | 1 | 42 | | AssertEncodingConstants(Encoding.UTF8, UTF8CodePage, UTF8Preamble); |
| | | 43 | | |
| | | 44 | | // UTF32 not supported on Phone |
| | 1 | 45 | | AssertEncodingConstants(Encoding.UTF32, UTF32CodePage, UTF32Preamble); |
| | | 46 | | |
| | 1 | 47 | | AssertEncodingConstants(Encoding.Unicode, UnicodeCodePage, UnicodePreamble); |
| | | 48 | | |
| | 1 | 49 | | AssertEncodingConstants(Encoding.BigEndianUnicode, BigEndianUnicodeCodePage, BigEndianUnicodePreamble); |
| | 1 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static void AssertEncodingConstants(Encoding encoding, int codePage, ReadOnlySpan<byte> preamble) |
| | 4 | 53 | | { |
| | 4 | 54 | | Debug.Assert(encoding != null); |
| | | 55 | | |
| | 4 | 56 | | Debug.Assert(codePage == encoding.CodePage, |
| | 4 | 57 | | $"Encoding code page mismatch for encoding: {encoding.EncodingName}", |
| | 4 | 58 | | $"Expected (constant): {codePage}, Actual (Encoding.CodePage): {encoding.CodePage}"); |
| | | 59 | | |
| | 4 | 60 | | byte[] actualPreamble = encoding.GetPreamble(); |
| | | 61 | | |
| | 4 | 62 | | Debug.Assert(preamble.SequenceEqual(actualPreamble), |
| | 4 | 63 | | $"Encoding preamble mismatch for encoding: {encoding.EncodingName}", |
| | 4 | 64 | | $"Expected (constant): {BitConverter.ToString(preamble.ToArray())}, Actual (Encoding.GetPreamble()): {Bi |
| | 4 | 65 | | } |
| | | 66 | | #endif |
| | | 67 | | |
| | 1 | 68 | | public HttpContentHeaders Headers => _headers ??= new HttpContentHeaders(this); |
| | | 69 | | |
| | | 70 | | internal void SetHeaders(HttpContentHeaders headers) |
| | 0 | 71 | | { |
| | 0 | 72 | | Debug.Assert(_headers is null); |
| | 0 | 73 | | Debug.Assert(headers is not null); |
| | | 74 | | |
| | 0 | 75 | | headers._parent = this; |
| | 0 | 76 | | _headers = headers; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | [MemberNotNullWhen(true, nameof(_bufferedContent))] |
| | 0 | 80 | | private bool IsBuffered => _bufferedContent is not null; |
| | | 81 | | |
| | 1 | 82 | | protected HttpContent() |
| | 1 | 83 | | { |
| | | 84 | | // Log to get an ID for the current content. This ID is used when the content gets associated to a message. |
| | 1 | 85 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this); |
| | | 86 | | |
| | | 87 | | // We start with the assumption that we can calculate the content length. |
| | 1 | 88 | | _canCalculateLength = true; |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | private MemoryStream CreateMemoryStreamFromBufferedContent() |
| | 0 | 92 | | { |
| | 0 | 93 | | Debug.Assert(IsBuffered); |
| | 0 | 94 | | return new MemoryStream(_bufferedContent.GetSingleBuffer(), 0, (int)_bufferedContent.Length, writable: false |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public Task<string> ReadAsStringAsync() => |
| | 0 | 98 | | ReadAsStringAsync(CancellationToken.None); |
| | | 99 | | |
| | | 100 | | public Task<string> ReadAsStringAsync(CancellationToken cancellationToken) |
| | 0 | 101 | | { |
| | 0 | 102 | | CheckDisposed(); |
| | 0 | 103 | | return WaitAndReturnAsync(LoadIntoBufferAsync(cancellationToken), this, static s => s.ReadBufferedContentAsS |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | private string ReadBufferedContentAsString() |
| | 0 | 107 | | { |
| | 0 | 108 | | Debug.Assert(IsBuffered); |
| | | 109 | | |
| | 0 | 110 | | return ReadBufferAsString(_bufferedContent, Headers); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | internal static string ReadBufferAsString(LimitArrayPoolWriteStream stream, HttpContentHeaders headers) |
| | 0 | 114 | | { |
| | 0 | 115 | | if (stream.Length == 0) |
| | 0 | 116 | | { |
| | 0 | 117 | | return string.Empty; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // We don't validate the Content-Encoding header: If the content was encoded, it's the caller's |
| | | 121 | | // responsibility to make sure to only call ReadAsString() on already decoded content. E.g. if the |
| | | 122 | | // Content-Encoding is 'gzip' the user should set HttpClientHandler.AutomaticDecompression to get a |
| | | 123 | | // decoded response stream. |
| | | 124 | | |
| | 0 | 125 | | ReadOnlySpan<byte> firstBuffer = stream.GetFirstBuffer(); |
| | 0 | 126 | | Debug.Assert(firstBuffer.Length >= 4 || firstBuffer.Length == stream.Length); |
| | | 127 | | |
| | 0 | 128 | | Encoding? encoding = null; |
| | 0 | 129 | | int bomLength = -1; |
| | | 130 | | |
| | 0 | 131 | | string? charset = headers.ContentType?.CharSet; |
| | | 132 | | |
| | | 133 | | // If we do have encoding information in the 'Content-Type' header, use that information to convert |
| | | 134 | | // the content to a string. |
| | 0 | 135 | | if (charset != null) |
| | 0 | 136 | | { |
| | | 137 | | try |
| | 0 | 138 | | { |
| | | 139 | | // Remove at most a single set of quotes. |
| | 0 | 140 | | if (charset.Length > 2 && |
| | 0 | 141 | | charset.StartsWith('\"') && |
| | 0 | 142 | | charset.EndsWith('\"')) |
| | 0 | 143 | | { |
| | 0 | 144 | | encoding = Encoding.GetEncoding(charset.Substring(1, charset.Length - 2)); |
| | 0 | 145 | | } |
| | | 146 | | else |
| | 0 | 147 | | { |
| | 0 | 148 | | encoding = Encoding.GetEncoding(charset); |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | // Byte-order-mark (BOM) characters may be present even if a charset was specified. |
| | 0 | 152 | | bomLength = GetPreambleLength(firstBuffer, encoding); |
| | 0 | 153 | | } |
| | 0 | 154 | | catch (ArgumentException e) |
| | 0 | 155 | | { |
| | 0 | 156 | | throw new InvalidOperationException(SR.net_http_content_invalid_charset, e); |
| | | 157 | | } |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | // If no content encoding is listed in the ContentType HTTP header, or no Content-Type header present, |
| | | 161 | | // then check for a BOM in the data to figure out the encoding. |
| | 0 | 162 | | if (encoding == null) |
| | 0 | 163 | | { |
| | 0 | 164 | | if (!TryDetectEncoding(firstBuffer, out encoding, out bomLength)) |
| | 0 | 165 | | { |
| | | 166 | | // Use the default encoding (UTF8) if we couldn't detect one. |
| | 0 | 167 | | encoding = DefaultStringEncoding; |
| | | 168 | | |
| | | 169 | | // We already checked to see if the data had a UTF8 BOM in TryDetectEncoding |
| | | 170 | | // and DefaultStringEncoding is UTF8, so the bomLength is 0. |
| | 0 | 171 | | bomLength = 0; |
| | 0 | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | // Drop the BOM when decoding the data. |
| | | 176 | | |
| | 0 | 177 | | if (firstBuffer.Length == stream.Length) |
| | 0 | 178 | | { |
| | 0 | 179 | | return encoding.GetString(firstBuffer[bomLength..]); |
| | | 180 | | } |
| | | 181 | | else |
| | 0 | 182 | | { |
| | 0 | 183 | | byte[] buffer = ArrayPool<byte>.Shared.Rent((int)stream.Length); |
| | 0 | 184 | | stream.CopyToCore(buffer); |
| | | 185 | | |
| | 0 | 186 | | string result = encoding.GetString(buffer.AsSpan(0, (int)stream.Length)[bomLength..]); |
| | | 187 | | |
| | 0 | 188 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 0 | 189 | | return result; |
| | | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | public Task<byte[]> ReadAsByteArrayAsync() => |
| | 0 | 194 | | ReadAsByteArrayAsync(CancellationToken.None); |
| | | 195 | | |
| | | 196 | | public Task<byte[]> ReadAsByteArrayAsync(CancellationToken cancellationToken) |
| | 0 | 197 | | { |
| | 0 | 198 | | CheckDisposed(); |
| | 0 | 199 | | return WaitAndReturnAsync(LoadIntoBufferAsync(cancellationToken), this, static s => s.ReadBufferedContentAsB |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | internal byte[] ReadBufferedContentAsByteArray() |
| | 0 | 203 | | { |
| | 0 | 204 | | Debug.Assert(_bufferedContent != null); |
| | | 205 | | // The returned array is exposed out of the library, so use CreateCopy rather than GetSingleBuffer. |
| | 0 | 206 | | return _bufferedContent.CreateCopy(); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public Stream ReadAsStream() => |
| | 0 | 210 | | ReadAsStream(CancellationToken.None); |
| | | 211 | | |
| | | 212 | | public Stream ReadAsStream(CancellationToken cancellationToken) |
| | 0 | 213 | | { |
| | 0 | 214 | | CheckDisposed(); |
| | | 215 | | |
| | | 216 | | // _contentReadStream will be either null (nothing yet initialized), a Stream (it was previously |
| | | 217 | | // initialized in TryReadAsStream/ReadAsStream), or a Task<Stream> (it was previously initialized |
| | | 218 | | // in ReadAsStreamAsync). |
| | | 219 | | |
| | 0 | 220 | | if (_contentReadStream == null) // don't yet have a Stream |
| | 0 | 221 | | { |
| | 0 | 222 | | Stream s = IsBuffered ? |
| | 0 | 223 | | CreateMemoryStreamFromBufferedContent() : |
| | 0 | 224 | | CreateContentReadStream(cancellationToken); |
| | 0 | 225 | | _contentReadStream = s; |
| | 0 | 226 | | return s; |
| | | 227 | | } |
| | 0 | 228 | | else if (_contentReadStream is Stream stream) // have a Stream |
| | 0 | 229 | | { |
| | 0 | 230 | | return stream; |
| | | 231 | | } |
| | | 232 | | else // have a Task<Stream> |
| | 0 | 233 | | { |
| | | 234 | | // Throw if ReadAsStreamAsync has been called previously since _contentReadStream contains a cached task |
| | 0 | 235 | | throw new HttpRequestException(SR.net_http_content_read_as_stream_has_task); |
| | | 236 | | } |
| | 0 | 237 | | } |
| | | 238 | | |
| | | 239 | | public Task<Stream> ReadAsStreamAsync() => |
| | 0 | 240 | | ReadAsStreamAsync(CancellationToken.None); |
| | | 241 | | |
| | | 242 | | public Task<Stream> ReadAsStreamAsync(CancellationToken cancellationToken) |
| | 0 | 243 | | { |
| | 0 | 244 | | CheckDisposed(); |
| | | 245 | | |
| | | 246 | | // _contentReadStream will be either null (nothing yet initialized), a Stream (it was previously |
| | | 247 | | // initialized in TryReadAsStream/ReadAsStream), or a Task<Stream> (it was previously initialized here |
| | | 248 | | // in ReadAsStreamAsync). |
| | | 249 | | |
| | 0 | 250 | | if (_contentReadStream == null) // don't yet have a Stream |
| | 0 | 251 | | { |
| | 0 | 252 | | Task<Stream> t = IsBuffered ? |
| | 0 | 253 | | Task.FromResult<Stream>(CreateMemoryStreamFromBufferedContent()) : |
| | 0 | 254 | | CreateContentReadStreamAsync(cancellationToken); |
| | 0 | 255 | | _contentReadStream = t; |
| | 0 | 256 | | return t; |
| | | 257 | | } |
| | 0 | 258 | | else if (_contentReadStream is Task<Stream> t) // have a Task<Stream> |
| | 0 | 259 | | { |
| | 0 | 260 | | return t; |
| | | 261 | | } |
| | | 262 | | else |
| | 0 | 263 | | { |
| | 0 | 264 | | Debug.Assert(_contentReadStream is Stream, $"Expected a Stream, got ${_contentReadStream}"); |
| | 0 | 265 | | Task<Stream> ts = Task.FromResult((Stream)_contentReadStream); |
| | 0 | 266 | | _contentReadStream = ts; |
| | 0 | 267 | | return ts; |
| | | 268 | | } |
| | 0 | 269 | | } |
| | | 270 | | |
| | | 271 | | internal Stream? TryReadAsStream() |
| | 0 | 272 | | { |
| | 0 | 273 | | CheckDisposed(); |
| | | 274 | | |
| | | 275 | | // _contentReadStream will be either null (nothing yet initialized), a Stream (it was previously |
| | | 276 | | // initialized in TryReadAsStream/ReadAsStream), or a Task<Stream> (it was previously initialized here |
| | | 277 | | // in ReadAsStreamAsync). |
| | | 278 | | |
| | 0 | 279 | | if (_contentReadStream == null) // don't yet have a Stream |
| | 0 | 280 | | { |
| | 0 | 281 | | Stream? s = IsBuffered ? |
| | 0 | 282 | | CreateMemoryStreamFromBufferedContent() : |
| | 0 | 283 | | TryCreateContentReadStream(); |
| | 0 | 284 | | _contentReadStream = s; |
| | 0 | 285 | | return s; |
| | | 286 | | } |
| | 0 | 287 | | else if (_contentReadStream is Stream s) // have a Stream |
| | 0 | 288 | | { |
| | 0 | 289 | | return s; |
| | | 290 | | } |
| | | 291 | | else // have a Task<Stream> |
| | 0 | 292 | | { |
| | 0 | 293 | | Debug.Assert(_contentReadStream is Task<Stream>, $"Expected a Task<Stream>, got ${_contentReadStream}"); |
| | 0 | 294 | | Task<Stream> t = (Task<Stream>)_contentReadStream; |
| | 0 | 295 | | return t.Status == TaskStatus.RanToCompletion ? t.Result : null; |
| | | 296 | | } |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | protected abstract Task SerializeToStreamAsync(Stream stream, TransportContext? context); |
| | | 300 | | |
| | | 301 | | // We cannot add abstract member to a public class in order to not to break already established contract of this |
| | | 302 | | // So we add virtual method, override it everywhere internally and provide proper implementation. |
| | | 303 | | // Unfortunately we cannot force everyone to implement so in such case we throw NSE. |
| | | 304 | | protected virtual void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellatio |
| | 0 | 305 | | { |
| | 0 | 306 | | throw new NotSupportedException(SR.Format(SR.net_http_missing_sync_implementation, GetType(), nameof(HttpCon |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | protected virtual Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancel |
| | 0 | 310 | | SerializeToStreamAsync(stream, context); |
| | | 311 | | |
| | | 312 | | // TODO https://github.com/dotnet/runtime/issues/31316: Expose something to enable this publicly. For very spec |
| | | 313 | | // HTTP/2 scenarios (e.g. gRPC), we need to be able to allow request content to continue sending after SendAsync |
| | | 314 | | // completed, which goes against the previous design of content, and which means that with some servers, even ou |
| | | 315 | | // of desired scenarios we could end up unexpectedly having request content still sending even after the respons |
| | | 316 | | // completes, which could lead to spurious failures in unsuspecting client code. To mitigate that, we prohibit |
| | | 317 | | // on all known HttpContent types, waiting for the request content to complete before completing the SendAsync t |
| | 0 | 318 | | internal virtual bool AllowDuplex => true; |
| | | 319 | | |
| | | 320 | | public void CopyTo(Stream stream, TransportContext? context, CancellationToken cancellationToken) |
| | 0 | 321 | | { |
| | 0 | 322 | | CheckDisposed(); |
| | 0 | 323 | | ArgumentNullException.ThrowIfNull(stream); |
| | | 324 | | try |
| | 0 | 325 | | { |
| | 0 | 326 | | if (IsBuffered) |
| | 0 | 327 | | { |
| | 0 | 328 | | stream.Write(_bufferedContent.GetSingleBuffer(), 0, (int)_bufferedContent.Length); |
| | 0 | 329 | | } |
| | | 330 | | else |
| | 0 | 331 | | { |
| | 0 | 332 | | SerializeToStream(stream, context, cancellationToken); |
| | 0 | 333 | | } |
| | 0 | 334 | | } |
| | 0 | 335 | | catch (Exception e) when (StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 336 | | { |
| | 0 | 337 | | throw GetStreamCopyException(e); |
| | | 338 | | } |
| | 0 | 339 | | } |
| | | 340 | | |
| | | 341 | | public Task CopyToAsync(Stream stream) => |
| | 0 | 342 | | CopyToAsync(stream, CancellationToken.None); |
| | | 343 | | |
| | | 344 | | public Task CopyToAsync(Stream stream, CancellationToken cancellationToken) => |
| | 0 | 345 | | CopyToAsync(stream, null, cancellationToken); |
| | | 346 | | |
| | | 347 | | public Task CopyToAsync(Stream stream, TransportContext? context) => |
| | 0 | 348 | | CopyToAsync(stream, context, CancellationToken.None); |
| | | 349 | | |
| | | 350 | | public Task CopyToAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken) |
| | 0 | 351 | | { |
| | 0 | 352 | | CheckDisposed(); |
| | 0 | 353 | | ArgumentNullException.ThrowIfNull(stream); |
| | | 354 | | try |
| | 0 | 355 | | { |
| | 0 | 356 | | return WaitAsync(InternalCopyToAsync(stream, context, cancellationToken)); |
| | | 357 | | } |
| | 0 | 358 | | catch (Exception e) when (StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 359 | | { |
| | 0 | 360 | | return Task.FromException(GetStreamCopyException(e)); |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | static async Task WaitAsync(ValueTask copyTask) |
| | 0 | 364 | | { |
| | | 365 | | try |
| | 0 | 366 | | { |
| | 0 | 367 | | await copyTask.ConfigureAwait(false); |
| | 0 | 368 | | } |
| | 0 | 369 | | catch (Exception e) when (StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 370 | | { |
| | 0 | 371 | | throw WrapStreamCopyException(e); |
| | | 372 | | } |
| | 0 | 373 | | } |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | internal ValueTask InternalCopyToAsync(Stream stream, TransportContext? context, CancellationToken cancellationT |
| | 0 | 377 | | { |
| | 0 | 378 | | if (IsBuffered) |
| | 0 | 379 | | { |
| | 0 | 380 | | return stream.WriteAsync(_bufferedContent.GetSingleBuffer().AsMemory(0, (int)_bufferedContent.Length), c |
| | | 381 | | } |
| | | 382 | | |
| | 0 | 383 | | Task task = SerializeToStreamAsync(stream, context, cancellationToken); |
| | 0 | 384 | | CheckTaskNotNull(task); |
| | 0 | 385 | | return new ValueTask(task); |
| | 0 | 386 | | } |
| | | 387 | | |
| | | 388 | | internal void LoadIntoBuffer(long maxBufferSize, CancellationToken cancellationToken) |
| | 0 | 389 | | { |
| | 0 | 390 | | CheckDisposed(); |
| | | 391 | | |
| | 0 | 392 | | if (!CreateTemporaryBuffer(maxBufferSize, out LimitArrayPoolWriteStream? tempBuffer, out Exception? error)) |
| | 0 | 393 | | { |
| | | 394 | | // If we already buffered the content, just return. |
| | 0 | 395 | | return; |
| | | 396 | | } |
| | | 397 | | |
| | 0 | 398 | | if (tempBuffer == null) |
| | 0 | 399 | | { |
| | 0 | 400 | | throw error!; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | // Register for cancellation and tear down the underlying stream in case of cancellation/timeout. |
| | | 404 | | // We're only comfortable disposing of the HttpContent instance like this because LoadIntoBuffer is internal |
| | | 405 | | // we're only using it on content instances we get back from a handler's Send call that haven't been given o |
| | | 406 | | // If we were to ever make LoadIntoBuffer public, we'd need to rethink this. |
| | 0 | 407 | | CancellationTokenRegistration cancellationRegistration = cancellationToken.Register(static s => ((HttpConten |
| | | 408 | | |
| | | 409 | | try |
| | 0 | 410 | | { |
| | 0 | 411 | | SerializeToStream(tempBuffer, null, cancellationToken); |
| | 0 | 412 | | } |
| | 0 | 413 | | catch (Exception e) |
| | 0 | 414 | | { |
| | 0 | 415 | | tempBuffer.Dispose(); |
| | | 416 | | |
| | 0 | 417 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, e); |
| | | 418 | | |
| | 0 | 419 | | if (CancellationHelper.ShouldWrapInOperationCanceledException(e, cancellationToken)) |
| | 0 | 420 | | { |
| | 0 | 421 | | throw CancellationHelper.CreateOperationCanceledException(e, cancellationToken); |
| | | 422 | | } |
| | | 423 | | |
| | 0 | 424 | | if (StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 425 | | { |
| | 0 | 426 | | throw GetStreamCopyException(e); |
| | | 427 | | } |
| | | 428 | | |
| | 0 | 429 | | throw; |
| | | 430 | | } |
| | | 431 | | finally |
| | 0 | 432 | | { |
| | | 433 | | // Clean up the cancellation registration. |
| | 0 | 434 | | cancellationRegistration.Dispose(); |
| | 0 | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | tempBuffer.ReallocateIfPooled(); |
| | 0 | 438 | | _bufferedContent = tempBuffer; |
| | 0 | 439 | | } |
| | | 440 | | |
| | | 441 | | public Task LoadIntoBufferAsync() => |
| | 0 | 442 | | LoadIntoBufferAsync(MaxBufferSize); |
| | | 443 | | |
| | | 444 | | // No "CancellationToken" parameter needed since canceling the CTS will close the connection, resulting |
| | | 445 | | // in an exception being thrown while we're buffering. |
| | | 446 | | // If buffering is used without a connection, it is supposed to be fast, thus no cancellation required. |
| | | 447 | | public Task LoadIntoBufferAsync(long maxBufferSize) => |
| | 0 | 448 | | LoadIntoBufferAsync(maxBufferSize, CancellationToken.None); |
| | | 449 | | |
| | | 450 | | /// <summary> |
| | | 451 | | /// Serialize the HTTP content to a memory buffer as an asynchronous operation. |
| | | 452 | | /// </summary> |
| | | 453 | | /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> |
| | | 454 | | /// <returns>The task object representing the asynchronous operation.</returns> |
| | | 455 | | /// <remarks> |
| | | 456 | | /// This operation will not block. The returned <see cref="Task"/> object will complete after all of the content |
| | | 457 | | /// After content is serialized to a memory buffer, calls to one of the <see cref="CopyToAsync(Stream)"/> method |
| | | 458 | | /// </remarks> |
| | | 459 | | /// <exception cref="OperationCanceledException">The cancellation token was canceled. This exception is stored i |
| | | 460 | | /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception> |
| | | 461 | | public Task LoadIntoBufferAsync(CancellationToken cancellationToken) => |
| | 0 | 462 | | LoadIntoBufferAsync(MaxBufferSize, cancellationToken); |
| | | 463 | | |
| | | 464 | | /// <summary> |
| | | 465 | | /// Serialize the HTTP content to a memory buffer as an asynchronous operation. |
| | | 466 | | /// </summary> |
| | | 467 | | /// <param name="maxBufferSize">The maximum size, in bytes, of the buffer to use.</param> |
| | | 468 | | /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> |
| | | 469 | | /// <returns>The task object representing the asynchronous operation.</returns> |
| | | 470 | | /// <remarks> |
| | | 471 | | /// This operation will not block. The returned <see cref="Task"/> object will complete after all of the content |
| | | 472 | | /// After content is serialized to a memory buffer, calls to one of the <see cref="CopyToAsync(Stream)"/> method |
| | | 473 | | /// </remarks> |
| | | 474 | | /// <exception cref="OperationCanceledException">The cancellation token was canceled. This exception is stored i |
| | | 475 | | /// <exception cref="ObjectDisposedException">The object has already been disposed.</exception> |
| | | 476 | | public Task LoadIntoBufferAsync(long maxBufferSize, CancellationToken cancellationToken) |
| | 0 | 477 | | { |
| | 0 | 478 | | CheckDisposed(); |
| | | 479 | | |
| | 0 | 480 | | if (!CreateTemporaryBuffer(maxBufferSize, out LimitArrayPoolWriteStream? tempBuffer, out Exception? error)) |
| | 0 | 481 | | { |
| | | 482 | | // If we already buffered the content, just return a completed task. |
| | 0 | 483 | | return Task.CompletedTask; |
| | | 484 | | } |
| | | 485 | | |
| | 0 | 486 | | if (tempBuffer == null) |
| | 0 | 487 | | { |
| | | 488 | | // We don't throw in LoadIntoBufferAsync(): return a faulted task. |
| | 0 | 489 | | return Task.FromException(error!); |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | try |
| | 0 | 493 | | { |
| | | 494 | | #pragma warning disable CA2025 |
| | 0 | 495 | | Task task = SerializeToStreamAsync(tempBuffer, null, cancellationToken); |
| | 0 | 496 | | CheckTaskNotNull(task); |
| | 0 | 497 | | return LoadIntoBufferAsyncCore(task, tempBuffer); |
| | | 498 | | #pragma warning restore |
| | | 499 | | } |
| | 0 | 500 | | catch (Exception e) |
| | 0 | 501 | | { |
| | 0 | 502 | | tempBuffer.Dispose(); |
| | | 503 | | |
| | 0 | 504 | | if (StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 505 | | { |
| | 0 | 506 | | return Task.FromException(GetStreamCopyException(e)); |
| | | 507 | | } |
| | | 508 | | |
| | | 509 | | // other synchronous exceptions from SerializeToStreamAsync/CheckTaskNotNull will propagate |
| | 0 | 510 | | throw; |
| | | 511 | | } |
| | 0 | 512 | | } |
| | | 513 | | |
| | | 514 | | private async Task LoadIntoBufferAsyncCore(Task serializeToStreamTask, LimitArrayPoolWriteStream tempBuffer) |
| | 0 | 515 | | { |
| | | 516 | | try |
| | 0 | 517 | | { |
| | 0 | 518 | | await serializeToStreamTask.ConfigureAwait(false); |
| | 0 | 519 | | } |
| | 0 | 520 | | catch (Exception e) |
| | 0 | 521 | | { |
| | 0 | 522 | | tempBuffer.Dispose(); // Cleanup partially filled stream. |
| | 0 | 523 | | Exception we = GetStreamCopyException(e); |
| | 0 | 524 | | if (we != e) throw we; |
| | 0 | 525 | | throw; |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | tempBuffer.ReallocateIfPooled(); |
| | 0 | 529 | | _bufferedContent = tempBuffer; |
| | 0 | 530 | | } |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// Serializes the HTTP content to a memory stream. |
| | | 534 | | /// </summary> |
| | | 535 | | /// <param name="cancellationToken">The cancellation token to cancel the operation.</param> |
| | | 536 | | /// <returns>The output memory stream which contains the serialized HTTP content.</returns> |
| | | 537 | | /// <remarks> |
| | | 538 | | /// Once the operation completes, the returned memory stream represents the HTTP content. The returned stream ca |
| | | 539 | | /// The <see cref="CreateContentReadStream(CancellationToken)"/> method buffers the content to a memory stream. |
| | | 540 | | /// Derived classes can override this behavior if there is a better way to retrieve the content as stream. |
| | | 541 | | /// For example, a byte array or a string could use a more efficient method way such as wrapping a read-only Mem |
| | | 542 | | /// </remarks> |
| | | 543 | | protected virtual Stream CreateContentReadStream(CancellationToken cancellationToken) |
| | 0 | 544 | | { |
| | 0 | 545 | | LoadIntoBuffer(MaxBufferSize, cancellationToken); |
| | 0 | 546 | | return CreateMemoryStreamFromBufferedContent(); |
| | 0 | 547 | | } |
| | | 548 | | |
| | | 549 | | protected virtual Task<Stream> CreateContentReadStreamAsync() |
| | 0 | 550 | | { |
| | | 551 | | // By default just buffer the content to a memory stream. Derived classes can override this behavior |
| | | 552 | | // if there is a better way to retrieve the content as stream (e.g. byte array/string use a more efficient |
| | | 553 | | // way, like wrapping a read-only MemoryStream around the bytes/string) |
| | 0 | 554 | | return WaitAndReturnAsync(LoadIntoBufferAsync(), this, static s => (Stream)s.CreateMemoryStreamFromBufferedC |
| | 0 | 555 | | } |
| | | 556 | | |
| | | 557 | | protected virtual Task<Stream> CreateContentReadStreamAsync(CancellationToken cancellationToken) |
| | 0 | 558 | | { |
| | | 559 | | // Drops the CT for compatibility reasons, see https://github.com/dotnet/runtime/issues/916#issuecomment-562 |
| | 0 | 560 | | return CreateContentReadStreamAsync(); |
| | 0 | 561 | | } |
| | | 562 | | |
| | | 563 | | // As an optimization for internal consumers of HttpContent (e.g. HttpClient.GetStreamAsync), and for |
| | | 564 | | // HttpContent-derived implementations that override CreateContentReadStreamAsync in a way that always |
| | | 565 | | // or frequently returns synchronously-completed tasks, we can avoid the task allocation by enabling |
| | | 566 | | // callers to try to get the Stream first synchronously. |
| | 0 | 567 | | internal virtual Stream? TryCreateContentReadStream() => null; |
| | | 568 | | |
| | | 569 | | // Derived types return true if they're able to compute the length. It's OK if derived types return false to |
| | | 570 | | // indicate that they're not able to compute the length. The transport channel needs to decide what to do in |
| | | 571 | | // that case (send chunked, buffer first, etc.). |
| | | 572 | | protected internal abstract bool TryComputeLength(out long length); |
| | | 573 | | |
| | | 574 | | internal long? GetComputedOrBufferLength() |
| | 0 | 575 | | { |
| | 0 | 576 | | CheckDisposed(); |
| | | 577 | | |
| | 0 | 578 | | if (IsBuffered) |
| | 0 | 579 | | { |
| | 0 | 580 | | return _bufferedContent.Length; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | // If we already tried to calculate the length, but the derived class returned 'false', then don't try |
| | | 584 | | // again; just return null. |
| | 0 | 585 | | if (_canCalculateLength) |
| | 0 | 586 | | { |
| | | 587 | | long length; |
| | 0 | 588 | | if (TryComputeLength(out length)) |
| | 0 | 589 | | { |
| | 0 | 590 | | return length; |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | // Set flag to make sure next time we don't try to compute the length, since we know that we're unable |
| | | 594 | | // to do so. |
| | 0 | 595 | | _canCalculateLength = false; |
| | 0 | 596 | | } |
| | 0 | 597 | | return null; |
| | 0 | 598 | | } |
| | | 599 | | |
| | | 600 | | private bool CreateTemporaryBuffer(long maxBufferSize, out LimitArrayPoolWriteStream? tempBuffer, out Exception? |
| | 0 | 601 | | { |
| | 0 | 602 | | if (maxBufferSize > HttpContent.MaxBufferSize) |
| | 0 | 603 | | { |
| | | 604 | | // This should only be hit when called directly; HttpClient/HttpClientHandler |
| | | 605 | | // will not exceed this limit. |
| | 0 | 606 | | throw new ArgumentOutOfRangeException(nameof(maxBufferSize), maxBufferSize, |
| | 0 | 607 | | SR.Format(CultureInfo.InvariantCulture, |
| | 0 | 608 | | SR.net_http_content_buffersize_limit, HttpContent.MaxBufferSize)); |
| | | 609 | | } |
| | | 610 | | |
| | 0 | 611 | | if (IsBuffered) |
| | 0 | 612 | | { |
| | | 613 | | // If we already buffered the content, just return false. |
| | 0 | 614 | | tempBuffer = default; |
| | 0 | 615 | | error = default; |
| | 0 | 616 | | return false; |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | // If we have a Content-Length allocate the right amount of buffer up-front. Also check whether the |
| | | 620 | | // content length exceeds the max. buffer size. |
| | 0 | 621 | | long contentLength = Headers.ContentLength.GetValueOrDefault(); |
| | 0 | 622 | | Debug.Assert(contentLength >= 0); |
| | | 623 | | |
| | 0 | 624 | | if (contentLength > maxBufferSize) |
| | 0 | 625 | | { |
| | 0 | 626 | | tempBuffer = null; |
| | 0 | 627 | | error = CreateOverCapacityException(maxBufferSize); |
| | 0 | 628 | | } |
| | | 629 | | else |
| | 0 | 630 | | { |
| | 0 | 631 | | tempBuffer = new LimitArrayPoolWriteStream((int)maxBufferSize, contentLength, getFinalSizeFromPool: fals |
| | 0 | 632 | | error = null; |
| | 0 | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | return true; |
| | 0 | 636 | | } |
| | | 637 | | |
| | | 638 | | #region IDisposable Members |
| | | 639 | | |
| | | 640 | | protected virtual void Dispose(bool disposing) |
| | 0 | 641 | | { |
| | 0 | 642 | | if (disposing && !_disposed) |
| | 0 | 643 | | { |
| | 0 | 644 | | _disposed = true; |
| | | 645 | | |
| | 0 | 646 | | if (_contentReadStream != null) |
| | 0 | 647 | | { |
| | 0 | 648 | | Stream? s = _contentReadStream as Stream ?? |
| | 0 | 649 | | (_contentReadStream is Task<Stream> t && t.Status == TaskStatus.RanToCompletion ? t.Result : nul |
| | 0 | 650 | | s?.Dispose(); |
| | 0 | 651 | | _contentReadStream = null; |
| | 0 | 652 | | } |
| | | 653 | | |
| | 0 | 654 | | if (IsBuffered) |
| | 0 | 655 | | { |
| | 0 | 656 | | _bufferedContent.Dispose(); |
| | 0 | 657 | | } |
| | 0 | 658 | | } |
| | 0 | 659 | | } |
| | | 660 | | |
| | | 661 | | public void Dispose() |
| | 0 | 662 | | { |
| | 0 | 663 | | Dispose(true); |
| | 0 | 664 | | GC.SuppressFinalize(this); |
| | 0 | 665 | | } |
| | | 666 | | |
| | | 667 | | #endregion |
| | | 668 | | |
| | | 669 | | #region Helpers |
| | | 670 | | |
| | | 671 | | private void CheckDisposed() |
| | 0 | 672 | | { |
| | 0 | 673 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 674 | | } |
| | | 675 | | |
| | | 676 | | private void CheckTaskNotNull(Task task) |
| | 0 | 677 | | { |
| | 0 | 678 | | if (task == null) |
| | 0 | 679 | | { |
| | 0 | 680 | | var e = new InvalidOperationException(SR.net_http_content_no_task_returned); |
| | 0 | 681 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, e); |
| | 0 | 682 | | throw e; |
| | | 683 | | } |
| | 0 | 684 | | } |
| | | 685 | | |
| | 0 | 686 | | internal static bool StreamCopyExceptionNeedsWrapping(Exception e) => e is IOException || e is ObjectDisposedExc |
| | | 687 | | |
| | | 688 | | private static Exception GetStreamCopyException(Exception originalException) |
| | 0 | 689 | | { |
| | | 690 | | // HttpContent derived types should throw HttpRequestExceptions if there is an error. However, since the str |
| | | 691 | | // provided by CopyToAsync() can also throw, we wrap such exceptions in HttpRequestException. This way custo |
| | | 692 | | // types don't have to worry about it. The goal is that users of HttpContent don't have to catch multiple |
| | | 693 | | // exceptions (depending on the underlying transport), but just HttpRequestExceptions |
| | | 694 | | // Custom stream should throw either IOException or HttpRequestException. |
| | | 695 | | // We don't want to wrap other exceptions thrown by Stream (e.g. InvalidOperationException), since we |
| | | 696 | | // don't want to hide such "usage error" exceptions in HttpRequestException. |
| | | 697 | | // ObjectDisposedException is also wrapped, since aborting HWR after a request is complete will result in |
| | | 698 | | // the response stream being closed. |
| | 0 | 699 | | return StreamCopyExceptionNeedsWrapping(originalException) ? |
| | 0 | 700 | | WrapStreamCopyException(originalException) : |
| | 0 | 701 | | originalException; |
| | 0 | 702 | | } |
| | | 703 | | |
| | | 704 | | internal static Exception WrapStreamCopyException(Exception e) |
| | 0 | 705 | | { |
| | 0 | 706 | | Debug.Assert(StreamCopyExceptionNeedsWrapping(e)); |
| | 0 | 707 | | HttpRequestError error = e is HttpIOException ioEx ? ioEx.HttpRequestError : HttpRequestError.Unknown; |
| | 0 | 708 | | return ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(error, SR.net_http_content_stream |
| | 0 | 709 | | } |
| | | 710 | | |
| | | 711 | | private static int GetPreambleLength(ReadOnlySpan<byte> data, Encoding encoding) |
| | 0 | 712 | | { |
| | 0 | 713 | | Debug.Assert(encoding != null); |
| | | 714 | | |
| | 0 | 715 | | switch (encoding.CodePage) |
| | | 716 | | { |
| | | 717 | | case UTF8CodePage: |
| | 0 | 718 | | return data.StartsWith(UTF8Preamble) ? UTF8Preamble.Length : 0; |
| | | 719 | | |
| | | 720 | | case UTF32CodePage: |
| | 0 | 721 | | return data.StartsWith(UTF32Preamble) ? UTF32Preamble.Length : 0; |
| | | 722 | | |
| | | 723 | | case UnicodeCodePage: |
| | 0 | 724 | | return data.StartsWith(UnicodePreamble) ? UnicodePreamble.Length : 0; |
| | | 725 | | |
| | | 726 | | case BigEndianUnicodeCodePage: |
| | 0 | 727 | | return data.StartsWith(BigEndianUnicodePreamble) ? BigEndianUnicodePreamble.Length : 0; |
| | | 728 | | |
| | | 729 | | default: |
| | 0 | 730 | | byte[] preamble = encoding.GetPreamble(); |
| | 0 | 731 | | return preamble is not null && data.StartsWith(preamble) ? preamble.Length : 0; |
| | | 732 | | } |
| | 0 | 733 | | } |
| | | 734 | | |
| | | 735 | | private static bool TryDetectEncoding(ReadOnlySpan<byte> data, [NotNullWhen(true)] out Encoding? encoding, out i |
| | 0 | 736 | | { |
| | 0 | 737 | | if (data.StartsWith(UTF8Preamble)) |
| | 0 | 738 | | { |
| | 0 | 739 | | encoding = Encoding.UTF8; |
| | 0 | 740 | | preambleLength = UTF8Preamble.Length; |
| | 0 | 741 | | return true; |
| | | 742 | | } |
| | | 743 | | |
| | 0 | 744 | | if (data.StartsWith(UTF32Preamble)) |
| | 0 | 745 | | { |
| | 0 | 746 | | encoding = Encoding.UTF32; |
| | 0 | 747 | | preambleLength = UTF32Preamble.Length; |
| | 0 | 748 | | return true; |
| | | 749 | | } |
| | | 750 | | |
| | 0 | 751 | | if (data.StartsWith(UnicodePreamble)) |
| | 0 | 752 | | { |
| | 0 | 753 | | encoding = Encoding.Unicode; |
| | 0 | 754 | | preambleLength = UnicodePreamble.Length; |
| | 0 | 755 | | return true; |
| | | 756 | | } |
| | | 757 | | |
| | 0 | 758 | | if (data.StartsWith(BigEndianUnicodePreamble)) |
| | 0 | 759 | | { |
| | 0 | 760 | | encoding = Encoding.BigEndianUnicode; |
| | 0 | 761 | | preambleLength = BigEndianUnicodePreamble.Length; |
| | 0 | 762 | | return true; |
| | | 763 | | } |
| | | 764 | | |
| | 0 | 765 | | encoding = null; |
| | 0 | 766 | | preambleLength = 0; |
| | 0 | 767 | | return false; |
| | 0 | 768 | | } |
| | | 769 | | |
| | | 770 | | #endregion Helpers |
| | | 771 | | |
| | | 772 | | private static async Task<TResult> WaitAndReturnAsync<TState, TResult>(Task waitTask, TState state, Func<TState, |
| | 0 | 773 | | { |
| | 0 | 774 | | await waitTask.ConfigureAwait(false); |
| | 0 | 775 | | return returnFunc(state); |
| | 0 | 776 | | } |
| | | 777 | | |
| | | 778 | | private static HttpRequestException CreateOverCapacityException(long maxBufferSize) |
| | 0 | 779 | | { |
| | 0 | 780 | | return (HttpRequestException)ExceptionDispatchInfo.SetCurrentStackTrace(new HttpRequestException(HttpRequest |
| | 0 | 781 | | } |
| | | 782 | | |
| | | 783 | | /// <summary> |
| | | 784 | | /// A write-only stream that limits the total length of the content to <see cref="_maxBufferSize"/>. |
| | | 785 | | /// It uses pooled buffers for the content, but can switch to a regular array allocation, which is useful when t |
| | | 786 | | /// already knows the final size and needs a new array anyway (e.g. <see cref="HttpClient.GetByteArrayAsync(stri |
| | | 787 | | /// <para>Since we can't rely on users to reliably dispose content objects, any pooled buffers must be returned |
| | | 788 | | /// the execution path we control. In practice this means <see cref="LoadIntoBufferAsync()"/> must call <see cre |
| | | 789 | | /// before storing the stream in <see cref="_bufferedContent"/>.</para> |
| | | 790 | | /// </summary> |
| | | 791 | | internal sealed class LimitArrayPoolWriteStream : Stream |
| | | 792 | | { |
| | | 793 | | /// <summary>Applies when a Content-Length header was not specified.</summary> |
| | | 794 | | private const int MinInitialBufferSize = 16 * 1024; // 16 KB |
| | | 795 | | |
| | | 796 | | /// <summary>Applies when a Content-Length header was set. If it's <= this limit, we'll allocate an exact |
| | | 797 | | private const int MaxInitialBufferSize = 16 * 1024 * 1024; // 16 MB |
| | | 798 | | |
| | | 799 | | private const int ResizeFactor = 2; |
| | | 800 | | |
| | | 801 | | /// <summary>Controls how quickly we're willing to expand up to <see cref="_expectedFinalSize"/> when a call |
| | | 802 | | /// <para>The factor is higher than usual to lower the number of memory copies when the caller already commi |
| | | 803 | | private const int LastResizeFactor = 4; |
| | | 804 | | |
| | | 805 | | /// <summary><see cref="_totalLength"/> may not exceed this limit, or we'll throw a <see cref="CreateOverCap |
| | | 806 | | private readonly int _maxBufferSize; |
| | | 807 | | /// <summary>The value of the Content-Length header or 0. <see cref="_totalLength"/> may exceed this value i |
| | | 808 | | private readonly int _expectedFinalSize; |
| | | 809 | | /// <summary>Indicates whether the caller will need an exactly-sized, non-pooled, buffer. The implementation |
| | | 810 | | private readonly bool _shouldPoolFinalSize; |
| | | 811 | | |
| | | 812 | | private bool _lastBufferIsPooled; |
| | | 813 | | private byte[] _lastBuffer; |
| | | 814 | | private byte[]?[]? _pooledBuffers; |
| | | 815 | | private int _lastBufferOffset; |
| | | 816 | | private int _totalLength; |
| | | 817 | | |
| | 0 | 818 | | public LimitArrayPoolWriteStream(int maxBufferSize, long expectedFinalSize, bool getFinalSizeFromPool) |
| | 0 | 819 | | { |
| | 0 | 820 | | Debug.Assert(maxBufferSize >= 0); |
| | 0 | 821 | | Debug.Assert(expectedFinalSize >= 0); |
| | | 822 | | |
| | 0 | 823 | | if (expectedFinalSize > maxBufferSize) |
| | 0 | 824 | | { |
| | 0 | 825 | | throw CreateOverCapacityException(maxBufferSize); |
| | | 826 | | } |
| | | 827 | | |
| | 0 | 828 | | _maxBufferSize = maxBufferSize; |
| | 0 | 829 | | _expectedFinalSize = (int)expectedFinalSize; |
| | 0 | 830 | | _shouldPoolFinalSize = getFinalSizeFromPool || expectedFinalSize == 0; |
| | 0 | 831 | | _lastBufferIsPooled = false; |
| | 0 | 832 | | _lastBuffer = []; |
| | 0 | 833 | | } |
| | | 834 | | |
| | | 835 | | #if DEBUG |
| | | 836 | | ~LimitArrayPoolWriteStream() |
| | 0 | 837 | | { |
| | | 838 | | // Ensure that we're not leaking pooled buffers. |
| | 0 | 839 | | Debug.Assert(_pooledBuffers is null); |
| | 0 | 840 | | Debug.Assert(!_lastBufferIsPooled); |
| | 0 | 841 | | } |
| | | 842 | | #endif |
| | | 843 | | |
| | | 844 | | protected override void Dispose(bool disposing) |
| | 0 | 845 | | { |
| | 0 | 846 | | ReturnAllPooledBuffers(); |
| | 0 | 847 | | base.Dispose(disposing); |
| | 0 | 848 | | } |
| | | 849 | | |
| | | 850 | | /// <summary>Should only be called once.</summary> |
| | | 851 | | public byte[] ToArray() |
| | 0 | 852 | | { |
| | 0 | 853 | | Debug.Assert(!_shouldPoolFinalSize || _expectedFinalSize == 0); |
| | | 854 | | |
| | 0 | 855 | | if (!_lastBufferIsPooled && _totalLength == _lastBuffer.Length) |
| | 0 | 856 | | { |
| | 0 | 857 | | Debug.Assert(_pooledBuffers is null); |
| | 0 | 858 | | return _lastBuffer; |
| | | 859 | | } |
| | | 860 | | |
| | 0 | 861 | | if (_totalLength == 0) |
| | 0 | 862 | | { |
| | 0 | 863 | | return []; |
| | | 864 | | } |
| | | 865 | | |
| | 0 | 866 | | byte[] buffer = new byte[_totalLength]; |
| | 0 | 867 | | CopyToCore(buffer); |
| | 0 | 868 | | return buffer; |
| | 0 | 869 | | } |
| | | 870 | | |
| | | 871 | | /// <summary>Should only be called if <see cref="ReallocateIfPooled"/> was used to avoid exposing pooled buf |
| | | 872 | | public byte[] GetSingleBuffer() |
| | 0 | 873 | | { |
| | 0 | 874 | | Debug.Assert(!_lastBufferIsPooled); |
| | 0 | 875 | | Debug.Assert(_pooledBuffers is null); |
| | | 876 | | |
| | 0 | 877 | | return _lastBuffer; |
| | 0 | 878 | | } |
| | | 879 | | |
| | | 880 | | public ReadOnlySpan<byte> GetFirstBuffer() |
| | 0 | 881 | | { |
| | 0 | 882 | | return _pooledBuffers is byte[]?[] buffers |
| | 0 | 883 | | ? buffers[0] |
| | 0 | 884 | | : _lastBuffer.AsSpan(0, _totalLength); |
| | 0 | 885 | | } |
| | | 886 | | |
| | | 887 | | public byte[] CreateCopy() |
| | 0 | 888 | | { |
| | 0 | 889 | | Debug.Assert(!_lastBufferIsPooled); |
| | 0 | 890 | | Debug.Assert(_pooledBuffers is null); |
| | 0 | 891 | | Debug.Assert(_lastBufferOffset == _totalLength); |
| | 0 | 892 | | Debug.Assert(_lastBufferOffset <= _lastBuffer.Length); |
| | | 893 | | |
| | 0 | 894 | | return _lastBuffer.AsSpan(0, _totalLength).ToArray(); |
| | 0 | 895 | | } |
| | | 896 | | |
| | | 897 | | public void ReallocateIfPooled() |
| | 0 | 898 | | { |
| | 0 | 899 | | Debug.Assert(_lastBufferIsPooled || _pooledBuffers is null); |
| | | 900 | | |
| | 0 | 901 | | if (_lastBufferIsPooled) |
| | 0 | 902 | | { |
| | 0 | 903 | | byte[] newBuffer = new byte[_totalLength]; |
| | 0 | 904 | | CopyToCore(newBuffer); |
| | 0 | 905 | | ReturnAllPooledBuffers(); |
| | 0 | 906 | | _lastBuffer = newBuffer; |
| | 0 | 907 | | _lastBufferOffset = newBuffer.Length; |
| | 0 | 908 | | } |
| | 0 | 909 | | } |
| | | 910 | | |
| | | 911 | | public override void Write(ReadOnlySpan<byte> buffer) |
| | 0 | 912 | | { |
| | 0 | 913 | | if (_maxBufferSize - _totalLength < buffer.Length) |
| | 0 | 914 | | { |
| | 0 | 915 | | throw CreateOverCapacityException(_maxBufferSize); |
| | | 916 | | } |
| | | 917 | | |
| | 0 | 918 | | byte[] lastBuffer = _lastBuffer; |
| | 0 | 919 | | int offset = _lastBufferOffset; |
| | | 920 | | |
| | 0 | 921 | | if (lastBuffer.Length - offset >= buffer.Length) |
| | 0 | 922 | | { |
| | 0 | 923 | | buffer.CopyTo(lastBuffer.AsSpan(offset)); |
| | 0 | 924 | | _lastBufferOffset = offset + buffer.Length; |
| | 0 | 925 | | _totalLength += buffer.Length; |
| | 0 | 926 | | } |
| | | 927 | | else |
| | 0 | 928 | | { |
| | 0 | 929 | | GrowAndWrite(buffer); |
| | 0 | 930 | | } |
| | 0 | 931 | | } |
| | | 932 | | |
| | | 933 | | private void GrowAndWrite(ReadOnlySpan<byte> buffer) |
| | 0 | 934 | | { |
| | 0 | 935 | | Debug.Assert(_totalLength + buffer.Length <= _maxBufferSize); |
| | | 936 | | |
| | 0 | 937 | | int lastBufferCapacity = _lastBuffer.Length; |
| | | 938 | | |
| | | 939 | | // Start by doubling the current array size. |
| | 0 | 940 | | int newBufferCapacity = (int)Math.Min((uint)lastBufferCapacity * ResizeFactor, Array.MaxLength); |
| | | 941 | | |
| | | 942 | | // If the required length is longer than Array.MaxLength, we'll let the runtime throw. |
| | 0 | 943 | | newBufferCapacity = Math.Max(newBufferCapacity, _totalLength + buffer.Length); |
| | | 944 | | |
| | | 945 | | // If this is the first write, set an initial minimum size. |
| | 0 | 946 | | if (lastBufferCapacity == 0) |
| | 0 | 947 | | { |
| | 0 | 948 | | int minCapacity = _expectedFinalSize == 0 |
| | 0 | 949 | | ? MinInitialBufferSize |
| | 0 | 950 | | : Math.Min(_expectedFinalSize, MaxInitialBufferSize / LastResizeFactor); |
| | | 951 | | |
| | 0 | 952 | | newBufferCapacity = Math.Max(newBufferCapacity, minCapacity); |
| | 0 | 953 | | } |
| | | 954 | | |
| | | 955 | | // Avoid having the last buffer expand beyond the size limit too much. |
| | | 956 | | // It may still go beyond the limit somewhat due to the ArrayPool's buffer sizes being powers of 2. |
| | 0 | 957 | | int currentTotalCapacity = _totalLength - _lastBufferOffset + lastBufferCapacity; |
| | 0 | 958 | | int remainingUntilMaxCapacity = _maxBufferSize - currentTotalCapacity; |
| | 0 | 959 | | newBufferCapacity = Math.Min(newBufferCapacity, remainingUntilMaxCapacity); |
| | | 960 | | |
| | 0 | 961 | | int newTotalCapacity = currentTotalCapacity + newBufferCapacity; |
| | | 962 | | |
| | 0 | 963 | | Debug.Assert(newBufferCapacity > 0); |
| | | 964 | | byte[] newBuffer; |
| | | 965 | | |
| | | 966 | | // If we don't want to pool our last buffer and we're getting close to its size, allocate the exact leng |
| | 0 | 967 | | if (!_shouldPoolFinalSize && newTotalCapacity >= _expectedFinalSize / LastResizeFactor) |
| | 0 | 968 | | { |
| | | 969 | | // We knew the Content-Length upfront, and the caller needs an exactly-sized, non-pooled, buffer. |
| | | 970 | | // It's almost certain that the final length will match the expected size, |
| | | 971 | | // so we switch from pooled buffers to a regular array now to reduce memory copies. |
| | | 972 | | |
| | | 973 | | // It's possible we're writing more bytes than the expected final size if the handler/content is not |
| | | 974 | | // enforcing Content-Length correctness. Such requests will likely throw later on anyway. |
| | 0 | 975 | | newBuffer = new byte[_totalLength + buffer.Length <= _expectedFinalSize ? _expectedFinalSize : newTo |
| | | 976 | | |
| | 0 | 977 | | CopyToCore(newBuffer); |
| | | 978 | | |
| | 0 | 979 | | ReturnAllPooledBuffers(); |
| | | 980 | | |
| | 0 | 981 | | buffer.CopyTo(newBuffer.AsSpan(_totalLength)); |
| | | 982 | | |
| | 0 | 983 | | _totalLength += buffer.Length; |
| | 0 | 984 | | _lastBufferOffset = _totalLength; |
| | 0 | 985 | | _lastBufferIsPooled = false; |
| | 0 | 986 | | } |
| | 0 | 987 | | else if (lastBufferCapacity == 0) |
| | 0 | 988 | | { |
| | | 989 | | // This is the first write call, allocate the initial buffer. |
| | 0 | 990 | | Debug.Assert(_pooledBuffers is null); |
| | 0 | 991 | | Debug.Assert(_lastBufferOffset == 0); |
| | 0 | 992 | | Debug.Assert(_totalLength == 0); |
| | | 993 | | |
| | 0 | 994 | | newBuffer = ArrayPool<byte>.Shared.Rent(newBufferCapacity); |
| | 0 | 995 | | Debug.Assert(_shouldPoolFinalSize || newBuffer.Length != _expectedFinalSize); |
| | | 996 | | |
| | 0 | 997 | | buffer.CopyTo(newBuffer); |
| | 0 | 998 | | _totalLength = _lastBufferOffset = buffer.Length; |
| | 0 | 999 | | _lastBufferIsPooled = true; |
| | 0 | 1000 | | } |
| | | 1001 | | else |
| | 0 | 1002 | | { |
| | 0 | 1003 | | Debug.Assert(_lastBufferIsPooled); |
| | | 1004 | | |
| | 0 | 1005 | | _totalLength += buffer.Length; |
| | | 1006 | | |
| | | 1007 | | // When buffers are stored in '_pooledBuffers', they are assumed to be full. |
| | | 1008 | | // Copy as many bytes as we can fit into the current buffer now. |
| | 0 | 1009 | | Span<byte> remainingInCurrentBuffer = _lastBuffer.AsSpan(_lastBufferOffset); |
| | 0 | 1010 | | Debug.Assert(remainingInCurrentBuffer.Length < buffer.Length); |
| | 0 | 1011 | | buffer.Slice(0, remainingInCurrentBuffer.Length).CopyTo(remainingInCurrentBuffer); |
| | 0 | 1012 | | buffer = buffer.Slice(remainingInCurrentBuffer.Length); |
| | | 1013 | | |
| | 0 | 1014 | | newBuffer = ArrayPool<byte>.Shared.Rent(newBufferCapacity); |
| | 0 | 1015 | | buffer.CopyTo(newBuffer); |
| | 0 | 1016 | | _lastBufferOffset = buffer.Length; |
| | | 1017 | | |
| | | 1018 | | // Find the first empty slot in '_pooledBuffers', resizing the array if needed. |
| | 0 | 1019 | | int bufferCount = 0; |
| | 0 | 1020 | | if (_pooledBuffers is null) |
| | 0 | 1021 | | { |
| | | 1022 | | // Starting with 4 buffers means we'll have capacity for at least |
| | | 1023 | | // 16 KB + 32 KB + 64 KB + 128 KB + 256 KB (last buffer) = 496 KB |
| | 0 | 1024 | | _pooledBuffers = new byte[]?[4]; |
| | 0 | 1025 | | } |
| | | 1026 | | else |
| | 0 | 1027 | | { |
| | 0 | 1028 | | byte[]?[] buffers = _pooledBuffers; |
| | 0 | 1029 | | while (bufferCount < buffers.Length && buffers[bufferCount] is not null) |
| | 0 | 1030 | | { |
| | 0 | 1031 | | bufferCount++; |
| | 0 | 1032 | | } |
| | | 1033 | | |
| | 0 | 1034 | | if (bufferCount == buffers.Length) |
| | 0 | 1035 | | { |
| | 0 | 1036 | | Debug.Assert(bufferCount <= 16); |
| | | 1037 | | |
| | | 1038 | | // After the first resize, we should have enough capacity for at least ~8 MB. |
| | | 1039 | | // ~128 MB after the second, ~2 GB after the third. |
| | 0 | 1040 | | Array.Resize(ref _pooledBuffers, bufferCount + 4); |
| | 0 | 1041 | | } |
| | 0 | 1042 | | } |
| | | 1043 | | |
| | 0 | 1044 | | _pooledBuffers[bufferCount] = _lastBuffer; |
| | 0 | 1045 | | } |
| | | 1046 | | |
| | 0 | 1047 | | _lastBuffer = newBuffer; |
| | 0 | 1048 | | } |
| | | 1049 | | |
| | | 1050 | | public void CopyToCore(Span<byte> destination) |
| | 0 | 1051 | | { |
| | 0 | 1052 | | Debug.Assert(destination.Length >= _totalLength); |
| | | 1053 | | |
| | 0 | 1054 | | if (_pooledBuffers is byte[]?[] buffers) |
| | 0 | 1055 | | { |
| | 0 | 1056 | | Debug.Assert(buffers.Length > 0 && buffers[0] is not null); |
| | | 1057 | | |
| | 0 | 1058 | | foreach (byte[]? buffer in buffers) |
| | 0 | 1059 | | { |
| | 0 | 1060 | | if (buffer is null) |
| | 0 | 1061 | | { |
| | 0 | 1062 | | break; |
| | | 1063 | | } |
| | | 1064 | | |
| | 0 | 1065 | | Debug.Assert(destination.Length >= buffer.Length); |
| | | 1066 | | |
| | 0 | 1067 | | buffer.CopyTo(destination); |
| | 0 | 1068 | | destination = destination.Slice(buffer.Length); |
| | 0 | 1069 | | } |
| | 0 | 1070 | | } |
| | | 1071 | | |
| | 0 | 1072 | | Debug.Assert(_lastBufferOffset <= _lastBuffer.Length); |
| | 0 | 1073 | | Debug.Assert(_lastBufferOffset <= destination.Length); |
| | | 1074 | | |
| | 0 | 1075 | | _lastBuffer.AsSpan(0, _lastBufferOffset).CopyTo(destination); |
| | 0 | 1076 | | } |
| | | 1077 | | |
| | | 1078 | | private void ReturnAllPooledBuffers() |
| | 0 | 1079 | | { |
| | 0 | 1080 | | if (_pooledBuffers is byte[]?[] buffers) |
| | 0 | 1081 | | { |
| | 0 | 1082 | | _pooledBuffers = null; |
| | | 1083 | | |
| | 0 | 1084 | | foreach (byte[]? buffer in buffers) |
| | 0 | 1085 | | { |
| | 0 | 1086 | | if (buffer is null) |
| | 0 | 1087 | | { |
| | 0 | 1088 | | break; |
| | | 1089 | | } |
| | | 1090 | | |
| | 0 | 1091 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 0 | 1092 | | } |
| | 0 | 1093 | | } |
| | | 1094 | | |
| | 0 | 1095 | | Debug.Assert(_lastBuffer is not null); |
| | 0 | 1096 | | byte[] lastBuffer = _lastBuffer; |
| | 0 | 1097 | | _lastBuffer = null!; |
| | | 1098 | | |
| | 0 | 1099 | | if (_lastBufferIsPooled) |
| | 0 | 1100 | | { |
| | 0 | 1101 | | _lastBufferIsPooled = false; |
| | 0 | 1102 | | ArrayPool<byte>.Shared.Return(lastBuffer); |
| | 0 | 1103 | | } |
| | 0 | 1104 | | } |
| | | 1105 | | |
| | | 1106 | | public override void Write(byte[] buffer, int offset, int count) |
| | 0 | 1107 | | { |
| | 0 | 1108 | | ValidateBufferArguments(buffer, offset, count); |
| | | 1109 | | |
| | 0 | 1110 | | Write(buffer.AsSpan(offset, count)); |
| | 0 | 1111 | | } |
| | | 1112 | | |
| | | 1113 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 0 | 1114 | | { |
| | 0 | 1115 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 1116 | | { |
| | 0 | 1117 | | return Task.FromCanceled(cancellationToken); |
| | | 1118 | | } |
| | | 1119 | | |
| | 0 | 1120 | | Write(buffer, offset, count); |
| | 0 | 1121 | | return Task.CompletedTask; |
| | 0 | 1122 | | } |
| | | 1123 | | |
| | | 1124 | | public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = defa |
| | 0 | 1125 | | { |
| | 0 | 1126 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 1127 | | { |
| | 0 | 1128 | | return ValueTask.FromCanceled(cancellationToken); |
| | | 1129 | | } |
| | | 1130 | | |
| | 0 | 1131 | | Write(buffer.Span); |
| | 0 | 1132 | | return default; |
| | 0 | 1133 | | } |
| | | 1134 | | |
| | | 1135 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, |
| | 0 | 1136 | | TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncS |
| | | 1137 | | |
| | | 1138 | | public override void EndWrite(IAsyncResult asyncResult) => |
| | 0 | 1139 | | TaskToAsyncResult.End(asyncResult); |
| | | 1140 | | |
| | | 1141 | | public override void WriteByte(byte value) => |
| | 0 | 1142 | | Write(new ReadOnlySpan<byte>(ref value)); |
| | | 1143 | | |
| | 0 | 1144 | | public override void Flush() { } |
| | 0 | 1145 | | public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| | | 1146 | | |
| | 0 | 1147 | | public override long Length => _totalLength; |
| | 0 | 1148 | | public override bool CanWrite => true; |
| | 0 | 1149 | | public override bool CanRead => false; |
| | 0 | 1150 | | public override bool CanSeek => false; |
| | | 1151 | | |
| | 0 | 1152 | | public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedExcep |
| | 0 | 1153 | | public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } |
| | 0 | 1154 | | public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } |
| | 0 | 1155 | | public override void SetLength(long value) { throw new NotSupportedException(); } |
| | | 1156 | | } |
| | | 1157 | | } |
| | | 1158 | | } |