ApplicationHost.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. static string [] types = { "Web.config", "Web.Config", "web.config" };
  37. private ApplicationHost ()
  38. {
  39. }
  40. static string FindWebConfig (string basedir)
  41. {
  42. string r = null;
  43. foreach (string s in types){
  44. r = Path.Combine (basedir, s);
  45. if (File.Exists (r))
  46. return r;
  47. }
  48. // default: return the last one
  49. return r;
  50. }
  51. static object create_dir = new object ();
  52. static void CreateDirectory (string directory)
  53. {
  54. #if NET_2_0
  55. lock (create_dir) {
  56. if (!Directory.Exists (directory))
  57. Directory.CreateDirectory (directory);
  58. }
  59. #else
  60. Directory.CreateDirectory (directory);
  61. #endif
  62. }
  63. //
  64. // For further details see `Hosting the ASP.NET runtime'
  65. //
  66. // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
  67. //
  68. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  69. public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
  70. {
  71. if (physicalDir == null)
  72. throw new NullReferenceException ();
  73. #if NET_2_0
  74. physicalDir = Path.GetFullPath (physicalDir);
  75. #endif
  76. // This might throw
  77. Uri u = new Uri (physicalDir);
  78. physicalDir = HttpUtility.UrlDecode (u.AbsolutePath);
  79. if (hostType == null)
  80. throw new NullReferenceException ();
  81. if (virtualDir == null)
  82. throw new NullReferenceException ();
  83. Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
  84. //
  85. // Unique Domain ID
  86. //
  87. string domain_id = "ASPHOST_" + DateTime.Now.ToString().GetHashCode().ToString("x");
  88. //
  89. // Setup
  90. //
  91. AppDomainSetup setup = new AppDomainSetup ();
  92. setup.ApplicationBase = physicalDir;
  93. setup.CachePath = null;
  94. setup.ApplicationName = domain_id;
  95. setup.ConfigurationFile = FindWebConfig (physicalDir);
  96. setup.DisallowCodeDownload = true;
  97. string bin_path = Path.Combine (physicalDir, "bin");
  98. setup.PrivateBinPath = bin_path;
  99. setup.PrivateBinPathProbe = "*";
  100. setup.ShadowCopyFiles = "true";
  101. setup.ShadowCopyDirectories = bin_path;
  102. string dynamic_dir = null;
  103. string user = Environment.UserName;
  104. for (int i = 0; ; i++){
  105. string d = Path.Combine (Path.GetTempPath (),
  106. String.Format ("{0}-temp-aspnet-{1:x}", user, i));
  107. try {
  108. CreateDirectory (d);
  109. string stamp = Path.Combine (d, "stamp");
  110. CreateDirectory (stamp);
  111. dynamic_dir = d;
  112. Directory.Delete (stamp);
  113. break;
  114. } catch (UnauthorizedAccessException){
  115. continue;
  116. }
  117. }
  118. setup.DynamicBase = dynamic_dir;
  119. CreateDirectory (setup.DynamicBase);
  120. //
  121. // Create app domain
  122. //
  123. AppDomain appdomain;
  124. appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
  125. //
  126. // Populate with the AppDomain data keys expected, Mono only uses a
  127. // few, but third party apps might use others:
  128. //
  129. appdomain.SetData (".appDomain", "*");
  130. int l = physicalDir.Length;
  131. if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
  132. physicalDir += Path.DirectorySeparatorChar;
  133. appdomain.SetData (".appPath", physicalDir);
  134. appdomain.SetData (".appVPath", virtualDir);
  135. appdomain.SetData (".domainId", domain_id);
  136. appdomain.SetData (".hostingVirtualPath", virtualDir);
  137. appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
  138. return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
  139. }
  140. }
  141. }