HttpListener.cs 8.6 KB

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