WebConfigurationSettings.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. //
  2. // System.Configuration.WebConfigurationSettings.cs
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) 2003,2004 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Configuration;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Reflection;
  14. using System.Runtime.Remoting;
  15. using System.Web.Util;
  16. using System.Xml;
  17. namespace System.Web.Configuration
  18. {
  19. class WebConfigurationSettings
  20. {
  21. static IConfigurationSystem oldConfig;
  22. static WebDefaultConfig config;
  23. static string machineConfigPath;
  24. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  25. private WebConfigurationSettings ()
  26. {
  27. }
  28. public static void Init ()
  29. {
  30. lock (typeof (WebConfigurationSettings)) {
  31. if (config != null)
  32. return;
  33. WebDefaultConfig settings = WebDefaultConfig.GetInstance ();
  34. Type t = typeof (ConfigurationSettings);
  35. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  36. privStatic);
  37. if (changeConfig == null)
  38. throw new ConfigurationException ("Cannot find method CCS");
  39. object [] args = new object [] {settings};
  40. oldConfig = (IConfigurationSystem) changeConfig.Invoke (null, args);
  41. config = settings;
  42. }
  43. }
  44. public static void Init (HttpContext context)
  45. {
  46. Init ();
  47. config.Init (context);
  48. }
  49. public static object GetConfig (string sectionName)
  50. {
  51. return config.GetConfig (sectionName);
  52. }
  53. public static object GetConfig (string sectionName, HttpContext context)
  54. {
  55. return config.GetConfig (sectionName, context);
  56. }
  57. public static string MachineConfigPath {
  58. get {
  59. lock (typeof (WebConfigurationSettings)) {
  60. if (machineConfigPath != null)
  61. return machineConfigPath;
  62. if (config == null)
  63. Init ();
  64. Type t = oldConfig.GetType ();
  65. MethodInfo getMC = t.GetMethod ("GetMachineConfigPath",
  66. privStatic);
  67. if (getMC == null)
  68. throw new ConfigurationException ("Cannot find method GMC");
  69. machineConfigPath = (string) getMC.Invoke (null, null);
  70. return machineConfigPath;
  71. }
  72. }
  73. }
  74. }
  75. //
  76. // class WebDefaultConfig: read configuration from machine.config file and application
  77. // config file if available.
  78. //
  79. class WebDefaultConfig : IConfigurationSystem
  80. {
  81. static WebDefaultConfig instance;
  82. Hashtable fileToConfig;
  83. HttpContext firstContext;
  84. bool initCalled;
  85. static WebDefaultConfig ()
  86. {
  87. instance = new WebDefaultConfig ();
  88. }
  89. private WebDefaultConfig ()
  90. {
  91. fileToConfig = new Hashtable ();
  92. }
  93. public static WebDefaultConfig GetInstance ()
  94. {
  95. return instance;
  96. }
  97. public object GetConfig (string sectionName)
  98. {
  99. HttpContext current = HttpContext.Current;
  100. if (current == null)
  101. current = firstContext;
  102. return GetConfig (sectionName, current);
  103. }
  104. public object GetConfig (string sectionName, HttpContext context)
  105. {
  106. if (context == null)
  107. return null;
  108. ConfigurationData config = GetConfigFromFileName (context.Request.FilePath, context);
  109. if (config == null)
  110. return null;
  111. return config.GetConfig (sectionName, context);
  112. }
  113. ConfigurationData GetConfigFromFileName (string filepath, HttpContext context)
  114. {
  115. if (filepath == "")
  116. return (ConfigurationData) fileToConfig [WebConfigurationSettings.MachineConfigPath];
  117. string dir = UrlUtils.GetDirectory (filepath);
  118. ConfigurationData data = (ConfigurationData) fileToConfig [dir];
  119. if (data != null)
  120. return data;
  121. string realpath = context.Request.MapPath (dir);
  122. string lower = Path.Combine (realpath, "web.config");
  123. string upper = Path.Combine (realpath, "Web.config");
  124. bool isUpper = File.Exists (upper);
  125. // This is a workaround for bug #56938
  126. // '\\' checks whether environment is Windows or not
  127. bool isLower = (Path.DirectorySeparatorChar == '\\') ? false : File.Exists (lower);
  128. if (isUpper && isLower && Directory.GetFiles (realpath, "Web.config").Length < 2)
  129. throw new ConfigurationException ("Both web.config and Web.config exist for " + dir);
  130. //
  131. string wcfile = (isUpper) ? upper : (isLower) ? lower : null;
  132. string tempDir = dir;
  133. if (tempDir == HttpRuntime.AppDomainAppVirtualPath)
  134. tempDir = "";
  135. ConfigurationData parent = GetConfigFromFileName (tempDir, context);
  136. if (wcfile == null) {
  137. data = new ConfigurationData (parent, null, realpath);
  138. data.DirName = dir;
  139. fileToConfig [dir] = data;
  140. }
  141. if (data == null) {
  142. data = new ConfigurationData (parent, wcfile);
  143. data.DirName = dir;
  144. data.LoadFromFile (wcfile);
  145. fileToConfig [dir] = data;
  146. RemotingConfiguration.Configure (wcfile);
  147. }
  148. return data;
  149. }
  150. public void Init ()
  151. {
  152. // nothing. We need a context.
  153. }
  154. public void Init (HttpContext context)
  155. {
  156. lock (this) {
  157. if (initCalled)
  158. return;
  159. firstContext = context;
  160. ConfigurationData data = new ConfigurationData ();
  161. if (!data.LoadFromFile (WebConfigurationSettings.MachineConfigPath))
  162. throw new ConfigurationException ("Cannot find " + WebConfigurationSettings.MachineConfigPath);
  163. fileToConfig [WebConfigurationSettings.MachineConfigPath] = data;
  164. initCalled = true;
  165. }
  166. }
  167. static string GetAppConfigPath ()
  168. {
  169. AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
  170. string configFile = currentInfo.ConfigurationFile;
  171. if (configFile == null || configFile.Length == 0)
  172. return null;
  173. return configFile;
  174. }
  175. }
  176. class FileWatcherCache
  177. {
  178. Hashtable cacheTable;
  179. string path;
  180. string filename;
  181. FileSystemWatcher watcher;
  182. ConfigurationData data;
  183. public FileWatcherCache (ConfigurationData data)
  184. {
  185. this.data = data;
  186. cacheTable = Hashtable.Synchronized (new Hashtable ());
  187. this.path = Path.GetDirectoryName (data.FileName);
  188. this.filename = Path.GetFileName (data.FileName);
  189. watcher = new FileSystemWatcher (this.path, this.filename);
  190. FileSystemEventHandler handler = new FileSystemEventHandler (SetChanged);
  191. watcher.Created += handler;
  192. watcher.Changed += handler;
  193. watcher.Deleted += handler;
  194. watcher.EnableRaisingEvents = true;
  195. }
  196. void SetChanged (object o, FileSystemEventArgs args)
  197. {
  198. lock (data) {
  199. if (watcher.Filter == "*.config" &&
  200. String.Compare (Path.GetFileName (args.FullPath), "web.config", true) != 0)
  201. return;
  202. cacheTable.Clear ();
  203. data.Reset ();
  204. if (args.ChangeType == WatcherChangeTypes.Created)
  205. RemotingConfiguration.Configure (args.FullPath);
  206. if (args.ChangeType != WatcherChangeTypes.Deleted)
  207. data.LoadFromFile (args.FullPath);
  208. }
  209. }
  210. public object this [string key] {
  211. get {
  212. lock (data)
  213. return cacheTable [key];
  214. }
  215. set {
  216. lock (data)
  217. cacheTable [key] = value;
  218. }
  219. }
  220. public void Close ()
  221. {
  222. watcher.EnableRaisingEvents = false;
  223. }
  224. }
  225. enum AllowDefinition
  226. {
  227. Everywhere,
  228. MachineOnly,
  229. MachineToApplication
  230. }
  231. class SectionData
  232. {
  233. public readonly string SectionName;
  234. public readonly string TypeName;
  235. public readonly bool AllowLocation;
  236. public readonly AllowDefinition AllowDefinition;
  237. public string FileName;
  238. public SectionData (string sectionName, string typeName,
  239. bool allowLocation, AllowDefinition allowDefinition)
  240. {
  241. SectionName = sectionName;
  242. TypeName = typeName;
  243. AllowLocation = allowLocation;
  244. AllowDefinition = allowDefinition;
  245. }
  246. }
  247. class ConfigurationData
  248. {
  249. ConfigurationData parent;
  250. Hashtable factories;
  251. Hashtable pending;
  252. Hashtable locations;
  253. string fileName;
  254. string dirname;
  255. string realdir;
  256. static object removedMark = new object ();
  257. static object groupMark = new object ();
  258. static object emptyMark = new object ();
  259. FileWatcherCache fileCache;
  260. static char [] forbiddenPathChars = new char [] {
  261. ';', '?', ':', '@', '&', '=', '+',
  262. '$', ',','\\', '*', '\"', '<', '>'
  263. };
  264. static string forbiddenStr = "';', '?', ':', '@', '&', '=', '+', '$', ',', '\\', '*', '\"', '<', '>'";
  265. internal FileWatcherCache FileCache {
  266. get {
  267. lock (this) {
  268. if (fileCache != null)
  269. return fileCache;
  270. fileCache = new FileWatcherCache (this);
  271. }
  272. return fileCache;
  273. }
  274. }
  275. internal string FileName {
  276. get { return fileName; }
  277. }
  278. internal ConfigurationData Parent {
  279. get { return parent; }
  280. }
  281. internal string DirName {
  282. get { return dirname; }
  283. set { dirname = value; }
  284. }
  285. internal void Reset ()
  286. {
  287. factories.Clear ();
  288. if (pending != null)
  289. pending.Clear ();
  290. if (locations != null)
  291. locations.Clear ();
  292. }
  293. public ConfigurationData () : this (null, null)
  294. {
  295. }
  296. public ConfigurationData (ConfigurationData parent, string filename)
  297. {
  298. this.parent = (parent == this) ? null : parent;
  299. this.fileName = filename;
  300. factories = new Hashtable ();
  301. }
  302. public ConfigurationData (ConfigurationData parent, string filename, string realdir)
  303. {
  304. this.parent = (parent == this) ? null : parent;
  305. this.realdir = realdir;
  306. if (filename == null) {
  307. this.fileName = Path.Combine (realdir, "*.config");
  308. } else {
  309. this.fileName = filename;
  310. }
  311. factories = new Hashtable ();
  312. }
  313. public bool LoadFromFile (string fileName)
  314. {
  315. this.fileName = fileName;
  316. if (fileName == null || !File.Exists (fileName))
  317. return false;
  318. XmlTextReader reader = null;
  319. try {
  320. FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  321. reader = new XmlTextReader (fs);
  322. InitRead (reader);
  323. ReadConfig (reader, false);
  324. } catch (ConfigurationException) {
  325. throw;
  326. } catch (Exception e) {
  327. throw new ConfigurationException ("Error reading " + fileName, e);
  328. } finally {
  329. if (reader != null)
  330. reader.Close();
  331. }
  332. return true;
  333. }
  334. public void LoadFromReader (XmlTextReader reader, string fakeFileName, bool isLocation)
  335. {
  336. fileName = fakeFileName;
  337. MoveToNextElement (reader);
  338. ReadConfig (reader, isLocation);
  339. }
  340. object GetHandler (string sectionName)
  341. {
  342. lock (factories) {
  343. object o = factories [sectionName];
  344. if (o == null || o == removedMark) {
  345. if (parent != null)
  346. return parent.GetHandler (sectionName);
  347. return null;
  348. }
  349. if (o is IConfigurationSectionHandler)
  350. return (IConfigurationSectionHandler) o;
  351. o = CreateNewHandler (sectionName, (SectionData) o);
  352. factories [sectionName] = o;
  353. return o;
  354. }
  355. }
  356. object CreateNewHandler (string sectionName, SectionData section)
  357. {
  358. Type t = Type.GetType (section.TypeName);
  359. if (t == null)
  360. throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
  361. Type iconfig = typeof (IConfigurationSectionHandler);
  362. if (!iconfig.IsAssignableFrom (t))
  363. throw new ConfigurationException (sectionName + " does not implement " + iconfig);
  364. object o = Activator.CreateInstance (t, true);
  365. if (o == null)
  366. throw new ConfigurationException ("Cannot get instance for " + t);
  367. return o;
  368. }
  369. XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
  370. {
  371. if (++i >= sectionPath.Length)
  372. return doc;
  373. if (doc.DocumentElement == null)
  374. return null;
  375. XmlNode node = doc.DocumentElement.FirstChild;
  376. while (node != null) {
  377. if (node.Name == sectionPath [i]) {
  378. ConfigXmlDocument result = new ConfigXmlDocument ();
  379. result.Load (new StringReader (node.OuterXml));
  380. return GetInnerDoc (result, i, sectionPath);
  381. }
  382. node = node.NextSibling;
  383. }
  384. return null;
  385. }
  386. XmlDocument GetDocumentForSection (string sectionName)
  387. {
  388. ConfigXmlDocument doc = new ConfigXmlDocument ();
  389. if (pending == null)
  390. return doc;
  391. string [] sectionPath = sectionName.Split ('/');
  392. string outerxml = pending [sectionPath [0]] as string;
  393. if (outerxml == null)
  394. return doc;
  395. doc.Load (new StringReader (outerxml));
  396. return GetInnerDoc (doc, 0, sectionPath);
  397. }
  398. object GetConfigInternal (string sectionName, HttpContext context, bool useLoc)
  399. {
  400. object handler = GetHandler (sectionName);
  401. IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
  402. if (iconf == null)
  403. return handler;
  404. object parentConfig = null;
  405. if (parent != null) {
  406. if (useLoc)
  407. parentConfig = parent.GetConfig (sectionName, context);
  408. else
  409. parentConfig = parent.GetConfigOptLocation (sectionName, context, false);
  410. }
  411. XmlDocument doc = GetDocumentForSection (sectionName);
  412. if (doc == null || doc.DocumentElement == null)
  413. return parentConfig;
  414. return iconf.Create (parentConfig, fileName, doc.DocumentElement);
  415. }
  416. public object GetConfig (string sectionName, HttpContext context)
  417. {
  418. if (locations != null && dirname != null) {
  419. string reduced = UrlUtils.MakeRelative (context.Request.FilePath, dirname);
  420. string [] parts = reduced.Split ('/');
  421. Location location = null;
  422. int length = parts.Length;
  423. string target = null;
  424. for (int i = 0; i < parts.Length; i++) {
  425. if (target == null)
  426. target = parts [i];
  427. else
  428. target = target + "/" + parts [i];
  429. if (locations.ContainsKey (target)) {
  430. location = locations [target] as Location;
  431. } else if (locations.ContainsKey (target + "/*")) {
  432. location = locations [target + "/*"] as Location;
  433. }
  434. }
  435. if (location == null) {
  436. location = locations ["*"] as Location;
  437. }
  438. if (location != null && location.Config != null) {
  439. object o = location.Config.GetConfigOptLocation (sectionName, context, false);
  440. if (o != null) {
  441. return o;
  442. }
  443. }
  444. }
  445. return GetConfigOptLocation (sectionName, context, true);
  446. }
  447. object GetConfigOptLocation (string sectionName, HttpContext context, bool useLoc)
  448. {
  449. object config = this.FileCache [sectionName];
  450. if (config == emptyMark)
  451. return null;
  452. if (config != null)
  453. return config;
  454. lock (this) {
  455. config = GetConfigInternal (sectionName, context, useLoc);
  456. this.FileCache [sectionName] = (config == null) ? emptyMark : config;
  457. }
  458. return config;
  459. }
  460. private object LookForFactory (string key)
  461. {
  462. object o = factories [key];
  463. if (o != null)
  464. return o;
  465. if (parent != null)
  466. return parent.LookForFactory (key);
  467. return null;
  468. }
  469. private void InitRead (XmlTextReader reader)
  470. {
  471. reader.MoveToContent ();
  472. if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
  473. ThrowException ("Configuration file does not have a valid root element", reader);
  474. if (reader.HasAttributes)
  475. ThrowException ("Unrecognized attribute in root element", reader);
  476. MoveToNextElement (reader);
  477. }
  478. internal void MoveToNextElement (XmlTextReader reader)
  479. {
  480. while (reader.Read ()) {
  481. XmlNodeType ntype = reader.NodeType;
  482. if (ntype == XmlNodeType.Element)
  483. return;
  484. if (ntype != XmlNodeType.Whitespace &&
  485. ntype != XmlNodeType.Comment &&
  486. ntype != XmlNodeType.SignificantWhitespace &&
  487. ntype != XmlNodeType.EndElement)
  488. ThrowException ("Unrecognized element", reader);
  489. }
  490. }
  491. private void ReadSection (XmlTextReader reader, string sectionName)
  492. {
  493. string attName;
  494. string nameValue = null;
  495. string typeValue = null;
  496. string allowLoc = null, allowDef = null;
  497. bool allowLocation = true;
  498. AllowDefinition allowDefinition = AllowDefinition.Everywhere;
  499. while (reader.MoveToNextAttribute ()) {
  500. attName = reader.Name;
  501. if (attName == null)
  502. continue;
  503. if (attName == "allowLocation") {
  504. if (allowLoc != null)
  505. ThrowException ("Duplicated allowLocation attribute.", reader);
  506. allowLoc = reader.Value;
  507. allowLocation = (allowLoc == "true");
  508. if (!allowLocation && allowLoc != "false")
  509. ThrowException ("Invalid attribute value", reader);
  510. continue;
  511. }
  512. if (attName == "allowDefinition") {
  513. if (allowDef != null)
  514. ThrowException ("Duplicated allowDefinition attribute.", reader);
  515. allowDef = reader.Value;
  516. try {
  517. allowDefinition = (AllowDefinition) Enum.Parse (
  518. typeof (AllowDefinition), allowDef);
  519. } catch {
  520. ThrowException ("Invalid attribute value", reader);
  521. }
  522. continue;
  523. }
  524. if (attName == "type") {
  525. if (typeValue != null)
  526. ThrowException ("Duplicated type attribute.", reader);
  527. typeValue = reader.Value;
  528. continue;
  529. }
  530. if (attName == "name") {
  531. if (nameValue != null)
  532. ThrowException ("Duplicated name attribute.", reader);
  533. nameValue = reader.Value;
  534. if (nameValue == "location")
  535. ThrowException ("location is a reserved section name", reader);
  536. continue;
  537. }
  538. ThrowException ("Unrecognized attribute.", reader);
  539. }
  540. if (nameValue == null || typeValue == null)
  541. ThrowException ("Required attribute missing", reader);
  542. if (sectionName != null)
  543. nameValue = sectionName + '/' + nameValue;
  544. reader.MoveToElement();
  545. object o = LookForFactory (nameValue);
  546. if (o != null && o != removedMark)
  547. ThrowException ("Already have a factory for " + nameValue, reader);
  548. SectionData section = new SectionData (nameValue, typeValue, allowLocation, allowDefinition);
  549. section.FileName = fileName;
  550. factories [nameValue] = section;
  551. MoveToNextElement (reader);
  552. }
  553. private void ReadRemoveSection (XmlTextReader reader, string sectionName)
  554. {
  555. if (!reader.MoveToNextAttribute () || reader.Name != "name")
  556. ThrowException ("Unrecognized attribute.", reader);
  557. string removeValue = reader.Value;
  558. if (removeValue == null || removeValue.Length == 0)
  559. ThrowException ("Empty name to remove", reader);
  560. reader.MoveToElement ();
  561. if (sectionName != null)
  562. removeValue = sectionName + '/' + removeValue;
  563. object o = LookForFactory (removeValue);
  564. if (o != null && o == removedMark)
  565. ThrowException ("No factory for " + removeValue, reader);
  566. factories [removeValue] = removedMark;
  567. MoveToNextElement (reader);
  568. }
  569. private void ReadSectionGroup (XmlTextReader reader, string configSection)
  570. {
  571. if (!reader.MoveToNextAttribute ())
  572. ThrowException ("sectionGroup must have a 'name' attribute.", reader);
  573. if (reader.Name != "name")
  574. ThrowException ("Unrecognized attribute.", reader);
  575. if (reader.MoveToNextAttribute ())
  576. ThrowException ("Unrecognized attribute.", reader);
  577. string value = reader.Value;
  578. if (value == "location")
  579. ThrowException ("location is a reserved section name", reader);
  580. if (configSection != null)
  581. value = configSection + '/' + value;
  582. object o = LookForFactory (value);
  583. if (o != null && o != removedMark && o != groupMark)
  584. ThrowException ("Already have a factory for " + value, reader);
  585. factories [value] = groupMark;
  586. MoveToNextElement (reader);
  587. ReadSections (reader, value);
  588. }
  589. private void ReadSections (XmlTextReader reader, string configSection)
  590. {
  591. int depth = reader.Depth;
  592. while (reader.Depth == depth) {
  593. string name = reader.Name;
  594. if (name == "section") {
  595. ReadSection (reader, configSection);
  596. continue;
  597. }
  598. if (name == "remove") {
  599. ReadRemoveSection (reader, configSection);
  600. continue;
  601. }
  602. if (name == "clear") {
  603. if (reader.HasAttributes)
  604. ThrowException ("Unrecognized attribute.", reader);
  605. factories.Clear ();
  606. MoveToNextElement (reader);
  607. continue;
  608. }
  609. if (name == "sectionGroup") {
  610. ReadSectionGroup (reader, configSection);
  611. continue;
  612. }
  613. ThrowException ("Unrecognized element: " + reader.Name, reader);
  614. }
  615. }
  616. void StoreLocation (string name, XmlTextReader reader)
  617. {
  618. if (locations == null) {
  619. locations = new Hashtable ();
  620. }
  621. string path = null;
  622. bool haveAllow = false;
  623. bool allowOverride = true;
  624. while (reader.MoveToNextAttribute ()) {
  625. string att = reader.Name;
  626. if (att == "path") {
  627. if (path != null)
  628. ThrowException ("Duplicate path attribute", reader);
  629. path = reader.Value;
  630. if (path.StartsWith ("."))
  631. ThrowException ("Path cannot begin with '.'", reader);
  632. if (path.IndexOfAny (forbiddenPathChars) != -1)
  633. ThrowException ("Path cannot contain " + forbiddenStr, reader);
  634. continue;
  635. }
  636. if (att == "allowOverride") {
  637. if (haveAllow)
  638. ThrowException ("Duplicate allowOverride attribute", reader);
  639. haveAllow = true;
  640. allowOverride = (reader.Value == "true");
  641. if (!allowOverride && reader.Value != "false")
  642. ThrowException ("allowOverride must be either true or false", reader);
  643. continue;
  644. }
  645. ThrowException ("Unrecognized attribute.", reader);
  646. }
  647. Location loc = new Location (this, path, allowOverride);
  648. if (locations.ContainsKey (loc.Path))
  649. ThrowException ("Duplicated location path: " + loc.Path, reader);
  650. reader.MoveToElement ();
  651. loc.LoadFromString (reader.ReadInnerXml ());
  652. locations [loc.Path] = loc;
  653. if (!loc.AllowOverride) {
  654. XmlTextReader inner = loc.GetReader ();
  655. if (inner != null) {
  656. MoveToNextElement (inner);
  657. ReadConfig (loc.GetReader (), true);
  658. }
  659. }
  660. loc.XmlStr = null;
  661. }
  662. void StorePending (string name, XmlTextReader reader)
  663. {
  664. if (pending == null)
  665. pending = new Hashtable ();
  666. if (pending.ContainsKey (name))
  667. ThrowException ("Sections can only appear once: " + name, reader);
  668. pending [name] = reader.ReadOuterXml ();
  669. }
  670. void ReadConfig (XmlTextReader reader, bool isLocation)
  671. {
  672. int depth = reader.Depth;
  673. while (!reader.EOF && reader.Depth == depth) {
  674. string name = reader.Name;
  675. if (name == "configSections") {
  676. if (isLocation)
  677. ThrowException ("<configSections> inside <location>", reader);
  678. if (reader.HasAttributes)
  679. ThrowException ("Unrecognized attribute in <configSections>.", reader);
  680. MoveToNextElement (reader);
  681. ReadSections (reader, null);
  682. } else if (name == "location") {
  683. if (isLocation)
  684. ThrowException ("<location> inside <location>", reader);
  685. StoreLocation (name, reader);
  686. MoveToNextElement (reader);
  687. } else if (name != null && name != ""){
  688. StorePending (name, reader);
  689. MoveToNextElement (reader);
  690. } else {
  691. MoveToNextElement (reader);
  692. }
  693. }
  694. }
  695. void ThrowException (string text, XmlTextReader reader)
  696. {
  697. throw new ConfigurationException (text, fileName, reader.LineNumber);
  698. }
  699. }
  700. class Location
  701. {
  702. string path;
  703. bool allowOverride;
  704. ConfigurationData parent;
  705. ConfigurationData thisOne;
  706. string xmlstr;
  707. public Location (ConfigurationData parent, string path, bool allowOverride)
  708. {
  709. this.parent = parent;
  710. this.allowOverride = allowOverride;
  711. this.path = (path == null || path == "") ? "*" : path;
  712. }
  713. public bool AllowOverride {
  714. get { return (path != "*" || allowOverride); }
  715. }
  716. public string Path {
  717. get { return path; }
  718. }
  719. public string XmlStr {
  720. set { xmlstr = value; }
  721. }
  722. public void LoadFromString (string str)
  723. {
  724. if (str == null)
  725. throw new ArgumentNullException ("str");
  726. if (thisOne != null)
  727. throw new InvalidOperationException ();
  728. this.xmlstr = str.Trim ();
  729. if (xmlstr == "")
  730. return;
  731. XmlTextReader reader = new XmlTextReader (new StringReader (str));
  732. thisOne = new ConfigurationData (parent, parent.FileName);
  733. thisOne.LoadFromReader (reader, parent.FileName, true);
  734. }
  735. public XmlTextReader GetReader ()
  736. {
  737. if (xmlstr == "")
  738. return null;
  739. XmlTextReader reader = new XmlTextReader (new StringReader (xmlstr));
  740. return reader;
  741. }
  742. public ConfigurationData Config {
  743. get { return thisOne; }
  744. }
  745. }
  746. }