| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- //
- // RangeHeaderValue.cs
- //
- // Authors:
- // Marek Safar <[email protected]>
- //
- // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Collections.Generic;
- using System.Text;
- namespace System.Net.Http.Headers
- {
- public class RangeHeaderValue : ICloneable
- {
- List<RangeItemHeaderValue> ranges;
- string unit;
- public RangeHeaderValue ()
- {
- unit = "bytes";
- }
- public RangeHeaderValue (long? from, long? to)
- : this ()
- {
- Ranges.Add (new RangeItemHeaderValue (from, to));
- }
- private RangeHeaderValue (RangeHeaderValue source)
- : this ()
- {
- if (source.ranges != null) {
- foreach (var item in source.ranges)
- Ranges.Add (item);
- }
- }
- public ICollection<RangeItemHeaderValue> Ranges {
- get {
- return ranges ?? (ranges = new List<RangeItemHeaderValue> ());
- }
- }
- public string Unit {
- get {
- return unit;
- }
- set {
- if (value == null)
- throw new ArgumentNullException ("Unit");
- Parser.Token.Check (value);
- unit = value;
- }
- }
- object ICloneable.Clone ()
- {
- return new RangeHeaderValue (this);
- }
- public override bool Equals (object obj)
- {
- var source = obj as RangeHeaderValue;
- if (source == null)
- return false;
- return string.Equals (source.Unit, Unit, StringComparison.OrdinalIgnoreCase) &&
- source.ranges.SequenceEqual (ranges);
- }
- public override int GetHashCode ()
- {
- return Unit.ToLowerInvariant ().GetHashCode () ^ HashCodeCalculator.Calculate (ranges);
- }
- public static RangeHeaderValue Parse (string input)
- {
- RangeHeaderValue value;
- if (TryParse (input, out value))
- return value;
- throw new FormatException (input);
- }
- public static bool TryParse (string input, out RangeHeaderValue parsedValue)
- {
- parsedValue = null;
- var lexer = new Lexer (input);
- var t = lexer.Scan ();
- if (t != Token.Type.Token)
- return false;
- var value = new RangeHeaderValue ();
- value.unit = lexer.GetStringValue (t);
- t = lexer.Scan ();
- if (t != Token.Type.SeparatorEqual)
- return false;
- bool token_read;
- do {
- long? from = null, to = null;
- long number;
- token_read = false;
- t = lexer.Scan (recognizeDash: true);
- switch (t.Kind) {
- case Token.Type.SeparatorDash:
- t = lexer.Scan ();
- if (!lexer.TryGetNumericValue (t, out number))
- return false;
- to = number;
- break;
- case Token.Type.Token:
- string s = lexer.GetStringValue (t);
- var values = s.Split (new [] { '-' }, StringSplitOptions.RemoveEmptyEntries);
- if (!Parser.Long.TryParse (values[0], out number))
- return false;
- switch (values.Length) {
- case 1:
- t = lexer.Scan (recognizeDash: true);
- from = number;
- switch (t.Kind) {
- case Token.Type.SeparatorDash:
- t = lexer.Scan ();
- if (t != Token.Type.Token) {
- token_read = true;
- break;
- }
- if (!lexer.TryGetNumericValue (t, out number))
- return false;
- to = number;
- if (to < from)
- return false;
- break;
- case Token.Type.End:
- if (s.Length > 0 && s [s.Length - 1] != '-')
- return false;
- token_read = true;
- break;
- case Token.Type.SeparatorComma:
- token_read = true;
- break;
- default:
- return false;
- }
- break;
- case 2:
- from = number;
- if (!Parser.Long.TryParse (values[1], out number))
- return false;
- to = number;
- if (to < from)
- return false;
- break;
- default:
- return false;
- }
- break;
- default:
- return false;
- }
- value.Ranges.Add (new RangeItemHeaderValue (from, to));
- if (!token_read)
- t = lexer.Scan ();
- } while (t == Token.Type.SeparatorComma);
- if (t != Token.Type.End)
- return false;
- parsedValue = value;
- return true;
- }
- public override string ToString ()
- {
- var sb = new StringBuilder (unit);
- sb.Append ("=");
- for (int i = 0; i < Ranges.Count; ++i) {
- if (i > 0)
- sb.Append (", ");
- sb.Append (ranges[i]);
- }
- return sb.ToString ();
- }
- }
- }
|