2
0

HttpListener.cs 8.5 KB

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