diff --git a/src/NzbDrone.Api/Extensions/Pipelines/UrlBasePipeline.cs b/src/NzbDrone.Api/Extensions/Pipelines/UrlBasePipeline.cs new file mode 100644 index 000000000..d8c765e67 --- /dev/null +++ b/src/NzbDrone.Api/Extensions/Pipelines/UrlBasePipeline.cs @@ -0,0 +1,46 @@ +using System; +using Nancy; +using Nancy.Bootstrapper; +using Nancy.Responses; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; + +namespace NzbDrone.Api.Extensions.Pipelines +{ + public class UrlBasePipeline : IRegisterNancyPipeline + { + private readonly string _urlBase; + + public UrlBasePipeline(IConfigFileProvider configFileProvider) + { + _urlBase = configFileProvider.UrlBase; + } + + public int Order => 99; + + public void Register(IPipelines pipelines) + { + if (_urlBase.IsNotNullOrWhiteSpace()) + { + pipelines.BeforeRequest.AddItemToStartOfPipeline((Func) Handle); + } + } + + private Response Handle(NancyContext context) + { + var basePath = context.Request.Url.BasePath; + + if (basePath.IsNullOrWhiteSpace()) + { + return new RedirectResponse($"{_urlBase}{context.Request.Path}{context.Request.Url.Query}"); + } + + if (_urlBase != basePath) + { + return new NotFoundResponse(); + } + + return null; + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Api/Frontend/StaticResourceModule.cs b/src/NzbDrone.Api/Frontend/StaticResourceModule.cs index 7ec5fe9d8..f58667c6c 100644 --- a/src/NzbDrone.Api/Frontend/StaticResourceModule.cs +++ b/src/NzbDrone.Api/Frontend/StaticResourceModule.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using Nancy.Responses; @@ -38,20 +38,6 @@ namespace NzbDrone.Api.Frontend return new NotFoundResponse(); } - //Redirect to the subfolder if the request went to the base URL - if (path.Equals("/")) - { - var urlBase = _configFileProvider.UrlBase; - - if (!string.IsNullOrEmpty(urlBase)) - { - if (Request.Url.BasePath != urlBase) - { - return new RedirectResponse(urlBase + "/"); - } - } - } - var mapper = _requestMappers.SingleOrDefault(m => m.CanHandle(path)); if (mapper != null) diff --git a/src/NzbDrone.Api/NzbDrone.Api.csproj b/src/NzbDrone.Api/NzbDrone.Api.csproj index cce77e637..bae3942e2 100644 --- a/src/NzbDrone.Api/NzbDrone.Api.csproj +++ b/src/NzbDrone.Api/NzbDrone.Api.csproj @@ -106,6 +106,7 @@ +