| | 1 | | using System.Reflection; |
| | 2 | | using Kestrun.Hosting; |
| | 3 | | using Kestrun.SharedState; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Utilities; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Provides utility methods for mapping and flattening variables from various sources. |
| | 9 | | /// </summary> |
| | 10 | | public static class VariablesMap |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Populates the provided dictionary with variables from the request context and shared state store. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="ctx">The Kestrun context containing request information.</param> |
| | 16 | | /// <param name="vars">The dictionary to populate with variables.</param> |
| | 17 | | /// <returns>True if variables were successfully mapped; otherwise, false.</returns> |
| | 18 | | public static bool GetVariablesMap(KestrunContext ctx, ref Dictionary<string, object?> vars) |
| | 19 | | { |
| | 20 | | // ① Initialize the dictionary |
| 1 | 21 | | vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase); |
| 1 | 22 | | return GetCommonProperties(ctx, ref vars) // ② Add common request properties |
| 1 | 23 | | && GetSharedStateStore(ref vars); // ③ Add shared state variables |
| | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Populates the provided dictionary with variables from the shared state store. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="vars">The dictionary to populate with shared state variables.</param> |
| | 30 | | /// <returns>True if variables were successfully mapped; otherwise, false.</returns> |
| | 31 | | public static bool GetSharedStateStore(ref Dictionary<string, object?> vars) |
| | 32 | | { |
| 1 | 33 | | vars ??= new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase); |
| 6 | 34 | | foreach (var kv in SharedStateStore.Snapshot()) |
| | 35 | | { |
| 2 | 36 | | vars[kv.Key] = kv.Value; // 1) top-level JSON |
| | 37 | | } |
| | 38 | |
|
| 1 | 39 | | return true; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Populates the provided dictionary with common request and server properties from the Kestrun context. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="ctx">The Kestrun context containing request information.</param> |
| | 46 | | /// <param name="vars">The dictionary to populate with common properties.</param> |
| | 47 | | /// <returns>True if properties were successfully mapped; otherwise, false.</returns> |
| | 48 | | public static bool GetCommonProperties(KestrunContext ctx, ref Dictionary<string, object?> vars) |
| | 49 | | { |
| | 50 | | // ① Initialize the dictionary |
| 2 | 51 | | vars["Context"] = ctx; |
| 2 | 52 | | vars["Request"] = ctx.Request; |
| 2 | 53 | | vars["QueryString"] = ctx.Request.Query.ToDictionary(q => q.Key, q => q.Value.ToString()); |
| 2 | 54 | | vars["Form"] = ctx.Request.Form; |
| 2 | 55 | | vars["Cookies"] = ctx.Request.Cookies; |
| 6 | 56 | | vars["Headers"] = ctx.Request.Headers.ToDictionary(h => h.Key, h => h.Value.ToString()); |
| 2 | 57 | | vars["UserAgent"] = ctx.Request.Headers["User-Agent"].ToString(); |
| 2 | 58 | | vars["ServerVersion"] = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; |
| 2 | 59 | | vars["ServerOS"] = Environment.OSVersion.ToString(); |
| 2 | 60 | | vars["ServerArch"] = Environment.Is64BitOperatingSystem ? "x64" : "x86"; |
| 2 | 61 | | vars["ServerIP"] = ctx.HttpContext.Connection.LocalIpAddress?.ToString() ?? "unknown"; |
| 2 | 62 | | vars["ServerPort"] = ctx.HttpContext.Connection.LocalPort; |
| 2 | 63 | | vars["ServerName"] = Environment.MachineName; |
| 2 | 64 | | vars["Timestamp"] = DateTimeOffset.UtcNow.ToString("O"); |
| | 65 | |
|
| 2 | 66 | | return true; |
| | 67 | | } |
| | 68 | | } |
| | 69 | |
|