< Summary

Information
Class: System.Net.Http.Headers.HttpHeaderParser
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpHeaderParser.cs
Line coverage
90%
Covered lines: 30
Uncovered lines: 3
Coverable lines: 33
Total lines: 78
Line coverage: 90.9%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%22100%
ParseValue(...)62.5%88100%
ToString(...)100%11100%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpHeaderParser.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.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Text;
 8
 9namespace System.Net.Http.Headers
 10{
 11    internal abstract class HttpHeaderParser
 12    {
 13        public const string DefaultSeparator = ", ";
 114        public static readonly byte[] DefaultSeparatorBytes = ", "u8.ToArray();
 15
 293849516        public bool SupportsMultipleValues { get; }
 17
 018        public string Separator { get; }
 19
 020        public byte[] SeparatorBytes { get; }
 21
 22        // If ValueType implements Equals() as required, there is no need to provide a comparer. A comparer is needed
 23        // e.g. if we want to compare strings using case-insensitive comparison.
 024        public virtual IEqualityComparer? Comparer => null;
 25
 4226        protected HttpHeaderParser(bool supportsMultipleValues)
 4227        {
 4228            SupportsMultipleValues = supportsMultipleValues;
 4229            Separator = DefaultSeparator;
 4230            SeparatorBytes = DefaultSeparatorBytes;
 4231        }
 32
 333        protected HttpHeaderParser(bool supportsMultipleValues, string separator) : this(supportsMultipleValues)
 334        {
 335            Debug.Assert(!string.IsNullOrEmpty(separator));
 336            Debug.Assert(Ascii.IsValid(separator));
 37
 338            if (supportsMultipleValues)
 239            {
 240                Separator = separator;
 241                SeparatorBytes = Encoding.ASCII.GetBytes(separator);
 242            }
 343        }
 44
 45        // If a parser supports multiple values, a call to ParseValue/TryParseValue should return a value for 'index'
 46        // pointing to the next non-whitespace character after a delimiter. E.g. if called with a start index of 0
 47        // for string "value , second_value", then after the call completes, 'index' must point to 's', i.e. the first
 48        // non-whitespace after the separator ','.
 49        public abstract bool TryParseValue(string? value, object? storeValue, ref int index, [NotNullWhen(true)] out obj
 50
 51        public object ParseValue(string? value, object? storeValue, ref int index)
 51613852        {
 53            // Index may be value.Length (e.g. both 0). This may be allowed for some headers (e.g. Accept but not
 54            // allowed by others (e.g. Content-Length). The parser has to decide if this is valid or not.
 51613855            Debug.Assert((value == null) || ((index >= 0) && (index <= value.Length)));
 56
 57            // If a parser returns 'null', it means there was no value, but that's valid (e.g. "Accept: "). The caller
 58            // can ignore the value.
 51613859            if (!TryParseValue(value, storeValue, ref index, out object? result))
 754060            {
 754061                throw new FormatException(SR.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_heade
 754062                    value == null ? "<null>" : value.Substring(index)));
 63            }
 50859864            return result;
 50859865        }
 66
 67        // If ValueType is a custom header value type (e.g. NameValueHeaderValue) it already implements ToString() corre
 68        // However for existing types like int, byte[], DateTimeOffset we can't override ToString(). Therefore the
 69        // parser provides a ToString() virtual method that can be overridden by derived types to correctly serialize
 70        // values (e.g. byte[] to Base64 encoded string).
 71        public virtual string? ToString(object value)
 121642272        {
 121642273            Debug.Assert(value != null);
 74
 121642275            return value.ToString();
 121642276        }
 77    }
 78}