HttpListener.cs 8.1 KB

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