ApplicationHost.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. internal static readonly string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
  40. internal static string [] WebConfigFileNames = { "web.config", "Web.config", "Web.Config" };
  41. ApplicationHost ()
  42. {
  43. }
  44. static string FindWebConfig (string basedir)
  45. {
  46. string r = null;
  47. foreach (string s in WebConfigFileNames){
  48. r = Path.Combine (basedir, s);
  49. if (File.Exists (r))
  50. return r;
  51. }
  52. // default: return the last one
  53. return r;
  54. }
  55. #if NET_2_0
  56. static object create_dir = new object ();
  57. #endif
  58. internal static bool ClearDynamicBaseDirectory (string directory)
  59. {
  60. string[] entries = null;
  61. try {
  62. entries = Directory.GetDirectories (directory);
  63. } catch {
  64. // ignore
  65. }
  66. bool dirEmpty = true;
  67. if (entries != null && entries.Length > 0) {
  68. foreach (string e in entries) {
  69. if (ClearDynamicBaseDirectory (e)) {
  70. try {
  71. Directory.Delete (e);
  72. } catch {
  73. dirEmpty = false;
  74. }
  75. }
  76. }
  77. }
  78. try {
  79. entries = Directory.GetFiles (directory);
  80. } catch {
  81. entries = null;
  82. }
  83. if (entries != null && entries.Length > 0) {
  84. foreach (string e in entries) {
  85. try {
  86. File.Delete (e);
  87. } catch {
  88. dirEmpty = false;
  89. }
  90. }
  91. }
  92. return dirEmpty;
  93. }
  94. static bool CreateDirectory (string directory)
  95. {
  96. #if NET_2_0
  97. lock (create_dir) {
  98. #endif
  99. if (!Directory.Exists (directory)) {
  100. Directory.CreateDirectory (directory);
  101. return false;
  102. } else
  103. return true;
  104. #if NET_2_0
  105. }
  106. #endif
  107. }
  108. static string BuildPrivateBinPath (string physicalPath, string[] dirs)
  109. {
  110. #if NET_2_0
  111. int len = dirs.Length;
  112. string[] ret = new string [len];
  113. for (int i = 0; i < len; i++)
  114. ret [i] = Path.Combine (physicalPath, dirs [i]);
  115. return String.Join (";", ret);
  116. #else
  117. return String.Join (";", dirs);
  118. #endif
  119. }
  120. //
  121. // For further details see `Hosting the ASP.NET runtime'
  122. //
  123. // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
  124. //
  125. #if TARGET_JVM
  126. [MonoNotSupported ("")]
  127. #endif
  128. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  129. public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
  130. {
  131. if (physicalDir == null)
  132. throw new NullReferenceException ();
  133. // Make sure physicalDir has file system semantics
  134. // and not uri semantics ( '\' and not '/' ).
  135. physicalDir = Path.GetFullPath (physicalDir);
  136. if (hostType == null)
  137. throw new ArgumentException ("hostType can't be null");
  138. if (virtualDir == null)
  139. throw new ArgumentNullException ("virtualDir");
  140. Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
  141. //
  142. // Setup
  143. //
  144. AppDomainSetup setup = new AppDomainSetup ();
  145. setup.ApplicationBase = physicalDir;
  146. setup.ConfigurationFile = FindWebConfig (physicalDir);
  147. setup.DisallowCodeDownload = true;
  148. string[] bindirPath = new string [1] {
  149. #if NET_2_0
  150. Path.Combine (physicalDir, "bin")
  151. #else
  152. "bin"
  153. #endif
  154. };
  155. string bindir;
  156. foreach (string dir in HttpApplication.BinDirs) {
  157. bindir = Path.Combine (physicalDir, dir);
  158. if (Directory.Exists (bindir)) {
  159. #if NET_2_0
  160. bindirPath [0] = bindir;
  161. #else
  162. bindirPath [0] = dir;
  163. #endif
  164. break;
  165. }
  166. }
  167. setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, bindirPath);
  168. setup.PrivateBinPathProbe = "*";
  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. // This is used by mod_mono's fail-over support
  196. string domain_id_suffix = Environment.GetEnvironmentVariable ("__MONO_DOMAIN_ID_SUFFIX");
  197. if (domain_id_suffix != null && domain_id_suffix.Length > 0)
  198. domain_id += domain_id_suffix;
  199. setup.ApplicationName = domain_id;
  200. setup.DynamicBase = dynamic_dir;
  201. setup.CachePath = dynamic_dir;
  202. string dynamic_base = setup.DynamicBase;
  203. if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
  204. ClearDynamicBaseDirectory (dynamic_base);
  205. //
  206. // Create app domain
  207. //
  208. AppDomain appdomain;
  209. appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
  210. //
  211. // Populate with the AppDomain data keys expected, Mono only uses a
  212. // few, but third party apps might use others:
  213. //
  214. appdomain.SetData (".appDomain", "*");
  215. int l = physicalDir.Length;
  216. if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
  217. physicalDir += Path.DirectorySeparatorChar;
  218. appdomain.SetData (".appPath", physicalDir);
  219. appdomain.SetData (".appVPath", virtualDir);
  220. appdomain.SetData (".appId", domain_id);
  221. appdomain.SetData (".domainId", domain_id);
  222. appdomain.SetData (".hostingVirtualPath", virtualDir);
  223. appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
  224. #if NET_2_0
  225. appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
  226. #endif
  227. appdomain.SetData (MonoHostedDataKey, "yes");
  228. #if NET_2_0
  229. appdomain.DoCallBack (SetHostingEnvironment);
  230. #endif
  231. return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
  232. }
  233. #if NET_2_0
  234. static void SetHostingEnvironment ()
  235. {
  236. bool shadow_copy_enabled = true;
  237. HostingEnvironmentSection he = WebConfigurationManager.GetWebApplicationSection ("system.web/hostingEnvironment") as HostingEnvironmentSection;
  238. if (he != null)
  239. shadow_copy_enabled = he.ShadowCopyBinAssemblies;
  240. if (shadow_copy_enabled) {
  241. AppDomain current = AppDomain.CurrentDomain;
  242. current.SetShadowCopyFiles ();
  243. current.SetShadowCopyPath (current.SetupInformation.PrivateBinPath);
  244. }
  245. HostingEnvironment.IsHosted = true;
  246. HostingEnvironment.SiteName = HostingEnvironment.ApplicationID;
  247. }
  248. #endif
  249. }
  250. }