ApplicationHost.cs 6.8 KB

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