2
0

RangeHeaderValue.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. int? from = null, to = null;
  107. int number;
  108. token_read = false;
  109. t = lexer.Scan ();
  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 (!int.TryParse (values[0], out number))
  121. return false;
  122. switch (values.Length) {
  123. case 1:
  124. t = lexer.Scan ();
  125. switch (t.Kind) {
  126. case Token.Type.SeparatorDash:
  127. from = number;
  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. default:
  140. return false;
  141. }
  142. break;
  143. case 2:
  144. from = number;
  145. if (!int.TryParse (values[1], out number))
  146. return false;
  147. to = number;
  148. if (to < from)
  149. return false;
  150. break;
  151. default:
  152. return false;
  153. }
  154. break;
  155. default:
  156. return false;
  157. }
  158. value.Ranges.Add (new RangeItemHeaderValue (from, to));
  159. if (!token_read)
  160. t = lexer.Scan ();
  161. } while (t == Token.Type.SeparatorComma);
  162. if (t != Token.Type.End)
  163. return false;
  164. parsedValue = value;
  165. return true;
  166. }
  167. public override string ToString ()
  168. {
  169. var sb = new StringBuilder (unit);
  170. sb.Append ("=");
  171. for (int i = 0; i < Ranges.Count; ++i) {
  172. if (i > 0)
  173. sb.Append (", ");
  174. sb.Append (ranges[i]);
  175. }
  176. return sb.ToString ();
  177. }
  178. }
  179. }