LegacyEmbyRouteRewriteMiddleware.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Logging;
  5. namespace Jellyfin.Api.Middleware;
  6. /// <summary>
  7. /// Removes /emby and /mediabrowser from requested route.
  8. /// </summary>
  9. public class LegacyEmbyRouteRewriteMiddleware
  10. {
  11. private const string EmbyPath = "/emby";
  12. private const string MediabrowserPath = "/mediabrowser";
  13. private readonly RequestDelegate _next;
  14. private readonly ILogger<LegacyEmbyRouteRewriteMiddleware> _logger;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="LegacyEmbyRouteRewriteMiddleware"/> class.
  17. /// </summary>
  18. /// <param name="next">The next delegate in the pipeline.</param>
  19. /// <param name="logger">The logger.</param>
  20. public LegacyEmbyRouteRewriteMiddleware(
  21. RequestDelegate next,
  22. ILogger<LegacyEmbyRouteRewriteMiddleware> logger)
  23. {
  24. _next = next;
  25. _logger = logger;
  26. }
  27. /// <summary>
  28. /// Executes the middleware action.
  29. /// </summary>
  30. /// <param name="httpContext">The current HTTP context.</param>
  31. /// <returns>The async task.</returns>
  32. public async Task Invoke(HttpContext httpContext)
  33. {
  34. var localPath = httpContext.Request.Path.ToString();
  35. if (localPath.StartsWith(EmbyPath, StringComparison.OrdinalIgnoreCase))
  36. {
  37. httpContext.Request.Path = localPath[EmbyPath.Length..];
  38. _logger.LogDebug("Removing {EmbyPath} from route.", EmbyPath);
  39. }
  40. else if (localPath.StartsWith(MediabrowserPath, StringComparison.OrdinalIgnoreCase))
  41. {
  42. httpContext.Request.Path = localPath[MediabrowserPath.Length..];
  43. _logger.LogDebug("Removing {MediabrowserPath} from route.", MediabrowserPath);
  44. }
  45. await _next(httpContext).ConfigureAwait(false);
  46. }
  47. }