jellyfin/Jellyfin.Server/Configuration/CorsPolicyProvider.cs

50 lines
1.7 KiB
C#
Raw Normal View History

2020-09-05 08:10:05 -07:00
using System;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Http;
namespace Jellyfin.Server.Configuration
{
2020-09-05 08:10:05 -07:00
/// <summary>
/// Cors policy provider.
/// </summary>
public class CorsPolicyProvider : ICorsPolicyProvider
{
2020-09-05 08:10:05 -07:00
private readonly IServerConfigurationManager _serverConfigurationManager;
/// <summary>
/// Initializes a new instance of the <see cref="CorsPolicyProvider"/> class.
/// </summary>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
public CorsPolicyProvider(IServerConfigurationManager serverConfigurationManager)
{
_serverConfigurationManager = serverConfigurationManager;
}
/// <inheritdoc />
2021-02-14 07:11:46 -07:00
public Task<CorsPolicy?> GetPolicyAsync(HttpContext context, string? policyName)
2020-09-05 08:10:05 -07:00
{
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();
}
2021-02-14 07:11:46 -07:00
return Task.FromResult<CorsPolicy?>(builder.Build());
2020-09-05 08:10:05 -07:00
}
}
}