RequestValidator.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // Copyright (C) 2010 Novell, Inc (http://novell.com/)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Configuration;
  29. using System.Web;
  30. using System.Web.Configuration;
  31. namespace System.Web.Util
  32. {
  33. public class RequestValidator
  34. {
  35. static readonly object currentCreationLock = new object();
  36. static RequestValidator current;
  37. static Lazy <RequestValidator> lazyLoader;
  38. // The stack trace from .NET shows it uses Lazy <T>:
  39. //
  40. // Server stack trace:
  41. // at System.Web.Configuration.ConfigUtil.GetType(String typeName, String propertyName, ConfigurationElement configElement, XmlNode node, Boolean checkAptcaBit, Boolean ignoreCase)
  42. // at System.Web.Util.RequestValidator.GetCustomValidatorFromConfig()
  43. // at System.Lazy`1.CreateValue()
  44. //
  45. public static RequestValidator Current {
  46. get {
  47. if (current == null)
  48. current = lazyLoader.Value;
  49. return current;
  50. }
  51. set {
  52. if (value == null)
  53. throw new ArgumentNullException ("value");
  54. current = value;
  55. }
  56. }
  57. static RequestValidator ()
  58. {
  59. lazyLoader = new Lazy <RequestValidator> (new Func <RequestValidator> (LoadConfiguredValidator));
  60. }
  61. public RequestValidator ()
  62. {
  63. }
  64. protected internal virtual bool IsValidRequestString (HttpContext context, string value, RequestValidationSource requestValidationSource,
  65. string collectionKey, out int validationFailureIndex)
  66. {
  67. validationFailureIndex = 0;
  68. return !HttpRequest.IsInvalidString (value, out validationFailureIndex);
  69. }
  70. static void ParseTypeName (string spec, out string typeName, out string assemblyName)
  71. {
  72. try {
  73. if (String.IsNullOrEmpty (spec)) {
  74. typeName = null;
  75. assemblyName = null;
  76. return;
  77. }
  78. int comma = spec.IndexOf (',');
  79. if (comma == -1) {
  80. typeName = spec;
  81. assemblyName = null;
  82. return;
  83. }
  84. typeName = spec.Substring (0, comma).Trim ();
  85. assemblyName = spec.Substring (comma + 1).Trim ();
  86. } catch {
  87. typeName = spec;
  88. assemblyName = null;
  89. }
  90. }
  91. static RequestValidator LoadConfiguredValidator ()
  92. {
  93. HttpRuntimeSection runtimeConfig = WebConfigurationManager.GetWebApplicationSection ("system.web/httpRuntime") as HttpRuntimeSection;
  94. Type validatorType = null;
  95. string typeSpec = runtimeConfig.RequestValidationType;
  96. try {
  97. validatorType = HttpApplication.LoadType <RequestValidator> (typeSpec, true);
  98. } catch (TypeLoadException ex) {
  99. string typeName, assemblyName;
  100. ParseTypeName (typeSpec, out typeName, out assemblyName);
  101. throw new ConfigurationErrorsException (
  102. String.Format ("Could not load type '{0}' from assembly '{1}'.", typeName, assemblyName),
  103. ex);
  104. }
  105. return (RequestValidator) Activator.CreateInstance (validatorType);
  106. }
  107. }
  108. }