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