2
0

ApplicationHost.cs 6.2 KB

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