From b11718a01d7341f37840893398f158e6cbcd5f9b Mon Sep 17 00:00:00 2001 From: crobibero Date: Tue, 13 Apr 2021 07:12:49 -0600 Subject: Properly redirect healthcheck endpoint if using BaseUrl --- .../Middleware/BaseUrlRedirectionMiddleware.cs | 29 +++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index c23da2fd6..4dda900b0 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -45,16 +45,27 @@ namespace Jellyfin.Server.Middleware var localPath = httpContext.Request.Path.ToString(); var baseUrlPrefix = serverConfigurationManager.GetNetworkConfiguration().BaseUrl; - if (string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) - || string.IsNullOrEmpty(localPath) - || !localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase)) + if (!string.IsNullOrEmpty(baseUrlPrefix)) { - // Always redirect back to the default path if the base prefix is invalid or missing - _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); - httpContext.Response.Redirect(baseUrlPrefix + "/" + _configuration[ConfigurationExtensions.DefaultRedirectKey]); - return; + if (!localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + && localPath.EndsWith("/health", StringComparison.OrdinalIgnoreCase)) + { + _logger.LogDebug("Redirecting /health check"); + httpContext.Response.Redirect(baseUrlPrefix + "/health"); + return; + } + + if (string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) + || string.IsNullOrEmpty(localPath) + || !localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase)) + { + // Always redirect back to the default path if the base prefix is invalid or missing + _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); + httpContext.Response.Redirect(baseUrlPrefix + "/" + _configuration[ConfigurationExtensions.DefaultRedirectKey]); + return; + } } await _next(httpContext).ConfigureAwait(false); -- cgit v1.2.3 From 78791a932f4a83834c1fb58e28a94cce6c8f349c Mon Sep 17 00:00:00 2001 From: crobibero Date: Wed, 14 Apr 2021 06:44:11 -0600 Subject: Simplify baseUrl check --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index 4dda900b0..3abf579e2 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -47,7 +47,9 @@ namespace Jellyfin.Server.Middleware if (!string.IsNullOrEmpty(baseUrlPrefix)) { - if (!localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + var startsWithBaseUrl = localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase); + + if (!startsWithBaseUrl && localPath.EndsWith("/health", StringComparison.OrdinalIgnoreCase)) { _logger.LogDebug("Redirecting /health check"); @@ -59,7 +61,7 @@ namespace Jellyfin.Server.Middleware || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty(localPath) - || !localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase)) + || !startsWithBaseUrl) { // Always redirect back to the default path if the base prefix is invalid or missing _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); -- cgit v1.2.3 From 9595aa258eaeab7cc5b73452bfe6a209512e77ce Mon Sep 17 00:00:00 2001 From: crobibero Date: Thu, 27 May 2021 06:31:00 -0600 Subject: Simplify BaseUrlRedirectionMiddleware --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index 3abf579e2..f09315de8 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -50,18 +50,15 @@ namespace Jellyfin.Server.Middleware var startsWithBaseUrl = localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase); if (!startsWithBaseUrl - && localPath.EndsWith("/health", StringComparison.OrdinalIgnoreCase)) + && (localPath.Equals("/health", StringComparison.OrdinalIgnoreCase) + || localPath.Equals("/health/", StringComparison.OrdinalIgnoreCase))) { _logger.LogDebug("Redirecting /health check"); httpContext.Response.Redirect(baseUrlPrefix + "/health"); return; } - if (string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase) - || string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase) - || string.IsNullOrEmpty(localPath) - || !startsWithBaseUrl) + if (!startsWithBaseUrl) { // Always redirect back to the default path if the base prefix is invalid or missing _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath); -- cgit v1.2.3 From dfc776e451bcd504d35d4619ff74be2fde0492e4 Mon Sep 17 00:00:00 2001 From: crobibero Date: Mon, 7 Jun 2021 07:47:49 -0600 Subject: Redirect to default if root is requested --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index f09315de8..2eef223e5 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -66,6 +66,14 @@ namespace Jellyfin.Server.Middleware return; } } + 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[ConfigurationExtensions.DefaultRedirectKey]); + return; + } await _next(httpContext).ConfigureAwait(false); } -- cgit v1.2.3 From a7585dd2d65895620ab82973c9cd72991b5ed38b Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sat, 7 Aug 2021 07:36:45 -0600 Subject: Fix redirect logic if request path is exactly the base url --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index 2eef223e5..e3f3911f9 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -58,9 +58,12 @@ namespace Jellyfin.Server.Middleware return; } - if (!startsWithBaseUrl) + if (!startsWithBaseUrl + || localPath.Equals(baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + // 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 + // 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[ConfigurationExtensions.DefaultRedirectKey]); return; -- cgit v1.2.3 From bdbac12d4f35bf47e75ed25c7bb10c0074e4f4ce Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sat, 7 Aug 2021 09:06:31 -0600 Subject: Update Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs Co-authored-by: Claus Vium --- Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs index e3f3911f9..3e5982eed 100644 --- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs +++ b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs @@ -59,7 +59,7 @@ namespace Jellyfin.Server.Middleware } if (!startsWithBaseUrl - || localPath.Equals(baseUrlPrefix, StringComparison.OrdinalIgnoreCase) + || localPath.Length == baseUrlPrefix.Length // Local path is /baseUrl/ || (localPath.Length == baseUrlPrefix.Length + 1 && localPath[^1] == '/')) { -- cgit v1.2.3