From d65e8d70442ecc2c03372abf5f81c65c6ab189a6 Mon Sep 17 00:00:00 2001 From: crobibero Date: Sun, 6 Dec 2020 19:40:43 -0700 Subject: Redirect robots.txt if hosting web content --- .../Middleware/RobotsRedirectionMiddleware.cs | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Jellyfin.Server/Middleware/RobotsRedirectionMiddleware.cs (limited to 'Jellyfin.Server/Middleware/RobotsRedirectionMiddleware.cs') diff --git a/Jellyfin.Server/Middleware/RobotsRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/RobotsRedirectionMiddleware.cs new file mode 100644 index 000000000..9d40d74fe --- /dev/null +++ b/Jellyfin.Server/Middleware/RobotsRedirectionMiddleware.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Server.Middleware +{ + /// + /// Redirect requests to robots.txt to web/robots.txt. + /// + public class RobotsRedirectionMiddleware + { + private readonly RequestDelegate _next; + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// The next delegate in the pipeline. + /// The logger. + public RobotsRedirectionMiddleware( + RequestDelegate next, + ILogger logger) + { + _next = next; + _logger = logger; + } + + /// + /// Executes the middleware action. + /// + /// The current HTTP context. + /// The async task. + public async Task Invoke(HttpContext httpContext) + { + var localPath = httpContext.Request.Path.ToString(); + if (string.Equals(localPath, "/robots.txt", StringComparison.OrdinalIgnoreCase)) + { + _logger.LogDebug("Redirecting robots.txt request to web/robots.txt"); + httpContext.Response.Redirect("web/robots.txt"); + return; + } + + await _next(httpContext).ConfigureAwait(false); + } + } +} \ No newline at end of file -- cgit v1.2.3