< Summary

Information
Class: System.Net.Http.HttpRequestMessage
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpRequestMessage.cs
Line coverage
13%
Covered lines: 14
Uncovered lines: 90
Coverable lines: 104
Total lines: 223
Line coverage: 13.4%
Branch coverage
7%
Covered branches: 2
Total branches: 26
Branch coverage: 7.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor()100%11100%
.ctor(...)0%220%
ToString()0%660%
MarkAsSent()100%110%
WasSentByHttpClient()100%110%
MarkPropagatorStateInjectedByDiagnosticsHandler()100%110%
WasPropagatorStateInjectedByDiagnosticsHandler()100%110%
DisableAuth()100%110%
IsAuthDisabled()100%110%
Dispose(...)0%660%
Dispose()100%110%
CheckDisposed()100%110%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpRequestMessage.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;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Net.Http.Headers;
 8using System.Text;
 9using System.Threading;
 10
 11namespace System.Net.Http
 12{
 13    public class HttpRequestMessage : IDisposable
 14    {
 115        internal static Version DefaultRequestVersion => HttpVersion.Version11;
 16        internal static HttpVersionPolicy DefaultVersionPolicy => HttpVersionPolicy.RequestVersionOrLower;
 17
 18        private const int MessageNotYetSent = 0;
 19        private const int MessageAlreadySent = 1;
 20        private const int PropagatorStateInjectedByDiagnosticsHandler = 2;
 21        private const int MessageDisposed = 4;
 22        private const int AuthDisabled = 8;
 23
 24        // Track whether the message has been sent.
 25        // The message shouldn't be sent again if this field is equal to MessageAlreadySent.
 126        private int _sendStatus = MessageNotYetSent;
 27
 28        private HttpMethod _method;
 29        private Uri? _requestUri;
 30        private HttpRequestHeaders? _headers;
 31        private Version _version;
 32        private HttpVersionPolicy _versionPolicy;
 33        private HttpContent? _content;
 34        internal HttpRequestOptions? _options;
 35
 36        public Version Version
 37        {
 038            get { return _version; }
 39            set
 040            {
 041                ArgumentNullException.ThrowIfNull(value);
 042                CheckDisposed();
 43
 044                _version = value;
 045            }
 46        }
 47
 48        /// <summary>
 49        /// Gets or sets the policy determining how <see cref="Version" /> is interpreted and how is the final HTTP vers
 50        /// </summary>
 51        public HttpVersionPolicy VersionPolicy
 52        {
 053            get { return _versionPolicy; }
 54            set
 055            {
 056                CheckDisposed();
 57
 058                _versionPolicy = value;
 059            }
 60        }
 61
 62        public HttpContent? Content
 63        {
 064            get { return _content; }
 65            set
 066            {
 067                CheckDisposed();
 68
 069                if (NetEventSource.Log.IsEnabled())
 070                {
 071                    if (value == null)
 072                    {
 073                        NetEventSource.ContentNull(this);
 074                    }
 75                    else
 076                    {
 077                        NetEventSource.Associate(this, value);
 078                    }
 079                }
 80
 81                // It's OK to set a 'null' content, even if the method is POST/PUT.
 082                _content = value;
 083            }
 84        }
 85
 86        public HttpMethod Method
 87        {
 088            get { return _method; }
 89            set
 090            {
 091                ArgumentNullException.ThrowIfNull(value);
 092                CheckDisposed();
 93
 094                _method = value;
 095            }
 96        }
 97
 98        public Uri? RequestUri
 99        {
 0100            get { return _requestUri; }
 101            set
 0102            {
 0103                CheckDisposed();
 0104                _requestUri = value;
 0105            }
 106        }
 107
 1108        public HttpRequestHeaders Headers => _headers ??= new HttpRequestHeaders();
 109
 0110        internal bool HasHeaders => _headers != null;
 111
 112        [Obsolete("HttpRequestMessage.Properties has been deprecated. Use Options instead.")]
 0113        public IDictionary<string, object?> Properties => Options;
 114
 115        /// <summary>
 116        /// Gets the collection of options to configure the HTTP request.
 117        /// </summary>
 0118        public HttpRequestOptions Options => _options ??= new HttpRequestOptions();
 119
 120        public HttpRequestMessage()
 1121            : this(HttpMethod.Get, (Uri?)null)
 1122        {
 1123        }
 124
 1125        public HttpRequestMessage(HttpMethod method, Uri? requestUri)
 1126        {
 1127            ArgumentNullException.ThrowIfNull(method);
 128
 129            // It's OK to have a 'null' request Uri. If HttpClient is used, the 'BaseAddress' will be added.
 130            // If there is no 'BaseAddress', sending this request message will throw.
 131            // Note that we also allow the string to be empty: null and empty are considered equivalent.
 1132            _method = method;
 1133            _requestUri = requestUri;
 1134            _version = DefaultRequestVersion;
 1135            _versionPolicy = DefaultVersionPolicy;
 1136        }
 137
 138        public HttpRequestMessage(HttpMethod method, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri)
 0139            : this(method, string.IsNullOrEmpty(requestUri) ? null : new Uri(requestUri, UriKind.RelativeOrAbsolute))
 0140        {
 0141        }
 142
 143        public override string ToString()
 0144        {
 0145            ValueStringBuilder sb = new ValueStringBuilder(stackalloc char[512]);
 146
 0147            sb.Append("Method: ");
 0148            sb.Append(_method.ToString());
 149
 0150            sb.Append(", RequestUri: '");
 0151            if (_requestUri is null)
 0152            {
 0153                sb.Append("<null>");
 0154            }
 155            else
 0156            {
 0157                sb.AppendSpanFormattable(_requestUri);
 0158            }
 159
 0160            sb.Append("', Version: ");
 0161            sb.AppendSpanFormattable(_version);
 162
 0163            sb.Append(", Content: ");
 0164            sb.Append(_content == null ? "<null>" : _content.GetType().ToString());
 165
 0166            sb.Append(", Headers:");
 0167            sb.Append(Environment.NewLine);
 0168            HeaderUtilities.DumpHeaders(ref sb, _headers, _content?.Headers);
 169
 0170            return sb.ToString();
 0171        }
 172
 0173        internal bool MarkAsSent() => Interlocked.CompareExchange(ref _sendStatus, MessageAlreadySent, MessageNotYetSent
 174
 0175        internal bool WasSentByHttpClient() => (_sendStatus & MessageAlreadySent) != 0;
 176
 0177        internal void MarkPropagatorStateInjectedByDiagnosticsHandler() => _sendStatus |= PropagatorStateInjectedByDiagn
 178
 0179        internal bool WasPropagatorStateInjectedByDiagnosticsHandler() => (_sendStatus & PropagatorStateInjectedByDiagno
 180
 0181        internal void DisableAuth() => _sendStatus |= AuthDisabled;
 182
 0183        internal bool IsAuthDisabled() => (_sendStatus & AuthDisabled) != 0;
 184
 185        private bool Disposed
 186        {
 0187            get => (_sendStatus & MessageDisposed) != 0;
 188            set
 0189            {
 0190                Debug.Assert(value);
 0191                _sendStatus |= MessageDisposed;
 0192            }
 193        }
 194
 0195        internal bool IsExtendedConnectRequest => Method == HttpMethod.Connect && _headers?.Protocol != null;
 196
 197        #region IDisposable Members
 198
 199        protected virtual void Dispose(bool disposing)
 0200        {
 201            // The reason for this type to implement IDisposable is that it contains instances of types that implement
 202            // IDisposable (content).
 0203            if (disposing && !Disposed)
 0204            {
 0205                Disposed = true;
 0206                _content?.Dispose();
 0207            }
 0208        }
 209
 210        public void Dispose()
 0211        {
 0212            Dispose(true);
 0213            GC.SuppressFinalize(this);
 0214        }
 215
 216        #endregion
 217
 218        private void CheckDisposed()
 0219        {
 0220            ObjectDisposedException.ThrowIf(Disposed, this);
 0221        }
 222    }
 223}