From 647f435f6e393c3fad393d3fe94b0d6412756215 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Wed, 14 Sep 2022 15:13:34 +0200 Subject: [PATCH 1/3] Redirect natively if no baseurl is set --- .../BaseUrlRedirectionMiddleware.cs | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index e0c112d60d..61b9c6f06e 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -45,36 +45,40 @@ namespace Jellyfin.Server.Middleware var localPath = httpContext.Request.Path.ToString(); var baseUrlPrefix = serverConfigurationManager.GetNetworkConfiguration().BaseUrl; - if (!string.IsNullOrEmpty(baseUrlPrefix)) + if (string.IsNullOrEmpty(localPath) + || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, "/web", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, "/web/", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, baseUrlPrefix + "/web", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, baseUrlPrefix + "/web/", StringComparison.OrdinalIgnoreCase) + || !localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + ) { - var startsWithBaseUrl = localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase); - - if (!startsWithBaseUrl - && (localPath.Equals("/health", StringComparison.OrdinalIgnoreCase) - || localPath.Equals("/health/", StringComparison.OrdinalIgnoreCase))) + // Redirect health endpoint + if (string.Equals(localPath, "/health", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, "/health/", StringComparison.OrdinalIgnoreCase)) { _logger.LogDebug("Redirecting /health check"); httpContext.Response.Redirect(baseUrlPrefix + "/health"); return; } - if (!startsWithBaseUrl - || localPath.Length == baseUrlPrefix.Length - // Local path is /baseUrl/ - || (localPath.Length == baseUrlPrefix.Length + 1 && localPath[^1] == '/')) + // Always redirect back to the default path if the base prefix is invalid or missing + _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); + + if (string.IsNullOrWhiteSpace(baseUrlPrefix) || baseUrlPrefix[^1] != '/') { - // Always redirect back to the default path if the base prefix is invalid, missing, or is the full path. - _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); - httpContext.Response.Redirect(baseUrlPrefix + "/" + _configuration[DefaultRedirectKey]); - return; + baseUrlPrefix += "/"; } - } - else if (string.IsNullOrEmpty(localPath) - || localPath.Equals("/", StringComparison.Ordinal)) - { - // Always redirect back to the default path if root is requested. - _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); - httpContext.Response.Redirect("/" + _configuration[DefaultRedirectKey]); + + var uri = new Uri(localPath); + var redirectUri = new Uri(baseUrlPrefix + _configuration[DefaultRedirectKey]); + var target = uri.MakeRelativeUri(redirectUri).ToString(); + _logger.LogDebug("Redirecting to {Target}", target); + + httpContext.Response.Redirect(target); return; } From 7239141181bb4498c3bbb4cb03dcac75729af9ff Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Fri, 7 Oct 2022 08:43:17 +0200 Subject: [PATCH 2/3] Apply review suggestions --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index 61b9c6f06e..4fbbeadf65 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -68,13 +68,8 @@ namespace Jellyfin.Server.Middleware // Always redirect back to the default path if the base prefix is invalid or missing _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); - if (string.IsNullOrWhiteSpace(baseUrlPrefix) || baseUrlPrefix[^1] != '/') - { - baseUrlPrefix += "/"; - } - var uri = new Uri(localPath); - var redirectUri = new Uri(baseUrlPrefix + _configuration[DefaultRedirectKey]); + var redirectUri = new Uri(baseUrlPrefix + "/" + _configuration[DefaultRedirectKey]); var target = uri.MakeRelativeUri(redirectUri).ToString(); _logger.LogDebug("Redirecting to {Target}", target); From 79b19e23259d5f41800aedfc697a4f0c4ac774ff Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Fri, 7 Oct 2022 09:21:51 +0200 Subject: [PATCH 3/3] Apply review suggestions --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index 4fbbeadf65..9875df3108 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -46,9 +46,6 @@ namespace Jellyfin.Server.Middleware var baseUrlPrefix = serverConfigurationManager.GetNetworkConfiguration().BaseUrl; if (string.IsNullOrEmpty(localPath) - || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, "/web", StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, "/web/", StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, baseUrlPrefix + "/web", StringComparison.OrdinalIgnoreCase)