using System; using System.Threading.Tasks; using MediaBrowser.Controller.Configuration; using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.Http; namespace Jellyfin.Server.Middleware { /// /// Cors policy provider. /// public class CorsPolicyProvider : ICorsPolicyProvider { private readonly IServerConfigurationManager _serverConfigurationManager; /// /// Initializes a new instance of the class. /// /// Instance of the interface. public CorsPolicyProvider(IServerConfigurationManager serverConfigurationManager) { _serverConfigurationManager = serverConfigurationManager; } /// public Task GetPolicyAsync(HttpContext context, string policyName) { var corsHosts = _serverConfigurationManager.Configuration.CorsHosts; var builder = new CorsPolicyBuilder() .AllowAnyMethod() .AllowAnyHeader(); // No hosts configured or only default configured. if (corsHosts.Length == 0 || (corsHosts.Length == 1 && string.Equals(corsHosts[0], CorsConstants.AnyOrigin, StringComparison.Ordinal))) { builder.AllowAnyOrigin(); } else { builder.WithOrigins(corsHosts) .AllowCredentials(); } return Task.FromResult(builder.Build()); } } }