PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/System.Net.Http/System/Net/Http/Headers/ProductInfoHeaderValue.cs

#
C# | 176 lines | 139 code | 30 blank | 7 comment | 23 complexity | dec9d7cd30f9a6d8aad32a4fab77aa2f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0
  1. using System.Diagnostics.Contracts;
  2. namespace System.Net.Http.Headers
  3. {
  4. public class ProductInfoHeaderValue : ICloneable
  5. {
  6. private ProductHeaderValue product;
  7. private string comment;
  8. public ProductHeaderValue Product
  9. {
  10. get { return product; }
  11. }
  12. public string Comment
  13. {
  14. get { return comment; }
  15. }
  16. public ProductInfoHeaderValue(string productName, string productVersion)
  17. : this(new ProductHeaderValue(productName, productVersion))
  18. {
  19. }
  20. public ProductInfoHeaderValue(ProductHeaderValue product)
  21. {
  22. if (product == null)
  23. {
  24. throw new ArgumentNullException("product");
  25. }
  26. this.product = product;
  27. }
  28. public ProductInfoHeaderValue(string comment)
  29. {
  30. HeaderUtilities.CheckValidComment(comment, "comment");
  31. this.comment = comment;
  32. }
  33. private ProductInfoHeaderValue(ProductInfoHeaderValue source)
  34. {
  35. Contract.Requires(source != null);
  36. this.product = source.product;
  37. this.comment = source.comment;
  38. }
  39. private ProductInfoHeaderValue()
  40. {
  41. }
  42. public override string ToString()
  43. {
  44. if (product == null)
  45. {
  46. return comment;
  47. }
  48. return product.ToString();
  49. }
  50. public override bool Equals(object obj)
  51. {
  52. ProductInfoHeaderValue other = obj as ProductInfoHeaderValue;
  53. if (other == null)
  54. {
  55. return false;
  56. }
  57. if (product == null)
  58. {
  59. // We compare comments using case-sensitive comparison.
  60. return string.CompareOrdinal(comment, other.comment) == 0;
  61. }
  62. return product.Equals(other.product);
  63. }
  64. public override int GetHashCode()
  65. {
  66. if (product == null)
  67. {
  68. return comment.GetHashCode();
  69. }
  70. return product.GetHashCode();
  71. }
  72. public static ProductInfoHeaderValue Parse(string input)
  73. {
  74. int index = 0;
  75. object result = ProductInfoHeaderParser.SingleValueParser.ParseValue(
  76. input, null, ref index);
  77. if (index < input.Length)
  78. {
  79. // There is some leftover data, invalid. Normaly BaseHeaderParser.TryParseValue would
  80. // handle this, but ProductInfoHeaderValue does not derive from BaseHeaderParser.
  81. throw new FormatException(string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_headers_invalid_value, input.Substring(index)));
  82. }
  83. return (ProductInfoHeaderValue)result;
  84. }
  85. public static bool TryParse(string input, out ProductInfoHeaderValue parsedValue)
  86. {
  87. int index = 0;
  88. object output;
  89. parsedValue = null;
  90. if (ProductInfoHeaderParser.SingleValueParser.TryParseValue(input, null, ref index, out output))
  91. {
  92. if (index < input.Length)
  93. {
  94. // There is some leftover data, invalid. Normaly BaseHeaderParser.TryParseValue would
  95. // handle this, but ProductInfoHeaderValue does not derive from BaseHeaderParser.
  96. return false;
  97. }
  98. parsedValue = (ProductInfoHeaderValue)output;
  99. return true;
  100. }
  101. return false;
  102. }
  103. internal static int GetProductInfoLength(string input, int startIndex, out ProductInfoHeaderValue parsedValue)
  104. {
  105. Contract.Requires(startIndex >= 0);
  106. parsedValue = null;
  107. if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
  108. {
  109. return 0;
  110. }
  111. int current = startIndex;
  112. // Caller must remove leading whitespaces.
  113. string comment = null;
  114. ProductHeaderValue product = null;
  115. if (input[current] == '(')
  116. {
  117. int commentLength = 0;
  118. if (HttpRuleParser.GetCommentLength(input, current, out commentLength) != HttpParseResult.Parsed)
  119. {
  120. return 0;
  121. }
  122. comment = input.Substring(current, commentLength);
  123. current = current + commentLength;
  124. current = current + HttpRuleParser.GetWhitespaceLength(input, current);
  125. }
  126. else
  127. {
  128. // trailing whitespaces are removed by GetProductLength()
  129. int productLength = ProductHeaderValue.GetProductLength(input, current, out product);
  130. if (productLength == 0)
  131. {
  132. return 0;
  133. }
  134. current = current + productLength;
  135. }
  136. parsedValue = new ProductInfoHeaderValue();
  137. parsedValue.product = product;
  138. parsedValue.comment = comment;
  139. return current - startIndex;
  140. }
  141. object ICloneable.Clone()
  142. {
  143. return new ProductInfoHeaderValue(this);
  144. }
  145. }
  146. }