WebConfigurationHost.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. i = -1;
  151. else
  152. i = configPath.LastIndexOf ("/");
  153. if (i != -1) {
  154. locationConfigPath = configPath.Substring (i+1);
  155. if (i == 0)
  156. locationSubPath = "/";
  157. else
  158. locationSubPath = fullPath.Substring (0, i);
  159. } else {
  160. locationSubPath = MachineWebPath;
  161. locationConfigPath = null;
  162. }
  163. }
  164. if (GetStreamName (configPath) == null) {
  165. // There is no config file for this path. Get the next one in the chain.
  166. InitForConfiguration (ref locationSubPath, out configPath, out locationConfigPath, root, hostInitConfigurationParams);
  167. }
  168. }
  169. public string MapPath (string virtualPath)
  170. {
  171. if (map != null)
  172. return MapPathFromMapper (virtualPath);
  173. else if (HttpContext.Current != null
  174. && HttpContext.Current.Request != null)
  175. return HttpContext.Current.Request.MapPath (virtualPath);
  176. else if (virtualPath.StartsWith (HttpRuntime.AppDomainAppVirtualPath)) {
  177. if (virtualPath == HttpRuntime.AppDomainAppVirtualPath)
  178. return HttpRuntime.AppDomainAppPath;
  179. return UrlUtils.Combine (HttpRuntime.AppDomainAppPath, virtualPath.Substring (HttpRuntime.AppDomainAppVirtualPath.Length));
  180. }
  181. else
  182. return virtualPath;
  183. }
  184. public string NormalizeVirtualPath (string virtualPath)
  185. {
  186. if (virtualPath == null || virtualPath.Length == 0)
  187. virtualPath = ".";
  188. else
  189. virtualPath = virtualPath.Trim ();
  190. if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
  191. virtualPath = virtualPath.Substring (1);
  192. if (System.IO.Path.DirectorySeparatorChar != '/')
  193. virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
  194. if (UrlUtils.IsRooted (virtualPath)) {
  195. virtualPath = UrlUtils.Canonic (virtualPath);
  196. } else {
  197. if (map.VirtualDirectories.Count > 0) {
  198. string root = map.VirtualDirectories [0].VirtualDirectory;
  199. virtualPath = UrlUtils.Combine (root, virtualPath);
  200. virtualPath = UrlUtils.Canonic (virtualPath);
  201. }
  202. }
  203. return virtualPath;
  204. }
  205. public string MapPathFromMapper (string virtualPath)
  206. {
  207. string path = NormalizeVirtualPath (virtualPath);
  208. foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
  209. if (path.StartsWith (mapping.VirtualDirectory)) {
  210. int i = mapping.VirtualDirectory.Length;
  211. if (path.Length == i) {
  212. return mapping.PhysicalDirectory;
  213. }
  214. else if (path [i] == '/') {
  215. string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
  216. return Path.Combine (mapping.PhysicalDirectory, pathPart);
  217. }
  218. }
  219. }
  220. throw new HttpException ("Invalid virtual directory: " + virtualPath);
  221. }
  222. string GetWebConfigFileName (string dir)
  223. {
  224. string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
  225. foreach (string fn in filenames) {
  226. string file = Path.Combine (dir, fn);
  227. if (File.Exists (file))
  228. return file;
  229. }
  230. return null;
  231. }
  232. public virtual bool IsAboveApplication (string configPath)
  233. {
  234. throw new NotImplementedException ();
  235. }
  236. public virtual bool IsConfigRecordRequired (string configPath)
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
  241. {
  242. switch (allowDefinition) {
  243. case ConfigurationAllowDefinition.MachineOnly:
  244. return configPath == MachinePath || configPath == MachineWebPath;
  245. case ConfigurationAllowDefinition.MachineToWebRoot:
  246. case ConfigurationAllowDefinition.MachineToApplication:
  247. return configPath == MachinePath || configPath == MachineWebPath || configPath == "/";
  248. default:
  249. return true;
  250. }
  251. }
  252. public virtual bool IsFile (string streamName)
  253. {
  254. throw new NotImplementedException ();
  255. }
  256. public virtual bool IsLocationApplicable (string configPath)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. public virtual Stream OpenStreamForRead (string streamName)
  261. {
  262. if (!File.Exists (streamName))
  263. throw new ConfigurationException ("File '" + streamName + "' not found");
  264. return new FileStream (streamName, FileMode.Open, FileAccess.Read);
  265. }
  266. [MonoTODO]
  267. public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
  272. {
  273. return new FileStream (streamName, FileMode.Create, FileAccess.Write);
  274. }
  275. [MonoTODO]
  276. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
  277. {
  278. throw new NotImplementedException ();
  279. }
  280. public virtual bool PrefetchAll (string configPath, string streamName)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
  285. {
  286. throw new NotImplementedException ();
  287. }
  288. [MonoTODO]
  289. public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  298. {
  299. throw new NotImplementedException ();
  300. }
  301. public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
  302. {
  303. if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
  304. throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
  305. }
  306. public virtual void WriteCompleted (string streamName, bool success, object writeContext)
  307. {
  308. }
  309. [MonoTODO]
  310. public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
  311. {
  312. }
  313. public virtual bool SupportsChangeNotifications {
  314. get { return false; }
  315. }
  316. public virtual bool SupportsLocation {
  317. get { return false; }
  318. }
  319. public virtual bool SupportsPath {
  320. get { return false; }
  321. }
  322. public virtual bool SupportsRefresh {
  323. get { return false; }
  324. }
  325. [MonoTODO]
  326. public virtual bool IsRemote {
  327. get { return false; }
  328. }
  329. [MonoTODO]
  330. public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
  331. {
  332. throw new NotImplementedException ();
  333. }
  334. [MonoTODO]
  335. public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
  336. {
  337. throw new NotImplementedException ();
  338. }
  339. [MonoTODO]
  340. public virtual bool IsSecondaryRoot (string configPath)
  341. {
  342. throw new NotImplementedException ();
  343. }
  344. [MonoTODO]
  345. public virtual bool IsTrustedConfigPath (string configPath)
  346. {
  347. throw new NotImplementedException ();
  348. }
  349. }
  350. }
  351. #endif