ApplicationHost.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // System.Web.Hosting.ApplicationHost.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Configuration;
  30. using System.IO;
  31. using System.Security.Permissions;
  32. using System.Security.Policy;
  33. using System.Text;
  34. using System.Web.Configuration;
  35. namespace System.Web.Hosting {
  36. // CAS - no InheritanceDemand here as the class is sealed
  37. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public sealed class ApplicationHost {
  39. const string DEFAULT_WEB_CONFIG_NAME = "web.config";
  40. internal const string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
  41. static object create_dir = new object ();
  42. ApplicationHost ()
  43. {
  44. }
  45. internal static string FindWebConfig (string basedir)
  46. {
  47. if (String.IsNullOrEmpty (basedir) || !Directory.Exists (basedir))
  48. return null;
  49. string[] files = Directory.GetFileSystemEntries (basedir, "?eb.?onfig");
  50. if (files == null || files.Length == 0)
  51. return null;
  52. return files [0];
  53. }
  54. internal static bool ClearDynamicBaseDirectory (string directory)
  55. {
  56. string[] entries = null;
  57. try {
  58. entries = Directory.GetDirectories (directory);
  59. } catch {
  60. // ignore
  61. }
  62. bool dirEmpty = true;
  63. if (entries != null && entries.Length > 0) {
  64. foreach (string e in entries) {
  65. if (ClearDynamicBaseDirectory (e)) {
  66. try {
  67. Directory.Delete (e);
  68. } catch {
  69. dirEmpty = false;
  70. }
  71. }
  72. }
  73. }
  74. try {
  75. entries = Directory.GetFiles (directory);
  76. } catch {
  77. entries = null;
  78. }
  79. if (entries != null && entries.Length > 0) {
  80. foreach (string e in entries) {
  81. try {
  82. File.Delete (e);
  83. } catch {
  84. dirEmpty = false;
  85. }
  86. }
  87. }
  88. return dirEmpty;
  89. }
  90. static bool CreateDirectory (string directory)
  91. {
  92. lock (create_dir) {
  93. if (!Directory.Exists (directory)) {
  94. Directory.CreateDirectory (directory);
  95. return false;
  96. } else
  97. return true;
  98. }
  99. }
  100. static string BuildPrivateBinPath (string physicalPath, string[] dirs)
  101. {
  102. int len = dirs.Length;
  103. string[] ret = new string [len];
  104. for (int i = 0; i < len; i++)
  105. ret [i] = Path.Combine (physicalPath, dirs [i]);
  106. return String.Join (";", ret);
  107. }
  108. //
  109. // For further details see `Hosting the ASP.NET runtime'
  110. //
  111. // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
  112. //
  113. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  114. public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
  115. {
  116. if (physicalDir == null)
  117. throw new NullReferenceException ();
  118. // Make sure physicalDir has file system semantics
  119. // and not uri semantics ( '\' and not '/' ).
  120. physicalDir = Path.GetFullPath (physicalDir);
  121. if (hostType == null)
  122. throw new ArgumentException ("hostType can't be null");
  123. if (virtualDir == null)
  124. throw new ArgumentNullException ("virtualDir");
  125. Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
  126. //
  127. // Setup
  128. //
  129. AppDomainSetup setup = new AppDomainSetup ();
  130. setup.ApplicationBase = physicalDir;
  131. string webConfig = FindWebConfig (physicalDir);
  132. if (webConfig == null)
  133. webConfig = Path.Combine (physicalDir, DEFAULT_WEB_CONFIG_NAME);
  134. setup.ConfigurationFile = webConfig;
  135. setup.DisallowCodeDownload = true;
  136. string[] bindirPath = new string [1] { Path.Combine (physicalDir, "bin") };
  137. string bindir;
  138. foreach (string dir in HttpApplication.BinDirs) {
  139. bindir = Path.Combine (physicalDir, dir);
  140. if (Directory.Exists (bindir)) {
  141. bindirPath [0] = bindir;
  142. break;
  143. }
  144. }
  145. setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, bindirPath);
  146. setup.PrivateBinPathProbe = "*";
  147. string dynamic_dir = null;
  148. string user = Environment.UserName;
  149. int tempDirTag = 0;
  150. string dirPrefix = String.Concat (user, "-temp-aspnet-");
  151. for (int i = 0; ; i++){
  152. string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
  153. try {
  154. CreateDirectory (d);
  155. string stamp = Path.Combine (d, "stamp");
  156. CreateDirectory (stamp);
  157. dynamic_dir = d;
  158. try {
  159. Directory.Delete (stamp);
  160. } catch (Exception) {
  161. // ignore
  162. }
  163. tempDirTag = i.GetHashCode ();
  164. break;
  165. } catch (UnauthorizedAccessException){
  166. continue;
  167. }
  168. }
  169. //
  170. // Unique Domain ID
  171. //
  172. string domain_id = (virtualDir.GetHashCode () + 1 ^ physicalDir.GetHashCode () + 2 ^ tempDirTag).ToString ("x");
  173. // This is used by mod_mono's fail-over support
  174. string domain_id_suffix = Environment.GetEnvironmentVariable ("__MONO_DOMAIN_ID_SUFFIX");
  175. if (domain_id_suffix != null && domain_id_suffix.Length > 0)
  176. domain_id += domain_id_suffix;
  177. setup.ApplicationName = domain_id;
  178. setup.DynamicBase = dynamic_dir;
  179. setup.CachePath = dynamic_dir;
  180. string dynamic_base = setup.DynamicBase;
  181. if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
  182. ClearDynamicBaseDirectory (dynamic_base);
  183. //
  184. // Create app domain
  185. //
  186. AppDomain appdomain;
  187. appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
  188. //
  189. // Populate with the AppDomain data keys expected, Mono only uses a
  190. // few, but third party apps might use others:
  191. //
  192. appdomain.SetData (".appDomain", "*");
  193. int l = physicalDir.Length;
  194. if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
  195. physicalDir += Path.DirectorySeparatorChar;
  196. appdomain.SetData (".appPath", physicalDir);
  197. appdomain.SetData (".appVPath", virtualDir);
  198. appdomain.SetData (".appId", domain_id);
  199. appdomain.SetData (".domainId", domain_id);
  200. appdomain.SetData (".hostingVirtualPath", virtualDir);
  201. appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
  202. appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
  203. appdomain.SetData (MonoHostedDataKey, "yes");
  204. appdomain.DoCallBack (SetHostingEnvironment);
  205. return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
  206. }
  207. static void SetHostingEnvironment ()
  208. {
  209. bool shadow_copy_enabled = true;
  210. HostingEnvironmentSection he = WebConfigurationManager.GetWebApplicationSection ("system.web/hostingEnvironment") as HostingEnvironmentSection;
  211. if (he != null)
  212. shadow_copy_enabled = he.ShadowCopyBinAssemblies;
  213. if (shadow_copy_enabled) {
  214. AppDomain current = AppDomain.CurrentDomain;
  215. current.SetShadowCopyFiles ();
  216. current.SetShadowCopyPath (current.SetupInformation.PrivateBinPath);
  217. }
  218. HostingEnvironment.IsHosted = true;
  219. HostingEnvironment.SiteName = HostingEnvironment.ApplicationID;
  220. }
  221. }
  222. }