WebConfigurationSettings.cs 23 KB

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