using Microsoft.AspNetCore.Http; namespace Febe.Web.Demo; /// Scenes of the demo engine (spec §10.6), chosen by address: ?scena=…. public enum DemoScene { /// «normale»: between two sessions, «In ascolto». Normal, /// «sessione»: a session in progress. Session, /// «fermo»: stopped for a verification, red strip on every page. Stopped, /// «scollegato»: no Instagram session yet. Disconnected, /// «errore»: a configuration problem (missing password): the interface shows only the problem page. Error, } /// /// Per-request choices of the demo: the scene (scena) and the shell it imitates (veste: desktop or docker). /// The page carries them on every call as query parameters or headers (X-Febe-Scena, X-Febe-Veste), so /// every page stays in the chosen scene. Outside the demo they are ignored. /// public static class DemoRequest { public const string SceneQuery = "scena"; public const string EditionQuery = "veste"; public const string SceneHeader = "X-Febe-Scena"; public const string EditionHeader = "X-Febe-Veste"; private const string SceneItem = "febe.demo.scena"; private const string EditionItem = "febe.demo.veste"; public static IReadOnlyList SceneNames { get; } = ["normale", "sessione", "fermo", "scollegato", "errore"]; public static DemoScene? ParseScene(string? text) => text?.Trim().ToLowerInvariant() switch { "normale" => DemoScene.Normal, "sessione" => DemoScene.Session, "fermo" => DemoScene.Stopped, "scollegato" => DemoScene.Disconnected, "errore" => DemoScene.Error, _ => null, }; public static string SceneName(DemoScene scene) => scene switch { DemoScene.Session => "sessione", DemoScene.Stopped => "fermo", DemoScene.Disconnected => "scollegato", DemoScene.Error => "errore", _ => "normale", }; public static FebeEdition? ParseEdition(string? text) => text?.Trim().ToLowerInvariant() switch { "desktop" or "windows" => FebeEdition.Desktop, "docker" => FebeEdition.Docker, _ => null, }; /// The scene of this request (normal when not given). public static DemoScene Scene(HttpContext? context) => context?.Items.TryGetValue(SceneItem, out var value) == true && value is DemoScene scene ? scene : DemoScene.Normal; /// The shell this request imitates, or null when not given. public static FebeEdition? Edition(HttpContext? context) => context?.Items.TryGetValue(EditionItem, out var value) == true && value is FebeEdition edition ? edition : null; internal static void Capture(HttpContext context) { var scene = ParseScene(First(context, SceneQuery, SceneHeader)); if (scene is not null) { context.Items[SceneItem] = scene.Value; } var edition = ParseEdition(First(context, EditionQuery, EditionHeader)); if (edition is not null) { context.Items[EditionItem] = edition.Value; } } private static string? First(HttpContext context, string query, string header) { var fromQuery = context.Request.Query[query].ToString(); if (fromQuery.Length > 0) { return fromQuery; } var fromHeader = context.Request.Headers[header].ToString(); return fromHeader.Length > 0 ? fromHeader : null; } } /// Reads the demo choices of each request (registered only in demo mode). public sealed class DemoRequestMiddleware(RequestDelegate next) { public Task InvokeAsync(HttpContext context) { ArgumentNullException.ThrowIfNull(context); DemoRequest.Capture(context); return next(context); } }