summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs (renamed from src/SMAPI.Web/Framework/RewriteSubdomainRule.cs)26
-rw-r--r--src/SMAPI.Web/Startup.cs18
2 files changed, 23 insertions, 21 deletions
diff --git a/src/SMAPI.Web/Framework/RewriteSubdomainRule.cs b/src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs
index cc183fe3..83f23058 100644
--- a/src/SMAPI.Web/Framework/RewriteSubdomainRule.cs
+++ b/src/SMAPI.Web/Framework/RewriteRules/ConditionalRewriteSubdomainRule.cs
@@ -1,45 +1,45 @@
using System;
-using System.Linq;
-using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
-namespace StardewModdingAPI.Web.Framework
+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 RewriteSubdomainRule : IRule
+ internal class ConditionalRewriteSubdomainRule : IRule
{
/*********
** Accessors
*********/
- /// <summary>The paths (excluding the hostname portion) to not rewrite.</summary>
- public Regex[] ExceptPaths { get; set; }
+ /// <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;
+ }
+
/// <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;
HttpRequest request = context.HttpContext.Request;
- // check ignores
- if (this.ExceptPaths?.Any(pattern => pattern.IsMatch(request.Path)) == true)
+ // check condition
+ if (this.ShouldRewrite != null && !this.ShouldRewrite(request))
return;
// get host parts
string host = 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
request.Path = $"/{parts[0]}{request.Path}";
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index e19593c7..0f656e55 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -1,4 +1,3 @@
-using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Rewrite;
@@ -9,6 +8,7 @@ using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using StardewModdingAPI.Web.Framework;
using StardewModdingAPI.Web.Framework.ConfigModels;
+using StardewModdingAPI.Web.Framework.RewriteRules;
namespace StardewModdingAPI.Web
{
@@ -65,13 +65,15 @@ namespace StardewModdingAPI.Web
loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app
- .UseRewriter(
- new RewriteOptions()
- .Add(new RewriteSubdomainRule
- {
- ExceptPaths = new[] { new Regex("^/Content", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase) }
- })
- ) // convert subdomain.smapi.io => smapi.io/subdomain for routing
+ .UseRewriter(new RewriteOptions()
+ // convert subdomain.smapi.io => smapi.io/subdomain for routing
+ .Add(new ConditionalRewriteSubdomainRule(
+ shouldRewrite: req =>
+ req.Host.Host != "localhost"
+ && (req.Host.Host.StartsWith("api.") || req.Host.Host.StartsWith("log."))
+ && !req.Path.StartsWithSegments("/content")
+ ))
+ )
.UseStaticFiles() // wwwroot folder
.UseMvc();
}