WebConfigurationHost.cs 13 KB

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