Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Febe.Web.Demo;
|
|
|
|
/// <summary>Scenes of the demo engine (spec §10.6), chosen by address: <c>?scena=…</c>.</summary>
|
|
public enum DemoScene
|
|
{
|
|
/// <summary>«normale»: between two sessions, «In ascolto».</summary>
|
|
Normal,
|
|
|
|
/// <summary>«sessione»: a session in progress.</summary>
|
|
Session,
|
|
|
|
/// <summary>«fermo»: stopped for a verification, red strip on every page.</summary>
|
|
Stopped,
|
|
|
|
/// <summary>«scollegato»: no Instagram session yet.</summary>
|
|
Disconnected,
|
|
|
|
/// <summary>«errore»: a configuration problem (missing password): the interface shows only the problem page.</summary>
|
|
Error,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Per-request choices of the demo: the scene (<c>scena</c>) and the shell it imitates (<c>veste</c>: desktop or docker).
|
|
/// The page carries them on every call as query parameters or headers (<c>X-Febe-Scena</c>, <c>X-Febe-Veste</c>), so
|
|
/// every page stays in the chosen scene. Outside the demo they are ignored.
|
|
/// </summary>
|
|
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<string> 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,
|
|
};
|
|
|
|
/// <summary>The scene of this request (normal when not given).</summary>
|
|
public static DemoScene Scene(HttpContext? context) =>
|
|
context?.Items.TryGetValue(SceneItem, out var value) == true && value is DemoScene scene ? scene : DemoScene.Normal;
|
|
|
|
/// <summary>The shell this request imitates, or null when not given.</summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>Reads the demo choices of each request (registered only in demo mode).</summary>
|
|
public sealed class DemoRequestMiddleware(RequestDelegate next)
|
|
{
|
|
public Task InvokeAsync(HttpContext context)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(context);
|
|
DemoRequest.Capture(context);
|
|
return next(context);
|
|
}
|
|
}
|