< Summary

Information
Class: System.Net.Http.UriRedactionHelper
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\Common\src\System\Net\Http\UriRedactionHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 49
Coverable lines: 49
Total lines: 93
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetDisableUriRedactionSettingValue()0%660%
GetRedactedPathAndQuery(...)0%660%
GetRedactedUriString(...)0%16160%
Slice(...)100%110%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Net\Http\UriRedactionHelper.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;
 5using System.Diagnostics.CodeAnalysis;
 6
 7namespace System.Net.Http
 8{
 9    internal static class UriRedactionHelper
 10    {
 011        public static bool IsDisabled { get; } = GetDisableUriRedactionSettingValue();
 12
 13        private static bool GetDisableUriRedactionSettingValue()
 014        {
 015            if (AppContext.TryGetSwitch("System.Net.Http.DisableUriRedaction", out bool value))
 016            {
 017                return value;
 18            }
 19
 020            string? envVar = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_DISABLEURIREDACTION");
 21
 022            if (bool.TryParse(envVar, out value))
 023            {
 024                return value;
 25            }
 026            else if (uint.TryParse(envVar, out uint intVal))
 027            {
 028                return intVal != 0;
 29            }
 30
 031            return false;
 032        }
 33
 34        public static string GetRedactedPathAndQuery(string pathAndQuery)
 035        {
 036            Debug.Assert(pathAndQuery is not null);
 37
 038            if (!IsDisabled)
 039            {
 040                int queryIndex = pathAndQuery.IndexOf('?');
 041                if (queryIndex >= 0 && queryIndex < (pathAndQuery.Length - 1))
 042                {
 043                    pathAndQuery = $"{Slice(pathAndQuery, 0, queryIndex + 1)}*";
 044                }
 045            }
 46
 047            return pathAndQuery;
 048        }
 49
 50        [return: NotNullIfNotNull(nameof(uri))]
 51        public static string? GetRedactedUriString(Uri? uri)
 052        {
 053            if (uri is null)
 054            {
 055                return null;
 56            }
 57
 058            if (IsDisabled)
 059            {
 060                return uri.IsAbsoluteUri ? uri.AbsoluteUri : uri.ToString();
 61            }
 62
 063            if (!uri.IsAbsoluteUri)
 064            {
 65                // We cannot guarantee the redaction of UserInfo for relative Uris without implementing some subset of U
 66                // To avoid this, we redact the whole Uri. Seeing a relative Uri here requires a custom handler chain wi
 67                // custom expansion logic implemented by the user's HttpMessageHandler.
 68                // In such advanced scenarios we recommend users to log the Uri in their handler.
 069                return "*";
 70            }
 71
 072            string pathAndQuery = uri.PathAndQuery;
 073            int queryIndex = pathAndQuery.IndexOf('?');
 74
 075            bool redactQuery = queryIndex >= 0 && // Query is present.
 076                queryIndex < pathAndQuery.Length - 1; // Query is not empty.
 77
 078            return (redactQuery, uri.IsDefaultPort) switch
 079            {
 080                (true, true) => $"{uri.Scheme}://{uri.Host}{Slice(pathAndQuery, 0, queryIndex + 1)}*",
 081                (true, false) => $"{uri.Scheme}://{uri.Host}:{uri.Port}{Slice(pathAndQuery, 0, queryIndex + 1)}*",
 082                (false, true) => $"{uri.Scheme}://{uri.Host}{pathAndQuery}",
 083                (false, false) => $"{uri.Scheme}://{uri.Host}:{uri.Port}{pathAndQuery}"
 084            };
 085        }
 86
 87#if NET
 088        private static ReadOnlySpan<char> Slice(string text, int startIndex, int length) => text.AsSpan(startIndex, leng
 89#else
 90        private static string Slice(string text, int startIndex, int length) => text.Substring(startIndex, length);
 91#endif
 92    }
 93}