| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Forms; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides helper methods for form handling. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class FormHelper |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Populates <see cref="KrFormPartRule.NestedRules"/> for rules within a <see cref="KrFormOptions"/> instance. |
| | | 12 | | /// This uses <see cref="KrFormPartRule.Scope"/> as the parent container rule name and attaches all scoped rules |
| | | 13 | | /// to their matching container rule(s) (case-insensitive). |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="options">The options containing rules to link.</param> |
| | | 16 | | internal static void PopulateNestedRulesFromScopes(KrFormOptions options) |
| | | 17 | | { |
| | 5 | 18 | | ArgumentNullException.ThrowIfNull(options); |
| | | 19 | | |
| | | 20 | | // Clear first to avoid duplication when called multiple times. |
| | 50 | 21 | | foreach (var rule in options.Rules) |
| | | 22 | | { |
| | 20 | 23 | | rule.NestedRules.Clear(); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | // Build a lookup of container-name -> list of container rules. |
| | 5 | 27 | | var containers = new Dictionary<string, List<KrFormPartRule>>(StringComparer.OrdinalIgnoreCase); |
| | 50 | 28 | | foreach (var rule in options.Rules) |
| | | 29 | | { |
| | 20 | 30 | | if (string.IsNullOrWhiteSpace(rule.Name)) |
| | | 31 | | { |
| | | 32 | | continue; |
| | | 33 | | } |
| | | 34 | | |
| | 20 | 35 | | if (!containers.TryGetValue(rule.Name, out var bag)) |
| | | 36 | | { |
| | 20 | 37 | | bag = []; |
| | 20 | 38 | | containers.Add(rule.Name, bag); |
| | | 39 | | } |
| | | 40 | | |
| | 20 | 41 | | bag.Add(rule); |
| | | 42 | | } |
| | | 43 | | |
| | 50 | 44 | | foreach (var child in options.Rules) |
| | | 45 | | { |
| | 20 | 46 | | if (string.IsNullOrWhiteSpace(child.Scope)) |
| | | 47 | | { |
| | | 48 | | continue; |
| | | 49 | | } |
| | | 50 | | |
| | 10 | 51 | | if (!containers.TryGetValue(child.Scope, out var parents) || parents.Count == 0) |
| | | 52 | | { |
| | | 53 | | continue; |
| | | 54 | | } |
| | | 55 | | |
| | 40 | 56 | | foreach (var parent in parents) |
| | | 57 | | { |
| | 10 | 58 | | var exists = false; |
| | 30 | 59 | | for (var i = 0; i < parent.NestedRules.Count; i++) |
| | | 60 | | { |
| | 5 | 61 | | if (ReferenceEquals(parent.NestedRules[i], child)) |
| | | 62 | | { |
| | 0 | 63 | | exists = true; |
| | 0 | 64 | | break; |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 10 | 68 | | if (!exists) |
| | | 69 | | { |
| | 10 | 70 | | parent.NestedRules.Add(child); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |
| | 5 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Applies KrPartAttribute annotations to the host's form part rules. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="host">The Kestrun host runtime.</param> |
| | | 80 | | /// <param name="p">The property info to inspect for KrPartAttribute annotations.</param> |
| | | 81 | | /// <param name="scopeName">The parent multipart scope name (null for root).</param> |
| | | 82 | | internal static void ApplyKrPartAttributes(Hosting.KestrunHost host, PropertyInfo p, string? scopeName) |
| | | 83 | | { |
| | 168 | 84 | | var name = ResolvePartName(p); |
| | | 85 | | // Apply KrPartAttribute annotations |
| | 372 | 86 | | foreach (var attr in p.GetCustomAttributes<KrPartAttribute>(inherit: false)) |
| | | 87 | | { |
| | 18 | 88 | | var formPartRule = new KrFormPartRule |
| | 18 | 89 | | { |
| | 18 | 90 | | Name = name ?? "", |
| | 18 | 91 | | Scope = scopeName, |
| | 18 | 92 | | Description = attr.Description, |
| | 18 | 93 | | Required = attr.Required, |
| | 18 | 94 | | AllowMultiple = attr.AllowMultiple, |
| | 18 | 95 | | |
| | 18 | 96 | | MaxBytes = attr.MaxBytes > 0 ? attr.MaxBytes : null, |
| | 18 | 97 | | DecodeMode = attr.DecodeMode, |
| | 18 | 98 | | DestinationPath = attr.DestinationPath, |
| | 18 | 99 | | StoreToDisk = attr.StoreToDisk, |
| | 18 | 100 | | }; |
| | | 101 | | |
| | 18 | 102 | | formPartRule.AllowedContentTypes.AddRange(attr.ContentTypes); |
| | 18 | 103 | | formPartRule.AllowedExtensions.AddRange(attr.Extensions); |
| | 18 | 104 | | var added = host.AddFormPartRule(formPartRule); |
| | 18 | 105 | | if (!added && host.Runtime.FormPartRules.TryGetValue(formPartRule.Name, out var existingRule)) |
| | | 106 | | { |
| | 0 | 107 | | formPartRule = existingRule; |
| | | 108 | | } |
| | 18 | 109 | | AttachNestedRule(host, scopeName, formPartRule); |
| | | 110 | | } |
| | 168 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Resolves the part name for a property, including PowerShell dynamic type handling. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="p">The property info to inspect.</param> |
| | | 117 | | /// <returns>The resolved part name.</returns> |
| | | 118 | | internal static string? ResolvePartName(PropertyInfo p) |
| | | 119 | | { |
| | 205 | 120 | | var name = p.Name; |
| | 205 | 121 | | var type = p.PropertyType; |
| | | 122 | | |
| | | 123 | | // Arrays / collections → unwrap |
| | 205 | 124 | | if (type.IsArray) |
| | | 125 | | { |
| | 21 | 126 | | type = type.GetElementType()!; |
| | | 127 | | } |
| | | 128 | | // Nullable<T> → unwrap |
| | 205 | 129 | | if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| | | 130 | | { |
| | 8 | 131 | | type = Nullable.GetUnderlyingType(type)!; |
| | | 132 | | } |
| | | 133 | | |
| | 205 | 134 | | var asm = type.Assembly; |
| | | 135 | | |
| | | 136 | | // PowerShell classes are emitted into a dynamic assembly |
| | 205 | 137 | | if (asm.IsDynamic) |
| | | 138 | | { |
| | | 139 | | // Stable PowerShell assembly naming convention |
| | 0 | 140 | | var fullName = asm.FullName; |
| | 0 | 141 | | if (fullName != null && |
| | 0 | 142 | | fullName.StartsWith("PowerShell Class Assembly", StringComparison.Ordinal)) |
| | | 143 | | { |
| | | 144 | | // PropertyType.FullName can be null for some dynamic types; avoid dereference |
| | 0 | 145 | | var propFullName = p.PropertyType.FullName; |
| | 0 | 146 | | if (propFullName is not null) |
| | | 147 | | { |
| | 0 | 148 | | name = propFullName.Trim('[', ']'); |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | 205 | 153 | | return name; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Builds form part rules for a CLR type decorated with <see cref="KrPartAttribute"/>. |
| | | 158 | | /// Nested multipart container parts (parts whose allowed content types include <c>multipart/*</c>) |
| | | 159 | | /// will have their inner part rules added to <see cref="KrFormPartRule.NestedRules"/>, and the |
| | | 160 | | /// inner rules will be scoped to the container's part name. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <param name="rootType">The root type to inspect.</param> |
| | | 163 | | /// <returns>A flattened list of rules including nested rules.</returns> |
| | | 164 | | internal static List<KrFormPartRule> BuildFormPartRulesFromType(Type rootType) |
| | | 165 | | { |
| | 6 | 166 | | ArgumentNullException.ThrowIfNull(rootType); |
| | 6 | 167 | | var visited = new HashSet<Type>(); |
| | 6 | 168 | | return BuildFormPartRulesFromType(rootType, scopeName: null, visited); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Builds form part rules for a type, scoped under a given parent container name. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="type">The type to inspect.</param> |
| | | 175 | | /// <param name="scopeName">The parent multipart scope name.</param> |
| | | 176 | | /// <param name="visited">The set of visited types to avoid cycles.</param> |
| | | 177 | | /// <returns>A flattened list of rules including nested rules.</returns> |
| | | 178 | | private static List<KrFormPartRule> BuildFormPartRulesFromType(Type type, string? scopeName, HashSet<Type> visited) |
| | | 179 | | { |
| | 11 | 180 | | if (!visited.Add(type)) |
| | | 181 | | { |
| | 0 | 182 | | return []; |
| | | 183 | | } |
| | | 184 | | |
| | 11 | 185 | | var rules = new List<KrFormPartRule>(); |
| | 60 | 186 | | foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| | 30 | 187 | | .Where(static p => p.DeclaringType == p.ReflectedType)) |
| | | 188 | | { |
| | 19 | 189 | | var ruleInfo = TryBuildRuleInfo(prop, scopeName); |
| | 19 | 190 | | if (ruleInfo is not { } info) |
| | | 191 | | { |
| | | 192 | | continue; |
| | | 193 | | } |
| | | 194 | | |
| | 19 | 195 | | rules.Add(info.Rule); |
| | 19 | 196 | | if (info.CanNest) |
| | | 197 | | { |
| | 5 | 198 | | AddNestedRules(info.Rule, info.UnderlyingType!, visited, rules); |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | 11 | 202 | | return rules; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Creates rule metadata for a property decorated with <see cref="KrPartAttribute"/>. |
| | | 207 | | /// </summary> |
| | | 208 | | /// <param name="prop">The property to inspect.</param> |
| | | 209 | | /// <param name="scopeName">The parent multipart scope name.</param> |
| | | 210 | | /// <returns>The rule info or null when no rule should be created.</returns> |
| | | 211 | | private static RuleInfo? TryBuildRuleInfo(PropertyInfo prop, string? scopeName) |
| | | 212 | | { |
| | 19 | 213 | | var attr = prop.GetCustomAttribute<KrPartAttribute>(inherit: false); |
| | 19 | 214 | | if (attr is null) |
| | | 215 | | { |
| | 0 | 216 | | return null; |
| | | 217 | | } |
| | | 218 | | |
| | 19 | 219 | | var partName = ResolvePartName(prop); |
| | 19 | 220 | | if (string.IsNullOrWhiteSpace(partName)) |
| | | 221 | | { |
| | 0 | 222 | | return null; |
| | | 223 | | } |
| | | 224 | | |
| | 19 | 225 | | var underlying = UnwrapElementType(prop.PropertyType); |
| | 19 | 226 | | var canNest = IsMultipartContainerRule(attr) && underlying is not null && IsComplexType(underlying); |
| | 19 | 227 | | var ruleName = canNest ? underlying!.Name : partName; |
| | | 228 | | |
| | 19 | 229 | | var rule = new KrFormPartRule |
| | 19 | 230 | | { |
| | 19 | 231 | | Name = ruleName, |
| | 19 | 232 | | Scope = scopeName, |
| | 19 | 233 | | Description = attr.Description, |
| | 19 | 234 | | Required = attr.Required, |
| | 19 | 235 | | AllowMultiple = attr.AllowMultiple, |
| | 19 | 236 | | MaxBytes = attr.MaxBytes > 0 ? attr.MaxBytes : null, |
| | 19 | 237 | | DecodeMode = attr.DecodeMode, |
| | 19 | 238 | | DestinationPath = attr.DestinationPath, |
| | 19 | 239 | | StoreToDisk = attr.StoreToDisk, |
| | 19 | 240 | | }; |
| | | 241 | | |
| | 19 | 242 | | rule.AllowedContentTypes.AddRange(attr.ContentTypes); |
| | 19 | 243 | | rule.AllowedExtensions.AddRange(attr.Extensions); |
| | | 244 | | |
| | 19 | 245 | | return new RuleInfo(rule, underlying, canNest); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Adds nested rules for a multipart container rule and flattens them into the output list. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <param name="parent">The parent container rule.</param> |
| | | 252 | | /// <param name="underlying">The underlying complex type to inspect.</param> |
| | | 253 | | /// <param name="visited">The set of visited types to avoid cycles.</param> |
| | | 254 | | /// <param name="rules">The master rules list to append to.</param> |
| | | 255 | | private static void AddNestedRules( |
| | | 256 | | KrFormPartRule parent, |
| | | 257 | | Type underlying, |
| | | 258 | | HashSet<Type> visited, |
| | | 259 | | List<KrFormPartRule> rules) |
| | | 260 | | { |
| | 5 | 261 | | var childRules = BuildFormPartRulesFromType(underlying, scopeName: parent.Name, visited); |
| | 28 | 262 | | foreach (var child in childRules) |
| | | 263 | | { |
| | 9 | 264 | | parent.NestedRules.Add(child); |
| | | 265 | | } |
| | | 266 | | |
| | 5 | 267 | | rules.AddRange(childRules); |
| | 5 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Holds rule construction details for a property. |
| | | 272 | | /// </summary> |
| | 48 | 273 | | private readonly record struct RuleInfo(KrFormPartRule Rule, Type? UnderlyingType, bool CanNest); |
| | | 274 | | |
| | | 275 | | /// <summary> |
| | | 276 | | /// Unwraps array or nullable types to their element or underlying type. |
| | | 277 | | /// </summary> |
| | | 278 | | /// <param name="type">The type to unwrap.</param> |
| | | 279 | | /// <returns>The unwrapped element or underlying type, or the original type if not an array or nullable.</returns> |
| | | 280 | | private static Type? UnwrapElementType(Type type) |
| | | 281 | | { |
| | 19 | 282 | | var t = type; |
| | 19 | 283 | | if (t.IsArray) |
| | | 284 | | { |
| | 4 | 285 | | t = t.GetElementType()!; |
| | | 286 | | } |
| | | 287 | | |
| | 19 | 288 | | if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) |
| | | 289 | | { |
| | 0 | 290 | | t = Nullable.GetUnderlyingType(t)!; |
| | | 291 | | } |
| | | 292 | | |
| | 19 | 293 | | return t; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private static bool IsMultipartContainerRule(KrPartAttribute attr) |
| | | 297 | | { |
| | 19 | 298 | | if (attr.ContentTypes is null || attr.ContentTypes.Length == 0) |
| | | 299 | | { |
| | 0 | 300 | | return false; |
| | | 301 | | } |
| | | 302 | | |
| | 71 | 303 | | foreach (var ct in attr.ContentTypes) |
| | | 304 | | { |
| | 19 | 305 | | if (ct is null) |
| | | 306 | | { |
| | | 307 | | continue; |
| | | 308 | | } |
| | | 309 | | |
| | 19 | 310 | | if (ct.StartsWith("multipart/", StringComparison.OrdinalIgnoreCase)) |
| | | 311 | | { |
| | 5 | 312 | | return true; |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | |
| | 14 | 316 | | return false; |
| | | 317 | | } |
| | | 318 | | |
| | 5 | 319 | | private static bool IsComplexType(Type type) => !type.IsEnum && type != typeof(string) && !type.IsPrimitive; |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Attaches a rule to its parent container rule when a scope is provided. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <param name="host">The Kestrun host runtime.</param> |
| | | 325 | | /// <param name="scopeName">The parent multipart scope name.</param> |
| | | 326 | | /// <param name="childRule">The rule to attach.</param> |
| | | 327 | | private static void AttachNestedRule(Hosting.KestrunHost host, string? scopeName, KrFormPartRule childRule) |
| | | 328 | | { |
| | 18 | 329 | | if (string.IsNullOrWhiteSpace(scopeName)) |
| | | 330 | | { |
| | 15 | 331 | | return; |
| | | 332 | | } |
| | | 333 | | |
| | 3 | 334 | | if (!host.Runtime.FormPartRules.TryGetValue(scopeName, out var parentRule)) |
| | | 335 | | { |
| | 0 | 336 | | return; |
| | | 337 | | } |
| | | 338 | | |
| | 8 | 339 | | for (var i = 0; i < parentRule.NestedRules.Count; i++) |
| | | 340 | | { |
| | 1 | 341 | | var existing = parentRule.NestedRules[i]; |
| | 1 | 342 | | if (string.Equals(existing.Name, childRule.Name, StringComparison.OrdinalIgnoreCase) && |
| | 1 | 343 | | string.Equals(existing.Scope, childRule.Scope, StringComparison.OrdinalIgnoreCase)) |
| | | 344 | | { |
| | 0 | 345 | | return; |
| | | 346 | | } |
| | | 347 | | } |
| | | 348 | | |
| | 3 | 349 | | parentRule.NestedRules.Add(childRule); |
| | 3 | 350 | | } |
| | | 351 | | |
| | | 352 | | /// <summary> |
| | | 353 | | /// Applies KrBindFormAttribute to form options. |
| | | 354 | | /// </summary> |
| | | 355 | | /// <param name="attr">The KrBindFormAttribute instance to apply.</param> |
| | | 356 | | /// <returns>A KrFormOptions object configured based on the attribute.</returns> |
| | | 357 | | internal static KrFormOptions ApplyKrPartAttributes(KrBindFormAttribute attr) |
| | | 358 | | { |
| | 5 | 359 | | var formOptions = new KrFormOptions |
| | 5 | 360 | | { |
| | 5 | 361 | | ComputeSha256 = attr.ComputeSha256, |
| | 5 | 362 | | EnablePartDecompression = attr.EnablePartDecompression, |
| | 5 | 363 | | RejectUnknownRequestContentType = attr.RejectUnknownRequestContentType, |
| | 5 | 364 | | RejectUnknownContentEncoding = attr.RejectUnknownContentEncoding |
| | 5 | 365 | | }; |
| | | 366 | | |
| | 5 | 367 | | if (attr.DefaultUploadPath is not null) |
| | | 368 | | { |
| | 1 | 369 | | formOptions.DefaultUploadPath = attr.DefaultUploadPath; |
| | | 370 | | } |
| | | 371 | | |
| | 5 | 372 | | if (attr.MaxDecompressedBytesPerPart > 0) |
| | | 373 | | { |
| | 1 | 374 | | formOptions.MaxDecompressedBytesPerPart = attr.MaxDecompressedBytesPerPart; |
| | | 375 | | } |
| | | 376 | | |
| | 5 | 377 | | if (attr.AllowedPartContentEncodings is not null) |
| | | 378 | | { |
| | 1 | 379 | | formOptions.AllowedPartContentEncodings.Clear(); |
| | 1 | 380 | | formOptions.AllowedPartContentEncodings.AddRange(attr.AllowedPartContentEncodings); |
| | | 381 | | } |
| | 5 | 382 | | if (attr.MaxRequestBodyBytes > 0) |
| | | 383 | | { |
| | 1 | 384 | | formOptions.Limits.MaxRequestBodyBytes = attr.MaxRequestBodyBytes; |
| | | 385 | | } |
| | 5 | 386 | | if (attr.MaxPartBodyBytes > 0) |
| | | 387 | | { |
| | 1 | 388 | | formOptions.Limits.MaxPartBodyBytes = attr.MaxPartBodyBytes; |
| | | 389 | | } |
| | 5 | 390 | | if (attr.MaxParts > 0) |
| | | 391 | | { |
| | 1 | 392 | | formOptions.Limits.MaxParts = attr.MaxParts; |
| | | 393 | | } |
| | 5 | 394 | | if (attr.MaxHeaderBytesPerPart > 0) |
| | | 395 | | { |
| | 1 | 396 | | formOptions.Limits.MaxHeaderBytesPerPart = attr.MaxHeaderBytesPerPart; |
| | | 397 | | } |
| | 5 | 398 | | if (attr.MaxFieldValueBytes > 0) |
| | | 399 | | { |
| | 1 | 400 | | formOptions.Limits.MaxFieldValueBytes = attr.MaxFieldValueBytes; |
| | | 401 | | } |
| | 5 | 402 | | if (attr.MaxNestingDepth > 0) |
| | | 403 | | { |
| | 5 | 404 | | formOptions.Limits.MaxNestingDepth = attr.MaxNestingDepth; |
| | | 405 | | } |
| | 5 | 406 | | return formOptions; |
| | | 407 | | } |
| | | 408 | | } |