< Summary

Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 85
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
TryParseValue(...)100%2222100%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/Headers/BaseHeaderParser.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;
 5using System.Diagnostics.CodeAnalysis;
 6
 7namespace System.Net.Http.Headers
 8{
 9    internal abstract class BaseHeaderParser : HttpHeaderParser
 10    {
 11        protected BaseHeaderParser(bool supportsMultipleValues)
 3612            : base(supportsMultipleValues)
 3613        {
 3614        }
 15
 16        /// <summary>
 17        /// Parses a full header or a segment of a multi-value header.
 18        /// </summary>
 19        /// <param name="value">The header value string to parse.</param>
 20        /// <param name="startIndex">The index to begin parsing at.</param>
 21        /// <param name="storeValue"></param>
 22        /// <param name="parsedValue">The resulting value parsed.</param>
 23        /// <returns>If a value could be parsed, the number of characters used to parse that value. Otherwise, 0.</retur
 24        protected abstract int GetParsedValueLength(string value, int startIndex, object? storeValue,
 25            out object? parsedValue);
 26
 27#pragma warning disable CS8765 // Doesn't match overridden member nullable attribute on out parameter
 28        public sealed override bool TryParseValue(string? value, object? storeValue, ref int index,
 29            out object? parsedValue)
 30#pragma warning restore CS8765
 17962631        {
 17962632            parsedValue = null;
 33
 34            // If multiple values are supported (i.e. list of values), then accept an empty string: The header may
 35            // be added multiple times to the request/response message. E.g.
 36            //  Accept: text/xml; q=1
 37            //  Accept:
 38            //  Accept: text/plain; q=0.2
 17962639            if (string.IsNullOrEmpty(value) || (index == value.Length))
 25240            {
 25241                return SupportsMultipleValues;
 42            }
 43
 44            bool separatorFound;
 17937445            int current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, index, SupportsMultipleValues,
 17937446                out separatorFound);
 47
 17937448            if (separatorFound && !SupportsMultipleValues)
 449            {
 450                return false; // leading separators not allowed if we don't support multiple values.
 51            }
 52
 17937053            if (current == value.Length)
 11554            {
 11555                if (SupportsMultipleValues)
 10556                {
 10557                    index = current;
 10558                }
 11559                return SupportsMultipleValues;
 60            }
 61
 17925562            int length = GetParsedValueLength(value, current, storeValue, out object? result);
 63
 17925564            if (length == 0)
 438665            {
 438666                return false;
 67            }
 68
 17486969            current += length;
 17486970            current = HeaderUtilities.GetNextNonEmptyOrWhitespaceIndex(value, current, SupportsMultipleValues,
 17486971                out separatorFound);
 72
 73            // If we support multiple values and we've not reached the end of the string, then we must have a separator.
 17486974            if ((separatorFound && !SupportsMultipleValues) || (!separatorFound && (current < value.Length)))
 151075            {
 151076                return false;
 77            }
 78
 17335979            index = current;
 17335980            parsedValue = result!;
 17335981            return true;
 17962682        }
 83    }
 84}
 85