2
0

BaseUrlRedirectionMiddleware.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Networking.Configuration;
  4. using MediaBrowser.Controller.Configuration;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Logging;
  8. using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
  9. namespace Jellyfin.Server.Middleware
  10. {
  11. /// <summary>
  12. /// Redirect requests without baseurl prefix to the baseurl prefixed URL.
  13. /// </summary>
  14. public class BaseUrlRedirectionMiddleware
  15. {
  16. private readonly RequestDelegate _next;
  17. private readonly ILogger<BaseUrlRedirectionMiddleware> _logger;
  18. private readonly IConfiguration _configuration;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="BaseUrlRedirectionMiddleware"/> class.
  21. /// </summary>
  22. /// <param name="next">The next delegate in the pipeline.</param>
  23. /// <param name="logger">The logger.</param>
  24. /// <param name="configuration">The application configuration.</param>
  25. public BaseUrlRedirectionMiddleware(
  26. RequestDelegate next,
  27. ILogger<BaseUrlRedirectionMiddleware> logger,
  28. IConfiguration configuration)
  29. {
  30. _next = next;
  31. _logger = logger;
  32. _configuration = configuration;
  33. }
  34. /// <summary>
  35. /// Executes the middleware action.
  36. /// </summary>
  37. /// <param name="httpContext">The current HTTP context.</param>
  38. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  39. /// <returns>The async task.</returns>
  40. public async Task Invoke(HttpContext httpContext, IServerConfigurationManager serverConfigurationManager)
  41. {
  42. var localPath = httpContext.Request.Path.ToString();
  43. var baseUrlPrefix = serverConfigurationManager.GetNetworkConfiguration().BaseUrl;
  44. if (!string.IsNullOrEmpty(baseUrlPrefix))
  45. {
  46. var startsWithBaseUrl = localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase);
  47. if (!startsWithBaseUrl
  48. && (localPath.Equals("/health", StringComparison.OrdinalIgnoreCase)
  49. || localPath.Equals("/health/", StringComparison.OrdinalIgnoreCase)))
  50. {
  51. _logger.LogDebug("Redirecting /health check");
  52. httpContext.Response.Redirect(baseUrlPrefix + "/health");
  53. return;
  54. }
  55. if (!startsWithBaseUrl
  56. || localPath.Length == baseUrlPrefix.Length
  57. // Local path is /baseUrl/
  58. || (localPath.Length == baseUrlPrefix.Length + 1 && localPath[^1] == '/'))
  59. {
  60. // Always redirect back to the default path if the base prefix is invalid, missing, or is the full path.
  61. _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);
  62. httpContext.Response.Redirect(baseUrlPrefix + "/" + _configuration[DefaultRedirectKey]);
  63. return;
  64. }
  65. }
  66. else if (string.IsNullOrEmpty(localPath)
  67. || localPath.Equals("/", StringComparison.Ordinal))
  68. {
  69. // Always redirect back to the default path if root is requested.
  70. _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);
  71. httpContext.Response.Redirect("/" + _configuration[DefaultRedirectKey]);
  72. return;
  73. }
  74. await _next(httpContext).ConfigureAwait(false);
  75. }
  76. }
  77. }