Revert accidental commit - this change is not accepted yet
[mono.git] / mcs / class / System.Web / System.Web.Hosting / ApplicationHost.cs
1 //
2 // System.Web.Hosting.ApplicationHost.cs 
3 // 
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com)
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
30 using System.IO;
31 using System.Security.Permissions;
32 using System.Security.Policy;
33
34 namespace System.Web.Hosting {
35
36         // CAS - no InheritanceDemand here as the class is sealed
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         public sealed class ApplicationHost {
39
40                 static string [] types = { "Web.config", "Web.Config", "web.config" };
41
42                 private ApplicationHost ()
43                 {
44                 }
45
46                 static string FindWebConfig (string basedir)
47                 {
48                         string r = null;
49                                 
50                         foreach (string s in types){
51                                 r = Path.Combine (basedir, s);
52
53                                 if (File.Exists (r))
54                                         return r;
55                         }
56                         // default: return the last one
57                         return r;
58                 }
59
60 #if NET_2_0
61                 static object create_dir = new object ();
62 #endif
63                 
64                 static void CreateDirectory (string directory)
65                 {
66 #if NET_2_0
67                         lock (create_dir) {
68                                 if (!Directory.Exists (directory))
69                                         Directory.CreateDirectory (directory);
70                         }
71 #else
72                         Directory.CreateDirectory (directory);
73 #endif
74                 }
75
76                 //
77                 // For further details see `Hosting the ASP.NET runtime'
78                 //
79                 //    http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
80                 // 
81                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
82                 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
83                 {
84                         if (physicalDir == null)
85                                 throw new NullReferenceException ();
86
87                         // Make sure physicalDir has file system semantics
88                         // and not uri semantics ( '\' and not '/' ).
89                         physicalDir = Path.GetFullPath (physicalDir);
90
91                         if (hostType == null)
92                                 throw new ArgumentException ("hostType can't be null");
93
94                         if (virtualDir == null)
95                                 throw new ArgumentNullException ("virtualDir");
96
97                         Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
98
99                         // 
100                         // Unique Domain ID
101                         //
102                         string domain_id = "ASPHOST_" + DateTime.Now.ToString().GetHashCode().ToString("x");
103                         
104                         //
105                         // Setup
106                         //
107                         AppDomainSetup setup = new AppDomainSetup ();
108
109                         setup.ApplicationBase = physicalDir;
110
111                         setup.CachePath = null;
112                         setup.ApplicationName = domain_id;
113                         setup.ConfigurationFile = FindWebConfig (physicalDir);
114                         setup.DisallowCodeDownload = true;
115                         string bin_path = Path.Combine (physicalDir, "bin");
116                         setup.PrivateBinPath = bin_path;
117                         setup.PrivateBinPathProbe = "*";
118                         setup.ShadowCopyFiles = "true";
119                         setup.ShadowCopyDirectories = bin_path;
120
121                         string dynamic_dir = null;
122                         string user = Environment.UserName;
123                         for (int i = 0; ; i++){
124                                 string d = Path.Combine (Path.GetTempPath (),
125                                         String.Format ("{0}-temp-aspnet-{1:x}", user, i));
126                         
127                                 try {
128                                         CreateDirectory (d);
129                                         string stamp = Path.Combine (d, "stamp");
130                                         CreateDirectory (stamp);
131                                         dynamic_dir = d;
132                                         Directory.Delete (stamp);
133                                         break;
134                                 } catch (UnauthorizedAccessException){
135                                         continue;
136                                 }
137                         }
138                         setup.DynamicBase = dynamic_dir;
139                         CreateDirectory (setup.DynamicBase);
140
141                         //
142                         // Create app domain
143                         //
144                         AppDomain appdomain;
145                         appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
146
147                         //
148                         // Populate with the AppDomain data keys expected, Mono only uses a
149                         // few, but third party apps might use others:
150                         //
151                         appdomain.SetData (".appDomain", "*");
152                         int l = physicalDir.Length;
153                         if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
154                                 physicalDir += Path.DirectorySeparatorChar;
155                         appdomain.SetData (".appPath", physicalDir);
156                         appdomain.SetData (".appVPath", virtualDir);
157                         appdomain.SetData (".domainId", domain_id);
158                         appdomain.SetData (".hostingVirtualPath", virtualDir);
159                         appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
160 #if NET_2_0
161                         appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
162 #endif
163                         return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
164                 }
165         }
166 }