summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/ClientPlatformDetection
diff options
context:
space:
mode:
authorDan Volchek <volchek2@illinois.edu>2019-08-25 17:21:03 -0700
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 19:14:24 -0400
commitf04f7811536ba6e35c4ad6357fb6904826eda9dc (patch)
treefac7f183ecac6ed63f29e1c01186ffdf46d9af7c /src/SMAPI.Web/Framework/ClientPlatformDetection
parent00b067fead282bb30eb8ca67572f16bd6e1920b7 (diff)
downloadSMAPI-f04f7811536ba6e35c4ad6357fb6904826eda9dc.tar.gz
SMAPI-f04f7811536ba6e35c4ad6357fb6904826eda9dc.tar.bz2
SMAPI-f04f7811536ba6e35c4ad6357fb6904826eda9dc.zip
rename namespace to be more accurate
Diffstat (limited to 'src/SMAPI.Web/Framework/ClientPlatformDetection')
-rw-r--r--src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddleware.cs52
-rw-r--r--src/SMAPI.Web/Framework/ClientPlatformDetection/ClientPlatformMiddlewareExtensions.cs16
-rw-r--r--src/SMAPI.Web/Framework/ClientPlatformDetection/Platform.cs18
3 files changed, 86 insertions, 0 deletions
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
+{
+ /// <summary>Middleware that detects the client's platform.</summary>
+ public class ClientPlatformMiddleware
+ {
+ /// <summary>The key used to retrieve the client's platform from <see cref="HttpContext.Items"/>.</summary>
+ public const string ClientPlatformKey = "ClientPlatformKey";
+
+ /// <summary>The next delegate in the middleware pipeline.</summary>
+ private readonly RequestDelegate Next;
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="next">The next delegate in the middleware pipeline.</param>
+ public ClientPlatformMiddleware(RequestDelegate next)
+ {
+ this.Next = next;
+ }
+
+ /// <summary>Invoke the middleware.</summary>
+ /// <param name="context">The HTTP request context.</param>
+ public async Task InvokeAsync(HttpContext context)
+ {
+ context.Items[ClientPlatformMiddleware.ClientPlatformKey] = this.DetectClientPlatform(context.Request.Headers["User-Agent"]);
+
+ await this.Next(context);
+ }
+
+ /// <summary>Detect the platform that the client is on.</summary>
+ /// <param name="userAgent">The client's user agent.</param>
+ /// <returns>The client's platform, or null if no platforms could be detected.</returns>
+ 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
+{
+ /// <summary>Extension methods for the client platform middleware.</summary>
+ internal static class ClientPlatformMiddlewareExtensions
+ {
+ /// <summary>Adds client platform detection to the request pipeline.</summary>
+ /// <param name="builder">The application builder.</param>
+ /// <returns>The application builder with the client platform middleware enabled.</returns>
+ public static IApplicationBuilder UseClientPlatform(this IApplicationBuilder builder)
+ {
+ return builder.UseMiddleware<ClientPlatformMiddleware>();
+ }
+ }
+}
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
+{
+ /// <summary>A software platform.</summary>
+ public enum Platform
+ {
+ /// <summary>The Android platform.</summary>
+ Android,
+
+ /// <summary>The Linux platform.</summary>
+ Linux,
+
+ /// <summary>The Mac platform.</summary>
+ Mac,
+
+ /// <summary>The Windows platform.</summary>
+ Windows
+ }
+}