| | 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/Headers/src |
| | 5 | |
|
| | 6 | | #pragma warning disable IDE0018 // Inline declaration |
| | 7 | | #pragma warning disable IDE0034 // default can be simplified |
| | 8 | | #pragma warning disable IDE0054 // Use compound assignment |
| | 9 | | #pragma warning disable IDE0059 // Unnecessary assignment |
| | 10 | |
|
| | 11 | | namespace Azure.Core.Http.Multipart |
| | 12 | | { |
| | 13 | | internal abstract class BaseHeaderParser<T> : HttpHeaderParser<T> |
| | 14 | | { |
| | 15 | | protected BaseHeaderParser(bool supportsMultipleValues) |
| 0 | 16 | | : base(supportsMultipleValues) |
| | 17 | | { |
| 0 | 18 | | } |
| | 19 | |
|
| | 20 | | protected abstract int GetParsedValueLength(StringSegment value, int startIndex, out T parsedValue); |
| | 21 | |
|
| | 22 | | public sealed override bool TryParseValue(StringSegment value, ref int index, out T parsedValue) |
| | 23 | | { |
| 0 | 24 | | parsedValue = default(T); |
| | 25 | |
|
| | 26 | | // If multiple values are supported (i.e. list of values), then accept an empty string: The header may |
| | 27 | | // be added multiple times to the request/response message. E.g. |
| | 28 | | // Accept: text/xml; q=1 |
| | 29 | | // Accept: |
| | 30 | | // Accept: text/plain; q=0.2 |
| 0 | 31 | | if (StringSegment.IsNullOrEmpty(value) || (index == value.Length)) |
| | 32 | | { |
| 0 | 33 | | return SupportsMultipleValues; |
| | 34 | | } |
| | 35 | |
|
| 0 | 36 | | var separatorFound = false; |
| 0 | 37 | | var current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, index, SupportsMultipleValues, |
| 0 | 38 | | out separatorFound); |
| | 39 | |
|
| 0 | 40 | | if (separatorFound && !SupportsMultipleValues) |
| | 41 | | { |
| 0 | 42 | | return false; // leading separators not allowed if we don't support multiple values. |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | if (current == value.Length) |
| | 46 | | { |
| 0 | 47 | | if (SupportsMultipleValues) |
| | 48 | | { |
| 0 | 49 | | index = current; |
| | 50 | | } |
| 0 | 51 | | return SupportsMultipleValues; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | T result; |
| 0 | 55 | | var length = GetParsedValueLength(value, current, out result); |
| | 56 | |
|
| 0 | 57 | | if (length == 0) |
| | 58 | | { |
| 0 | 59 | | return false; |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | current = current + length; |
| 0 | 63 | | current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, current, SupportsMultipleValues, |
| 0 | 64 | | out separatorFound); |
| | 65 | |
|
| | 66 | | // If we support multiple values and we've not reached the end of the string, then we must have a separator. |
| 0 | 67 | | if ((separatorFound && !SupportsMultipleValues) || (!separatorFound && (current < value.Length))) |
| | 68 | | { |
| 0 | 69 | | return false; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | index = current; |
| 0 | 73 | | parsedValue = result; |
| 0 | 74 | | return true; |
| | 75 | | } |
| | 76 | | } |
| | 77 | | } |