HttpListenerContext.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Net;
  3. using System.Security.Principal;
  4. using MediaBrowser.Model.Cryptography;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Text;
  8. using SocketHttpListener.Net.WebSockets;
  9. using SocketHttpListener.Primitives;
  10. using System.Threading.Tasks;
  11. namespace SocketHttpListener.Net
  12. {
  13. public sealed unsafe partial class HttpListenerContext
  14. {
  15. internal HttpListener _listener;
  16. private HttpListenerResponse _response;
  17. private IPrincipal _user;
  18. public HttpListenerRequest Request { get; }
  19. public IPrincipal User => _user;
  20. // This can be used to cache the results of HttpListener.AuthenticationSchemeSelectorDelegate.
  21. internal AuthenticationSchemes AuthenticationSchemes { get; set; }
  22. public HttpListenerResponse Response
  23. {
  24. get
  25. {
  26. return _response;
  27. }
  28. }
  29. public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol)
  30. {
  31. return AcceptWebSocketAsync(subProtocol, HttpWebSocket.DefaultReceiveBufferSize, WebSocket.DefaultKeepAliveInterval);
  32. }
  33. public Task<HttpListenerWebSocketContext> AcceptWebSocketAsync(string subProtocol, TimeSpan keepAliveInterval)
  34. {
  35. return AcceptWebSocketAsync(subProtocol, HttpWebSocket.DefaultReceiveBufferSize, keepAliveInterval);
  36. }
  37. }
  38. public class GenericPrincipal : IPrincipal
  39. {
  40. private IIdentity m_identity;
  41. private string[] m_roles;
  42. public GenericPrincipal(IIdentity identity, string[] roles)
  43. {
  44. if (identity == null)
  45. throw new ArgumentNullException("identity");
  46. m_identity = identity;
  47. if (roles != null)
  48. {
  49. m_roles = new string[roles.Length];
  50. for (int i = 0; i < roles.Length; ++i)
  51. {
  52. m_roles[i] = roles[i];
  53. }
  54. }
  55. else
  56. {
  57. m_roles = null;
  58. }
  59. }
  60. public virtual IIdentity Identity
  61. {
  62. get
  63. {
  64. return m_identity;
  65. }
  66. }
  67. public virtual bool IsInRole(string role)
  68. {
  69. if (role == null || m_roles == null)
  70. return false;
  71. for (int i = 0; i < m_roles.Length; ++i)
  72. {
  73. if (m_roles[i] != null && String.Compare(m_roles[i], role, StringComparison.OrdinalIgnoreCase) == 0)
  74. return true;
  75. }
  76. return false;
  77. }
  78. }
  79. }