WebConfigurationHost.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // System.Web.Configuration.WebConfigurationHost.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  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. #if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Security;
  32. using System.Configuration;
  33. using System.Configuration.Internal;
  34. using System.Web.Util;
  35. namespace System.Web.Configuration
  36. {
  37. class WebConfigurationHost: IInternalConfigHost
  38. {
  39. WebConfigurationFileMap map;
  40. const string MachinePath = "machine:";
  41. public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
  42. {
  43. return null;
  44. }
  45. public virtual object CreateDeprecatedConfigContext (string configPath)
  46. {
  47. throw new NotImplementedException ();
  48. }
  49. public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. public virtual void DeleteStream (string streamName)
  54. {
  55. File.Delete (streamName);
  56. }
  57. public virtual string EncryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
  58. {
  59. throw new NotImplementedException ();
  60. }
  61. public virtual string GetConfigPathFromLocationSubPath (string configPath, string locatinSubPath)
  62. {
  63. return configPath + "/" + locatinSubPath;
  64. }
  65. public virtual Type GetConfigType (string typeName, bool throwOnError)
  66. {
  67. Type type = Type.GetType (typeName);
  68. if (type == null && throwOnError)
  69. throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
  70. return type;
  71. }
  72. public virtual string GetConfigTypeName (Type t)
  73. {
  74. return t.AssemblyQualifiedName;
  75. }
  76. public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
  77. {
  78. throw new NotImplementedException ();
  79. }
  80. public virtual string GetStreamName (string configPath)
  81. {
  82. if (configPath == MachinePath) {
  83. if (map == null)
  84. return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
  85. else
  86. return map.MachineConfigFilename;
  87. }
  88. string dir = MapPath (configPath);
  89. return GetWebConfigFileName (dir);
  90. }
  91. public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. public virtual object GetStreamVersion (string streamName)
  96. {
  97. throw new NotImplementedException ();
  98. }
  99. public virtual IDisposable Impersonate ()
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
  104. {
  105. }
  106. public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
  107. {
  108. string fullPath = (string) hostInitConfigurationParams [1];
  109. map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
  110. if (locationSubPath == MachinePath) {
  111. locationSubPath = null;
  112. configPath = MachinePath;
  113. locationConfigPath = null;
  114. return;
  115. }
  116. int i;
  117. if (locationSubPath == null) {
  118. configPath = fullPath;
  119. i = fullPath.LastIndexOf ("/");
  120. } else {
  121. configPath = locationSubPath;
  122. if (locationSubPath != "/")
  123. i = locationSubPath.LastIndexOf ('/');
  124. else
  125. i = -1;
  126. }
  127. if (i != -1) {
  128. locationConfigPath = configPath.Substring (i+1);
  129. if (i == 0)
  130. locationSubPath = "/";
  131. else
  132. locationSubPath = fullPath.Substring (0, i);
  133. string dir = MapPath (configPath);
  134. if (GetWebConfigFileName (dir) == null) {
  135. InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath, root, hostInitConfigurationParams);
  136. }
  137. } else {
  138. locationSubPath = MachinePath;
  139. locationConfigPath = null;
  140. return;
  141. }
  142. }
  143. public string MapPath (string virtualPath)
  144. {
  145. if (map != null)
  146. return MapPathFromMapper (virtualPath);
  147. else
  148. return HttpContext.Current.Request.MapPath (virtualPath);
  149. }
  150. public string NormalizeVirtualPath (string virtualPath)
  151. {
  152. if (virtualPath == null || virtualPath.Length == 0)
  153. virtualPath = ".";
  154. else
  155. virtualPath = virtualPath.Trim ();
  156. if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
  157. virtualPath = virtualPath.Substring (1);
  158. if (System.IO.Path.DirectorySeparatorChar != '/')
  159. virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
  160. if (UrlUtils.IsRooted (virtualPath)) {
  161. virtualPath = UrlUtils.Reduce (virtualPath);
  162. } else {
  163. if (map.VirtualDirectories.Count > 0) {
  164. string root = map.VirtualDirectories [0].VirtualDirectory;
  165. virtualPath = UrlUtils.Combine (root, virtualPath);
  166. virtualPath = UrlUtils.Reduce (virtualPath);
  167. }
  168. }
  169. return virtualPath;
  170. }
  171. public string MapPathFromMapper (string virtualPath)
  172. {
  173. string path = NormalizeVirtualPath (virtualPath);
  174. foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
  175. if (path.StartsWith (mapping.VirtualDirectory)) {
  176. int i = mapping.VirtualDirectory.Length;
  177. if (path.Length == i) {
  178. return mapping.PhysicalDirectory;
  179. }
  180. else if (path [i] == '/') {
  181. string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
  182. return Path.Combine (mapping.PhysicalDirectory, pathPart);
  183. }
  184. }
  185. }
  186. throw new HttpException ("Invalid virtual directory: " + virtualPath);
  187. }
  188. string GetWebConfigFileName (string dir)
  189. {
  190. string file = Path.Combine (dir, "Web.config");
  191. if (File.Exists (file))
  192. return file;
  193. file = Path.Combine (dir, "web.config");
  194. if (File.Exists (file))
  195. return file;
  196. return null;
  197. }
  198. public virtual bool IsAboveApplication (string configPath)
  199. {
  200. throw new NotImplementedException ();
  201. }
  202. public virtual bool IsConfigRecordRequired (string configPath)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
  207. {
  208. throw new NotImplementedException ();
  209. }
  210. public virtual bool IsFile (string streamName)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. public virtual bool IsLocationApplicable (string configPath)
  215. {
  216. throw new NotImplementedException ();
  217. }
  218. public virtual Stream OpenStreamForRead (string streamName)
  219. {
  220. if (!File.Exists (streamName))
  221. throw new ConfigurationException ("File '" + streamName + "' not found");
  222. return new FileStream (streamName, FileMode.Open, FileAccess.Read);
  223. }
  224. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
  225. {
  226. return new FileStream (streamName, FileMode.Create, FileAccess.Write);
  227. }
  228. public virtual bool PrefetchAll (string configPath, string streamName)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
  233. {
  234. throw new NotImplementedException ();
  235. }
  236. public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
  245. {
  246. throw new NotImplementedException ();
  247. }
  248. public virtual void WriteCompleted (string streamName, bool success, object writeContext)
  249. {
  250. }
  251. public virtual bool SupportsChangeNotifications {
  252. get { return false; }
  253. }
  254. public virtual bool SupportsLocation {
  255. get { return false; }
  256. }
  257. public virtual bool SupportsPath {
  258. get { return false; }
  259. }
  260. public virtual bool SupportsRefresh {
  261. get { return false; }
  262. }
  263. }
  264. }
  265. #endif