< Summary

Information
Class: System.Net.Http.HttpMessageInvoker
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpMessageInvoker.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 75
Coverable lines: 75
Total lines: 135
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
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%110%
.ctor(...)0%220%
Send(...)0%220%
SendAsync(...)0%220%
SendAsyncWithTelemetry()100%110%
ShouldSendWithTelemetry(...)0%660%
LogRequestFailed(...)0%220%
Dispose()100%110%
Dispose(...)0%660%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpMessageInvoker.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.Runtime.Versioning;
 5using System.Threading;
 6using System.Threading.Tasks;
 7
 8namespace System.Net.Http
 9{
 10    public class HttpMessageInvoker : IDisposable
 11    {
 12        private volatile bool _disposed;
 13        private readonly bool _disposeHandler;
 14        private readonly HttpMessageHandler _handler;
 15
 16        public HttpMessageInvoker(HttpMessageHandler handler)
 017            : this(handler, true)
 018        {
 019        }
 20
 021        public HttpMessageInvoker(HttpMessageHandler handler, bool disposeHandler)
 022        {
 023            ArgumentNullException.ThrowIfNull(handler);
 24
 025            if (NetEventSource.Log.IsEnabled()) NetEventSource.Associate(this, handler);
 26
 027            _handler = handler;
 028            _disposeHandler = disposeHandler;
 029        }
 30
 31        [UnsupportedOSPlatform("android")]
 32        [UnsupportedOSPlatform("browser")]
 33        [UnsupportedOSPlatform("ios")]
 34        [UnsupportedOSPlatform("tvos")]
 35        public virtual HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
 036        {
 037            ArgumentNullException.ThrowIfNull(request);
 38
 039            ObjectDisposedException.ThrowIf(_disposed, this);
 40
 041            if (ShouldSendWithTelemetry(request))
 042            {
 043                HttpTelemetry.Log.RequestStart(request);
 44
 045                HttpResponseMessage? response = null;
 46                try
 047                {
 048                    response = _handler.Send(request, cancellationToken);
 049                    return response;
 50                }
 051                catch (Exception ex) when (LogRequestFailed(ex, telemetryStarted: true))
 052                {
 53                    // Unreachable as LogRequestFailed will return false
 054                    throw;
 55                }
 56                finally
 057                {
 058                    HttpTelemetry.Log.RequestStop(response);
 059                }
 60            }
 61            else
 062            {
 063                return _handler.Send(request, cancellationToken);
 64            }
 065        }
 66
 67        public virtual Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationTok
 068        {
 069            ArgumentNullException.ThrowIfNull(request);
 70
 071            ObjectDisposedException.ThrowIf(_disposed, this);
 72
 073            if (ShouldSendWithTelemetry(request))
 074            {
 075                return SendAsyncWithTelemetry(_handler, request, cancellationToken);
 76            }
 77
 078            return _handler.SendAsync(request, cancellationToken);
 79
 80            static async Task<HttpResponseMessage> SendAsyncWithTelemetry(HttpMessageHandler handler, HttpRequestMessage
 081            {
 082                HttpTelemetry.Log.RequestStart(request);
 83
 084                HttpResponseMessage? response = null;
 85                try
 086                {
 087                    response = await handler.SendAsync(request, cancellationToken).ConfigureAwait(false);
 088                    return response;
 89                }
 090                catch (Exception ex) when (LogRequestFailed(ex, telemetryStarted: true))
 091                {
 92                    // Unreachable as LogRequestFailed will return false
 093                    throw;
 94                }
 95                finally
 096                {
 097                    HttpTelemetry.Log.RequestStop(response);
 098                }
 099            }
 0100        }
 101
 102        private static bool ShouldSendWithTelemetry(HttpRequestMessage request) =>
 0103            HttpTelemetry.Log.IsEnabled() &&
 0104            !request.WasSentByHttpClient() &&
 0105            request.RequestUri is Uri requestUri &&
 0106            requestUri.IsAbsoluteUri;
 107
 108        internal static bool LogRequestFailed(Exception exception, bool telemetryStarted)
 0109        {
 0110            if (HttpTelemetry.Log.IsEnabled() && telemetryStarted)
 0111            {
 0112                HttpTelemetry.Log.RequestFailed(exception);
 0113            }
 0114            return false;
 0115        }
 116
 117        public void Dispose()
 0118        {
 0119            Dispose(true);
 0120            GC.SuppressFinalize(this);
 0121        }
 122
 123        protected virtual void Dispose(bool disposing)
 0124        {
 0125            if (disposing && !_disposed)
 0126            {
 0127                _disposed = true;
 0128                if (_disposeHandler)
 0129                {
 0130                    _handler.Dispose();
 0131                }
 0132            }
 0133        }
 134    }
 135}