WebConfigurationHost.cs 14 KB

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