ProductInfoHeaderValue.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // ProductInfoHeaderValue.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections.Generic;
  29. namespace System.Net.Http.Headers
  30. {
  31. public class ProductInfoHeaderValue : ICloneable
  32. {
  33. public ProductInfoHeaderValue (ProductHeaderValue product)
  34. {
  35. if (product == null)
  36. throw new ArgumentNullException ();
  37. Product = product;
  38. }
  39. public ProductInfoHeaderValue (string comment)
  40. {
  41. Parser.Token.CheckComment (comment);
  42. Comment = comment;
  43. }
  44. public ProductInfoHeaderValue (string productName, string productVersion)
  45. {
  46. Product = new ProductHeaderValue (productName, productVersion);
  47. }
  48. private ProductInfoHeaderValue ()
  49. {
  50. }
  51. public string Comment { get; private set; }
  52. public ProductHeaderValue Product { get; private set; }
  53. object ICloneable.Clone ()
  54. {
  55. return MemberwiseClone ();
  56. }
  57. public override bool Equals (object obj)
  58. {
  59. var source = obj as ProductInfoHeaderValue;
  60. if (source == null)
  61. return false;
  62. return Product != null ?
  63. Product.Equals (source.Product) :
  64. source.Comment == Comment;
  65. }
  66. public override int GetHashCode ()
  67. {
  68. return Product != null ?
  69. Product.GetHashCode () :
  70. Comment.GetHashCode ();
  71. }
  72. public static ProductInfoHeaderValue Parse (string input)
  73. {
  74. ProductInfoHeaderValue value;
  75. if (TryParse (input, out value))
  76. return value;
  77. throw new FormatException (input);
  78. }
  79. public static bool TryParse (string input, out ProductInfoHeaderValue parsedValue)
  80. {
  81. parsedValue = null;
  82. var lexer = new Lexer (input);
  83. if (!TryParseElement (lexer, out parsedValue) || parsedValue == null)
  84. return false;
  85. if (lexer.Scan () != Token.Type.End) {
  86. parsedValue = null;
  87. return false;
  88. }
  89. return true;
  90. }
  91. internal static bool TryParse (string input, int minimalCount, out List<ProductInfoHeaderValue> result)
  92. {
  93. var list = new List<ProductInfoHeaderValue> ();
  94. var lexer = new Lexer (input);
  95. result = null;
  96. while (true) {
  97. ProductInfoHeaderValue element;
  98. if (!TryParseElement (lexer, out element))
  99. return false;
  100. if (element == null) {
  101. if (list != null && minimalCount <= list.Count) {
  102. result = list;
  103. return true;
  104. }
  105. return false;
  106. }
  107. list.Add (element);
  108. // Separator parsing
  109. switch (lexer.PeekChar ()) {
  110. case ' ':
  111. case '\t':
  112. lexer.EatChar ();
  113. continue;
  114. case -1:
  115. if (minimalCount <= list.Count) {
  116. result = list;
  117. return true;
  118. }
  119. break;
  120. }
  121. return false;
  122. }
  123. }
  124. static bool TryParseElement (Lexer lexer, out ProductInfoHeaderValue parsedValue)
  125. {
  126. string comment;
  127. parsedValue = null;
  128. Token t;
  129. if (lexer.ScanCommentOptional (out comment, out t)) {
  130. if (comment == null)
  131. return false;
  132. parsedValue = new ProductInfoHeaderValue ();
  133. parsedValue.Comment = comment;
  134. return true;
  135. }
  136. if (t == Token.Type.End)
  137. return true;
  138. if (t != Token.Type.Token)
  139. return false;
  140. var value = new ProductHeaderValue ();
  141. value.Name = lexer.GetStringValue (t);
  142. var pos = lexer.Position;
  143. t = lexer.Scan ();
  144. if (t == Token.Type.SeparatorSlash) {
  145. t = lexer.Scan ();
  146. if (t != Token.Type.Token)
  147. return false;
  148. value.Version = lexer.GetStringValue (t);
  149. } else {
  150. lexer.Position = pos;
  151. }
  152. parsedValue = new ProductInfoHeaderValue (value);
  153. return true;
  154. }
  155. public override string ToString ()
  156. {
  157. if (Product == null)
  158. return Comment;
  159. return Product.ToString ();
  160. }
  161. }
  162. }