diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 20:19:12 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-22 20:19:12 -0400 |
commit | 2406d4b0a6fd8b2d439438687b0101a6f8934ca5 (patch) | |
tree | 0a7e374d1ef8badcc89979d91c19a565325a4871 /src/StardewModdingAPI.Web/Framework | |
parent | f8566067e0874428c53ffdb6cd12d3f91ad4c51c (diff) | |
download | SMAPI-2406d4b0a6fd8b2d439438687b0101a6f8934ca5.tar.gz SMAPI-2406d4b0a6fd8b2d439438687b0101a6f8934ca5.tar.bz2 SMAPI-2406d4b0a6fd8b2d439438687b0101a6f8934ca5.zip |
add subdomain rewrite rule (#336)
Diffstat (limited to 'src/StardewModdingAPI.Web/Framework')
-rw-r--r-- | src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs b/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs new file mode 100644 index 00000000..9b89cb65 --- /dev/null +++ b/src/StardewModdingAPI.Web/Framework/RewriteSubdomainRule.cs @@ -0,0 +1,30 @@ +using System; +using Microsoft.AspNetCore.Rewrite; + +namespace StardewModdingAPI.Web.Framework +{ + /// <summary>Rewrite requests to prepend the subdomain portion (if any) to the path.</summary> + /// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" />.</remarks> + public class RewriteSubdomainRule : IRule + { + /// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary> + /// <param name="context">The rewrite context.</param> + public void ApplyRule(RewriteContext context) + { + context.Result = RuleResult.ContinueRules; + + // get host parts + string host = context.HttpContext.Request.Host.Host; + string[] parts = host.Split('.'); + + // validate + if (parts.Length < 2) + return; + if (parts.Length < 3 && !"localhost".Equals(parts[1], StringComparison.InvariantCultureIgnoreCase)) + return; + + // prepend to path + context.HttpContext.Request.Path = $"/{parts[0]}{context.HttpContext.Request.Path}"; + } + } +} |