PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/System.Net.Http/System/Net/Http/Headers/TransferCodingHeaderValue.cs

#
C# | 157 lines | 123 code | 28 blank | 6 comment | 19 complexity | b27c6542005505e863cc1fd0c8bc50bd MD5 | raw file
  1using System.Collections.Generic;
  2using System.Diagnostics.Contracts;
  3
  4namespace System.Net.Http.Headers
  5{
  6    public class TransferCodingHeaderValue : ICloneable
  7    {
  8        // Use list instead of dictionary since we may have multiple parameters with the same name.
  9        private ICollection<NameValueHeaderValue> parameters;
 10        private string value;
 11
 12        public string Value 
 13        {
 14            get { return value; }
 15        }
 16
 17        public ICollection<NameValueHeaderValue> Parameters
 18        {
 19            get
 20            {
 21                if (parameters == null)
 22                {
 23                    parameters = new ObjectCollection<NameValueHeaderValue>();
 24                }
 25                return parameters;
 26            }
 27        }
 28
 29        internal TransferCodingHeaderValue()
 30        {
 31        }
 32
 33        protected TransferCodingHeaderValue(TransferCodingHeaderValue source)
 34        {
 35            Contract.Requires(source != null);
 36
 37            this.value = source.value;
 38
 39            if (source.parameters != null)
 40            {
 41                foreach (var parameter in source.parameters)
 42                {
 43                    this.Parameters.Add((NameValueHeaderValue)((ICloneable)parameter).Clone());
 44                }
 45            }
 46        }
 47
 48        public TransferCodingHeaderValue(string value)
 49        {
 50            HeaderUtilities.CheckValidToken(value, "value");
 51            this.value = value;
 52        }
 53
 54        public static TransferCodingHeaderValue Parse(string input)
 55        {
 56            int index = 0;
 57            return (TransferCodingHeaderValue)TransferCodingHeaderParser.SingleValueParser.ParseValue(
 58                input, null, ref index);
 59        }
 60
 61        public static bool TryParse(string input, out TransferCodingHeaderValue parsedValue)
 62        {
 63            int index = 0;
 64            object output;
 65            parsedValue = null;
 66
 67            if (TransferCodingHeaderParser.SingleValueParser.TryParseValue(input, null, ref index, out output))
 68            {
 69                parsedValue = (TransferCodingHeaderValue)output;
 70                return true;
 71            }
 72            return false;
 73        }
 74        
 75        internal static int GetTransferCodingLength(string input, int startIndex, 
 76            Func<TransferCodingHeaderValue> transferCodingCreator, out TransferCodingHeaderValue parsedValue)
 77        {
 78            Contract.Requires(transferCodingCreator != null);
 79            Contract.Requires(startIndex >= 0);
 80
 81            parsedValue = null;
 82
 83            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
 84            {
 85                return 0;
 86            }
 87
 88            // Caller must remove leading whitespaces. If not, we'll return 0.
 89            int valueLength = HttpRuleParser.GetTokenLength(input, startIndex);
 90            
 91            if (valueLength == 0)
 92            {
 93                return 0;
 94            }
 95
 96            string value = input.Substring(startIndex, valueLength);
 97            int current = startIndex + valueLength;
 98            current = current + HttpRuleParser.GetWhitespaceLength(input, current);
 99            TransferCodingHeaderValue transferCodingHeader = null;
100
101            // If we're not done and we have a parameter delimiter, then we have a list of parameters.
102            if ((current < input.Length) && (input[current] == ';'))
103            {
104                transferCodingHeader = transferCodingCreator();
105                transferCodingHeader.value = value;
106
107                current++; // skip delimiter.
108                int parameterLength = NameValueHeaderValue.GetNameValueListLength(input, current, ';',
109                    transferCodingHeader.Parameters);
110
111                if (parameterLength == 0)
112                {
113                    return 0;
114                }
115
116                parsedValue = transferCodingHeader;
117                return current + parameterLength - startIndex;
118            }
119
120            // We have a transfer coding without parameters.
121            transferCodingHeader = transferCodingCreator();
122            transferCodingHeader.value = value;
123            parsedValue = transferCodingHeader;
124            return current - startIndex;
125        }
126
127        public override string ToString()
128        {
129            return value + NameValueHeaderValue.ToString(parameters, ';', true);
130        }
131
132        public override bool Equals(object obj)
133        {
134            TransferCodingHeaderValue other = obj as TransferCodingHeaderValue;
135
136            if (other == null)
137            {
138                return false;
139            }
140
141            return (string.Compare(value, other.value, StringComparison.OrdinalIgnoreCase) == 0) &&
142                HeaderUtilities.AreEqualCollections(parameters, other.parameters);
143        }
144
145        public override int GetHashCode()
146        {
147            // The value string is case-insensitive.
148            return value.ToLowerInvariant().GetHashCode() ^ NameValueHeaderValue.GetHashCode(parameters);
149        }
150
151        // Implement ICloneable explicitly to allow derived types to "override" the implementation.
152        object ICloneable.Clone()
153        {
154            return new TransferCodingHeaderValue(this);
155        }
156    }
157}