BaseUrlRedirectionMiddleware.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ConfigurationExtensions = 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. {
  57. // Always redirect back to the default path if the base prefix is invalid or missing
  58. _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);
  59. httpContext.Response.Redirect(baseUrlPrefix + "/" + _configuration[ConfigurationExtensions.DefaultRedirectKey]);
  60. return;
  61. }
  62. }
  63. await _next(httpContext).ConfigureAwait(false);
  64. }
  65. }
  66. }