ApplicationHost.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. 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.ConfigurationFile = FindWebConfig (physicalDir);
  145. setup.DisallowCodeDownload = true;
  146. string[] bindirPath = new string [1] {
  147. #if NET_2_0
  148. Path.Combine (physicalDir, "bin")
  149. #else
  150. "bin"
  151. #endif
  152. };
  153. string bindir;
  154. foreach (string dir in HttpApplication.BinDirs) {
  155. bindir = Path.Combine (physicalDir, dir);
  156. if (Directory.Exists (bindir)) {
  157. #if NET_2_0
  158. bindirPath [0] = bindir;
  159. #else
  160. bindirPath [0] = dir;
  161. #endif
  162. break;
  163. }
  164. }
  165. setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, bindirPath);
  166. setup.PrivateBinPathProbe = "*";
  167. setup.ShadowCopyFiles = "true";
  168. setup.ShadowCopyDirectories = setup.PrivateBinPath;
  169. string dynamic_dir = null;
  170. string user = Environment.UserName;
  171. int tempDirTag = 0;
  172. string dirPrefix = String.Concat (user, "-temp-aspnet-");
  173. for (int i = 0; ; i++){
  174. string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
  175. try {
  176. CreateDirectory (d);
  177. string stamp = Path.Combine (d, "stamp");
  178. CreateDirectory (stamp);
  179. dynamic_dir = d;
  180. try {
  181. Directory.Delete (stamp);
  182. } catch (Exception) {
  183. // ignore
  184. }
  185. tempDirTag = i.GetHashCode ();
  186. break;
  187. } catch (UnauthorizedAccessException){
  188. continue;
  189. }
  190. }
  191. //
  192. // Unique Domain ID
  193. //
  194. string domain_id = (virtualDir.GetHashCode () + 1 ^ physicalDir.GetHashCode () + 2 ^ tempDirTag).ToString ("x");
  195. setup.ApplicationName = domain_id;
  196. setup.DynamicBase = dynamic_dir;
  197. setup.CachePath = dynamic_dir;
  198. string dynamic_base = setup.DynamicBase;
  199. if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
  200. ClearDynamicBaseDirectory (dynamic_base);
  201. //
  202. // Create app domain
  203. //
  204. AppDomain appdomain;
  205. appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
  206. //
  207. // Populate with the AppDomain data keys expected, Mono only uses a
  208. // few, but third party apps might use others:
  209. //
  210. appdomain.SetData (".appDomain", "*");
  211. int l = physicalDir.Length;
  212. if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
  213. physicalDir += Path.DirectorySeparatorChar;
  214. appdomain.SetData (".appPath", physicalDir);
  215. appdomain.SetData (".appVPath", virtualDir);
  216. appdomain.SetData (".domainId", domain_id);
  217. appdomain.SetData (".hostingVirtualPath", virtualDir);
  218. appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
  219. #if NET_2_0
  220. appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
  221. #endif
  222. appdomain.SetData (MonoHostedDataKey, "yes");
  223. return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
  224. }
  225. }
  226. }