Redirect natively if no baseurl is set

This commit is contained in:
Shadowghost 2022-09-14 15:13:34 +02:00
parent 55b0ebbbf3
commit 647f435f6e

View File

@ -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, missing, or is the full path.
// 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[DefaultRedirectKey]);
return;
}
}
else if (string.IsNullOrEmpty(localPath)
|| localPath.Equals("/", StringComparison.Ordinal))
if (string.IsNullOrWhiteSpace(baseUrlPrefix) || baseUrlPrefix[^1] != '/')
{
// Always redirect back to the default path if root is requested.
_logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);
httpContext.Response.Redirect("/" + _configuration[DefaultRedirectKey]);
baseUrlPrefix += "/";
}
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;
}