< Summary

Information
Class: System.Net.Http.Headers.HttpContentHeaders
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpContentHeaders.cs
Line coverage
9%
Covered lines: 4
Uncovered lines: 37
Coverable lines: 41
Total lines: 115
Line coverage: 9.7%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\Headers\HttpContentHeaders.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.Generic;
 5
 6namespace System.Net.Http.Headers
 7{
 8    public sealed class HttpContentHeaders : HttpHeaders
 9    {
 10        internal HttpContent _parent;
 11        private bool _contentLengthSet;
 12
 13        private HttpHeaderValueCollection<string>? _allow;
 14        private HttpHeaderValueCollection<string>? _contentEncoding;
 15        private HttpHeaderValueCollection<string>? _contentLanguage;
 16
 17        public ICollection<string> Allow =>
 018            _allow ??= new HttpHeaderValueCollection<string>(KnownHeaders.Allow.Descriptor, this);
 19
 20        public ContentDispositionHeaderValue? ContentDisposition
 21        {
 022            get { return (ContentDispositionHeaderValue?)GetSingleParsedValue(KnownHeaders.ContentDisposition.Descriptor
 023            set { SetOrRemoveParsedValue(KnownHeaders.ContentDisposition.Descriptor, value); }
 24        }
 25
 26        // Must be a collection (and not provide properties like "GZip", "Deflate", etc.) since the
 27        // order matters!
 28        public ICollection<string> ContentEncoding =>
 029            _contentEncoding ??= new HttpHeaderValueCollection<string>(KnownHeaders.ContentEncoding.Descriptor, this);
 30
 31        public ICollection<string> ContentLanguage =>
 032            _contentLanguage ??= new HttpHeaderValueCollection<string>(KnownHeaders.ContentLanguage.Descriptor, this);
 33
 34        public long? ContentLength
 35        {
 36            get
 037            {
 38                // 'Content-Length' can only hold one value. So either we get 'null' back or a boxed long value.
 039                object? storedValue = GetSingleParsedValue(KnownHeaders.ContentLength.Descriptor);
 40
 41                // Only try to calculate the length if the user didn't set the value explicitly using the setter.
 042                if (!_contentLengthSet && (storedValue == null))
 043                {
 44                    // If we don't have a value for Content-Length in the store, try to let the content calculate
 45                    // it's length. If the content object is able to calculate the length, we'll store it in the
 46                    // store.
 047                    long? calculatedLength = _parent.GetComputedOrBufferLength();
 48
 049                    if (calculatedLength != null)
 050                    {
 051                        SetParsedValue(KnownHeaders.ContentLength.Descriptor, (object)calculatedLength.Value);
 052                    }
 53
 054                    return calculatedLength;
 55                }
 56
 057                if (storedValue == null)
 058                {
 059                    return null;
 60                }
 61                else
 062                {
 063                    return (long)storedValue;
 64                }
 065            }
 66            set
 067            {
 068                SetOrRemoveParsedValue(KnownHeaders.ContentLength.Descriptor, value); // box long value
 069                _contentLengthSet = true;
 070            }
 71        }
 72
 73        public Uri? ContentLocation
 74        {
 075            get { return (Uri?)GetSingleParsedValue(KnownHeaders.ContentLocation.Descriptor); }
 076            set { SetOrRemoveParsedValue(KnownHeaders.ContentLocation.Descriptor, value); }
 77        }
 78
 79        public byte[]? ContentMD5
 80        {
 081            get { return (byte[]?)GetSingleParsedValue(KnownHeaders.ContentMD5.Descriptor); }
 082            set { SetOrRemoveParsedValue(KnownHeaders.ContentMD5.Descriptor, value); }
 83        }
 84
 85        public ContentRangeHeaderValue? ContentRange
 86        {
 087            get { return (ContentRangeHeaderValue?)GetSingleParsedValue(KnownHeaders.ContentRange.Descriptor); }
 088            set { SetOrRemoveParsedValue(KnownHeaders.ContentRange.Descriptor, value); }
 89        }
 90
 91        public MediaTypeHeaderValue? ContentType
 92        {
 093            get { return (MediaTypeHeaderValue?)GetSingleParsedValue(KnownHeaders.ContentType.Descriptor); }
 094            set { SetOrRemoveParsedValue(KnownHeaders.ContentType.Descriptor, value); }
 95        }
 96
 97        public DateTimeOffset? Expires
 98        {
 099            get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.Expires.Descriptor, this, DateTimeOffset.Mi
 0100            set { SetOrRemoveParsedValue(KnownHeaders.Expires.Descriptor, value); }
 101        }
 102
 103        public DateTimeOffset? LastModified
 104        {
 0105            get { return HeaderUtilities.GetDateTimeOffsetValue(KnownHeaders.LastModified.Descriptor, this); }
 0106            set { SetOrRemoveParsedValue(KnownHeaders.LastModified.Descriptor, value); }
 107        }
 108
 109        internal HttpContentHeaders(HttpContent parent)
 1110            : base(HttpHeaderType.Content | HttpHeaderType.Custom, HttpHeaderType.None)
 1111        {
 1112            _parent = parent;
 1113        }
 114    }
 115}