| | 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 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics.Contracts; |
| | 9 | | using System.Globalization; |
| | 10 | |
|
| | 11 | | #pragma warning disable IDE0018 // Inline declaration |
| | 12 | | #pragma warning disable IDE0032 // Use auto property |
| | 13 | | #pragma warning disable IDE0044 // Make field readonly |
| | 14 | |
|
| | 15 | | namespace Azure.Core.Http.Multipart |
| | 16 | | { |
| | 17 | | internal abstract class HttpHeaderParser<T> |
| | 18 | | { |
| | 19 | | private bool _supportsMultipleValues; |
| | 20 | |
|
| 0 | 21 | | protected HttpHeaderParser(bool supportsMultipleValues) |
| | 22 | | { |
| 0 | 23 | | _supportsMultipleValues = supportsMultipleValues; |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public bool SupportsMultipleValues |
| | 27 | | { |
| 0 | 28 | | get { return _supportsMultipleValues; } |
| | 29 | | } |
| | 30 | |
|
| | 31 | | // If a parser supports multiple values, a call to ParseValue/TryParseValue should return a value for 'index' |
| | 32 | | // pointing to the next non-whitespace character after a delimiter. E.g. if called with a start index of 0 |
| | 33 | | // for string "value , second_value", then after the call completes, 'index' must point to 's', i.e. the first |
| | 34 | | // non-whitespace after the separator ','. |
| | 35 | | public abstract bool TryParseValue(StringSegment value, ref int index, out T parsedValue); |
| | 36 | |
|
| | 37 | | public T ParseValue(StringSegment value, ref int index) |
| | 38 | | { |
| | 39 | | // Index may be value.Length (e.g. both 0). This may be allowed for some headers (e.g. Accept but not |
| | 40 | | // allowed by others (e.g. Content-Length). The parser has to decide if this is valid or not. |
| | 41 | | Contract.Requires((value == null) || ((index >= 0) && (index <= value.Length))); |
| | 42 | |
|
| | 43 | | // If a parser returns 'null', it means there was no value, but that's valid (e.g. "Accept: "). The caller |
| | 44 | | // can ignore the value. |
| | 45 | | T result; |
| 0 | 46 | | if (!TryParseValue(value, ref index, out result)) |
| | 47 | | { |
| 0 | 48 | | throw new FormatException(string.Format(CultureInfo.InvariantCulture, |
| 0 | 49 | | "The header contains invalid values at index {0}: '{1}'", index, value.Value ?? "<null>")); |
| | 50 | | } |
| 0 | 51 | | return result; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | public virtual bool TryParseValues(IList<string> values, out IList<T> parsedValues) |
| | 55 | | { |
| 0 | 56 | | return TryParseValues(values, strict: false, parsedValues: out parsedValues); |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public virtual bool TryParseStrictValues(IList<string> values, out IList<T> parsedValues) |
| | 60 | | { |
| 0 | 61 | | return TryParseValues(values, strict: true, parsedValues: out parsedValues); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | protected virtual bool TryParseValues(IList<string> values, bool strict, out IList<T> parsedValues) |
| | 65 | | { |
| | 66 | | Contract.Assert(_supportsMultipleValues); |
| | 67 | | // If a parser returns an empty list, it means there was no value, but that's valid (e.g. "Accept: "). The c |
| | 68 | | // can ignore the value. |
| 0 | 69 | | parsedValues = null; |
| 0 | 70 | | List<T> results = null; |
| 0 | 71 | | if (values == null) |
| | 72 | | { |
| 0 | 73 | | return false; |
| | 74 | | } |
| 0 | 75 | | for (var i = 0; i < values.Count; i++) |
| | 76 | | { |
| 0 | 77 | | var value = values[i]; |
| 0 | 78 | | int index = 0; |
| | 79 | |
|
| 0 | 80 | | while (!string.IsNullOrEmpty(value) && index < value.Length) |
| | 81 | | { |
| | 82 | | T output; |
| 0 | 83 | | if (TryParseValue(value, ref index, out output)) |
| | 84 | | { |
| | 85 | | // The entry may not contain an actual value, like " , " |
| 0 | 86 | | if (output != null) |
| | 87 | | { |
| 0 | 88 | | if (results == null) |
| | 89 | | { |
| 0 | 90 | | results = new List<T>(); // Allocate it only when used |
| | 91 | | } |
| 0 | 92 | | results.Add(output); |
| | 93 | | } |
| | 94 | | } |
| 0 | 95 | | else if (strict) |
| | 96 | | { |
| 0 | 97 | | return false; |
| | 98 | | } |
| | 99 | | else |
| | 100 | | { |
| | 101 | | // Skip the invalid values and keep trying. |
| 0 | 102 | | index++; |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |
| 0 | 106 | | if (results != null) |
| | 107 | | { |
| 0 | 108 | | parsedValues = results; |
| 0 | 109 | | return true; |
| | 110 | | } |
| 0 | 111 | | return false; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | public virtual IList<T> ParseValues(IList<string> values) |
| | 115 | | { |
| 0 | 116 | | return ParseValues(values, strict: false); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | public virtual IList<T> ParseStrictValues(IList<string> values) |
| | 120 | | { |
| 0 | 121 | | return ParseValues(values, strict: true); |
| | 122 | | } |
| | 123 | |
|
| | 124 | | protected virtual IList<T> ParseValues(IList<string> values, bool strict) |
| | 125 | | { |
| | 126 | | Contract.Assert(_supportsMultipleValues); |
| | 127 | | // If a parser returns an empty list, it means there was no value, but that's valid (e.g. "Accept: "). The c |
| | 128 | | // can ignore the value. |
| 0 | 129 | | var parsedValues = new List<T>(); |
| 0 | 130 | | if (values == null) |
| | 131 | | { |
| 0 | 132 | | return parsedValues; |
| | 133 | | } |
| 0 | 134 | | foreach (var value in values) |
| | 135 | | { |
| 0 | 136 | | int index = 0; |
| | 137 | |
|
| 0 | 138 | | while (!string.IsNullOrEmpty(value) && index < value.Length) |
| | 139 | | { |
| | 140 | | T output; |
| 0 | 141 | | if (TryParseValue(value, ref index, out output)) |
| | 142 | | { |
| | 143 | | // The entry may not contain an actual value, like " , " |
| 0 | 144 | | if (output != null) |
| | 145 | | { |
| 0 | 146 | | parsedValues.Add(output); |
| | 147 | | } |
| | 148 | | } |
| 0 | 149 | | else if (strict) |
| | 150 | | { |
| 0 | 151 | | throw new FormatException(string.Format(CultureInfo.InvariantCulture, |
| 0 | 152 | | "The header contains invalid values at index {0}: '{1}'", index, value)); |
| | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| | 156 | | // Skip the invalid values and keep trying. |
| 0 | 157 | | index++; |
| | 158 | | } |
| | 159 | | } |
| | 160 | | } |
| 0 | 161 | | return parsedValues; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // If ValueType is a custom header value type (e.g. NameValueHeaderValue) it implements ToString() correctly. |
| | 165 | | // However for existing types like int, byte[], DateTimeOffset we can't override ToString(). Therefore the |
| | 166 | | // parser provides a ToString() virtual method that can be overridden by derived types to correctly serialize |
| | 167 | | // values (e.g. byte[] to Base64 encoded string). |
| | 168 | | // The default implementation is to just call ToString() on the value itself which is the right thing to do |
| | 169 | | // for most headers (custom types, string, etc.). |
| | 170 | | public virtual string ToString(object value) |
| | 171 | | { |
| | 172 | | Contract.Requires(value != null); |
| | 173 | |
|
| 0 | 174 | | return value.ToString(); |
| | 175 | | } |
| | 176 | | } |
| | 177 | | } |