WebConfigurationHost.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. [MonoTODO ("this should consult with the build provider machinery")]
  81. public virtual Type GetConfigType (string typeName, bool throwOnError)
  82. {
  83. Type type = Type.GetType (typeName);
  84. if (type == null && throwOnError)
  85. throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
  86. return type;
  87. }
  88. public virtual string GetConfigTypeName (Type t)
  89. {
  90. return t.AssemblyQualifiedName;
  91. }
  92. public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
  93. {
  94. throw new NotImplementedException ();
  95. }
  96. public virtual string GetStreamName (string configPath)
  97. {
  98. if (configPath == MachinePath) {
  99. if (map == null)
  100. return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
  101. else
  102. return map.MachineConfigFilename;
  103. } else if (configPath == MachineWebPath) {
  104. string mdir;
  105. if (map == null)
  106. mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
  107. else
  108. mdir = Path.GetDirectoryName (map.MachineConfigFilename);
  109. return GetWebConfigFileName (mdir);
  110. }
  111. string dir = MapPath (configPath);
  112. return GetWebConfigFileName (dir);
  113. }
  114. public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
  115. {
  116. throw new NotImplementedException ();
  117. }
  118. public virtual object GetStreamVersion (string streamName)
  119. {
  120. throw new NotImplementedException ();
  121. }
  122. public virtual IDisposable Impersonate ()
  123. {
  124. throw new NotImplementedException ();
  125. }
  126. public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
  127. {
  128. }
  129. public virtual void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
  130. {
  131. string fullPath = (string) hostInitConfigurationParams [1];
  132. map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
  133. if (locationSubPath == MachineWebPath) {
  134. locationSubPath = MachinePath;
  135. configPath = MachineWebPath;
  136. locationConfigPath = null;
  137. }
  138. else if (locationSubPath == MachinePath) {
  139. locationSubPath = null;
  140. configPath = MachinePath;
  141. locationConfigPath = null;
  142. }
  143. else {
  144. int i;
  145. if (locationSubPath == null)
  146. configPath = fullPath;
  147. else
  148. configPath = locationSubPath;
  149. if (configPath == HttpRuntime.AppDomainAppVirtualPath
  150. || configPath == "/")
  151. i = -1;
  152. else
  153. i = configPath.LastIndexOf ("/");
  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 if (HttpContext.Current != null
  175. && HttpContext.Current.Request != null)
  176. return HttpContext.Current.Request.MapPath (virtualPath);
  177. else if (HttpRuntime.AppDomainAppVirtualPath != null && virtualPath.StartsWith (HttpRuntime.AppDomainAppVirtualPath)) {
  178. if (virtualPath == HttpRuntime.AppDomainAppVirtualPath)
  179. return HttpRuntime.AppDomainAppPath;
  180. return UrlUtils.Combine (HttpRuntime.AppDomainAppPath, virtualPath.Substring (HttpRuntime.AppDomainAppVirtualPath.Length));
  181. }
  182. else
  183. return virtualPath;
  184. }
  185. public string NormalizeVirtualPath (string virtualPath)
  186. {
  187. if (virtualPath == null || virtualPath.Length == 0)
  188. virtualPath = ".";
  189. else
  190. virtualPath = virtualPath.Trim ();
  191. if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
  192. virtualPath = virtualPath.Substring (1);
  193. if (System.IO.Path.DirectorySeparatorChar != '/')
  194. virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
  195. if (UrlUtils.IsRooted (virtualPath)) {
  196. virtualPath = UrlUtils.Canonic (virtualPath);
  197. } else {
  198. if (map.VirtualDirectories.Count > 0) {
  199. string root = map.VirtualDirectories [0].VirtualDirectory;
  200. virtualPath = UrlUtils.Combine (root, virtualPath);
  201. virtualPath = UrlUtils.Canonic (virtualPath);
  202. }
  203. }
  204. return virtualPath;
  205. }
  206. public string MapPathFromMapper (string virtualPath)
  207. {
  208. string path = NormalizeVirtualPath (virtualPath);
  209. foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
  210. if (path.StartsWith (mapping.VirtualDirectory)) {
  211. int i = mapping.VirtualDirectory.Length;
  212. if (path.Length == i) {
  213. return mapping.PhysicalDirectory;
  214. }
  215. else if (path [i] == '/') {
  216. string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
  217. return Path.Combine (mapping.PhysicalDirectory, pathPart);
  218. }
  219. }
  220. }
  221. throw new HttpException ("Invalid virtual directory: " + virtualPath);
  222. }
  223. string GetWebConfigFileName (string dir)
  224. {
  225. string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
  226. foreach (string fn in filenames) {
  227. string file = Path.Combine (dir, fn);
  228. if (File.Exists (file))
  229. return file;
  230. }
  231. return null;
  232. }
  233. public virtual bool IsAboveApplication (string configPath)
  234. {
  235. throw new NotImplementedException ();
  236. }
  237. public virtual bool IsConfigRecordRequired (string configPath)
  238. {
  239. throw new NotImplementedException ();
  240. }
  241. public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
  242. {
  243. switch (allowDefinition) {
  244. case ConfigurationAllowDefinition.MachineOnly:
  245. return configPath == MachinePath || configPath == MachineWebPath;
  246. case ConfigurationAllowDefinition.MachineToWebRoot:
  247. case ConfigurationAllowDefinition.MachineToApplication:
  248. return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
  249. default:
  250. return true;
  251. }
  252. }
  253. public virtual bool IsFile (string streamName)
  254. {
  255. throw new NotImplementedException ();
  256. }
  257. public virtual bool IsLocationApplicable (string configPath)
  258. {
  259. throw new NotImplementedException ();
  260. }
  261. public virtual Stream OpenStreamForRead (string streamName)
  262. {
  263. if (!File.Exists (streamName))
  264. throw new ConfigurationException ("File '" + streamName + "' not found");
  265. return new FileStream (streamName, FileMode.Open, FileAccess.Read);
  266. }
  267. [MonoTODO]
  268. public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
  269. {
  270. throw new NotImplementedException ();
  271. }
  272. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
  273. {
  274. return new FileStream (streamName, FileMode.Create, FileAccess.Write);
  275. }
  276. [MonoTODO]
  277. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
  278. {
  279. throw new NotImplementedException ();
  280. }
  281. public virtual bool PrefetchAll (string configPath, string streamName)
  282. {
  283. throw new NotImplementedException ();
  284. }
  285. public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. [MonoTODO]
  290. public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
  291. {
  292. throw new NotImplementedException ();
  293. }
  294. public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  295. {
  296. throw new NotImplementedException ();
  297. }
  298. public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  299. {
  300. throw new NotImplementedException ();
  301. }
  302. public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
  303. {
  304. if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
  305. throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
  306. }
  307. public virtual void WriteCompleted (string streamName, bool success, object writeContext)
  308. {
  309. }
  310. [MonoTODO]
  311. public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
  312. {
  313. }
  314. public virtual bool SupportsChangeNotifications {
  315. get { return false; }
  316. }
  317. public virtual bool SupportsLocation {
  318. get { return false; }
  319. }
  320. public virtual bool SupportsPath {
  321. get { return false; }
  322. }
  323. public virtual bool SupportsRefresh {
  324. get { return false; }
  325. }
  326. [MonoTODO]
  327. public virtual bool IsRemote {
  328. get { return false; }
  329. }
  330. [MonoTODO]
  331. public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. [MonoTODO]
  336. public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
  337. {
  338. throw new NotImplementedException ();
  339. }
  340. [MonoTODO]
  341. public virtual bool IsSecondaryRoot (string configPath)
  342. {
  343. throw new NotImplementedException ();
  344. }
  345. [MonoTODO]
  346. public virtual bool IsTrustedConfigPath (string configPath)
  347. {
  348. throw new NotImplementedException ();
  349. }
  350. }
  351. }
  352. #endif