New test.
[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" };
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                 //
61                 // For further details see `Hosting the ASP.NET runtime'
62                 //
63                 //    http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.asp
64                 // 
65                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
66                 public static object CreateApplicationHost (Type hostType, string virtualDir, string physicalDir)
67                 {
68                         if (physicalDir == null)
69                                 throw new NullReferenceException ();
70
71 #if NET_2_0
72                         physicalDir = Path.GetFullPath (physicalDir);
73 #endif
74                         // This might throw
75                         Uri u = new Uri (physicalDir);
76                         physicalDir = HttpUtility.UrlDecode (u.AbsolutePath);
77
78                         if (hostType == null)
79                                 throw new NullReferenceException ();
80
81                         if (virtualDir == null)
82                                 throw new NullReferenceException ();
83
84                         Evidence evidence = new Evidence (AppDomain.CurrentDomain.Evidence);
85
86                         // 
87                         // Unique Domain ID
88                         //
89                         string domain_id = "ASPHOST_" + DateTime.Now.ToString().GetHashCode().ToString("x");
90                         
91                         //
92                         // Setup
93                         //
94                         AppDomainSetup setup = new AppDomainSetup ();
95
96                         setup.ApplicationBase = physicalDir;
97
98                         setup.CachePath = null;
99                         setup.ApplicationName = domain_id;
100                         setup.ConfigurationFile = FindWebConfig (physicalDir);
101                         setup.DisallowCodeDownload = true;
102                         string bin_path = Path.Combine (physicalDir, "bin");
103                         setup.PrivateBinPath = bin_path;
104                         setup.PrivateBinPathProbe = "*";
105                         setup.ShadowCopyFiles = "true";
106                         setup.ShadowCopyDirectories = bin_path;
107
108                         string dynamic_dir = null;
109                         string user = Environment.UserName;
110                         for (int i = 0; ; i++){
111                                 string d = Path.Combine (Path.GetTempPath (),
112                                         String.Format ("{0}-temp-aspnet-{1:x}", user, i));
113                         
114                                 try {
115                                         Directory.CreateDirectory (d);
116                                         string stamp = Path.Combine (d, "stamp");
117                                         Directory.CreateDirectory (stamp);
118                                         dynamic_dir = d;
119                                         Directory.Delete (stamp);
120                                         break;
121                                 } catch (UnauthorizedAccessException){
122                                         continue;
123                                 }
124                         }
125                         setup.DynamicBase = dynamic_dir;
126                         Directory.CreateDirectory (setup.DynamicBase);
127
128                         //
129                         // Create app domain
130                         //
131                         AppDomain appdomain;
132                         appdomain = AppDomain.CreateDomain (domain_id, evidence, setup);
133
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
148                         return appdomain.CreateInstanceAndUnwrap (hostType.Module.Assembly.FullName, hostType.FullName);
149                 }
150         }
151 }