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;
38 namespace System.Web.Configuration
40 class WebConfigurationHost: IInternalConfigHost
42 WebConfigurationFileMap map;
43 const string MachinePath = ":machine:";
44 const string MachineWebPath = ":web:";
46 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
51 public virtual object CreateDeprecatedConfigContext (string configPath)
53 throw new NotImplementedException ();
56 public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
58 throw new NotImplementedException ();
61 public virtual void DeleteStream (string streamName)
63 File.Delete (streamName);
66 public virtual string EncryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
68 throw new NotImplementedException ();
71 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locatinSubPath)
73 return configPath + "/" + locatinSubPath;
76 public virtual Type GetConfigType (string typeName, bool throwOnError)
78 Type type = Type.GetType (typeName);
79 if (type == null && throwOnError)
80 throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
84 public virtual string GetConfigTypeName (Type t)
86 return t.AssemblyQualifiedName;
89 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
91 throw new NotImplementedException ();
94 public virtual string GetStreamName (string configPath)
96 if (configPath == MachinePath) {
98 return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
100 return map.MachineConfigFilename;
101 } else if (configPath == MachineWebPath) {
103 string mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
104 return GetWebConfigFileName (mdir);
110 string dir = MapPath (configPath);
111 return GetWebConfigFileName (dir);
114 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
116 throw new NotImplementedException ();
119 public virtual object GetStreamVersion (string streamName)
121 throw new NotImplementedException ();
124 public virtual IDisposable Impersonate ()
126 throw new NotImplementedException ();
129 public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
133 public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
135 string fullPath = (string) hostInitConfigurationParams [1];
137 map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
139 if (locationSubPath == MachineWebPath) {
140 locationSubPath = MachinePath;
141 configPath = MachineWebPath;
142 locationConfigPath = null;
144 else if (locationSubPath == MachinePath) {
145 locationSubPath = null;
146 configPath = MachinePath;
147 locationConfigPath = null;
152 if (locationSubPath == null) {
153 configPath = fullPath;
154 i = fullPath.LastIndexOf ("/");
156 configPath = locationSubPath;
157 if (locationSubPath != "/")
158 i = locationSubPath.LastIndexOf ('/');
164 locationConfigPath = configPath.Substring (i+1);
167 locationSubPath = "/";
169 locationSubPath = fullPath.Substring (0, i);
171 locationSubPath = MachineWebPath;
172 locationConfigPath = null;
176 if (GetStreamName (configPath) == null) {
177 // There is no config file for this path. Get the next one in the chain.
178 InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath, root, hostInitConfigurationParams);
182 public string MapPath (string virtualPath)
185 return MapPathFromMapper (virtualPath);
187 return HttpContext.Current.Request.MapPath (virtualPath);
190 public string NormalizeVirtualPath (string virtualPath)
192 if (virtualPath == null || virtualPath.Length == 0)
195 virtualPath = virtualPath.Trim ();
197 if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
198 virtualPath = virtualPath.Substring (1);
200 if (System.IO.Path.DirectorySeparatorChar != '/')
201 virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
203 if (UrlUtils.IsRooted (virtualPath)) {
204 virtualPath = UrlUtils.Canonic (virtualPath);
206 if (map.VirtualDirectories.Count > 0) {
207 string root = map.VirtualDirectories [0].VirtualDirectory;
208 virtualPath = UrlUtils.Combine (root, virtualPath);
209 virtualPath = UrlUtils.Canonic (virtualPath);
215 public string MapPathFromMapper (string virtualPath)
217 string path = NormalizeVirtualPath (virtualPath);
219 foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
220 if (path.StartsWith (mapping.VirtualDirectory)) {
221 int i = mapping.VirtualDirectory.Length;
222 if (path.Length == i) {
223 return mapping.PhysicalDirectory;
225 else if (path [i] == '/') {
226 string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
227 return Path.Combine (mapping.PhysicalDirectory, pathPart);
231 throw new HttpException ("Invalid virtual directory: " + virtualPath);
234 string GetWebConfigFileName (string dir)
236 string file = Path.Combine (dir, "Web.config");
237 if (File.Exists (file))
239 file = Path.Combine (dir, "web.config");
240 if (File.Exists (file))
245 public virtual bool IsAboveApplication (string configPath)
247 throw new NotImplementedException ();
250 public virtual bool IsConfigRecordRequired (string configPath)
252 throw new NotImplementedException ();
255 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
257 switch (allowDefinition) {
258 case ConfigurationAllowDefinition.MachineOnly:
259 return configPath == MachinePath || configPath == MachineWebPath;
260 case ConfigurationAllowDefinition.MachineToWebRoot:
261 case ConfigurationAllowDefinition.MachineToApplication:
262 return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
268 public virtual bool IsFile (string streamName)
270 throw new NotImplementedException ();
273 public virtual bool IsLocationApplicable (string configPath)
275 throw new NotImplementedException ();
278 public virtual Stream OpenStreamForRead (string streamName)
280 if (!File.Exists (streamName))
281 throw new ConfigurationException ("File '" + streamName + "' not found");
283 return new FileStream (streamName, FileMode.Open, FileAccess.Read);
287 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
289 throw new NotImplementedException ();
292 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
294 return new FileStream (streamName, FileMode.Create, FileAccess.Write);
298 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
300 throw new NotImplementedException ();
303 public virtual bool PrefetchAll (string configPath, string streamName)
305 throw new NotImplementedException ();
308 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
310 throw new NotImplementedException ();
314 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
316 throw new NotImplementedException ();
319 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
321 throw new NotImplementedException ();
324 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
326 throw new NotImplementedException ();
329 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
331 if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
332 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
335 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
340 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
344 public virtual bool SupportsChangeNotifications {
345 get { return false; }
348 public virtual bool SupportsLocation {
349 get { return false; }
352 public virtual bool SupportsPath {
353 get { return false; }
356 public virtual bool SupportsRefresh {
357 get { return false; }
361 public virtual bool IsRemote {
362 get { return false; }
366 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
368 throw new NotImplementedException ();
372 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
374 throw new NotImplementedException ();
378 public virtual bool IsSecondaryRoot (string configPath)
380 throw new NotImplementedException ();
384 public virtual bool IsTrustedConfigPath (string configPath)
386 throw new NotImplementedException ();