LegacyEmbyRouteRewriteMiddleware.cs 2.0 KB

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