< Summary

Information
Class: System.Net.Http.Headers.ByteArrayHeaderParser
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\ByteArrayHeaderParser.cs
Line coverage
89%
Covered lines: 25
Uncovered lines: 3
Coverable lines: 28
Total lines: 58
Line coverage: 89.2%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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%
ToString(...)100%11100%
TryParseValue(...)75%8885%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\ByteArrayHeaderParser.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.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6
 7namespace System.Net.Http.Headers
 8{
 9    // Don't derive from BaseHeaderParser since parsing the Base64 string is delegated to Convert.FromBase64String()
 10    // which will remove leading, trailing, and whitespace in the middle of the string.
 11    internal sealed class ByteArrayHeaderParser : HttpHeaderParser
 12    {
 113        internal static readonly ByteArrayHeaderParser Parser = new ByteArrayHeaderParser();
 14
 15        private ByteArrayHeaderParser()
 116            : base(false)
 117        {
 118        }
 19
 20        public override string ToString(object value)
 621        {
 622            Debug.Assert(value is byte[]);
 23
 624            return Convert.ToBase64String((byte[])value);
 625        }
 26
 27        public override bool TryParseValue([NotNullWhen(true)] string? value, object? storeValue, ref int index, [NotNul
 1828        {
 1829            parsedValue = null;
 30
 31            // Some headers support empty/null values. This one doesn't.
 1832            if (string.IsNullOrEmpty(value) || (index == value.Length))
 633            {
 634                return false;
 35            }
 36
 1237            string base64String = value;
 1238            if (index > 0)
 039            {
 040                base64String = value.Substring(index);
 041            }
 42
 43            // Try convert the string (we assume it's a valid Base64 string) to byte[].
 44            try
 1245            {
 1246                parsedValue = Convert.FromBase64String(base64String);
 447                index = value.Length;
 448                return true;
 49            }
 850            catch (FormatException e)
 851            {
 852                if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, SR.Format(SR.net_http_parser_invalid_base
 853            }
 54
 855            return false;
 1856        }
 57    }
 58}