| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | // Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/WebUtilities/src |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Buffers; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | |
| | | 13 | | #pragma warning disable CA1305 // ToString Locale |
| | | 14 | | #pragma warning disable CA1822 // Make member static |
| | | 15 | | #pragma warning disable IDE0016 // Simplify null check |
| | | 16 | | #pragma warning disable IDE0036 // Modifiers not ordered |
| | | 17 | | #pragma warning disable IDE0054 // Use compound assignment |
| | | 18 | | #pragma warning disable IDE0059 // Unnecessary assignment |
| | | 19 | | |
| | | 20 | | namespace Azure.Core.Http.Multipart |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// A Stream that wraps another stream and allows reading lines. |
| | | 24 | | /// The data is buffered in memory. |
| | | 25 | | /// </summary> |
| | | 26 | | internal class BufferedReadStream : Stream |
| | | 27 | | { |
| | | 28 | | private const byte CR = (byte)'\r'; |
| | | 29 | | private const byte LF = (byte)'\n'; |
| | | 30 | | |
| | | 31 | | private readonly Stream _inner; |
| | | 32 | | private readonly byte[] _buffer; |
| | | 33 | | private readonly ArrayPool<byte> _bytePool; |
| | | 34 | | private int _bufferOffset = 0; |
| | | 35 | | private int _bufferCount = 0; |
| | | 36 | | private bool _disposed; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Creates a new stream. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="inner">The stream to wrap.</param> |
| | | 42 | | /// <param name="bufferSize">Size of buffer in bytes.</param> |
| | | 43 | | public BufferedReadStream(Stream inner, int bufferSize) |
| | 1360 | 44 | | : this(inner, bufferSize, ArrayPool<byte>.Shared) |
| | | 45 | | { |
| | 1360 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates a new stream. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="inner">The stream to wrap.</param> |
| | | 52 | | /// <param name="bufferSize">Size of buffer in bytes.</param> |
| | | 53 | | /// <param name="bytePool">ArrayPool for the buffer.</param> |
| | 1360 | 54 | | public BufferedReadStream(Stream inner, int bufferSize, ArrayPool<byte> bytePool) |
| | | 55 | | { |
| | 1360 | 56 | | if (inner == null) |
| | | 57 | | { |
| | 0 | 58 | | throw new ArgumentNullException(nameof(inner)); |
| | | 59 | | } |
| | | 60 | | |
| | 1360 | 61 | | _inner = inner; |
| | 1360 | 62 | | _bytePool = bytePool; |
| | 1360 | 63 | | _buffer = bytePool.Rent(bufferSize); |
| | 1360 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// The currently buffered data. |
| | | 68 | | /// </summary> |
| | | 69 | | public ArraySegment<byte> BufferedData |
| | | 70 | | { |
| | 2660 | 71 | | get { return new ArraySegment<byte>(_buffer, _bufferOffset, _bufferCount); } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc/> |
| | | 75 | | public override bool CanRead |
| | | 76 | | { |
| | 1272 | 77 | | get { return _inner.CanRead || _bufferCount > 0; } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc/> |
| | | 81 | | public override bool CanSeek |
| | | 82 | | { |
| | 7836 | 83 | | get { return _inner.CanSeek; } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc/> |
| | | 87 | | public override bool CanTimeout |
| | | 88 | | { |
| | 0 | 89 | | get { return _inner.CanTimeout; } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc/> |
| | | 93 | | public override bool CanWrite |
| | | 94 | | { |
| | 0 | 95 | | get { return _inner.CanWrite; } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc/> |
| | | 99 | | public override long Length |
| | | 100 | | { |
| | 1272 | 101 | | get { return _inner.Length; } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc/> |
| | | 105 | | public override long Position |
| | | 106 | | { |
| | 6564 | 107 | | get { return _inner.Position - _bufferCount; } |
| | | 108 | | set |
| | | 109 | | { |
| | 0 | 110 | | if (value < 0) |
| | | 111 | | { |
| | 0 | 112 | | throw new ArgumentOutOfRangeException(nameof(value), value, "Position must be positive."); |
| | | 113 | | } |
| | 0 | 114 | | if (value == Position) |
| | | 115 | | { |
| | 0 | 116 | | return; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | // Backwards? |
| | 0 | 120 | | if (value <= _inner.Position) |
| | | 121 | | { |
| | | 122 | | // Forward within the buffer? |
| | 0 | 123 | | var innerOffset = (int)(_inner.Position - value); |
| | 0 | 124 | | if (innerOffset <= _bufferCount) |
| | | 125 | | { |
| | | 126 | | // Yes, just skip some of the buffered data |
| | 0 | 127 | | _bufferOffset += innerOffset; |
| | 0 | 128 | | _bufferCount -= innerOffset; |
| | | 129 | | } |
| | | 130 | | else |
| | | 131 | | { |
| | | 132 | | // No, reset the buffer |
| | 0 | 133 | | _bufferOffset = 0; |
| | 0 | 134 | | _bufferCount = 0; |
| | 0 | 135 | | _inner.Position = value; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | else |
| | | 139 | | { |
| | | 140 | | // Forward, reset the buffer |
| | 0 | 141 | | _bufferOffset = 0; |
| | 0 | 142 | | _bufferCount = 0; |
| | 0 | 143 | | _inner.Position = value; |
| | | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <inheritdoc/> |
| | | 149 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 150 | | { |
| | 0 | 151 | | if (origin == SeekOrigin.Begin) |
| | | 152 | | { |
| | 0 | 153 | | Position = offset; |
| | | 154 | | } |
| | 0 | 155 | | else if (origin == SeekOrigin.Current) |
| | | 156 | | { |
| | 0 | 157 | | Position = Position + offset; |
| | | 158 | | } |
| | | 159 | | else // if (origin == SeekOrigin.End) |
| | | 160 | | { |
| | 0 | 161 | | Position = Length + offset; |
| | | 162 | | } |
| | 0 | 163 | | return Position; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <inheritdoc/> |
| | | 167 | | public override void SetLength(long value) |
| | | 168 | | { |
| | 0 | 169 | | _inner.SetLength(value); |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <inheritdoc/> |
| | | 173 | | protected override void Dispose(bool disposing) |
| | | 174 | | { |
| | 1272 | 175 | | if (!_disposed) |
| | | 176 | | { |
| | 1272 | 177 | | _disposed = true; |
| | 1272 | 178 | | _bytePool.Return(_buffer); |
| | | 179 | | |
| | 1272 | 180 | | if (disposing) |
| | | 181 | | { |
| | 1272 | 182 | | _inner.Dispose(); |
| | | 183 | | } |
| | | 184 | | } |
| | 1272 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <inheritdoc/> |
| | | 188 | | public override void Flush() |
| | | 189 | | { |
| | 0 | 190 | | _inner.Flush(); |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <inheritdoc/> |
| | | 194 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | | 195 | | { |
| | 0 | 196 | | return _inner.FlushAsync(cancellationToken); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <inheritdoc/> |
| | | 200 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 201 | | { |
| | 0 | 202 | | _inner.Write(buffer, offset, count); |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <inheritdoc/> |
| | | 206 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 207 | | { |
| | 0 | 208 | | return _inner.WriteAsync(buffer, offset, count, cancellationToken); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <inheritdoc/> |
| | | 212 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 213 | | { |
| | 3336 | 214 | | ValidateBuffer(buffer, offset, count); |
| | | 215 | | |
| | | 216 | | // Drain buffer |
| | 3336 | 217 | | if (_bufferCount > 0) |
| | | 218 | | { |
| | 2700 | 219 | | int toCopy = Math.Min(_bufferCount, count); |
| | 2700 | 220 | | Buffer.BlockCopy(_buffer, _bufferOffset, buffer, offset, toCopy); |
| | 2700 | 221 | | _bufferOffset += toCopy; |
| | 2700 | 222 | | _bufferCount -= toCopy; |
| | 2700 | 223 | | return toCopy; |
| | | 224 | | } |
| | | 225 | | |
| | 636 | 226 | | return _inner.Read(buffer, offset, count); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <inheritdoc/> |
| | | 230 | | public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | | 231 | | { |
| | 764 | 232 | | ValidateBuffer(buffer, offset, count); |
| | | 233 | | |
| | | 234 | | // Drain buffer |
| | 764 | 235 | | if (_bufferCount > 0) |
| | | 236 | | { |
| | 40 | 237 | | int toCopy = Math.Min(_bufferCount, count); |
| | 40 | 238 | | Buffer.BlockCopy(_buffer, _bufferOffset, buffer, offset, toCopy); |
| | 40 | 239 | | _bufferOffset += toCopy; |
| | 40 | 240 | | _bufferCount -= toCopy; |
| | 40 | 241 | | return toCopy; |
| | | 242 | | } |
| | | 243 | | |
| | 724 | 244 | | return await _inner.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); |
| | 764 | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Ensures that the buffer is not empty. |
| | | 249 | | /// </summary> |
| | | 250 | | /// <returns>Returns <c>true</c> if the buffer is not empty; <c>false</c> otherwise.</returns> |
| | | 251 | | public bool EnsureBuffered() |
| | | 252 | | { |
| | 112510 | 253 | | if (_bufferCount > 0) |
| | | 254 | | { |
| | 111220 | 255 | | return true; |
| | | 256 | | } |
| | | 257 | | // Downshift to make room |
| | 1290 | 258 | | _bufferOffset = 0; |
| | 1290 | 259 | | _bufferCount = _inner.Read(_buffer, 0, _buffer.Length); |
| | 1290 | 260 | | return _bufferCount > 0; |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Ensures that the buffer is not empty. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 267 | | /// <returns>Returns <c>true</c> if the buffer is not empty; <c>false</c> otherwise.</returns> |
| | | 268 | | public async Task<bool> EnsureBufferedAsync(CancellationToken cancellationToken) |
| | | 269 | | { |
| | 176562 | 270 | | if (_bufferCount > 0) |
| | | 271 | | { |
| | 175268 | 272 | | return true; |
| | | 273 | | } |
| | | 274 | | // Downshift to make room |
| | 1294 | 275 | | _bufferOffset = 0; |
| | 1294 | 276 | | _bufferCount = await _inner.ReadAsync(_buffer, 0, _buffer.Length, cancellationToken).ConfigureAwait(false); |
| | 1294 | 277 | | return _bufferCount > 0; |
| | 176562 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Ensures that a minimum amount of buffered data is available. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <param name="minCount">Minimum amount of buffered data.</param> |
| | | 284 | | /// <returns>Returns <c>true</c> if the minimum amount of buffered data is available; <c>false</c> otherwise.</r |
| | | 285 | | public bool EnsureBuffered(int minCount) |
| | | 286 | | { |
| | 1286 | 287 | | if (minCount > _buffer.Length) |
| | | 288 | | { |
| | 0 | 289 | | throw new ArgumentOutOfRangeException(nameof(minCount), minCount, "The value must be smaller than the bu |
| | | 290 | | } |
| | 1318 | 291 | | while (_bufferCount < minCount) |
| | | 292 | | { |
| | | 293 | | // Downshift to make room |
| | 32 | 294 | | if (_bufferOffset > 0) |
| | | 295 | | { |
| | 32 | 296 | | if (_bufferCount > 0) |
| | | 297 | | { |
| | 26 | 298 | | Buffer.BlockCopy(_buffer, _bufferOffset, _buffer, 0, _bufferCount); |
| | | 299 | | } |
| | 32 | 300 | | _bufferOffset = 0; |
| | | 301 | | } |
| | 32 | 302 | | int read = _inner.Read(_buffer, _bufferOffset + _bufferCount, _buffer.Length - _bufferCount - _bufferOff |
| | 32 | 303 | | _bufferCount += read; |
| | 32 | 304 | | if (read == 0) |
| | | 305 | | { |
| | 0 | 306 | | return false; |
| | | 307 | | } |
| | | 308 | | } |
| | 1286 | 309 | | return true; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Ensures that a minimum amount of buffered data is available. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <param name="minCount">Minimum amount of buffered data.</param> |
| | | 316 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 317 | | /// <returns>Returns <c>true</c> if the minimum amount of buffered data is available; <c>false</c> otherwise.</r |
| | | 318 | | public async Task<bool> EnsureBufferedAsync(int minCount, CancellationToken cancellationToken) |
| | | 319 | | { |
| | 1374 | 320 | | if (minCount > _buffer.Length) |
| | | 321 | | { |
| | 0 | 322 | | throw new ArgumentOutOfRangeException(nameof(minCount), minCount, "The value must be smaller than the bu |
| | | 323 | | } |
| | 1494 | 324 | | while (_bufferCount < minCount) |
| | | 325 | | { |
| | | 326 | | // Downshift to make room |
| | 120 | 327 | | if (_bufferOffset > 0) |
| | | 328 | | { |
| | 32 | 329 | | if (_bufferCount > 0) |
| | | 330 | | { |
| | 26 | 331 | | Buffer.BlockCopy(_buffer, _bufferOffset, _buffer, 0, _bufferCount); |
| | | 332 | | } |
| | 32 | 333 | | _bufferOffset = 0; |
| | | 334 | | } |
| | 120 | 335 | | int read = await _inner.ReadAsync(_buffer, _bufferOffset + _bufferCount, _buffer.Length - _bufferCount - |
| | 120 | 336 | | _bufferCount += read; |
| | 120 | 337 | | if (read == 0) |
| | | 338 | | { |
| | 0 | 339 | | return false; |
| | | 340 | | } |
| | | 341 | | } |
| | 1374 | 342 | | return true; |
| | 1374 | 343 | | } |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Reads a line. A line is defined as a sequence of characters followed by |
| | | 347 | | /// a carriage return immediately followed by a line feed. The resulting string does not |
| | | 348 | | /// contain the terminating carriage return and line feed. |
| | | 349 | | /// </summary> |
| | | 350 | | /// <param name="lengthLimit">Maximum allowed line length.</param> |
| | | 351 | | /// <returns>A line.</returns> |
| | | 352 | | public string ReadLine(int lengthLimit) |
| | | 353 | | { |
| | 4504 | 354 | | CheckDisposed(); |
| | 4504 | 355 | | using (var builder = new MemoryStream(200)) |
| | | 356 | | { |
| | 9008 | 357 | | bool foundCR = false, foundCRLF = false; |
| | | 358 | | |
| | 116374 | 359 | | while (!foundCRLF && EnsureBuffered()) |
| | | 360 | | { |
| | 111870 | 361 | | if (builder.Length > lengthLimit) |
| | | 362 | | { |
| | 0 | 363 | | throw new InvalidDataException($"Line length limit {lengthLimit} exceeded."); |
| | | 364 | | } |
| | 111870 | 365 | | ProcessLineChar(builder, ref foundCR, ref foundCRLF); |
| | | 366 | | } |
| | | 367 | | |
| | 4504 | 368 | | return DecodeLine(builder, foundCRLF); |
| | | 369 | | } |
| | 4504 | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <summary> |
| | | 373 | | /// Reads a line. A line is defined as a sequence of characters followed by |
| | | 374 | | /// a carriage return immediately followed by a line feed. The resulting string does not |
| | | 375 | | /// contain the terminating carriage return and line feed. |
| | | 376 | | /// </summary> |
| | | 377 | | /// <param name="lengthLimit">Maximum allowed line length.</param> |
| | | 378 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 379 | | /// <returns>A line.</returns> |
| | | 380 | | public async Task<string> ReadLineAsync(int lengthLimit, CancellationToken cancellationToken) |
| | | 381 | | { |
| | 8404 | 382 | | CheckDisposed(); |
| | 8404 | 383 | | using (var builder = new MemoryStream(200)) |
| | | 384 | | { |
| | 16808 | 385 | | bool foundCR = false, foundCRLF = false; |
| | | 386 | | |
| | 184326 | 387 | | while (!foundCRLF && await EnsureBufferedAsync(cancellationToken).ConfigureAwait(false)) |
| | | 388 | | { |
| | 175922 | 389 | | if (builder.Length > lengthLimit) |
| | | 390 | | { |
| | 0 | 391 | | throw new InvalidDataException($"Line length limit {lengthLimit} exceeded."); |
| | | 392 | | } |
| | | 393 | | |
| | 175922 | 394 | | ProcessLineChar(builder, ref foundCR, ref foundCRLF); |
| | | 395 | | } |
| | | 396 | | |
| | 8404 | 397 | | return DecodeLine(builder, foundCRLF); |
| | | 398 | | } |
| | 8404 | 399 | | } |
| | | 400 | | |
| | | 401 | | private void ProcessLineChar(MemoryStream builder, ref bool foundCR, ref bool foundCRLF) |
| | | 402 | | { |
| | 287792 | 403 | | var b = _buffer[_bufferOffset]; |
| | 287792 | 404 | | builder.WriteByte(b); |
| | 287792 | 405 | | _bufferOffset++; |
| | 287792 | 406 | | _bufferCount--; |
| | 287792 | 407 | | if (b == LF && foundCR) |
| | | 408 | | { |
| | 11628 | 409 | | foundCRLF = true; |
| | 11628 | 410 | | return; |
| | | 411 | | } |
| | 276164 | 412 | | foundCR = b == CR; |
| | 276164 | 413 | | } |
| | | 414 | | |
| | | 415 | | private string DecodeLine(MemoryStream builder, bool foundCRLF) |
| | | 416 | | { |
| | | 417 | | // Drop the final CRLF, if any |
| | 12908 | 418 | | var length = foundCRLF ? builder.Length - 2 : builder.Length; |
| | 12908 | 419 | | return Encoding.UTF8.GetString(builder.ToArray(), 0, (int)length); |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | private void CheckDisposed() |
| | | 423 | | { |
| | 12908 | 424 | | if (_disposed) |
| | | 425 | | { |
| | 0 | 426 | | throw new ObjectDisposedException(nameof(BufferedReadStream)); |
| | | 427 | | } |
| | 12908 | 428 | | } |
| | | 429 | | |
| | | 430 | | private void ValidateBuffer(byte[] buffer, int offset, int count) |
| | | 431 | | { |
| | | 432 | | // Delegate most of our validation. |
| | 4100 | 433 | | var ignored = new ArraySegment<byte>(buffer, offset, count); |
| | 4100 | 434 | | if (count == 0) |
| | | 435 | | { |
| | 0 | 436 | | throw new ArgumentOutOfRangeException(nameof(count), "The value must be greater than zero."); |
| | | 437 | | } |
| | 4100 | 438 | | } |
| | | 439 | | } |
| | | 440 | | } |