PageRenderTime 204ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/referencesource/System.Web/Configuration/HandlerBase.cs

https://github.com/pruiz/mono
C# | 300 lines | 211 code | 61 blank | 28 comment | 54 complexity | b1d5e7ee40a73585c4db890f11db531a MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //------------------------------------------------------------------------------
  2. // <copyright file="HandlerBase.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. /*
  7. * HandlerBase contains static helper functions for consistent XML parsing
  8. * behavior and error messages.
  9. *
  10. * Copyright (c) 1998 Microsoft Corporation
  11. */
  12. namespace System.Web.Configuration {
  13. using System.Collections;
  14. using System.Collections.Specialized;
  15. using System.Configuration;
  16. using System.Globalization;
  17. using System.Text;
  18. using System.Web.Hosting;
  19. using System.Web.Util;
  20. using System.Xml;
  21. using System.Web.Compilation;
  22. static internal class HandlerBase {
  23. //
  24. // XML Attribute Helpers
  25. //
  26. private static XmlNode GetAndRemoveAttribute(XmlNode node, string attrib, bool fRequired) {
  27. XmlNode a = node.Attributes.RemoveNamedItem(attrib);
  28. // If the attribute is required and was not present, throw
  29. if (fRequired && a == null) {
  30. throw new ConfigurationErrorsException(
  31. SR.GetString(SR.Missing_required_attribute, attrib, node.Name),
  32. node);
  33. }
  34. return a;
  35. }
  36. private static XmlNode GetAndRemoveStringAttributeInternal(XmlNode node, string attrib, bool fRequired, ref string val) {
  37. XmlNode a = GetAndRemoveAttribute(node, attrib, fRequired);
  38. if (a != null) {
  39. val = a.Value;
  40. }
  41. return a;
  42. }
  43. internal static XmlNode GetAndRemoveStringAttribute(XmlNode node, string attrib, ref string val) {
  44. return GetAndRemoveStringAttributeInternal(node, attrib, false /*fRequired*/, ref val);
  45. }
  46. internal static XmlNode GetAndRemoveRequiredStringAttribute(XmlNode node, string attrib, ref string val) {
  47. return GetAndRemoveStringAttributeInternal(node, attrib, true /*fRequired*/, ref val);
  48. }
  49. internal static XmlNode GetAndRemoveNonEmptyStringAttribute(XmlNode node, string attrib, ref string val) {
  50. return GetAndRemoveNonEmptyStringAttributeInternal(node, attrib, false /*fRequired*/, ref val);
  51. }
  52. internal static XmlNode GetAndRemoveRequiredNonEmptyStringAttribute(XmlNode node, string attrib, ref string val) {
  53. return GetAndRemoveNonEmptyStringAttributeInternal(node, attrib, true /*fRequired*/, ref val);
  54. }
  55. private static XmlNode GetAndRemoveNonEmptyStringAttributeInternal(XmlNode node, string attrib, bool fRequired, ref string val) {
  56. XmlNode a = GetAndRemoveStringAttributeInternal(node, attrib, fRequired, ref val);
  57. if (a != null && val.Length == 0) {
  58. throw new ConfigurationErrorsException(
  59. SR.GetString(SR.Empty_attribute, attrib),
  60. a);
  61. }
  62. return a;
  63. }
  64. // input.Xml cursor must be at a true/false XML attribute
  65. private static XmlNode GetAndRemoveBooleanAttributeInternal(XmlNode node, string attrib, bool fRequired, ref bool val) {
  66. XmlNode a = GetAndRemoveAttribute(node, attrib, fRequired);
  67. if (a != null) {
  68. if (a.Value == "true") {
  69. val = true;
  70. }
  71. else if (a.Value == "false") {
  72. val = false;
  73. }
  74. else {
  75. throw new ConfigurationErrorsException(
  76. SR.GetString(SR.Invalid_boolean_attribute, a.Name),
  77. a);
  78. }
  79. }
  80. return a;
  81. }
  82. internal static XmlNode GetAndRemoveBooleanAttribute(XmlNode node, string attrib, ref bool val) {
  83. return GetAndRemoveBooleanAttributeInternal(node, attrib, false /*fRequired*/, ref val);
  84. }
  85. private static XmlNode GetAndRemoveIntegerAttributeInternal(XmlNode node, string attrib, bool fRequired, ref int val) {
  86. XmlNode a = GetAndRemoveAttribute(node, attrib, fRequired);
  87. if (a != null) {
  88. if (a.Value.Trim() != a.Value) {
  89. throw new ConfigurationErrorsException(
  90. SR.GetString(SR.Invalid_integer_attribute, a.Name),
  91. a);
  92. }
  93. try {
  94. val = int.Parse(a.Value, CultureInfo.InvariantCulture);
  95. }
  96. catch (Exception e) {
  97. throw new ConfigurationErrorsException(
  98. SR.GetString(SR.Invalid_integer_attribute, a.Name),
  99. e, a);
  100. }
  101. }
  102. return a;
  103. }
  104. private static XmlNode GetAndRemovePositiveAttributeInternal(XmlNode node, string attrib, bool fRequired, ref int val) {
  105. XmlNode a = GetAndRemoveIntegerAttributeInternal(node, attrib, fRequired, ref val);
  106. if (a != null && val <= 0) {
  107. throw new ConfigurationErrorsException(
  108. SR.GetString(SR.Invalid_positive_integer_attribute, attrib),
  109. a);
  110. }
  111. return a;
  112. }
  113. internal static XmlNode GetAndRemovePositiveIntegerAttribute(XmlNode node, string attrib, ref int val) {
  114. return GetAndRemovePositiveAttributeInternal(node, attrib, false /*fRequired*/, ref val);
  115. }
  116. private static XmlNode GetAndRemoveTypeAttributeInternal(XmlNode node, string attrib, bool fRequired, ref Type val) {
  117. XmlNode a = GetAndRemoveAttribute(node, attrib, fRequired);
  118. if (a != null) {
  119. val = ConfigUtil.GetType(a.Value, a);
  120. }
  121. return a;
  122. }
  123. internal static XmlNode GetAndRemoveTypeAttribute(XmlNode node, string attrib, ref Type val) {
  124. return GetAndRemoveTypeAttributeInternal(node, attrib, false /*fRequired*/, ref val);
  125. }
  126. internal static void CheckForbiddenAttribute(XmlNode node, string attrib) {
  127. XmlAttribute attr = node.Attributes[attrib];
  128. if (attr != null) {
  129. throw new ConfigurationErrorsException(
  130. SR.GetString(SR.Config_base_unrecognized_attribute, attrib),
  131. attr);
  132. }
  133. }
  134. internal static void CheckForUnrecognizedAttributes(XmlNode node) {
  135. if (node.Attributes.Count != 0) {
  136. throw new ConfigurationErrorsException(
  137. SR.GetString(SR.Config_base_unrecognized_attribute, node.Attributes[0].Name),
  138. node.Attributes[0]);
  139. }
  140. }
  141. //
  142. // Obsolete XML Attribute Helpers
  143. //
  144. // if attribute not found return null
  145. internal static string RemoveAttribute(XmlNode node, string name) {
  146. XmlNode attribute = node.Attributes.RemoveNamedItem(name);
  147. if (attribute != null) {
  148. return attribute.Value;
  149. }
  150. return null;
  151. }
  152. // if attr not found throw standard message - "attribute x required"
  153. internal static string RemoveRequiredAttribute(XmlNode node, string name) {
  154. return RemoveRequiredAttribute(node, name, false);
  155. }
  156. internal static string RemoveRequiredAttribute(XmlNode node, string name, bool allowEmpty) {
  157. XmlNode attribute = node.Attributes.RemoveNamedItem(name);
  158. if (attribute == null) {
  159. throw new ConfigurationErrorsException(
  160. SR.GetString(SR.Config_base_required_attribute_missing, name),
  161. node);
  162. }
  163. if (attribute.Value.Length == 0 && !allowEmpty) {
  164. throw new ConfigurationErrorsException(
  165. SR.GetString(SR.Config_base_required_attribute_empty, name),
  166. node);
  167. }
  168. return attribute.Value;
  169. }
  170. //
  171. // XML Element Helpers
  172. //
  173. internal static void CheckForNonCommentChildNodes(XmlNode node) {
  174. foreach (XmlNode childNode in node.ChildNodes) {
  175. if (childNode.NodeType != XmlNodeType.Comment) {
  176. throw new ConfigurationErrorsException(
  177. SR.GetString(SR.Config_base_no_child_nodes),
  178. childNode);
  179. }
  180. }
  181. }
  182. internal static void ThrowUnrecognizedElement(XmlNode node) {
  183. throw new ConfigurationErrorsException(
  184. SR.GetString(SR.Config_base_unrecognized_element),
  185. node);
  186. }
  187. internal static void CheckAssignableType(XmlNode node, Type baseType, Type type) {
  188. if (!baseType.IsAssignableFrom(type)) {
  189. throw new ConfigurationErrorsException(
  190. SR.GetString(SR.Type_doesnt_inherit_from_type, type.FullName, baseType.FullName),
  191. node);
  192. }
  193. }
  194. internal static void CheckAssignableType(string filename, int lineNumber, Type baseType, Type type) {
  195. if (!baseType.IsAssignableFrom(type)) {
  196. throw new ConfigurationErrorsException(
  197. SR.GetString(SR.Type_doesnt_inherit_from_type, type.FullName, baseType.FullName),
  198. filename, lineNumber);
  199. }
  200. }
  201. // Section handlers can run in client mode through:
  202. // ConfigurationManager.GetSection("sectionName")
  203. // See ASURT 123738
  204. internal static bool IsServerConfiguration(object context) {
  205. return context is HttpConfigurationContext;
  206. }
  207. internal static bool CheckAndReadRegistryValue(ref string value, bool throwIfError) {
  208. if (value == null) {
  209. return true;
  210. }
  211. if (!StringUtil.StringStartsWithIgnoreCase(value, "registry:")) {
  212. // Not a registry value. It's not an error.
  213. return true;
  214. }
  215. const int size = 1024;
  216. StringBuilder str = new StringBuilder(size);
  217. int iRet = UnsafeNativeMethods.GetCredentialFromRegistry(value, str, size);
  218. if (iRet == 0) {
  219. value = str.ToString();
  220. return true;
  221. }
  222. else {
  223. if (throwIfError) {
  224. throw new ConfigurationErrorsException(
  225. SR.GetString(SR.Invalid_registry_config));
  226. }
  227. else {
  228. return false;
  229. }
  230. }
  231. }
  232. internal static bool CheckAndReadConnectionString(ref string connectionString, bool throwIfError) {
  233. ConnectionStringSettings connObj = RuntimeConfig.GetConfig().ConnectionStrings.ConnectionStrings[connectionString];
  234. if (connObj != null && connObj.ConnectionString != null && connObj.ConnectionString.Length > 0)
  235. connectionString = connObj.ConnectionString;
  236. return CheckAndReadRegistryValue(ref connectionString, throwIfError);
  237. }
  238. }
  239. }