| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Runtime.Versioning; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | |
| | | 5 | | namespace Kestrun; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Utility class to expose information about the runtime environment |
| | | 9 | | /// that Kestrun was built for, and to gate features by TFM and runtime. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class KestrunRuntimeInfo |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Returns the target framework version this assembly was built against |
| | | 15 | | /// as a System.Version (e.g., 8.0, 9.0). |
| | | 16 | | /// </summary> |
| | | 17 | | public static Version GetBuiltTargetFrameworkVersion() |
| | | 18 | | { |
| | 3 | 19 | | var asm = typeof(KestrunRuntimeInfo).Assembly; |
| | 3 | 20 | | var tfm = asm.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName; |
| | 3 | 21 | | if (string.IsNullOrEmpty(tfm)) |
| | | 22 | | { |
| | 0 | 23 | | return new Version(0, 0); |
| | | 24 | | } |
| | | 25 | | |
| | 3 | 26 | | var key = "Version="; |
| | 3 | 27 | | var idx = tfm.IndexOf(key, StringComparison.OrdinalIgnoreCase); |
| | 3 | 28 | | if (idx >= 0) |
| | | 29 | | { |
| | 3 | 30 | | var ver = tfm[(idx + key.Length)..].TrimStart('v'); |
| | 3 | 31 | | if (Version.TryParse(ver, out var parsed)) |
| | | 32 | | { |
| | 3 | 33 | | return parsed; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | return new Version(0, 0); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // --- Feature gating --- |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Built-in Kestrun feature keys. Add more as you gate new APIs by TFM. |
| | | 44 | | /// </summary> |
| | | 45 | | public enum KnownFeature |
| | | 46 | | { |
| | | 47 | | /// <summary> |
| | | 48 | | /// Kestrel HTTP/3 listener support (requires QUIC at runtime) |
| | | 49 | | /// </summary> |
| | | 50 | | Http3 = 0, |
| | | 51 | | /// <summary> |
| | | 52 | | /// Suppresses reading the antiforgery token from the form body |
| | | 53 | | /// </summary> |
| | | 54 | | SuppressReadingTokenFromFormBody = 1 |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // Minimal TFM required for each feature. |
| | | 58 | | // Extend this table as you add features. |
| | 1 | 59 | | private static readonly Dictionary<string, Version> FeatureMinByName = |
| | 1 | 60 | | new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 61 | | { |
| | 1 | 62 | | [nameof(KnownFeature.Http3)] = new Version(8, 0), |
| | 1 | 63 | | // New in .NET 9+: Antiforgery option to suppress reading token from request form body. |
| | 1 | 64 | | [nameof(KnownFeature.SuppressReadingTokenFromFormBody)] = new Version(9, 0), |
| | 1 | 65 | | }; |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Returns the set of known feature identifiers (enum names) that have a |
| | | 69 | | /// compile-time (TFM) gate registered. This does not guarantee that |
| | | 70 | | /// <see cref="Supports(string)"/> will return true, only that the feature |
| | | 71 | | /// is recognized and has a minimum version entry. |
| | | 72 | | /// </summary> |
| | 1 | 73 | | public static IEnumerable<string> GetKnownFeatures() => FeatureMinByName.Keys; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// True if the loaded Kestrun assembly supports the feature, |
| | | 77 | | /// considering both build-time TFM and runtime requirements. |
| | | 78 | | /// </summary> |
| | | 79 | | public static bool Supports(KnownFeature feature) |
| | | 80 | | { |
| | 1 | 81 | | var name = feature.ToString(); |
| | 1 | 82 | | if (!TryGetMinVersion(name, out var min) || !IsAtLeast(min)) |
| | | 83 | | { |
| | 0 | 84 | | return false; // compile-time gate failed |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // runtime-sensitive checks |
| | 1 | 88 | | return feature switch |
| | 1 | 89 | | { |
| | 0 | 90 | | KnownFeature.Http3 => CheckHttp3Runtime(), |
| | 1 | 91 | | _ => true, |
| | 1 | 92 | | }; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// True if the loaded Kestrun assembly supports the feature, |
| | | 97 | | /// considering both build-time TFM and runtime requirements. |
| | | 98 | | /// Unknown names return false. |
| | | 99 | | /// </summary> |
| | | 100 | | public static bool Supports(string featureName) |
| | | 101 | | { |
| | 2 | 102 | | if (string.IsNullOrWhiteSpace(featureName)) |
| | | 103 | | { |
| | 0 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | 2 | 107 | | if (!TryGetMinVersion(featureName, out var min) || !IsAtLeast(min)) |
| | | 108 | | { |
| | 1 | 109 | | return false; // compile-time/TFM gate failed or unknown |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | // Central runtime-sensitive feature dispatch (mirrors enum switch in Supports(KnownFeature)) |
| | | 113 | | // Extend this map when adding new runtime-validated features. |
| | | 114 | | // For features with no runtime conditions, omission implies success. |
| | 1 | 115 | | var runtimeChecks = RuntimeFeatureChecks; |
| | 1 | 116 | | if (runtimeChecks.TryGetValue(featureName, out var checker)) |
| | | 117 | | { |
| | 0 | 118 | | return checker(); |
| | | 119 | | } |
| | 1 | 120 | | return true; // Known (by TFM table) & no runtime check required |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // Provides a single place to register runtime-sensitive checks by feature name. |
| | | 124 | | // Note: Uses StringComparer.OrdinalIgnoreCase to keep behavior consistent with FeatureMinByName. |
| | 1 | 125 | | private static readonly Dictionary<string, Func<bool>> RuntimeFeatureChecks = |
| | 1 | 126 | | new(StringComparer.OrdinalIgnoreCase) |
| | 1 | 127 | | { |
| | 1 | 128 | | [nameof(KnownFeature.Http3)] = CheckHttp3Runtime, |
| | 1 | 129 | | }; |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Returns the minimum TFM required for a feature, if known. |
| | | 133 | | /// </summary> |
| | | 134 | | /// <param name="featureName">Feature identifier (case-insensitive).</param> |
| | | 135 | | /// <param name="minVersion"> |
| | | 136 | | /// When this method returns true, contains the minimum target framework version required for the feature. |
| | | 137 | | /// When it returns false, the value is set to <c>0.0</c> as a harmless placeholder. |
| | | 138 | | /// </param> |
| | | 139 | | /// <returns>True if the feature is known; otherwise false.</returns> |
| | | 140 | | public static bool TryGetMinVersion(string featureName, [NotNullWhen(true)] out Version minVersion) |
| | | 141 | | { |
| | 5 | 142 | | if (FeatureMinByName.TryGetValue(featureName, out var value)) |
| | | 143 | | { |
| | 4 | 144 | | minVersion = value; // non-null by construction |
| | 4 | 145 | | return true; |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | // Provide a non-null sentinel to satisfy non-nullable contract while indicating absence. |
| | 1 | 149 | | minVersion = new Version(0, 0); |
| | 1 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private static bool IsAtLeast(Version min) |
| | | 154 | | { |
| | 2 | 155 | | var built = GetBuiltTargetFrameworkVersion(); |
| | 2 | 156 | | return built >= min; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | #region Runtime checks |
| | | 160 | | // ---------- Runtime-sensitive checks ---------- |
| | | 161 | | /// <summary> |
| | | 162 | | /// Checks runtime support for HTTP/3 (QUIC) in addition to compile-time gating. |
| | | 163 | | /// </summary> |
| | | 164 | | private static bool CheckHttp3Runtime() |
| | | 165 | | { |
| | | 166 | | // Use cached reflection metadata to avoid repeated lookups on hot paths. |
| | 0 | 167 | | if (_quicListenerType == null) |
| | | 168 | | { |
| | 0 | 169 | | return false; |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | if (_quicIsSupportedProperty != null) |
| | | 173 | | { |
| | 0 | 174 | | var value = _quicIsSupportedProperty.GetValue(null); |
| | 0 | 175 | | if (value is bool supported && !supported) |
| | | 176 | | { |
| | 0 | 177 | | return false; |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | // Kestrel HTTP/3 enum field presence check |
| | 0 | 182 | | return _httpProtocolsType != null && _http3Field != null; |
| | | 183 | | } |
| | | 184 | | #endregion |
| | | 185 | | |
| | | 186 | | // Cached reflection metadata for runtime checks (initialized once). |
| | 1 | 187 | | private static readonly Type? _quicListenerType = Type.GetType("System.Net.Quic.QuicListener, System.Net.Quic"); |
| | 1 | 188 | | private static readonly PropertyInfo? _quicIsSupportedProperty = _quicListenerType?.GetProperty("IsSupported", Bindi |
| | 1 | 189 | | private static readonly Type? _httpProtocolsType = Type.GetType("Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtoc |
| | 1 | 190 | | private static readonly FieldInfo? _http3Field = _httpProtocolsType?.GetField("Http3", BindingFlags.Public | Binding |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Returns the full target framework name this assembly was built against, |
| | | 194 | | /// e.g., ".NETCoreApp,Version=v9.0". |
| | | 195 | | /// </summary> |
| | | 196 | | public static string GetBuiltTargetFrameworkName() |
| | | 197 | | { |
| | 1 | 198 | | var tfm = typeof(KestrunRuntimeInfo).Assembly |
| | 1 | 199 | | .GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName; |
| | 1 | 200 | | return tfm ?? "Unknown"; |
| | | 201 | | } |
| | | 202 | | } |