HttpListenerContext.cs 2.5 KB

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