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