jellyfin/SocketHttpListener/Net/HttpListenerContext.cs

93 lines
2.6 KiB
C#
Raw Normal View History

2016-11-11 20:55:12 +01:00
using System;
using System.Net;
using System.Security.Principal;
using MediaBrowser.Model.Cryptography;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Text;
using SocketHttpListener.Net.WebSockets;
using SocketHttpListener.Primitives;
2018-09-12 19:26:21 +02:00
using System.Threading.Tasks;
2016-11-11 20:55:12 +01:00
namespace SocketHttpListener.Net
{
2018-09-12 19:26:21 +02:00
public sealed unsafe partial class HttpListenerContext
2016-11-11 20:55:12 +01:00
{
2018-09-12 19:26:21 +02:00
internal HttpListener _listener;
private HttpListenerResponse _response;
private IPrincipal _user;
2016-11-11 20:55:12 +01:00
2018-09-12 19:26:21 +02:00
public HttpListenerRequest Request { get; }
2016-11-11 20:55:12 +01:00
2018-09-12 19:26:21 +02:00
public IPrincipal User => _user;
2016-11-11 20:55:12 +01:00
2018-09-12 19:26:21 +02:00
// This can be used to cache the results of HttpListener.AuthenticationSchemeSelectorDelegate.
internal AuthenticationSchemes AuthenticationSchemes { get; set; }
2016-11-11 20:55:12 +01:00
public HttpListenerResponse Response
{
2018-09-12 19:26:21 +02:00
get
2016-11-11 20:55:12 +01:00
{
2018-09-12 19:26:21 +02:00
return _response;
2016-11-11 20:55:12 +01:00
}
}
2018-09-12 19:26:21 +02:00
public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol)
2016-11-11 20:55:12 +01:00
{
2018-09-12 19:26:21 +02:00
return AcceptWebSocketAsync(subProtocol, HttpWebSocket.DefaultReceiveBufferSize, WebSocket.DefaultKeepAliveInterval);
2016-11-11 20:55:12 +01:00
}
2018-09-12 19:26:21 +02:00
public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol, TimeSpan keepAliveInterval)
2016-11-11 20:55:12 +01:00
{
2018-09-12 19:26:21 +02:00
return AcceptWebSocketAsync(subProtocol, HttpWebSocket.DefaultReceiveBufferSize, keepAliveInterval);
2016-11-11 20:55:12 +01:00
}
}
public class GenericPrincipal : IPrincipal
{
private IIdentity m_identity;
private string[] m_roles;
public GenericPrincipal(IIdentity identity, string[] roles)
{
if (identity == null)
throw new ArgumentNullException("identity");
m_identity = identity;
if (roles != null)
{
m_roles = new string[roles.Length];
for (int i = 0; i < roles.Length; ++i)
{
m_roles[i] = roles[i];
}
}
else
{
m_roles = null;
}
}
public virtual IIdentity Identity
{
get
{
return m_identity;
}
}
public virtual bool IsInRole(string role)
{
if (role == null || m_roles == null)
return false;
for (int i = 0; i < m_roles.Length; ++i)
{
if (m_roles[i] != null && String.Compare(m_roles[i], role, StringComparison.OrdinalIgnoreCase) == 0)
return true;
}
return false;
}
}
}