RobotsRedirectionMiddleware.cs 1.6 KB

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