From f04f7811536ba6e35c4ad6357fb6904826eda9dc Mon Sep 17 00:00:00 2001 From: Dan Volchek Date: Sun, 25 Aug 2019 17:21:03 -0700 Subject: rename namespace to be more accurate --- .../ClientPlatformMiddleware.cs | 52 ++++++++++++++++++++++ .../ClientPlatformMiddlewareExtensions.cs | 16 +++++++ .../Framework/ClientPlatformDetection/Platform.cs | 18 ++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs create mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs create mode 100644 src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs (limited to 'src/SMAPI.Web/Framework/ClientPlatformDetection') diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs new file mode 100644 index 00000000..2430d239 --- /dev/null +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs @@ -0,0 +1,52 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; + +namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection +{ + /// Middleware that detects the client's platform. + public class ClientPlatformMiddleware + { + /// The key used to retrieve the client's platform from . + public const string ClientPlatformKey = "ClientPlatformKey"; + + /// The next delegate in the middleware pipeline. + private readonly RequestDelegate Next; + + /// Construct an instance. + /// The next delegate in the middleware pipeline. + public ClientPlatformMiddleware(RequestDelegate next) + { + this.Next = next; + } + + /// Invoke the middleware. + /// The HTTP request context. + public async Task InvokeAsync(HttpContext context) + { + context.Items[ClientPlatformMiddleware.ClientPlatformKey] = this.DetectClientPlatform(context.Request.Headers["User-Agent"]); + + await this.Next(context); + } + + /// Detect the platform that the client is on. + /// The client's user agent. + /// The client's platform, or null if no platforms could be detected. + private Platform? DetectClientPlatform(string userAgent) + { + switch (userAgent) + { + case string ua when ua.Contains("Windows"): + return Platform.Windows; + // check for Android before Linux because Android user agents also contain Linux + case string ua when ua.Contains("Android"): + return Platform.Android; + case string ua when ua.Contains("Linux"): + return Platform.Linux; + case string ua when ua.Contains("Mac"): + return Platform.Mac; + default: + return null; + } + } + } +} diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs new file mode 100644 index 00000000..274e5486 --- /dev/null +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Builder; + +namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection +{ + /// Extension methods for the client platform middleware. + internal static class ClientPlatformMiddlewareExtensions + { + /// Adds client platform detection to the request pipeline. + /// The application builder. + /// The application builder with the client platform middleware enabled. + public static IApplicationBuilder UseClientPlatform(this IApplicationBuilder builder) + { + return builder.UseMiddleware(); + } + } +} diff --git a/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs b/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs new file mode 100644 index 00000000..2fe1d324 --- /dev/null +++ b/src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs @@ -0,0 +1,18 @@ +namespace StardewModdingAPI.Web.Framework.ClientPlatformDetection +{ + /// A software platform. + public enum Platform + { + /// The Android platform. + Android, + + /// The Linux platform. + Linux, + + /// The Mac platform. + Mac, + + /// The Windows platform. + Windows + } +} -- cgit