CustomErrorsConfigHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Configuration;
  32. using System.IO;
  33. using System.Xml;
  34. namespace System.Web.Configuration
  35. {
  36. enum CustomErrorMode
  37. {
  38. RemoteOnly,
  39. On,
  40. Off
  41. }
  42. class CustomErrorsConfig
  43. {
  44. string defaultRedirect;
  45. CustomErrorMode mode;
  46. Hashtable redirects;
  47. string configFilePath;
  48. public CustomErrorsConfig (object parent, object context)
  49. {
  50. if (parent != null) {
  51. CustomErrorsConfig p = (CustomErrorsConfig) parent;
  52. mode = p.mode;
  53. defaultRedirect = p.defaultRedirect;
  54. if (p.redirects != null && p.redirects.Count > 0) {
  55. redirects = new Hashtable ();
  56. foreach (DictionaryEntry entry in p.redirects)
  57. redirects [entry.Key] = entry.Value;
  58. }
  59. }
  60. configFilePath = Path.GetDirectoryName ((string) context);
  61. }
  62. public string DefaultRedirect {
  63. get { return defaultRedirect; }
  64. set { defaultRedirect = value; }
  65. }
  66. public CustomErrorMode Mode {
  67. get { return mode; }
  68. set { mode = value; }
  69. }
  70. public string ConfigFilePath {
  71. get { return configFilePath; }
  72. }
  73. public string this [int statusCode] {
  74. get {
  75. if (redirects == null)
  76. return null;
  77. return (string) redirects [statusCode];
  78. }
  79. set {
  80. if (redirects == null)
  81. redirects = new Hashtable ();
  82. // Overrides any previous setting for statusCode even in the same file
  83. redirects [statusCode] = value;
  84. }
  85. }
  86. }
  87. class CustomErrorsConfigHandler : IConfigurationSectionHandler
  88. {
  89. public object Create (object parent, object context, XmlNode section)
  90. {
  91. CustomErrorsConfig config = new CustomErrorsConfig (parent, context);
  92. string defaultRedirect = AttValue ("defaultRedirect", section);
  93. if (defaultRedirect != null)
  94. config.DefaultRedirect = defaultRedirect;
  95. string mode = AttValue ("mode", section);
  96. if (mode != null) {
  97. switch (mode) {
  98. case "On":
  99. config.Mode = CustomErrorMode.On;
  100. break;
  101. case "Off":
  102. config.Mode = CustomErrorMode.Off;
  103. break;
  104. case "RemoteOnly":
  105. config.Mode = CustomErrorMode.RemoteOnly;
  106. break;
  107. default:
  108. ThrowException ("Invalid value for 'mode': " + mode, section);
  109. break;
  110. }
  111. }
  112. if (section.Attributes != null && section.Attributes.Count != 0)
  113. ThrowException ("Unrecognized attribute", section);
  114. if (!section.HasChildNodes)
  115. return config;
  116. XmlNodeList children = section.ChildNodes;
  117. foreach (XmlNode child in children) {
  118. XmlNodeType ntype = child.NodeType;
  119. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  120. continue;
  121. if (ntype != XmlNodeType.Element)
  122. ThrowException ("Only elements allowed", child);
  123. if (child.Name != "error")
  124. ThrowException ("Unrecognized node: " + child.Name, child);
  125. string statusCode = AttValue ("statusCode", child, false, false);
  126. string redirect = AttValue ("redirect", child, false, false);
  127. int code = 0;
  128. try {
  129. code = Int32.Parse (statusCode);
  130. } catch {
  131. ThrowException ("Unable to parse 'statusCode': " + statusCode, child);
  132. }
  133. if (code < 100 || code >= 1000)
  134. ThrowException ("Invalid value for 'statusCode': " + code, child);
  135. config [code] = redirect;
  136. }
  137. return config;
  138. }
  139. // To save some typing...
  140. static string AttValue (string name, XmlNode node, bool optional, bool allowEmpty)
  141. {
  142. return HandlersUtil.ExtractAttributeValue (name, node, optional, allowEmpty);
  143. }
  144. static string AttValue (string name, XmlNode node)
  145. {
  146. return HandlersUtil.ExtractAttributeValue (name, node, true);
  147. }
  148. static void ThrowException (string message, XmlNode node)
  149. {
  150. HandlersUtil.ThrowException (message, node);
  151. }
  152. //
  153. }
  154. }