| | | 1 | | |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Management.Automation; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | using System.Xml.Linq; |
| | | 7 | | using Kestrun.Logging; |
| | | 8 | | using Kestrun.Models; |
| | | 9 | | using Microsoft.Extensions.Primitives; |
| | | 10 | | using Microsoft.OpenApi; |
| | | 11 | | using YamlDotNet.Serialization; |
| | | 12 | | using YamlDotNet.Serialization.NamingConventions; |
| | | 13 | | |
| | | 14 | | namespace Kestrun.Languages; |
| | | 15 | | /// <summary> |
| | | 16 | | /// Information about a parameter to be injected into a script. |
| | | 17 | | /// </summary> |
| | | 18 | | public record ParameterForInjectionInfo |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The name of the parameter. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public string Name { get; init; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The .NET type of the parameter. |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public Type ParameterType { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The JSON schema type of the parameter. |
| | | 32 | | /// </summary> |
| | 0 | 33 | | public JsonSchemaType? Type { get; init; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The default value of the parameter. |
| | | 37 | | /// </summary> |
| | 0 | 38 | | public JsonNode? DefaultValue { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The location of the parameter. |
| | | 42 | | /// </summary> |
| | 0 | 43 | | public ParameterLocation? In { get; init; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Indicates whether the parameter is from the request body. |
| | | 47 | | /// </summary> |
| | 0 | 48 | | public bool IsRequestBody => In is null; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Constructs a ParameterForInjectionInfo from an OpenApiParameter. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="paramInfo">The parameter metadata.</param> |
| | | 54 | | /// <param name="parameter">The OpenApiParameter to construct from.</param> |
| | 0 | 55 | | public ParameterForInjectionInfo(ParameterMetadata paramInfo, OpenApiParameter? parameter) |
| | | 56 | | { |
| | 0 | 57 | | ArgumentNullException.ThrowIfNull(parameter); |
| | 0 | 58 | | ArgumentNullException.ThrowIfNull(paramInfo); |
| | 0 | 59 | | Name = paramInfo.Name; |
| | 0 | 60 | | ParameterType = paramInfo.ParameterType; |
| | 0 | 61 | | Type = parameter.Schema?.Type; |
| | 0 | 62 | | DefaultValue = parameter.Schema?.Default; |
| | 0 | 63 | | In = parameter.In; |
| | 0 | 64 | | } |
| | | 65 | | /// <summary> |
| | | 66 | | /// Constructs a ParameterForInjectionInfo from an OpenApiRequestBody. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="paramInfo">The parameter metadata.</param> |
| | | 69 | | /// <param name="requestBody">The OpenApiRequestBody to construct from.</param> |
| | 0 | 70 | | public ParameterForInjectionInfo(ParameterMetadata paramInfo, OpenApiRequestBody requestBody) |
| | | 71 | | { |
| | 0 | 72 | | ArgumentNullException.ThrowIfNull(requestBody); |
| | 0 | 73 | | ArgumentNullException.ThrowIfNull(paramInfo); |
| | 0 | 74 | | Name = paramInfo.Name; |
| | 0 | 75 | | ParameterType = paramInfo.ParameterType; |
| | 0 | 76 | | Type = requestBody.Content?.Values.FirstOrDefault()?.Schema?.Type; |
| | 0 | 77 | | var schema = requestBody.Content?.Values.FirstOrDefault()?.Schema; |
| | 0 | 78 | | if (schema is OpenApiSchemaReference) |
| | | 79 | | { |
| | 0 | 80 | | Type = JsonSchemaType.Object; |
| | | 81 | | } |
| | 0 | 82 | | else if (schema is OpenApiSchema sch) |
| | | 83 | | { |
| | 0 | 84 | | Type = sch.Type; |
| | 0 | 85 | | DefaultValue = sch.Default; |
| | | 86 | | } |
| | 0 | 87 | | In = null; |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Adds parameters from the HTTP context to the PowerShell instance. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="context">The current HTTP context.</param> |
| | | 94 | | /// <param name="ps">The PowerShell instance to which parameters will be added.</param> |
| | | 95 | | internal static void InjectParameters(KestrunContext context, PowerShell ps) |
| | | 96 | | { |
| | 4 | 97 | | if (context.HttpContext.GetEndpoint()? |
| | 4 | 98 | | .Metadata |
| | 4 | 99 | | .FirstOrDefault(m => m is List<ParameterForInjectionInfo>) is not List<ParameterForInjectionInfo> paramet |
| | | 100 | | { |
| | 4 | 101 | | return; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | var logger = context.Host.Logger; |
| | 0 | 105 | | if (logger.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 106 | | { |
| | 0 | 107 | | logger.Debug("Injecting {Count} parameters into PowerShell script.", parameters.Count); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | foreach (var param in parameters) |
| | | 111 | | { |
| | 0 | 112 | | InjectSingleParameter(context, ps, param); |
| | | 113 | | } |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Injects a single parameter into the PowerShell instance based on its location and type. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="context">The current Kestrun context.</param> |
| | | 120 | | /// <param name="ps">The PowerShell instance to inject parameters into.</param> |
| | | 121 | | /// <param name="param">The parameter information to inject.</param> |
| | | 122 | | private static void InjectSingleParameter(KestrunContext context, PowerShell ps, ParameterForInjectionInfo param) |
| | | 123 | | { |
| | 0 | 124 | | var logger = context.Host.Logger; |
| | 0 | 125 | | var name = param.Name; |
| | | 126 | | |
| | 0 | 127 | | if (logger.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 128 | | { |
| | 0 | 129 | | logger.Debug("Injecting parameter '{Name}' of type '{Type}' from '{In}'.", name, param.Type, param.In); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | object? converted; |
| | 0 | 133 | | var shouldLog = true; |
| | | 134 | | |
| | 0 | 135 | | converted = context.Request.Form is not null && context.Request.HasFormContentType |
| | 0 | 136 | | ? ConvertFormToValue(context.Request.Form, param) |
| | 0 | 137 | | : GetParameterValueFromContext(context, param, out shouldLog); |
| | | 138 | | |
| | 0 | 139 | | if (shouldLog && logger.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 140 | | { |
| | 0 | 141 | | logger.DebugSanitized("Adding parameter '{Name}': {ConvertedValue}", name, converted); |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | _ = ps.AddParameter(name, converted); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | private static object? GetParameterValueFromContext(KestrunContext context, ParameterForInjectionInfo param, out boo |
| | | 148 | | { |
| | 0 | 149 | | shouldLog = true; |
| | 0 | 150 | | var logger = context.Host.Logger; |
| | 0 | 151 | | var raw = GetRawValue(param, context); |
| | | 152 | | |
| | 0 | 153 | | if (raw is null) |
| | | 154 | | { |
| | 0 | 155 | | if (param.DefaultValue is not null) |
| | | 156 | | { |
| | 0 | 157 | | raw = param.DefaultValue.GetValue<object>(); |
| | | 158 | | } |
| | | 159 | | else |
| | | 160 | | { |
| | 0 | 161 | | shouldLog = false; |
| | 0 | 162 | | return null; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | if (logger.IsEnabled(Serilog.Events.LogEventLevel.Debug)) |
| | | 167 | | { |
| | 0 | 168 | | logger.Debug("Raw value for parameter '{Name}': {RawValue}", param.Name, raw); |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | var (singleValue, multiValue) = NormalizeRaw(raw); |
| | | 172 | | |
| | 0 | 173 | | if (singleValue is null && multiValue is null) |
| | | 174 | | { |
| | 0 | 175 | | shouldLog = false; |
| | 0 | 176 | | return null; |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | return ConvertValue(context, param, singleValue, multiValue); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Retrieves the raw value of a parameter from the HTTP context based on its location. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <param name="param">The parameter information.</param> |
| | | 186 | | /// <param name="context">The current HTTP context.</param> |
| | | 187 | | /// <returns>The raw value of the parameter.</returns> |
| | | 188 | | private static object? GetRawValue(ParameterForInjectionInfo param, KestrunContext context) |
| | | 189 | | { |
| | 0 | 190 | | return param.In switch |
| | 0 | 191 | | { |
| | 0 | 192 | | ParameterLocation.Path => |
| | 0 | 193 | | context.Request.RouteValues.TryGetValue(param.Name, out var routeVal) |
| | 0 | 194 | | ? routeVal |
| | 0 | 195 | | : null, |
| | 0 | 196 | | |
| | 0 | 197 | | ParameterLocation.Query => |
| | 0 | 198 | | context.Request.Query.TryGetValue(param.Name, out var queryVal) |
| | 0 | 199 | | ? (string?)queryVal |
| | 0 | 200 | | : null, |
| | 0 | 201 | | |
| | 0 | 202 | | ParameterLocation.Header => |
| | 0 | 203 | | context.Request.Headers.TryGetValue(param.Name, out var headerVal) |
| | 0 | 204 | | ? (string?)headerVal |
| | 0 | 205 | | : null, |
| | 0 | 206 | | |
| | 0 | 207 | | ParameterLocation.Cookie => |
| | 0 | 208 | | context.Request.Cookies.TryGetValue(param.Name, out var cookieVal) |
| | 0 | 209 | | ? cookieVal |
| | 0 | 210 | | : null, |
| | 0 | 211 | | null => (context.Request.Form is not null && context.Request.HasFormContentType) ? |
| | 0 | 212 | | context.Request.Form : |
| | 0 | 213 | | context.Request.Body, |
| | 0 | 214 | | |
| | 0 | 215 | | _ => null, |
| | 0 | 216 | | }; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Normalizes the raw parameter value into single and multi-value forms. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <param name="raw">The raw parameter value.</param> |
| | | 223 | | /// <returns>A tuple containing the single and multi-value forms of the parameter.</returns> |
| | | 224 | | private static (string? single, string?[]? multi) NormalizeRaw(object raw) |
| | | 225 | | { |
| | 0 | 226 | | string?[]? multiValue = null; |
| | | 227 | | |
| | | 228 | | string? singleValue; |
| | | 229 | | switch (raw) |
| | | 230 | | { |
| | | 231 | | case StringValues sv: |
| | 0 | 232 | | multiValue = [.. sv]; |
| | 0 | 233 | | singleValue = sv.Count > 0 ? sv[0] : null; |
| | 0 | 234 | | break; |
| | | 235 | | |
| | | 236 | | case string s: |
| | 0 | 237 | | singleValue = s; |
| | 0 | 238 | | break; |
| | | 239 | | |
| | | 240 | | default: |
| | 0 | 241 | | singleValue = raw?.ToString(); |
| | | 242 | | break; |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return (singleValue, multiValue); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Converts the parameter value to the appropriate type based on the JSON schema type. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <param name="context">The current HTTP context.</param> |
| | | 252 | | /// <param name="param">The parameter information.</param> |
| | | 253 | | /// <param name="singleValue">The single value of the parameter.</param> |
| | | 254 | | /// <param name="multiValue">The multi-value of the parameter.</param> |
| | | 255 | | /// <returns>The converted parameter value.</returns> |
| | | 256 | | private static object? ConvertValue(KestrunContext context, ParameterForInjectionInfo param, |
| | | 257 | | string? singleValue, string?[]? multiValue) |
| | | 258 | | { |
| | | 259 | | // Convert based on schema type |
| | 0 | 260 | | return param.Type switch |
| | 0 | 261 | | { |
| | 0 | 262 | | JsonSchemaType.Integer => int.TryParse(singleValue, out var i) ? (int?)i : null, |
| | 0 | 263 | | JsonSchemaType.Number => double.TryParse(singleValue, out var d) ? (double?)d : null, |
| | 0 | 264 | | JsonSchemaType.Boolean => bool.TryParse(singleValue, out var b) ? (bool?)b : null, |
| | 0 | 265 | | JsonSchemaType.Array => multiValue ?? (singleValue is not null ? new[] { singleValue } : null), // keep your |
| | 0 | 266 | | JsonSchemaType.Object => |
| | 0 | 267 | | ConvertBodyBasedOnContentType(context, singleValue ?? ""), |
| | 0 | 268 | | JsonSchemaType.String => singleValue, |
| | 0 | 269 | | _ => singleValue, |
| | 0 | 270 | | }; |
| | | 271 | | } |
| | | 272 | | //todo: test Yaml and XML bodies |
| | | 273 | | private static object? ConvertBodyBasedOnContentType( |
| | | 274 | | KestrunContext context, |
| | | 275 | | string rawBodyString) |
| | | 276 | | { |
| | 0 | 277 | | var contentType = context.Request.ContentType?.ToLowerInvariant() ?? ""; |
| | | 278 | | |
| | 0 | 279 | | if (contentType.Contains("json")) |
| | | 280 | | { |
| | 0 | 281 | | return ConvertJsonToHashtable(rawBodyString); |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | if (contentType.Contains("yaml") || contentType.Contains("yml")) |
| | | 285 | | { |
| | 0 | 286 | | return ConvertYamlToHashtable(rawBodyString); |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | if (contentType.Contains("xml")) |
| | | 290 | | { |
| | 0 | 291 | | return ConvertXmlToHashtable(rawBodyString); |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | if (contentType.Contains("application/x-www-form-urlencoded")) |
| | | 295 | | { |
| | 0 | 296 | | return ConvertFormToHashtable(context.Request.Form); |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | return rawBodyString; // fallback |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | private static readonly IDeserializer YamlDeserializer = |
| | 0 | 303 | | new DeserializerBuilder() |
| | 0 | 304 | | .WithNamingConvention(CamelCaseNamingConvention.Instance) |
| | 0 | 305 | | .Build(); |
| | | 306 | | |
| | | 307 | | private static Hashtable? ConvertYamlToHashtable(string yaml) |
| | | 308 | | { |
| | 0 | 309 | | if (string.IsNullOrWhiteSpace(yaml)) |
| | | 310 | | { |
| | 0 | 311 | | return null; |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | // Top-level YAML mapping → Hashtable |
| | 0 | 315 | | var ht = YamlDeserializer.Deserialize<Hashtable>(yaml); |
| | 0 | 316 | | return ht; |
| | | 317 | | } |
| | | 318 | | private static object? ConvertJsonToHashtable(string? json) |
| | | 319 | | { |
| | 0 | 320 | | if (string.IsNullOrWhiteSpace(json)) |
| | | 321 | | { |
| | 0 | 322 | | return null; |
| | | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | using var doc = JsonDocument.Parse(json); |
| | 0 | 326 | | return JsonElementToClr(doc.RootElement); |
| | 0 | 327 | | } |
| | | 328 | | |
| | | 329 | | private static object? JsonElementToClr(JsonElement element) |
| | | 330 | | { |
| | 0 | 331 | | return element.ValueKind switch |
| | 0 | 332 | | { |
| | 0 | 333 | | JsonValueKind.Object => ToHashtable(element), |
| | 0 | 334 | | JsonValueKind.Array => ToArray(element), |
| | 0 | 335 | | JsonValueKind.String => element.GetString(), |
| | 0 | 336 | | JsonValueKind.Number => element.TryGetInt64(out var l) ? l : element.GetDouble(), |
| | 0 | 337 | | JsonValueKind.True => true, |
| | 0 | 338 | | JsonValueKind.False => false, |
| | 0 | 339 | | JsonValueKind.Null => null, |
| | 0 | 340 | | JsonValueKind.Undefined => null, |
| | 0 | 341 | | _ => null |
| | 0 | 342 | | }; |
| | | 343 | | } |
| | | 344 | | |
| | | 345 | | private static Hashtable ToHashtable(JsonElement element) |
| | | 346 | | { |
| | 0 | 347 | | var ht = new Hashtable(StringComparer.OrdinalIgnoreCase); |
| | 0 | 348 | | foreach (var prop in element.EnumerateObject()) |
| | | 349 | | { |
| | 0 | 350 | | ht[prop.Name] = JsonElementToClr(prop.Value); |
| | | 351 | | } |
| | 0 | 352 | | return ht; |
| | | 353 | | } |
| | | 354 | | |
| | | 355 | | private static object?[] ToArray(JsonElement element) |
| | | 356 | | { |
| | 0 | 357 | | var list = new List<object?>(); |
| | 0 | 358 | | foreach (var item in element.EnumerateArray()) |
| | | 359 | | { |
| | 0 | 360 | | list.Add(JsonElementToClr(item)); |
| | | 361 | | } |
| | 0 | 362 | | return [.. list]; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | private static object? ConvertXmlToHashtable(string xml) |
| | | 366 | | { |
| | 0 | 367 | | if (string.IsNullOrWhiteSpace(xml)) |
| | | 368 | | { |
| | 0 | 369 | | return null; |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | var root = XElement.Parse(xml); |
| | 0 | 373 | | return XElementToClr(root); |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | private static object? XElementToClr(XElement element) |
| | | 377 | | { |
| | | 378 | | // If element has no children and no attributes → return primitive string |
| | 0 | 379 | | if (!element.HasElements && !element.HasAttributes) |
| | | 380 | | { |
| | 0 | 381 | | var trimmed = element.Value?.Trim(); |
| | 0 | 382 | | return string.IsNullOrWhiteSpace(trimmed) ? null : trimmed; |
| | | 383 | | } |
| | | 384 | | |
| | 0 | 385 | | var ht = new Hashtable(StringComparer.OrdinalIgnoreCase); |
| | | 386 | | |
| | | 387 | | // Attributes as @name entries |
| | 0 | 388 | | foreach (var attr in element.Attributes()) |
| | | 389 | | { |
| | 0 | 390 | | ht["@" + attr.Name.LocalName] = attr.Value; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | // Children |
| | 0 | 394 | | var childGroups = element.Elements().GroupBy(e => e.Name.LocalName); |
| | | 395 | | |
| | 0 | 396 | | foreach (var group in childGroups) |
| | | 397 | | { |
| | 0 | 398 | | var key = group.Key; |
| | 0 | 399 | | var items = group.ToList(); |
| | | 400 | | |
| | 0 | 401 | | if (items.Count == 1) |
| | | 402 | | { |
| | | 403 | | // Single element → convert directly |
| | 0 | 404 | | ht[key] = XElementToClr(items[0]); |
| | | 405 | | } |
| | | 406 | | else |
| | | 407 | | { |
| | | 408 | | // Multiple elements with same name → array |
| | 0 | 409 | | var arr = new object?[items.Count]; |
| | 0 | 410 | | for (var i = 0; i < items.Count; i++) |
| | | 411 | | { |
| | 0 | 412 | | arr[i] = XElementToClr(items[i]); |
| | | 413 | | } |
| | | 414 | | |
| | 0 | 415 | | ht[key] = arr; |
| | | 416 | | } |
| | | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | return ht; |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | /// <summary> |
| | | 423 | | /// Converts a form dictionary to a hashtable. |
| | | 424 | | /// </summary> |
| | | 425 | | /// <param name="form">The form dictionary to convert.</param> |
| | | 426 | | /// <returns>A hashtable representing the form data.</returns> |
| | | 427 | | private static Hashtable? ConvertFormToHashtable(Dictionary<string, string>? form) |
| | | 428 | | { |
| | 0 | 429 | | if (form is null || form.Count == 0) |
| | | 430 | | { |
| | 0 | 431 | | return null; |
| | | 432 | | } |
| | | 433 | | |
| | 0 | 434 | | var ht = new Hashtable(StringComparer.OrdinalIgnoreCase); |
| | | 435 | | |
| | 0 | 436 | | foreach (var kvp in form) |
| | | 437 | | { |
| | | 438 | | // x-www-form-urlencoded in your case has a single value per key |
| | 0 | 439 | | ht[kvp.Key] = kvp.Value; |
| | | 440 | | } |
| | | 441 | | |
| | 0 | 442 | | return ht; |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | private static object? ConvertFormToValue(Dictionary<string, string>? form, ParameterForInjectionInfo param) |
| | | 446 | | { |
| | 0 | 447 | | if (form is null || form.Count == 0) |
| | | 448 | | { |
| | 0 | 449 | | return null; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | // If the parameter is a simple type, return the first key if there's only one key-value pair |
| | | 453 | | // and it's a simple type (not an object or array) |
| | 0 | 454 | | return param.Type is JsonSchemaType.Integer or JsonSchemaType.Number or JsonSchemaType.Boolean or JsonSchemaType |
| | 0 | 455 | | ? form.Count == 1 ? form.First().Key : null |
| | 0 | 456 | | : ConvertFormToHashtable(form); |
| | | 457 | | } |
| | | 458 | | } |