| | | 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 | | |
| | | 4 | | using System.Globalization; |
| | | 5 | | |
| | | 6 | | namespace 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 |
| | 0 | 14 | | { |
| | | 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]. |
| | 0 | 19 | | if (AppContext.TryGetSwitch(appCtxSettingName, out value)) |
| | 0 | 20 | | { |
| | 0 | 21 | | return value; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | // AppContext switch wasn't used. Check the environment variable. |
| | 0 | 25 | | string? envVar = Environment.GetEnvironmentVariable(environmentVariableSettingName); |
| | | 26 | | |
| | 0 | 27 | | if (bool.TryParse(envVar, out value)) |
| | 0 | 28 | | { |
| | 0 | 29 | | return value; |
| | | 30 | | } |
| | 0 | 31 | | else if (uint.TryParse(envVar, out uint intVal)) |
| | 0 | 32 | | { |
| | 0 | 33 | | return intVal != 0; |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | return defaultValue; |
| | 0 | 37 | | } |
| | | 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) |
| | 0 | 43 | | { |
| | | 44 | | bool value; |
| | | 45 | | |
| | | 46 | | // First check for the AppContext switch, giving it priority over the environment variable. |
| | 0 | 47 | | if (AppContext.TryGetSwitch(appCtxSettingName, out value)) |
| | 0 | 48 | | { |
| | 0 | 49 | | return value; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return defaultValue; |
| | 0 | 53 | | } |
| | | 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 |
| | 0 | 59 | | { |
| | | 60 | | // First check for the AppContext data, giving it priority over the environment variable. |
| | 0 | 61 | | switch (AppContext.GetData(appCtxSettingName)) |
| | | 62 | | { |
| | | 63 | | case uint value: |
| | 0 | 64 | | return (int)value; |
| | | 65 | | case string str: |
| | 0 | 66 | | return int.Parse(str, NumberStyles.Any, NumberFormatInfo.InvariantInfo); |
| | | 67 | | case IConvertible convertible: |
| | 0 | 68 | | return convertible.ToInt32(NumberFormatInfo.InvariantInfo); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // AppContext data wasn't used (or cannot coerce value). Check the environment variable. |
| | 0 | 72 | | return ParseInt32EnvironmentVariableValue(environmentVariableSettingName, defaultValue); |
| | 0 | 73 | | } |
| | | 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) |
| | 0 | 79 | | { |
| | 0 | 80 | | string? envVar = Environment.GetEnvironmentVariable(environmentVariableSettingName); |
| | | 81 | | |
| | 0 | 82 | | if (int.TryParse(envVar, NumberStyles.Any, CultureInfo.InvariantCulture, out int value)) |
| | 0 | 83 | | { |
| | 0 | 84 | | return value; |
| | | 85 | | } |
| | 0 | 86 | | return defaultValue; |
| | 0 | 87 | | } |
| | | 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 |
| | 0 | 93 | | { |
| | 0 | 94 | | string? envVar = Environment.GetEnvironmentVariable(environmentVariableSettingName); |
| | 0 | 95 | | if (double.TryParse(envVar, NumberStyles.Any, CultureInfo.InvariantCulture, out double value)) |
| | 0 | 96 | | { |
| | 0 | 97 | | return value; |
| | | 98 | | } |
| | 0 | 99 | | return defaultValue; |
| | 0 | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |