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