Stubs for missed public APIs.
[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 using System.Text;
34
35 namespace System.Web.Hosting {
36
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" };
42
43                 private ApplicationHost ()
44                 {
45                 }
46
47                 static string FindWebConfig (string basedir)
48                 {
49                         string r = null;
50                                 
51                         foreach (string s in WebConfigFileNames){
52                                 r = Path.Combine (basedir, s);
53
54                                 if (File.Exists (r))
55                                         return r;
56                         }
57                         // default: return the last one
58                         return r;
59                 }
60
61 #if NET_2_0
62                 static object create_dir = new object ();
63 #endif
64
65                 internal static bool ClearDynamicBaseDirectory (string directory)
66                 {
67                         string[] entries = null;
68                         
69                         try {
70                                 entries = Directory.GetDirectories (directory);
71                         } catch {
72                                 // ignore
73                         }
74
75                         bool dirEmpty = true;
76                         if (entries != null && entries.Length > 0) {
77                                 foreach (string e in entries) {
78                                         if (ClearDynamicBaseDirectory (e)) {
79                                                 try {
80                                                         Directory.Delete (e);
81                                                 } catch {
82                                                         dirEmpty = false;
83                                                 }
84                                         }
85                                 }
86                         }
87
88                         try {
89                                 entries = Directory.GetFiles (directory);
90                         } catch {
91                                 entries = null;
92                         }
93
94                         if (entries != null && entries.Length > 0) {
95                                 foreach (string e in entries) {
96                                         try {
97                                                 File.Delete (e);
98                                         } catch {
99                                                 dirEmpty = false;
100                                         }
101                                 }
102                         }
103
104                         return dirEmpty;
105                 }
106                 
107                 static bool CreateDirectory (string directory)
108                 {
109 #if NET_2_0
110                         lock (create_dir) {
111 #endif
112                                 if (!Directory.Exists (directory)) {
113                                         Directory.CreateDirectory (directory);
114                                         return false;
115                                 } else
116                                         return true;
117 #if NET_2_0
118                         }
119 #endif
120                 }
121
122                 static string BuildPrivateBinPath (string physicalPath, string[] dirs)
123                 {
124 #if NET_2_0
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);
130 #else
131                         return String.Join (";", dirs);
132 #endif
133                 }
134                 
135                 //
136                 // For further details see `Hosting the ASP.NET runtime'
137                 //
138                 //    http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
139                 // 
140 #if TARGET_JVM
141                 [MonoNotSupported ("")]
142 #endif
143                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
144                 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
145                 {
146                         if (physicalDir == null)
147                                 throw new NullReferenceException ();
148
149                         // Make sure physicalDir has file system semantics
150                         // and not uri semantics ( '\' and not '/' ).
151                         physicalDir = Path.GetFullPath (physicalDir);
152
153                         if (hostType == null)
154                                 throw new ArgumentException ("hostType can't be null");
155
156                         if (virtualDir == null)
157                                 throw new ArgumentNullException ("virtualDir");
158
159                         Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
160                         
161                         //
162                         // Setup
163                         //
164                         AppDomainSetup setup = new AppDomainSetup ();
165
166                         setup.ApplicationBase = physicalDir;
167
168                         setup.CachePath = null;
169                         setup.ConfigurationFile = FindWebConfig (physicalDir);
170                         setup.DisallowCodeDownload = true;
171
172                         if (Environment.GetEnvironmentVariable ("MONO_IOMAP") != null || HttpApplication.IsRunningOnWindows)
173                                 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, new string [] { "bin" });
174                         else
175                                 setup.PrivateBinPath = BuildPrivateBinPath (physicalDir, HttpApplication.BinDirs);
176                         setup.PrivateBinPathProbe = "*";
177                         setup.ShadowCopyFiles = "true";
178                         setup.ShadowCopyDirectories = setup.PrivateBinPath;
179
180                         string dynamic_dir = null;
181                         string user = Environment.UserName;
182                         int tempDirTag = 0;
183                         string dirPrefix = String.Concat (user, "-temp-aspnet-");
184                         
185                         for (int i = 0; ; i++){
186                                 string d = Path.Combine (Path.GetTempPath (), String.Concat (dirPrefix, i.ToString ("x")));
187                         
188                                 try {
189                                         CreateDirectory (d);
190                                         string stamp = Path.Combine (d, "stamp");
191                                         CreateDirectory (stamp);
192                                         dynamic_dir = d;
193                                         try {
194                                                 Directory.Delete (stamp);
195                                         } catch (Exception) {
196                                                 // ignore
197                                         }
198                                         
199                                         tempDirTag = i.GetHashCode ();
200                                         break;
201                                 } catch (UnauthorizedAccessException){
202                                         continue;
203                                 }
204                         }
205                         // 
206                         // Unique Domain ID
207                         //
208                         string domain_id = (virtualDir.GetHashCode () ^ physicalDir.GetHashCode () ^ tempDirTag).ToString ("x");
209
210                         setup.ApplicationName = domain_id;
211                         setup.DynamicBase = dynamic_dir;
212
213                         string dynamic_base = setup.DynamicBase;
214                         if (CreateDirectory (dynamic_base) && (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") == null))
215                                 ClearDynamicBaseDirectory (dynamic_base);
216
217                         //
218                         // Create app domain
219                         //
220                         AppDomain appdomain;
221                         appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
222
223                         //
224                         // Populate with the AppDomain data keys expected, Mono only uses a
225                         // few, but third party apps might use others:
226                         //
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));
236 #if NET_2_0
237                         appdomain.SetData ("DataDirectory", Path.Combine (physicalDir, "App_Data"));
238 #endif
239                         appdomain.SetData (MonoHostedDataKey, "yes");
240                         
241                         return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
242                 }
243         }
244 }