RobotsRedirectionMiddleware.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. /// Redirect requests to robots.txt to web/robots.txt.
  8. /// </summary>
  9. public class RobotsRedirectionMiddleware
  10. {
  11. private readonly RequestDelegate _next;
  12. private readonly ILogger<RobotsRedirectionMiddleware> _logger;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="RobotsRedirectionMiddleware"/> class.
  15. /// </summary>
  16. /// <param name="next">The next delegate in the pipeline.</param>
  17. /// <param name="logger">The logger.</param>
  18. public RobotsRedirectionMiddleware(
  19. RequestDelegate next,
  20. ILogger<RobotsRedirectionMiddleware> logger)
  21. {
  22. _next = next;
  23. _logger = logger;
  24. }
  25. /// <summary>
  26. /// Executes the middleware action.
  27. /// </summary>
  28. /// <param name="httpContext">The current HTTP context.</param>
  29. /// <returns>The async task.</returns>
  30. public async Task Invoke(HttpContext httpContext)
  31. {
  32. if (httpContext.Request.Path.Equals("/robots.txt", StringComparison.OrdinalIgnoreCase))
  33. {
  34. _logger.LogDebug("Redirecting robots.txt request to web/robots.txt");
  35. httpContext.Response.Redirect("web/robots.txt");
  36. return;
  37. }
  38. await _next(httpContext).ConfigureAwait(false);
  39. }
  40. }