< Summary

Information
Class: System.Net.Http.Headers.HttpHeadersNonValidated
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpHeadersNonValidated.cs
Line coverage
39%
Covered lines: 27
Uncovered lines: 42
Coverable lines: 69
Total lines: 171
Line coverage: 39.1%
Branch coverage
31%
Covered branches: 10
Total branches: 32
Branch coverage: 31.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpHeadersNonValidated.cs

#LineLine coverage
 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
 4using System.Collections;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7
 8namespace 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>
 2510724        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 
 028        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) =>
 034            _headers is HttpHeaders headers &&
 035            headers.TryGetHeaderDescriptor(headerName, out HeaderDescriptor descriptor) &&
 036            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
 045            {
 046                if (TryGetValues(headerName, out HeaderStringValues values))
 047                {
 048                    return values;
 49                }
 50
 051                throw new KeyNotFoundException(SR.net_http_headers_not_found);
 052            }
 53        }
 54
 55        /// <inheritdoc/>
 056        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)
 063        {
 064            if (_headers is HttpHeaders headers &&
 065                headers.TryGetHeaderDescriptor(headerName, out HeaderDescriptor descriptor) &&
 066                headers.TryGetHeaderValue(descriptor, out object? info))
 067            {
 068                HttpHeaders.GetStoreValuesAsStringOrStringArray(descriptor, info, out string? singleValue, out string[]?
 069                Debug.Assert(singleValue is not null ^ multiValue is not null);
 070                values = singleValue is not null ?
 071                    new HeaderStringValues(descriptor, singleValue) :
 072                    new HeaderStringValues(descriptor, multiValue!);
 073                return true;
 74            }
 75
 076            values = default;
 077            return false;
 078        }
 79
 80        /// <inheritdoc/>
 081        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() =>
 2510786            _headers is HttpHeaders headers && headers.GetEntriesArray() is HeaderEntry[] entries ?
 2510787                new Enumerator(entries, headers.Count) :
 2510788                default;
 89
 90        /// <inheritdoc/>
 091        IEnumerator<KeyValuePair<string, HeaderStringValues>> IEnumerable<KeyValuePair<string, HeaderStringValues>>.GetE
 92
 93        /// <inheritdoc/>
 094        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 95
 96        /// <inheritdoc/>
 97        IEnumerable<string> IReadOnlyDictionary<string, HeaderStringValues>.Keys
 98        {
 99            get
 0100            {
 0101                foreach (KeyValuePair<string, HeaderStringValues> header in this)
 0102                {
 0103                    yield return header.Key;
 0104                }
 0105            }
 106        }
 107
 108        /// <inheritdoc/>
 109        IEnumerable<HeaderStringValues> IReadOnlyDictionary<string, HeaderStringValues>.Values
 110        {
 111            get
 0112            {
 0113                foreach (KeyValuePair<string, HeaderStringValues> header in this)
 0114                {
 0115                    yield return header.Value;
 0116                }
 0117            }
 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)
 25107129            {
 25107130                _entries = entries;
 25107131                _numberOfEntries = numberOfEntries;
 25107132                _index = 0;
 25107133                _current = default;
 25107134            }
 135
 136            /// <inheritdoc/>
 137            public bool MoveNext()
 38851138            {
 38851139                int index = _index;
 38851140                if (_entries is HeaderEntry[] entries && index < _numberOfEntries && (uint)index < (uint)entries.Length)
 13744141                {
 13744142                    HeaderEntry entry = entries[index];
 13744143                    _index++;
 144
 13744145                    HttpHeaders.GetStoreValuesAsStringOrStringArray(entry.Key, entry.Value, out string? singleValue, out
 13744146                    Debug.Assert(singleValue is not null ^ multiValue is not null);
 147
 13744148                    _current = new KeyValuePair<string, HeaderStringValues>(
 13744149                        entry.Key.Name,
 13744150                        singleValue is not null ? new HeaderStringValues(entry.Key, singleValue) : new HeaderStringValue
 13744151                    return true;
 152                }
 153
 25107154                _current = default;
 25107155                return false;
 38851156            }
 157
 158            /// <inheritdoc/>
 13744159            public KeyValuePair<string, HeaderStringValues> Current => _current;
 160
 161            /// <inheritdoc/>
 0162            object IEnumerator.Current => _current;
 163
 164            /// <inheritdoc/>
 50214165            public void Dispose() { }
 166
 167            /// <inheritdoc/>
 0168            void IEnumerator.Reset() => throw new NotSupportedException();
 169        }
 170    }
 171}