2005-10-29 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration / WebConfigurationHost.cs
1 //
2 // System.Web.Configuration.WebConfigurationHost.cs
3 //
4 // Authors:
5 //  Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Security;
34 using System.Configuration;
35 using System.Configuration.Internal;
36 using System.Web.Util;
37
38 namespace System.Web.Configuration
39 {
40         class WebConfigurationHost: IInternalConfigHost
41         {
42                 WebConfigurationFileMap map;
43                 const string MachinePath = ":machine:";
44                 const string MachineWebPath = ":web:";
45                 
46                 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
47                 {
48                         return null;
49                 }
50                 
51                 public virtual object CreateDeprecatedConfigContext (string configPath)
52                 {
53                         throw new NotImplementedException ();
54                 }
55                 
56                 public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
57                 {
58                         throw new NotImplementedException ();
59                 }
60                 
61                 public virtual void DeleteStream (string streamName)
62                 {
63                         File.Delete (streamName);
64                 }
65                 
66                 public virtual string EncryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
67                 {
68                         throw new NotImplementedException ();
69                 }
70                 
71                 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locatinSubPath)
72                 {
73                         return configPath + "/" + locatinSubPath;
74                 }
75                 
76                 public virtual Type GetConfigType (string typeName, bool throwOnError)
77                 {
78                         Type type = Type.GetType (typeName);
79                         if (type == null && throwOnError)
80                                 throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
81                         return type;
82                 }
83                 
84                 public virtual string GetConfigTypeName (Type t)
85                 {
86                         return t.AssemblyQualifiedName;
87                 }
88                 
89                 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
90                 {
91                         throw new NotImplementedException ();
92                 }
93                 
94                 public virtual string GetStreamName (string configPath)
95                 {
96                         if (configPath == MachinePath) {
97                                 if (map == null)
98                                         return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
99                                 else
100                                         return map.MachineConfigFilename;
101                         } else if (configPath == MachineWebPath) {
102                                 if (map == null) {
103                                         string mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
104                                         return GetWebConfigFileName (mdir);
105                                 }
106                                 else
107                                         return null;
108                         }
109                         
110                         string dir = MapPath (configPath);
111                         return GetWebConfigFileName (dir);
112                 }
113                 
114                 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
115                 {
116                         throw new NotImplementedException ();
117                 }
118                 
119                 public virtual object GetStreamVersion (string streamName)
120                 {
121                         throw new NotImplementedException ();
122                 }
123                 
124                 public virtual IDisposable Impersonate ()
125                 {
126                         throw new NotImplementedException ();
127                 }
128                 
129                 public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
130                 {
131                 }
132                 
133                 public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
134                 {
135                         string fullPath = (string) hostInitConfigurationParams [1];
136                         
137                         map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
138                         
139                         if (locationSubPath == MachineWebPath) {
140                                 locationSubPath = MachinePath;
141                                 configPath = MachineWebPath;
142                                 locationConfigPath = null;
143                         }
144                         else if (locationSubPath == MachinePath) {
145                                 locationSubPath = null;
146                                 configPath = MachinePath;
147                                 locationConfigPath = null;
148                         }
149                         else {
150                                 
151                                 int i;
152                                 if (locationSubPath == null) {
153                                         configPath = fullPath;
154                                         i = fullPath.LastIndexOf ("/");
155                                 } else {
156                                         configPath = locationSubPath;
157                                         if (locationSubPath != "/")
158                                                 i = locationSubPath.LastIndexOf ('/');
159                                         else
160                                                 i = -1;
161                                 }
162                                 
163                                 if (i != -1) {
164                                         locationConfigPath = configPath.Substring (i+1);
165                                         
166                                         if (i == 0)
167                                                 locationSubPath = "/";
168                                         else
169                                                 locationSubPath = fullPath.Substring (0, i);
170                                 } else {
171                                         locationSubPath = MachineWebPath;
172                                         locationConfigPath = null;
173                                 }
174                         }
175                         
176                         if (GetStreamName (configPath) == null) {
177                                 // There is no config file for this path. Get the next one in the chain.
178                                 InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath, root, hostInitConfigurationParams);
179                         }
180                 }
181                 
182                 public string MapPath (string virtualPath)
183                 {
184                         if (map != null)
185                                 return MapPathFromMapper (virtualPath);
186                         else
187                                 return HttpContext.Current.Request.MapPath (virtualPath);
188                 }
189                 
190                 public string NormalizeVirtualPath (string virtualPath)
191                 {
192                         if (virtualPath == null || virtualPath.Length == 0)
193                                 virtualPath = ".";
194                         else
195                                 virtualPath = virtualPath.Trim ();
196
197                         if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
198                                 virtualPath = virtualPath.Substring (1);
199                                 
200                         if (System.IO.Path.DirectorySeparatorChar != '/')
201                                 virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
202
203                         if (UrlUtils.IsRooted (virtualPath)) {
204                                 virtualPath = UrlUtils.Canonic (virtualPath);
205                         } else {
206                                 if (map.VirtualDirectories.Count > 0) {
207                                         string root = map.VirtualDirectories [0].VirtualDirectory;
208                                         virtualPath = UrlUtils.Combine (root, virtualPath);
209                                         virtualPath = UrlUtils.Canonic (virtualPath);
210                                 }
211                         }
212                         return virtualPath;
213                 }
214
215                 public string MapPathFromMapper (string virtualPath)
216                 {
217                         string path = NormalizeVirtualPath (virtualPath);
218                         
219                         foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
220                                 if (path.StartsWith (mapping.VirtualDirectory)) {
221                                         int i = mapping.VirtualDirectory.Length;
222                                         if (path.Length == i) {
223                                                 return mapping.PhysicalDirectory;
224                                         }
225                                         else if (path [i] == '/') {
226                                                 string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
227                                                 return Path.Combine (mapping.PhysicalDirectory, pathPart);
228                                         }
229                                 }
230                         }
231                         throw new HttpException ("Invalid virtual directory: " + virtualPath);
232                 }
233
234                 string GetWebConfigFileName (string dir)
235                 {
236                         string file = Path.Combine (dir, "Web.config");
237                         if (File.Exists (file))
238                                 return file;
239                         file = Path.Combine (dir, "web.config");
240                         if (File.Exists (file))
241                                 return file;
242                         return null;
243                 }
244                 
245                 public virtual bool IsAboveApplication (string configPath)
246                 {
247                         throw new NotImplementedException ();
248                 }
249                 
250                 public virtual bool IsConfigRecordRequired (string configPath)
251                 {
252                         throw new NotImplementedException ();
253                 }
254                 
255                 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
256                 {
257                         switch (allowDefinition) {
258                                 case ConfigurationAllowDefinition.MachineOnly:
259                                         return configPath == MachinePath || configPath == MachineWebPath;
260                                 case ConfigurationAllowDefinition.MachineToWebRoot:
261                                 case ConfigurationAllowDefinition.MachineToApplication:
262                                         return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
263                                 default:
264                                         return true;
265                         }
266                 }
267                 
268                 public virtual bool IsFile (string streamName)
269                 {
270                         throw new NotImplementedException ();
271                 }
272                 
273                 public virtual bool IsLocationApplicable (string configPath)
274                 {
275                         throw new NotImplementedException ();
276                 }
277                 
278                 public virtual Stream OpenStreamForRead (string streamName)
279                 {
280                         if (!File.Exists (streamName))
281                                 throw new ConfigurationException ("File '" + streamName + "' not found");
282                                 
283                         return new FileStream (streamName, FileMode.Open, FileAccess.Read);
284                 }
285
286                 [MonoTODO]
287                 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
288                 {
289                         throw new NotImplementedException ();
290                 }
291
292                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
293                 {
294                         return new FileStream (streamName, FileMode.Create, FileAccess.Write);
295                 }
296
297                 [MonoTODO]
298                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
299                 {
300                         throw new NotImplementedException ();
301                 }
302                 
303                 public virtual bool PrefetchAll (string configPath, string streamName)
304                 {
305                         throw new NotImplementedException ();
306                 }
307                 
308                 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
309                 {
310                         throw new NotImplementedException ();
311                 }
312
313                 [MonoTODO]
314                 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
315                 {
316                         throw new NotImplementedException ();
317                 }
318
319                 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
320                 {
321                         throw new NotImplementedException ();
322                 }
323                 
324                 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
325                 {
326                         throw new NotImplementedException ();
327                 }
328                 
329                 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
330                 {
331                         if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
332                                 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
333                 }
334                 
335                 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
336                 {
337                 }
338                 
339                 [MonoTODO]
340                 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
341                 {
342                 }
343
344                 public virtual bool SupportsChangeNotifications {
345                         get { return false; }
346                 }
347                 
348                 public virtual bool SupportsLocation {
349                         get { return false; }
350                 }
351                 
352                 public virtual bool SupportsPath {
353                         get { return false; }
354                 }
355                 
356                 public virtual bool SupportsRefresh {
357                         get { return false; }
358                 }
359
360                 [MonoTODO]
361                 public virtual bool IsRemote {
362                         get { return false; }
363                 }
364
365                 [MonoTODO]
366                 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
367                 {
368                         throw new NotImplementedException ();
369                 }
370
371                 [MonoTODO]
372                 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
373                 {
374                         throw new NotImplementedException ();
375                 }
376
377                 [MonoTODO]
378                 public virtual bool IsSecondaryRoot (string configPath)
379                 {
380                         throw new NotImplementedException ();
381                 }
382
383                 [MonoTODO]
384                 public virtual bool IsTrustedConfigPath (string configPath)
385                 {
386                         throw new NotImplementedException ();
387                 }
388         }
389 }
390
391 #endif