aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs
diff options
context:
space:
mode:
authorShadowghost <Ghost_of_Stone@web.de>2023-01-19 10:09:32 +0100
committerShadowghost <Ghost_of_Stone@web.de>2023-01-19 10:09:32 +0100
commit656a0bff6fd48ba66cfe8fc7b470380c38afbac2 (patch)
treea89f0545050bf6672936c6e7ea3e2b0e5c5561f2 /Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs
parentef085483b2ef54195e16f282330a3c204e3227b6 (diff)
parentd57dcf22452db4990aa2cdece3eb798ba98b8330 (diff)
Merge remote-tracking branch 'upstream/master' into network-rewrite
Diffstat (limited to 'Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs')
-rw-r--r--Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs81
1 files changed, 0 insertions, 81 deletions
diff --git a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs b/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs
deleted file mode 100644
index 6ee5bf38a..000000000
--- a/Jellyfin.Server/Middleware/BaseUrlRedirectionMiddleware.cs
+++ /dev/null
@@ -1,81 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using Jellyfin.Networking.Configuration;
-using MediaBrowser.Controller.Configuration;
-using Microsoft.AspNetCore.Http;
-using Microsoft.Extensions.Configuration;
-using Microsoft.Extensions.Logging;
-using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
-
-namespace Jellyfin.Server.Middleware
-{
- /// <summary>
- /// Redirect requests without baseurl prefix to the baseurl prefixed URL.
- /// </summary>
- public class BaseUrlRedirectionMiddleware
- {
- private readonly RequestDelegate _next;
- private readonly ILogger<BaseUrlRedirectionMiddleware> _logger;
- private readonly IConfiguration _configuration;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="BaseUrlRedirectionMiddleware"/> class.
- /// </summary>
- /// <param name="next">The next delegate in the pipeline.</param>
- /// <param name="logger">The logger.</param>
- /// <param name="configuration">The application configuration.</param>
- public BaseUrlRedirectionMiddleware(
- RequestDelegate next,
- ILogger<BaseUrlRedirectionMiddleware> logger,
- IConfiguration configuration)
- {
- _next = next;
- _logger = logger;
- _configuration = configuration;
- }
-
- /// <summary>
- /// Executes the middleware action.
- /// </summary>
- /// <param name="httpContext">The current HTTP context.</param>
- /// <param name="serverConfigurationManager">The server configuration manager.</param>
- /// <returns>The async task.</returns>
- public async Task Invoke(HttpContext httpContext, IServerConfigurationManager serverConfigurationManager)
- {
- var localPath = httpContext.Request.Path.ToString();
- var baseUrlPrefix = serverConfigurationManager.GetNetworkConfiguration().BaseUrl;
-
- if (string.IsNullOrEmpty(localPath)
- || string.Equals(localPath, baseUrlPrefix, StringComparison.OrdinalIgnoreCase)
- || string.Equals(localPath, baseUrlPrefix + "/", StringComparison.OrdinalIgnoreCase)
- || string.Equals(localPath, baseUrlPrefix + "/web", StringComparison.OrdinalIgnoreCase)
- || string.Equals(localPath, baseUrlPrefix + "/web/", StringComparison.OrdinalIgnoreCase)
- || !localPath.StartsWith(baseUrlPrefix, StringComparison.OrdinalIgnoreCase)
- )
- {
- // Redirect health endpoint
- if (string.Equals(localPath, "/health", StringComparison.OrdinalIgnoreCase)
- || string.Equals(localPath, "/health/", StringComparison.OrdinalIgnoreCase))
- {
- _logger.LogDebug("Redirecting /health check");
- httpContext.Response.Redirect(baseUrlPrefix + "/health");
- return;
- }
-
- // Always redirect back to the default path if the base prefix is invalid or missing
- _logger.LogDebug("Normalizing an URL at {LocalPath}", localPath);
-
- var port = httpContext.Request.Host.Port ?? -1;
- var uri = new UriBuilder(httpContext.Request.Scheme, httpContext.Request.Host.Host, port, localPath).Uri;
- var redirectUri = new UriBuilder(httpContext.Request.Scheme, httpContext.Request.Host.Host, port, baseUrlPrefix + "/" + _configuration[DefaultRedirectKey]).Uri;
- var target = uri.MakeRelativeUri(redirectUri).ToString();
- _logger.LogDebug("Redirecting to {Target}", target);
-
- httpContext.Response.Redirect(target);
- return;
- }
-
- await _next(httpContext).ConfigureAwait(false);
- }
- }
-}