jellyfin-server/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs

85 lines
3.6 KiB
C#
Raw Normal View History

2020-09-03 09:32:22 +00:00
using System;
using System.Threading.Tasks;
using Jellyfin.Networking.Configuration;
2020-09-03 09:32:22 +00:00
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
2022-02-14 13:39:33 +00:00
using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
2020-09-03 09:32:22 +00:00
namespace Jellyfin.Server.Middleware
{
/// <summary>
/// Redirect requests without baseurl prefix to the baseurl prefixed URL.
/// </summary>
public class BaseUrlRedirectionMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<BaseUrlRedirectionMiddleware> _logger;
private readonly IConfiguration _configuration;
/// <summary>
/// Initializes a new instance of the <see cref="BaseUrlRedirectionMiddleware"/> class.
/// </summary>
/// <param name="next">The next delegate in the pipeline.</param>
/// <param name="logger">The logger.</param>
/// <param name="configuration">The application configuration.</param>
public BaseUrlRedirectionMiddleware(
RequestDelegate next,
ILogger<BaseUrlRedirectionMiddleware> logger,
IConfiguration configuration)
{
_next = next;
_logger = logger;
_configuration = configuration;
}
/// <summary>
/// Executes the middleware action.
/// </summary>
/// <param name="httpContext">The current HTTP context.</param>
/// <param name="serverConfigurationManager">The server configuration manager.</param>
/// <returns>The async task.</returns>
public async Task Invoke(HttpContext httpContext, IServerConfigurationManager serverConfigurationManager)
{
var localPath = httpContext.Request.Path.ToString();
var baseUrlPrefix = serverConfigurationManager.GetNetworkConfiguration().BaseUrl;
2020-09-03 09:32:22 +00:00
if (!string.IsNullOrEmpty(baseUrlPrefix))
2020-09-03 09:32:22 +00:00
{
2021-04-14 12:44:11 +00:00
var startsWithBaseUrl = localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase);
if (!startsWithBaseUrl
2021-05-27 12:31:00 +00:00
&& (localPath.Equals("/health", StringComparison.OrdinalIgnoreCase)
|| localPath.Equals("/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.
_logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);
2022-02-14 13:39:33 +00:00
httpContext.Response.Redirect(baseUrlPrefix + "/" + _configuration[DefaultRedirectKey]);
return;
}
2020-09-03 09:32:22 +00:00
}
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);
2022-02-14 13:39:33 +00:00
httpContext.Response.Redirect("/" + _configuration[DefaultRedirectKey]);
return;
}
2020-09-03 09:32:22 +00:00
await _next(httpContext).ConfigureAwait(false);
}
}
}