RequestValidator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 RequestValidator current;
  36. static Lazy <RequestValidator> lazyLoader;
  37. // The stack trace from .NET shows it uses Lazy <T>:
  38. //
  39. // Server stack trace:
  40. // at System.Web.Configuration.ConfigUtil.GetType(String typeName, String propertyName, ConfigurationElement configElement, XmlNode node, Boolean checkAptcaBit, Boolean ignoreCase)
  41. // at System.Web.Util.RequestValidator.GetCustomValidatorFromConfig()
  42. // at System.Lazy`1.CreateValue()
  43. //
  44. public static RequestValidator Current {
  45. get {
  46. if (current == null)
  47. current = lazyLoader.Value;
  48. return current;
  49. }
  50. set {
  51. if (value == null)
  52. throw new ArgumentNullException ("value");
  53. current = value;
  54. }
  55. }
  56. static RequestValidator ()
  57. {
  58. lazyLoader = new Lazy <RequestValidator> (new Func <RequestValidator> (LoadConfiguredValidator));
  59. }
  60. public RequestValidator ()
  61. {
  62. }
  63. protected internal virtual bool IsValidRequestString (HttpContext context, string value, RequestValidationSource requestValidationSource,
  64. string collectionKey, out int validationFailureIndex)
  65. {
  66. validationFailureIndex = 0;
  67. return !HttpRequest.IsInvalidString (value, out validationFailureIndex);
  68. }
  69. static void ParseTypeName (string spec, out string typeName, out string assemblyName)
  70. {
  71. try {
  72. if (String.IsNullOrEmpty (spec)) {
  73. typeName = null;
  74. assemblyName = null;
  75. return;
  76. }
  77. int comma = spec.IndexOf (',');
  78. if (comma == -1) {
  79. typeName = spec;
  80. assemblyName = null;
  81. return;
  82. }
  83. typeName = spec.Substring (0, comma).Trim ();
  84. assemblyName = spec.Substring (comma + 1).Trim ();
  85. } catch {
  86. typeName = spec;
  87. assemblyName = null;
  88. }
  89. }
  90. static RequestValidator LoadConfiguredValidator ()
  91. {
  92. HttpRuntimeSection runtimeConfig = HttpRuntime.Section;
  93. Type validatorType = null;
  94. string typeSpec = runtimeConfig.RequestValidationType;
  95. try {
  96. validatorType = HttpApplication.LoadType <RequestValidator> (typeSpec, true);
  97. } catch (TypeLoadException ex) {
  98. string typeName, assemblyName;
  99. ParseTypeName (typeSpec, out typeName, out assemblyName);
  100. throw new ConfigurationErrorsException (
  101. String.Format ("Could not load type '{0}' from assembly '{1}'.", typeName, assemblyName),
  102. ex);
  103. }
  104. return (RequestValidator) Activator.CreateInstance (validatorType);
  105. }
  106. }
  107. }