| | 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.Text; |
| | 8 | |
|
| | 9 | | #pragma warning disable IDE0016 // Null check can be simplified |
| | 10 | |
|
| | 11 | | namespace Azure.Core.Http.Multipart |
| | 12 | | { |
| | 13 | | internal class MultipartBoundary |
| | 14 | | { |
| 88 | 15 | | private readonly int[] _skipTable = new int[256]; |
| | 16 | | private readonly string _boundary; |
| | 17 | | private bool _expectLeadingCrlf; |
| | 18 | |
|
| 88 | 19 | | public MultipartBoundary(string boundary, bool expectLeadingCrlf = true) |
| | 20 | | { |
| 88 | 21 | | if (boundary == null) |
| | 22 | | { |
| 0 | 23 | | throw new ArgumentNullException(nameof(boundary)); |
| | 24 | | } |
| | 25 | |
|
| 88 | 26 | | _boundary = boundary; |
| 88 | 27 | | _expectLeadingCrlf = expectLeadingCrlf; |
| 88 | 28 | | Initialize(_boundary, _expectLeadingCrlf); |
| 88 | 29 | | } |
| | 30 | |
|
| | 31 | | private void Initialize(string boundary, bool expectLeadingCrlf) |
| | 32 | | { |
| 176 | 33 | | if (expectLeadingCrlf) |
| | 34 | | { |
| 88 | 35 | | BoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary); |
| | 36 | | } |
| | 37 | | else |
| | 38 | | { |
| 88 | 39 | | BoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary); |
| | 40 | | } |
| 176 | 41 | | FinalBoundaryLength = BoundaryBytes.Length + 2; // Include the final '--' terminator. |
| | 42 | |
|
| 176 | 43 | | var length = BoundaryBytes.Length; |
| 90464 | 44 | | for (var i = 0; i < _skipTable.Length; ++i) |
| | 45 | | { |
| 45056 | 46 | | _skipTable[i] = length; |
| | 47 | | } |
| 19008 | 48 | | for (var i = 0; i < length; ++i) |
| | 49 | | { |
| 9328 | 50 | | _skipTable[BoundaryBytes[i]] = Math.Max(1, length - 1 - i); |
| | 51 | | } |
| 176 | 52 | | } |
| | 53 | |
|
| | 54 | | public int GetSkipValue(byte input) |
| | 55 | | { |
| 7826 | 56 | | return _skipTable[input]; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public bool ExpectLeadingCrlf |
| | 60 | | { |
| 0 | 61 | | get { return _expectLeadingCrlf; } |
| | 62 | | set |
| | 63 | | { |
| 1272 | 64 | | if (value != _expectLeadingCrlf) |
| | 65 | | { |
| 88 | 66 | | _expectLeadingCrlf = value; |
| 88 | 67 | | Initialize(_boundary, _expectLeadingCrlf); |
| | 68 | | } |
| 1272 | 69 | | } |
| | 70 | | } |
| | 71 | |
|
| 13876 | 72 | | public byte[] BoundaryBytes { get; private set; } |
| | 73 | |
|
| 2836 | 74 | | public int FinalBoundaryLength { get; private set; } |
| | 75 | | } |
| | 76 | | } |