2 // System.Web.Hosting.ApplicationHost.cs
5 // Miguel de Icaza (miguel@novell.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Security.Permissions;
32 using System.Security.Policy;
35 namespace System.Web.Hosting {
37 // CAS - no InheritanceDemand here as the class is sealed
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 public sealed class ApplicationHost {
40 internal static readonly string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
41 internal static string [] WebConfigFileNames = { "Web.config", "Web.Config", "web.config" };
43 private ApplicationHost ()
47 static string FindWebConfig (string basedir)
51 foreach (string s in WebConfigFileNames){
52 r = Path.Combine (basedir, s);
57 // default: return the last one
62 static object create_dir = new object ();
65 internal static bool ClearDynamicBaseDirectory (string directory)
67 string[] entries = null;
70 entries = Directory.GetDirectories (directory);
76 if (entries != null && entries.Length > 0) {
77 foreach (string e in entries) {
78 if (ClearDynamicBaseDirectory (e)) {
89 entries = Directory.GetFiles (directory);
94 if (entries != null && entries.Length > 0) {
95 foreach (string e in entries) {
107 static bool CreateDirectory (string directory)
112 if (!Directory.Exists (directory)) {
113 Directory.CreateDirectory (directory);
122 static string BuildPrivateBinPath (string physicalPath, string[] dirs)
125 int len = dirs.Length;
126 string[] ret = new string [len];
127 for (int i = 0; i < len; i++)
128 ret [i] = Path.Combine (physicalPath, dirs [i]);
129 return String.Join (";", ret);
131 return String.Join (";", dirs);
136 // For further details see `Hosting the ASP.NET runtime'
138 // http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
141 [MonoNotSupported ("")]
143 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
144 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
146 if (physicalDir == null)
147 throw new NullReferenceException ();
149 // Make sure physicalDir has file system semantics
150 // and not uri semantics ( '\' and not '/' ).
151 physicalDir = Path.GetFullPath (physicalDir);
153 if (hostType == null)
154 throw new ArgumentException ("hostType can't be null");
156 if (virtualDir == null)
157 throw new ArgumentNullException ("virtualDir");
159 Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
164 AppDomainSetup setup = new AppDomainSetup ();
166 setup.ApplicationBase = physicalDir;
168 setup.CachePath = null;
169 setup.ConfigurationFile = FindWebConfig (physicalDir);
170 setup.DisallowCodeDownload = true;
172 if (Environment.GetEnvironmentVariable ("MONO_IOMAP") != null || HttpApplication.IsRunningOnWindows)
173 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, new string [] { "bin" });
175 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, HttpApplication.BinDirs);
176 setup.PrivateBinPathProbe = "*";
177 setup.ShadowCopyFiles = "true";
178 setup.ShadowCopyDirectories = setup.PrivateBinPath;
180 string dynamic_dir = null;
181 string user = Environment.UserName;
183 string dirPrefix = String.Concat (user, "-temp-aspnet-");
185 for (int i = 0; ; i++){
186 string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
190 string stamp = Path.Combine (d, "stamp");
191 CreateDirectory (stamp);
194 Directory.Delete (stamp);
195 } catch (Exception) {
199 tempDirTag = i.GetHashCode ();
201 } catch (UnauthorizedAccessException){
208 string domain_id = (virtualDir.GetHashCode () ^ physicalDir.GetHashCode () ^ tempDirTag).ToString ("x");
210 setup.ApplicationName = domain_id;
211 setup.DynamicBase = dynamic_dir;
213 string dynamic_base = setup.DynamicBase;
214 if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
215 ClearDynamicBaseDirectory (dynamic_base);
221 appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
224 // Populate with the AppDomain data keys expected, Mono only uses a
225 // few, but third party apps might use others:
227 appdomain.SetData (".appDomain", "*");
228 int l = physicalDir.Length;
229 if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
230 physicalDir += Path.DirectorySeparatorChar;
231 appdomain.SetData (".appPath", physicalDir);
232 appdomain.SetData (".appVPath", virtualDir);
233 appdomain.SetData (".domainId", domain_id);
234 appdomain.SetData (".hostingVirtualPath", virtualDir);
235 appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
237 appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
239 appdomain.SetData (MonoHostedDataKey, "yes");
241 return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);