RobotsRedirectionMiddleware.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. var localPath = httpContext.Request.Path.ToString();
  33. if (string.Equals(localPath, "/robots.txt", StringComparison.OrdinalIgnoreCase))
  34. {
  35. _logger.LogDebug("Redirecting robots.txt request to web/robots.txt");
  36. httpContext.Response.Redirect("web/robots.txt");
  37. return;
  38. }
  39. await _next(httpContext).ConfigureAwait(false);
  40. }
  41. }