Host.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // **********************************************************************************
  2. // CassiniDev - http://cassinidev.codeplex.com
  3. //
  4. // Copyright (c) 2010 Sky Sanders. All rights reserved.
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // This source code is subject to terms and conditions of the Microsoft Public
  8. // License (Ms-PL). A copy of the license can be found in the license.txt file
  9. // included in this distribution.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // **********************************************************************************
  14. #region
  15. using System;
  16. using System.Globalization;
  17. using System.Security.Permissions;
  18. using System.Security.Principal;
  19. using System.Threading;
  20. using System.Web;
  21. using System.Web.Hosting;
  22. #endregion
  23. namespace CassiniDev
  24. {
  25. /// <summary>
  26. /// 01/01/10 sky: added HttpRuntime.Close to IRegisteredObject.Stop to eliminate
  27. /// System.AppDomainUnloadedException when running tests in NUnit GuiRunner.
  28. /// reference: http://stackoverflow.com/questions/561402/cassini-webserver-webdev-nunit-and-appdomainunloadedexception
  29. /// need to test thoroughly but seems to work just fine with no ill effects
  30. /// 01.03.10 sky: removed the HttpRuntime.Close because, even though it tests fine, I am not entirely certain it is in the right place
  31. /// and since I am no longer recommending that the server be used as a library in testing (run a console instance in a new process).
  32. /// 04.20.11 sky: un-second-guessed myself and un-removed the initial fix. seems to have resolved the issue.
  33. ///
  34. /// </summary>
  35. internal class Host : MarshalByRefObject, IRegisteredObject
  36. {
  37. private bool _disableDirectoryListing;
  38. private string _installPath;
  39. private string _lowerCasedClientScriptPathWithTrailingSlash;
  40. private string _lowerCasedVirtualPath;
  41. private string _lowerCasedVirtualPathWithTrailingSlash;
  42. private volatile int _pendingCallsCount;
  43. private string _physicalClientScriptPath;
  44. private string _physicalPath;
  45. private int _port;
  46. private bool _requireAuthentication;
  47. private Server _server;
  48. private string _virtualPath;
  49. public Host()
  50. {
  51. HostingEnvironment.RegisterObject(this);
  52. }
  53. public bool DisableDirectoryListing
  54. {
  55. get { return _disableDirectoryListing; }
  56. }
  57. public string InstallPath
  58. {
  59. get { return _installPath; }
  60. }
  61. public string NormalizedClientScriptPath
  62. {
  63. get { return _lowerCasedClientScriptPathWithTrailingSlash; }
  64. }
  65. public string NormalizedVirtualPath
  66. {
  67. get { return _lowerCasedVirtualPathWithTrailingSlash; }
  68. }
  69. public string PhysicalClientScriptPath
  70. {
  71. get { return _physicalClientScriptPath; }
  72. }
  73. public string PhysicalPath
  74. {
  75. get { return _physicalPath; }
  76. }
  77. public int Port
  78. {
  79. get { return _port; }
  80. }
  81. public bool RequireAuthentication
  82. {
  83. get { return _requireAuthentication; }
  84. }
  85. public string VirtualPath
  86. {
  87. get { return _virtualPath; }
  88. }
  89. #region IRegisteredObject Members
  90. void IRegisteredObject.Stop(bool immediate)
  91. {
  92. // Unhook the Host so Server will process the requests in the new appdomain.
  93. if (_server != null)
  94. {
  95. _server.HostStopped();
  96. }
  97. // Make sure all the pending calls complete before this Object is unregistered.
  98. WaitForPendingCallsToFinish();
  99. HostingEnvironment.UnregisterObject(this);
  100. Thread.Sleep(100);
  101. HttpRuntime.Close();
  102. Thread.Sleep(100);
  103. }
  104. #endregion
  105. public void Configure(Server server, int port, string virtualPath, string physicalPath,
  106. bool requireAuthentication)
  107. {
  108. Configure(server, port, virtualPath, physicalPath, requireAuthentication, false);
  109. }
  110. public void Configure(Server server, int port, string virtualPath, string physicalPath)
  111. {
  112. Configure(server, port, virtualPath, physicalPath, false, false);
  113. }
  114. public void Configure(Server server, int port, string virtualPath, string physicalPath,
  115. bool requireAuthentication, bool disableDirectoryListing)
  116. {
  117. _server = server;
  118. _port = port;
  119. _installPath = null;
  120. _virtualPath = virtualPath;
  121. _requireAuthentication = requireAuthentication;
  122. _disableDirectoryListing = disableDirectoryListing;
  123. _lowerCasedVirtualPath = CultureInfo.InvariantCulture.TextInfo.ToLower(_virtualPath);
  124. _lowerCasedVirtualPathWithTrailingSlash = virtualPath.EndsWith("/", StringComparison.Ordinal)
  125. ? virtualPath
  126. : virtualPath + "/";
  127. _lowerCasedVirtualPathWithTrailingSlash =
  128. CultureInfo.InvariantCulture.TextInfo.ToLower(_lowerCasedVirtualPathWithTrailingSlash);
  129. _physicalPath = physicalPath;
  130. _physicalClientScriptPath = HttpRuntime.AspClientScriptPhysicalPath + "\\";
  131. _lowerCasedClientScriptPathWithTrailingSlash =
  132. CultureInfo.InvariantCulture.TextInfo.ToLower(HttpRuntime.AspClientScriptVirtualPath + "/");
  133. }
  134. public SecurityIdentifier GetProcessSid()
  135. {
  136. using (WindowsIdentity identity = new WindowsIdentity(_server.GetProcessToken()))
  137. {
  138. return identity.User;
  139. }
  140. }
  141. public IntPtr GetProcessToken()
  142. {
  143. new SecurityPermission(PermissionState.Unrestricted).Assert();
  144. return _server.GetProcessToken();
  145. }
  146. public string GetProcessUser()
  147. {
  148. return _server.GetProcessUser();
  149. }
  150. public override object InitializeLifetimeService()
  151. {
  152. // never expire the license
  153. return null;
  154. }
  155. public bool IsVirtualPathAppPath(string path)
  156. {
  157. if (path == null)
  158. {
  159. return false;
  160. }
  161. path = CultureInfo.InvariantCulture.TextInfo.ToLower(path);
  162. return (path == _lowerCasedVirtualPath || path == _lowerCasedVirtualPathWithTrailingSlash);
  163. }
  164. public bool IsVirtualPathInApp(string path, out bool isClientScriptPath)
  165. {
  166. isClientScriptPath = false;
  167. if (path == null)
  168. {
  169. return false;
  170. }
  171. if (_virtualPath == "/" && path.StartsWith("/", StringComparison.Ordinal))
  172. {
  173. if (path.StartsWith(_lowerCasedClientScriptPathWithTrailingSlash, StringComparison.Ordinal))
  174. {
  175. isClientScriptPath = true;
  176. }
  177. return true;
  178. }
  179. path = CultureInfo.InvariantCulture.TextInfo.ToLower(path);
  180. if (path.StartsWith(_lowerCasedVirtualPathWithTrailingSlash, StringComparison.Ordinal))
  181. {
  182. return true;
  183. }
  184. if (path == _lowerCasedVirtualPath)
  185. {
  186. return true;
  187. }
  188. if (path.StartsWith(_lowerCasedClientScriptPathWithTrailingSlash, StringComparison.Ordinal))
  189. {
  190. isClientScriptPath = true;
  191. return true;
  192. }
  193. return false;
  194. }
  195. public bool IsVirtualPathInApp(String path)
  196. {
  197. bool isClientScriptPath;
  198. return IsVirtualPathInApp(path, out isClientScriptPath);
  199. }
  200. public void ProcessRequest(Connection conn)
  201. {
  202. // Add a pending call to make sure our thread doesn't get killed
  203. AddPendingCall();
  204. try
  205. {
  206. new Request(_server, this, conn).Process();
  207. }
  208. finally
  209. {
  210. RemovePendingCall();
  211. }
  212. }
  213. [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
  214. public void Shutdown()
  215. {
  216. HostingEnvironment.InitiateShutdown();
  217. }
  218. private void AddPendingCall()
  219. {
  220. //TODO: investigate this issue - ref var not volitile
  221. #pragma warning disable 0420
  222. Interlocked.Increment(ref _pendingCallsCount);
  223. #pragma warning restore 0420
  224. }
  225. private void RemovePendingCall()
  226. {
  227. //TODO: investigate this issue - ref var not volitile
  228. #pragma warning disable 0420
  229. Interlocked.Decrement(ref _pendingCallsCount);
  230. #pragma warning restore 0420
  231. }
  232. private void WaitForPendingCallsToFinish()
  233. {
  234. for (;;)
  235. {
  236. if (_pendingCallsCount <= 0)
  237. {
  238. break;
  239. }
  240. Thread.Sleep(250);
  241. }
  242. }
  243. }
  244. }