From c312ffd7db460cb627c04c1efed0a5ed45ea305d Mon Sep 17 00:00:00 2001 From: Mitchell Cash Date: Tue, 13 Jun 2017 16:42:49 +1000 Subject: [PATCH] Fixed: Redirect calls missing URL Base (#1668) --- .../Extensions/Pipelines/UrlBasePipeline.cs | 46 +++++++++++++++++++ .../Frontend/StaticResourceModule.cs | 16 +------ src/NzbDrone.Api/NzbDrone.Api.csproj | 1 + 3 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 src/NzbDrone.Api/Extensions/Pipelines/UrlBasePipeline.cs 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 2e44197df..c2bf53e79 100644 --- a/src/NzbDrone.Api/NzbDrone.Api.csproj +++ b/src/NzbDrone.Api/NzbDrone.Api.csproj @@ -113,6 +113,7 @@ +