WebConfigurationHost.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "/META-INF/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. try {
  146. string dir = MapPath (configPath);
  147. return GetWebConfigFileName (dir);
  148. } catch (Exception ex) {
  149. throw new HttpException (400, "Bad Request");
  150. }
  151. }
  152. public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
  153. {
  154. throw new NotImplementedException ();
  155. }
  156. public virtual object GetStreamVersion (string streamName)
  157. {
  158. throw new NotImplementedException ();
  159. }
  160. public virtual IDisposable Impersonate ()
  161. {
  162. throw new NotImplementedException ();
  163. }
  164. public virtual void Init (IInternalConfigRoot root, params object[] hostInitParams)
  165. {
  166. }
  167. public virtual void InitForConfiguration (ref string locationSubPath, out string configPath,
  168. out string locationConfigPath, IInternalConfigRoot root,
  169. params object[] hostInitConfigurationParams)
  170. {
  171. string fullPath = (string) hostInitConfigurationParams [1];
  172. map = (WebConfigurationFileMap) hostInitConfigurationParams [0];
  173. if (locationSubPath == MachineWebPath) {
  174. locationSubPath = MachinePath;
  175. configPath = MachineWebPath;
  176. locationConfigPath = null;
  177. } else if (locationSubPath == MachinePath) {
  178. locationSubPath = null;
  179. configPath = MachinePath;
  180. locationConfigPath = null;
  181. } else {
  182. int i;
  183. if (locationSubPath == null)
  184. configPath = fullPath;
  185. else
  186. configPath = locationSubPath;
  187. if (configPath == HttpRuntime.AppDomainAppVirtualPath
  188. || configPath == "/")
  189. i = -1;
  190. else
  191. i = configPath.LastIndexOf ("/");
  192. if (i != -1) {
  193. locationConfigPath = configPath.Substring (i+1);
  194. if (i == 0)
  195. locationSubPath = "/";
  196. else
  197. locationSubPath = fullPath.Substring (0, i);
  198. } else {
  199. locationSubPath = MachineWebPath;
  200. locationConfigPath = null;
  201. }
  202. }
  203. }
  204. public string MapPath (string virtualPath)
  205. {
  206. if (map != null)
  207. return MapPathFromMapper (virtualPath);
  208. else if (HttpContext.Current != null && HttpContext.Current.Request != null)
  209. return HttpContext.Current.Request.MapPath (virtualPath);
  210. else if (HttpRuntime.AppDomainAppVirtualPath != null &&
  211. virtualPath.StartsWith (HttpRuntime.AppDomainAppVirtualPath)) {
  212. if (virtualPath == HttpRuntime.AppDomainAppVirtualPath)
  213. return HttpRuntime.AppDomainAppPath;
  214. return UrlUtils.Combine (HttpRuntime.AppDomainAppPath,
  215. virtualPath.Substring (HttpRuntime.AppDomainAppVirtualPath.Length));
  216. }
  217. return virtualPath;
  218. }
  219. public string NormalizeVirtualPath (string virtualPath)
  220. {
  221. if (virtualPath == null || virtualPath.Length == 0)
  222. virtualPath = ".";
  223. else
  224. virtualPath = virtualPath.Trim ();
  225. if (virtualPath [0] == '~' && virtualPath.Length > 2 && virtualPath [1] == '/')
  226. virtualPath = virtualPath.Substring (1);
  227. if (System.IO.Path.DirectorySeparatorChar != '/')
  228. virtualPath = virtualPath.Replace (System.IO.Path.DirectorySeparatorChar, '/');
  229. if (UrlUtils.IsRooted (virtualPath)) {
  230. virtualPath = UrlUtils.Canonic (virtualPath);
  231. } else {
  232. if (map.VirtualDirectories.Count > 0) {
  233. string root = map.VirtualDirectories [0].VirtualDirectory;
  234. virtualPath = UrlUtils.Combine (root, virtualPath);
  235. virtualPath = UrlUtils.Canonic (virtualPath);
  236. }
  237. }
  238. return virtualPath;
  239. }
  240. public string MapPathFromMapper (string virtualPath)
  241. {
  242. string path = NormalizeVirtualPath (virtualPath);
  243. foreach (VirtualDirectoryMapping mapping in map.VirtualDirectories) {
  244. if (path.StartsWith (mapping.VirtualDirectory)) {
  245. int i = mapping.VirtualDirectory.Length;
  246. if (path.Length == i) {
  247. return mapping.PhysicalDirectory;
  248. }
  249. else if (path [i] == '/') {
  250. string pathPart = path.Substring (i + 1).Replace ('/', Path.DirectorySeparatorChar);
  251. return Path.Combine (mapping.PhysicalDirectory, pathPart);
  252. }
  253. }
  254. }
  255. throw new HttpException ("Invalid virtual directory: " + virtualPath);
  256. }
  257. string GetWebConfigFileName (string dir)
  258. {
  259. string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
  260. foreach (string fn in filenames) {
  261. string file = Path.Combine (dir, fn);
  262. if (File.Exists (file))
  263. return file;
  264. }
  265. return null;
  266. }
  267. public virtual bool IsAboveApplication (string configPath)
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. public virtual bool IsConfigRecordRequired (string configPath)
  272. {
  273. throw new NotImplementedException ();
  274. }
  275. public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition,
  276. ConfigurationAllowExeDefinition allowExeDefinition)
  277. {
  278. switch (allowDefinition) {
  279. case ConfigurationAllowDefinition.MachineOnly:
  280. return configPath == MachinePath || configPath == MachineWebPath;
  281. case ConfigurationAllowDefinition.MachineToWebRoot:
  282. case ConfigurationAllowDefinition.MachineToApplication:
  283. return configPath == MachinePath || configPath == MachineWebPath || configPath == "/" ||
  284. configPath == HttpRuntime.AppDomainAppVirtualPath;
  285. default:
  286. return true;
  287. }
  288. }
  289. public virtual bool IsFile (string streamName)
  290. {
  291. throw new NotImplementedException ();
  292. }
  293. public virtual bool IsLocationApplicable (string configPath)
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. public virtual Stream OpenStreamForRead (string streamName)
  298. {
  299. if (!File.Exists (streamName)) {
  300. #if TARGET_J2EE
  301. if (streamName != null && (streamName.EndsWith ("machine.config") ||
  302. streamName.EndsWith ("web.config"))) {
  303. if (streamName.StartsWith ("/"))
  304. streamName = streamName.Substring (1);
  305. java.lang.ClassLoader cl = (java.lang.ClassLoader) AppDomain.CurrentDomain.GetData ("GH_ContextClassLoader");
  306. if (cl != null) {
  307. java.io.InputStream inputStream = cl.getResourceAsStream (streamName);
  308. return (Stream) vmw.common.IOUtils.getStream (inputStream);
  309. }
  310. }
  311. #endif
  312. throw new ConfigurationException ("File '" + streamName + "' not found");
  313. }
  314. return new FileStream (streamName, FileMode.Open, FileAccess.Read);
  315. }
  316. [MonoTODO ("Not implemented")]
  317. public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
  322. {
  323. return new FileStream (streamName, FileMode.Create, FileAccess.Write);
  324. }
  325. [MonoTODO ("Not implemented")]
  326. public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext,
  327. bool assertPermissions)
  328. {
  329. throw new NotImplementedException ();
  330. }
  331. public virtual bool PrefetchAll (string configPath, string streamName)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
  336. {
  337. throw new NotImplementedException ();
  338. }
  339. [MonoTODO ("Not implemented")]
  340. public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
  341. {
  342. throw new NotImplementedException ();
  343. }
  344. public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  345. {
  346. throw new NotImplementedException ();
  347. }
  348. public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
  349. {
  350. throw new NotImplementedException ();
  351. }
  352. public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition,
  353. ConfigurationAllowExeDefinition allowExeDefinition,
  354. IConfigErrorInfo errorInfo)
  355. {
  356. if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
  357. throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
  358. }
  359. [MonoTODO("Does nothing")]
  360. public virtual void WriteCompleted (string streamName, bool success, object writeContext)
  361. {
  362. }
  363. [MonoTODO("Does nothing")]
  364. public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
  365. {
  366. }
  367. public virtual bool SupportsChangeNotifications {
  368. get { return false; }
  369. }
  370. public virtual bool SupportsLocation {
  371. get { return false; }
  372. }
  373. public virtual bool SupportsPath {
  374. get { return false; }
  375. }
  376. public virtual bool SupportsRefresh {
  377. get { return false; }
  378. }
  379. [MonoTODO("Always returns false")]
  380. public virtual bool IsRemote {
  381. get { return false; }
  382. }
  383. [MonoTODO ("Not implemented")]
  384. public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
  385. {
  386. throw new NotImplementedException ();
  387. }
  388. [MonoTODO ("Not implemented")]
  389. public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
  390. {
  391. throw new NotImplementedException ();
  392. }
  393. [MonoTODO ("Not implemented")]
  394. public virtual bool IsSecondaryRoot (string configPath)
  395. {
  396. throw new NotImplementedException ();
  397. }
  398. [MonoTODO ("Not implemented")]
  399. public virtual bool IsTrustedConfigPath (string configPath)
  400. {
  401. throw new NotImplementedException ();
  402. }
  403. }
  404. }
  405. #endif