RangeHeaderValue.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // RangeHeaderValue.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. using System.Text;
  30. namespace System.Net.Http.Headers
  31. {
  32. public class RangeHeaderValue : ICloneable
  33. {
  34. List<RangeItemHeaderValue> ranges;
  35. string unit;
  36. public RangeHeaderValue ()
  37. {
  38. unit = "bytes";
  39. }
  40. public RangeHeaderValue (long? from, long? to)
  41. : this ()
  42. {
  43. Ranges.Add (new RangeItemHeaderValue (from, to));
  44. }
  45. private RangeHeaderValue (RangeHeaderValue source)
  46. : this ()
  47. {
  48. if (source.ranges != null) {
  49. foreach (var item in source.ranges)
  50. Ranges.Add (item);
  51. }
  52. }
  53. public ICollection<RangeItemHeaderValue> Ranges {
  54. get {
  55. return ranges ?? (ranges = new List<RangeItemHeaderValue> ());
  56. }
  57. }
  58. public string Unit {
  59. get {
  60. return unit;
  61. }
  62. set {
  63. if (value == null)
  64. throw new ArgumentNullException ("Unit");
  65. Parser.Token.Check (value);
  66. unit = value;
  67. }
  68. }
  69. object ICloneable.Clone ()
  70. {
  71. return new RangeHeaderValue (this);
  72. }
  73. public override bool Equals (object obj)
  74. {
  75. var source = obj as RangeHeaderValue;
  76. if (source == null)
  77. return false;
  78. return string.Equals (source.Unit, Unit, StringComparison.OrdinalIgnoreCase) &&
  79. source.ranges.SequenceEqual (ranges);
  80. }
  81. public override int GetHashCode ()
  82. {
  83. return Unit.ToLowerInvariant ().GetHashCode () ^ HashCodeCalculator.Calculate (ranges);
  84. }
  85. public static RangeHeaderValue Parse (string input)
  86. {
  87. RangeHeaderValue value;
  88. if (TryParse (input, out value))
  89. return value;
  90. throw new FormatException (input);
  91. }
  92. public static bool TryParse (string input, out RangeHeaderValue parsedValue)
  93. {
  94. parsedValue = null;
  95. var lexer = new Lexer (input);
  96. var t = lexer.Scan ();
  97. if (t != Token.Type.Token)
  98. return false;
  99. var value = new RangeHeaderValue ();
  100. value.unit = lexer.GetStringValue (t);
  101. t = lexer.Scan ();
  102. if (t != Token.Type.SeparatorEqual)
  103. return false;
  104. bool token_read;
  105. do {
  106. long? from = null, to = null;
  107. long number;
  108. token_read = false;
  109. t = lexer.Scan (recognizeDash: true);
  110. switch (t.Kind) {
  111. case Token.Type.SeparatorDash:
  112. t = lexer.Scan ();
  113. if (!lexer.TryGetNumericValue (t, out number))
  114. return false;
  115. to = number;
  116. break;
  117. case Token.Type.Token:
  118. string s = lexer.GetStringValue (t);
  119. var values = s.Split (new [] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  120. if (!Parser.Long.TryParse (values[0], out number))
  121. return false;
  122. switch (values.Length) {
  123. case 1:
  124. t = lexer.Scan (recognizeDash: true);
  125. from = number;
  126. switch (t.Kind) {
  127. case Token.Type.SeparatorDash:
  128. t = lexer.Scan ();
  129. if (t != Token.Type.Token) {
  130. token_read = true;
  131. break;
  132. }
  133. if (!lexer.TryGetNumericValue (t, out number))
  134. return false;
  135. to = number;
  136. if (to < from)
  137. return false;
  138. break;
  139. case Token.Type.End:
  140. if (s.Length > 0 && s [s.Length - 1] != '-')
  141. return false;
  142. token_read = true;
  143. break;
  144. case Token.Type.SeparatorComma:
  145. token_read = true;
  146. break;
  147. default:
  148. return false;
  149. }
  150. break;
  151. case 2:
  152. from = number;
  153. if (!Parser.Long.TryParse (values[1], out number))
  154. return false;
  155. to = number;
  156. if (to < from)
  157. return false;
  158. break;
  159. default:
  160. return false;
  161. }
  162. break;
  163. default:
  164. return false;
  165. }
  166. value.Ranges.Add (new RangeItemHeaderValue (from, to));
  167. if (!token_read)
  168. t = lexer.Scan ();
  169. } while (t == Token.Type.SeparatorComma);
  170. if (t != Token.Type.End)
  171. return false;
  172. parsedValue = value;
  173. return true;
  174. }
  175. public override string ToString ()
  176. {
  177. var sb = new StringBuilder (unit);
  178. sb.Append ("=");
  179. for (int i = 0; i < Ranges.Count; ++i) {
  180. if (i > 0)
  181. sb.Append (", ");
  182. sb.Append (ranges[i]);
  183. }
  184. return sb.ToString ();
  185. }
  186. }
  187. }