WebConfigurationHost.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. const string MachineWebPath = ":web:";
  42. public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
  43. {
  44. return null;
  45. }
  46. public virtual object CreateDeprecatedConfigContext (string configPath)
  47. {
  48. throw new NotImplementedException ();
  49. }
  50. public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
  51. {
  52. throw new NotImplementedException ();
  53. }
  54. public virtual void DeleteStream (string streamName)
  55. {
  56. File.Delete (streamName);
  57. }
  58. public virtual string EncryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
  59. {
  60. throw new NotImplementedException ();
  61. }
  62. public virtual string GetConfigPathFromLocationSubPath (string configPath, string locatinSubPath)
  63. {
  64. return configPath + "/" + locatinSubPath;
  65. }
  66. public virtual Type GetConfigType (string typeName, bool throwOnError)
  67. {
  68. Type type = Type.GetType (typeName);
  69. if (type == null && throwOnError)
  70. throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
  71. return type;
  72. }
  73. public virtual string GetConfigTypeName (Type t)
  74. {
  75. return t.AssemblyQualifiedName;
  76. }
  77. public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
  78. {
  79. throw new NotImplementedException ();
  80. }
  81. public virtual string GetStreamName (string configPath)
  82. {
  83. if (configPath == MachinePath) {
  84. if (map == null)
  85. return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
  86. else
  87. return map.MachineConfigFilename;
  88. } else if (configPath == MachineWebPath) {
  89. if (map == null) {
  90. string mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
  91. return GetWebConfigFileName (mdir);
  92. }
  93. else
  94. return null;
  95. }
  96. string dir = MapPath (configPath);
  97. return GetWebConfigFileName (dir);
  98. }
  99. public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public virtual object GetStreamVersion (string streamName)
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. public virtual IDisposable Impersonate ()
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
  112. {
  113. }
  114. public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
  115. {
  116. string fullPath = (string) hostInitConfigurationParams [1];
  117. map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
  118. if (locationSubPath == MachineWebPath) {
  119. locationSubPath = MachinePath;
  120. configPath = MachineWebPath;
  121. locationConfigPath = null;
  122. }
  123. else if (locationSubPath == MachinePath) {
  124. locationSubPath = null;
  125. configPath = MachinePath;
  126. locationConfigPath = null;
  127. }
  128. else {
  129. int i;
  130. if (locationSubPath == null) {
  131. configPath = fullPath;
  132. i = fullPath.LastIndexOf ("/");
  133. } else {
  134. configPath = locationSubPath;
  135. if (locationSubPath != "/")
  136. i = locationSubPath.LastIndexOf ('/');
  137. else
  138. i = -1;
  139. }
  140. if (i != -1) {
  141. locationConfigPath = configPath.Substring (i+1);
  142. if (i == 0)
  143. locationSubPath = "/";
  144. else
  145. locationSubPath = fullPath.Substring (0, i);
  146. } else {
  147. locationSubPath = MachineWebPath;
  148. locationConfigPath = null;
  149. }
  150. }
  151. if (GetStreamName (configPath) == null) {
  152. // There is no config file for this path. Get the next one in the chain.
  153. InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath, root, hostInitConfigurationParams);
  154. }
  155. }
  156. public string MapPath (string virtualPath)
  157. {
  158. if (map != null)
  159. return MapPathFromMapper (virtualPath);
  160. else
  161. return HttpContext.Current.Request.MapPath (virtualPath);
  162. }
  163. public string NormalizeVirtualPath (string virtualPath)
  164. {
  165. if (virtualPath == null || virtualPath.Length == 0)
  166. virtualPath = ".";
  167. else
  168. virtualPath = virtualPath.Trim ();
  169. if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
  170. virtualPath = virtualPath.Substring (1);
  171. if (System.IO.Path.DirectorySeparatorChar != '/')
  172. virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
  173. if (UrlUtils.IsRooted (virtualPath)) {
  174. virtualPath = UrlUtils.Canonic (virtualPath);
  175. } else {
  176. if (map.VirtualDirectories.Count > 0) {
  177. string root = map.VirtualDirectories [0].VirtualDirectory;
  178. virtualPath = UrlUtils.Combine (root, virtualPath);
  179. virtualPath = UrlUtils.Canonic (virtualPath);
  180. }
  181. }
  182. return virtualPath;
  183. }
  184. public string MapPathFromMapper (string virtualPath)
  185. {
  186. string path = NormalizeVirtualPath (virtualPath);
  187. foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
  188. if (path.StartsWith (mapping.VirtualDirectory)) {
  189. int i = mapping.VirtualDirectory.Length;
  190. if (path.Length == i) {
  191. return mapping.PhysicalDirectory;
  192. }
  193. else if (path [i] == '/') {
  194. string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
  195. return Path.Combine (mapping.PhysicalDirectory, pathPart);
  196. }
  197. }
  198. }
  199. throw new HttpException ("Invalid virtual directory: " + virtualPath);
  200. }
  201. string GetWebConfigFileName (string dir)
  202. {
  203. string file = Path.Combine (dir, "Web.config");
  204. if (File.Exists (file))
  205. return file;
  206. file = Path.Combine (dir, "web.config");
  207. if (File.Exists (file))
  208. return file;
  209. return null;
  210. }
  211. public virtual bool IsAboveApplication (string configPath)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. public virtual bool IsConfigRecordRequired (string configPath)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
  220. {
  221. switch (allowDefinition) {
  222. case ConfigurationAllowDefinition.MachineOnly:
  223. return configPath == MachinePath || configPath == MachineWebPath;
  224. case ConfigurationAllowDefinition.MachineToWebRoot:
  225. case ConfigurationAllowDefinition.MachineToApplication:
  226. return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
  227. default:
  228. return true;
  229. }
  230. }
  231. public virtual bool IsFile (string streamName)
  232. {
  233. throw new NotImplementedException ();
  234. }
  235. public virtual bool IsLocationApplicable (string configPath)
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. public virtual Stream OpenStreamForRead (string streamName)
  240. {
  241. if (!File.Exists (streamName))
  242. throw new ConfigurationException ("File '" + streamName + "' not found");
  243. return new FileStream (streamName, FileMode.Open, FileAccess.Read);
  244. }
  245. [MonoTODO]
  246. public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
  251. {
  252. return new FileStream (streamName, FileMode.Create, FileAccess.Write);
  253. }
  254. [MonoTODO]
  255. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
  256. {
  257. throw new NotImplementedException ();
  258. }
  259. public virtual bool PrefetchAll (string configPath, string streamName)
  260. {
  261. throw new NotImplementedException ();
  262. }
  263. public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
  264. {
  265. throw new NotImplementedException ();
  266. }
  267. [MonoTODO]
  268. public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
  269. {
  270. throw new NotImplementedException ();
  271. }
  272. public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  273. {
  274. throw new NotImplementedException ();
  275. }
  276. public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  277. {
  278. throw new NotImplementedException ();
  279. }
  280. public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
  281. {
  282. if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
  283. throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
  284. }
  285. public virtual void WriteCompleted (string streamName, bool success, object writeContext)
  286. {
  287. }
  288. [MonoTODO]
  289. public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
  290. {
  291. }
  292. public virtual bool SupportsChangeNotifications {
  293. get { return false; }
  294. }
  295. public virtual bool SupportsLocation {
  296. get { return false; }
  297. }
  298. public virtual bool SupportsPath {
  299. get { return false; }
  300. }
  301. public virtual bool SupportsRefresh {
  302. get { return false; }
  303. }
  304. [MonoTODO]
  305. public virtual bool IsRemote {
  306. get { return false; }
  307. }
  308. [MonoTODO]
  309. public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
  310. {
  311. throw new NotImplementedException ();
  312. }
  313. [MonoTODO]
  314. public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
  315. {
  316. throw new NotImplementedException ();
  317. }
  318. [MonoTODO]
  319. public virtual bool IsSecondaryRoot (string configPath)
  320. {
  321. throw new NotImplementedException ();
  322. }
  323. [MonoTODO]
  324. public virtual bool IsTrustedConfigPath (string configPath)
  325. {
  326. throw new NotImplementedException ();
  327. }
  328. }
  329. }
  330. #endif