| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.Headers |
| | | 9 | | { |
| | | 10 | | /// <summary>Provides a view on top of a <see cref="HttpHeaders"/> collection that avoids forcing validation or pars |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The view surfaces data as it's stored in the headers collection. Any header values that have not yet been parse |
| | | 13 | | /// as part of any accesses from this view, e.g. a raw header value of "one, two" that hasn't yet been parsed due to |
| | | 14 | | /// on the <see cref="HttpHeaders"/> will be surfaced as a single header value rather than two. For any header valu |
| | | 15 | | /// been parsed and validated, that value will be converted to a string to be returned from operations on this view. |
| | | 16 | | /// </remarks> |
| | | 17 | | public readonly struct HttpHeadersNonValidated : IReadOnlyDictionary<string, HeaderStringValues> |
| | | 18 | | { |
| | | 19 | | /// <summary>The wrapped headers collection.</summary> |
| | | 20 | | private readonly HttpHeaders? _headers; |
| | | 21 | | |
| | | 22 | | /// <summary>Initializes the view.</summary> |
| | | 23 | | /// <param name="headers">The wrapped headers collection.</param> |
| | 25107 | 24 | | internal HttpHeadersNonValidated(HttpHeaders headers) => _headers = headers; |
| | | 25 | | |
| | | 26 | | /// <summary>Gets the number of headers stored in the collection.</summary> |
| | | 27 | | /// <remarks>Multiple header values associated with the same header name are considered to be one header as far |
| | 0 | 28 | | public int Count => _headers?.Count ?? 0; |
| | | 29 | | |
| | | 30 | | /// <summary>Gets whether the collection contains the specified header.</summary> |
| | | 31 | | /// <param name="headerName">The name of the header.</param> |
| | | 32 | | /// <returns>true if the collection contains the header; otherwise, false.</returns> |
| | | 33 | | public bool Contains(string headerName) => |
| | 0 | 34 | | _headers is HttpHeaders headers && |
| | 0 | 35 | | headers.TryGetHeaderDescriptor(headerName, out HeaderDescriptor descriptor) && |
| | 0 | 36 | | headers.Contains(descriptor); |
| | | 37 | | |
| | | 38 | | /// <summary>Gets the values for the specified header name.</summary> |
| | | 39 | | /// <param name="headerName">The name of the header.</param> |
| | | 40 | | /// <returns>The values for the specified header.</returns> |
| | | 41 | | /// <exception cref="KeyNotFoundException">The header was not contained in the collection.</exception> |
| | | 42 | | public HeaderStringValues this[string headerName] |
| | | 43 | | { |
| | | 44 | | get |
| | 0 | 45 | | { |
| | 0 | 46 | | if (TryGetValues(headerName, out HeaderStringValues values)) |
| | 0 | 47 | | { |
| | 0 | 48 | | return values; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | throw new KeyNotFoundException(SR.net_http_headers_not_found); |
| | 0 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc/> |
| | 0 | 56 | | bool IReadOnlyDictionary<string, HeaderStringValues>.ContainsKey(string key) => Contains(key); |
| | | 57 | | |
| | | 58 | | /// <summary>Attempts to retrieve the values associated with the specified header name.</summary> |
| | | 59 | | /// <param name="headerName">The name of the header.</param> |
| | | 60 | | /// <param name="values">The retrieved header values.</param> |
| | | 61 | | /// <returns>true if the collection contains the specified header; otherwise, false.</returns> |
| | | 62 | | public bool TryGetValues(string headerName, out HeaderStringValues values) |
| | 0 | 63 | | { |
| | 0 | 64 | | if (_headers is HttpHeaders headers && |
| | 0 | 65 | | headers.TryGetHeaderDescriptor(headerName, out HeaderDescriptor descriptor) && |
| | 0 | 66 | | headers.TryGetHeaderValue(descriptor, out object? info)) |
| | 0 | 67 | | { |
| | 0 | 68 | | HttpHeaders.GetStoreValuesAsStringOrStringArray(descriptor, info, out string? singleValue, out string[]? |
| | 0 | 69 | | Debug.Assert(singleValue is not null ^ multiValue is not null); |
| | 0 | 70 | | values = singleValue is not null ? |
| | 0 | 71 | | new HeaderStringValues(descriptor, singleValue) : |
| | 0 | 72 | | new HeaderStringValues(descriptor, multiValue!); |
| | 0 | 73 | | return true; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | values = default; |
| | 0 | 77 | | return false; |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc/> |
| | 0 | 81 | | bool IReadOnlyDictionary<string, HeaderStringValues>.TryGetValue(string key, out HeaderStringValues value) => Tr |
| | | 82 | | |
| | | 83 | | /// <summary>Gets an enumerator that iterates through the <see cref="HttpHeadersNonValidated"/>.</summary> |
| | | 84 | | /// <returns>An enumerator that iterates through the <see cref="HttpHeadersNonValidated"/>.</returns> |
| | | 85 | | public Enumerator GetEnumerator() => |
| | 25107 | 86 | | _headers is HttpHeaders headers && headers.GetEntriesArray() is HeaderEntry[] entries ? |
| | 25107 | 87 | | new Enumerator(entries, headers.Count) : |
| | 25107 | 88 | | default; |
| | | 89 | | |
| | | 90 | | /// <inheritdoc/> |
| | 0 | 91 | | IEnumerator<KeyValuePair<string, HeaderStringValues>> IEnumerable<KeyValuePair<string, HeaderStringValues>>.GetE |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | 0 | 94 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 95 | | |
| | | 96 | | /// <inheritdoc/> |
| | | 97 | | IEnumerable<string> IReadOnlyDictionary<string, HeaderStringValues>.Keys |
| | | 98 | | { |
| | | 99 | | get |
| | 0 | 100 | | { |
| | 0 | 101 | | foreach (KeyValuePair<string, HeaderStringValues> header in this) |
| | 0 | 102 | | { |
| | 0 | 103 | | yield return header.Key; |
| | 0 | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc/> |
| | | 109 | | IEnumerable<HeaderStringValues> IReadOnlyDictionary<string, HeaderStringValues>.Values |
| | | 110 | | { |
| | | 111 | | get |
| | 0 | 112 | | { |
| | 0 | 113 | | foreach (KeyValuePair<string, HeaderStringValues> header in this) |
| | 0 | 114 | | { |
| | 0 | 115 | | yield return header.Value; |
| | 0 | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary>Enumerates the elements of a <see cref="HttpHeadersNonValidated"/>.</summary> |
| | | 121 | | public struct Enumerator : IEnumerator<KeyValuePair<string, HeaderStringValues>> |
| | | 122 | | { |
| | | 123 | | private readonly HeaderEntry[] _entries; |
| | | 124 | | private readonly int _numberOfEntries; |
| | | 125 | | private int _index; |
| | | 126 | | private KeyValuePair<string, HeaderStringValues> _current; |
| | | 127 | | |
| | | 128 | | internal Enumerator(HeaderEntry[] entries, int numberOfEntries) |
| | 25107 | 129 | | { |
| | 25107 | 130 | | _entries = entries; |
| | 25107 | 131 | | _numberOfEntries = numberOfEntries; |
| | 25107 | 132 | | _index = 0; |
| | 25107 | 133 | | _current = default; |
| | 25107 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc/> |
| | | 137 | | public bool MoveNext() |
| | 38851 | 138 | | { |
| | 38851 | 139 | | int index = _index; |
| | 38851 | 140 | | if (_entries is HeaderEntry[] entries && index < _numberOfEntries && (uint)index < (uint)entries.Length) |
| | 13744 | 141 | | { |
| | 13744 | 142 | | HeaderEntry entry = entries[index]; |
| | 13744 | 143 | | _index++; |
| | | 144 | | |
| | 13744 | 145 | | HttpHeaders.GetStoreValuesAsStringOrStringArray(entry.Key, entry.Value, out string? singleValue, out |
| | 13744 | 146 | | Debug.Assert(singleValue is not null ^ multiValue is not null); |
| | | 147 | | |
| | 13744 | 148 | | _current = new KeyValuePair<string, HeaderStringValues>( |
| | 13744 | 149 | | entry.Key.Name, |
| | 13744 | 150 | | singleValue is not null ? new HeaderStringValues(entry.Key, singleValue) : new HeaderStringValue |
| | 13744 | 151 | | return true; |
| | | 152 | | } |
| | | 153 | | |
| | 25107 | 154 | | _current = default; |
| | 25107 | 155 | | return false; |
| | 38851 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <inheritdoc/> |
| | 13744 | 159 | | public KeyValuePair<string, HeaderStringValues> Current => _current; |
| | | 160 | | |
| | | 161 | | /// <inheritdoc/> |
| | 0 | 162 | | object IEnumerator.Current => _current; |
| | | 163 | | |
| | | 164 | | /// <inheritdoc/> |
| | 50214 | 165 | | public void Dispose() { } |
| | | 166 | | |
| | | 167 | | /// <inheritdoc/> |
| | 0 | 168 | | void IEnumerator.Reset() => throw new NotSupportedException(); |
| | | 169 | | } |
| | | 170 | | } |
| | | 171 | | } |