2 // ContentRangeHeaderValue.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
31 using System.Globalization;
33 namespace System.Net.Http.Headers
35 public class ContentRangeHeaderValue : ICloneable
37 string unit = "bytes";
39 private ContentRangeHeaderValue ()
43 public ContentRangeHeaderValue (long length)
46 throw new ArgumentOutOfRangeException ("length");
51 public ContentRangeHeaderValue (long from, long to)
53 if (from < 0 || from > to)
54 throw new ArgumentOutOfRangeException ("from");
60 public ContentRangeHeaderValue (long from, long to, long length)
64 throw new ArgumentOutOfRangeException ("length");
67 throw new ArgumentOutOfRangeException ("to");
72 public long? From { get; private set; }
74 public bool HasLength {
76 return Length != null;
80 public bool HasRange {
86 public long? Length { get; private set; }
87 public long? To { get; private set; }
95 throw new ArgumentNullException ("Unit");
97 Parser.Token.Check (value);
103 object ICloneable.Clone ()
105 return MemberwiseClone ();
108 public override bool Equals (object obj)
110 var source = obj as ContentRangeHeaderValue;
114 return source.Length == Length && source.From == From && source.To == To &&
115 string.Equals (source.unit, unit, StringComparison.OrdinalIgnoreCase);
118 public override int GetHashCode ()
120 return Unit.GetHashCode () ^ Length.GetHashCode () ^
121 From.GetHashCode () ^ To.GetHashCode () ^
122 unit.ToLowerInvariant ().GetHashCode ();
125 public static ContentRangeHeaderValue Parse (string input)
127 ContentRangeHeaderValue value;
128 if (TryParse (input, out value))
131 throw new FormatException (input);
134 public static bool TryParse (string input, out ContentRangeHeaderValue parsedValue)
138 var lexer = new Lexer (input);
139 var t = lexer.Scan ();
140 if (t != Token.Type.Token)
143 var value = new ContentRangeHeaderValue ();
144 value.unit = lexer.GetStringValue (t);
147 if (t != Token.Type.Token)
151 if (!lexer.IsStarStringValue (t)) {
152 if (!lexer.TryGetNumericValue (t, out nvalue)) {
153 var s = lexer.GetStringValue (t);
157 var sep = s.Split ('-');
161 if (!int.TryParse (sep[0], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
166 if (!int.TryParse (sep[1], NumberStyles.None, CultureInfo.InvariantCulture, out nvalue))
174 if (t != Token.Type.SeparatorDash)
179 if (!lexer.TryGetNumericValue (t, out nvalue))
188 if (t != Token.Type.SeparatorSlash)
193 if (!lexer.IsStarStringValue (t)) {
194 if (!lexer.TryGetNumericValue (t, out nvalue))
197 value.Length = nvalue;
202 if (t != Token.Type.End)
210 public override string ToString ()
212 var sb = new StringBuilder (unit);
217 sb.Append (From.Value.ToString (CultureInfo.InvariantCulture));
219 sb.Append (To.Value.ToString (CultureInfo.InvariantCulture));
223 sb.Append (Length == null ? "*" :
224 Length.Value.ToString (CultureInfo.InvariantCulture));
226 return sb.ToString ();