In .:
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / 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 /*
39  * this class needs to be rewritten to support usage of the
40  * IRemoteWebConfigurationHostServer interface.  Once that's done, we
41  * need an implementation of that interface that talks (through a web
42  * service?) to a remote site..
43  *
44  * for now, though, just implement it as we do
45  * System.Configuration.InternalConfigurationHost, i.e. the local
46  * case.
47  */
48 namespace System.Web.Configuration
49 {
50         class WebConfigurationHost: IInternalConfigHost
51         {
52                 WebConfigurationFileMap map;
53                 const string MachinePath = ":machine:";
54                 const string MachineWebPath = ":web:";
55                 
56                 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
57                 {
58                         return new WebContext (WebApplicationLevel.AtApplication /* XXX */,
59                                                "" /* site XXX */,
60                                                "" /* application path XXX */,
61                                                configPath,
62                                                locationSubPath);
63                 }
64                 
65                 public virtual object CreateDeprecatedConfigContext (string configPath)
66                 {
67                         throw new NotImplementedException ();
68                 }
69                 
70                 public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
71                 {
72                         throw new NotImplementedException ();
73                 }
74                 
75                 public virtual void DeleteStream (string streamName)
76                 {
77                         File.Delete (streamName);
78                 }
79                 
80                 public virtual string EncryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
81                 {
82                         throw new NotImplementedException ();
83                 }
84                 
85                 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locatinSubPath)
86                 {
87                         return configPath + "/" + locatinSubPath;
88                 }
89                 
90                 public virtual Type GetConfigType (string typeName, bool throwOnError)
91                 {
92                         Type type = Type.GetType (typeName);
93                         if (type == null && throwOnError)
94                                 throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
95                         return type;
96                 }
97                 
98                 public virtual string GetConfigTypeName (Type t)
99                 {
100                         return t.AssemblyQualifiedName;
101                 }
102                 
103                 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
104                 {
105                         throw new NotImplementedException ();
106                 }
107                 
108                 public virtual string GetStreamName (string configPath)
109                 {
110                         if (configPath == MachinePath) {
111                                 if (map == null)
112                                         return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
113                                 else
114                                         return map.MachineConfigFilename;
115                         } else if (configPath == MachineWebPath) {
116                                 string mdir;
117
118                                 if (map == null)
119                                         mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
120                                 else
121                                         mdir = Path.GetDirectoryName (map.MachineConfigFilename);
122
123                                 return GetWebConfigFileName (mdir);
124                         }
125                         
126                         string dir = MapPath (configPath);
127                         return GetWebConfigFileName (dir);
128                 }
129                 
130                 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
131                 {
132                         throw new NotImplementedException ();
133                 }
134                 
135                 public virtual object GetStreamVersion (string streamName)
136                 {
137                         throw new NotImplementedException ();
138                 }
139                 
140                 public virtual IDisposable Impersonate ()
141                 {
142                         throw new NotImplementedException ();
143                 }
144                 
145                 public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
146                 {
147                 }
148                 
149                 public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
150                 {
151                         string fullPath = (string) hostInitConfigurationParams [1];
152                         
153                         map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
154                         
155                         if (locationSubPath == MachineWebPath) {
156                                 locationSubPath = MachinePath;
157                                 configPath = MachineWebPath;
158                                 locationConfigPath = null;
159                         }
160                         else if (locationSubPath == MachinePath) {
161                                 locationSubPath = null;
162                                 configPath = MachinePath;
163                                 locationConfigPath = null;
164                         }
165                         else {
166                                 
167                                 int i;
168                                 if (locationSubPath == null) {
169                                         configPath = fullPath;
170                                         i = fullPath.LastIndexOf ("/");
171                                 } else {
172                                         configPath = locationSubPath;
173                                         if (locationSubPath != "/")
174                                                 i = locationSubPath.LastIndexOf ('/');
175                                         else
176                                                 i = -1;
177                                 }
178                                 
179                                 if (i != -1) {
180                                         locationConfigPath = configPath.Substring (i+1);
181                                         
182                                         if (i == 0)
183                                                 locationSubPath = "/";
184                                         else
185                                                 locationSubPath = fullPath.Substring (0, i);
186                                 } else {
187                                         locationSubPath = MachineWebPath;
188                                         locationConfigPath = null;
189                                 }
190                         }
191                         
192                         if (GetStreamName (configPath) == null) {
193                                 // There is no config file for this path. Get the next one in the chain.
194                                 InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath, root, hostInitConfigurationParams);
195                         }
196                 }
197                 
198                 public string MapPath (string virtualPath)
199                 {
200                         if (map != null)
201                                 return MapPathFromMapper (virtualPath);
202                         else if (HttpContext.Current != null
203                                  && HttpContext.Current.Request != null)
204                                 return HttpContext.Current.Request.MapPath (virtualPath);
205                         else
206                                 return virtualPath;
207                 }
208                 
209                 public string NormalizeVirtualPath (string virtualPath)
210                 {
211                         if (virtualPath == null || virtualPath.Length == 0)
212                                 virtualPath = ".";
213                         else
214                                 virtualPath = virtualPath.Trim ();
215
216                         if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
217                                 virtualPath = virtualPath.Substring (1);
218                                 
219                         if (System.IO.Path.DirectorySeparatorChar != '/')
220                                 virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
221
222                         if (UrlUtils.IsRooted (virtualPath)) {
223                                 virtualPath = UrlUtils.Canonic (virtualPath);
224                         } else {
225                                 if (map.VirtualDirectories.Count > 0) {
226                                         string root = map.VirtualDirectories [0].VirtualDirectory;
227                                         virtualPath = UrlUtils.Combine (root, virtualPath);
228                                         virtualPath = UrlUtils.Canonic (virtualPath);
229                                 }
230                         }
231                         return virtualPath;
232                 }
233
234                 public string MapPathFromMapper (string virtualPath)
235                 {
236                         string path = NormalizeVirtualPath (virtualPath);
237                         
238                         foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
239                                 if (path.StartsWith (mapping.VirtualDirectory)) {
240                                         int i = mapping.VirtualDirectory.Length;
241                                         if (path.Length == i) {
242                                                 return mapping.PhysicalDirectory;
243                                         }
244                                         else if (path [i] == '/') {
245                                                 string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
246                                                 return Path.Combine (mapping.PhysicalDirectory, pathPart);
247                                         }
248                                 }
249                         }
250                         throw new HttpException ("Invalid virtual directory: " + virtualPath);
251                 }
252
253                 string GetWebConfigFileName (string dir)
254                 {
255                         string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
256
257                         foreach (string fn in filenames) {
258                                 string file = Path.Combine (dir, fn);
259                                 if (File.Exists (file))
260                                         return file;
261                         }
262
263                         return null;
264                 }
265                 
266                 public virtual bool IsAboveApplication (string configPath)
267                 {
268                         throw new NotImplementedException ();
269                 }
270                 
271                 public virtual bool IsConfigRecordRequired (string configPath)
272                 {
273                         throw new NotImplementedException ();
274                 }
275                 
276                 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
277                 {
278                         switch (allowDefinition) {
279                                 case ConfigurationAllowDefinition.MachineOnly:
280                                         return configPath == MachinePath || configPath == MachineWebPath;
281                                 case ConfigurationAllowDefinition.MachineToWebRoot:
282                                 case ConfigurationAllowDefinition.MachineToApplication:
283                                         return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
284                                 default:
285                                         return true;
286                         }
287                 }
288                 
289                 public virtual bool IsFile (string streamName)
290                 {
291                         throw new NotImplementedException ();
292                 }
293                 
294                 public virtual bool IsLocationApplicable (string configPath)
295                 {
296                         throw new NotImplementedException ();
297                 }
298                 
299                 public virtual Stream OpenStreamForRead (string streamName)
300                 {
301                         if (!File.Exists (streamName))
302                                 throw new ConfigurationException ("File '" + streamName + "' not found");
303                                 
304                         return new FileStream (streamName, FileMode.Open, FileAccess.Read);
305                 }
306
307                 [MonoTODO]
308                 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
309                 {
310                         throw new NotImplementedException ();
311                 }
312
313                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
314                 {
315                         return new FileStream (streamName, FileMode.Create, FileAccess.Write);
316                 }
317
318                 [MonoTODO]
319                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
320                 {
321                         throw new NotImplementedException ();
322                 }
323                 
324                 public virtual bool PrefetchAll (string configPath, string streamName)
325                 {
326                         throw new NotImplementedException ();
327                 }
328                 
329                 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
330                 {
331                         throw new NotImplementedException ();
332                 }
333
334                 [MonoTODO]
335                 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
336                 {
337                         throw new NotImplementedException ();
338                 }
339
340                 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
341                 {
342                         throw new NotImplementedException ();
343                 }
344                 
345                 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
346                 {
347                         throw new NotImplementedException ();
348                 }
349                 
350                 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
351                 {
352                         if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
353                                 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
354                 }
355                 
356                 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
357                 {
358                 }
359                 
360                 [MonoTODO]
361                 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
362                 {
363                 }
364
365                 public virtual bool SupportsChangeNotifications {
366                         get { return false; }
367                 }
368                 
369                 public virtual bool SupportsLocation {
370                         get { return false; }
371                 }
372                 
373                 public virtual bool SupportsPath {
374                         get { return false; }
375                 }
376                 
377                 public virtual bool SupportsRefresh {
378                         get { return false; }
379                 }
380
381                 [MonoTODO]
382                 public virtual bool IsRemote {
383                         get { return false; }
384                 }
385
386                 [MonoTODO]
387                 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
388                 {
389                         throw new NotImplementedException ();
390                 }
391
392                 [MonoTODO]
393                 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
394                 {
395                         throw new NotImplementedException ();
396                 }
397
398                 [MonoTODO]
399                 public virtual bool IsSecondaryRoot (string configPath)
400                 {
401                         throw new NotImplementedException ();
402                 }
403
404                 [MonoTODO]
405                 public virtual bool IsTrustedConfigPath (string configPath)
406                 {
407                         throw new NotImplementedException ();
408                 }
409         }
410 }
411
412 #endif