diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-29 19:40:47 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-29 19:40:47 -0400 |
commit | ef90bdbde5b473d866c74053a7dd23fbd0691b53 (patch) | |
tree | db8067999193769a53eacc1b5fa225cfd1a3e45f /src/SMAPI.Web/Framework/RewriteRules | |
parent | 3d8bdacc8cb5c9d5514e052d5d4c1d5f2dbc6e9e (diff) | |
parent | 650d729bc3e58ec1ecbb554805f8600f63b59a13 (diff) | |
download | SMAPI-ef90bdbde5b473d866c74053a7dd23fbd0691b53.tar.gz SMAPI-ef90bdbde5b473d866c74053a7dd23fbd0691b53.tar.bz2 SMAPI-ef90bdbde5b473d866c74053a7dd23fbd0691b53.zip |
Merge branch 'add-log-parser' into develop
Diffstat (limited to 'src/SMAPI.Web/Framework/RewriteRules')
-rw-r--r-- | src/SMAPI.Web/Framework/RewriteRules/ConditionalRedirectToHttpsRule.cs | 62 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs | 48 |
2 files changed, 110 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/RewriteRules/ConditionalRedirectToHttpsRule.cs b/src/SMAPI.Web/Framework/RewriteRules/ConditionalRedirectToHttpsRule.cs new file mode 100644 index 00000000..d6a56bb7 --- /dev/null +++ b/src/SMAPI.Web/Framework/RewriteRules/ConditionalRedirectToHttpsRule.cs @@ -0,0 +1,62 @@ +using System; +using System.Net; +using System.Text; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite; + +namespace StardewModdingAPI.Web.Framework.RewriteRules +{ + /// <summary>Redirect requests to HTTPS.</summary> + /// <remarks>Derived from <a href="https://stackoverflow.com/a/44526747/262123" /> and <see cref="Microsoft.AspNetCore.Rewrite.Internal.RedirectToHttpsRule"/>.</remarks> + internal class ConditionalRedirectToHttpsRule : IRule + { + /********* + ** Properties + *********/ + /// <summary>A predicate which indicates when the rule should be applied.</summary> + private readonly Func<HttpRequest, bool> ShouldRewrite; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param> + public ConditionalRedirectToHttpsRule(Func<HttpRequest, bool> shouldRewrite = null) + { + this.ShouldRewrite = shouldRewrite ?? (req => true); + } + + /// <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) + { + HttpRequest request = context.HttpContext.Request; + + // check condition + if (this.IsSecure(request) || !this.ShouldRewrite(request)) + return; + + // redirect request + HttpResponse response = context.HttpContext.Response; + response.StatusCode = (int)HttpStatusCode.RedirectKeepVerb; + response.Headers["Location"] = new StringBuilder() + .Append("https://") + .Append(request.Host.Host) + .Append(request.PathBase) + .Append(request.Path) + .Append(request.QueryString) + .ToString(); + context.Result = RuleResult.EndResponse; + } + + /// <summary>Get whether the request was received over HTTPS.</summary> + /// <param name="request">The request to check.</param> + public bool IsSecure(HttpRequest request) + { + return + request.IsHttps // HTTPS to server + || string.Equals(request.Headers["x-forwarded-proto"], "HTTPS", StringComparison.OrdinalIgnoreCase); // HTTPS to AWS load balancer + } + } +} diff --git a/src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs b/src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs new file mode 100644 index 00000000..920632ab --- /dev/null +++ b/src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs @@ -0,0 +1,48 @@ +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite; + +namespace StardewModdingAPI.Web.Framework.RewriteRules +{ + /// <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> + internal class ConditionalRewriteSubdomainRule : IRule + { + /********* + ** Accessors + *********/ + /// <summary>A predicate which indicates when the rule should be applied.</summary> + private readonly Func<HttpRequest, bool> ShouldRewrite; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param> + public ConditionalRewriteSubdomainRule(Func<HttpRequest, bool> shouldRewrite = null) + { + this.ShouldRewrite = shouldRewrite ?? (req => true); + } + + /// <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) + { + HttpRequest request = context.HttpContext.Request; + + // check condition + if (!this.ShouldRewrite(request)) + return; + + // get host parts + string host = request.Host.Host; + string[] parts = host.Split('.'); + if (parts.Length < 2) + return; + + // prepend to path + request.Path = $"/{parts[0]}{request.Path}"; + } + } +} |