ApplicationHost.cs 5.3 KB

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