2 // System.Web.Configuration.WebConfigurationHost.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
33 using System.Security;
34 using System.Configuration;
35 using System.Configuration.Internal;
36 using System.Web.Util;
37 using System.Reflection;
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..
45 * for now, though, just implement it as we do
46 * System.Configuration.InternalConfigurationHost, i.e. the local
49 namespace System.Web.Configuration
51 class WebConfigurationHost: IInternalConfigHost
53 WebConfigurationFileMap map;
54 const string MachinePath = ":machine:";
55 const string MachineWebPath = ":web:";
57 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
59 return new WebContext (WebApplicationLevel.AtApplication /* XXX */,
61 "" /* application path XXX */,
66 public virtual object CreateDeprecatedConfigContext (string configPath)
68 throw new NotImplementedException ();
71 public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
73 throw new NotImplementedException ();
76 public virtual void DeleteStream (string streamName)
78 File.Delete (streamName);
81 public virtual string EncryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
83 throw new NotImplementedException ();
86 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locatinSubPath)
88 return configPath + "/" + locatinSubPath;
91 private static string privateBinPath;
93 private static string PrivateBinPath {
95 if (privateBinPath != null)
96 return privateBinPath;
98 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
99 privateBinPath = Path.Combine(setup.ApplicationBase, setup.PrivateBinPath);
100 return privateBinPath;
104 private Type LoadType(string typeName)
106 Type type = Type.GetType (typeName);
110 if (!Directory.Exists (PrivateBinPath))
113 string[] binDlls = Directory.GetFiles(PrivateBinPath, "*.dll");
114 foreach (string s in binDlls) {
115 Assembly binA = Assembly.LoadFrom (s);
116 type = binA.GetType (typeName);
126 public virtual Type GetConfigType (string typeName, bool throwOnError)
128 Type type = LoadType(typeName);
129 if (type == null && throwOnError)
130 throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
134 public virtual string GetConfigTypeName (Type t)
136 return t.AssemblyQualifiedName;
139 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
141 throw new NotImplementedException ();
144 public virtual string GetStreamName (string configPath)
146 if (configPath == MachinePath) {
148 return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
150 return map.MachineConfigFilename;
151 } else if (configPath == MachineWebPath) {
156 return "/web.config";
158 mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
161 mdir = Path.GetDirectoryName (map.MachineConfigFilename);
163 return GetWebConfigFileName (mdir);
166 string dir = MapPath (configPath);
167 return GetWebConfigFileName (dir);
170 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
172 throw new NotImplementedException ();
175 public virtual object GetStreamVersion (string streamName)
177 throw new NotImplementedException ();
180 public virtual IDisposable Impersonate ()
182 throw new NotImplementedException ();
185 public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
189 public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
191 string fullPath = (string) hostInitConfigurationParams [1];
193 map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
195 if (locationSubPath == MachineWebPath) {
196 locationSubPath = MachinePath;
197 configPath = MachineWebPath;
198 locationConfigPath = null;
200 else if (locationSubPath == MachinePath) {
201 locationSubPath = null;
202 configPath = MachinePath;
203 locationConfigPath = null;
208 if (locationSubPath == null)
209 configPath = fullPath;
211 configPath = locationSubPath;
213 if (configPath == HttpRuntime.AppDomainAppVirtualPath
214 || configPath == "/")
217 i = configPath.LastIndexOf ("/");
220 locationConfigPath = configPath.Substring (i+1);
223 locationSubPath = "/";
225 locationSubPath = fullPath.Substring (0, i);
227 locationSubPath = MachineWebPath;
228 locationConfigPath = null;
233 public string MapPath (string virtualPath)
236 return MapPathFromMapper (virtualPath);
237 else if (HttpContext.Current != null
238 && HttpContext.Current.Request != null)
239 return HttpContext.Current.Request.MapPath (virtualPath);
240 else if (HttpRuntime.AppDomainAppVirtualPath != null && virtualPath.StartsWith (HttpRuntime.AppDomainAppVirtualPath)) {
241 if (virtualPath == HttpRuntime.AppDomainAppVirtualPath)
242 return HttpRuntime.AppDomainAppPath;
243 return UrlUtils.Combine (HttpRuntime.AppDomainAppPath, virtualPath.Substring (HttpRuntime.AppDomainAppVirtualPath.Length));
249 public string NormalizeVirtualPath (string virtualPath)
251 if (virtualPath == null || virtualPath.Length == 0)
254 virtualPath = virtualPath.Trim ();
256 if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
257 virtualPath = virtualPath.Substring (1);
259 if (System.IO.Path.DirectorySeparatorChar != '/')
260 virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
262 if (UrlUtils.IsRooted (virtualPath)) {
263 virtualPath = UrlUtils.Canonic (virtualPath);
265 if (map.VirtualDirectories.Count > 0) {
266 string root = map.VirtualDirectories [0].VirtualDirectory;
267 virtualPath = UrlUtils.Combine (root, virtualPath);
268 virtualPath = UrlUtils.Canonic (virtualPath);
274 public string MapPathFromMapper (string virtualPath)
276 string path = NormalizeVirtualPath (virtualPath);
278 foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
279 if (path.StartsWith (mapping.VirtualDirectory)) {
280 int i = mapping.VirtualDirectory.Length;
281 if (path.Length == i) {
282 return mapping.PhysicalDirectory;
284 else if (path [i] == '/') {
285 string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
286 return Path.Combine (mapping.PhysicalDirectory, pathPart);
290 throw new HttpException ("Invalid virtual directory: " + virtualPath);
293 string GetWebConfigFileName (string dir)
295 string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
297 foreach (string fn in filenames) {
298 string file = Path.Combine (dir, fn);
299 if (File.Exists (file))
306 public virtual bool IsAboveApplication (string configPath)
308 throw new NotImplementedException ();
311 public virtual bool IsConfigRecordRequired (string configPath)
313 throw new NotImplementedException ();
316 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
318 switch (allowDefinition) {
319 case ConfigurationAllowDefinition.MachineOnly:
320 return configPath == MachinePath || configPath == MachineWebPath;
321 case ConfigurationAllowDefinition.MachineToWebRoot:
322 case ConfigurationAllowDefinition.MachineToApplication:
323 return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
329 public virtual bool IsFile (string streamName)
331 throw new NotImplementedException ();
334 public virtual bool IsLocationApplicable (string configPath)
336 throw new NotImplementedException ();
339 public virtual Stream OpenStreamForRead (string streamName)
341 if (!File.Exists (streamName)) {
343 if (streamName != null && (streamName.EndsWith ("machine.config") || streamName.EndsWith ("web.config"))) {
344 if (streamName.StartsWith ("/"))
345 streamName = streamName.Substring (1);
346 java.lang.ClassLoader cl = (java.lang.ClassLoader) AppDomain.CurrentDomain.GetData ("GH_ContextClassLoader");
348 java.io.InputStream inputStream = cl.getResourceAsStream (streamName);
349 return (Stream) vmw.common.IOUtils.getStream (inputStream);
353 throw new ConfigurationException ("File '" + streamName + "' not found");
356 return new FileStream (streamName, FileMode.Open, FileAccess.Read);
359 [MonoTODO ("Not implemented")]
360 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
362 throw new NotImplementedException ();
365 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
367 return new FileStream (streamName, FileMode.Create, FileAccess.Write);
370 [MonoTODO ("Not implemented")]
371 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
373 throw new NotImplementedException ();
376 public virtual bool PrefetchAll (string configPath, string streamName)
378 throw new NotImplementedException ();
381 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
383 throw new NotImplementedException ();
386 [MonoTODO ("Not implemented")]
387 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
389 throw new NotImplementedException ();
392 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
394 throw new NotImplementedException ();
397 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
399 throw new NotImplementedException ();
402 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
404 if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
405 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
408 [MonoTODO("Does nothing")]
409 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
413 [MonoTODO("Does nothing")]
414 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
418 public virtual bool SupportsChangeNotifications {
419 get { return false; }
422 public virtual bool SupportsLocation {
423 get { return false; }
426 public virtual bool SupportsPath {
427 get { return false; }
430 public virtual bool SupportsRefresh {
431 get { return false; }
434 [MonoTODO("Always returns false")]
435 public virtual bool IsRemote {
436 get { return false; }
439 [MonoTODO ("Not implemented")]
440 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
442 throw new NotImplementedException ();
445 [MonoTODO ("Not implemented")]
446 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
448 throw new NotImplementedException ();
451 [MonoTODO ("Not implemented")]
452 public virtual bool IsSecondaryRoot (string configPath)
454 throw new NotImplementedException ();
457 [MonoTODO ("Not implemented")]
458 public virtual bool IsTrustedConfigPath (string configPath)
460 throw new NotImplementedException ();