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