diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-05-07 14:27:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-07 14:27:53 -0500 |
| commit | 6ac44e4540e5b0bf42f99563b47317add8d22aff (patch) | |
| tree | 3638bc7683b68b0304c2a7ad32c3c9316b123acf /src/slskd/Program.cs | |
| parent | 09adf4655f43ab6abeba1d1ac219df94d70c634c (diff) | |
| parent | 2391114fbcadba7378451c8a066fb466269fdb13 (diff) | |
| download | slskd-6ac44e4540e5b0bf42f99563b47317add8d22aff.tar.gz | |
Merge pull request #452 from slskd/routes
Fix UI routing issues
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 78 |
1 files changed, 38 insertions, 40 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index afdf4ab3..ecf028b4 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -602,44 +602,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.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)) + fileServerOptions = new FileServerOptions { - Log.Warning($"Static content disabled; cannot find content path '{contentPath}'"); - } - else - { - 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) { @@ -671,6 +661,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(); @@ -680,12 +682,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 = "/"; } @@ -693,12 +695,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; } |
