aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Program.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-05-07 15:44:28 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-05-07 15:44:28 -0500
commit35091805ce75a0d1a17d9a0c1d5faeea6be925fe (patch)
tree0fa2b9a98bebca4aef355c4ac121f5a24b6c4614 /src/slskd/Program.cs
parent581fa92428ec49f63421119a7bfcfef4e46df80a (diff)
parentb8f0e6f28ab3f189a47eb373b1efbb45abb9ccc8 (diff)
downloadslskd-35091805ce75a0d1a17d9a0c1d5faeea6be925fe.tar.gz
Merge remote-tracking branch 'origin/master' into search-list
Diffstat (limited to 'src/slskd/Program.cs')
-rw-r--r--src/slskd/Program.cs91
1 files changed, 38 insertions, 53 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index e21bf52e..94cd0eba 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -603,57 +603,34 @@ namespace slskd
var urlBase = OptionsAtStartup.Web.UrlBase;
urlBase = urlBase.StartsWith("/") ? urlBase : "/" + urlBase;
+ // 404 if a custom urlBase is set, but the request doesn't include it.
+ // this is necessary to make sure the development-time and run-time behavior
+ // are consistent.
+ app.EnforceUrlBase(urlBase);
+
+ // use urlBase. this effectively just removes urlBase from the path, which is
+ // why the enforcement behavior exists above (the actually requested path becomes ambiguous)
+ // inject urlBase into any html files we serve, and rewrite links to ./static or /static to
+ // prepend the url base.
app.UsePathBase(urlBase);
- app.Use((context, next) =>
- {
- context.Response.OnStarting(() =>
- {
- // the UI needs to be aware of what the URL base is,
- // so that react-router can use explicit paths.
- context.Response.Headers.Add("X-URL-Base", urlBase);
- return Task.CompletedTask;
- });
-
- return next();
- });
-
+ app.UseHTMLRewrite("((\\.)?\\/static)", $"{(urlBase == "/" ? string.Empty : urlBase)}/static");
+ app.UseHTMLInjection($"<script>window.urlBase=\"{urlBase}\"</script>");
Log.Information("Using base url {UrlBase}", urlBase);
- // remove any errant double forward slashes which may have been introduced by manipulating the path base
- app.Use(async (context, next) =>
- {
- var path = context.Request.Path.ToString();
-
- if (path.StartsWith("//"))
- {
- context.Request.Path = new string(path.Skip(1).ToArray());
- }
-
- await next();
- });
-
// serve static content from the configured path
FileServerOptions fileServerOptions = default;
-
var contentPath = Path.GetFullPath(OptionsAtStartup.Web.ContentPath);
- if (!System.IO.Directory.Exists(contentPath))
- {
- Log.Warning($"Static content disabled; cannot find content path '{contentPath}'");
- }
- else
+ fileServerOptions = new FileServerOptions
{
- fileServerOptions = new FileServerOptions
- {
- FileProvider = new PhysicalFileProvider(contentPath),
- RequestPath = string.Empty,
- EnableDirectoryBrowsing = false,
- EnableDefaultFiles = true,
- };
-
- app.UseFileServer(fileServerOptions);
- Log.Information("Serving static content from {ContentPath}", contentPath);
- }
+ FileProvider = new PhysicalFileProvider(contentPath),
+ RequestPath = string.Empty,
+ EnableDirectoryBrowsing = false,
+ EnableDefaultFiles = true,
+ };
+
+ app.UseFileServer(fileServerOptions);
+ Log.Information("Serving static content from {ContentPath}", contentPath);
if (OptionsAtStartup.Feature.Prometheus)
{
@@ -685,6 +662,18 @@ namespace slskd
}
});
+ // if this is an /api route and no API controller was matched, give up and return a 404.
+ app.Use(async (context, next) =>
+ {
+ if (context.Request.Path.StartsWithSegments("/api"))
+ {
+ context.Response.StatusCode = 404;
+ return;
+ }
+
+ await next();
+ });
+
if (OptionsAtStartup.Feature.Swagger)
{
app.UseSwagger();
@@ -694,12 +683,12 @@ namespace slskd
Log.Information("Publishing Swagger documentation to /swagger");
}
- // if we made it this far and the route still wasn't matched, return the index unless it's an api route. this is
- // required so that SPA routing (React Router, etc) can work properly
+ // if we made it this far, the caller is either looking for a route that was synthesized with a SPA router, or is genuinely confused.
+ // if the request is for a directory, modify the request to redirect it to the index, otherwise leave it alone and let it 404 in the next
+ // middleware
app.Use(async (context, next) =>
{
- // exclude API routes which are not matched or return a 404
- if (!context.Request.Path.StartsWithSegments("/api"))
+ if (Path.GetExtension(context.Request.Path.ToString()) == string.Empty)
{
context.Request.Path = "/";
}
@@ -707,12 +696,8 @@ namespace slskd
await next();
});
- // finally, hit the fileserver again. if the path was modified to return the index above, the index document will be
- // returned. otherwise it will throw a final 404 back to the client.
- if (System.IO.Directory.Exists(contentPath))
- {
- app.UseFileServer(fileServerOptions);
- }
+ // either serve the index, or 404
+ app.UseFileServer(fileServerOptions);
return app;
}