HttpListener.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Security.Cryptography.X509Certificates;
  6. using MediaBrowser.Common.Net;
  7. using MediaBrowser.Model.Cryptography;
  8. using MediaBrowser.Model.IO;
  9. using MediaBrowser.Model.Net;
  10. using MediaBrowser.Model.System;
  11. using Microsoft.Extensions.Logging;
  12. namespace SocketHttpListener.Net
  13. {
  14. public sealed class HttpListener : IDisposable
  15. {
  16. internal ICryptoProvider CryptoProvider { get; private set; }
  17. internal ISocketFactory SocketFactory { get; private set; }
  18. internal IFileSystem FileSystem { get; private set; }
  19. internal IStreamHelper StreamHelper { get; private set; }
  20. internal INetworkManager NetworkManager { get; private set; }
  21. internal IEnvironmentInfo EnvironmentInfo { get; private set; }
  22. public bool EnableDualMode { get; set; }
  23. AuthenticationSchemes auth_schemes;
  24. HttpListenerPrefixCollection prefixes;
  25. AuthenticationSchemeSelector auth_selector;
  26. string realm;
  27. bool unsafe_ntlm_auth;
  28. bool listening;
  29. bool disposed;
  30. Dictionary<HttpListenerContext, HttpListenerContext> registry; // Dictionary<HttpListenerContext,HttpListenerContext>
  31. Dictionary<HttpConnection, HttpConnection> connections;
  32. private ILogger _logger;
  33. private X509Certificate _certificate;
  34. public Action<HttpListenerContext> OnContext { get; set; }
  35. public HttpListener(ILogger logger, ICryptoProvider cryptoProvider, ISocketFactory socketFactory,
  36. INetworkManager networkManager, IStreamHelper streamHelper, IFileSystem fileSystem,
  37. IEnvironmentInfo environmentInfo)
  38. {
  39. _logger = logger;
  40. CryptoProvider = cryptoProvider;
  41. SocketFactory = socketFactory;
  42. NetworkManager = networkManager;
  43. StreamHelper = streamHelper;
  44. FileSystem = fileSystem;
  45. EnvironmentInfo = environmentInfo;
  46. prefixes = new HttpListenerPrefixCollection(logger, this);
  47. registry = new Dictionary<HttpListenerContext, HttpListenerContext>();
  48. connections = new Dictionary<HttpConnection, HttpConnection>();
  49. auth_schemes = AuthenticationSchemes.Anonymous;
  50. }
  51. public HttpListener(ILogger logger, X509Certificate certificate, ICryptoProvider cryptoProvider,
  52. ISocketFactory socketFactory, INetworkManager networkManager, IStreamHelper streamHelper,
  53. IFileSystem fileSystem, IEnvironmentInfo environmentInfo)
  54. : this(logger, cryptoProvider, socketFactory, networkManager, streamHelper, fileSystem, environmentInfo)
  55. {
  56. _certificate = certificate;
  57. }
  58. public void LoadCert(X509Certificate cert)
  59. {
  60. _certificate = cert;
  61. }
  62. // TODO: Digest, NTLM and Negotiate require ControlPrincipal
  63. public AuthenticationSchemes AuthenticationSchemes
  64. {
  65. get => auth_schemes;
  66. set
  67. {
  68. CheckDisposed();
  69. auth_schemes = value;
  70. }
  71. }
  72. public AuthenticationSchemeSelector AuthenticationSchemeSelectorDelegate
  73. {
  74. get => auth_selector;
  75. set
  76. {
  77. CheckDisposed();
  78. auth_selector = value;
  79. }
  80. }
  81. public bool IsListening => listening;
  82. public static bool IsSupported => true;
  83. public HttpListenerPrefixCollection Prefixes
  84. {
  85. get
  86. {
  87. CheckDisposed();
  88. return prefixes;
  89. }
  90. }
  91. // TODO: use this
  92. public string Realm
  93. {
  94. get => realm;
  95. set
  96. {
  97. CheckDisposed();
  98. realm = value;
  99. }
  100. }
  101. public bool UnsafeConnectionNtlmAuthentication
  102. {
  103. get => unsafe_ntlm_auth;
  104. set
  105. {
  106. CheckDisposed();
  107. unsafe_ntlm_auth = value;
  108. }
  109. }
  110. //internal IMonoSslStream CreateSslStream(Stream innerStream, bool ownsStream, MSI.MonoRemoteCertificateValidationCallback callback)
  111. //{
  112. // lock (registry)
  113. // {
  114. // if (tlsProvider == null)
  115. // tlsProvider = MonoTlsProviderFactory.GetProviderInternal();
  116. // if (tlsSettings == null)
  117. // tlsSettings = MSI.MonoTlsSettings.CopyDefaultSettings();
  118. // if (tlsSettings.RemoteCertificateValidationCallback == null)
  119. // tlsSettings.RemoteCertificateValidationCallback = callback;
  120. // return tlsProvider.CreateSslStream(innerStream, ownsStream, tlsSettings);
  121. // }
  122. //}
  123. internal X509Certificate Certificate => _certificate;
  124. public void Abort()
  125. {
  126. if (disposed)
  127. return;
  128. if (!listening)
  129. {
  130. return;
  131. }
  132. Close(true);
  133. }
  134. public void Close()
  135. {
  136. if (disposed)
  137. return;
  138. if (!listening)
  139. {
  140. disposed = true;
  141. return;
  142. }
  143. Close(true);
  144. disposed = true;
  145. }
  146. void Close(bool force)
  147. {
  148. CheckDisposed();
  149. HttpEndPointManager.RemoveListener(_logger, this);
  150. Cleanup(force);
  151. }
  152. void Cleanup(bool close_existing)
  153. {
  154. lock (registry)
  155. {
  156. if (close_existing)
  157. {
  158. // Need to copy this since closing will call UnregisterContext
  159. ICollection keys = registry.Keys;
  160. var all = new HttpListenerContext[keys.Count];
  161. keys.CopyTo(all, 0);
  162. registry.Clear();
  163. for (int i = all.Length - 1; i >= 0; i--)
  164. all[i].Connection.Close(true);
  165. }
  166. lock (connections)
  167. {
  168. ICollection keys = connections.Keys;
  169. var conns = new HttpConnection[keys.Count];
  170. keys.CopyTo(conns, 0);
  171. connections.Clear();
  172. for (int i = conns.Length - 1; i >= 0; i--)
  173. conns[i].Close(true);
  174. }
  175. }
  176. }
  177. internal AuthenticationSchemes SelectAuthenticationScheme(HttpListenerContext context)
  178. {
  179. if (AuthenticationSchemeSelectorDelegate != null)
  180. return AuthenticationSchemeSelectorDelegate(context.Request);
  181. else
  182. return auth_schemes;
  183. }
  184. public void Start()
  185. {
  186. CheckDisposed();
  187. if (listening)
  188. return;
  189. HttpEndPointManager.AddListener(_logger, this);
  190. listening = true;
  191. }
  192. public void Stop()
  193. {
  194. CheckDisposed();
  195. listening = false;
  196. Close(false);
  197. }
  198. void IDisposable.Dispose()
  199. {
  200. if (disposed)
  201. return;
  202. Close(true); //TODO: Should we force here or not?
  203. disposed = true;
  204. }
  205. internal void CheckDisposed()
  206. {
  207. if (disposed)
  208. throw new ObjectDisposedException(GetType().Name);
  209. }
  210. internal void RegisterContext(HttpListenerContext context)
  211. {
  212. if (OnContext != null && IsListening)
  213. {
  214. OnContext(context);
  215. }
  216. lock (registry)
  217. registry[context] = context;
  218. }
  219. internal void UnregisterContext(HttpListenerContext context)
  220. {
  221. lock (registry)
  222. registry.Remove(context);
  223. }
  224. internal void AddConnection(HttpConnection cnc)
  225. {
  226. lock (connections)
  227. {
  228. connections[cnc] = cnc;
  229. }
  230. }
  231. internal void RemoveConnection(HttpConnection cnc)
  232. {
  233. lock (connections)
  234. {
  235. connections.Remove(cnc);
  236. }
  237. }
  238. }
  239. }