[Cleanup] Removed TARGET_JVM
[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.Configuration;
31 using System.IO;
32 using System.Security.Permissions;
33 using System.Security.Policy;
34 using System.Text;
35 using System.Web.Configuration;
36
37 namespace System.Web.Hosting {
38
39         // CAS - no InheritanceDemand here as the class is sealed
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public sealed class ApplicationHost {
42                 const string DEFAULT_WEB_CONFIG_NAME = "web.config";
43                 internal const string MonoHostedDataKey = ".:!MonoAspNetHostedApp!:.";
44
45                 static object create_dir = new object ();
46
47                 ApplicationHost ()
48                 {
49                 }
50
51                 internal static string FindWebConfig (string basedir)
52                 {
53                         if (String.IsNullOrEmpty (basedir) || !Directory.Exists (basedir))
54                                 return null;
55
56                         string[] files = Directory.GetFileSystemEntries (basedir, "?eb.?onfig");
57                         if (files == null || files.Length == 0)
58                                 return null;
59                         return files [0];
60                 }
61
62                 internal static bool ClearDynamicBaseDirectory (string directory)
63                 {
64                         string[] entries = null;
65                         
66                         try {
67                                 entries = Directory.GetDirectories (directory);
68                         } catch {
69                                 // ignore
70                         }
71
72                         bool dirEmpty = true;
73                         if (entries != null && entries.Length > 0) {
74                                 foreach (string e in entries) {
75                                         if (ClearDynamicBaseDirectory (e)) {
76                                                 try {
77                                                         Directory.Delete (e);
78                                                 } catch {
79                                                         dirEmpty = false;
80                                                 }
81                                         }
82                                 }
83                         }
84
85                         try {
86                                 entries = Directory.GetFiles (directory);
87                         } catch {
88                                 entries = null;
89                         }
90
91                         if (entries != null && entries.Length > 0) {
92                                 foreach (string e in entries) {
93                                         try {
94                                                 File.Delete (e);
95                                         } catch {
96                                                 dirEmpty = false;
97                                         }
98                                 }
99                         }
100
101                         return dirEmpty;
102                 }
103                 
104                 static bool CreateDirectory (string directory)
105                 {
106                         lock (create_dir) {
107                                 if (!Directory.Exists (directory)) {
108                                         Directory.CreateDirectory (directory);
109                                         return false;
110                                 } else
111                                         return true;
112                         }
113                 }
114
115                 static string BuildPrivateBinPath (string physicalPath, string[] dirs)
116                 {
117                         int len = dirs.Length;
118                         string[] ret = new string [len];
119                         for (int i = 0; i < len; i++)
120                                 ret [i] = Path.Combine (physicalPath, dirs [i]);
121                         return String.Join (";", ret);
122                 }
123                 
124                 //
125                 // For further details see `Hosting the ASP.NET runtime'
126                 //
127                 //    http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
128                 // 
129                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
130                 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
131                 {
132                         if (physicalDir == null)
133                                 throw new NullReferenceException ();
134
135                         // Make sure physicalDir has file system semantics
136                         // and not uri semantics ( '\' and not '/' ).
137                         physicalDir = Path.GetFullPath (physicalDir);
138
139                         if (hostType == null)
140                                 throw new ArgumentException ("hostType can't be null");
141
142                         if (virtualDir == null)
143                                 throw new ArgumentNullException ("virtualDir");
144
145                         Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
146                         
147                         //
148                         // Setup
149                         //
150                         AppDomainSetup setup = new AppDomainSetup ();
151
152                         setup.ApplicationBase = physicalDir;
153
154                         string webConfig = FindWebConfig (physicalDir);
155
156                         if (webConfig == null)
157                                 webConfig = Path.Combine (physicalDir, DEFAULT_WEB_CONFIG_NAME);
158                         setup.ConfigurationFile = webConfig;
159                         setup.DisallowCodeDownload = true;
160
161                         string[] bindirPath = new string [1] { Path.Combine (physicalDir, "bin") };
162                         string bindir;
163
164                         foreach (string dir in HttpApplication.BinDirs) {
165                                 bindir = Path.Combine (physicalDir, dir);
166                         
167                                 if (Directory.Exists (bindir)) {
168                                         bindirPath [0] = bindir;
169                                         break;
170                                 }
171                         }
172
173                         setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, bindirPath);
174                         setup.PrivateBinPathProbe = "*";
175                         string dynamic_dir = null;
176                         string user = Environment.UserName;
177                         int tempDirTag = 0;
178                         string dirPrefix = String.Concat (user, "-temp-aspnet-");
179                         
180                         for (int i = 0; ; i++){
181                                 string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
182                         
183                                 try {
184                                         CreateDirectory (d);
185                                         string stamp = Path.Combine (d, "stamp");
186                                         CreateDirectory (stamp);
187                                         dynamic_dir = d;
188                                         try {
189                                                 Directory.Delete (stamp);
190                                         } catch (Exception) {
191                                                 // ignore
192                                         }
193                                         
194                                         tempDirTag = i.GetHashCode ();
195                                         break;
196                                 } catch (UnauthorizedAccessException){
197                                         continue;
198                                 }
199                         }
200                         // 
201                         // Unique Domain ID
202                         //
203                         string domain_id = (virtualDir.GetHashCode () + 1 ^ physicalDir.GetHashCode () + 2 ^ tempDirTag).ToString ("x");
204
205                         // This is used by mod_mono's fail-over support
206                         string domain_id_suffix = Environment.GetEnvironmentVariable ("__MONO_DOMAIN_ID_SUFFIX");
207                         if (domain_id_suffix != null && domain_id_suffix.Length > 0)
208                                 domain_id += domain_id_suffix;
209                         
210                         setup.ApplicationName = domain_id;
211                         setup.DynamicBase = dynamic_dir;
212                         setup.CachePath = dynamic_dir;
213
214                         string dynamic_base = setup.DynamicBase;
215                         if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
216                                 ClearDynamicBaseDirectory (dynamic_base);
217
218                         //
219                         // Create app domain
220                         //
221                         AppDomain appdomain;
222                         appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
223
224                         //
225                         // Populate with the AppDomain data keys expected, Mono only uses a
226                         // few, but third party apps might use others:
227                         //
228                         appdomain.SetData (".appDomain", "*");
229                         int l = physicalDir.Length;
230                         if (physicalDir [l - 1] != Path.DirectorySeparatorChar)
231                                 physicalDir += Path.DirectorySeparatorChar;
232                         appdomain.SetData (".appPath", physicalDir);
233                         appdomain.SetData (".appVPath", virtualDir);
234                         appdomain.SetData (".appId", domain_id);
235                         appdomain.SetData (".domainId", domain_id);
236                         appdomain.SetData (".hostingVirtualPath", virtualDir);
237                         appdomain.SetData (".hostingInstallDir", Path.GetDirectoryName (typeof (Object).Assembly.CodeBase));
238                         appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
239                         appdomain.SetData (MonoHostedDataKey, "yes");
240
241                         appdomain.DoCallBack (SetHostingEnvironment);
242                         return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
243                 }
244
245                 static void SetHostingEnvironment ()
246                 {
247                         bool shadow_copy_enabled = true;
248                         HostingEnvironmentSection he = WebConfigurationManager.GetWebApplicationSection ("system.web/hostingEnvironment") as HostingEnvironmentSection;
249                         if (he != null)
250                                 shadow_copy_enabled = he.ShadowCopyBinAssemblies;
251
252                         if (shadow_copy_enabled) {
253                                 AppDomain current = AppDomain.CurrentDomain;
254                                 current.SetShadowCopyFiles ();
255                                 current.SetShadowCopyPath (current.SetupInformation.PrivateBinPath);
256                         }
257
258                         HostingEnvironment.IsHosted = true;
259                         HostingEnvironment.SiteName = HostingEnvironment.ApplicationID;
260                 }
261         }
262 }