WebConfigurationHost.cs 12 KB

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