| | 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.Diagnostics; |
| | 9 | | using System.IO; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | |
|
| | 13 | | #pragma warning disable CA1820 // Use IsNullOrEmpty |
| | 14 | | #pragma warning disable IDE0008 // Use explicit type |
| | 15 | | #pragma warning disable IDE0016 // Simplify null check |
| | 16 | | #pragma warning disable IDE0018 // Inline declaration |
| | 17 | | #pragma warning disable IDE0054 // Use compound assignment |
| | 18 | | #pragma warning disable IDE0059 // Unnecessary assignment |
| | 19 | |
|
| | 20 | | namespace Azure.Core.Http.Multipart |
| | 21 | | { |
| | 22 | | internal sealed class MultipartReaderStream : Stream |
| | 23 | | { |
| | 24 | | private readonly MultipartBoundary _boundary; |
| | 25 | | private readonly BufferedReadStream _innerStream; |
| | 26 | | private readonly ArrayPool<byte> _bytePool; |
| | 27 | |
|
| | 28 | | private readonly long _innerOffset; |
| | 29 | | private long _position; |
| | 30 | | private long _observedLength; |
| | 31 | | private bool _finished; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Creates a stream that reads until it reaches the given boundary pattern. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="stream">The <see cref="BufferedReadStream"/>.</param> |
| | 37 | | /// <param name="boundary">The boundary pattern to use.</param> |
| | 38 | | public MultipartReaderStream(BufferedReadStream stream, MultipartBoundary boundary) |
| 1360 | 39 | | : this(stream, boundary, ArrayPool<byte>.Shared) |
| | 40 | | { |
| 1360 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Creates a stream that reads until it reaches the given boundary pattern. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="stream">The <see cref="BufferedReadStream"/>.</param> |
| | 47 | | /// <param name="boundary">The boundary pattern to use.</param> |
| | 48 | | /// <param name="bytePool">The ArrayPool pool to use for temporary byte arrays.</param> |
| 1360 | 49 | | public MultipartReaderStream(BufferedReadStream stream, MultipartBoundary boundary, ArrayPool<byte> bytePool) |
| | 50 | | { |
| 1360 | 51 | | if (stream == null) |
| | 52 | | { |
| 0 | 53 | | throw new ArgumentNullException(nameof(stream)); |
| | 54 | | } |
| | 55 | |
|
| 1360 | 56 | | if (boundary == null) |
| | 57 | | { |
| 0 | 58 | | throw new ArgumentNullException(nameof(boundary)); |
| | 59 | | } |
| | 60 | |
|
| 1360 | 61 | | _bytePool = bytePool; |
| 1360 | 62 | | _innerStream = stream; |
| 1360 | 63 | | _innerOffset = _innerStream.CanSeek ? _innerStream.Position : 0; |
| 1360 | 64 | | _boundary = boundary; |
| 1360 | 65 | | } |
| | 66 | |
|
| 1448 | 67 | | public bool FinalBoundaryFound { get; private set; } |
| | 68 | |
|
| 2660 | 69 | | public long? LengthLimit { get; set; } |
| | 70 | |
|
| | 71 | | public override bool CanRead |
| | 72 | | { |
| 1272 | 73 | | get { return true; } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public override bool CanSeek |
| | 77 | | { |
| 1272 | 78 | | get { return _innerStream.CanSeek; } |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public override bool CanWrite |
| | 82 | | { |
| 0 | 83 | | get { return false; } |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public override long Length |
| | 87 | | { |
| 1272 | 88 | | get { return _observedLength; } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | public override long Position |
| | 92 | | { |
| 1272 | 93 | | get { return _position; } |
| | 94 | | set |
| | 95 | | { |
| 0 | 96 | | if (value < 0) |
| | 97 | | { |
| 0 | 98 | | throw new ArgumentOutOfRangeException(nameof(value), value, "The Position must be positive."); |
| | 99 | | } |
| 0 | 100 | | if (value > _observedLength) |
| | 101 | | { |
| 0 | 102 | | throw new ArgumentOutOfRangeException(nameof(value), value, "The Position must be less than length." |
| | 103 | | } |
| 0 | 104 | | _position = value; |
| 0 | 105 | | if (_position < _observedLength) |
| | 106 | | { |
| 0 | 107 | | _finished = false; |
| | 108 | | } |
| 0 | 109 | | } |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public override long Seek(long offset, SeekOrigin origin) |
| | 113 | | { |
| 0 | 114 | | if (origin == SeekOrigin.Begin) |
| | 115 | | { |
| 0 | 116 | | Position = offset; |
| | 117 | | } |
| 0 | 118 | | else if (origin == SeekOrigin.Current) |
| | 119 | | { |
| 0 | 120 | | Position = Position + offset; |
| | 121 | | } |
| | 122 | | else // if (origin == SeekOrigin.End) |
| | 123 | | { |
| 0 | 124 | | Position = Length + offset; |
| | 125 | | } |
| 0 | 126 | | return Position; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | public override void SetLength(long value) |
| | 130 | | { |
| 0 | 131 | | throw new NotSupportedException(); |
| | 132 | | } |
| | 133 | |
|
| | 134 | | public override void Write(byte[] buffer, int offset, int count) |
| | 135 | | { |
| 0 | 136 | | throw new NotSupportedException(); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 140 | | { |
| 0 | 141 | | throw new NotSupportedException(); |
| | 142 | | } |
| | 143 | |
|
| | 144 | | public override void Flush() |
| | 145 | | { |
| | 146 | | // Flush is allowed on read-only stream |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | private void PositionInnerStream() |
| | 150 | | { |
| 2660 | 151 | | if (_innerStream.CanSeek && _innerStream.Position != (_innerOffset + _position)) |
| | 152 | | { |
| 0 | 153 | | _innerStream.Position = _innerOffset + _position; |
| | 154 | | } |
| 2660 | 155 | | } |
| | 156 | |
|
| | 157 | | private int UpdatePosition(int read) |
| | 158 | | { |
| 1300 | 159 | | _position += read; |
| 1300 | 160 | | if (_observedLength < _position) |
| | 161 | | { |
| 1300 | 162 | | _observedLength = _position; |
| 1300 | 163 | | if (LengthLimit.HasValue && _observedLength > LengthLimit.GetValueOrDefault()) |
| | 164 | | { |
| 0 | 165 | | throw new InvalidDataException($"Multipart body length limit {LengthLimit.GetValueOrDefault()} excee |
| | 166 | | } |
| | 167 | | } |
| 1300 | 168 | | return read; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | public override int Read(byte[] buffer, int offset, int count) |
| | 172 | | { |
| 1882 | 173 | | if (_finished) |
| | 174 | | { |
| 596 | 175 | | return 0; |
| | 176 | | } |
| | 177 | |
|
| 1286 | 178 | | PositionInnerStream(); |
| 1286 | 179 | | if (!_innerStream.EnsureBuffered(_boundary.FinalBoundaryLength)) |
| | 180 | | { |
| 0 | 181 | | throw new IOException("Unexpected end of Stream, the content may have already been read by another compo |
| | 182 | | } |
| 1286 | 183 | | var bufferedData = _innerStream.BufferedData; |
| | 184 | |
|
| | 185 | | // scan for a boundary match, full or partial. |
| | 186 | | int read; |
| 1286 | 187 | | if (SubMatch(bufferedData, _boundary.BoundaryBytes, out var matchOffset, out var matchCount)) |
| | 188 | | { |
| | 189 | | // We found a possible match, return any data before it. |
| 1280 | 190 | | if (matchOffset > bufferedData.Offset) |
| | 191 | | { |
| 644 | 192 | | read = _innerStream.Read(buffer, offset, Math.Min(count, matchOffset - bufferedData.Offset)); |
| 644 | 193 | | return UpdatePosition(read); |
| | 194 | | } |
| | 195 | |
|
| 636 | 196 | | var length = _boundary.BoundaryBytes.Length; |
| | 197 | | Debug.Assert(matchCount == length); |
| | 198 | |
|
| | 199 | | // "The boundary may be followed by zero or more characters of |
| | 200 | | // linear whitespace. It is then terminated by either another CRLF" |
| | 201 | | // or -- for the final boundary. |
| 636 | 202 | | var boundary = _bytePool.Rent(length); |
| 636 | 203 | | read = _innerStream.Read(boundary, 0, length); |
| 636 | 204 | | _bytePool.Return(boundary); |
| | 205 | | Debug.Assert(read == length); // It should have all been buffered |
| | 206 | |
|
| 636 | 207 | | var remainder = _innerStream.ReadLine(lengthLimit: 100); // Whitespace may exceed the buffer. |
| 636 | 208 | | remainder = remainder.Trim(); |
| 636 | 209 | | if (string.Equals("--", remainder, StringComparison.Ordinal)) |
| | 210 | | { |
| 44 | 211 | | FinalBoundaryFound = true; |
| | 212 | | } |
| | 213 | | Debug.Assert(FinalBoundaryFound || string.Equals(string.Empty, remainder, StringComparison.Ordinal), "Un |
| 636 | 214 | | _finished = true; |
| 636 | 215 | | return 0; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | // No possible boundary match within the buffered data, return the data from the buffer. |
| 6 | 219 | | read = _innerStream.Read(buffer, offset, Math.Min(count, bufferedData.Count)); |
| 6 | 220 | | return UpdatePosition(read); |
| | 221 | | } |
| | 222 | |
|
| | 223 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | 224 | | { |
| 3242 | 225 | | if (_finished) |
| | 226 | | { |
| 1868 | 227 | | return 0; |
| | 228 | | } |
| | 229 | |
|
| 1374 | 230 | | PositionInnerStream(); |
| 1374 | 231 | | if (!await _innerStream.EnsureBufferedAsync(_boundary.FinalBoundaryLength, cancellationToken).ConfigureAwait |
| | 232 | | { |
| 0 | 233 | | throw new IOException("Unexpected end of Stream, the content may have already been read by another compo |
| | 234 | | } |
| 1374 | 235 | | var bufferedData = _innerStream.BufferedData; |
| | 236 | |
|
| | 237 | | // scan for a boundary match, full or partial. |
| | 238 | | int matchOffset; |
| | 239 | | int matchCount; |
| | 240 | | int read; |
| 1374 | 241 | | if (SubMatch(bufferedData, _boundary.BoundaryBytes, out matchOffset, out matchCount)) |
| | 242 | | { |
| | 243 | | // We found a possible match, return any data before it. |
| 1368 | 244 | | if (matchOffset > bufferedData.Offset) |
| | 245 | | { |
| | 246 | | // Sync, it's already buffered |
| 644 | 247 | | read = _innerStream.Read(buffer, offset, Math.Min(count, matchOffset - bufferedData.Offset)); |
| 644 | 248 | | return UpdatePosition(read); |
| | 249 | | } |
| | 250 | |
|
| 724 | 251 | | var length = _boundary.BoundaryBytes.Length; |
| | 252 | | Debug.Assert(matchCount == length); |
| | 253 | |
|
| | 254 | | // "The boundary may be followed by zero or more characters of |
| | 255 | | // linear whitespace. It is then terminated by either another CRLF" |
| | 256 | | // or -- for the final boundary. |
| 724 | 257 | | var boundary = _bytePool.Rent(length); |
| 724 | 258 | | read = _innerStream.Read(boundary, 0, length); |
| 724 | 259 | | _bytePool.Return(boundary); |
| | 260 | | Debug.Assert(read == length); // It should have all been buffered |
| | 261 | |
|
| 724 | 262 | | var remainder = await _innerStream.ReadLineAsync(lengthLimit: 100, cancellationToken: cancellationToken) |
| 724 | 263 | | remainder = remainder.Trim(); |
| 724 | 264 | | if (string.Equals("--", remainder, StringComparison.Ordinal)) |
| | 265 | | { |
| 44 | 266 | | FinalBoundaryFound = true; |
| | 267 | | } |
| | 268 | | Debug.Assert(FinalBoundaryFound || string.Equals(string.Empty, remainder, StringComparison.Ordinal), "Un |
| | 269 | |
|
| 724 | 270 | | _finished = true; |
| 724 | 271 | | return 0; |
| | 272 | | } |
| | 273 | |
|
| | 274 | | // No possible boundary match within the buffered data, return the data from the buffer. |
| 6 | 275 | | read = _innerStream.Read(buffer, offset, Math.Min(count, bufferedData.Count)); |
| 6 | 276 | | return UpdatePosition(read); |
| 3242 | 277 | | } |
| | 278 | |
|
| | 279 | | // Does segment1 contain all of matchBytes, or does it end with the start of matchBytes? |
| | 280 | | // 1: AAAAABBBBBCCCCC |
| | 281 | | // 2: BBBBB |
| | 282 | | // Or: |
| | 283 | | // 1: AAAAABBB |
| | 284 | | // 2: BBBBB |
| | 285 | | private bool SubMatch(ArraySegment<byte> segment1, byte[] matchBytes, out int matchOffset, out int matchCount) |
| | 286 | | { |
| | 287 | | // clear matchCount to zero |
| 2660 | 288 | | matchCount = 0; |
| | 289 | |
|
| | 290 | | // case 1: does segment1 fully contain matchBytes? |
| | 291 | | { |
| 2660 | 292 | | var matchBytesLengthMinusOne = matchBytes.Length - 1; |
| 2660 | 293 | | var matchBytesLastByte = matchBytes[matchBytesLengthMinusOne]; |
| 2660 | 294 | | var segmentEndMinusMatchBytesLength = segment1.Offset + segment1.Count - matchBytes.Length; |
| | 295 | |
|
| 2660 | 296 | | matchOffset = segment1.Offset; |
| 10486 | 297 | | while (matchOffset < segmentEndMinusMatchBytesLength) |
| | 298 | | { |
| 10442 | 299 | | var lookaheadTailChar = segment1.Array[matchOffset + matchBytesLengthMinusOne]; |
| 10442 | 300 | | if (lookaheadTailChar == matchBytesLastByte && |
| 10442 | 301 | | CompareBuffers(segment1.Array, matchOffset, matchBytes, 0, matchBytesLengthMinusOne) == 0) |
| | 302 | | { |
| 2616 | 303 | | matchCount = matchBytes.Length; |
| 2616 | 304 | | return true; |
| | 305 | | } |
| 7826 | 306 | | matchOffset += _boundary.GetSkipValue(lookaheadTailChar); |
| | 307 | | } |
| | 308 | | } |
| | 309 | |
|
| | 310 | | // case 2: does segment1 end with the start of matchBytes? |
| 44 | 311 | | var segmentEnd = segment1.Offset + segment1.Count; |
| | 312 | |
|
| 44 | 313 | | matchCount = 0; |
| 2136 | 314 | | for (; matchOffset < segmentEnd; matchOffset++) |
| | 315 | | { |
| 1078 | 316 | | var countLimit = segmentEnd - matchOffset; |
| 3564 | 317 | | for (matchCount = 0; matchCount < matchBytes.Length && matchCount < countLimit; matchCount++) |
| | 318 | | { |
| 1750 | 319 | | if (matchBytes[matchCount] != segment1.Array[matchOffset + matchCount]) |
| | 320 | | { |
| 1046 | 321 | | matchCount = 0; |
| 1046 | 322 | | break; |
| | 323 | | } |
| | 324 | | } |
| 1078 | 325 | | if (matchCount > 0) |
| | 326 | | { |
| | 327 | | break; |
| | 328 | | } |
| | 329 | | } |
| 44 | 330 | | return matchCount > 0; |
| | 331 | | } |
| | 332 | |
|
| | 333 | | private static int CompareBuffers(byte[] buffer1, int offset1, byte[] buffer2, int offset2, int count) |
| | 334 | | { |
| 418154 | 335 | | for (; count-- > 0; offset1++, offset2++) |
| | 336 | | { |
| 138578 | 337 | | if (buffer1[offset1] != buffer2[offset2]) |
| | 338 | | { |
| 98 | 339 | | return buffer1[offset1] - buffer2[offset2]; |
| | 340 | | } |
| | 341 | | } |
| 2616 | 342 | | return 0; |
| | 343 | | } |
| | 344 | | } |
| | 345 | | } |