| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Http.Headers |
| | | 8 | | { |
| | | 9 | | // This type is used for headers supporting a list of values. It essentially just forwards calls to |
| | | 10 | | // the actual header-store in HttpHeaders. |
| | | 11 | | // |
| | | 12 | | // This type can deal with a so called "special value": The RFC defines some headers which are collection of |
| | | 13 | | // values, but the RFC only defines 1 value, e.g. Transfer-Encoding: chunked, Connection: close, |
| | | 14 | | // Expect: 100-continue. |
| | | 15 | | // We expose strongly typed properties for these special values: TransferEncodingChunked, ConnectionClose, |
| | | 16 | | // ExpectContinue. |
| | | 17 | | // So we have 2 properties for each of these headers ('Transfer-Encoding' => TransferEncoding, |
| | | 18 | | // TransferEncodingChunked; 'Connection' => Connection, ConnectionClose; 'Expect' => Expect, ExpectContinue) |
| | | 19 | | // |
| | | 20 | | // The following solution was chosen: |
| | | 21 | | // - Keep HttpHeaders clean: HttpHeaders is unaware of these "special values"; it just stores the collection of |
| | | 22 | | // headers. |
| | | 23 | | // - It is the responsibility of "higher level" components (HttpHeaderValueCollection, HttpRequestHeaders, |
| | | 24 | | // HttpResponseHeaders) to deal with special values. |
| | | 25 | | // - HttpHeaderValueCollection can be configured with an IEqualityComparer and a "special value". |
| | | 26 | | // |
| | | 27 | | // Example: Server sends header "Transfer-Encoding: gzip, custom, chunked" to the client. |
| | | 28 | | // - HttpHeaders: HttpHeaders will have an entry in the header store for "Transfer-Encoding" with values |
| | | 29 | | // "gzip", "custom", "chunked" |
| | | 30 | | // - HttpGeneralHeaders: |
| | | 31 | | // - Property TransferEncoding: has three values "gzip", "custom", and "chunked" |
| | | 32 | | // - Property TransferEncodingChunked: is set to "true". |
| | | 33 | | public sealed class HttpHeaderValueCollection<T> : ICollection<T> where T : class |
| | | 34 | | { |
| | | 35 | | private readonly HeaderDescriptor _descriptor; |
| | | 36 | | private readonly HttpHeaders _store; |
| | | 37 | | |
| | | 38 | | public int Count |
| | | 39 | | { |
| | 0 | 40 | | get { return GetCount(); } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public bool IsReadOnly |
| | | 44 | | { |
| | 0 | 45 | | get { return false; } |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | internal HttpHeaderValueCollection(HeaderDescriptor descriptor, HttpHeaders store) |
| | 0 | 49 | | { |
| | 0 | 50 | | _store = store; |
| | 0 | 51 | | _descriptor = descriptor; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | public void Add(T item) |
| | 0 | 55 | | { |
| | 0 | 56 | | CheckValue(item); |
| | 0 | 57 | | _store.AddParsedValue(_descriptor, item); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public void ParseAdd(string? input) |
| | 0 | 61 | | { |
| | 0 | 62 | | _store.Add(_descriptor, input); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public bool TryParseAdd(string? input) |
| | 0 | 66 | | { |
| | 0 | 67 | | return _store.TryParseAndAddValue(_descriptor, input); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public void Clear() |
| | 0 | 71 | | { |
| | 0 | 72 | | _store.Remove(_descriptor); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public bool Contains(T item) |
| | 0 | 76 | | { |
| | 0 | 77 | | CheckValue(item); |
| | 0 | 78 | | return _store.ContainsParsedValue(_descriptor, item); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public void CopyTo(T[] array, int arrayIndex) |
| | 0 | 82 | | { |
| | 0 | 83 | | ArgumentNullException.ThrowIfNull(array); |
| | | 84 | | |
| | | 85 | | // Allow arrayIndex == array.Length in case our own collection is empty |
| | 0 | 86 | | ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex); |
| | 0 | 87 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length); |
| | | 88 | | |
| | 0 | 89 | | object? storeValue = _store.GetParsedAndInvalidValues(_descriptor); |
| | | 90 | | |
| | 0 | 91 | | if (storeValue == null) |
| | 0 | 92 | | { |
| | 0 | 93 | | return; |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | List<object>? storeValues = storeValue as List<object>; |
| | | 97 | | |
| | 0 | 98 | | if (storeValues == null) |
| | 0 | 99 | | { |
| | 0 | 100 | | if (storeValue is not HttpHeaders.InvalidValue) |
| | 0 | 101 | | { |
| | 0 | 102 | | Debug.Assert(storeValue is T); |
| | 0 | 103 | | if (arrayIndex == array.Length) |
| | 0 | 104 | | { |
| | 0 | 105 | | throw new ArgumentException(SR.net_http_copyto_array_too_small); |
| | | 106 | | } |
| | 0 | 107 | | array[arrayIndex] = (T)storeValue; |
| | 0 | 108 | | } |
| | 0 | 109 | | } |
| | | 110 | | else |
| | 0 | 111 | | { |
| | 0 | 112 | | foreach (object item in storeValues) |
| | 0 | 113 | | { |
| | 0 | 114 | | if (item is not HttpHeaders.InvalidValue) |
| | 0 | 115 | | { |
| | 0 | 116 | | Debug.Assert(item is T); |
| | 0 | 117 | | if (arrayIndex == array.Length) |
| | 0 | 118 | | { |
| | 0 | 119 | | throw new ArgumentException(SR.net_http_copyto_array_too_small); |
| | | 120 | | } |
| | 0 | 121 | | array[arrayIndex++] = (T)item; |
| | 0 | 122 | | } |
| | 0 | 123 | | } |
| | 0 | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | public bool Remove(T item) |
| | 0 | 128 | | { |
| | 0 | 129 | | CheckValue(item); |
| | 0 | 130 | | return _store.RemoveParsedValue(_descriptor, item); |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | #region IEnumerable<T> Members |
| | | 134 | | |
| | | 135 | | public IEnumerator<T> GetEnumerator() |
| | 0 | 136 | | { |
| | 0 | 137 | | object? storeValue = _store.GetParsedAndInvalidValues(_descriptor); |
| | 0 | 138 | | return storeValue is null || storeValue is HttpHeaders.InvalidValue ? |
| | 0 | 139 | | ((IEnumerable<T>)Array.Empty<T>()).GetEnumerator() : // use singleton empty array enumerator |
| | 0 | 140 | | Iterate(storeValue); |
| | | 141 | | |
| | | 142 | | static IEnumerator<T> Iterate(object storeValue) |
| | 0 | 143 | | { |
| | 0 | 144 | | if (storeValue is List<object> storeValues) |
| | 0 | 145 | | { |
| | | 146 | | // We have multiple values. Iterate through the values and return them. |
| | 0 | 147 | | foreach (object item in storeValues) |
| | 0 | 148 | | { |
| | 0 | 149 | | if (item is HttpHeaders.InvalidValue) |
| | 0 | 150 | | { |
| | 0 | 151 | | continue; |
| | | 152 | | } |
| | 0 | 153 | | Debug.Assert(item is T); |
| | 0 | 154 | | yield return (T)item; |
| | 0 | 155 | | } |
| | 0 | 156 | | } |
| | | 157 | | else |
| | 0 | 158 | | { |
| | 0 | 159 | | Debug.Assert(storeValue is T); |
| | 0 | 160 | | yield return (T)storeValue; |
| | 0 | 161 | | } |
| | 0 | 162 | | } |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | #endregion |
| | | 166 | | |
| | | 167 | | #region IEnumerable Members |
| | | 168 | | |
| | | 169 | | Collections.IEnumerator Collections.IEnumerable.GetEnumerator() |
| | 0 | 170 | | { |
| | 0 | 171 | | return GetEnumerator(); |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | #endregion |
| | | 175 | | |
| | | 176 | | public override string ToString() |
| | 0 | 177 | | { |
| | 0 | 178 | | return _store.GetHeaderString(_descriptor); |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | private void CheckValue(T item) |
| | 0 | 182 | | { |
| | 0 | 183 | | ArgumentNullException.ThrowIfNull(item); |
| | | 184 | | |
| | 0 | 185 | | if (_descriptor.Parser == GenericHeaderParser.TokenListParser) |
| | 0 | 186 | | { |
| | | 187 | | // The collection expects valid HTTP tokens, which are typed as string. |
| | | 188 | | // Unlike other parsed values (which are always valid by construction), |
| | | 189 | | // we can't assume the provided string is a valid token. So validate it before we use it. |
| | 0 | 190 | | Debug.Assert(typeof(T) == typeof(string)); |
| | 0 | 191 | | HeaderUtilities.CheckValidToken((string)(object)item, nameof(item)); |
| | 0 | 192 | | } |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | private int GetCount() |
| | 0 | 196 | | { |
| | | 197 | | // This is an O(n) operation. |
| | | 198 | | |
| | 0 | 199 | | object? storeValue = _store.GetParsedAndInvalidValues(_descriptor); |
| | | 200 | | |
| | 0 | 201 | | if (storeValue == null) |
| | 0 | 202 | | { |
| | 0 | 203 | | return 0; |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | List<object>? storeValues = storeValue as List<object>; |
| | | 207 | | |
| | 0 | 208 | | if (storeValues == null) |
| | 0 | 209 | | { |
| | 0 | 210 | | if (storeValue is not HttpHeaders.InvalidValue) |
| | 0 | 211 | | { |
| | 0 | 212 | | return 1; |
| | | 213 | | } |
| | 0 | 214 | | return 0; |
| | | 215 | | } |
| | | 216 | | else |
| | 0 | 217 | | { |
| | 0 | 218 | | int count = 0; |
| | 0 | 219 | | foreach (object item in storeValues) |
| | 0 | 220 | | { |
| | 0 | 221 | | if (item is not HttpHeaders.InvalidValue) |
| | 0 | 222 | | { |
| | 0 | 223 | | count++; |
| | 0 | 224 | | } |
| | 0 | 225 | | } |
| | 0 | 226 | | return count; |
| | | 227 | | } |
| | 0 | 228 | | } |
| | | 229 | | } |
| | | 230 | | } |