< Summary

Information
Class: System.Net.Http.RuntimeSettingParser
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\SocketsHttpHandler\RuntimeSettingParser.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 40
Coverable lines: 40
Total lines: 102
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
QueryRuntimeSettingSwitch(...)0%660%
QueryRuntimeSettingSwitch(...)0%220%
QueryRuntimeSettingInt32(...)0%660%
ParseInt32EnvironmentVariableValue(...)0%220%
ParseDoubleEnvironmentVariableValue(...)0%220%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\SocketsHttpHandler\RuntimeSettingParser.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.Globalization;
 5
 6namespace System.Net.Http
 7{
 8    internal static class RuntimeSettingParser
 9    {
 10        /// <summary>
 11        /// Parse a <see cref="bool"/> value from an AppContext switch or an environment variable.
 12        /// </summary>
 13        public static bool QueryRuntimeSettingSwitch(string appCtxSettingName, string environmentVariableSettingName, bo
 014        {
 15            bool value;
 16
 17            // First check for the AppContext switch, giving it priority over the environment variable.
 18            // This being first is important for correctness of all callers marked [FeatureSwitchDefinition].
 019            if (AppContext.TryGetSwitch(appCtxSettingName, out value))
 020            {
 021                return value;
 22            }
 23
 24            // AppContext switch wasn't used. Check the environment variable.
 025            string? envVar = Environment.GetEnvironmentVariable(environmentVariableSettingName);
 26
 027            if (bool.TryParse(envVar, out value))
 028            {
 029                return value;
 30            }
 031            else if (uint.TryParse(envVar, out uint intVal))
 032            {
 033                return intVal != 0;
 34            }
 35
 036            return defaultValue;
 037        }
 38
 39        /// <summary>
 40        /// Parse a <see cref="bool"/> value from an AppContext switch.
 41        /// </summary>
 42        public static bool QueryRuntimeSettingSwitch(string appCtxSettingName, bool defaultValue)
 043        {
 44            bool value;
 45
 46            // First check for the AppContext switch, giving it priority over the environment variable.
 047            if (AppContext.TryGetSwitch(appCtxSettingName, out value))
 048            {
 049                return value;
 50            }
 51
 052            return defaultValue;
 053        }
 54
 55        /// <summary>
 56        /// Parse a <see cref="int"/> value from an AppContext data or an environment variable.
 57        /// </summary>
 58        public static int QueryRuntimeSettingInt32(string appCtxSettingName, string environmentVariableSettingName, int 
 059        {
 60            // First check for the AppContext data, giving it priority over the environment variable.
 061            switch (AppContext.GetData(appCtxSettingName))
 62            {
 63                case uint value:
 064                    return (int)value;
 65                case string str:
 066                    return int.Parse(str, NumberStyles.Any, NumberFormatInfo.InvariantInfo);
 67                case IConvertible convertible:
 068                    return convertible.ToInt32(NumberFormatInfo.InvariantInfo);
 69            }
 70
 71            // AppContext data wasn't used (or cannot coerce value). Check the environment variable.
 072            return ParseInt32EnvironmentVariableValue(environmentVariableSettingName, defaultValue);
 073        }
 74
 75        /// <summary>
 76        /// Parse an environment variable for an <see cref="int"/> value.
 77        /// </summary>
 78        public static int ParseInt32EnvironmentVariableValue(string environmentVariableSettingName, int defaultValue)
 079        {
 080            string? envVar = Environment.GetEnvironmentVariable(environmentVariableSettingName);
 81
 082            if (int.TryParse(envVar, NumberStyles.Any, CultureInfo.InvariantCulture, out int value))
 083            {
 084                return value;
 85            }
 086            return defaultValue;
 087        }
 88
 89        /// <summary>
 90        /// Parse an environment variable for a <see cref="double"/> value.
 91        /// </summary>
 92        public static double ParseDoubleEnvironmentVariableValue(string environmentVariableSettingName, double defaultVa
 093        {
 094            string? envVar = Environment.GetEnvironmentVariable(environmentVariableSettingName);
 095            if (double.TryParse(envVar, NumberStyles.Any, CultureInfo.InvariantCulture, out double value))
 096            {
 097                return value;
 98            }
 099            return defaultValue;
 0100        }
 101    }
 102}