< Summary

Information
Class: System.Net.Http.Headers.HttpGeneralHeaders
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpGeneralHeaders.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 86
Coverable lines: 86
Total lines: 172
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 44
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetConnectionClose(...)0%660%
GetTransferEncodingChunked(...)0%12120%
.ctor(...)100%110%
AddSpecialsFrom(...)0%440%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpGeneralHeaders.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;
 5
 6namespace System.Net.Http.Headers
 7{
 8    // The purpose of this type is to extract the handling of general headers in one place rather than duplicating
 9    // functionality in both HttpRequestHeaders and HttpResponseHeaders.
 10    internal sealed class HttpGeneralHeaders
 11    {
 12        private HttpHeaderValueCollection<string>? _connection;
 13        private HttpHeaderValueCollection<string>? _trailer;
 14        private HttpHeaderValueCollection<TransferCodingHeaderValue>? _transferEncoding;
 15        private HttpHeaderValueCollection<ProductHeaderValue>? _upgrade;
 16        private HttpHeaderValueCollection<ViaHeaderValue>? _via;
 17        private HttpHeaderValueCollection<WarningHeaderValue>? _warning;
 18        private HttpHeaderValueCollection<NameValueHeaderValue>? _pragma;
 19        private readonly HttpHeaders _parent;
 20        private bool _transferEncodingChunkedSet;
 21        private bool _connectionCloseSet;
 22
 23        public CacheControlHeaderValue? CacheControl
 24        {
 025            get { return (CacheControlHeaderValue?)_parent.GetSingleParsedValue(KnownHeaders.CacheControl.Descriptor); }
 026            set { _parent.SetOrRemoveParsedValue(KnownHeaders.CacheControl.Descriptor, value); }
 27        }
 28
 29        public bool? ConnectionClose
 30        {
 31            get
 032            {
 33                // Separated out into a static to enable access to TransferEncodingChunked
 34                // without the caller needing to force the creation of HttpGeneralHeaders
 35                // if it wasn't created for other reasons.
 036                return GetConnectionClose(_parent, this);
 037            }
 38            set
 039            {
 040                if (value == true)
 041                {
 042                    _connectionCloseSet = true;
 043                    if (!_parent.ContainsParsedValue(KnownHeaders.Connection.Descriptor, HeaderUtilities.ConnectionClose
 044                    {
 045                        _parent.AddParsedValue(KnownHeaders.Connection.Descriptor, HeaderUtilities.ConnectionClose);
 046                    }
 047                }
 48                else
 049                {
 050                    _connectionCloseSet = value != null;
 51                    // We intentionally ignore the return value. It's OK if "close" wasn't in the store.
 052                    _parent.RemoveParsedValue(KnownHeaders.Connection.Descriptor, HeaderUtilities.ConnectionClose);
 053                }
 054            }
 55        }
 56
 57        internal static bool? GetConnectionClose(HttpHeaders parent, HttpGeneralHeaders? headers)
 058        {
 059            if (parent.ContainsParsedValue(KnownHeaders.Connection.Descriptor, HeaderUtilities.ConnectionClose))
 060            {
 061                return true;
 62            }
 063            if (headers != null && headers._connectionCloseSet)
 064            {
 065                return false;
 66            }
 067            return null;
 068        }
 69
 70        public DateTimeOffset? Date
 71        {
 072            get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.Date.Descriptor, _parent); }
 073            set { _parent.SetOrRemoveParsedValue(KnownHeaders.Date.Descriptor, value); }
 74        }
 75
 76        public HttpHeaderValueCollection<NameValueHeaderValue> Pragma =>
 077            _pragma ??= new HttpHeaderValueCollection<NameValueHeaderValue>(KnownHeaders.Pragma.Descriptor, _parent);
 78
 79        public HttpHeaderValueCollection<string> Trailer =>
 080            _trailer ??= new HttpHeaderValueCollection<string>(KnownHeaders.Trailer.Descriptor, _parent);
 81
 82        internal static bool? GetTransferEncodingChunked(HttpHeaders parent, HttpGeneralHeaders? headers)
 083        {
 084            if (parent.TryGetHeaderValue(KnownHeaders.TransferEncoding.Descriptor, out object? value))
 085            {
 86                // Fast-path for the very common case where "chunked" is the only value.
 087                if (value is string stringValue && stringValue.Equals("chunked", StringComparison.OrdinalIgnoreCase))
 088                {
 089                    return true;
 90                }
 91
 092                if (parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodin
 093                {
 094                    return true;
 95                }
 096            }
 97
 098            if (headers != null && headers._transferEncodingChunkedSet)
 099            {
 0100                return false;
 101            }
 102
 0103            return null;
 0104        }
 105
 106        public bool? TransferEncodingChunked
 107        {
 108            get
 0109            {
 110                // Separated out into a static to enable access to TransferEncodingChunked
 111                // without the caller needing to force the creation of HttpGeneralHeaders
 112                // if it wasn't created for other reasons.
 0113                return GetTransferEncodingChunked(_parent, this);
 0114            }
 115            set
 0116            {
 0117                if (value == true)
 0118                {
 0119                    _transferEncodingChunkedSet = true;
 0120                    if (!_parent.ContainsParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferE
 0121                    {
 0122                        _parent.AddParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncodin
 0123                    }
 0124                }
 125                else
 0126                {
 0127                    _transferEncodingChunkedSet = value != null;
 128                    // Remove all occurrences of "chunked" in a single pass.
 0129                    _parent.RemoveParsedValue(KnownHeaders.TransferEncoding.Descriptor, HeaderUtilities.TransferEncoding
 0130                }
 0131            }
 132        }
 133
 134        public HttpHeaderValueCollection<ProductHeaderValue> Upgrade =>
 0135            _upgrade ??= new HttpHeaderValueCollection<ProductHeaderValue>(KnownHeaders.Upgrade.Descriptor, _parent);
 136
 137        public HttpHeaderValueCollection<ViaHeaderValue> Via =>
 0138            _via ??= new HttpHeaderValueCollection<ViaHeaderValue>(KnownHeaders.Via.Descriptor, _parent);
 139
 140        public HttpHeaderValueCollection<WarningHeaderValue> Warning =>
 0141            _warning ??= new HttpHeaderValueCollection<WarningHeaderValue>(KnownHeaders.Warning.Descriptor, _parent);
 142
 143        public HttpHeaderValueCollection<string> Connection =>
 0144            _connection ??= new HttpHeaderValueCollection<string>(KnownHeaders.Connection.Descriptor, _parent);
 145
 146        public HttpHeaderValueCollection<TransferCodingHeaderValue> TransferEncoding =>
 0147            _transferEncoding ??= new HttpHeaderValueCollection<TransferCodingHeaderValue>(KnownHeaders.TransferEncoding
 148
 0149        internal HttpGeneralHeaders(HttpHeaders parent)
 0150        {
 0151            Debug.Assert(parent != null);
 152
 0153            _parent = parent;
 0154        }
 155
 156        internal void AddSpecialsFrom(HttpGeneralHeaders sourceHeaders)
 0157        {
 158            // Copy special values, but do not overwrite
 0159            bool? chunked = TransferEncodingChunked;
 0160            if (!chunked.HasValue)
 0161            {
 0162                TransferEncodingChunked = sourceHeaders.TransferEncodingChunked;
 0163            }
 164
 0165            bool? close = ConnectionClose;
 0166            if (!close.HasValue)
 0167            {
 0168                ConnectionClose = sourceHeaders.ConnectionClose;
 0169            }
 0170        }
 171    }
 172}