diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-29 22:18:08 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-29 22:18:08 -0400 |
commit | 13baaf8920f4a80ac3c0cd41a16b9afb1b993048 (patch) | |
tree | 3f8e976fff0d5092b03a29862bae8f8814e7d9df /src/SMAPI.Web/Framework/RewriteRules | |
parent | c3cd9a3120cd74cb1dfc4455d3ecca203b163013 (diff) | |
download | SMAPI-13baaf8920f4a80ac3c0cd41a16b9afb1b993048.tar.gz SMAPI-13baaf8920f4a80ac3c0cd41a16b9afb1b993048.tar.bz2 SMAPI-13baaf8920f4a80ac3c0cd41a16b9afb1b993048.zip |
add smapi.io shortcut URLs (#375)
Diffstat (limited to 'src/SMAPI.Web/Framework/RewriteRules')
-rw-r--r-- | src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs b/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs new file mode 100644 index 00000000..0719e311 --- /dev/null +++ b/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs @@ -0,0 +1,61 @@ +using System; +using System.Net; +using System.Text.RegularExpressions; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Rewrite; + +namespace StardewModdingAPI.Web.Framework.RewriteRules +{ + /// <summary>Redirect requests to an external URL if they match a condition.</summary> + internal class RedirectToUrlRule : IRule + { + /********* + ** Properties + *********/ + /// <summary>A predicate which indicates when the rule should be applied.</summary> + private readonly Func<HttpRequest, bool> ShouldRewrite; + + /// <summary>The new URL to which to redirect.</summary> + private readonly string NewUrl; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="shouldRewrite">A predicate which indicates when the rule should be applied.</param> + /// <param name="url">The new URL to which to redirect.</param> + public RedirectToUrlRule(Func<HttpRequest, bool> shouldRewrite, string url) + { + this.ShouldRewrite = shouldRewrite ?? (req => true); + this.NewUrl = url; + } + + /// <summary>Construct an instance.</summary> + /// <param name="pathRegex">A case-insensitive regex to match against the path.</param> + /// <param name="url">The external URL.</param> + public RedirectToUrlRule(string pathRegex, string url) + { + Regex regex = new Regex(pathRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled); + this.ShouldRewrite = req => req.Path.HasValue && regex.IsMatch(req.Path.Value); + this.NewUrl = url; + } + + /// <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; + + // redirect request + HttpResponse response = context.HttpContext.Response; + response.StatusCode = (int)HttpStatusCode.Redirect; + response.Headers["Location"] = this.NewUrl; + context.Result = RuleResult.EndResponse; + } + } +} |