CustomErrorsConfigHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // System.Web.Configuration.CustomErrorsConfigHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Configuration;
  12. using System.IO;
  13. using System.Xml;
  14. namespace System.Web.Configuration
  15. {
  16. enum CustomErrorMode
  17. {
  18. RemoteOnly,
  19. On,
  20. Off
  21. }
  22. class CustomErrorsConfig
  23. {
  24. string defaultRedirect;
  25. CustomErrorMode mode;
  26. Hashtable redirects;
  27. string configFilePath;
  28. public CustomErrorsConfig (object parent, object context)
  29. {
  30. if (parent != null) {
  31. CustomErrorsConfig p = (CustomErrorsConfig) parent;
  32. mode = p.mode;
  33. defaultRedirect = p.defaultRedirect;
  34. if (p.redirects != null && p.redirects.Count > 0) {
  35. redirects = new Hashtable ();
  36. foreach (DictionaryEntry entry in p.redirects)
  37. redirects [entry.Key] = entry.Value;
  38. }
  39. }
  40. configFilePath = Path.GetDirectoryName ((string) context);
  41. }
  42. public string DefaultRedirect {
  43. get { return defaultRedirect; }
  44. set { defaultRedirect = value; }
  45. }
  46. public CustomErrorMode Mode {
  47. get { return mode; }
  48. set { mode = value; }
  49. }
  50. public string ConfigFilePath {
  51. get { return configFilePath; }
  52. }
  53. public string this [int statusCode] {
  54. get {
  55. if (redirects == null)
  56. return null;
  57. return (string) redirects [statusCode];
  58. }
  59. set {
  60. if (redirects == null)
  61. redirects = new Hashtable ();
  62. // Overrides any previous setting for statusCode even in the same file
  63. redirects [statusCode] = value;
  64. }
  65. }
  66. }
  67. class CustomErrorsConfigHandler : IConfigurationSectionHandler
  68. {
  69. public object Create (object parent, object context, XmlNode section)
  70. {
  71. CustomErrorsConfig config = new CustomErrorsConfig (parent, context);
  72. string defaultRedirect = AttValue ("defaultRedirect", section);
  73. if (defaultRedirect != null)
  74. config.DefaultRedirect = defaultRedirect;
  75. string mode = AttValue ("mode", section);
  76. if (mode != null) {
  77. switch (mode) {
  78. case "On":
  79. config.Mode = CustomErrorMode.On;
  80. break;
  81. case "Off":
  82. config.Mode = CustomErrorMode.Off;
  83. break;
  84. case "RemoteOnly":
  85. config.Mode = CustomErrorMode.RemoteOnly;
  86. break;
  87. default:
  88. ThrowException ("Invalid value for 'mode': " + mode, section);
  89. break;
  90. }
  91. }
  92. if (section.Attributes != null && section.Attributes.Count != 0)
  93. ThrowException ("Unrecognized attribute", section);
  94. if (!section.HasChildNodes)
  95. return config;
  96. XmlNodeList children = section.ChildNodes;
  97. foreach (XmlNode child in children) {
  98. XmlNodeType ntype = child.NodeType;
  99. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  100. continue;
  101. if (ntype != XmlNodeType.Element)
  102. ThrowException ("Only elements allowed", child);
  103. if (child.Name != "error")
  104. ThrowException ("Unrecognized node: " + child.Name, child);
  105. string statusCode = AttValue ("statusCode", child, false, false);
  106. string redirect = AttValue ("redirect", child, false, false);
  107. int code = 0;
  108. try {
  109. code = Int32.Parse (statusCode);
  110. } catch {
  111. ThrowException ("Unable to parse 'statusCode': " + statusCode, child);
  112. }
  113. if (code < 100 || code >= 1000)
  114. ThrowException ("Invalid value for 'statusCode': " + code, child);
  115. config [code] = redirect;
  116. }
  117. return config;
  118. }
  119. // To save some typing...
  120. static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
  121. {
  122. return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
  123. }
  124. static string AttValue (string name, XmlNode node, bool optional)
  125. {
  126. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  127. }
  128. static string AttValue (string name, XmlNode node)
  129. {
  130. return HandlersUtil.ExtractAttributeValue (name, node, true);
  131. }
  132. static void ThrowException (string message, XmlNode node)
  133. {
  134. HandlersUtil.ThrowException (message, node);
  135. }
  136. //
  137. }
  138. }