ApplicationHost.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.IO;
  30. using System.Security.Permissions;
  31. using System.Security.Policy;
  32. namespace System.Web.Hosting {
  33. // CAS - no InheritanceDemand here as the class is sealed
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. public sealed class ApplicationHost {
  36. internal static readonly string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
  37. internal static string [] WebConfigFileNames = { "Web.config", "Web.Config", "web.config" };
  38. private ApplicationHost ()
  39. {
  40. }
  41. static string FindWebConfig (string basedir)
  42. {
  43. string r = null;
  44. foreach (string s in WebConfigFileNames){
  45. r = Path.Combine (basedir, s);
  46. if (File.Exists (r))
  47. return r;
  48. }
  49. // default: return the last one
  50. return r;
  51. }
  52. #if NET_2_0
  53. static object create_dir = new object ();
  54. #endif
  55. internal static bool ClearDynamicBaseDirectory (string directory)
  56. {
  57. string[] entries = null;
  58. try {
  59. entries = Directory.GetDirectories (directory);
  60. } catch {
  61. // ignore
  62. }
  63. bool dirEmpty = true;
  64. if (entries != null && entries.Length > 0) {
  65. foreach (string e in entries) {
  66. if (ClearDynamicBaseDirectory (e)) {
  67. try {
  68. Directory.Delete (e);
  69. } catch {
  70. dirEmpty = false;
  71. }
  72. }
  73. }
  74. }
  75. try {
  76. entries = Directory.GetFiles (directory);
  77. } catch {
  78. entries = null;
  79. }
  80. if (entries != null && entries.Length > 0) {
  81. foreach (string e in entries) {
  82. try {
  83. File.Delete (e);
  84. } catch {
  85. dirEmpty = false;
  86. }
  87. }
  88. }
  89. return dirEmpty;
  90. }
  91. static bool CreateDirectory (string directory)
  92. {
  93. #if NET_2_0
  94. lock (create_dir) {
  95. #endif
  96. if (!Directory.Exists (directory)) {
  97. Directory.CreateDirectory (directory);
  98. return false;
  99. } else
  100. return true;
  101. #if NET_2_0
  102. }
  103. #endif
  104. }
  105. //
  106. // For further details see `Hosting the ASP.NET runtime'
  107. //
  108. // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
  109. //
  110. #if TARGET_JVM
  111. [MonoNotSupported ("")]
  112. #endif
  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. setup.CachePath = null;
  132. setup.ConfigurationFile = FindWebConfig (physicalDir);
  133. setup.DisallowCodeDownload = true;
  134. if (Environment.GetEnvironmentVariable ("MONO_IOMAP") != null || HttpApplication.IsRunningOnWindows)
  135. setup.PrivateBinPath = "bin";
  136. else
  137. setup.PrivateBinPath = String.Join (";", HttpApplication.BinDirs);
  138. setup.PrivateBinPathProbe = "*";
  139. setup.ShadowCopyFiles = "true";
  140. setup.ShadowCopyDirectories = setup.PrivateBinPath;
  141. string dynamic_dir = null;
  142. string user = Environment.UserName;
  143. int tempDirTag = 0;
  144. for (int i = 0; ; i++){
  145. string d = Path.Combine (Path.GetTempPath (),
  146. String.Format ("{0}-temp-aspnet-{1:x}", user, i));
  147. try {
  148. CreateDirectory (d);
  149. string stamp = Path.Combine (d, "stamp");
  150. CreateDirectory (stamp);
  151. dynamic_dir = d;
  152. try {
  153. Directory.Delete (stamp);
  154. } catch (Exception) {
  155. // ignore
  156. }
  157. tempDirTag = i.GetHashCode ();
  158. break;
  159. } catch (UnauthorizedAccessException){
  160. continue;
  161. }
  162. }
  163. //
  164. // Unique Domain ID
  165. //
  166. string domain_id = (virtualDir.GetHashCode () ^ physicalDir.GetHashCode () ^ tempDirTag).ToString ("x");
  167. setup.ApplicationName = domain_id;
  168. setup.DynamicBase = dynamic_dir;
  169. string dynamic_base = setup.DynamicBase;
  170. if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
  171. ClearDynamicBaseDirectory (dynamic_base);
  172. //
  173. // Create app domain
  174. //
  175. AppDomain appdomain;
  176. appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
  177. //
  178. // Populate with the AppDomain data keys expected, Mono only uses a
  179. // few, but third party apps might use others:
  180. //
  181. appdomain.SetData (".appDomain", "*");
  182. int l = physicalDir.Length;
  183. if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
  184. physicalDir += Path.DirectorySeparatorChar;
  185. appdomain.SetData (".appPath", physicalDir);
  186. appdomain.SetData (".appVPath", virtualDir);
  187. appdomain.SetData (".domainId", domain_id);
  188. appdomain.SetData (".hostingVirtualPath", virtualDir);
  189. appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
  190. #if NET_2_0
  191. appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
  192. #endif
  193. appdomain.SetData (MonoHostedDataKey, "yes");
  194. return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
  195. }
  196. }
  197. }