ValidationHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //Copyright 2010 Microsoft Corporation
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. //
  6. //http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  9. //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. //See the License for the specific language governing permissions and limitations under the License.
  11. namespace System.Data.Services.Http
  12. {
  13. using System;
  14. using System.Diagnostics;
  15. internal static class ValidationHelper
  16. {
  17. private static readonly char[] HttpTrimCharacters = new char[] { '\t', '\n', '\v', '\f', '\r', ' ' };
  18. private static readonly char[] InvalidParamChars = new char[]
  19. {
  20. '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '\'', '/', '[', ']', '?', '=', '{', '}', ' ', '\t', '\r', '\n'
  21. };
  22. internal static string CheckBadChars(string name, bool isHeaderValue)
  23. {
  24. if (String.IsNullOrEmpty(name))
  25. {
  26. if (!isHeaderValue)
  27. {
  28. if (name == null)
  29. {
  30. throw new ArgumentNullException("name");
  31. }
  32. else
  33. {
  34. throw new InvalidOperationException(
  35. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars.7", name));
  36. }
  37. }
  38. return string.Empty;
  39. }
  40. if (isHeaderValue)
  41. {
  42. name = name.Trim(HttpTrimCharacters);
  43. int crlf = 0;
  44. for (int i = 0; i < name.Length; i++)
  45. {
  46. char c = (char)('\x00ff' & name[i]);
  47. switch (crlf)
  48. {
  49. case 0:
  50. {
  51. if (c != '\r')
  52. {
  53. break;
  54. }
  55. crlf = 1;
  56. continue;
  57. }
  58. case 1:
  59. {
  60. if (c != '\n')
  61. {
  62. throw new InvalidOperationException(
  63. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars", name));
  64. }
  65. crlf = 2;
  66. continue;
  67. }
  68. case 2:
  69. {
  70. if ((c != ' ') && (c != '\t'))
  71. {
  72. throw new InvalidOperationException(
  73. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars.2", name));
  74. }
  75. crlf = 0;
  76. continue;
  77. }
  78. default:
  79. {
  80. continue;
  81. }
  82. }
  83. if (c == '\n')
  84. {
  85. crlf = 2;
  86. }
  87. else if ((c == '\x007f') || ((c < ' ') && (c != '\t')))
  88. {
  89. throw new InvalidOperationException(
  90. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars.3", name));
  91. }
  92. }
  93. if (crlf != 0)
  94. {
  95. throw new InvalidOperationException(
  96. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars.4", name));
  97. }
  98. return name;
  99. }
  100. if (name.IndexOfAny(InvalidParamChars) != -1)
  101. {
  102. throw new InvalidOperationException(
  103. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars.5", name));
  104. }
  105. if (ContainsNonAsciiChars(name))
  106. {
  107. throw new InvalidOperationException(
  108. System.Data.Services.Client.Strings.HttpWeb_InternalArgument("ValidationHelper.CheckBadChars.6", name));
  109. }
  110. return name;
  111. }
  112. private static bool ContainsNonAsciiChars(string token)
  113. {
  114. Debug.Assert(token != null, "token != null");
  115. for (int i = 0; i < token.Length; i++)
  116. {
  117. if ((token[i] < ' ') || (token[i] > '~'))
  118. {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. }